Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Select an option

  • Save aaron-prindle/dd5c572bd1d9ff6f78629b6181adce23 to your computer and use it in GitHub Desktop.

Select an option

Save aaron-prindle/dd5c572bd1d9ff6f78629b6181adce23 to your computer and use it in GitHub Desktop.

Revisions

  1. aaron-prindle created this gist Jun 26, 2025.
    152 changes: 152 additions & 0 deletions gistfile1.txt
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,152 @@
    package main

    import (
    "context"
    "encoding/json"
    "fmt"
    "log"
    "path/filepath"

    metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
    "k8s.io/apimachinery/pkg/types"
    "k8s.io/client-go/kubernetes"
    "k8s.io/client-go/tools/clientcmd"
    "k8s.io/client-go/util/homedir"
    )

    func main() {
    // Build config from kubeconfig
    var kubeconfig string
    if home := homedir.HomeDir(); home != "" {
    kubeconfig = filepath.Join(home, ".kube", "config")
    }

    config, err := clientcmd.BuildConfigFromFlags("", kubeconfig)
    if err != nil {
    log.Fatal(err)
    }

    // Create clientset
    clientset, err := kubernetes.NewForConfig(config)
    if err != nil {
    log.Fatal(err)
    }

    ctx := context.Background()

    // Test 1: GET operation
    fmt.Println("=== Testing GET operation ===")
    deployment, err := clientset.AppsV1().Deployments("default").Get(ctx, "test-deployment", metav1.GetOptions{})
    if err != nil {
    log.Fatal(err)
    }
    fmt.Printf("GET - Kind: %q, APIVersion: %q\n", deployment.Kind, deployment.APIVersion)
    fmt.Printf("GET - Name: %s, Replicas: %d\n", deployment.Name, *deployment.Spec.Replicas)

    // Let's also check the raw JSON to see what the server actually sends
    fmt.Println("\n=== Checking raw JSON from server ===")
    raw, err := clientset.AppsV1().RESTClient().
    Get().
    Resource("deployments").
    Namespace("default").
    Name("test-deployment").
    DoRaw(ctx)
    if err != nil {
    log.Fatal(err)
    }

    var rawMap map[string]interface{}
    json.Unmarshal(raw, &rawMap)
    fmt.Printf("Raw JSON - kind: %v, apiVersion: %v\n", rawMap["kind"], rawMap["apiVersion"])

    // Test 2: PATCH operation
    fmt.Println("\n=== Testing PATCH operation ===")
    patchData := []byte(`{"spec":{"replicas":5}}`)
    patched, err := clientset.AppsV1().Deployments("default").Patch(
    ctx,
    "test-deployment",
    types.StrategicMergePatchType,
    patchData,
    metav1.PatchOptions{},
    )
    if err != nil {
    log.Fatal(err)
    }
    fmt.Printf("PATCH - Kind: %q, APIVersion: %q\n", patched.Kind, patched.APIVersion)
    fmt.Printf("PATCH - Name: %s, Replicas: %d\n", patched.Name, *patched.Spec.Replicas)

    // Test 3: Server-Side Apply (with force to avoid conflicts)
    fmt.Println("\n=== Testing Server-Side Apply ===")
    applyData := []byte(`{
    "apiVersion": "apps/v1",
    "kind": "Deployment",
    "metadata": {
    "name": "test-deployment",
    "namespace": "default"
    },
    "spec": {
    "replicas": 7,
    "selector": {
    "matchLabels": {
    "app": "test"
    }
    },
    "template": {
    "metadata": {
    "labels": {
    "app": "test"
    }
    },
    "spec": {
    "containers": [{
    "name": "nginx",
    "image": "nginx:latest"
    }]
    }
    }
    }
    }`)

    force := true
    applied, err := clientset.AppsV1().Deployments("default").Patch(
    ctx,
    "test-deployment",
    types.ApplyPatchType,
    applyData,
    metav1.PatchOptions{
    FieldManager: "test-client",
    Force: &force, // Force to avoid conflicts
    },
    )
    if err != nil {
    log.Fatal(err)
    }
    fmt.Printf("APPLY - Kind: %q, APIVersion: %q\n", applied.Kind, applied.APIVersion)
    fmt.Printf("APPLY - Name: %s, Replicas: %d\n", applied.Name, *applied.Spec.Replicas)

    // Test 4: LIST operation
    fmt.Println("\n=== Testing LIST operation ===")
    list, err := clientset.AppsV1().Deployments("default").List(ctx, metav1.ListOptions{})
    if err != nil {
    log.Fatal(err)
    }
    fmt.Printf("LIST - Kind: %q, APIVersion: %q\n", list.Kind, list.APIVersion)
    if len(list.Items) > 0 {
    fmt.Printf("First item - Kind: %q, APIVersion: %q\n",
    list.Items[0].Kind, list.Items[0].APIVersion)
    }

    // Test 5: CREATE operation (create a new deployment)
    fmt.Println("\n=== Testing CREATE operation ===")
    newDep := deployment.DeepCopy()
    newDep.Name = "test-deployment-2"
    newDep.ResourceVersion = ""
    created, err := clientset.AppsV1().Deployments("default").Create(ctx, newDep, metav1.CreateOptions{})
    if err != nil {
    fmt.Printf("Create failed (might already exist): %v\n", err)
    } else {
    fmt.Printf("CREATE - Kind: %q, APIVersion: %q\n", created.Kind, created.APIVersion)
    // Clean up
    clientset.AppsV1().Deployments("default").Delete(ctx, "test-deployment-2", metav1.DeleteOptions{})
    }
    }