k6 – 使用Go客户端创建自定义资源

huangapple go评论89阅读模式
英文:

k6 - create custom resource using go client

问题

有人知道如何使用go-client创建自定义资源吗?基本上相当于kubectl apply -f 'yaml路径'

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"

以下是生成自定义资源的go-client代码

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
}

但是在AbsPath("/apis/k6.io/v1alpha1/namespaces/default/k6")上我得到了404页面未找到的错误。

英文:

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").

答案1

得分: 1

发现了问题所在,当你执行 kubectl apply 命令时,使用 -v 8 参数来查看 POST 请求的 Abspath 检查。

kubectl apply -f '资源路径'

修正后的代码如下:

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

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()) 

huangapple
  • 本文由 发表于 2022年8月16日 02:46:02
  • 转载请务必保留本文链接:https://go.coder-hub.com/73365206.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定