diff --git a/gwctl/cmd/describe.go b/gwctl/cmd/describe.go index 6dd4edd36e..70d73015b0 100644 --- a/gwctl/cmd/describe.go +++ b/gwctl/cmd/describe.go @@ -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 @@ -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} diff --git a/gwctl/cmd/get.go b/gwctl/cmd/get.go index 330356ccde..25dd1665f7 100644 --- a/gwctl/cmd/get.go +++ b/gwctl/cmd/get.go @@ -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) diff --git a/gwctl/pkg/printer/policies.go b/gwctl/pkg/printer/policies.go index 4713945c51..d7faff4fe0 100644 --- a/gwctl/pkg/printer/policies.go +++ b/gwctl/pkg/printer/policies.go @@ -17,7 +17,6 @@ limitations under the License. package printer import ( - "encoding/json" "fmt" "io" "os" @@ -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" ) @@ -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()) @@ -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()) @@ -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()) @@ -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()) @@ -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, }, } @@ -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 -} \ No newline at end of file diff --git a/gwctl/pkg/printer/policies_test.go b/gwctl/pkg/printer/policies_test.go index c8d68ead3a..554961a49f 100644 --- a/gwctl/pkg/printer/policies_test.go +++ b/gwctl/pkg/printer/policies_test.go @@ -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 @@ -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 @@ -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 := ` @@ -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 := ` @@ -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) } -} \ No newline at end of file +}