Skip to content

Commit

Permalink
Respect existing eniConfig label if set on node. (#1596)
Browse files Browse the repository at this point in the history
* fix: use existing eni label if set

Signed-off-by: Jonah Back <jonah@jonahback.com>

* format code

* change approach to use new external label for managing eniConfig out of band
  • Loading branch information
backjo authored Oct 27, 2021
1 parent 2b91029 commit ad55677
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 4 deletions.
16 changes: 12 additions & 4 deletions pkg/eniconfig/eniconfig.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,11 @@ const (
defaultEniConfigAnnotationDef = "k8s.amazonaws.com/eniConfig"
defaultEniConfigLabelDef = "k8s.amazonaws.com/eniConfig"
eniConfigDefault = "default"
eniConfigLabel = "vpc.amazonaws.com/eniConfig"

// when this is defined, it is to be treated as the source of truth for the eniconfig.
// it is meant to be used for out-of-band mananagement of the eniConfig - i.e. on the kubelet or elsewhere
externalEniConfigLabel = "vpc.amazonaws.com/externalEniConfig"

// when "ENI_CONFIG_LABEL_DEF is defined, ENIConfigController will use that label key to
// search if is setting value for eniConfigLabelDef
Expand Down Expand Up @@ -123,12 +128,15 @@ func GetNodeSpecificENIConfigName(ctx context.Context, k8sClient client.Client)
return eniConfigName, err
}

//Derive ENIConfig Name from either Node Annotations or Labels
val, ok := node.GetAnnotations()[getEniConfigAnnotationDef()]
//Derive ENIConfig Name from either externally managed label, Node Annotations or Labels
val, ok := node.GetLabels()[externalEniConfigLabel]
if !ok {
val, ok = node.GetLabels()[getEniConfigLabelDef()]
val, ok = node.GetAnnotations()[getEniConfigAnnotationDef()]
if !ok {
val = eniConfigDefault
val, ok = node.GetLabels()[getEniConfigLabelDef()]
if !ok {
val = eniConfigDefault
}
}
}

Expand Down
24 changes: 24 additions & 0 deletions pkg/eniconfig/eniconfig_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,16 @@ func TestMyENIConfig(t *testing.T) {
},
}

testENIConfigCustom := &v1alpha1.ENIConfig{
ObjectMeta: metav1.ObjectMeta{
Name: "custom",
},
Spec: v1alpha1.ENIConfigSpec{
SecurityGroups: []string{"SG1"},
Subnet: "SB1",
},
}

type env struct {
nodes []*corev1.Node
eniconfigs []*v1alpha1.ENIConfig
Expand Down Expand Up @@ -167,6 +177,20 @@ func TestMyENIConfig(t *testing.T) {
want: &testENIConfigAZ1.Spec,
wantErr: nil,
},
{
name: "Matching ENIConfig available - Using external label",
env: env{
nodes: []*corev1.Node{testNode},
eniconfigs: []*v1alpha1.ENIConfig{testENIConfigAZ1, testENIConfigCustom},
Labels: map[string]string{
"vpc.amazonaws.com/externalEniConfig": "custom",
"failure-domain.beta.kubernetes.io/zone": "az2",
},
eniConfigLabelKey: "failure-domain.beta.kubernetes.io/zone",
},
want: &testENIConfigCustom.Spec,
wantErr: nil,
},
{
name: "No Matching ENIConfig available - Using Custom Label Key exposed via ENI_CONFIG_ANNOTATION_DEF",
env: env{
Expand Down

0 comments on commit ad55677

Please sign in to comment.