diff --git a/pkg/config/config.go b/pkg/config/config.go index 493c1fffe..56ea80eef 100644 --- a/pkg/config/config.go +++ b/pkg/config/config.go @@ -387,29 +387,43 @@ func (c *Config) String() string { return string(yamlData) } +// ToYaml returns a YAML document +// It serializes the given Config object to a valid YAML document func (c *Config) ToYaml() ([]byte, error) { return yaml.Marshal(&c) } +// LoadedConfigPath returns the file path of airship config +// from where the current Config object is created func (c *Config) LoadedConfigPath() string { return c.loadedConfigPath } + +// SetLoadedConfigPath updates the file path of airship config +// in the Config object func (c *Config) SetLoadedConfigPath(lcp string) { c.loadedConfigPath = lcp } +// KubeConfigPath returns the file path of the kube config +// from Config object func (c *Config) KubeConfigPath() string { return c.kubeConfigPath } +// SetKubeConfigPath updates the file path of the kubeconfig +// in Config object func (c *Config) SetKubeConfigPath(kubeConfigPath string) { c.kubeConfigPath = kubeConfigPath } +// KubeConfig returns kube config object from the +// context of current Config object func (c *Config) KubeConfig() *clientcmdapi.Config { return c.kubeConfig } +// SetKubeConfig updates kube config in Config object func (c *Config) SetKubeConfig(kubeConfig *clientcmdapi.Config) { c.kubeConfig = kubeConfig } @@ -428,6 +442,8 @@ func (c *Config) GetCluster(cName, cType string) (*Cluster, error) { return cluster, nil } +// AddCluster creates a new cluster and returns the +// newly created cluster object func (c *Config) AddCluster(theCluster *ClusterOptions) (*Cluster, error) { // Need to create new cluster placeholder // Get list of ClusterPurposes that match the theCluster.name @@ -452,6 +468,7 @@ func (c *Config) AddCluster(theCluster *ClusterOptions) (*Cluster, error) { return c.ModifyCluster(nCluster, theCluster) } +// ModifyCluster updates cluster object with given cluster options func (c *Config) ModifyCluster(cluster *Cluster, theCluster *ClusterOptions) (*Cluster, error) { kcluster := cluster.KubeCluster() if kcluster == nil { @@ -542,6 +559,8 @@ func (c *Config) GetContexts() []*Context { return contexts } +// AddContext creates a new context and returns the instance of +// newly created context func (c *Config) AddContext(theContext *ContextOptions) *Context { // Create the new Airship config context nContext := NewContext() @@ -559,6 +578,7 @@ func (c *Config) AddContext(theContext *ContextOptions) *Context { return nContext } +// ModifyContext updates Context object with given given context options func (c *Config) ModifyContext(context *Context, theContext *ContextOptions) { kubeContext := context.KubeContext() if kubeContext == nil { @@ -596,6 +616,8 @@ func (c *Config) GetCurrentContext() (*Context, error) { } return currentContext, nil } + +// CurrentContextCluster returns the Cluster for the current context func (c *Config) CurrentContextCluster() (*Cluster, error) { currentContext, err := c.GetCurrentContext() if err != nil { @@ -606,6 +628,7 @@ func (c *Config) CurrentContextCluster() (*Cluster, error) { return c.Clusters[clusterName.Name].ClusterTypes[currentContext.ClusterType()], nil } +// CurrentContextAuthInfo returns the AuthInfo for the current context func (c *Config) CurrentContextAuthInfo() (*AuthInfo, error) { currentContext, err := c.GetCurrentContext() if err != nil { @@ -614,6 +637,8 @@ func (c *Config) CurrentContextAuthInfo() (*AuthInfo, error) { return c.AuthInfos[currentContext.KubeContext().AuthInfo], nil } + +// CurrentContextManifest returns the manifest for the current context func (c *Config) CurrentContextManifest() (*Manifest, error) { currentContext, err := c.GetCurrentContext() if err != nil { @@ -687,6 +712,8 @@ func (c *Config) GetAuthInfos() []*AuthInfo { return authInfos } +// AddAuthInfo creates new AuthInfo with context details updated +// in the airship config and kube config func (c *Config) AddAuthInfo(theAuthInfo *AuthInfoOptions) *AuthInfo { // Create the new Airship config context nAuthInfo := NewAuthInfo() @@ -700,6 +727,7 @@ func (c *Config) AddAuthInfo(theAuthInfo *AuthInfoOptions) *AuthInfo { return nAuthInfo } +// ModifyAuthInfo updates the AuthInfo in the Config object func (c *Config) ModifyAuthInfo(authinfo *AuthInfo, theAuthInfo *AuthInfoOptions) { kubeAuthInfo := authinfo.KubeAuthInfo() if kubeAuthInfo == nil { @@ -768,7 +796,6 @@ func (c *Config) Purge() error { return os.Remove(c.loadedConfigPath) } -// Context functions func (c *Context) String() string { cyaml, err := yaml.Marshal(&c) if err != nil { @@ -782,19 +809,24 @@ func (c *Context) String() string { return fmt.Sprintf("%s\n%s", string(cyaml), string(kyaml)) } +// PrettyString returns cluster name in a formatted string func (c *Context) PrettyString() string { clusterName := NewClusterComplexNameFromKubeClusterName(c.NameInKubeconf) return fmt.Sprintf("Context: %s\n%s\n", clusterName.Name, c) } +// KubeContext returns kube context object func (c *Context) KubeContext() *clientcmdapi.Context { return c.context } +// SetKubeContext updates kube contect with given context details func (c *Context) SetKubeContext(kc *clientcmdapi.Context) { c.context = kc } +// ClusterType returns cluster type by extracting the type portion from +// the complex cluster name func (c *Context) ClusterType() string { return NewClusterComplexNameFromKubeClusterName(c.NameInKubeconf).Type } @@ -803,7 +835,6 @@ func (c *Context) ClusterName() string { return NewClusterComplexNameFromKubeClusterName(c.NameInKubeconf).Name } -// Manifest functions func (m *Manifest) String() string { yamlData, err := yaml.Marshal(&m) if err != nil { @@ -830,7 +861,7 @@ func (m *ManagementConfiguration) String() string { return string(yamlData) } -// Container functions +// String returns Container object in a serialized string format func (c *Container) String() string { yamlData, err := yaml.Marshal(&c) if err != nil { @@ -839,7 +870,7 @@ func (c *Container) String() string { return string(yamlData) } -// Builder functions +// String returns Builder object in a serialized string format func (b *Builder) String() string { yamlData, err := yaml.Marshal(&b) if err != nil { diff --git a/pkg/config/options.go b/pkg/config/options.go index 8b2e7d285..7f9c57b5d 100644 --- a/pkg/config/options.go +++ b/pkg/config/options.go @@ -24,6 +24,8 @@ import ( "os" ) +// AuthInfoOptions holds all configurable options for +// authentication information or credential type AuthInfoOptions struct { Name string ClientCertificate string @@ -34,6 +36,7 @@ type AuthInfoOptions struct { EmbedCertData bool } +// ContextOptions holds all configurable options for context type ContextOptions struct { Name string ClusterType string @@ -45,6 +48,7 @@ type ContextOptions struct { Current bool } +// ClusterOptions holds all configurable options for cluster configuration type ClusterOptions struct { Name string ClusterType string @@ -54,6 +58,9 @@ type ClusterOptions struct { EmbedCAData bool } +// Validate checks for the possible authentication values and returns +// Error when invalid value or incompatible choice of values given + // TODO(howell): The following functions are tightly coupled with flags passed // on the command line. We should find a way to remove this coupling, since it // is possible to create (and validate) these objects without using the command @@ -87,6 +94,8 @@ func (o *AuthInfoOptions) Validate() error { return nil } +// Validate checks for the possible context option values and returns +// Error when invalid value or incompatible choice of values given func (o *ContextOptions) Validate() error { if !o.Current && o.Name == "" { return errors.New("you must specify a non-empty context name") @@ -112,6 +121,8 @@ func (o *ContextOptions) Validate() error { return nil } +// Validate checks for the possible cluster option values and returns +// Error when invalid value or incompatible choice of values given func (o *ClusterOptions) Validate() error { if o.Name == "" { return errors.New("you must specify a non-empty cluster name") diff --git a/pkg/config/repo.go b/pkg/config/repo.go index 91551ee4d..4ee8cc05a 100644 --- a/pkg/config/repo.go +++ b/pkg/config/repo.go @@ -27,6 +27,7 @@ import ( "opendev.org/airship/airshipctl/pkg/errors" ) +// Constants for possible repo authentication types const ( SSHAuth = "ssh-key" SSHPass = "ssh-pass" @@ -43,6 +44,9 @@ func (c *RepoCheckout) String() string { return string(yaml) } +// Validate checks for possible values for +// repository checkout and returns Error for incorrect values +// returns nil when there are no errors func (c *RepoCheckout) Validate() error { possibleValues := []string{c.CommitHash, c.Branch, c.Tag, c.RemoteRef} var count int @@ -65,6 +69,7 @@ var ( AllowedAuthTypes = []string{SSHAuth, SSHPass, HTTPBasic} ) +// String returns repository authentication details in string format func (auth *RepoAuth) String() string { yaml, err := yaml.Marshal(&auth) if err != nil { @@ -73,6 +78,9 @@ func (auth *RepoAuth) String() string { return string(yaml) } +// Validate checks for possible values for +// repository authentication and returns Error for incorrect values +// returns nil when there are no errors func (auth *RepoAuth) Validate() error { if !stringInSlice(auth.Type, AllowedAuthTypes) { return ErrAuthTypeNotSupported{} @@ -104,8 +112,7 @@ func stringInSlice(a string, list []string) bool { return false } -// Repository functions - +// String returns repository details in a string format func (repo *Repository) String() string { yaml, err := yaml.Marshal(&repo) if err != nil { @@ -114,6 +121,9 @@ func (repo *Repository) String() string { return string(yaml) } +// Validate check possible values for repository and +// returns Error when incorrect value is given +// retruns nill when there are no errors func (repo *Repository) Validate() error { if repo.URLString == "" { return ErrRepoSpecRequiresURL{} @@ -136,6 +146,8 @@ func (repo *Repository) Validate() error { return nil } +// ToAuth returns an implementation of transport.AuthMethod for +// the given auth type to establish an ssh connection func (repo *Repository) ToAuth() (transport.AuthMethod, error) { if repo.Auth == nil { return nil, nil @@ -152,6 +164,9 @@ func (repo *Repository) ToAuth() (transport.AuthMethod, error) { } } +// ToCheckoutOptions returns an instance of git.CheckoutOptions with +// respective values(Branch/Tag/Hash) in checkout options initialized +// CheckoutOptions describes how a checkout operation should be performed func (repo *Repository) ToCheckoutOptions(force bool) *git.CheckoutOptions { co := &git.CheckoutOptions{ Force: force, @@ -168,6 +183,9 @@ func (repo *Repository) ToCheckoutOptions(force bool) *git.CheckoutOptions { return co } +// ToCloneOptions returns an instance of git.CloneOptions with +// authentication and URL set +// CloneOptions describes how a clone should be performed func (repo *Repository) ToCloneOptions(auth transport.AuthMethod) *git.CloneOptions { return &git.CloneOptions{ Auth: auth, @@ -175,10 +193,13 @@ func (repo *Repository) ToCloneOptions(auth transport.AuthMethod) *git.CloneOpti } } +// ToFetchOptions returns an instance of git.FetchOptions for given authentication +// FetchOptions describes how a fetch should be performed func (repo *Repository) ToFetchOptions(auth transport.AuthMethod) *git.FetchOptions { return &git.FetchOptions{Auth: auth} } +// URL returns the repository URL in a string format func (repo *Repository) URL() string { return repo.URLString } diff --git a/pkg/config/utils.go b/pkg/config/utils.go index ed69b9a28..986700db9 100644 --- a/pkg/config/utils.go +++ b/pkg/config/utils.go @@ -21,6 +21,7 @@ import ( ) const ( + // DefaultTestPrimaryRepo holds default repo name DefaultTestPrimaryRepo = "primary" ) @@ -100,10 +101,12 @@ func NewManifest() *Manifest { } } +// NewRepository is a convenience function that returns a new Repository func NewRepository() *Repository { return &Repository{} } +// NewAuthInfo is a convenience function that returns a new AuthInfo func NewAuthInfo() *AuthInfo { return &AuthInfo{} } diff --git a/pkg/container/container_docker.go b/pkg/container/container_docker.go index 841184e2e..76603d121 100644 --- a/pkg/container/container_docker.go +++ b/pkg/container/container_docker.go @@ -206,6 +206,7 @@ func (c *DockerContainer) getImageID(url string) (string, error) { return img[0].ID, nil } +// GetID returns ID of the container func (c *DockerContainer) GetID() string { return c.id } diff --git a/pkg/document/pull/pull.go b/pkg/document/pull/pull.go index 4715046fe..a09f436ac 100644 --- a/pkg/document/pull/pull.go +++ b/pkg/document/pull/pull.go @@ -19,10 +19,13 @@ import ( "opendev.org/airship/airshipctl/pkg/environment" ) +// Settings is a reference to environment.AirshipCTLSettings +// AirshipCTLSettings is a container for all of the settings needed by airshipctl type Settings struct { *environment.AirshipCTLSettings } +// Pull clones repositories func (s *Settings) Pull() error { err := s.cloneRepositories() if err != nil {