...

Source file src/k8s.io/kubernetes/cmd/kubeadm/app/phases/markcontrolplane/markcontrolplane.go

Documentation: k8s.io/kubernetes/cmd/kubeadm/app/phases/markcontrolplane

     1  /*
     2  Copyright 2017 The Kubernetes Authors.
     3  
     4  Licensed under the Apache License, Version 2.0 (the "License");
     5  you may not use this file except in compliance with the License.
     6  You may obtain a copy of the License at
     7  
     8      http://www.apache.org/licenses/LICENSE-2.0
     9  
    10  Unless required by applicable law or agreed to in writing, software
    11  distributed under the License is distributed on an "AS IS" BASIS,
    12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  See the License for the specific language governing permissions and
    14  limitations under the License.
    15  */
    16  
    17  package markcontrolplane
    18  
    19  import (
    20  	"fmt"
    21  
    22  	v1 "k8s.io/api/core/v1"
    23  	clientset "k8s.io/client-go/kubernetes"
    24  
    25  	"k8s.io/kubernetes/cmd/kubeadm/app/constants"
    26  	"k8s.io/kubernetes/cmd/kubeadm/app/util/apiclient"
    27  )
    28  
    29  // labelsToAdd holds a list of labels that are applied on kubeadm managed control plane nodes
    30  var labelsToAdd = []string{
    31  	constants.LabelNodeRoleControlPlane,
    32  	constants.LabelExcludeFromExternalLB,
    33  }
    34  
    35  // MarkControlPlane taints the control-plane and sets the control-plane label
    36  func MarkControlPlane(client clientset.Interface, controlPlaneName string, taints []v1.Taint) error {
    37  	fmt.Printf("[mark-control-plane] Marking the node %s as control-plane by adding the labels: %v\n",
    38  		controlPlaneName, labelsToAdd)
    39  
    40  	if len(taints) > 0 {
    41  		taintStrs := []string{}
    42  		for _, taint := range taints {
    43  			taintStrs = append(taintStrs, taint.ToString())
    44  		}
    45  		fmt.Printf("[mark-control-plane] Marking the node %s as control-plane by adding the taints %v\n", controlPlaneName, taintStrs)
    46  	}
    47  
    48  	return apiclient.PatchNode(client, controlPlaneName, func(n *v1.Node) {
    49  		markControlPlaneNode(n, taints)
    50  	})
    51  }
    52  
    53  func taintExists(taint v1.Taint, taints []v1.Taint) bool {
    54  	for _, t := range taints {
    55  		if t == taint {
    56  			return true
    57  		}
    58  	}
    59  
    60  	return false
    61  }
    62  
    63  func markControlPlaneNode(n *v1.Node, taints []v1.Taint) {
    64  	for _, label := range labelsToAdd {
    65  		n.ObjectMeta.Labels[label] = ""
    66  	}
    67  
    68  	for _, nt := range n.Spec.Taints {
    69  		if !taintExists(nt, taints) {
    70  			taints = append(taints, nt)
    71  		}
    72  	}
    73  
    74  	n.Spec.Taints = taints
    75  }
    76  

View as plain text