Skip to content

Commit

Permalink
Added functionality to remove fields from a struct
Browse files Browse the repository at this point in the history
  • Loading branch information
Devaansh-Kumar committed Apr 2, 2024
1 parent bce74df commit 87cd786
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 13 deletions.
30 changes: 21 additions & 9 deletions gwctl/pkg/printer/policies.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
Expand Down Expand Up @@ -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) {
Expand All @@ -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,
Expand All @@ -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,
Expand Down
8 changes: 4 additions & 4 deletions gwctl/pkg/printer/policies_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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:
Expand Down
41 changes: 41 additions & 0 deletions gwctl/pkg/printer/utils.go
Original file line number Diff line number Diff line change
@@ -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()
}

0 comments on commit 87cd786

Please sign in to comment.