b2af034e57
This implementation creates named references between an airship config file , and a user specified or system default kubeconfig file airshipconfig location can be specified via an envirnment variable or via --airshipconf string Path to file for airshipctl configuration. (default ".airship/config") kubeconfig has to be explicitly stated using the argument below --kubeconfig string Path to kubeconfig associated with airshipctl configuration. (default ".airship/kubeconfig") if the argument is not specified a default empty kubeconfig will be used using the default ".airship/kubeconfig" All subcommands exposed via airshipctl config will update airship config and airship related kubeconfig when appropriate. This patchset adds : - Config Struct (type) - config cmd and pkg - get_cluster : List a specific name cluster or List all clusters if no name is provided. - set-cluster : Create or Modify an existing cluster. Review comment fixes as of Pathset 19 - Moved core functionality from cmd to pkg - Encapsulate cmd needs in pck in nw files cmds, cmds_types and cmds_test . Expectation is that other functions will need func an structs there. - added test for GetCluster - Added GetCluster method to config object to be used by get_cluster command - Change ClusterNames func as per review suggestion - Change TestEmpty Cluster to avoid pointing to non test kubecnfig by default - Change constant AirshipConfigFilePath to AirshipConfigDir - Renamed config_utils to utils - Added config cmd output tests - Changes to settings_test.go to clean after itself. - Created new pkg/config/testdata/GoldenString for struct data comparison values to avoid confusion - Fix small get_cluster no name issue when empty config - Fix issue when reconciling a cluster info that only exists in airship config and not in kubeconfig Increased coverage to: SUCCESS: Test coverage is at 84.2%, Started to move all testdata to a single place under pkg/config for now. Change-Id: I7aae1f15afaebc99407f7fabccecf86ab0923bc3
150 lines
4.7 KiB
Go
150 lines
4.7 KiB
Go
/*
|
|
Copyright 2016 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"
|
|
|
|
"github.com/spf13/cobra"
|
|
|
|
"opendev.org/airship/airshipctl/pkg/config"
|
|
"opendev.org/airship/airshipctl/pkg/environment"
|
|
"opendev.org/airship/airshipctl/pkg/log"
|
|
)
|
|
|
|
var (
|
|
setClusterLong = (`
|
|
Sets a cluster entry in arshipctl config.
|
|
Specifying a name that already exists will merge new fields on top of existing values for those fields.`)
|
|
|
|
setClusterExample = fmt.Sprintf(`
|
|
# Set only the server field on the e2e cluster entry without touching other values.
|
|
airshipctl config set-cluster e2e --%v=ephemeral --%v=https://1.2.3.4
|
|
|
|
# Embed certificate authority data for the e2e cluster entry
|
|
airshipctl config set-cluster e2e --%v-type=target --%v-authority=~/.airship/e2e/kubernetes.ca.crt
|
|
|
|
# Disable cert checking for the dev cluster entry
|
|
airshipctl config set-cluster e2e --%v-type=target --%v=true
|
|
|
|
# Configure Client Certificate
|
|
airshipctl config set-cluster e2e --%v-type=target --%v=true --%v=".airship/cert_file"`,
|
|
config.FlagClusterType,
|
|
config.FlagAPIServer,
|
|
config.FlagClusterType,
|
|
config.FlagCAFile,
|
|
config.FlagClusterType,
|
|
config.FlagInsecure,
|
|
config.FlagClusterType,
|
|
config.FlagEmbedCerts,
|
|
config.FlagCertFile)
|
|
)
|
|
|
|
// NewCmdConfigSetCluster creates a command object for the "set-cluster" action, which
|
|
// defines a new cluster airship config.
|
|
func NewCmdConfigSetCluster(rootSettings *environment.AirshipCTLSettings) *cobra.Command {
|
|
theCluster := &config.ClusterOptions{}
|
|
|
|
setclustercmd := &cobra.Command{
|
|
Use: "set-cluster NAME",
|
|
Short: "Sets a cluster entry in the airshipctl config",
|
|
Long: setClusterLong,
|
|
Example: setClusterExample,
|
|
Args: cobra.ExactArgs(1),
|
|
RunE: func(cmd *cobra.Command, args []string) error {
|
|
theCluster.Name = cmd.Flags().Args()[0]
|
|
modified, err := runSetCluster(theCluster, rootSettings)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
if modified {
|
|
fmt.Fprintf(cmd.OutOrStdout(), "Cluster %q of type %q modified.\n",
|
|
theCluster.Name, theCluster.ClusterType)
|
|
} else {
|
|
fmt.Fprintf(cmd.OutOrStdout(), "Cluster %q of type %q created.\n",
|
|
theCluster.Name, theCluster.ClusterType)
|
|
}
|
|
return nil
|
|
},
|
|
}
|
|
|
|
scInitFlags(theCluster, setclustercmd)
|
|
return setclustercmd
|
|
}
|
|
|
|
func scInitFlags(o *config.ClusterOptions, setclustercmd *cobra.Command) {
|
|
|
|
setclustercmd.Flags().StringVar(&o.Server, config.FlagAPIServer, o.Server,
|
|
config.FlagAPIServer+" for the cluster entry in airshipctl config")
|
|
|
|
setclustercmd.Flags().StringVar(&o.ClusterType, config.FlagClusterType, o.ClusterType,
|
|
config.FlagClusterType+" for the cluster entry in airshipctl config")
|
|
|
|
setclustercmd.Flags().BoolVar(&o.InsecureSkipTLSVerify, config.FlagInsecure, true,
|
|
config.FlagInsecure+" for the cluster entry in airshipctl config")
|
|
|
|
setclustercmd.Flags().StringVar(&o.CertificateAuthority, config.FlagCAFile, o.CertificateAuthority,
|
|
"Path to "+config.FlagCAFile+" file for the cluster entry in airshipctl config")
|
|
err := setclustercmd.MarkFlagFilename(config.FlagCAFile)
|
|
if err != nil {
|
|
log.Fatal(err)
|
|
}
|
|
|
|
setclustercmd.Flags().BoolVar(&o.EmbedCAData, config.FlagEmbedCerts, false,
|
|
config.FlagEmbedCerts+" for the cluster entry in airshipctl config")
|
|
|
|
}
|
|
|
|
func runSetCluster(o *config.ClusterOptions, rootSettings *environment.AirshipCTLSettings) (bool, error) {
|
|
|
|
clusterWasModified := false
|
|
err := o.Validate()
|
|
if err != nil {
|
|
return clusterWasModified, err
|
|
}
|
|
|
|
airconfig := rootSettings.Config()
|
|
cluster, err := airconfig.GetCluster(o.Name, o.ClusterType)
|
|
// Safe to ignore the error. Simple means I didnt find the cluster
|
|
if cluster == nil {
|
|
// New Cluster
|
|
_, err := airconfig.AddCluster(o)
|
|
if err != nil {
|
|
return clusterWasModified, err
|
|
}
|
|
clusterWasModified = false
|
|
} else {
|
|
// Cluster exists, lets update
|
|
_, err := airconfig.ModifyCluster(cluster, o)
|
|
if err != nil {
|
|
return clusterWasModified, err
|
|
}
|
|
clusterWasModified = true
|
|
}
|
|
|
|
// Update configuration file
|
|
// Just in time persistence approach
|
|
if err := airconfig.PersistConfig(); err != nil {
|
|
// Some warning here , that it didnt persit the changes because of this
|
|
// Or should we float this up
|
|
// What would it mean? No value.
|
|
return clusterWasModified, err
|
|
}
|
|
|
|
return clusterWasModified, nil
|
|
}
|