Skip to content

Instantly share code, notes, and snippets.

@witalisoft

witalisoft/main.go

Last active Aug 30, 2020
Embed
What would you like to do?
create custom node condition
package main
import (
"encoding/json"
"fmt"
"os"
"path/filepath"
"github.com/prometheus/common/log"
v1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/types"
"k8s.io/client-go/kubernetes"
"k8s.io/client-go/tools/clientcmd"
)
func newK8SClient() (*kubernetes.Clientset, error) {
userHomeDir, err := os.UserHomeDir()
if err != nil {
userHomeDir = ""
}
kubeConfig := filepath.Join(userHomeDir, ".kube", "config")
k8sConfig, err := clientcmd.BuildConfigFromFlags("", kubeConfig)
if err != nil {
log.Fatalf("cannot build k8s config from file %s, err: %v", kubeConfig, err)
}
return kubernetes.NewForConfig(k8sConfig)
}
func main() {
k8sClient, err := newK8SClient()
if err != nil {
log.Fatalf("cannot create k8s clientset, err: %v", err)
}
var samplecondition = []v1.NodeCondition{
{
Type: v1.NodeConditionType("SampleCondition"),
Status: v1.ConditionStatus(v1.ConditionTrue),
LastHeartbeatTime: metav1.Now(),
LastTransitionTime: metav1.Now(),
Reason: "Sample Reason",
Message: "Sample Messsage",
},
}
raw, err := json.Marshal(&samplecondition)
patch := []byte(fmt.Sprintf(`{"status":{"conditions":%s}}`, raw))
nodename := "kind-control-plane"
err = k8sClient.RESTClient().Patch(types.StrategicMergePatchType).RequestURI("/api/v1/nodes/" + nodename + "/status").Body(patch).Do().Error()
if err != nil {
log.Fatalf("cannot set node condition, err: %v", err)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.