diff --git a/cmd/kops-controller/controllers/node_controller.go b/cmd/kops-controller/controllers/node_controller.go index 363242502f..a12fbaddaa 100644 --- a/cmd/kops-controller/controllers/node_controller.go +++ b/cmd/kops-controller/controllers/node_controller.go @@ -119,6 +119,15 @@ func (r *NodeReconciler) Reconcile(req ctrl.Request) (ctrl.Result, error) { return ctrl.Result{}, fmt.Errorf("unable to build config for node %s: %v", node.Name, err) } + lifecycle, err := r.getInstanceLifecycle(ctx, node) + if err != nil { + return ctrl.Result{}, fmt.Errorf("unable to get instance lifecycle %s: %v", node.Name, err) + } + + if len(lifecycle) > 0 { + labels[fmt.Sprintf("node-role.kubernetes.io/%s-worker", lifecycle)] = "true" + } + updateLabels := make(map[string]string) for k, v := range labels { actual, found := node.Labels[k] @@ -188,6 +197,17 @@ func (r *NodeReconciler) getClusterForNode(node *corev1.Node) (*kops.Cluster, er return cluster, nil } +// getInstanceLifecycle returns InstanceLifecycle string object +func (r *NodeReconciler) getInstanceLifecycle(ctx context.Context, node *corev1.Node) (string, error) { + + identity, err := r.identifier.IdentifyNode(ctx, node) + if err != nil { + return "", fmt.Errorf("error identifying node %q: %v", node.Name, err) + } + + return identity.InstanceLifecycle, nil +} + // getInstanceGroupForNode returns the kops.InstanceGroup object for the node func (r *NodeReconciler) getInstanceGroupForNode(ctx context.Context, node *corev1.Node) (*kops.InstanceGroup, error) { // We assume that if the instancegroup label is set, that it is correct diff --git a/pkg/nodeidentity/aws/identify.go b/pkg/nodeidentity/aws/identify.go index 5162d3dca6..177b365aff 100644 --- a/pkg/nodeidentity/aws/identify.go +++ b/pkg/nodeidentity/aws/identify.go @@ -102,6 +102,11 @@ func (i *nodeIdentifier) IdentifyNode(ctx context.Context, node *corev1.Node) (* return nil, fmt.Errorf("found instance %q, but state is %q", instanceID, instanceState) } + lifecycle := "" + if instance.InstanceLifecycle != nil { + lifecycle = *instance.InstanceLifecycle + } + // TODO: Should we traverse to the ASG to confirm the tags there? igName := getTag(instance.Tags, CloudTagInstanceGroupName) if igName == "" { @@ -110,6 +115,7 @@ func (i *nodeIdentifier) IdentifyNode(ctx context.Context, node *corev1.Node) (* info := &nodeidentity.Info{} info.InstanceGroup = igName + info.InstanceLifecycle = lifecycle return info, nil } diff --git a/pkg/nodeidentity/interfaces.go b/pkg/nodeidentity/interfaces.go index 0f4c70fceb..0578e057d2 100644 --- a/pkg/nodeidentity/interfaces.go +++ b/pkg/nodeidentity/interfaces.go @@ -27,5 +27,6 @@ type Identifier interface { } type Info struct { - InstanceGroup string + InstanceGroup string + InstanceLifecycle string }