Skip to content

Commit

Permalink
Modified GetCRD to fetch PolicyCRD based on CRD name
Browse files Browse the repository at this point in the history
  • Loading branch information
Devaansh-Kumar committed Apr 3, 2024
1 parent 9c219d8 commit 3006dd2
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 48 deletions.
39 changes: 7 additions & 32 deletions gwctl/pkg/policymanager/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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
Expand Down
44 changes: 34 additions & 10 deletions gwctl/pkg/printer/policies.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ limitations under the License.
package printer

import (
"encoding/json"
"fmt"
"io"
"os"
Expand Down Expand Up @@ -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"`
Expand All @@ -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),
},
}

Expand All @@ -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
}
6 changes: 0 additions & 6 deletions gwctl/pkg/printer/policies_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{
Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -475,8 +471,6 @@ Status:
Name: timeoutpolicies.bar.com
Labels:
gateway.networking.k8s.io/policy: direct
APIVersion: apiextensions.k8s.io/v1
Kind: CustomResourceDefinition
Metadata:
Expand Down

0 comments on commit 3006dd2

Please sign in to comment.