Created
March 28, 2020 05:47
-
-
Save jacobstr/2139040e64dc8accfaed0ca87d5c9bf1 to your computer and use it in GitHub Desktop.
Simple client to set kubernetes node conditions.
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 ( | |
"context" | |
"encoding/json" | |
"fmt" | |
"os" | |
"path/filepath" | |
"time" | |
_ "k8s.io/client-go/plugin/pkg/client/auth/gcp" | |
"gopkg.in/alecthomas/kingpin.v2" | |
core "k8s.io/api/core/v1" | |
meta "k8s.io/apimachinery/pkg/apis/meta/v1" | |
client "k8s.io/client-go/kubernetes" | |
"k8s.io/client-go/rest" | |
"k8s.io/client-go/tools/clientcmd" | |
"k8s.io/client-go/tools/clientcmd/api" | |
) | |
var ( | |
app = kingpin.New(filepath.Base(os.Args[0]), "Sets an arbitary node-condition on a given node.").DefaultEnvars() | |
kubecfg = app.Flag("kubeconfig", "Kubeconfig location.").String() | |
nodeName = app.Flag("node-name", "Name of the node to affect.").Required().String() | |
conditionName = app.Flag("condition-name", "Name of the condition to modify.").String() | |
conditionStatus = app.Flag("condition-status", "Condition status, should be one of True/False/Unknown.").String() | |
conditionUnset = app.Flag("unset", "Unset the condition instead of modifying it.").Short('u').Bool() | |
) | |
func main() { | |
kingpin.MustParse(app.Parse(os.Args[1:])) | |
c, err := BuildConfigFromFlags("", *kubecfg) | |
kingpin.FatalIfError(err, "cannot create Kubernetes client configuration") | |
cs, err := client.NewForConfig(c) | |
kingpin.FatalIfError(err, "cannot create Kubernetes client") | |
node, err := cs.CoreV1().Nodes().Get(context.TODO(), *nodeName, meta.GetOptions{}) | |
kingpin.FatalIfError(err, "cannot get node by the name provided") | |
statusToSet := core.ConditionUnknown | |
switch *conditionStatus { | |
case "True": | |
statusToSet = core.ConditionTrue | |
case "False": | |
statusToSet = core.ConditionFalse | |
case "Unknown": | |
statusToSet = core.ConditionUnknown | |
default: | |
kingpin.Fatalf("Invalid condition value, use True, False or Unknown.") | |
} | |
node.Status.Conditions = []core.NodeCondition{ | |
core.NodeCondition{ | |
Type: core.NodeConditionType(*conditionName), | |
Status: statusToSet, | |
LastHeartbeatTime: meta.Time{time.Now()}, | |
LastTransitionTime: meta.Time{time.Now()}, | |
Reason: "NodeConditioner", | |
Message: "Condition set via CLI.", | |
}, | |
} | |
node, err = cs.CoreV1().Nodes().UpdateStatus(context.TODO(), node, meta.UpdateOptions{}) | |
if err != nil { | |
kingpin.FatalIfError(err, "Could not update node status.") | |
} | |
bytes, err := json.Marshal(node) | |
if err != nil { | |
kingpin.FatalIfError(err, "Could not marshal returned node.") | |
} | |
fmt.Println("Node condition modified successfully:") | |
fmt.Println(string(bytes)) | |
} | |
// BuildConfigFromFlags is clientcmd.BuildConfigFromFlags with no annoying | |
// dependencies on glog. | |
// https://godoc.org/k8s.io/client-go/tools/clientcmd#BuildConfigFromFlags | |
func BuildConfigFromFlags(apiserver, kubecfg string) (*rest.Config, error) { | |
if kubecfg != "" || apiserver != "" { | |
return clientcmd.NewNonInteractiveDeferredLoadingClientConfig( | |
&clientcmd.ClientConfigLoadingRules{ExplicitPath: kubecfg}, | |
&clientcmd.ConfigOverrides{ClusterInfo: api.Cluster{Server: apiserver}}).ClientConfig() | |
} | |
return rest.InClusterConfig() | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment