Skip to content

Commit

Permalink
Merge pull request #68 from bgzzz/annotation-fix
Browse files Browse the repository at this point in the history
Annotation to tagging fix
  • Loading branch information
Søren Mathiasen authored Dec 17, 2019
2 parents 98958dd + 1631071 commit e434241
Show file tree
Hide file tree
Showing 2 changed files with 110 additions and 3 deletions.
33 changes: 30 additions & 3 deletions rds/rds_provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"fmt"
"log"
"regexp"
"strings"
"time"

Expand Down Expand Up @@ -201,15 +202,41 @@ func (r *RDS) rdsclient() *rds.Client {
func dbidentifier(v *crd.Database) string {
return v.Name + "-" + v.Namespace
}
func convertSpecToInput(v *crd.Database, subnetName string, securityGroups []string, password string) *rds.CreateDBInstanceInput {

const (
maxTagLengthAllowed = 255
tagRegexp = `^kube.*$`
)

func toTags(annotations, labels map[string]string) []rds.Tag {
tags := []rds.Tag{}
for k, v := range v.Annotations {
r := regexp.MustCompile(tagRegexp)

for k, v := range annotations {
if len(k) > maxTagLengthAllowed || len(v) > maxTagLengthAllowed ||
r.Match([]byte(k)) {
log.Printf("WARNING: Not Adding annotation KV to tags: %v %v", k, v)
continue
}

tags = append(tags, rds.Tag{Key: aws.String(k), Value: aws.String(v)})
}
for k, v := range v.Labels {
for k, v := range labels {
if len(k) > maxTagLengthAllowed || len(v) > maxTagLengthAllowed {
log.Printf("WARNING: Not Adding CRD labels KV to tags: %v %v", k, v)
continue
}

tags = append(tags, rds.Tag{Key: aws.String(k), Value: aws.String(v)})
}

return tags
}

func convertSpecToInput(v *crd.Database, subnetName string, securityGroups []string, password string) *rds.CreateDBInstanceInput {

tags := toTags(v.Annotations, v.Labels)

input := &rds.CreateDBInstanceInput{
DBName: aws.String(v.Spec.DBName),
AllocatedStorage: aws.Int64(v.Spec.Size),
Expand Down
80 changes: 80 additions & 0 deletions rds/rds_provider_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,3 +44,83 @@ func TestgetIDFromProvider(t *testing.T) {
x := getIDFromProvider("aws:///eu-west-1a/i-02ab67f4da79c3caa")
assert.Equal(t, "i-02ab67f4da79c3caa", x)
}

// 270 characters length
const testRandString = "banjmdvgeezuadqvehvqaxxmzwykirejkwvktkxmvjdevcfhqootqyfdfvqatjiebglktdswnvzxcpnstvrurpfjfuxhsjvgogrnhazjizakttdncmjnbofvwcsccigfcyxzlunfndcjteuqmjpslqvefvobfnejjxtwbyrkcvsvqokkrskrryzbhhayegyuwhugyorkltmsipvznxkonqzzwihjdejqgzfjivjdqmieidkowryfjnnyrxszsyhnpfeepxyoliskexxpjtxn"

func TestToTags(t *testing.T) {
tests := []struct {
Annotations map[string]string
Labels map[string]string
Result map[string]string
}{
{
Annotations: map[string]string{
"annotation-key1-test": "test-value",
"annotation-key2-test": "test-value",
},
Labels: map[string]string{
"label-key1-test": "test-value",
"label-key2-test": "test-value",
},
Result: map[string]string{
"annotation-key1-test": "test-value",
"annotation-key2-test": "test-value",
"label-key1-test": "test-value",
"label-key2-test": "test-value",
},
},

{
Annotations: map[string]string{
"annotation-key1-test": "test-value",
"kubectl-key2-test": "test-value",
},
Labels: map[string]string{
"kubectl-key1-test": "test-value",
"label-key2-test": "test-value",
},
Result: map[string]string{
"annotation-key1-test": "test-value",
"label-key2-test": "test-value",
"kubectl-key1-test": "test-value",
},
},

{
Annotations: map[string]string{
"annotation-key1-test": testRandString,
"kubectl-key2-test": "test-value",
},
Labels: map[string]string{
"kubectl-key1-test": "test-value",
"label-key2-test": testRandString,
"label-key3-test": "test",
},
Result: map[string]string{
"label-key3-test": "test",
"kubectl-key1-test": "test-value",
},
},
}

for testInd, test := range tests {

tags := toTags(test.Annotations, test.Labels)

if len(tags) != len(test.Result) {
t.Fatalf("Not desired result %v != %v len(%v, %v): %v", tags, test.Result,
len(tags), len(test.Result), testInd)
}

for _, tag := range tags {
_, ok := test.Result[*tag.Key]
if !ok {
t.Fatalf("Not desired result in per component comparison %v != %v key %v: %v",
tags, test.Result, *tag.Key, testInd)
}

}

}
}

0 comments on commit e434241

Please sign in to comment.