Skip to content

Commit

Permalink
feat: Display annotations using rpaasv2 info command (#152)
Browse files Browse the repository at this point in the history
* feat: Display annotations using rpaasv2 info command

* filtering custom annotations
  • Loading branch information
gvicentin authored May 16, 2024
1 parent f69ce91 commit cb9c21d
Show file tree
Hide file tree
Showing 5 changed files with 29 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
13 changes: 13 additions & 0 deletions internal/pkg/rpaas/k8s.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import (
"net"
"net/url"
"regexp"
"slices"
"sort"
"strings"
"text/template"
Expand Down Expand Up @@ -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 {
Expand All @@ -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,
Expand Down
7 changes: 6 additions & 1 deletion internal/pkg/rpaas/k8s_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{
Expand All @@ -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",
Expand Down Expand Up @@ -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",
Expand Down Expand Up @@ -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"},
})
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 cb9c21d

Please sign in to comment.