Skip to content
This repository has been archived by the owner on Oct 15, 2020. It is now read-only.

Commit

Permalink
Refactor Taints from Struct feild to method
Browse files Browse the repository at this point in the history
  • Loading branch information
errm committed Nov 20, 2018
1 parent c6e9fcf commit ba21b2c
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 18 deletions.
12 changes: 5 additions & 7 deletions pkg/node/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@ type Node struct {
ReservedMemory string
ClusterDNS string
Region string
Taints []string
}

type metadataClient interface {
Expand Down Expand Up @@ -68,7 +67,6 @@ func New(e ec2iface.EC2API, m metadataClient, region *string) (*Node, error) {
ReservedMemory: reservedMemory(instance.InstanceType),
ClusterDNS: clusterDNS(instance.PrivateIpAddress),
Region: *region,
Taints: taints(instance.Tags),
}
if node.ClusterName() == "" {
sleepFor := b.Duration(tries)
Expand Down Expand Up @@ -124,15 +122,15 @@ func (n *Node) Spot() bool {
return false
}

func taints(tags []*ec2.Tag) []string {
var ts []string
func (n *Node) Taints() []string {
var taints []string
re := regexp.MustCompile(`k8s.io\/cluster-autoscaler\/node-template\/taint\/(.*)`)
for _, t := range tags {
for _, t := range n.Tags {
if matches := re.FindStringSubmatch(*t.Key); len(matches) == 2 {
ts = append(ts, matches[1]+"="+*t.Value)
taints = append(taints, matches[1]+"="+*t.Value)
}
}
return ts
return taints
}

func instanceID(m metadataClient) (*string, error) {
Expand Down
4 changes: 2 additions & 2 deletions pkg/node/node_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -146,8 +146,8 @@ func TestNodeTaints(t *testing.T) {
"dedicated=foo:NoSchedule",
}

if !reflect.DeepEqual(node.Taints, expected) {
t.Errorf("Expected node.Taints to be %v but was %v", expected, node.Taints)
if !reflect.DeepEqual(node.Taints(), expected) {
t.Errorf("Expected node.Taints to be %v but was %v", expected, node.Taints())
}
}

Expand Down
17 changes: 8 additions & 9 deletions pkg/system/system_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ func TestConfigure(t *testing.T) {
hn := &FakeHostname{}
init := &FakeInit{}

i := instance("10.6.28.199", "ip-10-6-28-199.us-west-2.compute.internal", 18, "60m", "960Mi", map[string]string{}, []string{}, false)
i := instance("10.6.28.199", "ip-10-6-28-199.us-west-2.compute.internal", 18, "60m", "960Mi", map[string]string{}, false)
c := cluster(
"aws-om-cluster",
"https://74770F6B05F7A8FB0F02CFB5F7AF530C.yl4.us-west-2.eks.amazonaws.com",
Expand Down Expand Up @@ -156,7 +156,7 @@ func TestConfigureNoReserved(t *testing.T) {
hn := &FakeHostname{}
init := &FakeInit{}

i := instance("10.6.28.199", "ip-10-6-28-199.us-west-2.compute.internal", 18, "", "", map[string]string{}, []string{}, false)
i := instance("10.6.28.199", "ip-10-6-28-199.us-west-2.compute.internal", 18, "", "", map[string]string{}, false)
c := cluster(
"aws-om-cluster",
"https://74770F6B05F7A8FB0F02CFB5F7AF530C.yl4.us-west-2.eks.amazonaws.com",
Expand All @@ -182,7 +182,7 @@ func TestConfigureSpotInstanceLabels(t *testing.T) {
"node-role.kubernetes.io/worker": "true",
}

i := instance("10.6.28.199", "ip-10-6-28-199.us-west-2.compute.internal", 18, "", "", labels, []string{}, true)
i := instance("10.6.28.199", "ip-10-6-28-199.us-west-2.compute.internal", 18, "", "", labels, true)
c := cluster(
"aws-om-cluster",
"https://74770F6B05F7A8FB0F02CFB5F7AF530C.yl4.us-west-2.eks.amazonaws.com",
Expand Down Expand Up @@ -210,7 +210,7 @@ func TestConfigureLabels(t *testing.T) {
"k8s.io/cluster-autoscaler/node-template/label/gpu-type": "K80",
}

i := instance("10.6.28.199", "ip-10-6-28-199.us-west-2.compute.internal", 18, "", "", tags, []string{}, false)
i := instance("10.6.28.199", "ip-10-6-28-199.us-west-2.compute.internal", 18, "", "", tags, false)
c := cluster(
"aws-om-cluster",
"https://74770F6B05F7A8FB0F02CFB5F7AF530C.yl4.us-west-2.eks.amazonaws.com",
Expand All @@ -234,11 +234,11 @@ func TestConfigureTaints(t *testing.T) {
hn := &FakeHostname{}
init := &FakeInit{}

taints := []string{
"node-role.kubernetes.io/worker=true:PreferNoSchedule",
tags := map[string]string{
"k8s.io/cluster-autoscaler/node-template/taint/node-role.kubernetes.io/worker": "true:PreferNoSchedule",
}

i := instance("10.6.28.199", "ip-10-6-28-199.us-west-2.compute.internal", 18, "", "", map[string]string{}, taints, false)
i := instance("10.6.28.199", "ip-10-6-28-199.us-west-2.compute.internal", 18, "", "", tags, false)
c := cluster(
"aws-om-cluster",
"https://74770F6B05F7A8FB0F02CFB5F7AF530C.yl4.us-west-2.eks.amazonaws.com",
Expand All @@ -257,7 +257,7 @@ Environment='KUBELET_NODE_TAINTS=--register-with-taints="node-role.kubernetes.io
fs.Check(t, "/etc/systemd/system/kubelet.service.d/50-taints.conf", expected, 0640)
}

func instance(ip, dnsName string, maxPods int, reservedCPU, reservedMemory string, tags map[string]string, taints []string, spot bool) *node.Node {
func instance(ip, dnsName string, maxPods int, reservedCPU, reservedMemory string, tags map[string]string, spot bool) *node.Node {
var ec2tags []*ec2.Tag
for key, value := range tags {
ec2tags = append(ec2tags, &ec2.Tag{
Expand All @@ -282,7 +282,6 @@ func instance(ip, dnsName string, maxPods int, reservedCPU, reservedMemory strin
Region: "us-east-1",
ReservedCPU: reservedCPU,
ReservedMemory: reservedMemory,
Taints: taints,
}
}

Expand Down

0 comments on commit ba21b2c

Please sign in to comment.