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..b991a54c 100644 --- a/internal/pkg/rpaas/k8s.go +++ b/internal/pkg/rpaas/k8s.go @@ -1570,6 +1570,14 @@ func setAnnotations(instance *v1alpha1.RpaasInstance, annotations map[string]str } instance.Annotations = mergeMap(instance.Annotations, annotations) + + var annotationPairs []string + for key, val := range annotations { + annotationPairs = append(annotationPairs, fmt.Sprintf("%s=%s", key, val)) + } + instance.Annotations = mergeMap(instance.Annotations, map[string]string{ + labelKey("annotations"): strings.Join(annotationPairs, ","), + }) } func setDescription(instance *v1alpha1.RpaasInstance, description string) { @@ -1656,6 +1664,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: strings.Split(instance.Annotations[labelKey("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..ddd0dfac 100644 --- a/internal/pkg/rpaas/k8s_test.go +++ b/internal/pkg/rpaas/k8s_test.go @@ -2663,6 +2663,7 @@ func Test_k8sRpaasManager_CreateInstance(t *testing.T) { Annotations: map[string]string{ "rpaas.extensions.tsuru.io/description": "", "rpaas.extensions.tsuru.io/tags": "", + "rpaas.extensions.tsuru.io/annotations": "", "rpaas.extensions.tsuru.io/team-owner": "t1", }, Labels: map[string]string{ @@ -2713,6 +2714,7 @@ func Test_k8sRpaasManager_CreateInstance(t *testing.T) { Annotations: map[string]string{ "rpaas.extensions.tsuru.io/description": "", "rpaas.extensions.tsuru.io/tags": "", + "rpaas.extensions.tsuru.io/annotations": "", "rpaas.extensions.tsuru.io/team-owner": "t1", }, Labels: map[string]string{ @@ -2763,6 +2765,7 @@ func Test_k8sRpaasManager_CreateInstance(t *testing.T) { Annotations: map[string]string{ "rpaas.extensions.tsuru.io/description": "", "rpaas.extensions.tsuru.io/tags": "", + "rpaas.extensions.tsuru.io/annotations": "", "rpaas.extensions.tsuru.io/team-owner": "t1", "rpaas.extensions.tsuru.io/cluster-name": "cluster-01", }, @@ -2821,6 +2824,7 @@ func Test_k8sRpaasManager_CreateInstance(t *testing.T) { Annotations: map[string]string{ "rpaas.extensions.tsuru.io/description": "", "rpaas.extensions.tsuru.io/tags": "", + "rpaas.extensions.tsuru.io/annotations": "", "rpaas.extensions.tsuru.io/team-owner": "t1", "rpaas.extensions.tsuru.io/cluster-name": "cluster-01", }, @@ -2881,6 +2885,7 @@ func Test_k8sRpaasManager_CreateInstance(t *testing.T) { Annotations: map[string]string{ "rpaas.extensions.tsuru.io/description": "", "rpaas.extensions.tsuru.io/tags": "", + "rpaas.extensions.tsuru.io/annotations": "", "rpaas.extensions.tsuru.io/team-owner": "t1", "rpaas.extensions.tsuru.io/cluster-name": "cluster-01", }, @@ -2935,6 +2940,7 @@ func Test_k8sRpaasManager_CreateInstance(t *testing.T) { Annotations: map[string]string{ "rpaas.extensions.tsuru.io/description": "", "rpaas.extensions.tsuru.io/tags": `plan-override={"config": {"cacheEnabled": false}}`, + "rpaas.extensions.tsuru.io/annotations": "", "rpaas.extensions.tsuru.io/team-owner": "t1", }, Labels: map[string]string{ @@ -2988,6 +2994,7 @@ func Test_k8sRpaasManager_CreateInstance(t *testing.T) { ResourceVersion: "1", Annotations: map[string]string{ "rpaas.extensions.tsuru.io/tags": "", + "rpaas.extensions.tsuru.io/annotations": "", "rpaas.extensions.tsuru.io/description": "", "rpaas.extensions.tsuru.io/team-owner": "t1", }, @@ -3038,6 +3045,7 @@ func Test_k8sRpaasManager_CreateInstance(t *testing.T) { ResourceVersion: "1", Annotations: map[string]string{ "rpaas.extensions.tsuru.io/tags": "", + "rpaas.extensions.tsuru.io/annotations": "", "rpaas.extensions.tsuru.io/description": "", "rpaas.extensions.tsuru.io/team-owner": "t1", }, @@ -3089,6 +3097,7 @@ func Test_k8sRpaasManager_CreateInstance(t *testing.T) { Annotations: map[string]string{ "rpaas.extensions.tsuru.io/description": "", "rpaas.extensions.tsuru.io/tags": "", + "rpaas.extensions.tsuru.io/annotations": "", "rpaas.extensions.tsuru.io/team-owner": "team-one", }, Labels: map[string]string{ @@ -3155,6 +3164,7 @@ func Test_k8sRpaasManager_CreateInstance(t *testing.T) { Annotations: map[string]string{ "rpaas.extensions.tsuru.io/description": "", "rpaas.extensions.tsuru.io/tags": "", + "rpaas.extensions.tsuru.io/annotations": "", "rpaas.extensions.tsuru.io/team-owner": "t1", }, Labels: map[string]string{ @@ -3194,7 +3204,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{ @@ -3207,9 +3217,11 @@ func Test_k8sRpaasManager_CreateInstance(t *testing.T) { ResourceVersion: "1", Annotations: map[string]string{ "rpaas.extensions.tsuru.io/tags": "", + "rpaas.extensions.tsuru.io/annotations": "my-custom-annotation=my-value,my-custom-annotation2=my-value2", "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", @@ -4563,6 +4575,7 @@ func Test_k8sRpaasManager_GetInstanceInfo(t *testing.T) { Annotations: map[string]string{ "rpaas.extensions.tsuru.io/description": "Some description about this instance", "rpaas.extensions.tsuru.io/tags": "tag1,tag2,tag3", + "rpaas.extensions.tsuru.io/annotations": "key1=val1,key2=val2,key3=val3", "rpaas.extensions.tsuru.io/team-owner": "tsuru", }, Labels: map[string]string{ @@ -4600,6 +4613,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"`