Skip to content

Commit

Permalink
Used concrete types for policycrds and renamed functions and tests fo…
Browse files Browse the repository at this point in the history
…r policies and policycrds
  • Loading branch information
Devaansh-Kumar committed Apr 3, 2024
1 parent 3006dd2 commit 1d78035
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 61 deletions.
8 changes: 3 additions & 5 deletions gwctl/cmd/describe.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ func runDescribe(cmd *cobra.Command, args []string, params *utils.CmdParams) {
policyList = []policymanager.Policy{policy}
}
}
policiesPrinter.PrintDescribeView(policyList)
policiesPrinter.PrintPoliciesDescribeView(policyList)

case "policycrd", "policycrds":
var policyCrdList []policymanager.PolicyCRD
Expand All @@ -107,11 +107,9 @@ func runDescribe(cmd *cobra.Command, args []string, params *utils.CmdParams) {
fmt.Fprintf(os.Stderr, "failed to find PolicyCrd: %v\n", err)
os.Exit(1)
}
if found {
policyCrdList = []policymanager.PolicyCRD{policyCrd}
}
policyCrdList = []policymanager.PolicyCRD{policyCrd}
}
policiesPrinter.PolicyCrd_PrintDescribeView(policyCrdList)
policiesPrinter.PrintPolicyCRDsDescribeView(policyCrdList)

case "httproute", "httproutes":
filter := resourcediscovery.Filter{Namespace: ns}
Expand Down
6 changes: 3 additions & 3 deletions gwctl/cmd/get.go
Original file line number Diff line number Diff line change
Expand Up @@ -136,11 +136,11 @@ func runGet(cmd *cobra.Command, args []string, params *utils.CmdParams) {

case "policy", "policies":
list := params.PolicyManager.GetPolicies()
policiesPrinter.Print(list)
policiesPrinter.PrintPoliciesGetView(list)

case "policycrds":
case "policycrd", "policycrds":
list := params.PolicyManager.GetCRDs()
policiesPrinter.PrintCRDs(list)
policiesPrinter.PrintPolicyCRDsGetView(list)

case "httproute", "httproutes":
selector, err := labels.Parse(labelSelector)
Expand Down
63 changes: 16 additions & 47 deletions gwctl/pkg/printer/policies.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ limitations under the License.
package printer

import (
"encoding/json"
"fmt"
"io"
"os"
Expand All @@ -28,8 +27,8 @@ import (
"sigs.k8s.io/gateway-api/gwctl/pkg/policymanager"
"sigs.k8s.io/yaml"

_ "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1"
_ "k8s.io/apimachinery/pkg/apis/meta/v1"
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"
)
Expand All @@ -39,7 +38,7 @@ type PoliciesPrinter struct {
Clock clock.Clock
}

func (pp *PoliciesPrinter) Print(policies []policymanager.Policy) {
func (pp *PoliciesPrinter) PrintPoliciesGetView(policies []policymanager.Policy) {
sort.Slice(policies, func(i, j int) bool {
a := fmt.Sprintf("%v/%v", policies[i].Unstructured().GetNamespace(), policies[i].Unstructured().GetName())
b := fmt.Sprintf("%v/%v", policies[j].Unstructured().GetNamespace(), policies[j].Unstructured().GetName())
Expand Down Expand Up @@ -73,7 +72,7 @@ func (pp *PoliciesPrinter) Print(policies []policymanager.Policy) {
tw.Flush()
}

func (pp *PoliciesPrinter) PrintCRDs(policyCRDs []policymanager.PolicyCRD) {
func (pp *PoliciesPrinter) PrintPolicyCRDsGetView(policyCRDs []policymanager.PolicyCRD) {
sort.Slice(policyCRDs, func(i, j int) bool {
a := fmt.Sprintf("%v/%v", policyCRDs[i].CRD().GetNamespace(), policyCRDs[i].CRD().GetName())
b := fmt.Sprintf("%v/%v", policyCRDs[j].CRD().GetNamespace(), policyCRDs[j].CRD().GetName())
Expand Down Expand Up @@ -112,7 +111,7 @@ type policyDescribeView struct {
Spec map[string]interface{} `json:",omitempty"`
}

func (pp *PoliciesPrinter) PrintDescribeView(policies []policymanager.Policy) {
func (pp *PoliciesPrinter) PrintPoliciesDescribeView(policies []policymanager.Policy) {
sort.Slice(policies, func(i, j int) bool {
a := fmt.Sprintf("%v/%v", policies[i].Unstructured().GetNamespace(), policies[i].Unstructured().GetName())
b := fmt.Sprintf("%v/%v", policies[j].Unstructured().GetNamespace(), policies[j].Unstructured().GetName())
Expand Down Expand Up @@ -153,16 +152,16 @@ func (pp *PoliciesPrinter) PrintDescribeView(policies []policymanager.Policy) {
}

type policyCrdDescribeView struct {
Name string `json:",omitempty"`
Namespace string `json:",omitempty"`
APIVersion string `json:",omitempty"`
Kind string `json:",omitempty"`
Metadata map[string]interface{} `json:",omitempty"`
Spec map[string]interface{} `json:",omitempty"`
Status map[string]interface{} `json:",omitempty"`
Name string `json:",omitempty"`
Namespace string `json:",omitempty"`
APIVersion string `json:",omitempty"`
Kind string `json:",omitempty"`
Metadata *metav1.ObjectMeta `json:",omitempty"`
Spec *apiextensionsv1.CustomResourceDefinitionSpec `json:",omitempty"`
Status *apiextensionsv1.CustomResourceDefinitionStatus `json:",omitempty"`
}

func (pp *PoliciesPrinter) PolicyCrd_PrintDescribeView(policyCrds []policymanager.PolicyCRD) {
func (pp *PoliciesPrinter) PrintPolicyCRDsDescribeView(policyCrds []policymanager.PolicyCRD) {
sort.Slice(policyCrds, func(i, j int) bool {
a := fmt.Sprintf("%v/%v", policyCrds[i].CRD().GetNamespace(), policyCrds[i].CRD().GetName())
b := fmt.Sprintf("%v/%v", policyCrds[j].CRD().GetNamespace(), policyCrds[j].CRD().GetName())
Expand All @@ -182,13 +181,13 @@ func (pp *PoliciesPrinter) PolicyCrd_PrintDescribeView(policyCrds []policymanage
Kind: crd.Kind,
},
{
Metadata: policyCrdMetadata(policyCrd),
Metadata: &crd.ObjectMeta,
},
{
Spec: policyCrdSpec(policyCrd),
Spec: &crd.Spec,
},
{
Status: policyCrdStatus(policyCrd),
Status: &crd.Status,
},
}

Expand All @@ -206,33 +205,3 @@ 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
}
12 changes: 6 additions & 6 deletions gwctl/pkg/printer/policies_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ func TestPoliciesPrinter_Print_And_PrintDescribeView(t *testing.T) {
Clock: fakeClock,
}

pp.Print(params.PolicyManager.GetPolicies())
pp.PrintPoliciesGetView(params.PolicyManager.GetPolicies())
got := pp.Out.(*bytes.Buffer).String()
want := `
NAME KIND TARGET NAME TARGET KIND POLICY TYPE AGE
Expand All @@ -183,7 +183,7 @@ timeout-policy-namespace TimeoutPolicy.bar.com default Namespac
}

pp.Out = &bytes.Buffer{}
pp.PrintDescribeView(params.PolicyManager.GetPolicies())
pp.PrintPoliciesDescribeView(params.PolicyManager.GetPolicies())
got = pp.Out.(*bytes.Buffer).String()
want = `
Name: health-check-gateway
Expand Down Expand Up @@ -342,7 +342,7 @@ func TestPoliciesPrinter_PrintCRDs(t *testing.T) {
Out: &bytes.Buffer{},
Clock: fakeClock,
}
pp.PrintCRDs(params.PolicyManager.GetCRDs())
pp.PrintPolicyCRDsGetView(params.PolicyManager.GetCRDs())

got := pp.Out.(*bytes.Buffer).String()
want := `
Expand Down Expand Up @@ -437,9 +437,9 @@ func TestPolicyCrd_PrintDescribeView(t *testing.T) {

params := utils.MustParamsForTest(t, common.MustClientsForTest(t, objects...))
pp := &PoliciesPrinter{
Out: &bytes.Buffer{},
Out: &bytes.Buffer{},
}
pp.PolicyCrd_PrintDescribeView(params.PolicyManager.GetCRDs())
pp.PrintPolicyCRDsDescribeView(params.PolicyManager.GetCRDs())

got := pp.Out.(*bytes.Buffer).String()
want := `
Expand Down Expand Up @@ -499,4 +499,4 @@ Status:
if diff := cmp.Diff(common.YamlString(want), common.YamlString(got), common.YamlStringTransformer); diff != "" {
t.Errorf("Unexpected diff\ngot=\n%v\nwant=\n%v\ndiff (-want +got)=\n%v", got, want, diff)
}
}
}

0 comments on commit 1d78035

Please sign in to comment.