Skip to content

Commit

Permalink
Added tests and readme for taint
Browse files Browse the repository at this point in the history
Signed-off-by: Gianluca Arbezzano <gianarb92@gmail.com>
  • Loading branch information
Gianluca Arbezzano committed May 12, 2019
1 parent 53a161f commit 545c9a3
Show file tree
Hide file tree
Showing 3 changed files with 130 additions and 3 deletions.
1 change: 0 additions & 1 deletion .goreleaser.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ dockers:
image_templates:
- "gianarb/kube-aws-node-labeler:latest"
- "gianarb/kube-aws-node-labeler:{{ .Tag }}"
- "gianarb/kube-aws-node-labeler:v{{ .Major }}"
skip_push: false
# Path to the Dockerfile (from the project root).
dockerfile: Dockerfile
16 changes: 14 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,27 @@ new node joins the cluster. If the node has the label
tags from AWS. Specifically it looks for instance tags like:

```
kubernetes/aws-labeler/label/<labelName>=<labelValue>
kubernetes/aws-labeler/label/type=ci
```
If the EC2 has this tag the Kubernetes node will have a label called

```
awslabeler.com/type=frontend
awslabeler.com/type=ci
```
That you can use to schedule your pods.

Other than labels it also support taints:

AWS EC2 tags like:
```
kubernetes/aws-labeler/taint/type=ci:NoExecute
```
Will become:

```
awslabeler.com/type=ci:NoExecute
```

## Build

```
Expand Down
116 changes: 116 additions & 0 deletions cmd/aws-node-labeler/main_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
package main

import (
"testing"

"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/service/ec2"
v1 "k8s.io/api/core/v1"
)

func TestInstanceIDFromProviderID(t *testing.T) {
r, err := InstanceIDFromProviderID("aws:///us-east-1c/i-0393ac1bb853a1fb5")
if err != nil {
t.Fatal(err)
}
if r != "i-0393ac1bb853a1fb5" {
t.Fatalf("expected i-0393ac1bb853a1fb5 got %s", r)
}
}

func TestEC2TagIsManagableByTheLabeler(t *testing.T) {
cases := []struct {
input string
output bool
}{
{"hello", false},
{"kubernetes/aws-labeler/label/type", true},
{"kubernetes/aws-labeler/tail/type", true},
}

for _, v := range cases {
t.Run("Input: "+v.input, func(t *testing.T) {
if isManagable(v.input) != v.output {
t.Errorf("different expectation for the instance tag %s", v.input)
}
})
}
}

func TestSplitOfTheEC2TagKey(t *testing.T) {
cases := []struct {
input string
expectedOutputLen int
output bool
}{
{"hello", 0, false},
{"kubernetes/aws-labeler", 0, false},
{"kubernetes/aws-labeler/label/type", 4, true},
{"kubernetes/aws-labeler/tail/type", 4, true},
{"kubernetes/aws-labeler/what/ever", 4, true},
}

for _, v := range cases {
t.Run("Input: "+v.input, func(t *testing.T) {
res, err := splitEC2Tag(v.input)
if err != nil && v.output {
t.Error(err.Error())
}
if err == nil && len(res) != v.expectedOutputLen {
t.Error("No error from the splitted function but the len is not what we expect")
}
})
}
}

func TestGetTaintFromEC2Tag(t *testing.T) {
cases := []struct {
input ec2.Tag
output v1.Taint
}{
{
ec2.Tag{
Key: aws.String("kubernetes/aws-labeler/tail/type"),
Value: aws.String("ci:NoSchedule"),
},
v1.Taint{
Key: "awslabeler.com/type",
Value: "ci",
Effect: v1.TaintEffectNoSchedule,
},
},
{
ec2.Tag{
Key: aws.String("kubernetes/aws-labeler/tail/type"),
Value: aws.String("frontline:blabla"),
},
v1.Taint{
Key: "awslabeler.com/type",
Value: "frontline",
Effect: v1.TaintEffectNoSchedule,
},
},
}

for _, v := range cases {
t.Run("Input: "+*v.input.Key+"="+*v.input.Value, func(t *testing.T) {
res, err := splitEC2Tag(*v.input.Key)
if err != nil {
t.Fatal(err)
}
taint, err := getTaintFromAWSTag(res, *v.input.Value)
if err != nil {
t.Fatal(err)
}
if taint.Value != v.output.Value {
t.Errorf("Expected taint value %s got %s", v.output.Value, taint.Value)
}
if taint.Value != v.output.Value {
t.Errorf("Expected taint effect %s got %s", v.output.Effect, taint.Effect)
}
if taint.Key != v.output.Key {
t.Errorf("Expected taint effect %s got %s", v.output.Key, taint.Key)
}
})
}
}

0 comments on commit 545c9a3

Please sign in to comment.