Skip to content

Commit

Permalink
*: rename "KubeConfig" to "Kubeconfig", "enable-prompt" to "prompt"
Browse files Browse the repository at this point in the history
Signed-off-by: Gyuho Lee <leegyuho@amazon.com>
  • Loading branch information
gyuho committed May 29, 2021
1 parent 5cbe28b commit e7e1025
Show file tree
Hide file tree
Showing 20 changed files with 163 additions and 143 deletions.
30 changes: 15 additions & 15 deletions client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,10 @@ type Config struct {

// KubectlPath is the kubectl path.
KubectlPath string
// KubeConfigPath is the kubeconfig path to load.
KubeConfigPath string
// KubeConfigContext is the kubeconfig context.
KubeConfigContext string
// KubeconfigPath is the kubeconfig path to load.
KubeconfigPath string
// KubeconfigContext is the kubeconfig context.
KubeconfigContext string

// EKS defines EKS-specific configuration.
EKS *EKS
Expand Down Expand Up @@ -84,7 +84,7 @@ type EKS struct {

// CreateConfig creates the Kubernetes client configuration.
func CreateConfig(cfg *Config) (kcfg *k8s_client_rest.Config, err error) {
if kcfg, err = createConfigFromKubeConfig(cfg); err != nil {
if kcfg, err = createConfigFromKubeconfig(cfg); err != nil {
cfg.Logger.Error("failed to create config using KUBECONFIG", zap.Error(err))
}

Expand Down Expand Up @@ -192,23 +192,23 @@ func CreateConfig(cfg *Config) (kcfg *k8s_client_rest.Config, err error) {
return kcfg, nil
}

func createConfigFromKubeConfig(cfg *Config) (kcfg *k8s_client_rest.Config, err error) {
if cfg.KubeConfigPath == "" {
func createConfigFromKubeconfig(cfg *Config) (kcfg *k8s_client_rest.Config, err error) {
if cfg.KubeconfigPath == "" {
return nil, errors.New("empty KUBECONFIG")
}

switch {
case cfg.KubeConfigContext != "":
case cfg.KubeconfigContext != "":
cfg.Logger.Info("creating config from KUBECONFIG and context",
zap.String("kubeconfig", cfg.KubeConfigPath),
zap.String("context", cfg.KubeConfigContext),
zap.String("kubeconfig", cfg.KubeconfigPath),
zap.String("context", cfg.KubeconfigContext),
)
kcfg, err = clientcmd.NewNonInteractiveDeferredLoadingClientConfig(
&clientcmd.ClientConfigLoadingRules{
ExplicitPath: cfg.KubeConfigPath,
ExplicitPath: cfg.KubeconfigPath,
},
&clientcmd.ConfigOverrides{
CurrentContext: cfg.KubeConfigContext,
CurrentContext: cfg.KubeconfigContext,
ClusterInfo: clientcmd_api.Cluster{Server: ""},
},
).ClientConfig()
Expand All @@ -217,9 +217,9 @@ func createConfigFromKubeConfig(cfg *Config) (kcfg *k8s_client_rest.Config, err
kcfg = nil
}

case cfg.KubeConfigContext == "":
cfg.Logger.Info("creating config from KUBECONFIG", zap.String("kubeconfig", cfg.KubeConfigPath))
kcfg, err = clientcmd.BuildConfigFromFlags("", cfg.KubeConfigPath)
case cfg.KubeconfigContext == "":
cfg.Logger.Info("creating config from KUBECONFIG", zap.String("kubeconfig", cfg.KubeconfigPath))
kcfg, err = clientcmd.BuildConfigFromFlags("", cfg.KubeconfigPath)
if kcfg == nil || err != nil {
cfg.Logger.Warn("failed to create config from KUBECONFIG", zap.Error(err))
kcfg = nil
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,23 +23,23 @@ func init() {
}

var (
enablePrompt bool
prompt bool
logLevel string
logOutputs []string
minimumNodes int
namespace string
kubectlPath string
kubeConfigPath string
kubeconfigPath string
)

func init() {
rootCmd.PersistentFlags().BoolVar(&enablePrompt, "enable-prompt", true, "'true' to enable prompt mode")
rootCmd.PersistentFlags().BoolVar(&prompt, "prompt", true, "'true' to enable prompt mode")
rootCmd.PersistentFlags().StringVar(&logLevel, "log-level", log.DefaultLogLevel, "Logging level")
rootCmd.PersistentFlags().StringSliceVar(&logOutputs, "log-outputs", []string{"stderr"}, "Additional logger outputs")
rootCmd.PersistentFlags().IntVar(&minimumNodes, "minimum-nodes", cloudwatch_agent.DefaultMinimumNodes, "minimum number of Kubernetes nodes required for installing this addon")
rootCmd.PersistentFlags().StringVar(&namespace, "namespace", "test-namespace", "'true' to auto-generate path for create config/cluster, overwrites existing --path value")
rootCmd.PersistentFlags().StringVar(&kubectlPath, "kubectl-path", "", "kubectl path")
rootCmd.PersistentFlags().StringVar(&kubeConfigPath, "kubeconfig-path", "", "KUBECONFIG path")
rootCmd.PersistentFlags().StringVar(&kubeconfigPath, "kubeconfig-path", "", "KUBECONFIG path")

rootCmd.AddCommand(
newApply(),
Expand Down Expand Up @@ -81,15 +81,15 @@ func createApplyFunc(cmd *cobra.Command, args []string) {
_ = zap.ReplaceGlobals(lg)

cfg := &cloudwatch_agent.Config{
EnablePrompt: enablePrompt,
Prompt: prompt,
Logger: lg,
LogWriter: logWriter,
MinimumNodes: minimumNodes,
Namespace: namespace,
ClientConfig: &client.Config{
Logger: lg,
KubectlPath: kubectlPath,
KubeConfigPath: kubeConfigPath,
KubeconfigPath: kubeconfigPath,
},
Region: region,
ClusterName: clusterName,
Expand Down Expand Up @@ -122,14 +122,14 @@ func createDeleteFunc(cmd *cobra.Command, args []string) {
_ = zap.ReplaceGlobals(lg)

cfg := &cloudwatch_agent.Config{
EnablePrompt: enablePrompt,
Logger: lg,
LogWriter: logWriter,
Namespace: namespace,
Prompt: prompt,
Logger: lg,
LogWriter: logWriter,
Namespace: namespace,
ClientConfig: &client.Config{
Logger: lg,
KubectlPath: kubectlPath,
KubeConfigPath: kubeConfigPath,
KubeconfigPath: kubeconfigPath,
},
}

Expand Down
8 changes: 4 additions & 4 deletions k8s-tester/cloudwatch-agent/tester.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ import (
)

type Config struct {
EnablePrompt bool `json:"-"`
Prompt bool `json:"-"`

Logger *zap.Logger `json:"-"`
LogWriter io.Writer `json:"-"`
Expand Down Expand Up @@ -171,7 +171,7 @@ func (ts *tester) Delete() error {
}

func (ts *tester) runPrompt(action string) (ok bool) {
if ts.cfg.EnablePrompt {
if ts.cfg.Prompt {
msg := fmt.Sprintf("Ready to %q resources for the namespace %q, should we continue?", action, ts.cfg.Namespace)
prompt := promptui.Select{
Label: msg,
Expand Down Expand Up @@ -849,7 +849,7 @@ func (ts *tester) _checkPods() error {

descArgsPods := []string{
ts.cfg.ClientConfig.KubectlPath,
"--kubeconfig=" + ts.cfg.ClientConfig.KubeConfigPath,
"--kubeconfig=" + ts.cfg.ClientConfig.KubeconfigPath,
"--namespace=" + ts.cfg.Namespace,
"describe",
"pods/" + pod.Name,
Expand All @@ -858,7 +858,7 @@ func (ts *tester) _checkPods() error {

logArgs := []string{
ts.cfg.ClientConfig.KubectlPath,
"--kubeconfig=" + ts.cfg.ClientConfig.KubeConfigPath,
"--kubeconfig=" + ts.cfg.ClientConfig.KubeconfigPath,
"--namespace=" + ts.cfg.Namespace,
"logs",
"pods/" + pod.Name,
Expand Down
8 changes: 4 additions & 4 deletions k8s-tester/cmd/k8s-tester/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,21 +22,21 @@ func init() {
}

var (
enablePrompt bool
prompt bool
logLevel string
logOutputs []string
minimumNodes int
kubectlPath string
kubeConfigPath string
kubeconfigPath string
)

func init() {
rootCmd.PersistentFlags().BoolVar(&enablePrompt, "enable-prompt", true, "'true' to enable prompt mode")
rootCmd.PersistentFlags().BoolVar(&prompt, "prompt", true, "'true' to enable prompt mode")
rootCmd.PersistentFlags().StringVar(&logLevel, "log-level", log.DefaultLogLevel, "Logging level")
rootCmd.PersistentFlags().StringSliceVar(&logOutputs, "log-outputs", []string{"stderr"}, "Additional logger outputs")
rootCmd.PersistentFlags().IntVar(&minimumNodes, "minimum-nodes", k8s_tester.DefaultMinimumNodes, "minimum number of Kubernetes nodes required")
rootCmd.PersistentFlags().StringVar(&kubectlPath, "kubectl-path", "", "kubectl path")
rootCmd.PersistentFlags().StringVar(&kubeConfigPath, "kubeconfig-path", "", "KUBECONFIG path")
rootCmd.PersistentFlags().StringVar(&kubeconfigPath, "kubeconfig-path", "", "KUBECONFIG path")

rootCmd.AddCommand(
newApply(),
Expand Down
43 changes: 23 additions & 20 deletions k8s-tester/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,15 +28,16 @@ import (
// By default, it uses the environmental variables as https://github.com/aws/aws-k8s-tester/blob/v1.5.9/eksconfig/env.go.
// TODO: support https://github.com/onsi/ginkgo.
type Config struct {
EnablePrompt bool `json:"-"`

Logger *zap.Logger `json:"-"`
LogWriter io.Writer `json:"-"`
Stopc chan struct{} `json:"-"`

Logger *zap.Logger `json:"-"`
LogWriter io.Writer `json:"-"`
Stopc chan struct{} `json:"-"`
ClientConfig *client.Config `json:"-"`

ClusterName string `json:"cluster-name"`
Prompt bool `json:"prompt"`
KubectlPath string `json:"kubectl-path"`
KubeconfigPath string `json:"kubeconfig-path"`
KubeconfigContext string `json:"kubeconfig-context"`
ClusterName string `json:"cluster-name"`

// MinimumNodes is the minimum number of Kubernetes nodes required for installing this addon.
MinimumNodes int `json:"minimum-nodes"`
Expand All @@ -55,21 +56,10 @@ type Config struct {

const DefaultMinimumNodes = 1

func Load(p string) (cfg *Config, err error) {
var d []byte
d, err = ioutil.ReadFile(p)
if err != nil {
return nil, err
}
cfg = new(Config)
if err = yaml.Unmarshal(d, cfg, yaml.DisallowUnknownFields); err != nil {
return nil, err
}
return cfg, nil
}

func NewDefault() *Config {
return &Config{
Prompt: true,

MinimumNodes: DefaultMinimumNodes,

CloudwatchAgent: cloudwatch_agent.NewDefault(),
Expand All @@ -86,6 +76,19 @@ func NewDefault() *Config {
// ENV_PREFIX is the environment variable prefix.
const ENV_PREFIX = "K8S_TESTER_"

func Load(p string) (cfg *Config, err error) {
var d []byte
d, err = ioutil.ReadFile(p)
if err != nil {
return nil, err
}
cfg = new(Config)
if err = yaml.Unmarshal(d, cfg, yaml.DisallowUnknownFields); err != nil {
return nil, err
}
return cfg, nil
}

// UpdateFromEnvs updates fields from environmental variables.
// Empty values are ignored and do not overwrite fields with empty values.
// WARNING: The environmental variable value always overwrites current field
Expand Down
17 changes: 17 additions & 0 deletions k8s-tester/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,23 @@ import (
"testing"
)

func TestEnv(t *testing.T) {
cfg := NewDefault()

os.Setenv("K8S_TESTER_PROMPT", "false")
defer os.Unsetenv("K8S_TESTER_PROMPT")
os.Setenv("K8S_TESTER_CLUSTER_NAME", "hello")
defer os.Unsetenv("K8S_TESTER_CLUSTER_NAME")

if err := cfg.UpdateFromEnvs(); err != nil {
t.Fatal(err)
}

if cfg.ClusterName != "hello" {
t.Fatalf("unexpected cfg.ClusterName %v", cfg.ClusterName)
}
}

func TestEnvEmpty(t *testing.T) {
cfg := NewDefault()

Expand Down
22 changes: 11 additions & 11 deletions k8s-tester/fluent-bit/cmd/k8s-tester-fluent-bit/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,23 +23,23 @@ func init() {
}

var (
enablePrompt bool
prompt bool
logLevel string
logOutputs []string
minimumNodes int
namespace string
kubectlPath string
kubeConfigPath string
kubeconfigPath string
)

func init() {
rootCmd.PersistentFlags().BoolVar(&enablePrompt, "enable-prompt", true, "'true' to enable prompt mode")
rootCmd.PersistentFlags().BoolVar(&prompt, "prompt", true, "'true' to enable prompt mode")
rootCmd.PersistentFlags().StringVar(&logLevel, "log-level", log.DefaultLogLevel, "Logging level")
rootCmd.PersistentFlags().StringSliceVar(&logOutputs, "log-outputs", []string{"stderr"}, "Additional logger outputs")
rootCmd.PersistentFlags().IntVar(&minimumNodes, "minimum-nodes", fluent_bit.DefaultMinimumNodes, "minimum number of Kubernetes nodes required for installing this addon")
rootCmd.PersistentFlags().StringVar(&namespace, "namespace", "test-namespace", "'true' to auto-generate path for create config/cluster, overwrites existing --path value")
rootCmd.PersistentFlags().StringVar(&kubectlPath, "kubectl-path", "", "kubectl path")
rootCmd.PersistentFlags().StringVar(&kubeConfigPath, "kubeconfig-path", "", "KUBECONFIG path")
rootCmd.PersistentFlags().StringVar(&kubeconfigPath, "kubeconfig-path", "", "KUBECONFIG path")

rootCmd.AddCommand(
newApply(),
Expand Down Expand Up @@ -73,15 +73,15 @@ func createApplyFunc(cmd *cobra.Command, args []string) {
_ = zap.ReplaceGlobals(lg)

cfg := &fluent_bit.Config{
EnablePrompt: enablePrompt,
Prompt: prompt,
Logger: lg,
LogWriter: logWriter,
MinimumNodes: minimumNodes,
Namespace: namespace,
ClientConfig: &client.Config{
Logger: lg,
KubectlPath: kubectlPath,
KubeConfigPath: kubeConfigPath,
KubeconfigPath: kubeconfigPath,
},
}

Expand Down Expand Up @@ -112,14 +112,14 @@ func createDeleteFunc(cmd *cobra.Command, args []string) {
_ = zap.ReplaceGlobals(lg)

cfg := &fluent_bit.Config{
EnablePrompt: enablePrompt,
Logger: lg,
LogWriter: logWriter,
Namespace: namespace,
Prompt: prompt,
Logger: lg,
LogWriter: logWriter,
Namespace: namespace,
ClientConfig: &client.Config{
Logger: lg,
KubectlPath: kubectlPath,
KubeConfigPath: kubeConfigPath,
KubeconfigPath: kubeconfigPath,
},
}

Expand Down
2 changes: 1 addition & 1 deletion k8s-tester/fluent-bit/fluent-bit.go
Original file line number Diff line number Diff line change
Expand Up @@ -498,7 +498,7 @@ func (ts *tester) checkDaemonSet() error {
client.WithQueryFunc(func() {
descArgs := []string{
ts.cfg.ClientConfig.KubectlPath,
"--kubeconfig=" + ts.cfg.ClientConfig.KubeConfigPath,
"--kubeconfig=" + ts.cfg.ClientConfig.KubeconfigPath,
"--namespace=" + ts.cfg.Namespace,
"describe",
"daemonset",
Expand Down
4 changes: 2 additions & 2 deletions k8s-tester/fluent-bit/tester.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import (
)

type Config struct {
EnablePrompt bool `json:"-"`
Prompt bool `json:"-"`

Logger *zap.Logger `json:"-"`
LogWriter io.Writer `json:"-"`
Expand Down Expand Up @@ -231,7 +231,7 @@ func (ts *tester) Delete() error {
}

func (ts *tester) runPrompt(action string) (ok bool) {
if ts.cfg.EnablePrompt {
if ts.cfg.Prompt {
msg := fmt.Sprintf("Ready to %q resources for the namespace %q, should we continue?", action, ts.cfg.Namespace)
prompt := promptui.Select{
Label: msg,
Expand Down
Loading

0 comments on commit e7e1025

Please sign in to comment.