From 754eb7bd460f6720423ce3a7d4f06ddb8a838313 Mon Sep 17 00:00:00 2001 From: Lior Okman Date: Sat, 11 May 2024 19:11:34 +0300 Subject: [PATCH] Make the linter happy Signed-off-by: Lior Okman --- internal/gatewayapi/extensionserverpolicy.go | 27 +++++++++---------- .../gatewayapi/extensionserverpolicy_test.go | 4 +-- internal/gatewayapi/runner/runner.go | 5 +++- internal/gatewayapi/translator_test.go | 5 ++-- internal/provider/kubernetes/controller.go | 4 +-- 5 files changed, 21 insertions(+), 24 deletions(-) diff --git a/internal/gatewayapi/extensionserverpolicy.go b/internal/gatewayapi/extensionserverpolicy.go index 118d8aa234ba..6e855861cbee 100644 --- a/internal/gatewayapi/extensionserverpolicy.go +++ b/internal/gatewayapi/extensionserverpolicy.go @@ -26,7 +26,6 @@ func (t *Translator) ProcessExtensionServerPolicies(policies []unstructured.Unst gateways []*GatewayContext, xdsIR XdsIRMap, ) []unstructured.Unstructured { - var res []unstructured.Unstructured // Sort based on timestamp @@ -87,18 +86,13 @@ func (t *Translator) ProcessExtensionServerPolicies(policies []unstructured.Unst continue } - // Set conditions for translation error if it got any - if err := t.translateExtServerPolicyForGateway(policy, gateway, currTarget, xdsIR); err != nil { - status.SetTranslationErrorForPolicyAncestors(&policyStatus, - ancestorRefs, - t.GatewayControllerName, - policy.GetGeneration(), - status.Error2ConditionMsg(err), - ) + // Set conditions for translation if it got any + if t.translateExtServerPolicyForGateway(policy, gateway, currTarget, xdsIR) { + // Set Accepted condition if it is unset + // Only add a status condition if the policy was added into the IR + status.SetAcceptedForPolicyAncestors(&policyStatus, ancestorRefs, t.GatewayControllerName) } - // Set Accepted condition if it is unset - status.SetAcceptedForPolicyAncestors(&policyStatus, ancestorRefs, t.GatewayControllerName) } policy.Object["status"] = policyStatusToUnstructured(policyStatus) } @@ -157,8 +151,9 @@ func extractSingleTargetRef(data any) (gwv1a2.LocalPolicyTargetReferenceWithSect func policyStatusToUnstructured(policyStatus gwv1a2.PolicyStatus) map[string]any { ret := map[string]any{} + // No need to check the marshal/unmarshal error here d, _ := json.Marshal(policyStatus) - json.Unmarshal(d, &ret) + _ = json.Unmarshal(d, &ret) return ret } @@ -195,10 +190,11 @@ func (t *Translator) translateExtServerPolicyForGateway( policy *unstructured.Unstructured, gateway *GatewayContext, target gwv1a2.LocalPolicyTargetReferenceWithSectionName, - xdsIR XdsIRMap) error { - + xdsIR XdsIRMap, +) bool { irKey := t.getIRKey(gateway.Gateway) gwIR := xdsIR[irKey] + found := false for _, currListener := range gwIR.HTTP { listenerName := currListener.Name[strings.LastIndex(currListener.Name, "/")+1:] if target.SectionName != nil && string(*target.SectionName) != listenerName { @@ -207,6 +203,7 @@ func (t *Translator) translateExtServerPolicyForGateway( currListener.ExtensionRefs = append(currListener.ExtensionRefs, &ir.UnstructuredRef{ Object: policy, }) + found = true } - return nil + return found } diff --git a/internal/gatewayapi/extensionserverpolicy_test.go b/internal/gatewayapi/extensionserverpolicy_test.go index 78a9e8580168..907e244c971b 100644 --- a/internal/gatewayapi/extensionserverpolicy_test.go +++ b/internal/gatewayapi/extensionserverpolicy_test.go @@ -15,7 +15,6 @@ import ( ) func TestExtractTargetRefs(t *testing.T) { - tests := []struct { desc string specInput map[string]any @@ -166,10 +165,9 @@ func TestExtractTargetRefs(t *testing.T) { if currTest.expectedError != "" { require.EqualError(t, err, currTest.expectedError) } else { - require.Nil(t, err) + require.NoError(t, err) require.Equal(t, currTest.output, targets) } }) } - } diff --git a/internal/gatewayapi/runner/runner.go b/internal/gatewayapi/runner/runner.go index 4a1054559dbd..219ceb597198 100644 --- a/internal/gatewayapi/runner/runner.go +++ b/internal/gatewayapi/runner/runner.go @@ -229,8 +229,11 @@ func (r *Runner) subscribeAndTranslate(ctx context.Context) { func unstructuredToPolicyStatus(policyStatus map[string]any) gwv1a2.PolicyStatus { var ret gwv1a2.PolicyStatus + // No need to check the json marshal/unmarshal error, the policyStatus was + // created via a typed object so the marshalling/unmarshalling will always + // work d, _ := json.Marshal(policyStatus) - json.Unmarshal(d, &ret) + _ = json.Unmarshal(d, &ret) return ret } diff --git a/internal/gatewayapi/translator_test.go b/internal/gatewayapi/translator_test.go index 28089f336749..3590551634fb 100644 --- a/internal/gatewayapi/translator_test.go +++ b/internal/gatewayapi/translator_test.go @@ -29,7 +29,6 @@ import ( "sigs.k8s.io/gateway-api/apis/v1beta1" "sigs.k8s.io/yaml" - "github.com/envoyproxy/gateway/api/v1alpha1" egv1a1 "github.com/envoyproxy/gateway/api/v1alpha1" "github.com/envoyproxy/gateway/internal/logging" "github.com/envoyproxy/gateway/internal/utils/field" @@ -79,7 +78,7 @@ func TestTranslate(t *testing.T) { EnvoyPatchPolicyEnabled: envoyPatchPolicyEnabled, Namespace: "envoy-gateway-system", MergeGateways: IsMergeGatewaysEnabled(resources), - Logger: logging.NewLogger(&v1alpha1.EnvoyGatewayLogging{}), + Logger: logging.NewLogger(&egv1a1.EnvoyGatewayLogging{}), } // Add common test fixtures @@ -276,7 +275,7 @@ func TestTranslateWithExtensionKinds(t *testing.T) { {Group: "bar.example.io", Kind: "Bar"}, }, MergeGateways: IsMergeGatewaysEnabled(resources), - Logger: logging.NewLogger(&v1alpha1.EnvoyGatewayLogging{}), + Logger: logging.NewLogger(&egv1a1.EnvoyGatewayLogging{}), } // Add common test fixtures diff --git a/internal/provider/kubernetes/controller.go b/internal/provider/kubernetes/controller.go index 17b1be3cf239..8c040a0c98e3 100644 --- a/internal/provider/kubernetes/controller.go +++ b/internal/provider/kubernetes/controller.go @@ -213,7 +213,7 @@ func (r *gatewayAPIReconciler) Reconcile(ctx context.Context, _ reconcile.Reques return reconcile.Result{}, err } - if err = r.processExtensionServerPolicies(ctx, gwcResource, resourceMappings); err != nil { + if err = r.processExtensionServerPolicies(ctx, gwcResource); err != nil { return reconcile.Result{}, err } @@ -1591,7 +1591,7 @@ func (r *gatewayAPIReconciler) processEnvoyExtensionPolicies( // processExtensionServerPolicies adds directly attached policies intended for the extension server func (r *gatewayAPIReconciler) processExtensionServerPolicies( - ctx context.Context, resourceTree *gatewayapi.Resources, resourceMap *resourceMappings, + ctx context.Context, resourceTree *gatewayapi.Resources, ) error { for _, gvk := range r.extServerPolicies { polList := unstructured.UnstructuredList{}