From 19348c8435f66f5ab3a58177f64a19a1a751cb6a Mon Sep 17 00:00:00 2001 From: panigs7 <92028646+panigs7@users.noreply.github.com> Date: Tue, 22 Feb 2022 17:54:42 +0530 Subject: [PATCH] fix lint, vet, fmt, gosec issues (#17) --- .github/workflows/actions.yaml | 6 +- api/v1/csiisilon_types.go | 2 +- api/v1/csipowermax_types.go | 2 +- api/v1/csipowermaxrevproxy_types.go | 3 +- api/v1/csipowerstore_types.go | 2 +- api/v1/csiunity_types.go | 2 +- api/v1/csivxflexos_types.go | 2 +- api/v1/types.go | 2 + api/v1/zz_generated.deepcopy.go | 1 + controllers/csiisilon_controller.go | 2 + controllers/csipowermax_controller.go | 2 + controllers/csipowermaxrevproxy_controller.go | 2 + controllers/csipowerstore_controller.go | 2 + controllers/csiunity_controller.go | 2 + controllers/csivxflexos_controller.go | 137 +++++++++--------- core/semver/semver.go | 5 +- main.go | 9 +- pkg/ctrlconfig/config.go | 2 +- pkg/ctrlconfig/opconfig.go | 7 +- pkg/resources/deployment/deployment.go | 2 + pkg/resources/helper.go | 4 +- pkg/resources/rbac/controllerrbac.go | 5 +- pkg/utils/status.go | 4 +- pkg/utils/utils.go | 1 + test/driver/operatorutils.go | 50 ++++--- test/integration-tests/basic_test.go | 2 +- test/integration-tests/framework/framework.go | 3 + test/integration-tests/utils/resources.go | 6 +- 28 files changed, 155 insertions(+), 114 deletions(-) diff --git a/.github/workflows/actions.yaml b/.github/workflows/actions.yaml index f63b70b..39c5d2a 100644 --- a/.github/workflows/actions.yaml +++ b/.github/workflows/actions.yaml @@ -24,7 +24,7 @@ jobs: - name: Run Go Security uses: securego/gosec@master with: - args: -exclude=G108,G402,G307 ./... + args: -exclude=G108,G402,G307,G101,G404 ./... test: name: Run Go unit tests and check package coverage runs-on: ubuntu-latest @@ -37,7 +37,9 @@ jobs: repository: 'dell/dell-csi-operator' path: 'dell-csi-operator' - name: Run unit tests and check package coverage - run: make unit-test + uses: dell/common-github-actions/go-code-tester@main + with: + threshold: 60 malware_security_scan: name: Malware Scanner runs-on: ubuntu-latest diff --git a/api/v1/csiisilon_types.go b/api/v1/csiisilon_types.go index 2fe7eb3..200de4b 100644 --- a/api/v1/csiisilon_types.go +++ b/api/v1/csiisilon_types.go @@ -33,8 +33,8 @@ type CSIIsilonSpec struct { // +kubebuilder:subresource:status // +kubebuilder:resource:path=csiisilons,scope=Namespaced -// +operator-sdk:csv:customresourcedefinitions:displayName="CSI PowerScale",resources={{Deployment,v1,isilon-controller},{DameonSet,v1,isilon-node}} // CSIIsilon is the Schema for the csiisilons API +// +operator-sdk:csv:customresourcedefinitions:displayName="CSI PowerScale",resources={{Deployment,v1,isilon-controller},{DameonSet,v1,isilon-node}} type CSIIsilon struct { metav1.TypeMeta `json:",inline"` metav1.ObjectMeta `json:"metadata,omitempty"` diff --git a/api/v1/csipowermax_types.go b/api/v1/csipowermax_types.go index b2cfa73..509d0ca 100644 --- a/api/v1/csipowermax_types.go +++ b/api/v1/csipowermax_types.go @@ -33,8 +33,8 @@ type CSIPowerMaxSpec struct { // +kubebuilder:subresource:status // +kubebuilder:resource:path=csipowermaxes,scope=Namespaced -// +operator-sdk:csv:customresourcedefinitions:displayName="CSI PowerMax",resources={{Deployment,v1,powermax-controller},{DameonSet,v1,powermax-node}} // CSIPowerMax is the Schema for the csipowermaxes API +// +operator-sdk:csv:customresourcedefinitions:displayName="CSI PowerMax",resources={{Deployment,v1,powermax-controller},{DameonSet,v1,powermax-node}} type CSIPowerMax struct { metav1.TypeMeta `json:",inline"` metav1.ObjectMeta `json:"metadata,omitempty"` diff --git a/api/v1/csipowermaxrevproxy_types.go b/api/v1/csipowermaxrevproxy_types.go index ba4748e..51209e5 100644 --- a/api/v1/csipowermaxrevproxy_types.go +++ b/api/v1/csipowermaxrevproxy_types.go @@ -23,6 +23,7 @@ import ( // EDIT THIS FILE! THIS IS SCAFFOLDING FOR YOU TO OWN! // NOTE: json tags are required. Any new fields you add must have json tags for the fields to be serialized. + // ProxyLimits is used for storing the various types of limits // applied for a particular proxy instance type ProxyLimits struct { @@ -113,8 +114,8 @@ type CSIPowerMaxRevProxyStatus struct { // +kubebuilder:subresource:status // +kubebuilder:resource:path=csipowermaxrevproxies,scope=Namespaced -// +operator-sdk:csv:customresourcedefinitions:displayName="CSI PowerMax ReverseProxy",resources={{Deployment,v1,powermax-reverseproxy}} // CSIPowerMaxRevProxy is the Schema for the csipowermaxrevproxies API +// +operator-sdk:csv:customresourcedefinitions:displayName="CSI PowerMax ReverseProxy",resources={{Deployment,v1,powermax-reverseproxy}} type CSIPowerMaxRevProxy struct { metav1.TypeMeta `json:",inline"` metav1.ObjectMeta `json:"metadata,omitempty"` diff --git a/api/v1/csipowerstore_types.go b/api/v1/csipowerstore_types.go index 0c6358f..582e22f 100644 --- a/api/v1/csipowerstore_types.go +++ b/api/v1/csipowerstore_types.go @@ -33,8 +33,8 @@ type CSIPowerStoreSpec struct { // +kubebuilder:subresource:status // +kubebuilder:resource:path=csipowerstores,scope=Namespaced -// +operator-sdk:csv:customresourcedefinitions:displayName="CSI PowerStore",resources={{Deployment,v1,powerstore-controller},{DameonSet,v1,powerstore-node}} // CSIPowerStore is the Schema for the csipowerstores API +// +operator-sdk:csv:customresourcedefinitions:displayName="CSI PowerStore",resources={{Deployment,v1,powerstore-controller},{DameonSet,v1,powerstore-node}} type CSIPowerStore struct { metav1.TypeMeta `json:",inline"` metav1.ObjectMeta `json:"metadata,omitempty"` diff --git a/api/v1/csiunity_types.go b/api/v1/csiunity_types.go index 1f258f5..8f8bd63 100644 --- a/api/v1/csiunity_types.go +++ b/api/v1/csiunity_types.go @@ -33,8 +33,8 @@ type CSIUnitySpec struct { // +kubebuilder:subresource:status // +kubebuilder:resource:path=csiunities,scope=Namespaced -// +operator-sdk:csv:customresourcedefinitions:displayName="CSI Unity",resources={{Deployment,v1,unity-controller},{DameonSet,v1,unity-node}} // CSIUnity is the Schema for the csiunities API +// +operator-sdk:csv:customresourcedefinitions:displayName="CSI Unity",resources={{Deployment,v1,unity-controller},{DameonSet,v1,unity-node}} type CSIUnity struct { metav1.TypeMeta `json:",inline"` metav1.ObjectMeta `json:"metadata,omitempty"` diff --git a/api/v1/csivxflexos_types.go b/api/v1/csivxflexos_types.go index 4136c43..5b37ceb 100644 --- a/api/v1/csivxflexos_types.go +++ b/api/v1/csivxflexos_types.go @@ -33,8 +33,8 @@ type CSIVXFlexOSSpec struct { // +kubebuilder:subresource:status // +kubebuilder:resource:path=csivxflexoses,scope=Namespaced -// +operator-sdk:csv:customresourcedefinitions:displayName="CSI PowerFlex",resources={{Deployment,v1,vxflexos-controller},{DameonSet,v1,vxflexos-node}} // CSIVXFlexOS is the Schema for the csivxflexos API +// +operator-sdk:csv:customresourcedefinitions:displayName="CSI PowerFlex",resources={{Deployment,v1,vxflexos-controller},{DameonSet,v1,vxflexos-node}} type CSIVXFlexOS struct { metav1.TypeMeta `json:",inline"` metav1.ObjectMeta `json:"metadata,omitempty"` diff --git a/api/v1/types.go b/api/v1/types.go index aef68a6..c28b2e5 100644 --- a/api/v1/types.go +++ b/api/v1/types.go @@ -390,6 +390,8 @@ func (cr *CSIPowerStore) GetDriverEnvName() string { // DriverType - type representing the type of the driver. e.g. - powermax, unity type DriverType string + +// K8sVersion - type representing the kubernetes version. type K8sVersion string // CSIOperatorConditionType defines the type of the last status update diff --git a/api/v1/zz_generated.deepcopy.go b/api/v1/zz_generated.deepcopy.go index e15c1e3..9908c04 100644 --- a/api/v1/zz_generated.deepcopy.go +++ b/api/v1/zz_generated.deepcopy.go @@ -1,3 +1,4 @@ +//go:build !ignore_autogenerated // +build !ignore_autogenerated /* diff --git a/controllers/csiisilon_controller.go b/controllers/csiisilon_controller.go index 2745c69..ab7e9db 100644 --- a/controllers/csiisilon_controller.go +++ b/controllers/csiisilon_controller.go @@ -68,6 +68,7 @@ type CSIIsilonReconciler struct { // +kubebuilder:rbac:groups="coordination.k8s.io",resources=leases,verbs=get;list;watch;create;update;delete // +kubebuilder:rbac:groups="security.openshift.io",resources=securitycontextconstraints,resourceNames=privileged,verbs=use +// Reconcile function reconciles a CSIIsilon object func (r *CSIIsilonReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.Result, error) { log := r.Log.WithValues("csiisilon", req.NamespacedName) @@ -130,6 +131,7 @@ func (r *CSIIsilonReconciler) ValidateDriverSpec(ctx context.Context, instance s return nil } +// SetupWithManager - sets up the controller func (r *CSIIsilonReconciler) SetupWithManager(mgr ctrl.Manager) error { c, err := controller.New("CSIIsilon", mgr, controller.Options{Reconciler: r}) if err != nil { diff --git a/controllers/csipowermax_controller.go b/controllers/csipowermax_controller.go index 427d2fd..ed2cb2b 100644 --- a/controllers/csipowermax_controller.go +++ b/controllers/csipowermax_controller.go @@ -73,6 +73,7 @@ type CSIPowerMaxReconciler struct { // +kubebuilder:rbac:groups="coordination.k8s.io",resources=leases,verbs=get;list;watch;create;update;delete // +kubebuilder:rbac:groups="security.openshift.io",resources=securitycontextconstraints,resourceNames=privileged,verbs=use +// Reconcile function reconciles a CSIPowermax object func (r *CSIPowerMaxReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.Result, error) { log := r.Log.WithValues("csipowermax", req.NamespacedName) @@ -80,6 +81,7 @@ func (r *CSIPowerMaxReconciler) Reconcile(ctx context.Context, req ctrl.Request) return utils.Reconcile(ctx, instance, req, r, log) } +// SetupWithManager - sets up controller func (r *CSIPowerMaxReconciler) SetupWithManager(mgr ctrl.Manager) error { c, err := controller.New("CSIPowerMax", mgr, controller.Options{Reconciler: r}) if err != nil { diff --git a/controllers/csipowermaxrevproxy_controller.go b/controllers/csipowermaxrevproxy_controller.go index d5b2ec0..293ba3b 100644 --- a/controllers/csipowermaxrevproxy_controller.go +++ b/controllers/csipowermaxrevproxy_controller.go @@ -79,6 +79,7 @@ type CSIPowerMaxRevProxyReconciler struct { // +kubebuilder:rbac:groups=storage.dell.com,resources=csipowermaxrevproxies;csipowermaxrevproxies/finalizers;csipowermaxrevproxies/status,verbs=* +// Reconcile function reconciles a CSIPowerMax object func (r *CSIPowerMaxRevProxyReconciler) Reconcile(ctx context.Context, request ctrl.Request) (ctrl.Result, error) { reqLogger := log.WithValues("Request.Namespace", request.Namespace, "Request.Name", request.Name) reqLogger.Info("Reconciling CSIPowerMaxRevProxy") @@ -220,6 +221,7 @@ func (r *CSIPowerMaxRevProxyReconciler) Reconcile(ctx context.Context, request c return logBannerAndReturn(reconcile.Result{Requeue: true, RequeueAfter: retryInterval}, nil, reqLogger) } +// SetupWithManager - sets up controller func (r *CSIPowerMaxRevProxyReconciler) SetupWithManager(mgr ctrl.Manager) error { c, err := controller.New("CSIPowerMaxRevProxy", mgr, controller.Options{Reconciler: r}) if err != nil { diff --git a/controllers/csipowerstore_controller.go b/controllers/csipowerstore_controller.go index d85db1d..0621359 100644 --- a/controllers/csipowerstore_controller.go +++ b/controllers/csipowerstore_controller.go @@ -68,6 +68,7 @@ type CSIPowerStoreReconciler struct { // +kubebuilder:rbac:groups="coordination.k8s.io",resources=leases,verbs=get;list;watch;create;update;delete // +kubebuilder:rbac:groups="security.openshift.io",resources=securitycontextconstraints,resourceNames=privileged,verbs=use +//Reconcile function reconciles a CSIPowerStore Object func (r *CSIPowerStoreReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.Result, error) { log := r.Log.WithValues("csipowerstore", req.NamespacedName) @@ -76,6 +77,7 @@ func (r *CSIPowerStoreReconciler) Reconcile(ctx context.Context, req ctrl.Reques return utils.Reconcile(ctx, csiPowerStore, req, r, log) } +// SetupWithManager - sets up the controller func (r *CSIPowerStoreReconciler) SetupWithManager(mgr ctrl.Manager) error { c, err := controller.New("CSIPowerStore", mgr, controller.Options{Reconciler: r}) if err != nil { diff --git a/controllers/csiunity_controller.go b/controllers/csiunity_controller.go index b346da8..a6c9af7 100644 --- a/controllers/csiunity_controller.go +++ b/controllers/csiunity_controller.go @@ -75,6 +75,7 @@ type CSIUnityReconciler struct { // +kubebuilder:rbac:groups="coordination.k8s.io",resources=leases,verbs=get;list;watch;create;update;delete // +kubebuilder:rbac:groups="security.openshift.io",resources=securitycontextconstraints,resourceNames=privileged,verbs=use +// Reconcile function reconciles a CSIUnity Object func (r *CSIUnityReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.Result, error) { log := r.Log.WithValues("csiunity", req.NamespacedName) @@ -82,6 +83,7 @@ func (r *CSIUnityReconciler) Reconcile(ctx context.Context, req ctrl.Request) (c return utils.Reconcile(ctx, csiUnity, req, r, log) } +// SetupWithManager - sets up controller func (r *CSIUnityReconciler) SetupWithManager(mgr ctrl.Manager) error { c, err := controller.New("CSIUnity", mgr, controller.Options{Reconciler: r}) if err != nil { diff --git a/controllers/csivxflexos_controller.go b/controllers/csivxflexos_controller.go index bdde458..29d469f 100644 --- a/controllers/csivxflexos_controller.go +++ b/controllers/csivxflexos_controller.go @@ -21,10 +21,11 @@ import ( "fmt" "os" "regexp" - "sigs.k8s.io/yaml" "strings" "sync/atomic" + "sigs.k8s.io/yaml" + "github.com/dell/dell-csi-operator/pkg/utils" "github.com/go-logr/logr" @@ -74,6 +75,7 @@ type CSIVXFlexOSReconciler struct { // +kubebuilder:rbac:groups="coordination.k8s.io",resources=leases,verbs=get;list;watch;create;update;delete // +kubebuilder:rbac:groups="security.openshift.io",resources=securitycontextconstraints,resourceNames=privileged,verbs=use +// Reconcile function reconciles a CSIVFlex Object func (r *CSIVXFlexOSReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.Result, error) { log := r.Log.WithValues("csivxflexos", req.NamespacedName) @@ -131,7 +133,7 @@ func (r *CSIVXFlexOSReconciler) InitializeDriverSpec(instance storagev1.CSIDrive ctx := context.Background() if driver.ConfigVersion == "v5" { var newmdm corev1.EnvVar - mdm_var, err := r.GetMDMFromSecret(ctx, instance, reqLogger) + mdmVar, err := r.GetMDMFromSecret(ctx, instance, reqLogger) if err != nil { return false, err } @@ -141,7 +143,7 @@ func (r *CSIVXFlexOSReconciler) InitializeDriverSpec(instance storagev1.CSIDrive initenv := initcontainer.Envs for c, env := range initenv { if env.Name == "MDM" { - env.Value = mdm_var + env.Value = mdmVar newmdm = env k = c break @@ -160,7 +162,7 @@ func (r *CSIVXFlexOSReconciler) InitializeDriverSpec(instance storagev1.CSIDrive j := 0 for c, env := range sidenv { if env.Name == "MDM" { - env.Value = mdm_var + env.Value = mdmVar updatenv = env j = c break @@ -171,69 +173,68 @@ func (r *CSIVXFlexOSReconciler) InitializeDriverSpec(instance storagev1.CSIDrive } } return isDriverupdate, nil - } else { - mdm_ip := "" - mdm_fin := "" - ismdmip := false - for _, initcontainer := range driver.InitContainers { - if initcontainer.Name == "sdc" { - initenv := initcontainer.Envs - for i, env := range initenv { - if env.Name == "MDM" { - mdm_ip = env.Value - mdm_fin, ismdmip = ValidateIpAddress(mdm_ip) - if !ismdmip { - return false, fmt.Errorf("Invalid MDM value. Ip address should be nummeric and comma separated without space") - } - env.Value = mdm_fin - initenv[i] = env - break + } + mdmIP := "" + mdmFin := "" + ismdmip := false + for _, initcontainer := range driver.InitContainers { + if initcontainer.Name == "sdc" { + initenv := initcontainer.Envs + for i, env := range initenv { + if env.Name == "MDM" { + mdmIP = env.Value + mdmFin, ismdmip = ValidateIPAddress(mdmIP) + if !ismdmip { + return false, fmt.Errorf("Invalid MDM value. Ip address should be nummeric and comma separated without space") } + env.Value = mdmFin + initenv[i] = env + break } } } - for i, sidecar := range driver.SideCars { - if sidecar.Name == "sdc-monitor" { - sidenv := sidecar.Envs - updatenv, err := Getmdmipforsdc(sidenv, mdm_fin, reqLogger) - if err != nil { - return false, err - } - if len(updatenv.Value) > 0 { - sidenv = append(sidenv, updatenv) - } - sidecar.Envs = sidenv - driver.SideCars[i] = sidecar - isDriverupdate = true + } + for i, sidecar := range driver.SideCars { + if sidecar.Name == "sdc-monitor" { + sidenv := sidecar.Envs + updatenv, err := Getmdmipforsdc(sidenv, mdmFin, reqLogger) + if err != nil { + return false, err } + if len(updatenv.Value) > 0 { + sidenv = append(sidenv, updatenv) + } + sidecar.Envs = sidenv + driver.SideCars[i] = sidecar + isDriverupdate = true } - return isDriverupdate, nil } + return isDriverupdate, nil } // Getmdmipforsdc - Appends MDM value to sdc-monitor // Function to append MDM varibale to sdc-monitor environment varibale if not provided -func Getmdmipforsdc(sidenv []corev1.EnvVar, mdm_fin string, reqLogger logr.Logger) (corev1.EnvVar, error) { +func Getmdmipforsdc(sidenv []corev1.EnvVar, mdmFin string, reqLogger logr.Logger) (corev1.EnvVar, error) { var updatenv corev1.EnvVar - var mdm_found bool + var mdmFound bool for _, env := range sidenv { if env.Name == "MDM" { - mdm_found = true - existing_ip, ismdmip := ValidateIpAddress(env.Value) + mdmFound = true + existingIP, ismdmip := ValidateIPAddress(env.Value) if !ismdmip { return updatenv, fmt.Errorf("Invalid MDM value, ip address should be nummeric, comma separated without space") } - if mdm_fin != existing_ip { - env.Value = mdm_fin + if mdmFin != existingIP { + env.Value = mdmFin updatenv = env break } } } - if !mdm_found { + if !mdmFound { updatenv = corev1.EnvVar{ Name: "MDM", - Value: mdm_fin, + Value: mdmFin, } } return updatenv, nil @@ -265,24 +266,24 @@ func (r *CSIVXFlexOSReconciler) ValidateDriverSpec(ctx context.Context, instance return nil } -// ValidateIpAddress - Validates the Ip Address +// ValidateIPAddress - Validates the Ip Address // returns error if the Ip Address is not valid -func ValidateIpAddress(ipAdd string) (string, bool) { - trim_ip := strings.Split(ipAdd, ",") - if len(trim_ip) < 1 { +func ValidateIPAddress(ipAdd string) (string, bool) { + trimIP := strings.Split(ipAdd, ",") + if len(trimIP) < 1 { return "", false } - new_ip := "" - for i := range trim_ip { - trim_ip[i] = strings.TrimSpace(trim_ip[i]) - istrueip := IsIpv4Regex(trim_ip[i]) + newIP := "" + for i := range trimIP { + trimIP[i] = strings.TrimSpace(trimIP[i]) + istrueip := IsIpv4Regex(trimIP[i]) if istrueip { - new_ip = strings.Join(trim_ip[:], ",") + newIP = strings.Join(trimIP[:], ",") } else { - return new_ip, false + return newIP, false } } - return new_ip, true + return newIP, true } var ( @@ -295,6 +296,7 @@ func IsIpv4Regex(ipAddress string) bool { return ipRegex.MatchString(ipAddress) } +// GetMDMFromSecret - Returns MDM value func (r *CSIVXFlexOSReconciler) GetMDMFromSecret(ctx context.Context, instance storagev1.CSIDriver, log logr.Logger) (string, error) { client := r.GetClient() secretName := fmt.Sprintf("%s-config", instance.GetDriverType()) @@ -304,21 +306,21 @@ func (r *CSIVXFlexOSReconciler) GetMDMFromSecret(ctx context.Context, instance s } type StorageArrayConfig struct { - Username string `json:"username"` - Password string `json:"password"` - SystemID string `json:"systemId"` - Endpoint string `json:"endpoint"` + Username string `json:"username"` + Password string `json:"password"` + SystemID string `json:"systemId"` + Endpoint string `json:"endpoint"` SkipCertificateValidation bool `json:"skipCertificateValidation,omitempty"` AllSystemNames string `json:"allSystemNames"` - IsDefault bool `json:"isDefault,omitempty"` - MDM string `json:"mdm"` + IsDefault bool `json:"isDefault,omitempty"` + MDM string `json:"mdm"` } //To parse the secret json file data := credSecret.Data configBytes := data["config"] - mdm_val := "" - mdm_fin := "" + mdmVal := "" + mdmFin := "" ismdmip := false if string(configBytes) != "" { @@ -349,14 +351,14 @@ func (r *CSIVXFlexOSReconciler) GetMDMFromSecret(ctx context.Context, instance s return "", fmt.Errorf("invalid value for RestGateway at index [%d]", i) } if config.MDM != "" { - mdm_fin, ismdmip = ValidateIpAddress(config.MDM) + mdmFin, ismdmip = ValidateIPAddress(config.MDM) if !ismdmip { return "", fmt.Errorf("Invalid MDM value. Ip address should be numeric and comma separated without space") } if i == 0 { - mdm_val += mdm_fin + mdmVal += mdmFin } else { - mdm_val += "&" + mdm_fin + mdmVal += "&" + mdmFin } } if config.AllSystemNames != "" { @@ -380,10 +382,11 @@ func (r *CSIVXFlexOSReconciler) GetMDMFromSecret(ctx context.Context, instance s } else { return "", fmt.Errorf("Arrays details are not provided in vxflexos-config secret") } - fmt.Printf("mdm_val_fin: %s", mdm_val) - return mdm_val, nil + fmt.Printf("mdmValFin: %s", mdmVal) + return mdmVal, nil } +// SetupWithManager - sets up the controller func (r *CSIVXFlexOSReconciler) SetupWithManager(mgr ctrl.Manager) error { c, err := controller.New("CSIVXFlexOS", mgr, controller.Options{Reconciler: r}) if err != nil { diff --git a/core/semver/semver.go b/core/semver/semver.go index 90c03d4..74bba84 100644 --- a/core/semver/semver.go +++ b/core/semver/semver.go @@ -9,6 +9,7 @@ import ( "io/ioutil" "os" "os/exec" + "path/filepath" "regexp" "runtime" "strconv" @@ -57,7 +58,7 @@ func main() { format = "ver" } else { if fileExists(format) { - buf, err := ioutil.ReadFile(format) + buf, err := ioutil.ReadFile(filepath.Clean(format)) if err != nil { fmt.Fprintf(os.Stderr, "error: read tpl failed: %v\n", err) os.Exit(1) @@ -70,7 +71,7 @@ func main() { var w io.Writer = os.Stdout if len(output) > 0 { - fout, err := os.Create(output) + fout, err := os.Create(filepath.Clean(output)) if err != nil { fmt.Fprintf(os.Stderr, "error: %v\n", err) os.Exit(1) diff --git a/main.go b/main.go index fe0c26c..15c666d 100644 --- a/main.go +++ b/main.go @@ -19,7 +19,6 @@ package main import ( "flag" "fmt" - "gopkg.in/yaml.v2" "io/ioutil" "os" "path/filepath" @@ -29,6 +28,8 @@ import ( "strings" "time" + "gopkg.in/yaml.v2" + operatorconfig "github.com/dell/dell-csi-operator/pkg/config" "github.com/dell/dell-csi-operator/core" @@ -58,6 +59,8 @@ var ( // BaseKubernetesVersion - Kubernetes Version which the operator defaults to const BaseKubernetesVersion = "v121" + +// DefaultConfigFile - config.yaml is the default config file const DefaultConfigFile = "config.yaml" func init() { @@ -91,7 +94,7 @@ func kubeAPIServerVersion(configDirectory, configFile string) (storagev1.K8sVers kubeVersion := fmt.Sprintf("v%s%s", majorVersion, minorVersion) log.Info(fmt.Sprintf("Kubernetes Version: %s", kubeVersion)) supportedVersionFilePath := filepath.Join(configDirectory, configFile) - file, err := os.Open(supportedVersionFilePath) + file, err := os.Open(filepath.Clean(supportedVersionFilePath)) if err != nil { log.Info(fmt.Sprintf("Failed to find the config file %s", configFile)) return "", fmt.Errorf("missing config file") @@ -157,7 +160,7 @@ func getOperatorConfig() operatorconfig.Config { } else { // Check if the directory is empty fileName := configDir + "/" + configFile - _, err := os.Open(fileName) + _, err := os.Open(filepath.Clean(fileName)) if err != nil { // This means that the configmap is not mounted // fall back to the local copy diff --git a/pkg/ctrlconfig/config.go b/pkg/ctrlconfig/config.go index 1dc59b5..a28f2ec 100644 --- a/pkg/ctrlconfig/config.go +++ b/pkg/ctrlconfig/config.go @@ -103,7 +103,7 @@ const ( func readConfig(configDirectory string, driverVersion string, log logr.Logger) (DriverConfig, error) { var driverConfigMap DriverConfigMap jsonFileName := filepath.Join(configDirectory, fmt.Sprintf("%s.json", driverVersion)) - jsonFile, err := os.Open(jsonFileName) + jsonFile, err := os.Open(filepath.Clean(jsonFileName)) if err != nil { log.Error(err, "unable to find config file for driver") return DriverConfig{}, err diff --git a/pkg/ctrlconfig/opconfig.go b/pkg/ctrlconfig/opconfig.go index 7a74a43..2517603 100644 --- a/pkg/ctrlconfig/opconfig.go +++ b/pkg/ctrlconfig/opconfig.go @@ -2,11 +2,12 @@ package ctrlconfig import ( "fmt" - csiv1 "github.com/dell/dell-csi-operator/api/v1" - "gopkg.in/yaml.v2" "io/ioutil" "os" "path/filepath" + + csiv1 "github.com/dell/dell-csi-operator/api/v1" + "gopkg.in/yaml.v2" ) // OpConfig - Represents the image & RBAC config used by Operator @@ -61,7 +62,7 @@ type ContainerApp struct { func ReadOpConfig(configDirectory, configFileName string) (*OpConfig, error) { configFile := filepath.Join(configDirectory, configFileName) log.Info("Reading file for default image tags", "Filename", configFile) - jsonFile, err := os.Open(configFile) + jsonFile, err := os.Open(filepath.Clean(configFile)) if err != nil { return nil, err } diff --git a/pkg/resources/deployment/deployment.go b/pkg/resources/deployment/deployment.go index ce5bae9..3a019c1 100644 --- a/pkg/resources/deployment/deployment.go +++ b/pkg/resources/deployment/deployment.go @@ -3,6 +3,7 @@ package deployment import ( "context" "fmt" + csiv1 "github.com/dell/dell-csi-operator/api/v1" "github.com/dell/dell-csi-operator/pkg/constants" "github.com/dell/dell-csi-operator/pkg/ctrlconfig" @@ -177,6 +178,7 @@ func SyncControllerDeployment(ctx context.Context, deployment *appsv1.Deployment podTemplateSpec := found.Spec.Template.Spec for _, controllerPod := range podList.Items { + controllerPod := controllerPod podSpec := controllerPod.Spec if !comparePodSpec(podTemplateSpec, podSpec, reqLogger) { reqLogger.Info(fmt.Sprintf("Controller pod'spec doesn't match the spec from deployment. Pod Name: %s. Deleting it to force an update", diff --git a/pkg/resources/helper.go b/pkg/resources/helper.go index 1e5e4f8..58bd4e5 100644 --- a/pkg/resources/helper.go +++ b/pkg/resources/helper.go @@ -3,9 +3,10 @@ package resources import ( "bytes" "fmt" - rbacv1 "k8s.io/api/rbac/v1" "os" + rbacv1 "k8s.io/api/rbac/v1" + "github.com/Jeffail/gabs" csiv1 "github.com/dell/dell-csi-operator/api/v1" "github.com/dell/dell-csi-operator/pkg/constants" @@ -85,6 +86,7 @@ func GetOwnerReferences(driver csiv1.CSIDriver) []metav1.OwnerReference { return ownerReferences } +// GetDummyOwnerReferences - returns owner references func GetDummyOwnerReferences(clusterRole *rbacv1.ClusterRole) []metav1.OwnerReference { meta := &clusterRole.TypeMeta ownerReferences := []metav1.OwnerReference{ diff --git a/pkg/resources/rbac/controllerrbac.go b/pkg/resources/rbac/controllerrbac.go index bca4aad..e363d91 100644 --- a/pkg/resources/rbac/controllerrbac.go +++ b/pkg/resources/rbac/controllerrbac.go @@ -3,6 +3,7 @@ package rbac import ( "context" "fmt" + csiv1 "github.com/dell/dell-csi-operator/api/v1" "github.com/dell/dell-csi-operator/pkg/resources" "github.com/go-logr/logr" @@ -13,18 +14,20 @@ import ( "sigs.k8s.io/controller-runtime/pkg/client" ) +// NewDummyClusterRole - returns the cluster role func NewDummyClusterRole(name string) *rbacv1.ClusterRole { labels := map[string]string{ "name": name, } clusterRole := &rbacv1.ClusterRole{ ObjectMeta: metav1.ObjectMeta{ - Name: name, + Name: name, Labels: labels, }, } return clusterRole } + // NewControllerClusterRole - Returns a ClusterRole for the controller plugin func NewControllerClusterRole(instance csiv1.CSIDriver, customClusterRoleName bool, haRequired bool, dummyClusterRole *rbacv1.ClusterRole) *rbacv1.ClusterRole { driverName := instance.GetName() diff --git a/pkg/utils/status.go b/pkg/utils/status.go index e9da363..b3776ab 100644 --- a/pkg/utils/status.go +++ b/pkg/utils/status.go @@ -4,12 +4,13 @@ import ( "context" "encoding/json" "fmt" - "github.com/dell/dell-csi-operator/pkg/ctrlconfig" "hash/fnv" "math" "reflect" "time" + "github.com/dell/dell-csi-operator/pkg/ctrlconfig" + csiv1 "github.com/dell/dell-csi-operator/api/v1" "github.com/dell/dell-csi-operator/pkg/constants" "github.com/go-logr/logr" @@ -169,6 +170,7 @@ func getDaemonSetStatus(ctx context.Context, instance csiv1.CSIDriver, r Reconci return node.Status.DesiredNumberScheduled, csiv1.PodStatus{}, err } for _, pod := range podList.Items { + pod := pod if podutil.IsPodAvailable(&pod, node.Spec.MinReadySeconds, metav1.Now()) { available = append(available, pod.Name) } else if podutil.IsPodReady(&pod) { diff --git a/pkg/utils/utils.go b/pkg/utils/utils.go index 38f4cd3..fed2da3 100644 --- a/pkg/utils/utils.go +++ b/pkg/utils/utils.go @@ -207,6 +207,7 @@ func isEnvPresent(name string, envs []corev1.EnvVar) (bool, string) { return false, "" } +// GetSideCarTypeFromName - returns sidecar type from name func GetSideCarTypeFromName(sideCarName string) (csiv1.ImageType, error) { switch sideCarName { case csiv1.Provisioner: diff --git a/test/driver/operatorutils.go b/test/driver/operatorutils.go index 4ffd2ed..eae9012 100644 --- a/test/driver/operatorutils.go +++ b/test/driver/operatorutils.go @@ -2,14 +2,16 @@ package main import ( "fmt" + "os" + "path/filepath" + "strings" + csiv1 "github.com/dell/dell-csi-operator/api/v1" controllerutils "github.com/dell/dell-csi-operator/pkg/utils" corev1 "k8s.io/api/core/v1" "k8s.io/apimachinery/pkg/runtime" "k8s.io/apimachinery/pkg/runtime/schema" k8sJson "k8s.io/apimachinery/pkg/runtime/serializer/json" - "os" - "strings" ) type mockTyper struct { @@ -28,9 +30,10 @@ func (t *mockTyper) Recognizes(_ schema.GroupVersionKind) bool { return false } +// SideCar - structure of SideCar type SideCar struct { - Name string - Args string + Name string + Args string Image string } @@ -108,7 +111,7 @@ func readEnv() { parameterList := strings.TrimSuffix(pair[1], "]") parameterList = strings.TrimPrefix(parameterList, "[") args := strings.Split(parameterList, ",") - for i, _ := range args { + for i := range args { tempArg := strings.TrimSuffix(args[i], "\"") tempArg = strings.TrimPrefix(tempArg, "\"") pv := strings.Split(tempArg, "=") @@ -132,7 +135,7 @@ func readEnv() { parameterList := strings.TrimSuffix(pair[1], "]") parameterList = strings.TrimPrefix(parameterList, "[") args := strings.Split(parameterList, ",") - for i, _ := range args { + for i := range args { tempArg := strings.TrimSuffix(args[i], "\"") tempArg = strings.TrimPrefix(tempArg, "\"") pv := strings.Split(tempArg, "=") @@ -146,12 +149,12 @@ func readEnv() { } } -func getDriver() csiv1.Driver{ +func getDriver() csiv1.Driver { readEnv() commonEnvs := make([]corev1.EnvVar, 0) for k, v := range commonEnv { env := corev1.EnvVar{ - Name: k, + Name: k, Value: v, } commonEnvs = append(commonEnvs, env) @@ -159,7 +162,7 @@ func getDriver() csiv1.Driver{ controllerEnvs := make([]corev1.EnvVar, 0) for k, v := range controllerEnv { env := corev1.EnvVar{ - Name: k, + Name: k, Value: v, } controllerEnvs = append(controllerEnvs, env) @@ -167,7 +170,7 @@ func getDriver() csiv1.Driver{ nodeEnvs := make([]corev1.EnvVar, 0) for k, v := range nodeEnv { env := corev1.EnvVar{ - Name: k, + Name: k, Value: v, } nodeEnvs = append(nodeEnvs, env) @@ -185,7 +188,7 @@ func getDriver() csiv1.Driver{ argList := strings.TrimSuffix(v.Args, "]") argList = strings.TrimPrefix(argList, "[") args := strings.Split(argList, ",") - for i, _ := range args { + for i := range args { tempArg := strings.TrimSuffix(args[i], "\"") tempArg = strings.TrimPrefix(tempArg, "\"") args[i] = tempArg @@ -207,16 +210,16 @@ func getDriver() csiv1.Driver{ Replicas: 2, Common: csiv1.ContainerTemplate{ Image: driverMap["IMAGE"], - Envs: commonEnvs, - }, -/* Controller: csiv1.ContainerTemplate{ - Envs: controllerEnvs, + Envs: commonEnvs, }, - Node: csiv1.ContainerTemplate{ - Envs: nodeEnvs, - }, */ - SideCars: sideCars, - StorageClass: storageClasses, + /* Controller: csiv1.ContainerTemplate{ + Envs: controllerEnvs, + }, + Node: csiv1.ContainerTemplate{ + Envs: nodeEnvs, + }, */ + SideCars: sideCars, + StorageClass: storageClasses, SnapshotClass: snapshotClasses, } return driver @@ -269,8 +272,8 @@ func main() { } typer := &mockTyper{ gvk: &schema.GroupVersionKind{ - Kind: "CSIPowerMax", - Group: "storage.dell.com", + Kind: "CSIPowerMax", + Group: "storage.dell.com", Version: "v1", }, } @@ -285,7 +288,7 @@ func main() { }, ) outfilename := driverMap["MANIFEST"] - outFile, err := os.Create(outfilename) + outFile, err := os.Create(filepath.Clean(outfilename)) if err != nil { fmt.Println("Failed to create yaml file") } @@ -296,4 +299,3 @@ func main() { } fmt.Printf("Driver manifest: %s created successfully\n", outfilename) } - diff --git a/test/integration-tests/basic_test.go b/test/integration-tests/basic_test.go index 2893458..94bad4a 100644 --- a/test/integration-tests/basic_test.go +++ b/test/integration-tests/basic_test.go @@ -80,7 +80,7 @@ func TestBasic1PowerStore(t *testing.T) { } } -func testBasicDriver(driverType string, driverContext *DriverContext, initialDriverSpec v1.CSIDriver, afterDriverSpec v1.CSIDriver, unisphereUrl, isiEndpoint, isiPort, username, password, chapsecret, customDriverName string, isCHAPEnabled bool) error { +func testBasicDriver(driverType string, driverContext *DriverContext, initialDriverSpec v1.CSIDriver, afterDriverSpec v1.CSIDriver, unisphereURL, isiEndpoint, isiPort, username, password, chapsecret, customDriverName string, isCHAPEnabled bool) error { f := framework.Global secretName := driverType + "-creds" diff --git a/test/integration-tests/framework/framework.go b/test/integration-tests/framework/framework.go index a61c575..9360228 100644 --- a/test/integration-tests/framework/framework.go +++ b/test/integration-tests/framework/framework.go @@ -20,6 +20,7 @@ import ( "k8s.io/client-go/tools/clientcmd" ) +// Constants const ( Before = "before" After = "after" @@ -94,11 +95,13 @@ func Setup() error { return nil } +// GetRestClientForDellStorage - returns the rest client for storage func GetRestClientForDellStorage(kubeconfig string) (*rest.RESTClient, error) { var gv = schema.GroupVersion{Group: "storage.dell.com", Version: "v1"} return GetRestClient(&gv, kubeconfig) } +// GetRestClient - returns rest client func GetRestClient(schemaObj *schema.GroupVersion, kubeconfig string) (*rest.RESTClient, error) { cfg, err := clientcmd.BuildConfigFromFlags("", kubeconfig) if err != nil { diff --git a/test/integration-tests/utils/resources.go b/test/integration-tests/utils/resources.go index d22164e..ceb448c 100644 --- a/test/integration-tests/utils/resources.go +++ b/test/integration-tests/utils/resources.go @@ -37,7 +37,8 @@ func NewSecret(secretName, namespace, username, password, chapsecret string) *co return secret } -func JsonPrettyPrint(obj interface{}) string { +// JSONPrettyPrint - Indent the json output +func JSONPrettyPrint(obj interface{}) string { s, err := json.Marshal(obj) if err != nil { return "Not a instance" @@ -58,6 +59,7 @@ var DefaultRetry = wait.Backoff{ Jitter: 0.1, } +// SmallRetry holds parameters applied to a Backoff function var SmallRetry = wait.Backoff{ Steps: 5, Duration: 8 * time.Second, @@ -84,7 +86,7 @@ func Retry(backoff wait.Backoff, fn wait.ConditionFunc) error { return err } -// Waits for the given daemonset to available +// WaitForDaemonSetAvailable - Waits for the given daemonset to available func WaitForDaemonSetAvailable(namespace string, daemonSetName string, backoff wait.Backoff, kubeClient kubernetes.Interface, log *logrus.Logger) error { var err error log.Infof("Waiting for daemonset '%s' to available...\n", daemonSetName)