diff --git a/gwctl/pkg/printer/policies.go b/gwctl/pkg/printer/policies.go index d7faff4fe0..3ad641a445 100644 --- a/gwctl/pkg/printer/policies.go +++ b/gwctl/pkg/printer/policies.go @@ -28,7 +28,6 @@ import ( "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" ) @@ -152,13 +151,15 @@ func (pp *PoliciesPrinter) PrintPoliciesDescribeView(policies []policymanager.Po } type policyCrdDescribeView struct { - 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"` + Name string `json:",omitempty"` + Namespace string `json:",omitempty"` + APIVersion string `json:",omitempty"` + Kind string `json:",omitempty"` + Labels map[string]string `json:",omitempty"` + Annotations map[string]string `json:",omitempty"` + Metadata interface{} `json:",omitempty"` + Spec *apiextensionsv1.CustomResourceDefinitionSpec `json:",omitempty"` + Status *apiextensionsv1.CustomResourceDefinitionStatus `json:",omitempty"` } func (pp *PoliciesPrinter) PrintPolicyCRDsDescribeView(policyCrds []policymanager.PolicyCRD) { @@ -171,6 +172,13 @@ func (pp *PoliciesPrinter) PrintPolicyCRDsDescribeView(policyCrds []policymanage for i, policyCrd := range policyCrds { crd := policyCrd.CRD() + excludedFieldsFromMetadata := map[string]bool{ + "Labels": true, + "Annotations": true, + } + + modifiedMetadata := ExcludeFieldsFromStruct(crd.ObjectMeta, excludedFieldsFromMetadata) + views := []policyCrdDescribeView{ { Name: crd.Name, @@ -181,7 +189,11 @@ func (pp *PoliciesPrinter) PrintPolicyCRDsDescribeView(policyCrds []policymanage Kind: crd.Kind, }, { - Metadata: &crd.ObjectMeta, + Labels: crd.Labels, + Annotations: crd.Annotations, + }, + { + Metadata: modifiedMetadata, }, { Spec: &crd.Spec, diff --git a/gwctl/pkg/printer/policies_test.go b/gwctl/pkg/printer/policies_test.go index 554961a49f..a11051a505 100644 --- a/gwctl/pkg/printer/policies_test.go +++ b/gwctl/pkg/printer/policies_test.go @@ -446,10 +446,10 @@ func TestPolicyCrd_PrintDescribeView(t *testing.T) { Name: healthcheckpolicies.foo.com APIVersion: apiextensions.k8s.io/v1 Kind: CustomResourceDefinition +Labels: + gateway.networking.k8s.io/policy: inherited Metadata: creationTimestamp: null - labels: - gateway.networking.k8s.io/policy: inherited name: healthcheckpolicies.foo.com resourceVersion: "999" Spec: @@ -473,10 +473,10 @@ Status: Name: timeoutpolicies.bar.com APIVersion: apiextensions.k8s.io/v1 Kind: CustomResourceDefinition +Labels: + gateway.networking.k8s.io/policy: direct Metadata: creationTimestamp: null - labels: - gateway.networking.k8s.io/policy: direct name: timeoutpolicies.bar.com resourceVersion: "999" Spec: diff --git a/gwctl/pkg/printer/utils.go b/gwctl/pkg/printer/utils.go new file mode 100644 index 0000000000..84b3365720 --- /dev/null +++ b/gwctl/pkg/printer/utils.go @@ -0,0 +1,41 @@ +/* +Copyright 2024 The Kubernetes Authors. + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +*/ + +package printer + +import "reflect" + +// ExcludeFieldsFromStruct will accept a struct and a map containing fields to be removed +// and return a new struct with the removed fields +func ExcludeFieldsFromStruct(originalStruct interface{}, excludeFields map[string]bool) interface{} { + typ := reflect.TypeOf(originalStruct) + + fieldValues := make(map[int]reflect.Value) + for i := 0; i < typ.NumField(); i++ { + field := typ.Field(i) + + if !excludeFields[field.Name] { + fieldValues[i] = reflect.ValueOf(originalStruct).Field(i) + } + } + + newValue := reflect.New(typ).Elem() + for i, value := range fieldValues { + newValue.Field(i).Set(value) + } + + return newValue.Interface() +} \ No newline at end of file