49027f4151
This change causes the linter to be a bit more complainy. The hope is that this will cut down on some of the more pedantic issues being caught in code reviews, and thus reduce the overall time a change spends in the review process. This change includes various changes to the codebase to bring it up to the new standards. Change-Id: I570d304bca5554404354f972d8a2743279a0171b
154 lines
4.9 KiB
Go
154 lines
4.9 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 (
|
|
"errors"
|
|
"fmt"
|
|
|
|
"github.com/spf13/cobra"
|
|
|
|
"opendev.org/airship/airshipctl/pkg/config"
|
|
"opendev.org/airship/airshipctl/pkg/environment"
|
|
conferrors "opendev.org/airship/airshipctl/pkg/errors"
|
|
"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)
|
|
if err != nil {
|
|
var cerr conferrors.ErrMissingConfig
|
|
if !errors.As(err, &cerr) {
|
|
// An error occurred, but it wasn't a "missing" config error.
|
|
return clusterWasModified, err
|
|
}
|
|
|
|
// Cluster didn't exist, create it
|
|
_, 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
|
|
}
|