From 12b33797ae6bdb4083c032da1f426a21c26d5664 Mon Sep 17 00:00:00 2001 From: Gaurav Ghildiyal Date: Thu, 18 Apr 2024 00:30:10 -0700 Subject: [PATCH] Fix golint errors --- gwctl/cmd/describe.go | 2 +- gwctl/pkg/common/clients.go | 29 +++++++++++---- gwctl/pkg/policymanager/manager.go | 5 ++- gwctl/pkg/policymanager/merger.go | 1 - gwctl/pkg/printer/gatewayclasses.go | 25 ++++++++----- gwctl/pkg/printer/gateways.go | 14 +++++-- gwctl/pkg/printer/httproutes.go | 14 +++++-- gwctl/pkg/printer/namespace.go | 14 +++++-- gwctl/pkg/printer/policies.go | 26 ++++++++++--- gwctl/pkg/resourcediscovery/discoverer.go | 33 ++++++++++------- gwctl/pkg/resourcediscovery/nodes.go | 26 ++++++------- gwctl/pkg/resourcediscovery/resourcemodel.go | 39 +++++++++++--------- 12 files changed, 151 insertions(+), 77 deletions(-) diff --git a/gwctl/cmd/describe.go b/gwctl/cmd/describe.go index 70d73015b0..2e2b59e51a 100644 --- a/gwctl/cmd/describe.go +++ b/gwctl/cmd/describe.go @@ -95,7 +95,7 @@ func runDescribe(cmd *cobra.Command, args []string, params *utils.CmdParams) { } } policiesPrinter.PrintPoliciesDescribeView(policyList) - + case "policycrd", "policycrds": var policyCrdList []policymanager.PolicyCRD if len(args) == 1 { diff --git a/gwctl/pkg/common/clients.go b/gwctl/pkg/common/clients.go index 186e57e901..d29f869f7c 100644 --- a/gwctl/pkg/common/clients.go +++ b/gwctl/pkg/common/clients.go @@ -30,6 +30,7 @@ import ( "k8s.io/client-go/tools/clientcmd" "sigs.k8s.io/controller-runtime/pkg/client" fakeclient "sigs.k8s.io/controller-runtime/pkg/client/fake" + gatewayv1 "sigs.k8s.io/gateway-api/apis/v1" gatewayv1alpha2 "sigs.k8s.io/gateway-api/apis/v1alpha2" gatewayv1beta1 "sigs.k8s.io/gateway-api/apis/v1beta1" @@ -51,9 +52,15 @@ func NewK8sClients(kubeconfig string) (*K8sClients, error) { if err != nil { return nil, fmt.Errorf("failed to initialize Kubernetes client: %v", err) } - gatewayv1alpha2.AddToScheme(client.Scheme()) - gatewayv1beta1.AddToScheme(client.Scheme()) - gatewayv1.AddToScheme(client.Scheme()) + if err := gatewayv1alpha2.AddToScheme(client.Scheme()); err != nil { + return nil, err + } + if err := gatewayv1beta1.AddToScheme(client.Scheme()); err != nil { + return nil, err + } + if err := gatewayv1.AddToScheme(client.Scheme()); err != nil { + return nil, err + } dc := dynamic.NewForConfigOrDie(restConfig) @@ -66,10 +73,18 @@ func NewK8sClients(kubeconfig string) (*K8sClients, error) { func MustClientsForTest(t *testing.T, initRuntimeObjects ...runtime.Object) *K8sClients { scheme := scheme.Scheme - gatewayv1alpha2.AddToScheme(scheme) - gatewayv1beta1.AddToScheme(scheme) - gatewayv1.AddToScheme(scheme) - apiextensionsv1.AddToScheme(scheme) + if err := gatewayv1alpha2.AddToScheme(scheme); err != nil { + t.Fatal(err) + } + if err := gatewayv1beta1.AddToScheme(scheme); err != nil { + t.Fatal(err) + } + if err := gatewayv1.AddToScheme(scheme); err != nil { + t.Fatal(err) + } + if err := apiextensionsv1.AddToScheme(scheme); err != nil { + t.Fatal(err) + } fakeClient := fakeclient.NewClientBuilder().WithScheme(scheme).WithRuntimeObjects(initRuntimeObjects...).Build() fakeDC := fakedynamicclient.NewSimpleDynamicClient(scheme, initRuntimeObjects...) diff --git a/gwctl/pkg/policymanager/manager.go b/gwctl/pkg/policymanager/manager.go index 3eac669e5f..ee0602e0c5 100644 --- a/gwctl/pkg/policymanager/manager.go +++ b/gwctl/pkg/policymanager/manager.go @@ -28,6 +28,7 @@ import ( "k8s.io/apimachinery/pkg/runtime" "k8s.io/apimachinery/pkg/runtime/schema" "k8s.io/client-go/dynamic" + gatewayv1alpha2 "sigs.k8s.io/gateway-api/apis/v1alpha2" ) @@ -67,7 +68,9 @@ func (p *PolicyManager) Init(ctx context.Context) error { return err } for _, unstrucutredPolicy := range allPolicies { - p.AddPolicy(unstrucutredPolicy) + if err := p.AddPolicy(unstrucutredPolicy); err != nil { + return err + } } return nil diff --git a/gwctl/pkg/policymanager/merger.go b/gwctl/pkg/policymanager/merger.go index 9b54e7ea96..0c92ed192d 100644 --- a/gwctl/pkg/policymanager/merger.go +++ b/gwctl/pkg/policymanager/merger.go @@ -192,7 +192,6 @@ func orderPolicyByPrecedence(a, b Policy) (Policy, Policy) { if higherNN > lowerNN { higherPolicy, lowerPolicy = lowerPolicy, higherPolicy } - } else if higherPolicy.u.GetCreationTimestamp().Time.After(lowerPolicy.u.GetCreationTimestamp().Time) { // Policies have difference creation time, so this will decide the precedence higherPolicy, lowerPolicy = lowerPolicy, higherPolicy diff --git a/gwctl/pkg/printer/gatewayclasses.go b/gwctl/pkg/printer/gatewayclasses.go index 2cce9a3240..8186ea283e 100644 --- a/gwctl/pkg/printer/gatewayclasses.go +++ b/gwctl/pkg/printer/gatewayclasses.go @@ -19,21 +19,20 @@ package printer import ( "fmt" "io" - metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" - "k8s.io/utils/ptr" "os" - v1 "sigs.k8s.io/gateway-api/apis/v1" "sort" "strings" "text/tabwriter" - "sigs.k8s.io/yaml" - + gatewayv1 "sigs.k8s.io/gateway-api/apis/v1" "sigs.k8s.io/gateway-api/gwctl/pkg/policymanager" "sigs.k8s.io/gateway-api/gwctl/pkg/resourcediscovery" + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" "k8s.io/apimachinery/pkg/util/duration" "k8s.io/utils/clock" + "k8s.io/utils/ptr" + "sigs.k8s.io/yaml" ) type GatewayClassesPrinter struct { @@ -54,14 +53,18 @@ type gatewayClassDescribeView struct { // GatewayClass description Description *string `json:",omitempty"` - Status *v1.GatewayClassStatus `json:",omitempty"` - DirectlyAttachedPolicies []policymanager.ObjRef `json:",omitempty"` + Status *gatewayv1.GatewayClassStatus `json:",omitempty"` + DirectlyAttachedPolicies []policymanager.ObjRef `json:",omitempty"` } func (gcp *GatewayClassesPrinter) Print(model *resourcediscovery.ResourceModel) { tw := tabwriter.NewWriter(gcp.Out, 0, 0, 2, ' ', 0) row := []string{"NAME", "CONTROLLER", "ACCEPTED", "AGE"} - tw.Write([]byte(strings.Join(row, "\t") + "\n")) + _, err := tw.Write([]byte(strings.Join(row, "\t") + "\n")) + if err != nil { + fmt.Fprint(os.Stderr, err) + os.Exit(1) + } gatewayClassNodes := make([]*resourcediscovery.GatewayClassNode, 0, len(model.GatewayClasses)) for _, gatewayClassNode := range model.GatewayClasses { @@ -91,7 +94,11 @@ func (gcp *GatewayClassesPrinter) Print(model *resourcediscovery.ResourceModel) accepted, age, } - tw.Write([]byte(strings.Join(row, "\t") + "\n")) + _, err := tw.Write([]byte(strings.Join(row, "\t") + "\n")) + if err != nil { + fmt.Fprint(os.Stderr, err) + os.Exit(1) + } } tw.Flush() } diff --git a/gwctl/pkg/printer/gateways.go b/gwctl/pkg/printer/gateways.go index 41bf66a8d6..47c6d7a4d7 100644 --- a/gwctl/pkg/printer/gateways.go +++ b/gwctl/pkg/printer/gateways.go @@ -26,10 +26,10 @@ import ( "sigs.k8s.io/gateway-api/gwctl/pkg/policymanager" "sigs.k8s.io/gateway-api/gwctl/pkg/resourcediscovery" - "sigs.k8s.io/yaml" "k8s.io/apimachinery/pkg/util/duration" "k8s.io/utils/clock" + "sigs.k8s.io/yaml" ) type GatewaysPrinter struct { @@ -50,7 +50,11 @@ type gatewayDescribeView struct { func (gp *GatewaysPrinter) Print(resourceModel *resourcediscovery.ResourceModel) { tw := tabwriter.NewWriter(gp.Out, 0, 0, 2, ' ', 0) row := []string{"NAME", "CLASS", "ADDRESSES", "PORTS", "PROGRAMMED", "AGE"} - tw.Write([]byte(strings.Join(row, "\t") + "\n")) + _, err := tw.Write([]byte(strings.Join(row, "\t") + "\n")) + if err != nil { + fmt.Fprint(os.Stderr, err) + os.Exit(1) + } gatewayNodes := make([]*resourcediscovery.GatewayNode, 0, len(resourceModel.Gateways)) for _, gatewayNode := range resourceModel.Gateways { @@ -98,7 +102,11 @@ func (gp *GatewaysPrinter) Print(resourceModel *resourcediscovery.ResourceModel) programmedStatus, age, } - tw.Write([]byte(strings.Join(row, "\t") + "\n")) + _, err := tw.Write([]byte(strings.Join(row, "\t") + "\n")) + if err != nil { + fmt.Fprint(os.Stderr, err) + os.Exit(1) + } } tw.Flush() } diff --git a/gwctl/pkg/printer/httproutes.go b/gwctl/pkg/printer/httproutes.go index 75da155914..4ef3dcd853 100644 --- a/gwctl/pkg/printer/httproutes.go +++ b/gwctl/pkg/printer/httproutes.go @@ -27,10 +27,10 @@ import ( gatewayv1 "sigs.k8s.io/gateway-api/apis/v1" "sigs.k8s.io/gateway-api/gwctl/pkg/policymanager" "sigs.k8s.io/gateway-api/gwctl/pkg/resourcediscovery" - "sigs.k8s.io/yaml" "k8s.io/apimachinery/pkg/util/duration" "k8s.io/utils/clock" + "sigs.k8s.io/yaml" ) type HTTPRoutesPrinter struct { @@ -41,7 +41,11 @@ type HTTPRoutesPrinter struct { func (hp *HTTPRoutesPrinter) Print(resourceModel *resourcediscovery.ResourceModel) { tw := tabwriter.NewWriter(hp.Out, 0, 0, 2, ' ', 0) row := []string{"NAMESPACE", "NAME", "HOSTNAMES", "PARENT REFS", "AGE"} - tw.Write([]byte(strings.Join(row, "\t") + "\n")) + _, err := tw.Write([]byte(strings.Join(row, "\t") + "\n")) + if err != nil { + fmt.Fprint(os.Stderr, err) + os.Exit(1) + } httpRouteNodes := make([]*resourcediscovery.HTTPRouteNode, 0, len(resourceModel.HTTPRoutes)) for _, httpRouteNode := range resourceModel.HTTPRoutes { @@ -80,7 +84,11 @@ func (hp *HTTPRoutesPrinter) Print(resourceModel *resourcediscovery.ResourceMode parentRefsCount, age, } - tw.Write([]byte(strings.Join(row, "\t") + "\n")) + _, err := tw.Write([]byte(strings.Join(row, "\t") + "\n")) + if err != nil { + fmt.Fprint(os.Stderr, err) + os.Exit(1) + } } tw.Flush() } diff --git a/gwctl/pkg/printer/namespace.go b/gwctl/pkg/printer/namespace.go index 6f677c388e..7e9729e458 100644 --- a/gwctl/pkg/printer/namespace.go +++ b/gwctl/pkg/printer/namespace.go @@ -26,10 +26,10 @@ import ( "k8s.io/apimachinery/pkg/util/duration" "k8s.io/utils/clock" + "sigs.k8s.io/yaml" "sigs.k8s.io/gateway-api/gwctl/pkg/policymanager" "sigs.k8s.io/gateway-api/gwctl/pkg/resourcediscovery" - "sigs.k8s.io/yaml" ) type NamespacesPrinter struct { @@ -48,7 +48,11 @@ type namespaceDescribeView struct { func (nsp *NamespacesPrinter) Print(resourceModel *resourcediscovery.ResourceModel) { tw := tabwriter.NewWriter(nsp.Out, 0, 0, 2, ' ', 0) row := []string{"NAME", "STATUS", "AGE"} - tw.Write([]byte(strings.Join(row, "\t") + "\n")) + _, err := tw.Write([]byte(strings.Join(row, "\t") + "\n")) + if err != nil { + fmt.Fprint(os.Stderr, err) + os.Exit(1) + } namespaceNodes := make([]*resourcediscovery.NamespaceNode, 0, len(resourceModel.Namespaces)) for _, namespaceNode := range resourceModel.Namespaces { @@ -66,7 +70,11 @@ func (nsp *NamespacesPrinter) Print(resourceModel *resourcediscovery.ResourceMod string(namespaceNode.Namespace.Status.Phase), age, } - tw.Write([]byte(strings.Join(row, "\t") + "\n")) + _, err := tw.Write([]byte(strings.Join(row, "\t") + "\n")) + if err != nil { + fmt.Fprint(os.Stderr, err) + os.Exit(1) + } } tw.Flush() } diff --git a/gwctl/pkg/printer/policies.go b/gwctl/pkg/printer/policies.go index 7c23b09777..e03e3272ce 100644 --- a/gwctl/pkg/printer/policies.go +++ b/gwctl/pkg/printer/policies.go @@ -25,12 +25,12 @@ import ( "text/tabwriter" "sigs.k8s.io/gateway-api/gwctl/pkg/policymanager" - "sigs.k8s.io/yaml" apiextensionsv1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" "k8s.io/apimachinery/pkg/util/duration" "k8s.io/utils/clock" + "sigs.k8s.io/yaml" ) type PoliciesPrinter struct { @@ -47,7 +47,11 @@ func (pp *PoliciesPrinter) PrintPoliciesGetView(policies []policymanager.Policy) tw := tabwriter.NewWriter(pp.Out, 0, 0, 2, ' ', 0) row := []string{"NAME", "KIND", "TARGET NAME", "TARGET KIND", "POLICY TYPE", "AGE"} - tw.Write([]byte(strings.Join(row, "\t") + "\n")) + _, err := tw.Write([]byte(strings.Join(row, "\t") + "\n")) + if err != nil { + fmt.Fprint(os.Stderr, err) + os.Exit(1) + } for _, policy := range policies { policyType := "Direct" @@ -67,7 +71,11 @@ func (pp *PoliciesPrinter) PrintPoliciesGetView(policies []policymanager.Policy) policyType, age, } - tw.Write([]byte(strings.Join(row, "\t") + "\n")) + _, err := tw.Write([]byte(strings.Join(row, "\t") + "\n")) + if err != nil { + fmt.Fprint(os.Stderr, err) + os.Exit(1) + } } tw.Flush() } @@ -81,7 +89,11 @@ func (pp *PoliciesPrinter) PrintPolicyCRDsGetView(policyCRDs []policymanager.Pol tw := tabwriter.NewWriter(pp.Out, 0, 0, 2, ' ', 0) row := []string{"NAME", "POLICY TYPE", "SCOPE", "AGE"} - tw.Write([]byte(strings.Join(row, "\t") + "\n")) + _, err := tw.Write([]byte(strings.Join(row, "\t") + "\n")) + if err != nil { + fmt.Fprint(os.Stderr, err) + os.Exit(1) + } for _, policyCRD := range policyCRDs { policyType := "Direct" @@ -97,7 +109,11 @@ func (pp *PoliciesPrinter) PrintPolicyCRDsGetView(policyCRDs []policymanager.Pol string(policyCRD.CRD().Spec.Scope), age, } - tw.Write([]byte(strings.Join(row, "\t") + "\n")) + _, err := tw.Write([]byte(strings.Join(row, "\t") + "\n")) + if err != nil { + fmt.Fprint(os.Stderr, err) + os.Exit(1) + } } tw.Flush() } diff --git a/gwctl/pkg/resourcediscovery/discoverer.go b/gwctl/pkg/resourcediscovery/discoverer.go index 55d9433b93..c7e8ee0a4e 100644 --- a/gwctl/pkg/resourcediscovery/discoverer.go +++ b/gwctl/pkg/resourcediscovery/discoverer.go @@ -20,7 +20,7 @@ import ( "context" "fmt" "os" - "sigs.k8s.io/controller-runtime/pkg/client" + gatewayv1 "sigs.k8s.io/gateway-api/apis/v1" "sigs.k8s.io/gateway-api/gwctl/pkg/common" "sigs.k8s.io/gateway-api/gwctl/pkg/policymanager" @@ -33,6 +33,7 @@ import ( "k8s.io/apimachinery/pkg/runtime/schema" apimachinerytypes "k8s.io/apimachinery/pkg/types" "k8s.io/klog/v2" + "sigs.k8s.io/controller-runtime/pkg/client" ) // Filter struct defines parameters for filtering resources @@ -66,7 +67,7 @@ func (d Discoverer) DiscoverResourcesForGatewayClass(filter Filter) (*ResourceMo } resourceModel.addGatewayClasses(gatewayClasses...) - d.discoverPolicies(ctx, resourceModel) + d.discoverPolicies(resourceModel) return resourceModel, nil } @@ -84,9 +85,11 @@ func (d Discoverer) DiscoverResourcesForGateway(filter Filter) (*ResourceModel, d.discoverGatewayClassesFromGateways(ctx, resourceModel) d.discoverNamespaces(ctx, resourceModel) - d.discoverPolicies(ctx, resourceModel) + d.discoverPolicies(resourceModel) - resourceModel.calculateEffectivePolicies() + if err := resourceModel.calculateEffectivePolicies(); err != nil { + return resourceModel, err + } return resourceModel, nil } @@ -105,9 +108,11 @@ func (d Discoverer) DiscoverResourcesForHTTPRoute(filter Filter) (*ResourceModel d.discoverGatewaysFromHTTPRoutes(ctx, resourceModel) d.discoverGatewayClassesFromGateways(ctx, resourceModel) d.discoverNamespaces(ctx, resourceModel) - d.discoverPolicies(ctx, resourceModel) + d.discoverPolicies(resourceModel) - resourceModel.calculateEffectivePolicies() + if err := resourceModel.calculateEffectivePolicies(); err != nil { + return resourceModel, err + } return resourceModel, nil } @@ -127,9 +132,11 @@ func (d Discoverer) DiscoverResourcesForBackend(filter Filter) (*ResourceModel, d.discoverGatewaysFromHTTPRoutes(ctx, resourceModel) d.discoverGatewayClassesFromGateways(ctx, resourceModel) d.discoverNamespaces(ctx, resourceModel) - d.discoverPolicies(ctx, resourceModel) + d.discoverPolicies(resourceModel) - resourceModel.calculateEffectivePolicies() + if err := resourceModel.calculateEffectivePolicies(); err != nil { + return resourceModel, err + } return resourceModel, nil } @@ -146,7 +153,7 @@ func (d Discoverer) DiscoverResourcesForNamespace(filter Filter) (*ResourceModel resourceModel.addNamespace(namespaces...) - d.discoverPolicies(ctx, resourceModel) + d.discoverPolicies(resourceModel) return resourceModel, nil } @@ -166,8 +173,8 @@ func (d Discoverer) discoverGatewayClassesFromGateways(ctx context.Context, reso } for gatewayID, gatewayNode := range resourceModel.Gateways { - gatewayClassID := GatewayClassID(relations.FindGatewayClassNameForGateway(*gatewayNode.Gateway)) - gatewayClass, ok := gatewayClassesByID[gatewayClassID] + gwcID := GatewayClassID(relations.FindGatewayClassNameForGateway(*gatewayNode.Gateway)) + gatewayClass, ok := gatewayClassesByID[gwcID] if !ok { klog.V(1).ErrorS(nil, "GatewayClass referenced in Gateway does not exist", "gateway", gatewayNode.Gateway.GetNamespace()+"/"+gatewayNode.Gateway.GetName(), @@ -176,7 +183,7 @@ func (d Discoverer) discoverGatewayClassesFromGateways(ctx context.Context, reso } resourceModel.addGatewayClasses(gatewayClass) - resourceModel.connectGatewayWithGatewayClass(gatewayID, gatewayClassID) + resourceModel.connectGatewayWithGatewayClass(gatewayID, gwcID) } } @@ -271,7 +278,7 @@ func (d Discoverer) discoverNamespaces(ctx context.Context, resourceModel *Resou } // discoverPolicies adds Policies for resources that exist in the resourceModel. -func (d Discoverer) discoverPolicies(ctx context.Context, resourceModel *ResourceModel) { +func (d Discoverer) discoverPolicies(resourceModel *ResourceModel) { resourceModel.addPolicyIfTargetExists(d.PolicyManager.GetPolicies()...) } diff --git a/gwctl/pkg/resourcediscovery/nodes.go b/gwctl/pkg/resourcediscovery/nodes.go index 474c51c9e3..35cc97705a 100644 --- a/gwctl/pkg/resourcediscovery/nodes.go +++ b/gwctl/pkg/resourcediscovery/nodes.go @@ -47,12 +47,12 @@ type ( ) // GatewayClassID returns an ID for a GatewayClass. -func GatewayClassID(gatewayClassName string) gatewayClassID { +func GatewayClassID(gatewayClassName string) gatewayClassID { //nolint:revive return gatewayClassID(resourceID{Name: gatewayClassName}) } // NamespaceID returns an ID for a Namespace. -func NamespaceID(namespaceName string) namespaceID { +func NamespaceID(namespaceName string) namespaceID { //nolint:revive if namespaceName == "" { namespaceName = metav1.NamespaceDefault } @@ -60,7 +60,7 @@ func NamespaceID(namespaceName string) namespaceID { } // GatewayID returns an ID for a Gateway. -func GatewayID(namespace, name string) gatewayID { +func GatewayID(namespace, name string) gatewayID { //nolint:revive if namespace == "" { namespace = metav1.NamespaceDefault } @@ -68,7 +68,7 @@ func GatewayID(namespace, name string) gatewayID { } // HTTPRouteID returns an ID for a HTTPRoute. -func HTTPRouteID(namespace, name string) httpRouteID { +func HTTPRouteID(namespace, name string) httpRouteID { //nolint:revive if namespace == "" { namespace = metav1.NamespaceDefault } @@ -76,7 +76,7 @@ func HTTPRouteID(namespace, name string) httpRouteID { } // BackendID returns an ID for a Backend. -func BackendID(group, kind, namespace, name string) backendID { +func BackendID(group, kind, namespace, name string) backendID { //nolint:revive return backendID(resourceID{ Group: strings.ToLower(group), Kind: strings.ToLower(kind), @@ -87,12 +87,12 @@ func BackendID(group, kind, namespace, name string) backendID { // BackendIDForService returns an ID for a Backend which contains an underlying // Service type. -func BackendIDForService(namespace, name string) backendID { +func BackendIDForService(namespace, name string) backendID { //nolint:revive return BackendID("", "service", namespace, name) } // PolicyID returns an ID for a Policy. -func PolicyID(group, kind, namespace, name string) policyID { +func PolicyID(group, kind, namespace, name string) policyID { //nolint:revive return policyID(resourceID{ Group: strings.ToLower(group), Kind: strings.ToLower(kind), @@ -127,7 +127,7 @@ func NewGatewayClassNode(gatewayClass *gatewayv1.GatewayClass) *GatewayClassNode } } -func (g *GatewayClassNode) ID() gatewayClassID { +func (g *GatewayClassNode) ID() gatewayClassID { //nolint:revive if g.GatewayClass == nil { klog.V(0).ErrorS(nil, "returning empty ID since GatewayClass is nil") return gatewayClassID(resourceID{}) @@ -162,7 +162,7 @@ func NewGatewayNode(gateway *gatewayv1.Gateway) *GatewayNode { } } -func (g *GatewayNode) ID() gatewayID { +func (g *GatewayNode) ID() gatewayID { //nolint:revive if g.Gateway == nil { klog.V(0).ErrorS(nil, "returning empty ID since Gateway is nil") return gatewayID(resourceID{}) @@ -200,7 +200,7 @@ func NewHTTPRouteNode(httpRoute *gatewayv1.HTTPRoute) *HTTPRouteNode { } } -func (h *HTTPRouteNode) ID() httpRouteID { +func (h *HTTPRouteNode) ID() httpRouteID { //nolint:revive if h.HTTPRoute == nil { klog.V(0).ErrorS(nil, "returning empty ID since HTTPRoute is nil") return httpRouteID(resourceID{}) @@ -236,7 +236,7 @@ func NewBackendNode(backend *unstructured.Unstructured) *BackendNode { } } -func (b *BackendNode) ID() backendID { +func (b *BackendNode) ID() backendID { //nolint:revive if b.Backend == nil { klog.V(0).ErrorS(nil, "returning empty ID since Backend is empty") return backendID(resourceID{}) @@ -277,7 +277,7 @@ func NewNamespaceNode(namespace corev1.Namespace) *NamespaceNode { } } -func (n *NamespaceNode) ID() namespaceID { +func (n *NamespaceNode) ID() namespaceID { //nolint:revive if n.Namespace.Name == "" { klog.V(0).ErrorS(nil, "returning empty ID since Namespace is empty") return namespaceID(resourceID{}) @@ -316,7 +316,7 @@ func NewPolicyNode(policy *policymanager.Policy) *PolicyNode { } } -func (p *PolicyNode) ID() policyID { +func (p *PolicyNode) ID() policyID { //nolint:revive if p.Policy == nil { klog.V(0).ErrorS(nil, "returning empty ID since Policy is empty") return policyID(resourceID{}) diff --git a/gwctl/pkg/resourcediscovery/resourcemodel.go b/gwctl/pkg/resourcediscovery/resourcemodel.go index 6f4dad00d1..99be5c632f 100644 --- a/gwctl/pkg/resourcediscovery/resourcemodel.go +++ b/gwctl/pkg/resourcediscovery/resourcemodel.go @@ -125,13 +125,15 @@ func (rm *ResourceModel) addPolicyIfTargetExists(policies ...policymanager.Polic for _, policy := range policies { policy := policy policyNode := NewPolicyNode(&policy) - if policy.TargetRef().Group == gatewayv1.GroupName { + + switch { + case policy.TargetRef().Group == gatewayv1.GroupName: switch policy.TargetRef().Kind { case "GatewayClass": - gatewayClassID := GatewayClassID(policy.TargetRef().Name) - gatewayClassNode, ok := rm.GatewayClasses[gatewayClassID] + gwcID := GatewayClassID(policy.TargetRef().Name) + gatewayClassNode, ok := rm.GatewayClasses[gwcID] if !ok { - klog.V(1).ErrorS(nil, "Skipping policy since targetRef GatewayClass does not exist in ResourceModel", "policy", policy.Name(), "gatewayClassID", gatewayClassID) + klog.V(1).ErrorS(nil, "Skipping policy since targetRef GatewayClass does not exist in ResourceModel", "policy", policy.Name(), "gatewayClassID", gwcID) continue } rm.Policies[policyNode.ID()] = policyNode @@ -139,10 +141,10 @@ func (rm *ResourceModel) addPolicyIfTargetExists(policies ...policymanager.Polic gatewayClassNode.Policies[policyNode.ID()] = policyNode case "Gateway": - gatewayID := GatewayID(policy.TargetRef().Namespace, policy.TargetRef().Name) - gatewayNode, ok := rm.Gateways[gatewayID] + gwID := GatewayID(policy.TargetRef().Namespace, policy.TargetRef().Name) + gatewayNode, ok := rm.Gateways[gwID] if !ok { - klog.V(1).ErrorS(nil, "Skipping policy since targetRef Gateway does not exist in ResourceModel", "policy", policy.Name(), "gatewayID", gatewayID) + klog.V(1).ErrorS(nil, "Skipping policy since targetRef Gateway does not exist in ResourceModel", "policy", policy.Name(), "gatewayID", gwID) continue } rm.Policies[policyNode.ID()] = policyNode @@ -150,32 +152,33 @@ func (rm *ResourceModel) addPolicyIfTargetExists(policies ...policymanager.Polic gatewayNode.Policies[policyNode.ID()] = policyNode case "HTTPRoute": - httpRouteID := HTTPRouteID(policy.TargetRef().Namespace, policy.TargetRef().Name) - httpRouteNode, ok := rm.HTTPRoutes[httpRouteID] + hrID := HTTPRouteID(policy.TargetRef().Namespace, policy.TargetRef().Name) + httpRouteNode, ok := rm.HTTPRoutes[hrID] if !ok { - klog.V(1).ErrorS(nil, "Skipping policy since targetRef HTTPRoute does not exist in ResourceModel", "policy", policy.Name(), "httpRouteID", httpRouteID) + klog.V(1).ErrorS(nil, "Skipping policy since targetRef HTTPRoute does not exist in ResourceModel", "policy", policy.Name(), "httpRouteID", hrID) continue } rm.Policies[policyNode.ID()] = policyNode policyNode.HTTPRoute = httpRouteNode httpRouteNode.Policies[policyNode.ID()] = policyNode } - } else if policy.TargetRef().Group == corev1.GroupName && policy.TargetRef().Kind == "Namespace" { - namespaceID := NamespaceID(policy.TargetRef().Name) - namespaceNode, ok := rm.Namespaces[namespaceID] + + case policy.TargetRef().Group == corev1.GroupName && policy.TargetRef().Kind == "Namespace": + nsID := NamespaceID(policy.TargetRef().Name) + namespaceNode, ok := rm.Namespaces[nsID] if !ok { - klog.V(1).ErrorS(nil, "Skipping policy since targetRef Namespace does not exist in ResourceModel", "policy", policy.Name(), "namespaceID", namespaceID) + klog.V(1).ErrorS(nil, "Skipping policy since targetRef Namespace does not exist in ResourceModel", "policy", policy.Name(), "namespaceID", nsID) continue } rm.Policies[policyNode.ID()] = policyNode policyNode.Namespace = namespaceNode namespaceNode.Policies[policyNode.ID()] = policyNode - } else { // Assume attached to backend and evaluate further. - backendID := BackendID(policy.TargetRef().Group, policy.TargetRef().Kind, policy.TargetRef().Namespace, policy.TargetRef().Name) - backendNode, ok := rm.Backends[backendID] + default: // Assume attached to backend and evaluate further. + bID := BackendID(policy.TargetRef().Group, policy.TargetRef().Kind, policy.TargetRef().Namespace, policy.TargetRef().Name) + backendNode, ok := rm.Backends[bID] if !ok { - klog.V(1).ErrorS(nil, "Skipping policy since targetRef Backend does not exist in ResourceModel", "policy", policy.Name(), "backendID", backendID) + klog.V(1).ErrorS(nil, "Skipping policy since targetRef Backend does not exist in ResourceModel", "policy", policy.Name(), "backendID", bID) continue } rm.Policies[policyNode.ID()] = policyNode