1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51
| package main
import ( "context" "fmt" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" "k8s.io/apimachinery/pkg/types" "k8s.io/apimachinery/pkg/util/json" "kube-base3/lib/kubeConfig" "log" )
func main() { ctx := context.Background() client := kubeConfig.InitClient()
mydep, err := client.AppsV1().Deployments("default").Get(ctx, "myapp-deploy", metav1.GetOptions{}) if err != nil { log.Fatal(err) } frontPost := map[string]interface{}{ "spec": map[string]interface{}{ "template": map[string]interface{}{ "spec": map[string]interface{}{ "containers": []map[string]interface{}{ { "$patch": "delete", "name": "redis", }, }, }, }, }, } b, err := json.Marshal(frontPost) if err != nil { log.Fatal(err) } _, err = client.AppsV1().Deployments(mydep.Namespace).Patch(ctx, mydep.Name, types.StrategicMergePatchType, b, metav1.PatchOptions{}) if err != nil { log.Fatal(err) } fmt.Println("patch success") }
|