// annotation node
func AnnotaClusterNodes(nodeName string, annotationKey string, annotationNewValue string) error {
	node, err := K8sClientSet.CoreV1().Nodes().Get(nodeName, metav1.GetOptions{})
	if err != nil {
		return err
	}
	annotations := node.Annotations
	annotations[annotationKey] = annotationNewValue

	patchData := map[string]interface{}{"metadata": map[string]map[string]string{"annotations": annotations}}
	playLoadBytes, _ := json.Marshal(patchData)

	_, err = K8sClientSet.CoreV1().Nodes().Patch(nodeName, types.StrategicMergePatchType, playLoadBytes)
	if err != nil {
		return err
	}
	logrus.Printf("annotation node [%s]: %s=%s \n", nodeName, annotationKey, annotationNewValue)
	return nil
}

//label node
func UpdateNodeLabels(nodeName string, labelKey string, labelNewValue string) error {
	node, err := K8sClientSet.CoreV1().Nodes().Get(nodeName, metav1.GetOptions{})
	if err != nil {
		return err
	}
	labels := node.Labels
	labels[labelKey] = labelNewValue

	patchData := map[string]interface{}{"metadata": map[string]map[string]string{"labels": labels}}
	playLoadBytes, _ := json.Marshal(patchData)

	_, err = K8sClientSet.CoreV1().Nodes().Patch(nodeName, types.StrategicMergePatchType, playLoadBytes)
	if err != nil {
		return err
	}
	log.Printf("> label node [%s]: %s=%s \n", nodeName, labelKey, labelNewValue)
	return nil
}

func DeleteNamespaceLabels(name string, labelKey string) error {
	log.Printf("> del ns label [%s]: %s- \n", name, labelKey)
	payloads := []RemoveStringValue{
		{
			Op:   "remove",
			Path: "/metadata/labels/" + labelKey,
		},
	}

	playLoadBytes, _ := json.Marshal(payloads)

	_, err := K8sClientSet.CoreV1().Namespaces().Patch(name, types.JSONPatchType, playLoadBytes)
	if err != nil {
		return err
	}

	return nil
}

Logo

K8S/Kubernetes社区为您提供最前沿的新闻资讯和知识内容

更多推荐