Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
Example showing how to patch an Istio VirtualService CRDs Golang
// Example showing how to patch Kubernetes resources.
package main
import (
types ""
_ ""
var (
// Leave blank for the default context in your kube config.
context = ""
// Name of the VirtualService to weight, and the two weight values.
virtualServiceName = "service2"
weight1 = uint32(50)
weight2 = uint32(50)
// patchStringValue specifies a patch operation for a string.
type patchStringValue struct {
Op string `json:"op"`
Path string `json:"path"`
Value string `json:"value"`
// patchStringValue specifies a patch operation for a uint32.
type patchUInt32Value struct {
Op string `json:"op"`
Path string `json:"path"`
Value uint32 `json:"value"`
func setVirtualServiceWeights(client dynamic.Interface, virtualServiceName string, weight1 uint32, weight2 uint32) error {
// Create a GVR which represents an Istio Virtual Service.
virtualServiceGVR := schema.GroupVersionResource{
Group: "",
Version: "v1alpha3",
Resource: "virtualservices",
// Weight the two routes - 50/50.
patchPayload := make([]patchUInt32Value, 2)
patchPayload[0].Op = "replace"
patchPayload[0].Path = "/spec/http/0/route/0/weight"
patchPayload[0].Value = 50
patchPayload[1].Op = "replace"
patchPayload[1].Path = "/spec/http/0/route/1/weight"
patchPayload[1].Value = 50
patchBytes, _ := json.Marshal(patchPayload)
// Apply the patch to the 'service2' service.
_, err := client.Resource(virtualServiceGVR).Namespace("default").Patch(virtualServiceName, types.JSONPatchType, patchBytes)
return err
func main() {
// Get the local kube config.
fmt.Printf("Connecting to Kubernetes Context %v\n", context)
config, err := clientcmd.NewNonInteractiveDeferredLoadingClientConfig(
&clientcmd.ConfigOverrides{CurrentContext: context}).ClientConfig()
if err != nil {
// Creates the dynamic interface.
dynamicClient, err := dynamic.NewForConfig(config)
if err != nil {
// Re-balance the weights of the hosts in the virtual service.
setVirtualServiceWeights(dynamicClient, "service2", 50, 50)

This comment has been minimized.

Copy link

commented Jun 6, 2019

--# go build
--# tmp/for/tool/work
./k8s-patch-virtualservice.go:57:73: not enough arguments in call to client.Resource(virtualServiceGVR).Namespace("default").Patch
have (string, types.PatchType, []byte)
want (string, types.PatchType, []byte, v1.PatchOptions, ...string)

--- about go version ???


This comment has been minimized.

Copy link
Owner Author

commented Jun 7, 2019

I think it's the fact that the k8s api is changing regularly - dependency management for golang is a total PITA


This comment has been minimized.

Copy link

commented Aug 23, 2019

I see from your example, that you are updating a VirtualService changing the weights.. is there an example for creating a new VirtualService similarly ? Thanks, Sreeni

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.