From a52b454fbf9efca21aa2439b4b3c0cf64ca37741 Mon Sep 17 00:00:00 2001 From: Devaansh-Kumar Date: Fri, 22 Mar 2024 16:01:43 +0530 Subject: [PATCH] Modified GetCRD to fetch PolicyCRD based on CRD name --- gwctl/pkg/policymanager/manager.go | 39 +++++--------------------- gwctl/pkg/printer/policies.go | 44 +++++++++++++++++++++++------- gwctl/pkg/printer/policies_test.go | 6 ---- 3 files changed, 41 insertions(+), 48 deletions(-) diff --git a/gwctl/pkg/policymanager/manager.go b/gwctl/pkg/policymanager/manager.go index b231106ccf..35e66918c9 100644 --- a/gwctl/pkg/policymanager/manager.go +++ b/gwctl/pkg/policymanager/manager.go @@ -92,8 +92,13 @@ func (p *PolicyManager) GetCRDs() []PolicyCRD { } func (p *PolicyManager) GetCRD(name string) (PolicyCRD, bool) { - policyCrd, ok := p.policyCRDs[PolicyCrdID(name)] - return policyCrd, ok + for _, policyCrd := range p.policyCRDs { + if name == policyCrd.CRD().Name { + return policyCrd, true + } + } + + return PolicyCRD{}, false } func (p *PolicyManager) GetPolicies() []Policy { @@ -201,36 +206,6 @@ func (p PolicyCRD) IsClusterScoped() bool { return p.crd.Spec.Scope == apiextensionsv1.ClusterScoped } -func (p PolicyCRD) Spec() map[string]interface{} { - spec := p.crd.Spec - - var result map[string]interface{} - marshalledSpec, _ := json.Marshal(spec) - json.Unmarshal(marshalledSpec, &result) - - return result -} - -func (p PolicyCRD) Metadata() map[string]interface{} { - om := p.crd.ObjectMeta - - var result map[string]interface{} - marshalledMetadata, _ := json.Marshal(om) - json.Unmarshal(marshalledMetadata, &result) - - return result -} - -func (p PolicyCRD) Status() map[string]interface{} { - status := p.crd.Status - - var result map[string]interface{} - marshalledStatus, _ := json.Marshal(status) - json.Unmarshal(marshalledStatus, &result) - - return result -} - type Policy struct { u unstructured.Unstructured // targetRef references the target object this policy is attached to. This diff --git a/gwctl/pkg/printer/policies.go b/gwctl/pkg/printer/policies.go index 0b9099059a..4713945c51 100644 --- a/gwctl/pkg/printer/policies.go +++ b/gwctl/pkg/printer/policies.go @@ -17,6 +17,7 @@ limitations under the License. package printer import ( + "encoding/json" "fmt" "io" "os" @@ -152,11 +153,8 @@ func (pp *PoliciesPrinter) PrintDescribeView(policies []policymanager.Policy) { } type policyCrdDescribeView struct { - // PolicyCrd *apiextensionsv1.CustomResourceDefinition `json:",omitempty"` Name string `json:",omitempty"` Namespace string `json:",omitempty"` - Labels map[string]string `json:",omitempty"` - Annotations map[string]string `json:",omitempty"` APIVersion string `json:",omitempty"` Kind string `json:",omitempty"` Metadata map[string]interface{} `json:",omitempty"` @@ -179,22 +177,18 @@ func (pp *PoliciesPrinter) PolicyCrd_PrintDescribeView(policyCrds []policymanage Name: crd.Name, Namespace: crd.Namespace, }, - { - Labels: crd.Labels, - Annotations: crd.Annotations, - }, { APIVersion: crd.APIVersion, Kind: crd.Kind, }, { - Metadata: policyCrd.Metadata(), + Metadata: policyCrdMetadata(policyCrd), }, { - Spec: policyCrd.Spec(), + Spec: policyCrdSpec(policyCrd), }, { - Status: policyCrd.Status(), + Status: policyCrdStatus(policyCrd), }, } @@ -212,3 +206,33 @@ func (pp *PoliciesPrinter) PolicyCrd_PrintDescribeView(policyCrds []policymanage } } } + +func policyCrdSpec(p policymanager.PolicyCRD) map[string]interface{} { + spec := p.CRD().Spec + + var result map[string]interface{} + marshalledSpec, _ := json.Marshal(spec) + json.Unmarshal(marshalledSpec, &result) + + return result +} + +func policyCrdMetadata(p policymanager.PolicyCRD) map[string]interface{} { + om := p.CRD().ObjectMeta + + var result map[string]interface{} + marshalledMetadata, _ := json.Marshal(om) + json.Unmarshal(marshalledMetadata, &result) + + return result +} + +func policyCrdStatus(p policymanager.PolicyCRD) map[string]interface{} { + status := p.CRD().Status + + var result map[string]interface{} + marshalledStatus, _ := json.Marshal(status) + json.Unmarshal(marshalledStatus, &result) + + return result +} \ No newline at end of file diff --git a/gwctl/pkg/printer/policies_test.go b/gwctl/pkg/printer/policies_test.go index 9823f0b8c5..c8d68ead3a 100644 --- a/gwctl/pkg/printer/policies_test.go +++ b/gwctl/pkg/printer/policies_test.go @@ -356,7 +356,6 @@ timeoutpolicies.bar.com Direct Namespaced 5m } func TestPolicyCrd_PrintDescribeView(t *testing.T) { - // fakeClock := testingclock.NewFakeClock(time.Now()) objects := []runtime.Object{ &apiextensionsv1.CustomResourceDefinition{ ObjectMeta: metav1.ObjectMeta{ @@ -439,15 +438,12 @@ func TestPolicyCrd_PrintDescribeView(t *testing.T) { params := utils.MustParamsForTest(t, common.MustClientsForTest(t, objects...)) pp := &PoliciesPrinter{ Out: &bytes.Buffer{}, - // Clock: fakeClock, } pp.PolicyCrd_PrintDescribeView(params.PolicyManager.GetCRDs()) got := pp.Out.(*bytes.Buffer).String() want := ` Name: healthcheckpolicies.foo.com -Labels: - gateway.networking.k8s.io/policy: inherited APIVersion: apiextensions.k8s.io/v1 Kind: CustomResourceDefinition Metadata: @@ -475,8 +471,6 @@ Status: Name: timeoutpolicies.bar.com -Labels: - gateway.networking.k8s.io/policy: direct APIVersion: apiextensions.k8s.io/v1 Kind: CustomResourceDefinition Metadata: