From bb0bc67243b22658c80ca32affa7c4341b5e3c58 Mon Sep 17 00:00:00 2001 From: Jakub Jarosz Date: Tue, 27 Sep 2022 15:12:52 +0100 Subject: [PATCH 1/2] Fix staticcheck linter issues in k8s package --- internal/k8s/controller.go | 42 +++++----- internal/k8s/controller_test.go | 141 ++++++++++++++++---------------- internal/k8s/handlers.go | 2 +- internal/k8s/handlers_test.go | 2 +- internal/k8s/status.go | 13 ++- internal/k8s/task_queue.go | 4 +- 6 files changed, 101 insertions(+), 103 deletions(-) diff --git a/internal/k8s/controller.go b/internal/k8s/controller.go index 75e2eec488..3f8e2f5358 100644 --- a/internal/k8s/controller.go +++ b/internal/k8s/controller.go @@ -2609,19 +2609,19 @@ func (lbc *LoadBalancerController) createIngressEx(ing *networking.Ingress, vali func (lbc *LoadBalancerController) getAppProtectLogConfAndDst(ing *networking.Ingress) ([]configs.AppProtectLog, error) { var apLogs []configs.AppProtectLog if _, exists := ing.Annotations[configs.AppProtectLogConfDstAnnotation]; !exists { - return apLogs, fmt.Errorf("Error: %v requires %v in %v", configs.AppProtectLogConfAnnotation, configs.AppProtectLogConfDstAnnotation, ing.Name) + return apLogs, fmt.Errorf("error: %v requires %v in %v", configs.AppProtectLogConfAnnotation, configs.AppProtectLogConfDstAnnotation, ing.Name) } logDsts := strings.Split(ing.Annotations[configs.AppProtectLogConfDstAnnotation], ",") logConfNsNs := appprotectcommon.ParseResourceReferenceAnnotationList(ing.Namespace, ing.Annotations[configs.AppProtectLogConfAnnotation]) if len(logDsts) != len(logConfNsNs) { - return apLogs, fmt.Errorf("Error Validating App Protect Destination and Config for Ingress %v: LogConf and LogDestination must have equal number of items", ing.Name) + return apLogs, fmt.Errorf("error Validating App Protect Destination and Config for Ingress %v: LogConf and LogDestination must have equal number of items", ing.Name) } for i, logConfNsN := range logConfNsNs { logConf, err := lbc.appProtectConfiguration.GetAppResource(appprotect.LogConfGVK.Kind, logConfNsN) if err != nil { - return apLogs, fmt.Errorf("Error retrieving App Protect Log Config for Ingress %v: %w", ing.Name, err) + return apLogs, fmt.Errorf("error retrieving App Protect Log Config for Ingress %v: %w", ing.Name, err) } apLogs = append(apLogs, configs.AppProtectLog{ LogConf: logConf, @@ -2637,7 +2637,7 @@ func (lbc *LoadBalancerController) getAppProtectPolicy(ing *networking.Ingress) apPolicy, err = lbc.appProtectConfiguration.GetAppResource(appprotect.PolicyGVK.Kind, polNsN) if err != nil { - return nil, fmt.Errorf("Error retrieving App Protect Policy for Ingress %v: %w", ing.Name, err) + return nil, fmt.Errorf("error retrieving App Protect Policy for Ingress %v: %w", ing.Name, err) } return apPolicy, nil @@ -2943,12 +2943,12 @@ func (lbc *LoadBalancerController) getPolicies(policies []conf_v1.PolicyReferenc } } if err != nil { - errors = append(errors, fmt.Errorf("Failed to get policy %s: %w", policyKey, err)) + errors = append(errors, fmt.Errorf("failed to get policy %s: %w", policyKey, err)) continue } if !exists { - errors = append(errors, fmt.Errorf("Policy %s doesn't exist", policyKey)) + errors = append(errors, fmt.Errorf("policy %s doesn't exist", policyKey)) continue } @@ -2961,7 +2961,7 @@ func (lbc *LoadBalancerController) getPolicies(policies []conf_v1.PolicyReferenc err = validation.ValidatePolicy(policy, lbc.isNginxPlus, lbc.enableOIDC, lbc.appProtectEnabled) if err != nil { - errors = append(errors, fmt.Errorf("Policy %s is invalid: %w", policyKey, err)) + errors = append(errors, fmt.Errorf("policy %s is invalid: %w", policyKey, err)) continue } @@ -3238,7 +3238,7 @@ func (lbc *LoadBalancerController) createTransportServerEx(transportServer *conf func (lbc *LoadBalancerController) getEndpointsForUpstream(namespace string, upstreamService string, upstreamPort uint16) (endps []podEndpoint, isExternal bool, err error) { svc, err := lbc.getServiceForUpstream(namespace, upstreamService, upstreamPort) if err != nil { - return nil, false, fmt.Errorf("Error getting service %v: %w", upstreamService, err) + return nil, false, fmt.Errorf("error getting service %v: %w", upstreamService, err) } backend := &networking.IngressBackend{ @@ -3252,7 +3252,7 @@ func (lbc *LoadBalancerController) getEndpointsForUpstream(namespace string, ups endps, isExternal, err = lbc.getEndpointsForIngressBackend(backend, svc) if err != nil { - return nil, false, fmt.Errorf("Error retrieving endpoints for the service %v: %w", upstreamService, err) + return nil, false, fmt.Errorf("error retrieving endpoints for the service %v: %w", upstreamService, err) } return endps, isExternal, err @@ -3261,7 +3261,7 @@ func (lbc *LoadBalancerController) getEndpointsForUpstream(namespace string, ups func (lbc *LoadBalancerController) getEndpointsForSubselector(namespace string, upstream conf_v1.Upstream) (endps []podEndpoint, err error) { svc, err := lbc.getServiceForUpstream(namespace, upstream.Service, upstream.Port) if err != nil { - return nil, fmt.Errorf("Error getting service %v: %w", upstream.Service, err) + return nil, fmt.Errorf("error getting service %v: %w", upstream.Service, err) } var targetPort int32 @@ -3270,19 +3270,19 @@ func (lbc *LoadBalancerController) getEndpointsForSubselector(namespace string, if port.Port == int32(upstream.Port) { targetPort, err = lbc.getTargetPort(port, svc) if err != nil { - return nil, fmt.Errorf("Error determining target port for port %v in service %v: %w", upstream.Port, svc.Name, err) + return nil, fmt.Errorf("error determining target port for port %v in service %v: %w", upstream.Port, svc.Name, err) } break } } if targetPort == 0 { - return nil, fmt.Errorf("No port %v in service %s", upstream.Port, svc.Name) + return nil, fmt.Errorf("no port %v in service %s", upstream.Port, svc.Name) } endps, err = lbc.getEndpointsForServiceWithSubselector(targetPort, upstream.Subselector, svc) if err != nil { - return nil, fmt.Errorf("Error retrieving endpoints for the service %v: %w", upstream.Service, err) + return nil, fmt.Errorf("error retrieving endpoints for the service %v: %w", upstream.Service, err) } return endps, err @@ -3297,7 +3297,7 @@ func (lbc *LoadBalancerController) getEndpointsForServiceWithSubselector(targetP } } if err != nil { - return nil, fmt.Errorf("Error getting pods in namespace %v that match the selector %v: %w", svc.Namespace, labels.Merge(svc.Spec.Selector, subselector), err) + return nil, fmt.Errorf("error getting pods in namespace %v that match the selector %v: %w", svc.Namespace, labels.Merge(svc.Spec.Selector, subselector), err) } var svcEps api_v1.Endpoints @@ -3432,7 +3432,7 @@ func (lbc *LoadBalancerController) getEndpointsForIngressBackend(backend *networ if err != nil { if svc.Spec.Type == api_v1.ServiceTypeExternalName { if !lbc.isNginxPlus { - return nil, false, fmt.Errorf("Type ExternalName Services feature is only available in NGINX Plus") + return nil, false, fmt.Errorf("type ExternalName Services feature is only available in NGINX Plus") } result = lbc.getExternalEndpointsForIngressBackend(backend, svc) return result, true, nil @@ -3457,14 +3457,14 @@ func (lbc *LoadBalancerController) getEndpointsForPort(endps api_v1.Endpoints, b if (backendPort.Name == "" && port.Port == backendPort.Number) || port.Name == backendPort.Name { targetPort, err = lbc.getTargetPort(port, svc) if err != nil { - return nil, fmt.Errorf("Error determining target port for port %v in Ingress: %w", backendPort, err) + return nil, fmt.Errorf("error determining target port for port %v in Ingress: %w", backendPort, err) } break } } if targetPort == 0 { - return nil, fmt.Errorf("No port %v in service %s", backendPort, svc.Name) + return nil, fmt.Errorf("no port %v in service %s", backendPort, svc.Name) } for _, subset := range endps.Subsets { @@ -3489,7 +3489,7 @@ func (lbc *LoadBalancerController) getEndpointsForPort(endps api_v1.Endpoints, b } } - return nil, fmt.Errorf("No endpoints for target port %v in service %s", targetPort, svc.Name) + return nil, fmt.Errorf("no endpoints for target port %v in service %s", targetPort, svc.Name) } func (lbc *LoadBalancerController) getPodOwnerTypeAndNameFromAddress(ns, name string) (parentType, parentName string) { @@ -3556,18 +3556,18 @@ func (lbc *LoadBalancerController) getTargetPort(svcPort api_v1.ServicePort, svc } } if err != nil { - return 0, fmt.Errorf("Error getting pod information: %w", err) + return 0, fmt.Errorf("error getting pod information: %w", err) } if len(pods) == 0 { - return 0, fmt.Errorf("No pods of service %s", svc.Name) + return 0, fmt.Errorf("no pods of service %s", svc.Name) } pod := pods[0] portNum, err := findPort(pod, svcPort) if err != nil { - return 0, fmt.Errorf("Error finding named port %v in pod %s: %w", svcPort, pod.Name, err) + return 0, fmt.Errorf("error finding named port %v in pod %s: %w", svcPort, pod.Name, err) } return portNum, nil diff --git a/internal/k8s/controller_test.go b/internal/k8s/controller_test.go index c13a289c7e..6034380aa1 100644 --- a/internal/k8s/controller_test.go +++ b/internal/k8s/controller_test.go @@ -19,7 +19,6 @@ import ( conf_v1 "github.com/nginxinc/kubernetes-ingress/pkg/apis/configuration/v1" conf_v1alpha1 "github.com/nginxinc/kubernetes-ingress/pkg/apis/configuration/v1alpha1" api_v1 "k8s.io/api/core/v1" - v1 "k8s.io/api/core/v1" networking "k8s.io/api/networking/v1" meta_v1 "k8s.io/apimachinery/pkg/apis/meta/v1" "k8s.io/apimachinery/pkg/apis/meta/v1/unstructured" @@ -221,43 +220,43 @@ func TestIngressClassForCustomResources(t *testing.T) { func TestComparePorts(t *testing.T) { scenarios := []struct { - sp v1.ServicePort - cp v1.ContainerPort + sp api_v1.ServicePort + cp api_v1.ContainerPort expected bool }{ { // match TargetPort.strval and Protocol - v1.ServicePort{ + api_v1.ServicePort{ TargetPort: intstr.FromString("name"), - Protocol: v1.ProtocolTCP, + Protocol: api_v1.ProtocolTCP, }, - v1.ContainerPort{ + api_v1.ContainerPort{ Name: "name", - Protocol: v1.ProtocolTCP, + Protocol: api_v1.ProtocolTCP, ContainerPort: 80, }, true, }, { // don't match Name and Protocol - v1.ServicePort{ + api_v1.ServicePort{ Name: "name", - Protocol: v1.ProtocolTCP, + Protocol: api_v1.ProtocolTCP, }, - v1.ContainerPort{ + api_v1.ContainerPort{ Name: "name", - Protocol: v1.ProtocolTCP, + Protocol: api_v1.ProtocolTCP, ContainerPort: 80, }, false, }, { // TargetPort intval mismatch, don't match by TargetPort.Name - v1.ServicePort{ + api_v1.ServicePort{ Name: "name", TargetPort: intstr.FromInt(80), }, - v1.ContainerPort{ + api_v1.ContainerPort{ Name: "name", ContainerPort: 81, }, @@ -265,23 +264,23 @@ func TestComparePorts(t *testing.T) { }, { // match by TargetPort intval - v1.ServicePort{ + api_v1.ServicePort{ TargetPort: intstr.IntOrString{ IntVal: 80, }, }, - v1.ContainerPort{ + api_v1.ContainerPort{ ContainerPort: 80, }, true, }, { // Fall back on ServicePort.Port if TargetPort is empty - v1.ServicePort{ + api_v1.ServicePort{ Name: "name", Port: 80, }, - v1.ContainerPort{ + api_v1.ContainerPort{ Name: "name", ContainerPort: 80, }, @@ -289,18 +288,18 @@ func TestComparePorts(t *testing.T) { }, { // TargetPort intval mismatch - v1.ServicePort{ + api_v1.ServicePort{ TargetPort: intstr.FromInt(80), }, - v1.ContainerPort{ + api_v1.ContainerPort{ ContainerPort: 81, }, false, }, { // don't match empty ports - v1.ServicePort{}, - v1.ContainerPort{}, + api_v1.ServicePort{}, + api_v1.ContainerPort{}, false, }, } @@ -313,14 +312,14 @@ func TestComparePorts(t *testing.T) { } func TestFindProbeForPods(t *testing.T) { - pods := []*v1.Pod{ + pods := []*api_v1.Pod{ { - Spec: v1.PodSpec{ - Containers: []v1.Container{ + Spec: api_v1.PodSpec{ + Containers: []api_v1.Container{ { - ReadinessProbe: &v1.Probe{ - ProbeHandler: v1.ProbeHandler{ - HTTPGet: &v1.HTTPGetAction{ + ReadinessProbe: &api_v1.Probe{ + ProbeHandler: api_v1.ProbeHandler{ + HTTPGet: &api_v1.HTTPGetAction{ Path: "/", Host: "asdf.com", Port: intstr.IntOrString{ @@ -330,11 +329,11 @@ func TestFindProbeForPods(t *testing.T) { }, PeriodSeconds: 42, }, - Ports: []v1.ContainerPort{ + Ports: []api_v1.ContainerPort{ { Name: "name", ContainerPort: 80, - Protocol: v1.ProtocolTCP, + Protocol: api_v1.ProtocolTCP, HostIP: "1.2.3.4", }, }, @@ -343,7 +342,7 @@ func TestFindProbeForPods(t *testing.T) { }, }, } - svcPort := v1.ServicePort{ + svcPort := api_v1.ServicePort{ TargetPort: intstr.FromInt(80), } probe := findProbeForPods(pods, &svcPort) @@ -351,25 +350,25 @@ func TestFindProbeForPods(t *testing.T) { t.Errorf("ServicePort.TargetPort as int match failed: %+v", probe) } - svcPort = v1.ServicePort{ + svcPort = api_v1.ServicePort{ TargetPort: intstr.FromString("name"), - Protocol: v1.ProtocolTCP, + Protocol: api_v1.ProtocolTCP, } probe = findProbeForPods(pods, &svcPort) if probe == nil || probe.PeriodSeconds != 42 { t.Errorf("ServicePort.TargetPort as string failed: %+v", probe) } - svcPort = v1.ServicePort{ + svcPort = api_v1.ServicePort{ TargetPort: intstr.FromInt(80), - Protocol: v1.ProtocolTCP, + Protocol: api_v1.ProtocolTCP, } probe = findProbeForPods(pods, &svcPort) if probe == nil || probe.PeriodSeconds != 42 { t.Errorf("ServicePort.TargetPort as int failed: %+v", probe) } - svcPort = v1.ServicePort{ + svcPort = api_v1.ServicePort{ Port: 80, } probe = findProbeForPods(pods, &svcPort) @@ -377,7 +376,7 @@ func TestFindProbeForPods(t *testing.T) { t.Errorf("ServicePort.Port should match if TargetPort is not set: %+v", probe) } - svcPort = v1.ServicePort{ + svcPort = api_v1.ServicePort{ TargetPort: intstr.FromString("wrong_name"), } probe = findProbeForPods(pods, &svcPort) @@ -385,7 +384,7 @@ func TestFindProbeForPods(t *testing.T) { t.Errorf("ServicePort.TargetPort should not have matched string: %+v", probe) } - svcPort = v1.ServicePort{ + svcPort = api_v1.ServicePort{ TargetPort: intstr.FromInt(22), } probe = findProbeForPods(pods, &svcPort) @@ -393,7 +392,7 @@ func TestFindProbeForPods(t *testing.T) { t.Errorf("ServicePort.TargetPort should not have matched int: %+v", probe) } - svcPort = v1.ServicePort{ + svcPort = api_v1.ServicePort{ Port: 22, } probe = findProbeForPods(pods, &svcPort) @@ -411,14 +410,14 @@ func TestGetServicePortForIngressPort(t *testing.T) { configurator: cnf, metricsCollector: collectors.NewControllerFakeCollector(), } - svc := v1.Service{ + svc := api_v1.Service{ TypeMeta: meta_v1.TypeMeta{}, ObjectMeta: meta_v1.ObjectMeta{ Name: "coffee-svc", Namespace: "default", }, - Spec: v1.ServiceSpec{ - Ports: []v1.ServicePort{ + Spec: api_v1.ServiceSpec{ + Ports: []api_v1.ServicePort{ { Name: "foo", Port: 80, @@ -426,7 +425,7 @@ func TestGetServicePortForIngressPort(t *testing.T) { }, }, }, - Status: v1.ServiceStatus{}, + Status: api_v1.ServiceStatus{}, } backendPort := networking.ServiceBackendPort{ Name: "foo", @@ -476,7 +475,7 @@ func TestGetEndpointsBySubselectedPods(t *testing.T) { tests := []struct { desc string targetPort int32 - svcEps v1.Endpoints + svcEps api_v1.Endpoints expectedEps []podEndpoint }{ { @@ -499,7 +498,7 @@ func TestGetEndpointsBySubselectedPods(t *testing.T) { }, } - pods := []*v1.Pod{ + pods := []*api_v1.Pod{ { ObjectMeta: meta_v1.ObjectMeta{ OwnerReferences: []meta_v1.OwnerReference{ @@ -510,22 +509,22 @@ func TestGetEndpointsBySubselectedPods(t *testing.T) { }, }, }, - Status: v1.PodStatus{ + Status: api_v1.PodStatus{ PodIP: "1.2.3.4", }, }, } - svcEps := v1.Endpoints{ - Subsets: []v1.EndpointSubset{ + svcEps := api_v1.Endpoints{ + Subsets: []api_v1.EndpointSubset{ { - Addresses: []v1.EndpointAddress{ + Addresses: []api_v1.EndpointAddress{ { IP: "1.2.3.4", Hostname: "asdf.com", }, }, - Ports: []v1.EndpointPort{ + Ports: []api_v1.EndpointPort{ { Port: 80, }, @@ -687,9 +686,9 @@ func TestGetPolicies(t *testing.T) { expectedPolicies := []*conf_v1.Policy{validPolicy} expectedErrors := []error{ - errors.New("Policy default/invalid-policy is invalid: spec: Invalid value: \"\": must specify exactly one of: `accessControl`, `rateLimit`, `ingressMTLS`, `egressMTLS`, `basicAuth`, `jwt`, `oidc`, `waf`"), - errors.New("Policy nginx-ingress/valid-policy doesn't exist"), - errors.New("Failed to get policy nginx-ingress/some-policy: GetByKey error"), + errors.New("policy default/invalid-policy is invalid: spec: Invalid value: \"\": must specify exactly one of: `accessControl`, `rateLimit`, `ingressMTLS`, `egressMTLS`, `basicAuth`, `jwt`, `oidc`, `waf`"), + errors.New("policy nginx-ingress/valid-policy doesn't exist"), + errors.New("failed to get policy nginx-ingress/some-policy: GetByKey error"), errors.New("referenced policy default/valid-policy-ingress-class has incorrect ingress class: test-class (controller ingress class: )"), } @@ -762,37 +761,37 @@ func TestGetPodOwnerTypeAndName(t *testing.T) { desc string expType string expName string - pod *v1.Pod + pod *api_v1.Pod }{ { desc: "deployment", expType: "deployment", expName: "deploy-name", - pod: &v1.Pod{ObjectMeta: createTestObjMeta("Deployment", "deploy-name", true)}, + pod: &api_v1.Pod{ObjectMeta: createTestObjMeta("Deployment", "deploy-name", true)}, }, { desc: "stateful set", expType: "statefulset", expName: "statefulset-name", - pod: &v1.Pod{ObjectMeta: createTestObjMeta("StatefulSet", "statefulset-name", true)}, + pod: &api_v1.Pod{ObjectMeta: createTestObjMeta("StatefulSet", "statefulset-name", true)}, }, { desc: "daemon set", expType: "daemonset", expName: "daemonset-name", - pod: &v1.Pod{ObjectMeta: createTestObjMeta("DaemonSet", "daemonset-name", true)}, + pod: &api_v1.Pod{ObjectMeta: createTestObjMeta("DaemonSet", "daemonset-name", true)}, }, { desc: "replica set with no pod hash", expType: "deployment", expName: "replicaset-name", - pod: &v1.Pod{ObjectMeta: createTestObjMeta("ReplicaSet", "replicaset-name", false)}, + pod: &api_v1.Pod{ObjectMeta: createTestObjMeta("ReplicaSet", "replicaset-name", false)}, }, { desc: "replica set with pod hash", expType: "deployment", expName: "replicaset-name", - pod: &v1.Pod{ + pod: &api_v1.Pod{ ObjectMeta: createTestObjMeta("ReplicaSet", "replicaset-name-67c6f7c5fd", true), }, }, @@ -800,7 +799,7 @@ func TestGetPodOwnerTypeAndName(t *testing.T) { desc: "nil controller should use default values", expType: "deployment", expName: "deploy-name", - pod: &v1.Pod{ + pod: &api_v1.Pod{ ObjectMeta: meta_v1.ObjectMeta{ OwnerReferences: []meta_v1.OwnerReference{ { @@ -1156,14 +1155,14 @@ func errorComparer(e1, e2 error) bool { func TestAddJWTSecrets(t *testing.T) { invalidErr := errors.New("invalid") - validJWKSecret := &v1.Secret{ + validJWKSecret := &api_v1.Secret{ ObjectMeta: meta_v1.ObjectMeta{ Name: "valid-jwk-secret", Namespace: "default", }, Type: secrets.SecretTypeJWK, } - invalidJWKSecret := &v1.Secret{ + invalidJWKSecret := &api_v1.Secret{ ObjectMeta: meta_v1.ObjectMeta{ Name: "invalid-jwk-secret", Namespace: "default", @@ -1280,14 +1279,14 @@ func TestAddJWTSecrets(t *testing.T) { func TestAddBasicSecrets(t *testing.T) { invalidErr := errors.New("invalid") - validBasicSecret := &v1.Secret{ + validBasicSecret := &api_v1.Secret{ ObjectMeta: meta_v1.ObjectMeta{ Name: "valid-basic-auth-secret", Namespace: "default", }, Type: secrets.SecretTypeJWK, } - invalidBasicSecret := &v1.Secret{ + invalidBasicSecret := &api_v1.Secret{ ObjectMeta: meta_v1.ObjectMeta{ Name: "invalid-basic-auth-secret", Namespace: "default", @@ -1404,14 +1403,14 @@ func TestAddBasicSecrets(t *testing.T) { func TestAddIngressMTLSSecret(t *testing.T) { invalidErr := errors.New("invalid") - validSecret := &v1.Secret{ + validSecret := &api_v1.Secret{ ObjectMeta: meta_v1.ObjectMeta{ Name: "valid-ingress-mtls-secret", Namespace: "default", }, Type: secrets.SecretTypeCA, } - invalidSecret := &v1.Secret{ + invalidSecret := &api_v1.Secret{ ObjectMeta: meta_v1.ObjectMeta{ Name: "invalid-ingress-mtls-secret", Namespace: "default", @@ -1526,28 +1525,28 @@ func TestAddIngressMTLSSecret(t *testing.T) { func TestAddEgressMTLSSecrets(t *testing.T) { invalidErr := errors.New("invalid") - validMTLSSecret := &v1.Secret{ + validMTLSSecret := &api_v1.Secret{ ObjectMeta: meta_v1.ObjectMeta{ Name: "valid-egress-mtls-secret", Namespace: "default", }, Type: api_v1.SecretTypeTLS, } - validTrustedSecret := &v1.Secret{ + validTrustedSecret := &api_v1.Secret{ ObjectMeta: meta_v1.ObjectMeta{ Name: "valid-egress-trusted-secret", Namespace: "default", }, Type: secrets.SecretTypeCA, } - invalidMTLSSecret := &v1.Secret{ + invalidMTLSSecret := &api_v1.Secret{ ObjectMeta: meta_v1.ObjectMeta{ Name: "invalid-egress-mtls-secret", Namespace: "default", }, Type: api_v1.SecretTypeTLS, } - invalidTrustedSecret := &v1.Secret{ + invalidTrustedSecret := &api_v1.Secret{ ObjectMeta: meta_v1.ObjectMeta{ Name: "invalid-egress-trusted-secret", Namespace: "default", @@ -1743,7 +1742,7 @@ func TestAddEgressMTLSSecrets(t *testing.T) { func TestAddOidcSecret(t *testing.T) { invalidErr := errors.New("invalid") - validSecret := &v1.Secret{ + validSecret := &api_v1.Secret{ ObjectMeta: meta_v1.ObjectMeta{ Name: "valid-oidc-secret", Namespace: "default", @@ -1753,7 +1752,7 @@ func TestAddOidcSecret(t *testing.T) { }, Type: secrets.SecretTypeOIDC, } - invalidSecret := &v1.Secret{ + invalidSecret := &api_v1.Secret{ ObjectMeta: meta_v1.ObjectMeta{ Name: "invalid-oidc-secret", Namespace: "default", diff --git a/internal/k8s/handlers.go b/internal/k8s/handlers.go index e520acf4b3..5cb40e271a 100644 --- a/internal/k8s/handlers.go +++ b/internal/k8s/handlers.go @@ -539,7 +539,7 @@ func areResourcesDifferent(oldresource, resource *unstructured.Unstructured) (bo return false, err } if !found { - return false, fmt.Errorf("Error, spec has unexpected format") + return false, fmt.Errorf("spec has unexpected format") } eq := reflect.DeepEqual(oldSpec, spec) if eq { diff --git a/internal/k8s/handlers_test.go b/internal/k8s/handlers_test.go index e2b880e706..eaed6eb2bc 100644 --- a/internal/k8s/handlers_test.go +++ b/internal/k8s/handlers_test.go @@ -204,7 +204,7 @@ func TestAreResourcesDifferent(t *testing.T) { Object: map[string]interface{}{}, }, expected: false, - expectErr: errors.New(`Error, spec has unexpected format`), + expectErr: errors.New(`spec has unexpected format`), msg: "new resource with missing spec", }, { diff --git a/internal/k8s/status.go b/internal/k8s/status.go index a8ab81c508..672c8aeeb8 100644 --- a/internal/k8s/status.go +++ b/internal/k8s/status.go @@ -10,7 +10,6 @@ import ( "github.com/golang/glog" conf_v1 "github.com/nginxinc/kubernetes-ingress/pkg/apis/configuration/v1" - v1 "github.com/nginxinc/kubernetes-ingress/pkg/apis/configuration/v1" conf_v1alpha1 "github.com/nginxinc/kubernetes-ingress/pkg/apis/configuration/v1alpha1" k8s_nginx "github.com/nginxinc/kubernetes-ingress/pkg/client/clientset/versioned" api_v1 "k8s.io/api/core/v1" @@ -35,7 +34,7 @@ type statusUpdater struct { externalServicePorts string bigIPAddress string bigIPPorts string - externalEndpoints []v1.ExternalEndpoint + externalEndpoints []conf_v1.ExternalEndpoint status []api_v1.LoadBalancerIngress statusInitialized bool keyFunc func(obj interface{}) (string, error) @@ -522,7 +521,7 @@ func hasVsrStatusChanged(vsr *conf_v1.VirtualServerRoute, state string, reason s } // UpdateVirtualServerRouteStatusWithReferencedBy updates the status of a VirtualServerRoute, including the referencedBy field. -func (su *statusUpdater) UpdateVirtualServerRouteStatusWithReferencedBy(vsr *conf_v1.VirtualServerRoute, state string, reason string, message string, referencedBy []*v1.VirtualServer) error { +func (su *statusUpdater) UpdateVirtualServerRouteStatusWithReferencedBy(vsr *conf_v1.VirtualServerRoute, state string, reason string, message string, referencedBy []*conf_v1.VirtualServer) error { var referencedByString string if len(referencedBy) != 0 { vs := referencedBy[0] @@ -687,12 +686,12 @@ func (su *statusUpdater) generateExternalEndpointsFromStatus(status []api_v1.Loa return externalEndpoints } -func hasPolicyStatusChanged(pol *v1.Policy, state string, reason string, message string) bool { +func hasPolicyStatusChanged(pol *conf_v1.Policy, state string, reason string, message string) bool { return pol.Status.State != state || pol.Status.Reason != reason || pol.Status.Message != message } // UpdatePolicyStatus updates the status of a Policy. -func (su *statusUpdater) UpdatePolicyStatus(pol *v1.Policy, state string, reason string, message string) error { +func (su *statusUpdater) UpdatePolicyStatus(pol *conf_v1.Policy, state string, reason string, message string) error { // Get an up-to-date Policy from the Store var polLatest interface{} var exists bool @@ -717,7 +716,7 @@ func (su *statusUpdater) UpdatePolicyStatus(pol *v1.Policy, state string, reason return nil } - polCopy := polLatest.(*v1.Policy) + polCopy := polLatest.(*conf_v1.Policy) if !hasPolicyStatusChanged(polCopy, state, reason, message) { return nil @@ -736,7 +735,7 @@ func (su *statusUpdater) UpdatePolicyStatus(pol *v1.Policy, state string, reason return nil } -func (su *statusUpdater) retryUpdatePolicyStatus(polCopy *v1.Policy) error { +func (su *statusUpdater) retryUpdatePolicyStatus(polCopy *conf_v1.Policy) error { pol, err := su.confClient.K8sV1().Policies(polCopy.Namespace).Get(context.TODO(), polCopy.Name, metav1.GetOptions{}) if err != nil { return err diff --git a/internal/k8s/task_queue.go b/internal/k8s/task_queue.go index 5a77f4e718..021b415ed4 100644 --- a/internal/k8s/task_queue.go +++ b/internal/k8s/task_queue.go @@ -173,10 +173,10 @@ func newTask(key string, obj interface{}) (task, error) { } else if objectKind == appprotectdos.DosLogConfGVK.Kind { k = appProtectDosLogConf } else { - return task{}, fmt.Errorf("Unknown unstructured kind: %v", objectKind) + return task{}, fmt.Errorf("unknown unstructured kind: %v", objectKind) } default: - return task{}, fmt.Errorf("Unknown type: %v", t) + return task{}, fmt.Errorf("unknown type: %v", t) } return task{k, key}, nil From d17e6908305bb6a446fbe0b899bed5f4b4adb31c Mon Sep 17 00:00:00 2001 From: Jakub Jarosz Date: Tue, 27 Sep 2022 16:27:29 +0100 Subject: [PATCH 2/2] Fix linter issues in nginx package --- internal/nginx/manager.go | 4 ++-- internal/nginx/utils.go | 8 ++++---- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/internal/nginx/manager.go b/internal/nginx/manager.go index 93949f20fd..713d9053fd 100644 --- a/internal/nginx/manager.go +++ b/internal/nginx/manager.go @@ -235,7 +235,7 @@ func (lm *LocalManager) CreateDHParam(content string) (string, error) { err := createFileAndWrite(lm.dhparamFilename, []byte(content)) if err != nil { - return lm.dhparamFilename, fmt.Errorf("Failed to write dhparam file from %v: %w", lm.dhparamFilename, err) + return lm.dhparamFilename, fmt.Errorf("failed to write dhparam file from %v: %w", lm.dhparamFilename, err) } return lm.dhparamFilename, nil @@ -423,7 +423,7 @@ func (lm *LocalManager) CreateOpenTracingTracerConfig(content string) error { glog.V(3).Infof("Writing OpenTracing tracer config file to %v", jsonFileForOpenTracingTracer) err := createFileAndWrite(jsonFileForOpenTracingTracer, []byte(content)) if err != nil { - return fmt.Errorf("Failed to write config file: %w", err) + return fmt.Errorf("failed to write config file: %w", err) } return nil diff --git a/internal/nginx/utils.go b/internal/nginx/utils.go index 6cc70c742a..682db8ca15 100644 --- a/internal/nginx/utils.go +++ b/internal/nginx/utils.go @@ -22,12 +22,12 @@ func shellOut(cmd string) (err error) { err = command.Start() if err != nil { - return fmt.Errorf("Failed to execute %v, err: %w", cmd, err) + return fmt.Errorf("failed to execute %v, err: %w", cmd, err) } err = command.Wait() if err != nil { - return fmt.Errorf("Command %v stdout: %q\nstderr: %q\nfinished with error: %w", cmd, + return fmt.Errorf("command %v stdout: %q\nstderr: %q\nfinished with error: %w", cmd, stdout.String(), stderr.String(), err) } return nil @@ -36,14 +36,14 @@ func shellOut(cmd string) (err error) { func createFileAndWrite(name string, b []byte) error { w, err := os.Create(name) if err != nil { - return fmt.Errorf("Failed to open %v: %w", name, err) + return fmt.Errorf("failed to open %v: %w", name, err) } defer w.Close() _, err = w.Write(b) if err != nil { - return fmt.Errorf("Failed to write to %v: %w", name, err) + return fmt.Errorf("failed to write to %v: %w", name, err) } return nil