简体   繁体   中英

k6 - create custom resource using go client

Anyone know how to create a Custom Resource using go-client. basically equivalent of kubectl apply -f 'yaml path'

apiVersion: k6.io/v1alpha1
kind: K6
metadata:
  name: k6-sample
spec:
  parallelism: 1
  #arguments: --out statsd
  #cleanup: post
  script:
    configMap:
      name: "staging-stress-test"
      file: "staging.js"

Have a go-client code to generate a Custom Resource below

func createk6CR(clientset *kubernetes.Clientset) (string, error) {

    k6plugin := &v1alpha1.K6{
        TypeMeta: metav1.TypeMeta{
            APIVersion: "k6.io/v1alpha1",
            Kind:       "K6",
        },
        ObjectMeta: metav1.ObjectMeta{
            Name:      "k6-sample-1",
            Namespace: "default",
        },
        Spec: v1alpha1.K6Spec{
            Parallelism: 3,
            Script: v1alpha1.K6Script{
                ConfigMap: v1alpha1.K6Configmap{
                    Name: "staging-stress-test",
                    File: "staging.js",
                },
            },
        },
        // Status: v1alpha1.K6Status{
        //  Stage: "started",
        // },
    }

    body, err := json.Marshal(k6plugin)

    if err != nil {
        fmt.Printf("error getting Kubernetes config: %v\n", err)
        os.Exit(1)
    }

    data, err := clientset.RESTClient().
        Post().
        AbsPath("/apis/k6.io/v1alpha1/namespaces/default/k6").
        Body(body).
        DoRaw(context.TODO())

    if data != nil {
        str := string(data[:])

        fmt.Printf("return data: %v\n", str)
        //os.Exit(1)
    }

    return "success", err
}

But I get Page 404 not found on AbsPath("/apis/k6.io/v1alpha1/namespaces/default/k6").

Found what was wrong with it, when you do kubectl apply pass it with -v 8 to see the Abspath check for POST

kubectl apply -f 'resource path'

I0816 09:20:56.239402   15535 round_trippers.go:463] POST https://0.0.0.0:43117/apis/k6.io/v1alpha1/namespaces/default/k6s?fieldManager=kubectl-client-side-apply&fieldValidation=Strict
I0816 09:20:56.239428   15535 round_trippers.go:469] Request Headers: 

so the code with corrected AbsPath should be below,

data, err := clientset.RESTClient().
        Post().
        AbsPath("/apis/k6.io/v1alpha1/namespaces/default/k6s").
        Body(body).
        DoRaw(context.TODO()) 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM