From 342666c8c9d8bbdd84ff0209c366d857023a3f60 Mon Sep 17 00:00:00 2001 From: Vladislav Kuzmin Date: Tue, 28 Apr 2020 02:25:46 +0400 Subject: [PATCH] Improve config package organization pt.2 This is a series of patches that refactor config.go into several smaller modules, each relating specifically to one component of the configuration structure. This patch split bootstrap, context and manifest related part in separate modules. Relates-To: #35 Change-Id: I692ab90a62c54f21157e093af0fbdab86c36a486 --- pkg/config/bootstrap.go | 82 ++++++++++++++++++++++++++++ pkg/config/config.go | 74 ------------------------- pkg/config/context.go | 77 ++++++++++++++++++++++++++ pkg/config/manifest.go | 94 ++++++++++++++++++++++++++++++++ pkg/config/types.go | 116 ---------------------------------------- 5 files changed, 253 insertions(+), 190 deletions(-) create mode 100644 pkg/config/bootstrap.go create mode 100644 pkg/config/context.go create mode 100644 pkg/config/manifest.go diff --git a/pkg/config/bootstrap.go b/pkg/config/bootstrap.go new file mode 100644 index 000000000..7d6dd01b5 --- /dev/null +++ b/pkg/config/bootstrap.go @@ -0,0 +1,82 @@ +/* +Copyright 2014 The Kubernetes Authors. + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +*/ + +package config + +import "sigs.k8s.io/yaml" + +// Bootstrap holds configurations for bootstrap steps +type Bootstrap struct { + // Configuration parameters for container + Container *Container `json:"container,omitempty"` + // Configuration parameters for ISO builder + Builder *Builder `json:"builder,omitempty"` + // Configuration parameters for ephemeral node remote management + RemoteDirect *RemoteDirect `json:"remoteDirect,omitempty"` +} + +// Container parameters +type Container struct { + // Container volume directory binding. + Volume string `json:"volume,omitempty"` + // ISO generator container image URL + Image string `json:"image,omitempty"` + // Container Runtime Interface driver + ContainerRuntime string `json:"containerRuntime,omitempty"` +} + +// Builder parameters +type Builder struct { + // Cloud Init user-data file name placed to the container volume root + UserDataFileName string `json:"userDataFileName,omitempty"` + // Cloud Init network-config file name placed to the container volume root + NetworkConfigFileName string `json:"networkConfigFileName,omitempty"` + // File name for output metadata + OutputMetadataFileName string `json:"outputMetadataFileName,omitempty"` +} + +// RemoteDirect configuration options +type RemoteDirect struct { + // IsoURL specifies url to download ISO image for ephemeral node + IsoURL string `json:"isoUrl,omitempty"` +} + +// Bootstrap functions +func (b *Bootstrap) String() string { + yamlData, err := yaml.Marshal(&b) + if err != nil { + return "" + } + return string(yamlData) +} + +// String returns Container object in a serialized string format +func (c *Container) String() string { + yamlData, err := yaml.Marshal(&c) + if err != nil { + return "" + } + return string(yamlData) +} + +// String returns Builder object in a serialized string format +func (b *Builder) String() string { + yamlData, err := yaml.Marshal(&b) + if err != nil { + return "" + } + return string(yamlData) +} diff --git a/pkg/config/config.go b/pkg/config/config.go index 56ea80eef..3d9d976ab 100644 --- a/pkg/config/config.go +++ b/pkg/config/config.go @@ -796,62 +796,6 @@ func (c *Config) Purge() error { return os.Remove(c.loadedConfigPath) } -func (c *Context) String() string { - cyaml, err := yaml.Marshal(&c) - if err != nil { - return "" - } - kcluster := c.KubeContext() - kyaml, err := yaml.Marshal(&kcluster) - if err != nil { - return string(cyaml) - } - 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 -} - -func (c *Context) ClusterName() string { - return NewClusterComplexNameFromKubeClusterName(c.NameInKubeconf).Name -} - -func (m *Manifest) String() string { - yamlData, err := yaml.Marshal(&m) - if err != nil { - return "" - } - return string(yamlData) -} - -// Bootstrap functions -func (b *Bootstrap) String() string { - yamlData, err := yaml.Marshal(&b) - if err != nil { - return "" - } - return string(yamlData) -} - // Management Configuration functions func (m *ManagementConfiguration) String() string { yamlData, err := yaml.Marshal(&m) @@ -860,21 +804,3 @@ func (m *ManagementConfiguration) String() string { } return string(yamlData) } - -// String returns Container object in a serialized string format -func (c *Container) String() string { - yamlData, err := yaml.Marshal(&c) - if err != nil { - return "" - } - return string(yamlData) -} - -// String returns Builder object in a serialized string format -func (b *Builder) String() string { - yamlData, err := yaml.Marshal(&b) - if err != nil { - return "" - } - return string(yamlData) -} diff --git a/pkg/config/context.go b/pkg/config/context.go new file mode 100644 index 000000000..3844564c3 --- /dev/null +++ b/pkg/config/context.go @@ -0,0 +1,77 @@ +/* +Copyright 2014 The Kubernetes Authors. + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +*/ + +package config + +import ( + "fmt" + + "k8s.io/client-go/tools/clientcmd/api" + "sigs.k8s.io/yaml" +) + +// Context is a tuple of references to a cluster (how do I communicate with a kubernetes context), +// a user (how do I identify myself), and a namespace (what subset of resources do I want to work with) +type Context struct { + // Context name in kubeconf + NameInKubeconf string `json:"contextKubeconf"` + + // Manifest is the default manifest to be use with this context + // +optional + Manifest string `json:"manifest,omitempty"` + + // KubeConfig Context Object + context *api.Context +} + +func (c *Context) String() string { + cyaml, err := yaml.Marshal(&c) + if err != nil { + return "" + } + kcluster := c.KubeContext() + kyaml, err := yaml.Marshal(&kcluster) + if err != nil { + return string(cyaml) + } + 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() *api.Context { + return c.context +} + +// SetKubeContext updates kube contect with given context details +func (c *Context) SetKubeContext(kc *api.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 +} + +func (c *Context) ClusterName() string { + return NewClusterComplexNameFromKubeClusterName(c.NameInKubeconf).Name +} diff --git a/pkg/config/manifest.go b/pkg/config/manifest.go new file mode 100644 index 000000000..252faabbc --- /dev/null +++ b/pkg/config/manifest.go @@ -0,0 +1,94 @@ +/* +Copyright 2014 The Kubernetes Authors. + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +*/ + +package config + +import "sigs.k8s.io/yaml" + +// Manifest is a tuple of references to a Manifest (how do Identify, collect , +// find the yaml manifests that airship uses to perform its operations) +type Manifest struct { + // PrimaryRepositoryName is a name of the repo, that contains site/ directory + // and is a starting point for building document bundle + PrimaryRepositoryName string `json:"primaryRepositoryName"` + // ExtraRepositories is the map of extra repositories addressable by a name + Repositories map[string]*Repository `json:"repositories,omitempty"` + // TargetPath Local Target path for working or home directory for all Manifest Cloned/Returned/Generated + TargetPath string `json:"targetPath"` + // SubPath is a path relative to TargetPath + Path where PrimaryRepository is cloned and contains + // directories with ClusterType and Phase bundles, example: + // Repositories[PrimaryRepositoryName].Url = 'https://github.com/airshipit/treasuremap' + // SubPath = "manifests" + // you would expect that at treasuremap/manifests you would have ephemeral/initinfra and + // ephemera/target directories, containing kustomize.yaml. + SubPath string `json:"subPath"` +} + +// Repository is a tuple that holds the information for the remote sources of manifest yaml documents. +// Information such as location, authentication info, +// as well as details of what to get such as branch, tag, commit it, etc. +type Repository struct { + // URLString for Repository + URLString string `json:"url"` + // Auth holds authentication options against remote + Auth *RepoAuth `json:"auth,omitempty"` + // CheckoutOptions holds options to checkout repository + CheckoutOptions *RepoCheckout `json:"checkout,omitempty"` +} + +// RepoAuth struct describes method of authentication against given repository +type RepoAuth struct { + // Type of authentication method to be used with given repository + // supported types are "ssh-key", "ssh-pass", "http-basic" + Type string `json:"type,omitempty"` + //KeyPassword is a password decrypt ssh private key (used with ssh-key auth type) + KeyPassword string `json:"keyPass,omitempty"` + // KeyPath is path to private ssh key on disk (used with ssh-key auth type) + KeyPath string `json:"sshKey,omitempty"` + //HTTPPassword is password for basic http authentication (used with http-basic auth type) + HTTPPassword string `json:"httpPass,omitempty"` + // SSHPassword is password for ssh password authentication (used with ssh-pass) + SSHPassword string `json:"sshPass,omitempty"` + // Username to authenticate against git remote (used with any type) + Username string `json:"username,omitempty"` +} + +// RepoCheckout container holds information how to checkout repository +// Each field is mutually exclusive +type RepoCheckout struct { + // CommitHash is full hash of the commit that will be used to checkout + CommitHash string `json:"commitHash,omitempty"` + // Branch is the branch name to checkout + Branch string `json:"branch"` + // Tag is the tag name to checkout + Tag string `json:"tag"` + // RemoteRef is not supported currently TODO + // RemoteRef is used for remote checkouts such as gerrit change requests/github pull request + // for example refs/changes/04/691202/5 + // TODO Add support for fetching remote refs + RemoteRef string `json:"remoteRef"` + // ForceCheckout is a boolean to indicate whether to use the `--force` option when checking out + ForceCheckout bool `json:"force"` +} + +// Manifest functions +func (m *Manifest) String() string { + yamlData, err := yaml.Marshal(&m) + if err != nil { + return "" + } + return string(yamlData) +} diff --git a/pkg/config/types.go b/pkg/config/types.go index 77365b60e..0f6aab5fd 100644 --- a/pkg/config/types.go +++ b/pkg/config/types.go @@ -69,86 +69,6 @@ type Config struct { kubeConfig *kubeconfig.Config } -// Context is a tuple of references to a cluster (how do I communicate with a kubernetes context), -// a user (how do I identify myself), and a namespace (what subset of resources do I want to work with) -type Context struct { - // Context name in kubeconf - NameInKubeconf string `json:"contextKubeconf"` - - // Manifest is the default manifest to be use with this context - // +optional - Manifest string `json:"manifest,omitempty"` - - // KubeConfig Context Object - context *kubeconfig.Context -} - -// Manifest is a tuple of references to a Manifest (how do Identify, collect , -// find the yaml manifests that airship uses to perform its operations) -type Manifest struct { - // PrimaryRepositoryName is a name of the repo, that contains site/ directory - // and is a starting point for building document bundle - PrimaryRepositoryName string `json:"primaryRepositoryName"` - // ExtraRepositories is the map of extra repositories addressable by a name - Repositories map[string]*Repository `json:"repositories,omitempty"` - // TargetPath Local Target path for working or home dirctory for all Manifest Cloned/Returned/Generated - TargetPath string `json:"targetPath"` - // SubPath is a path relative to TargetPath + Path where PrimaryRepository is cloned and contains - // directories with ClusterType and Phase bundles, example: - // Repositories[PrimaryRepositoryName].Url = 'https://github.com/airshipit/treasuremap' - // SubPath = "manifests" - // you would expect that at treasuremap/manifests you would have ephemeral/initinfra and - // ephemera/target directories, containing kustomize.yaml. - SubPath string `json:"subPath"` -} - -// Repository is a tuple that holds the information for the remote sources of manifest yaml documents. -// Information such as location, authentication info, -// as well as details of what to get such as branch, tag, commit it, etc. -type Repository struct { - // URLString for Repository - URLString string `json:"url"` - // Auth holds authentication options against remote - Auth *RepoAuth `json:"auth,omitempty"` - // CheckoutOptions holds options to checkout repository - CheckoutOptions *RepoCheckout `json:"checkout,omitempty"` -} - -// RepoAuth struct describes method of authentication agaist given repository -type RepoAuth struct { - // Type of authentication method to be used with given repository - // supported types are "ssh-key", "ssh-pass", "http-basic" - Type string `json:"type,omitempty"` - //KeyPassword is a password decrypt ssh private key (used with ssh-key auth type) - KeyPassword string `json:"keyPass,omitempty"` - // KeyPath is path to private ssh key on disk (used with ssh-key auth type) - KeyPath string `json:"sshKey,omitempty"` - //HTTPPassword is password for basic http authentication (used with http-basic auth type) - HTTPPassword string `json:"httpPass,omitempty"` - // SSHPassword is password for ssh password authentication (used with ssh-pass) - SSHPassword string `json:"sshPass,omitempty"` - // Username to authenticate against git remote (used with any type) - Username string `json:"username,omitempty"` -} - -// RepoCheckout container holds information how to checkout repository -// Each field is mutually exclusive -type RepoCheckout struct { - // CommitHash is full hash of the commit that will be used to checkout - CommitHash string `json:"commitHash,omitempty"` - // Branch is the branch name to checkout - Branch string `json:"branch"` - // Tag is the tag name to checkout - Tag string `json:"tag"` - // RemoteRef is not supported currently TODO - // RemoteRef is used for remote checkouts such as gerrit change requests/github pull request - // for example refs/changes/04/691202/5 - // TODO Add support for fetching remote refs - RemoteRef string `json:"remoteRef"` - // ForceCheckout is a boolean to indicate whether to use the `--force` option when checking out - ForceCheckout bool `json:"force"` -} - // ManagementConfiguration defines configuration data for all remote systems within a context. type ManagementConfiguration struct { // Insecure indicates whether the SSL certificate should be checked on remote management requests. @@ -160,39 +80,3 @@ type ManagementConfiguration struct { // one is configured in an environment. UseProxy bool `json:"useproxy,omitempty"` } - -// Bootstrap holds configurations for bootstrap steps -type Bootstrap struct { - // Configuration parameters for container - Container *Container `json:"container,omitempty"` - // Configuration parameters for ISO builder - Builder *Builder `json:"builder,omitempty"` - // Configuration parameters for ephmeral node remote management - RemoteDirect *RemoteDirect `json:"remoteDirect,omitempty"` -} - -// Container parameters -type Container struct { - // Container volume directory binding. - Volume string `json:"volume,omitempty"` - // ISO generator container image URL - Image string `json:"image,omitempty"` - // Container Runtime Interface driver - ContainerRuntime string `json:"containerRuntime,omitempty"` -} - -// Builder parameters -type Builder struct { - // Cloud Init user-data file name placed to the container volume root - UserDataFileName string `json:"userDataFileName,omitempty"` - // Cloud Init network-config file name placed to the container volume root - NetworkConfigFileName string `json:"networkConfigFileName,omitempty"` - // File name for output metadata - OutputMetadataFileName string `json:"outputMetadataFileName,omitempty"` -} - -// RemoteDirect configuration options -type RemoteDirect struct { - // IsoURL specifies url to download ISO image for epehemeral node - IsoURL string `json:"isoUrl,omitempty"` -}