Skip to content

Commit

Permalink
feat: Display annotations using rpaasv2 info command
Browse files Browse the repository at this point in the history
  • Loading branch information
gvicentin committed May 16, 2024
1 parent f69ce91 commit e4e0b65
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 2 deletions.
1 change: 1 addition & 0 deletions cmd/plugin/rpaasv2/cmd/info.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ Dashboard: {{ .Dashboard }}
{{- end }}
Description: {{ .Description }}
Tags: {{ join ", " .Tags }}
Annotations: {{ join ", " .Annotations }}
Team owner: {{ .Team }}
Plan: {{ .Plan }}
Flavors: {{ join ", " .Flavors }}
Expand Down
9 changes: 8 additions & 1 deletion cmd/plugin/rpaasv2/cmd/info_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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{
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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{
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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",
},
}

Expand Down
9 changes: 9 additions & 0 deletions internal/pkg/rpaas/k8s.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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,
Expand Down
16 changes: 15 additions & 1 deletion internal/pkg/rpaas/k8s_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{
Expand Down Expand Up @@ -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{
Expand Down Expand Up @@ -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",
},
Expand Down Expand Up @@ -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",
},
Expand Down Expand Up @@ -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",
},
Expand Down Expand Up @@ -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{
Expand Down Expand Up @@ -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",
},
Expand Down Expand Up @@ -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",
},
Expand Down Expand Up @@ -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{
Expand Down Expand Up @@ -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{
Expand Down Expand Up @@ -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{
Expand All @@ -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",
Expand Down Expand Up @@ -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{
Expand Down Expand Up @@ -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"},
})
Expand Down
1 change: 1 addition & 0 deletions pkg/rpaas/client/types/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"`
Expand Down

0 comments on commit e4e0b65

Please sign in to comment.