Last active
August 30, 2020 10:00
-
-
Save witalisoft/ea41f975ed6de3a04df350eff3853a4c to your computer and use it in GitHub Desktop.
create custom node condition
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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