generated from kubernetes/kubernetes-template-project
-
Notifications
You must be signed in to change notification settings - Fork 480
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Implement basic functionality of gwctl describe namespace #2836
Merged
k8s-ci-robot
merged 4 commits into
kubernetes-sigs:main
from
Devaansh-Kumar:gwctl/describe-namespace
Mar 11, 2024
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
6839ab2
Implement basic functionality of gwctl describe namespace
Devaansh-Kumar 26119f1
Finalized printing of gwctl describe namespace command
Devaansh-Kumar 790a9bb
Added test for gwctl describe namespace and modified ResourceModel.ad…
Devaansh-Kumar e3bbe15
Fixed failing httproutes test and Makefile
Devaansh-Kumar File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -19,4 +19,4 @@ deps: | |
|
||
build: deps | ||
@echo "Building gwctl..." | ||
@go build -o bin/gwctl cmd/main.go | ||
@go build -o bin/gwctl main.go |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
/* | ||
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 ( | ||
"fmt" | ||
"io" | ||
"os" | ||
|
||
"sigs.k8s.io/gateway-api/gwctl/pkg/policymanager" | ||
"sigs.k8s.io/gateway-api/gwctl/pkg/resourcediscovery" | ||
"sigs.k8s.io/yaml" | ||
) | ||
|
||
type NamespacesPrinter struct { | ||
Out io.Writer | ||
} | ||
|
||
type namespaceDescribeView struct { | ||
Name string `json:",omitempty"` | ||
Labels map[string]string `json:",omitempty"` | ||
Annotations map[string]string `json:",omitempty"` | ||
Status string `json:",omitempty"` | ||
DirectlyAttachedPolicies []policymanager.ObjRef `json:",omitempty"` | ||
} | ||
|
||
func (nsp *NamespacesPrinter) PrintDescribeView(resourceModel *resourcediscovery.ResourceModel) { | ||
index := 0 | ||
for _, namespaceNode := range resourceModel.Namespaces { | ||
index++ | ||
|
||
views := []namespaceDescribeView{ | ||
{ | ||
Name: namespaceNode.Namespace.Name, | ||
}, | ||
{ | ||
Annotations: namespaceNode.Namespace.Annotations, | ||
Labels: namespaceNode.Namespace.Labels, | ||
}, | ||
{ | ||
Status: string(namespaceNode.Namespace.Status.Phase), | ||
}, | ||
} | ||
|
||
if policyRefs := resourcediscovery.ConvertPoliciesMapToPolicyRefs(namespaceNode.Policies); len(policyRefs) != 0 { | ||
views = append(views, namespaceDescribeView{ | ||
DirectlyAttachedPolicies: policyRefs, | ||
}) | ||
} | ||
|
||
for _, view := range views { | ||
b, err := yaml.Marshal(view) | ||
if err != nil { | ||
fmt.Fprintf(os.Stderr, "failed to marshal to yaml: %v\n", err) | ||
os.Exit(1) | ||
} | ||
fmt.Fprint(nsp.Out, string(b)) | ||
} | ||
|
||
if index+1 <= len(resourceModel.Namespaces) { | ||
fmt.Fprintf(nsp.Out, "\n\n") | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,187 @@ | ||
/* | ||
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 ( | ||
"bytes" | ||
"testing" | ||
|
||
"github.com/google/go-cmp/cmp" | ||
corev1 "k8s.io/api/core/v1" | ||
apiextensionsv1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1" | ||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured" | ||
"k8s.io/apimachinery/pkg/runtime" | ||
gatewayv1alpha2 "sigs.k8s.io/gateway-api/apis/v1alpha2" | ||
"sigs.k8s.io/gateway-api/gwctl/pkg/utils" | ||
"sigs.k8s.io/gateway-api/gwctl/pkg/common" | ||
"sigs.k8s.io/gateway-api/gwctl/pkg/resourcediscovery" | ||
) | ||
|
||
func TestNamespacePrinter_PrintDescribeView(t *testing.T) { | ||
objects := []runtime.Object{ | ||
// Defining a Namespace called development | ||
&corev1.Namespace{ | ||
ObjectMeta: metav1.ObjectMeta{ | ||
Name: "development", | ||
Labels: map[string]string{ | ||
"type": "test-namespace", | ||
}, | ||
Annotations: map[string]string{ | ||
"test-annotation": "development-annotation", | ||
}, | ||
}, | ||
Status: corev1.NamespaceStatus{ | ||
Phase: corev1.NamespaceActive, | ||
}, | ||
}, | ||
|
||
// CRD and definition for HealthCheckPolicy attached to development namespace | ||
&apiextensionsv1.CustomResourceDefinition{ | ||
ObjectMeta: metav1.ObjectMeta{ | ||
Name: "healthcheckpolicies.foo.com", | ||
Labels: map[string]string{ | ||
gatewayv1alpha2.PolicyLabelKey: "inherited", | ||
}, | ||
}, | ||
Spec: apiextensionsv1.CustomResourceDefinitionSpec{ | ||
Scope: apiextensionsv1.ClusterScoped, | ||
Group: "foo.com", | ||
Versions: []apiextensionsv1.CustomResourceDefinitionVersion{{Name: "v1"}}, | ||
Names: apiextensionsv1.CustomResourceDefinitionNames{ | ||
Plural: "healthcheckpolicies", | ||
Kind: "HealthCheckPolicy", | ||
}, | ||
}, | ||
}, | ||
&unstructured.Unstructured{ | ||
Object: map[string]interface{}{ | ||
"apiVersion": "foo.com/v1", | ||
"kind": "HealthCheckPolicy", | ||
"metadata": map[string]interface{}{ | ||
"name": "health-check-gatewayclass", | ||
}, | ||
"spec": map[string]interface{}{ | ||
"override": map[string]interface{}{ | ||
"key1": "value-parent-1", | ||
"key3": "value-parent-3", | ||
"key5": "value-parent-5", | ||
}, | ||
"default": map[string]interface{}{ | ||
"key2": "value-parent-2", | ||
"key4": "value-parent-4", | ||
}, | ||
"targetRef": map[string]interface{}{ | ||
"kind": "Namespace", | ||
"name": "development", | ||
}, | ||
}, | ||
}, | ||
}, | ||
|
||
// Defining a Namespace called production | ||
&corev1.Namespace{ | ||
ObjectMeta: metav1.ObjectMeta{ | ||
Name: "production", | ||
Labels: map[string]string{ | ||
"type": "production-namespace", | ||
}, | ||
}, | ||
Status: corev1.NamespaceStatus{ | ||
Phase: corev1.NamespaceActive, | ||
}, | ||
}, | ||
// CRD and definition for TimeoutPolicy attached to default namespace | ||
&apiextensionsv1.CustomResourceDefinition{ | ||
ObjectMeta: metav1.ObjectMeta{ | ||
Name: "timeoutpolicies.bar.com", | ||
Labels: map[string]string{ | ||
gatewayv1alpha2.PolicyLabelKey: "direct", | ||
}, | ||
}, | ||
Spec: apiextensionsv1.CustomResourceDefinitionSpec{ | ||
Scope: apiextensionsv1.ClusterScoped, | ||
Group: "bar.com", | ||
Versions: []apiextensionsv1.CustomResourceDefinitionVersion{{Name: "v1"}}, | ||
Names: apiextensionsv1.CustomResourceDefinitionNames{ | ||
Plural: "timeoutpolicies", | ||
Kind: "TimeoutPolicy", | ||
}, | ||
}, | ||
}, | ||
&unstructured.Unstructured{ | ||
Object: map[string]interface{}{ | ||
"apiVersion": "bar.com/v1", | ||
"kind": "TimeoutPolicy", | ||
"metadata": map[string]interface{}{ | ||
"name": "timeout-policy-namespace", | ||
}, | ||
"spec": map[string]interface{}{ | ||
"condition": "path=/abc", | ||
"seconds": int64(30), | ||
"targetRef": map[string]interface{}{ | ||
"kind": "Namespace", | ||
"name": "production", | ||
}, | ||
}, | ||
}, | ||
}, | ||
} | ||
|
||
params := utils.MustParamsForTest(t, common.MustClientsForTest(t, objects...)) | ||
discoverer := resourcediscovery.Discoverer{ | ||
K8sClients: params.K8sClients, | ||
PolicyManager: params.PolicyManager, | ||
} | ||
resourceModel, err := discoverer.DiscoverResourcesForNamespace(resourcediscovery.Filter{}) | ||
if err != nil { | ||
t.Fatalf("Failed to construct resourceModel: %v", resourceModel) | ||
} | ||
|
||
nsp := &NamespacesPrinter{ | ||
Out: params.Out, | ||
} | ||
nsp.PrintDescribeView(resourceModel) | ||
|
||
got := params.Out.(*bytes.Buffer).String() | ||
want := ` | ||
Name: development | ||
Annotations: | ||
test-annotation: development-annotation | ||
Labels: | ||
type: test-namespace | ||
Status: Active | ||
DirectlyAttachedPolicies: | ||
- Group: foo.com | ||
Kind: HealthCheckPolicy | ||
Name: health-check-gatewayclass | ||
|
||
|
||
Name: production | ||
Labels: | ||
type: production-namespace | ||
Status: Active | ||
DirectlyAttachedPolicies: | ||
- Group: bar.com | ||
Kind: TimeoutPolicy | ||
Name: timeout-policy-namespace | ||
` | ||
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) | ||
} | ||
|
||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Lets add a test for this as well. (You can use some other tests like those for gateway and httproute for examples on how they can be written)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes I was planning to add that in a seperate PR, but I will do it in this one PR itself now.