From 75c5fa3888b7292dff98461217fb81240ce1f115 Mon Sep 17 00:00:00 2001 From: sibashi Date: Fri, 10 Feb 2023 19:47:07 +0530 Subject: [PATCH 1/2] Updated install command to receive env as a flag Signed-off-by: sibashi --- cmd/install.go | 5 +++++ install/install.go | 36 ++++++++++++++++++++++++++++++++---- 2 files changed, 37 insertions(+), 4 deletions(-) diff --git a/cmd/install.go b/cmd/install.go index 75453962..b16bf165 100644 --- a/cmd/install.go +++ b/cmd/install.go @@ -4,6 +4,7 @@ package cmd import ( + "fmt" "github.com/kubearmor/kubearmor-client/install" "github.com/spf13/cobra" ) @@ -17,6 +18,9 @@ var installCmd = &cobra.Command{ Long: `Install KubeArmor in a Kubernetes Clusters`, RunE: func(cmd *cobra.Command, args []string) error { installOptions.Animation = true + if err := installOptions.Env.CheckAndSetValidEnvironmentOption(cmd.Flag("env").Value.String()); err != nil { + return fmt.Errorf("error in checking environment option: %v", err) + } if err := install.K8sInstaller(client, installOptions); err != nil { return err } @@ -31,5 +35,6 @@ func init() { installCmd.Flags().StringVarP(&installOptions.KubearmorImage, "image", "i", "kubearmor/kubearmor:stable", "Kubearmor daemonset image to use") installCmd.Flags().StringVarP(&installOptions.Audit, "audit", "a", "", "Kubearmor Audit Posture Context [all,file,network,capabilities]") installCmd.Flags().BoolVar(&installOptions.Save, "save", false, "Save KubeArmor Manifest ") + installCmd.Flags().StringVarP(&installOptions.Env.Environment, "env", "e", "", "Supported KubeArmor Environment [k3s,microK8s,minikube,gke,bottlerocket,eks,docker,oke,generic]") } diff --git a/install/install.go b/install/install.go index a84b781c..25515193 100644 --- a/install/install.go +++ b/install/install.go @@ -36,11 +36,33 @@ type Options struct { Force bool Save bool Animation bool + Env EnvOption +} + +type EnvOption struct { + Auto bool + Environment string } var animation bool var progress int var cursorcount int +var validEnvironments = []string{"k3s", "microK8s", "minikube", "gke", "bottlerocket", "eks", "docker", "oke", "generic"} + +func (env *EnvOption) CheckAndSetValidEnvironmentOption(envOption string) error { + for _, v := range validEnvironments { + if v == envOption { + env.Environment = envOption + env.Auto = false + return nil + } + } + env.Auto = true + if envOption == "" { + return nil + } + return errors.New("Invalid environment passed") +} func clearLine(size int) int { for i := 0; i < size; i++ { @@ -146,11 +168,17 @@ func checkTerminatingPods(c *k8s.Client) int { // K8sInstaller for karmor install func K8sInstaller(c *k8s.Client, o Options) error { animation = o.Animation - env := AutoDetectEnvironment(c) - if env == "none" { - return errors.New("unsupported environment or cluster not configured correctly") + var env string + if o.Env.Auto { + env = AutoDetectEnvironment(c) + if env == "none" { + return errors.New("unsupported environment or cluster not configured correctly") + } + printMessage("😄 Auto Detected Environment : "+env, true) + } else { + env = o.Env.Environment + printMessage("😄 Environment : "+env, true) } - printMessage("😄 Auto Detected Environment : "+env, true) var printYAML []interface{} From 359476c57bd66cac9a19d9a80279d79394a49a4b Mon Sep 17 00:00:00 2001 From: Sibasish Behera Date: Tue, 14 Feb 2023 09:55:28 +0530 Subject: [PATCH 2/2] Fix: linting issues Signed-off-by: Sibasish Behera --- install/install.go | 15 ++++++++------- utils/portforward.go | 1 + 2 files changed, 9 insertions(+), 7 deletions(-) diff --git a/install/install.go b/install/install.go index f5c88880..c0709e88 100644 --- a/install/install.go +++ b/install/install.go @@ -37,10 +37,10 @@ type Options struct { Force bool Save bool Animation bool - Env EnvOption + Env envOption } -type EnvOption struct { +type envOption struct { Auto bool Environment string } @@ -50,7 +50,12 @@ var progress int var cursorcount int var validEnvironments = []string{"k3s", "microK8s", "minikube", "gke", "bottlerocket", "eks", "docker", "oke", "generic"} -func (env *EnvOption) CheckAndSetValidEnvironmentOption(envOption string) error { +// Checks if passed string is a valid environment +func (env *envOption) CheckAndSetValidEnvironmentOption(envOption string) error { + if envOption == "" { + env.Auto = true + return nil + } for _, v := range validEnvironments { if v == envOption { env.Environment = envOption @@ -58,10 +63,6 @@ func (env *EnvOption) CheckAndSetValidEnvironmentOption(envOption string) error return nil } } - env.Auto = true - if envOption == "" { - return nil - } return errors.New("Invalid environment passed") } diff --git a/utils/portforward.go b/utils/portforward.go index d4c3bbc2..ac9d30a8 100644 --- a/utils/portforward.go +++ b/utils/portforward.go @@ -1,3 +1,4 @@ +// Package utils provides utility for port forwarding. package utils import (