func generateNodeAndTaintedNode(oldTaints []api.Taint, newTaints []api.Taint) (*api.Node, *api.Node) { var taintedNode *api.Node oldTaintsData, _ := json.Marshal(oldTaints) // Create a node. node := &api.Node{ ObjectMeta: api.ObjectMeta{ Name: "node-name", CreationTimestamp: unversioned.Time{Time: time.Now()}, Annotations: map[string]string{ api.TaintsAnnotationKey: string(oldTaintsData), }, }, Spec: api.NodeSpec{ ExternalID: "node-name", }, Status: api.NodeStatus{}, } clone, _ := conversion.NewCloner().DeepCopy(node) newTaintsData, _ := json.Marshal(newTaints) // A copy of the same node, but tainted. taintedNode = clone.(*api.Node) taintedNode.Annotations = map[string]string{ api.TaintsAnnotationKey: string(newTaintsData), } return node, taintedNode }
func addTaint(client *kube_client.Client, node *kube_api.Node, value string) error { taints, err := kube_api.GetTaintsFromNodeAnnotations(node.Annotations) if err != nil { return err } taint := kube_api.Taint{ Key: criticalAddonsOnlyTaintKey, Value: value, Effect: kube_api.TaintEffectNoSchedule, } taints = append(taints, taint) taintsJson, err := json.Marshal(taints) if err != nil { return err } if node.Annotations == nil { node.Annotations = make(map[string]string) } node.Annotations[kube_api.TaintsAnnotationKey] = string(taintsJson) _, err = client.Nodes().Update(node) if err != nil { return err } return nil }
// reconcileCMADAnnotationWithExistingNode reconciles the controller-managed // attach-detach annotation on a new node and the existing node, returning // whether the existing node must be updated. func (kl *Kubelet) reconcileCMADAnnotationWithExistingNode(node, existingNode *api.Node) bool { var ( existingCMAAnnotation = existingNode.Annotations[volumehelper.ControllerManagedAttachAnnotation] newCMAAnnotation, newSet = node.Annotations[volumehelper.ControllerManagedAttachAnnotation] ) if newCMAAnnotation == existingCMAAnnotation { return false } // If the just-constructed node and the existing node do // not have the same value, update the existing node with // the correct value of the annotation. if !newSet { glog.Info("Controller attach-detach setting changed to false; updating existing Node") delete(existingNode.Annotations, volumehelper.ControllerManagedAttachAnnotation) } else { glog.Info("Controller attach-detach setting changed to true; updating existing Node") if existingNode.Annotations == nil { existingNode.Annotations = make(map[string]string) } existingNode.Annotations[volumehelper.ControllerManagedAttachAnnotation] = newCMAAnnotation } return true }
// Create creates a new node api object with the given hostname, // slave attribute labels and annotations func Create( client *client.Client, hostName string, slaveAttrLabels, annotations map[string]string, ) (*api.Node, error) { n := api.Node{ ObjectMeta: api.ObjectMeta{ Name: hostName, }, Spec: api.NodeSpec{ ExternalID: hostName, }, Status: api.NodeStatus{ Phase: api.NodePending, }, } n.Labels = mergeMaps( map[string]string{"kubernetes.io/hostname": hostName}, slaveAttrLabels, ) n.Annotations = annotations // try to create return client.Nodes().Create(&n) }
// Create creates a new node api object with the given hostname, // slave attribute labels and annotations func Create( client unversionedcore.NodesGetter, hostName string, slaveAttrLabels, annotations map[string]string, ) (*api.Node, error) { n := api.Node{ ObjectMeta: api.ObjectMeta{ Name: hostName, }, Spec: api.NodeSpec{ ExternalID: hostName, }, Status: api.NodeStatus{ Phase: api.NodePending, // WORKAROUND(sttts): make sure that the Ready condition is the // first one. The kube-ui v3 depends on this assumption. // TODO(sttts): remove this workaround when kube-ui v4 is used or we // merge this with the statusupdate in the controller manager. Conditions: []api.NodeCondition{ { Type: api.NodeReady, Status: api.ConditionTrue, Reason: slaveReadyReason, Message: slaveReadyMessage, LastHeartbeatTime: unversioned.Now(), }, }, }, } n.Labels = mergeMaps( map[string]string{"kubernetes.io/hostname": hostName}, slaveAttrLabels, ) n.Annotations = annotations // try to create return client.Nodes().Create(&n) }