From c93f2fed1fa343c47497f24b602ba9b830b1561e Mon Sep 17 00:00:00 2001 From: Cheng Fang Date: Fri, 6 Sep 2024 08:06:09 -0400 Subject: [PATCH] chore(lint): update golangci-lint config and fix errors reported by golangci-lint (#1537) Signed-off-by: Cheng Fang --- .golangci.yml | 17 +++++++---------- api/v1alpha1/argocd_conversion.go | 2 ++ controllers/argocd/configmap.go | 2 ++ controllers/argocd/deployment.go | 3 ++- controllers/argocd/ingress.go | 2 ++ controllers/argocd/networkpolicies.go | 5 +++-- controllers/argocd/prometheus.go | 14 +++++++------- controllers/argocd/role_test.go | 2 +- controllers/argocd/route.go | 2 ++ controllers/argocd/secret.go | 2 ++ controllers/argocd/service.go | 2 ++ controllers/argocd/util.go | 2 +- .../notificationsconfiguration/configmap.go | 5 +++-- 13 files changed, 36 insertions(+), 24 deletions(-) diff --git a/.golangci.yml b/.golangci.yml index ef4c15761..882e48ad8 100644 --- a/.golangci.yml +++ b/.golangci.yml @@ -1,21 +1,18 @@ run: timeout: 2m - skip-files: - - ".*_test\\.go" - skip-dirs: - - vendor/ linters: enable: - - vet - - deadcode + - govet + - unused - goimports - - varcheck - - structcheck - ineffassign - unconvert - unparam +issues: + exclude-files: + - ".*_test\\.go" + exclude-dirs: + - vendor/ linters-settings: goimports: local-prefixes: github.com/argoproj-labs/argocd-operator -service: - golangci-lint-version: 1.56.2 diff --git a/api/v1alpha1/argocd_conversion.go b/api/v1alpha1/argocd_conversion.go index 63ca43a4f..2837c0289 100644 --- a/api/v1alpha1/argocd_conversion.go +++ b/api/v1alpha1/argocd_conversion.go @@ -61,6 +61,7 @@ func (src *ArgoCD) ConvertTo(dstRaw conversion.Hub) error { dst.Spec.ExtraConfig = src.Spec.ExtraConfig dst.Spec.GATrackingID = src.Spec.GATrackingID dst.Spec.GAAnonymizeUsers = src.Spec.GAAnonymizeUsers + //nolint:staticcheck dst.Spec.Grafana = *ConvertAlphaToBetaGrafana(&src.Spec.Grafana) dst.Spec.HA = *ConvertAlphaToBetaHA(&src.Spec.HA) dst.Spec.HelpChatURL = src.Spec.HelpChatURL @@ -130,6 +131,7 @@ func (dst *ArgoCD) ConvertFrom(srcRaw conversion.Hub) error { dst.Spec.ExtraConfig = src.Spec.ExtraConfig dst.Spec.GATrackingID = src.Spec.GATrackingID dst.Spec.GAAnonymizeUsers = src.Spec.GAAnonymizeUsers + //nolint:staticcheck dst.Spec.Grafana = *ConvertBetaToAlphaGrafana(&src.Spec.Grafana) dst.Spec.HA = *ConvertBetaToAlphaHA(&src.Spec.HA) dst.Spec.HelpChatURL = src.Spec.HelpChatURL diff --git a/controllers/argocd/configmap.go b/controllers/argocd/configmap.go index b51515267..eede784be 100644 --- a/controllers/argocd/configmap.go +++ b/controllers/argocd/configmap.go @@ -476,6 +476,7 @@ func (r *ReconcileArgoCD) reconcileArgoConfigMap(cr *argoproj.ArgoCD) error { // reconcileGrafanaConfiguration will ensure that the Grafana configuration ConfigMap is present. func (r *ReconcileArgoCD) reconcileGrafanaConfiguration(cr *argoproj.ArgoCD) error { + //nolint:staticcheck if !cr.Spec.Grafana.Enabled { return nil // Grafana not enabled, do nothing. } @@ -487,6 +488,7 @@ func (r *ReconcileArgoCD) reconcileGrafanaConfiguration(cr *argoproj.ArgoCD) err // reconcileGrafanaDashboards will ensure that the Grafana dashboards ConfigMap is present. func (r *ReconcileArgoCD) reconcileGrafanaDashboards(cr *argoproj.ArgoCD) error { + //nolint:staticcheck if !cr.Spec.Grafana.Enabled { return nil // Grafana not enabled, do nothing. } diff --git a/controllers/argocd/deployment.go b/controllers/argocd/deployment.go index cad590693..116f1b928 100644 --- a/controllers/argocd/deployment.go +++ b/controllers/argocd/deployment.go @@ -326,7 +326,7 @@ func getArgoServerCommand(cr *argoproj.ArgoCD, useTLSForRedis bool) []string { if err != nil { return cmd } - if cr.Spec.SourceNamespaces != nil && len(cr.Spec.SourceNamespaces) > 0 { + if len(cr.Spec.SourceNamespaces) > 0 { cmd = append(cmd, "--application-namespaces", fmt.Sprint(strings.Join(cr.Spec.SourceNamespaces, ","))) } @@ -451,6 +451,7 @@ func (r *ReconcileArgoCD) reconcileDeployments(cr *argoproj.ArgoCD, useTLSForRed // reconcileGrafanaDeployment will ensure the Deployment resource is present for the ArgoCD Grafana component. func (r *ReconcileArgoCD) reconcileGrafanaDeployment(cr *argoproj.ArgoCD) error { + //nolint:staticcheck if !cr.Spec.Grafana.Enabled { return nil // Grafana not enabled, do nothing. } diff --git a/controllers/argocd/ingress.go b/controllers/argocd/ingress.go index 824b5a0f6..b58d98f78 100644 --- a/controllers/argocd/ingress.go +++ b/controllers/argocd/ingress.go @@ -250,6 +250,7 @@ func (r *ReconcileArgoCD) reconcileArgoServerGRPCIngress(cr *argoproj.ArgoCD) er func (r *ReconcileArgoCD) reconcileGrafanaIngress(cr *argoproj.ArgoCD) error { ingress := newIngressWithSuffix("grafana", cr) if argoutil.IsObjectFound(r.Client, cr.Namespace, ingress.Name, ingress) { + //nolint:staticcheck if !cr.Spec.Grafana.Enabled || !cr.Spec.Grafana.Ingress.Enabled { // Ingress exists but enabled flag has been set to false, delete the Ingress return r.Client.Delete(context.TODO(), ingress) @@ -258,6 +259,7 @@ func (r *ReconcileArgoCD) reconcileGrafanaIngress(cr *argoproj.ArgoCD) error { return nil // Ingress found and enabled, do nothing } + //nolint:staticcheck if !cr.Spec.Grafana.Enabled || !cr.Spec.Grafana.Ingress.Enabled { return nil // Grafana itself or Ingress not enabled, move along... } diff --git a/controllers/argocd/networkpolicies.go b/controllers/argocd/networkpolicies.go index 80c7bc7a5..7ea323af9 100644 --- a/controllers/argocd/networkpolicies.go +++ b/controllers/argocd/networkpolicies.go @@ -5,13 +5,14 @@ import ( "fmt" "reflect" - argoproj "github.com/argoproj-labs/argocd-operator/api/v1beta1" - "github.com/argoproj-labs/argocd-operator/controllers/argoutil" corev1 "k8s.io/api/core/v1" networkingv1 "k8s.io/api/networking/v1" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" "k8s.io/apimachinery/pkg/util/intstr" "sigs.k8s.io/controller-runtime/pkg/controller/controllerutil" + + argoproj "github.com/argoproj-labs/argocd-operator/api/v1beta1" + "github.com/argoproj-labs/argocd-operator/controllers/argoutil" ) var ( diff --git a/controllers/argocd/prometheus.go b/controllers/argocd/prometheus.go index a1b24a1df..9f7e2fe36 100644 --- a/controllers/argocd/prometheus.go +++ b/controllers/argocd/prometheus.go @@ -279,7 +279,7 @@ func (r *ReconcileArgoCD) reconcilePrometheusRule(cr *argoproj.ArgoCD) error { }, Expr: intstr.IntOrString{ Type: intstr.String, - StrVal: fmt.Sprintf("kube_statefulset_status_replicas{statefulset=\"%s\", namespace=\"%s\"} != kube_statefulset_status_replicas_ready{statefulset=\"%s\", namespace=\"%s\"} ", fmt.Sprintf(cr.Name+"-application-controller"), cr.Namespace, fmt.Sprintf(cr.Name+"-application-controller"), cr.Namespace), + StrVal: fmt.Sprintf("kube_statefulset_status_replicas{statefulset=\"%s\", namespace=\"%s\"} != kube_statefulset_status_replicas_ready{statefulset=\"%s\", namespace=\"%s\"} ", cr.Name+"-application-controller", cr.Namespace, cr.Name+"-application-controller", cr.Namespace), }, For: "1m", Labels: map[string]string{ @@ -293,7 +293,7 @@ func (r *ReconcileArgoCD) reconcilePrometheusRule(cr *argoproj.ArgoCD) error { }, Expr: intstr.IntOrString{ Type: intstr.String, - StrVal: fmt.Sprintf("kube_deployment_status_replicas{deployment=\"%s\", namespace=\"%s\"} != kube_deployment_status_replicas_ready{deployment=\"%s\", namespace=\"%s\"} ", fmt.Sprintf(cr.Name+"-server"), cr.Namespace, fmt.Sprintf(cr.Name+"-server"), cr.Namespace), + StrVal: fmt.Sprintf("kube_deployment_status_replicas{deployment=\"%s\", namespace=\"%s\"} != kube_deployment_status_replicas_ready{deployment=\"%s\", namespace=\"%s\"} ", cr.Name+"-server", cr.Namespace, cr.Name+"-server", cr.Namespace), }, For: "1m", Labels: map[string]string{ @@ -307,7 +307,7 @@ func (r *ReconcileArgoCD) reconcilePrometheusRule(cr *argoproj.ArgoCD) error { }, Expr: intstr.IntOrString{ Type: intstr.String, - StrVal: fmt.Sprintf("kube_deployment_status_replicas{deployment=\"%s\", namespace=\"%s\"} != kube_deployment_status_replicas_ready{deployment=\"%s\", namespace=\"%s\"} ", fmt.Sprintf(cr.Name+"-repo-server"), cr.Namespace, fmt.Sprintf(cr.Name+"-repo-server"), cr.Namespace), + StrVal: fmt.Sprintf("kube_deployment_status_replicas{deployment=\"%s\", namespace=\"%s\"} != kube_deployment_status_replicas_ready{deployment=\"%s\", namespace=\"%s\"} ", cr.Name+"-repo-server", cr.Namespace, cr.Name+"-repo-server", cr.Namespace), }, For: "1m", Labels: map[string]string{ @@ -321,7 +321,7 @@ func (r *ReconcileArgoCD) reconcilePrometheusRule(cr *argoproj.ArgoCD) error { }, Expr: intstr.IntOrString{ Type: intstr.String, - StrVal: fmt.Sprintf("kube_deployment_status_replicas{deployment=\"%s\", namespace=\"%s\"} != kube_deployment_status_replicas_ready{deployment=\"%s\", namespace=\"%s\"} ", fmt.Sprintf(cr.Name+"-applicationset-controller"), cr.Namespace, fmt.Sprintf(cr.Name+"-applicationset-controller"), cr.Namespace), + StrVal: fmt.Sprintf("kube_deployment_status_replicas{deployment=\"%s\", namespace=\"%s\"} != kube_deployment_status_replicas_ready{deployment=\"%s\", namespace=\"%s\"} ", cr.Name+"-applicationset-controller", cr.Namespace, cr.Name+"-applicationset-controller", cr.Namespace), }, For: "5m", Labels: map[string]string{ @@ -335,7 +335,7 @@ func (r *ReconcileArgoCD) reconcilePrometheusRule(cr *argoproj.ArgoCD) error { }, Expr: intstr.IntOrString{ Type: intstr.String, - StrVal: fmt.Sprintf("kube_deployment_status_replicas{deployment=\"%s\", namespace=\"%s\"} != kube_deployment_status_replicas_ready{deployment=\"%s\", namespace=\"%s\"} ", fmt.Sprintf(cr.Name+"-dex-server"), cr.Namespace, fmt.Sprintf(cr.Name+"-dex-server"), cr.Namespace), + StrVal: fmt.Sprintf("kube_deployment_status_replicas{deployment=\"%s\", namespace=\"%s\"} != kube_deployment_status_replicas_ready{deployment=\"%s\", namespace=\"%s\"} ", cr.Name+"-dex-server", cr.Namespace, cr.Name+"-dex-server", cr.Namespace), }, For: "5m", Labels: map[string]string{ @@ -349,7 +349,7 @@ func (r *ReconcileArgoCD) reconcilePrometheusRule(cr *argoproj.ArgoCD) error { }, Expr: intstr.IntOrString{ Type: intstr.String, - StrVal: fmt.Sprintf("kube_deployment_status_replicas{deployment=\"%s\", namespace=\"%s\"} != kube_deployment_status_replicas_ready{deployment=\"%s\", namespace=\"%s\"} ", fmt.Sprintf(cr.Name+"-notifications-controller"), cr.Namespace, fmt.Sprintf(cr.Name+"-notifications-controller"), cr.Namespace), + StrVal: fmt.Sprintf("kube_deployment_status_replicas{deployment=\"%s\", namespace=\"%s\"} != kube_deployment_status_replicas_ready{deployment=\"%s\", namespace=\"%s\"} ", cr.Name+"-notifications-controller", cr.Namespace, cr.Name+"-notifications-controller", cr.Namespace), }, For: "5m", Labels: map[string]string{ @@ -363,7 +363,7 @@ func (r *ReconcileArgoCD) reconcilePrometheusRule(cr *argoproj.ArgoCD) error { }, Expr: intstr.IntOrString{ Type: intstr.String, - StrVal: fmt.Sprintf("kube_deployment_status_replicas{deployment=\"%s\", namespace=\"%s\"} != kube_deployment_status_replicas_ready{deployment=\"%s\", namespace=\"%s\"} ", fmt.Sprintf(cr.Name+"-redis"), cr.Namespace, fmt.Sprintf(cr.Name+"-redis"), cr.Namespace), + StrVal: fmt.Sprintf("kube_deployment_status_replicas{deployment=\"%s\", namespace=\"%s\"} != kube_deployment_status_replicas_ready{deployment=\"%s\", namespace=\"%s\"} ", cr.Name+"-redis", cr.Namespace, cr.Name+"-redis", cr.Namespace), }, For: "5m", Labels: map[string]string{ diff --git a/controllers/argocd/role_test.go b/controllers/argocd/role_test.go index 029e978b7..c5cd665e4 100644 --- a/controllers/argocd/role_test.go +++ b/controllers/argocd/role_test.go @@ -216,7 +216,7 @@ func TestReconcileArgoCD_reconcileRoleForApplicationSourceNamespaces(t *testing. assert.NoError(t, createNamespace(r, a.Namespace, "")) assert.NoError(t, createNamespaceManagedByClusterArgoCDLabel(r, sourceNamespace, a.Namespace)) - workloadIdentifier := common.ArgoCDServerComponent + workloadIdentifier := common.ArgoCDServerComponent + "-custom" expectedRules := policyRuleForServerApplicationSourceNamespaces() err := r.reconcileRoleForApplicationSourceNamespaces(workloadIdentifier, expectedRules, a) assert.NoError(t, err) diff --git a/controllers/argocd/route.go b/controllers/argocd/route.go index e600936fe..46482fecb 100644 --- a/controllers/argocd/route.go +++ b/controllers/argocd/route.go @@ -106,6 +106,7 @@ func (r *ReconcileArgoCD) reconcileRoutes(cr *argoproj.ArgoCD) error { func (r *ReconcileArgoCD) reconcileGrafanaRoute(cr *argoproj.ArgoCD) error { route := newRouteWithSuffix("grafana", cr) if argoutil.IsObjectFound(r.Client, cr.Namespace, route.Name, route) { + //nolint:staticcheck if !cr.Spec.Grafana.Enabled || !cr.Spec.Grafana.Route.Enabled { // Route exists but enabled flag has been set to false, delete the Route return r.Client.Delete(context.TODO(), route) @@ -114,6 +115,7 @@ func (r *ReconcileArgoCD) reconcileGrafanaRoute(cr *argoproj.ArgoCD) error { return nil // Route found, do nothing } + //nolint:staticcheck if !cr.Spec.Grafana.Enabled || !cr.Spec.Grafana.Route.Enabled { return nil // Grafana itself or Route not enabled, do nothing. } diff --git a/controllers/argocd/secret.go b/controllers/argocd/secret.go index f4ab94580..b1f58a73e 100644 --- a/controllers/argocd/secret.go +++ b/controllers/argocd/secret.go @@ -125,6 +125,7 @@ func newCertificateSecret(suffix string, caCert *x509.Certificate, caKey *rsa.Pr fmt.Sprintf("%s.%s.svc.cluster.local", cr.ObjectMeta.Name, cr.ObjectMeta.Namespace), } + //nolint:staticcheck if cr.Spec.Grafana.Enabled { log.Info(grafanaDeprecatedWarning) } @@ -366,6 +367,7 @@ func (r *ReconcileArgoCD) reconcileExistingArgoSecret(cr *argoproj.ArgoCD, secre // reconcileGrafanaSecret will ensure that the Grafana Secret is present. func (r *ReconcileArgoCD) reconcileGrafanaSecret(cr *argoproj.ArgoCD) error { + //nolint:staticcheck if !cr.Spec.Grafana.Enabled { return nil // Grafana not enabled, do nothing. } diff --git a/controllers/argocd/service.go b/controllers/argocd/service.go index e23410bb2..98bb02707 100644 --- a/controllers/argocd/service.go +++ b/controllers/argocd/service.go @@ -70,6 +70,7 @@ func newServiceWithSuffix(suffix string, component string, cr *argoproj.ArgoCD) func (r *ReconcileArgoCD) reconcileGrafanaService(cr *argoproj.ArgoCD) error { svc := newServiceWithSuffix("grafana", "grafana", cr) if argoutil.IsObjectFound(r.Client, cr.Namespace, svc.Name, svc) { + //nolint:staticcheck if !cr.Spec.Grafana.Enabled { // Service exists but enabled flag has been set to false, delete the Service return r.Client.Delete(context.TODO(), svc) @@ -78,6 +79,7 @@ func (r *ReconcileArgoCD) reconcileGrafanaService(cr *argoproj.ArgoCD) error { return nil // Service found, do nothing } + //nolint:staticcheck if !cr.Spec.Grafana.Enabled { return nil // Grafana not enabled, do nothing. } diff --git a/controllers/argocd/util.go b/controllers/argocd/util.go index b5f680fa8..323b00ebc 100644 --- a/controllers/argocd/util.go +++ b/controllers/argocd/util.go @@ -164,7 +164,7 @@ func getArgoApplicationControllerCommand(cr *argoproj.ArgoCD, useTLSForRedis boo cmd = append(cmd, "--status-processors", fmt.Sprint(getArgoServerStatusProcessors(cr))) cmd = append(cmd, "--kubectl-parallelism-limit", fmt.Sprint(getArgoControllerParellismLimit(cr))) - if cr.Spec.SourceNamespaces != nil && len(cr.Spec.SourceNamespaces) > 0 { + if len(cr.Spec.SourceNamespaces) > 0 { cmd = append(cmd, "--application-namespaces", fmt.Sprint(strings.Join(cr.Spec.SourceNamespaces, ","))) } diff --git a/controllers/notificationsconfiguration/configmap.go b/controllers/notificationsconfiguration/configmap.go index 1e2dc2141..fd81188e0 100644 --- a/controllers/notificationsconfiguration/configmap.go +++ b/controllers/notificationsconfiguration/configmap.go @@ -5,12 +5,13 @@ import ( "fmt" "reflect" - "github.com/argoproj-labs/argocd-operator/api/v1alpha1" - "github.com/argoproj-labs/argocd-operator/controllers/argoutil" corev1 "k8s.io/api/core/v1" "k8s.io/apimachinery/pkg/api/errors" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" "sigs.k8s.io/controller-runtime/pkg/controller/controllerutil" + + "github.com/argoproj-labs/argocd-operator/api/v1alpha1" + "github.com/argoproj-labs/argocd-operator/controllers/argoutil" ) const (