Created
August 11, 2022 08:49
-
-
Save chendotjs/eeb25c89e7166db1f2fb5a803cc1c2ab to your computer and use it in GitHub Desktop.
This is a client-go example of https://kubernetes.io/docs/tasks/administer-cluster/extended-resource-node/
This file contains hidden or 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" | |
"flag" | |
"fmt" | |
"log" | |
"path/filepath" | |
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" | |
types "k8s.io/apimachinery/pkg/types" | |
"k8s.io/client-go/kubernetes" | |
"k8s.io/client-go/tools/clientcmd" | |
"k8s.io/client-go/util/homedir" | |
"k8s.io/klog/v2" | |
) | |
// This is a client-go example of https://kubernetes.io/docs/tasks/administer-cluster/extended-resource-node/ | |
// Run `./patch-extended-resource -v 10 -n <nodeName>` | |
var ( | |
nodeName = "" | |
jsonPatch = `[ | |
{ | |
"op": "add", | |
"path": "/status/capacity/example.com~1dongle", | |
"value": "4" | |
} | |
]` | |
subresourceStatus = "status" | |
) | |
func main() { | |
var kubeconfig *string | |
if home := homedir.HomeDir(); home != "" { | |
kubeconfig = flag.String("kubeconfig", filepath.Join(home, ".kube", "config"), "(optional) absolute path to the kubeconfig file") | |
} else { | |
kubeconfig = flag.String("kubeconfig", "", "absolute path to the kubeconfig file") | |
} | |
flag.StringVar(&nodeName, "n", "kind-worker", "node name") | |
klog.InitFlags(nil) | |
flag.Parse() | |
// use the current context in kubeconfig | |
config, err := clientcmd.BuildConfigFromFlags("", *kubeconfig) | |
if err != nil { | |
panic(err.Error()) | |
} | |
// create the clientset | |
clientset, err := kubernetes.NewForConfig(config) | |
if err != nil { | |
log.Fatalln("create client", err.Error()) | |
} | |
// let's patch | |
node, err := clientset.CoreV1().Nodes().Patch(context.TODO(), nodeName, types.JSONPatchType, []byte(jsonPatch), metav1.PatchOptions{}, subresourceStatus) | |
if err != nil { | |
log.Fatalln("patch status", err.Error()) | |
} | |
// print result | |
fmt.Println("Capacity", node.Status.Capacity) | |
fmt.Println("Allocatable", node.Status.Allocatable) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment