diff --git a/gwctl/pkg/printer/policies.go b/gwctl/pkg/printer/policies.go index 3ad641a445..7c23b09777 100644 --- a/gwctl/pkg/printer/policies.go +++ b/gwctl/pkg/printer/policies.go @@ -28,6 +28,7 @@ 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" ) @@ -157,7 +158,7 @@ type policyCrdDescribeView struct { Kind string `json:",omitempty"` Labels map[string]string `json:",omitempty"` Annotations map[string]string `json:",omitempty"` - Metadata interface{} `json:",omitempty"` + Metadata *metav1.ObjectMeta `json:",omitempty"` Spec *apiextensionsv1.CustomResourceDefinitionSpec `json:",omitempty"` Status *apiextensionsv1.CustomResourceDefinitionStatus `json:",omitempty"` } @@ -172,12 +173,11 @@ 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) + metadata := crd.ObjectMeta.DeepCopy() + metadata.Labels = nil + metadata.Annotations = nil + metadata.Name = "" + metadata.Namespace = "" views := []policyCrdDescribeView{ { @@ -189,11 +189,11 @@ func (pp *PoliciesPrinter) PrintPolicyCRDsDescribeView(policyCrds []policymanage Kind: crd.Kind, }, { - Labels: crd.Labels, + Labels: crd.Labels, Annotations: crd.Annotations, }, { - Metadata: modifiedMetadata, + Metadata: metadata, }, { Spec: &crd.Spec, diff --git a/gwctl/pkg/printer/policies_test.go b/gwctl/pkg/printer/policies_test.go index a11051a505..9a6ff127da 100644 --- a/gwctl/pkg/printer/policies_test.go +++ b/gwctl/pkg/printer/policies_test.go @@ -363,6 +363,7 @@ func TestPolicyCrd_PrintDescribeView(t *testing.T) { Labels: map[string]string{ gatewayv1alpha2.PolicyLabelKey: "inherited", }, + CreationTimestamp: metav1.NewTime(time.Date(2024, time.Month(2), 1, 13, 9, 0, 0, time.UTC)), }, Spec: apiextensionsv1.CustomResourceDefinitionSpec{ Scope: apiextensionsv1.ClusterScoped, @@ -405,6 +406,7 @@ func TestPolicyCrd_PrintDescribeView(t *testing.T) { Labels: map[string]string{ gatewayv1alpha2.PolicyLabelKey: "direct", }, + CreationTimestamp: metav1.NewTime(time.Date(2023, time.Month(11), 9, 4, 56, 0, 0, time.UTC)), }, Spec: apiextensionsv1.CustomResourceDefinitionSpec{ Scope: apiextensionsv1.NamespaceScoped, @@ -449,8 +451,7 @@ Kind: CustomResourceDefinition Labels: gateway.networking.k8s.io/policy: inherited Metadata: - creationTimestamp: null - name: healthcheckpolicies.foo.com + creationTimestamp: "2024-02-01T13:09:00Z" resourceVersion: "999" Spec: group: foo.com @@ -476,8 +477,7 @@ Kind: CustomResourceDefinition Labels: gateway.networking.k8s.io/policy: direct Metadata: - creationTimestamp: null - name: timeoutpolicies.bar.com + creationTimestamp: "2023-11-09T04:56:00Z" resourceVersion: "999" Spec: group: bar.com diff --git a/gwctl/pkg/printer/utils.go b/gwctl/pkg/printer/utils.go deleted file mode 100644 index 84b3365720..0000000000 --- a/gwctl/pkg/printer/utils.go +++ /dev/null @@ -1,41 +0,0 @@ -/* -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