Skip to content

Instantly share code, notes, and snippets.

@shaneutt
Forked from dwmkerr/k8s-patch.go
Created April 1, 2021 19:47
Show Gist options
  • Save shaneutt/f7565a3aaedb42d32b2641ed92ddb670 to your computer and use it in GitHub Desktop.
Save shaneutt/f7565a3aaedb42d32b2641ed92ddb670 to your computer and use it in GitHub Desktop.

Revisions

  1. @dwmkerr dwmkerr revised this gist Feb 11, 2020. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion k8s-patch.go
    Original file line number Diff line number Diff line change
    @@ -29,7 +29,7 @@ type patchStringValue struct {
    Value string `json:"value"`
    }

    // patchStringValue specifies a patch operation for a uint32.
    // patchUint32Value specifies a patch operation for a uint32.
    type patchUInt32Value struct {
    Op string `json:"op"`
    Path string `json:"path"`
  2. @dwmkerr dwmkerr revised this gist Oct 8, 2018. 1 changed file with 2 additions and 0 deletions.
    2 changes: 2 additions & 0 deletions k8s-patch.go
    Original file line number Diff line number Diff line change
    @@ -1,4 +1,6 @@
    // Example showing how to patch kubernetes resources.
    // This is the companion to my article 'Patching Kubernetes Resources in Golang':
    // https://dwmkerr.com/patching-kubernetes-resources-in-golang/
    package main

    import (
  3. @dwmkerr dwmkerr revised this gist Oct 8, 2018. No changes.
  4. @dwmkerr dwmkerr renamed this gist Oct 8, 2018. 1 changed file with 0 additions and 0 deletions.
    File renamed without changes.
  5. @dwmkerr dwmkerr created this gist Jul 13, 2018.
    73 changes: 73 additions & 0 deletions patch.go
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,73 @@
    // Example showing how to patch kubernetes resources.
    package main

    import (
    "encoding/json"
    "fmt"

    types "k8s.io/apimachinery/pkg/types"
    "k8s.io/client-go/kubernetes"
    _ "k8s.io/client-go/plugin/pkg/client/auth"
    "k8s.io/client-go/tools/clientcmd"
    )

    var (
    // Leave blank for the default context in your kube config.
    context = ""

    // Name of the replication controller to scale, and the desired number of replicas.
    replicationControllerName = "my-rc"
    replicas = uint32(3)
    )

    // 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 scaleReplicationController(clientSet *kubernetes.Clientset, replicasetName string, scale uint32) error {
    payload := []patchUInt32Value{{
    Op: "replace",
    Path: "/spec/replicas",
    Value: scale,
    }}
    payloadBytes, _ := json.Marshal(payload)
    _, err := clientSet.
    CoreV1().
    ReplicationControllers("default").
    Patch(replicasetName, types.JSONPatchType, payloadBytes)
    return err
    }

    func main() {
    // Get the local kube config.
    fmt.Printf("Connecting to Kubernetes Context %v\n", context)
    config, err := clientcmd.NewNonInteractiveDeferredLoadingClientConfig(
    clientcmd.NewDefaultClientConfigLoadingRules(),
    &clientcmd.ConfigOverrides{CurrentContext: context}).ClientConfig()
    if err != nil {
    panic(err.Error())
    }

    // Creates the clientset
    clientset, err := kubernetes.NewForConfig(config)
    if err != nil {
    panic(err.Error())
    }

    // Scale our replication controller.
    fmt.Printf("Scaling replication controller %v to %v\n", replicationControllerName, replicas)
    err = scaleReplicationController(clientset, replicationControllerName, replicas)
    if err != nil {
    panic(err.Error())
    }
    }