From cb9c21debe8d15a3a54e97228f01bbf8b5e0fb18 Mon Sep 17 00:00:00 2001 From: Guilherme Vicentin <79584418+gvicentin@users.noreply.github.com> Date: Thu, 16 May 2024 16:07:59 -0300 Subject: [PATCH] feat: Display annotations using rpaasv2 info command (#152) * feat: Display annotations using rpaasv2 info command * filtering custom annotations --- cmd/plugin/rpaasv2/cmd/info.go | 1 + cmd/plugin/rpaasv2/cmd/info_test.go | 9 ++++++++- internal/pkg/rpaas/k8s.go | 13 +++++++++++++ internal/pkg/rpaas/k8s_test.go | 7 ++++++- pkg/rpaas/client/types/types.go | 1 + 5 files changed, 29 insertions(+), 2 deletions(-) diff --git a/cmd/plugin/rpaasv2/cmd/info.go b/cmd/plugin/rpaasv2/cmd/info.go index 7ab522ce..d83c4f06 100644 --- a/cmd/plugin/rpaasv2/cmd/info.go +++ b/cmd/plugin/rpaasv2/cmd/info.go @@ -85,6 +85,7 @@ Dashboard: {{ .Dashboard }} {{- end }} Description: {{ .Description }} Tags: {{ join ", " .Tags }} +Annotations: {{ join ", " .Annotations }} Team owner: {{ .Team }} Plan: {{ .Plan }} Flavors: {{ join ", " .Flavors }} diff --git a/cmd/plugin/rpaasv2/cmd/info_test.go b/cmd/plugin/rpaasv2/cmd/info_test.go index 5e499255..0da22a9c 100644 --- a/cmd/plugin/rpaasv2/cmd/info_test.go +++ b/cmd/plugin/rpaasv2/cmd/info_test.go @@ -112,6 +112,7 @@ func TestInfo(t *testing.T) { Pool: "my-pool", Description: "some description", Tags: []string{"tag1", "tag2", "tag3"}, + Annotations: []string{"key1=val1", "key2=val2", "key3=val3"}, Flavors: []string{"flavor1", "flavor2", "flavor-N"}, Autoscale: &autogenerated.Autoscale{ MaxReplicas: 5, @@ -325,6 +326,7 @@ func TestInfo(t *testing.T) { Dashboard: https://grafana.mycompany.io/my-instance Description: some description Tags: tag1, tag2, tag3 +Annotations: key1=val1, key2=val2, key3=val3 Team owner: some-team Plan: basic Flavors: flavor1, flavor2, flavor-N @@ -514,6 +516,7 @@ Events: Cluster: "my-dedicated-cluster", Description: "some description", Tags: []string{"tag1", "tag2", "tag3"}, + Annotations: []string{"key1=val1", "key2=val2", "key3=val3"}, Flavors: []string{"flavor1", "flavor2", "flavor-N"}, Autoscale: nil, Pods: []clientTypes.Pod{ @@ -583,6 +586,7 @@ Events: expected: `Name: my-instance Description: some description Tags: tag1, tag2, tag3 +Annotations: key1=val1, key2=val2, key3=val3 Team owner: some-team Plan: basic Flavors: flavor1, flavor2, flavor-N @@ -618,6 +622,7 @@ Pods: (current: 2 / desired: 3) Cluster: "my-dedicated-cluster", Description: "some description", Tags: []string{"tag1", "tag2", "tag3"}, + Annotations: []string{"key1=val1", "key2=val2", "key3=val3"}, Flavors: []string{"flavor1", "flavor2", "flavor-N"}, Autoscale: nil, Pods: []clientTypes.Pod{ @@ -679,6 +684,7 @@ Pods: (current: 2 / desired: 3) expected: `Name: my-instance Description: some description Tags: tag1, tag2, tag3 +Annotations: key1=val1, key2=val2, key3=val3 Team owner: some-team Plan: basic Flavors: flavor1, flavor2, flavor-N @@ -740,11 +746,12 @@ Pods: (current: 2 / desired: 3) Team: "some team", Description: "some description", Tags: []string{"tag1", "tag2", "tag3"}, + Annotations: []string{"key1=val1", "key2=val2", "key3=val3"}, Shutdown: true, }, nil }, }, - expected: "{\n\t\"addresses\": [\n\t\t{\n\t\t\t\"type\": \"cluster-external\",\n\t\t\t\"hostname\": \"some-host\",\n\t\t\t\"ip\": \"0.0.0.0\",\n\t\t\t\"status\": \"ready\"\n\t\t},\n\t\t{\n\t\t\t\"type\": \"cluster-external\",\n\t\t\t\"hostname\": \"some-host2\",\n\t\t\t\"ip\": \"0.0.0.1\",\n\t\t\t\"status\": \"ready\"\n\t\t}\n\t],\n\t\"replicas\": 5,\n\t\"plan\": \"basic\",\n\t\"routes\": [\n\t\t{\n\t\t\t\"path\": \"some-path\",\n\t\t\t\"destination\": \"some-destination\"\n\t\t}\n\t],\n\t\"binds\": [\n\t\t{\n\t\t\t\"name\": \"some-name\",\n\t\t\t\"host\": \"some-host\"\n\t\t},\n\t\t{\n\t\t\t\"name\": \"some-name2\",\n\t\t\t\"host\": \"some-host2\"\n\t\t}\n\t],\n\t\"team\": \"some team\",\n\t\"name\": \"my-instance\",\n\t\"description\": \"some description\",\n\t\"tags\": [\n\t\t\"tag1\",\n\t\t\"tag2\",\n\t\t\"tag3\"\n\t],\n\t\"shutdown\": true\n}\n", + expected: "{\n\t\"addresses\": [\n\t\t{\n\t\t\t\"type\": \"cluster-external\",\n\t\t\t\"hostname\": \"some-host\",\n\t\t\t\"ip\": \"0.0.0.0\",\n\t\t\t\"status\": \"ready\"\n\t\t},\n\t\t{\n\t\t\t\"type\": \"cluster-external\",\n\t\t\t\"hostname\": \"some-host2\",\n\t\t\t\"ip\": \"0.0.0.1\",\n\t\t\t\"status\": \"ready\"\n\t\t}\n\t],\n\t\"replicas\": 5,\n\t\"plan\": \"basic\",\n\t\"routes\": [\n\t\t{\n\t\t\t\"path\": \"some-path\",\n\t\t\t\"destination\": \"some-destination\"\n\t\t}\n\t],\n\t\"binds\": [\n\t\t{\n\t\t\t\"name\": \"some-name\",\n\t\t\t\"host\": \"some-host\"\n\t\t},\n\t\t{\n\t\t\t\"name\": \"some-name2\",\n\t\t\t\"host\": \"some-host2\"\n\t\t}\n\t],\n\t\"team\": \"some team\",\n\t\"name\": \"my-instance\",\n\t\"description\": \"some description\",\n\t\"tags\": [\n\t\t\"tag1\",\n\t\t\"tag2\",\n\t\t\"tag3\"\n\t],\n\t\"annotations\": [\n\t\t\"key1=val1\",\n\t\t\"key2=val2\",\n\t\t\"key3=val3\"\n\t],\n\t\"shutdown\": true\n}\n", }, } diff --git a/internal/pkg/rpaas/k8s.go b/internal/pkg/rpaas/k8s.go index b730ec99..64d5afa0 100644 --- a/internal/pkg/rpaas/k8s.go +++ b/internal/pkg/rpaas/k8s.go @@ -19,6 +19,7 @@ import ( "net" "net/url" "regexp" + "slices" "sort" "strings" "text/template" @@ -1642,6 +1643,17 @@ func setLoadBalancerName(instance *v1alpha1.RpaasInstance, lbName string) { instance.Spec.Service.Annotations[lbNameLabelKey] = lbName } +func filterAnnotations(annotations map[string]string) []string { + var filterAnnotations []string + for key, val := range annotations { + if !strings.HasPrefix(key, defaultKeyLabelPrefix) { + filterAnnotations = append(filterAnnotations, fmt.Sprintf("%s=%s", key, val)) + } + } + slices.Sort(filterAnnotations) + return filterAnnotations +} + func (m *k8sRpaasManager) GetInstanceInfo(ctx context.Context, instanceName string) (*clientTypes.InstanceInfo, error) { instance, err := m.GetInstance(ctx, instanceName) if err != nil { @@ -1656,6 +1668,7 @@ func (m *k8sRpaasManager) GetInstanceInfo(ctx context.Context, instanceName stri Description: instance.Annotations[labelKey("description")], Team: instance.Annotations[labelKey("team-owner")], Tags: strings.Split(instance.Annotations[labelKey("tags")], ","), + Annotations: filterAnnotations(instance.Annotations), Replicas: instance.Spec.Replicas, Plan: instance.Spec.PlanName, Binds: instance.Spec.Binds, diff --git a/internal/pkg/rpaas/k8s_test.go b/internal/pkg/rpaas/k8s_test.go index 0b4f4c33..72ad2577 100644 --- a/internal/pkg/rpaas/k8s_test.go +++ b/internal/pkg/rpaas/k8s_test.go @@ -3194,7 +3194,7 @@ func Test_k8sRpaasManager_CreateInstance(t *testing.T) { }, { name: "with custom annotations only set allowed ones", - args: CreateArgs{Name: "r1", Team: "t1", Parameters: map[string]interface{}{"annotations": "{\"my-custom-annotation\": \"my-value\"}"}}, + args: CreateArgs{Name: "r1", Team: "t1", Parameters: map[string]interface{}{"annotations": "{\"my-custom-annotation\": \"my-value\",\"my-custom-annotation2\": \"my-value2\"}"}}, extraConfig: config.RpaasConfig{ForbiddenAnnotationsPrefixes: []string{"rpaas.extensions.tsuru.io"}}, expected: v1alpha1.RpaasInstance{ TypeMeta: metav1.TypeMeta{ @@ -3210,6 +3210,7 @@ func Test_k8sRpaasManager_CreateInstance(t *testing.T) { "rpaas.extensions.tsuru.io/description": "", "rpaas.extensions.tsuru.io/team-owner": "t1", "my-custom-annotation": "my-value", + "my-custom-annotation2": "my-value2", }, Labels: map[string]string{ "rpaas.extensions.tsuru.io/service-name": "rpaasv2", @@ -4561,6 +4562,9 @@ func Test_k8sRpaasManager_GetInstanceInfo(t *testing.T) { Namespace: "rpaasv2", UID: types.UID("my-instance"), Annotations: map[string]string{ + "key1": "val1", + "key2": "val2", + "key3": "val3", "rpaas.extensions.tsuru.io/description": "Some description about this instance", "rpaas.extensions.tsuru.io/tags": "tag1,tag2,tag3", "rpaas.extensions.tsuru.io/team-owner": "tsuru", @@ -4600,6 +4604,7 @@ func Test_k8sRpaasManager_GetInstanceInfo(t *testing.T) { Description: "Some description about this instance", Team: "tsuru", Tags: []string{"tag1", "tag2", "tag3"}, + Annotations: []string{"key1=val1", "key2=val2", "key3=val3"}, Plan: "huge", Flavors: []string{"mango", "milk"}, }) diff --git a/pkg/rpaas/client/types/types.go b/pkg/rpaas/client/types/types.go index b5e3d240..3e843ac2 100644 --- a/pkg/rpaas/client/types/types.go +++ b/pkg/rpaas/client/types/types.go @@ -143,6 +143,7 @@ type InstanceInfo struct { Cluster string `json:"cluster,omitempty"` // for multi-cluster environments Pool string `json:"pool,omitempty"` // for multi-cluster environments Tags []string `json:"tags,omitempty"` + Annotations []string `json:"annotations,omitempty" ` Pods []Pod `json:"pods,omitempty"` Flavors []string `json:"flavors,omitempty"` Certificates []CertificateInfo `json:"certificates,omitempty"`