更新k8s资源:patch,update
update方法:
创建和更新方法:
func Create() {
namespace := "wm"
restConfig, err := clientcmd.BuildConfigFromFlags("", "/home/wm/config")
if err != nil {
return
}
istioClient,err := versionedclient.NewForConfig(restConfig)
if err != nil {
return
}
var (
httpRouteList []*networkingv1alpha3.HTTPRoute
HTTPRouteDestinationList []*networkingv1alpha3.HTTPRouteDestination
)
HTTPRouteDestination := &networkingv1alpha3.HTTPRouteDestination{
Destination: &networkingv1alpha3.Destination{
Host: "reviews",
Subset: "v2",
},
Weight: 50,
}
HTTPRouteDestinationList = append(HTTPRouteDestinationList,HTTPRouteDestination)
httpRouteSign := networkingv1alpha3.HTTPRoute{
Route: HTTPRouteDestinationList,
}
httpRouteList = append(httpRouteList,&httpRouteSign)
virtualService := &v1alpha3.VirtualService{
ObjectMeta: v1.ObjectMeta{
Name: "vs-test",
Namespace: namespace,
},
Spec: networkingv1alpha3.VirtualService{
Hosts: []string{"reviews"},
Gateways: []string{"knative-serving/knative-ingress-gateway"},
Http: httpRouteList,
},
}
vs,err :=istioClient.NetworkingV1alpha3().VirtualServices(namespace).Create(context.TODO(),virtualService,v1.CreateOptions{})
if err != nil {
return
}
log.Print(vs)
}
func Update() {
namespace := "wm"
restConfig, err := clientcmd.BuildConfigFromFlags("", "/home/wm/config")
if err != nil {
return
}
istioClient,err := versionedclient.NewForConfig(restConfig)
if err != nil {
return
}
oldVs, err := istioClient.NetworkingV1alpha3().VirtualServices(namespace).Get(context.TODO(),"vs-test",v1.GetOptions{})
resourceVersion := oldVs.ObjectMeta.ResourceVersion
var (
httpRouteList []*networkingv1alpha3.HTTPRoute
HTTPRouteDestinationList []*networkingv1alpha3.HTTPRouteDestination
)
HTTPRouteDestination := &networkingv1alpha3.HTTPRouteDestination{
Destination: &networkingv1alpha3.Destination{
Host: "reviews",
Subset: "v3",
},
Weight: 51,
}
HTTPRouteDestinationList = append(HTTPRouteDestinationList,HTTPRouteDestination)
httpRouteSign := networkingv1alpha3.HTTPRoute{
Route: HTTPRouteDestinationList,
}
httpRouteList = append(httpRouteList,&httpRouteSign)
virtualService := &v1alpha3.VirtualService{
ObjectMeta: v1.ObjectMeta{
Name: "vs-test",
Namespace: namespace,
ResourceVersion: resourceVersion,
},
Spec: networkingv1alpha3.VirtualService{
Hosts: []string{"reviews"},
Gateways: []string{"knative-serving/knative-ingress-gateway"},
Http: httpRouteList,
},
}
vs,err :=istioClient.NetworkingV1alpha3().VirtualServices(namespace).Update(context.TODO(),virtualService,v1.UpdateOptions{})
if err != nil {
log.Print("Update error:%s", err)
return
}
log.Print(vs)
}
patch方法:
JSON Patch 和 JSON Merge Patch 的不同
posted on 2022-05-07 18:01 MissSimple 阅读(269) 评论(0) 收藏 举报
浙公网安备 33010602011771号