使用k8s SDK应用yaml文件

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

Apply yaml file using k8s SDK

问题

我有以下的yaml文件,我需要使用K8S go sdk(而不是k8s cli)来应用它。我在go sdk中没有找到适用于自定义资源的方法,你有什么办法可以通过代码将其应用到k8s上吗?

这是文件的内容:

任何示例都将非常有帮助!

  1. apiVersion: aps.dp.com/v1alpha1
  2. kind: Edtack
  3. metadata:
  4. name: ed01
  5. namespace: ctr
  6. spec:
  7. intRef:
  8. name: evr
  9. stack:
  10. - name: vectnt
  11. namespace: aps
  12. path: https://packages.timber.io/helm/latest/vect-0.11.0.tgz
  13. valuesRef:
  14. name: vecvalues
  15. - name: ek
  16. namespace: lg
  17. path: rescharts/bing
  18. - name: apigw-gloo-ee
  19. namespace: apw
  20. path: https://common.cdn.repositories.cloud.sap/api-gateway/apigw-gloo-ee/apigw-gloo-ee-0.3.0.tgz
  21. pullSecretRef:
  22. name: svr-secret
  23. valuesSecretRef:
  24. name: apis
  25. - name: kuback
  26. namespace: kube-prom
  27. path: https://github.com/prometheus-community/helm-charts/releases/download/kube-prometheus-stack-16.12.0/kube-prometheus-stack-16.12.0.tgz
  28. valuesSecretRef:
  29. name: kubes
英文:

I’ve the following yaml which I need to apply using the K8S go sdk (and not k8s cli)
I didn’t find a way with the go sdk as it is custom resource, any idea how I can apply it via code to k8s?

This is the file

Any example will be very helpful!

  1. apiVersion: aps.dp.com/v1alpha1
  2. kind: Edtack
  3. metadata:
  4.   name: ed01
  5.   namespace: ctr
  6. spec:
  7.   intRef:
  8.     name: evr
  9.   stack:
  10.   - name: vectnt
  11.     namespace: aps
  12.     path: https://packages.timber.io/helm/latest/vect-0.11.0.tgz
  13.     valuesRef:
  14.       name: vecvalues
  15.   - name: ek
  16.     namespace: lg
  17.     path: rescharts/bing
  18.   - name: apigw-gloo-ee
  19.     namespace: apw
  20.     path: https://common.cdn.repositories.cloud.sap/api-gateway/apigw-gloo-ee/apigw-gloo-ee-0.3.0.tgz
  21.     pullSecretRef:
  22.       name: svr-secret
  23.     valuesSecretRef:
  24.       name: apis
  25.   - name: kuback
  26.     namespace: kube-prom
  27.     path: https://github.com/prometheus-community/helm-charts/releases/download/kube-prometheus-stack-16.12.0/kube-prometheus-stack-16.12.0.tgz
  28.     valuesSecretRef:
  29.       name: kubes

 

答案1

得分: 1

你可以使用k8sutil仓库,查看apply示例:

  1. package main
  2. import (
  3. "context"
  4. "flag"
  5. "log"
  6. "path/filepath"
  7. "github.com/pytimer/k8sutil/apply"
  8. "k8s.io/client-go/discovery"
  9. "k8s.io/client-go/dynamic"
  10. "k8s.io/client-go/tools/clientcmd"
  11. "k8s.io/client-go/util/homedir"
  12. )
  13. const applyStr = `
  14. apiVersion: apps/v1
  15. kind: Deployment
  16. metadata:
  17. name: nginx
  18. spec:
  19. replicas: 1
  20. selector:
  21. matchLabels:
  22. app: nginx
  23. template:
  24. metadata:
  25. labels:
  26. app: nginx
  27. spec:
  28. containers:
  29. - name: nginx
  30. image: nginx
  31. ---
  32. apiVersion: v1
  33. kind: Service
  34. metadata:
  35. name: nginx-svc
  36. spec:
  37. ports:
  38. - name: web
  39. port: 80
  40. protocol: TCP
  41. targetPort: 80
  42. selector:
  43. app: nginx
  44. type: ClusterIP
  45. `
  46. func main() {
  47. var kubeconfig *string
  48. if home := homedir.HomeDir(); home != "" {
  49. kubeconfig = flag.String("kubeconfig", filepath.Join(home, ".kube", "config"), "(optional) absolute path to the kubeconfig file")
  50. } else {
  51. kubeconfig = flag.String("kubeconfig", "", "absolute path to the kubeconfig file")
  52. }
  53. flag.Parse()
  54. // use the current context in kubeconfig
  55. config, err := clientcmd.BuildConfigFromFlags("", *kubeconfig)
  56. if err != nil {
  57. panic(err.Error())
  58. }
  59. dynamicClient, err := dynamic.NewForConfig(config)
  60. if err != nil {
  61. panic(err.Error())
  62. }
  63. discoveryClient, err := discovery.NewDiscoveryClientForConfig(config)
  64. if err != nil {
  65. panic(err.Error())
  66. }
  67. applyOptions := apply.NewApplyOptions(dynamicClient, discoveryClient)
  68. if err := applyOptions.Apply(context.TODO(), []byte(applyStr)); err != nil {
  69. log.Fatalf("apply error: %v", err)
  70. }
  71. }
英文:

You can use the k8sutil repo, see the apply example:

  1. package main
  2. import (
  3. "context"
  4. "flag"
  5. "log"
  6. "path/filepath"
  7. "github.com/pytimer/k8sutil/apply"
  8. "k8s.io/client-go/discovery"
  9. "k8s.io/client-go/dynamic"
  10. "k8s.io/client-go/tools/clientcmd"
  11. "k8s.io/client-go/util/homedir"
  12. )
  13. const applyStr = `
  14. apiVersion: apps/v1
  15. kind: Deployment
  16. metadata:
  17. name: nginx
  18. spec:
  19. replicas: 1
  20. selector:
  21. matchLabels:
  22. app: nginx
  23. template:
  24. metadata:
  25. labels:
  26. app: nginx
  27. spec:
  28. containers:
  29. - name: nginx
  30. image: nginx
  31. ---
  32. apiVersion: v1
  33. kind: Service
  34. metadata:
  35. name: nginx-svc
  36. spec:
  37. ports:
  38. - name: web
  39. port: 80
  40. protocol: TCP
  41. targetPort: 80
  42. selector:
  43. app: nginx
  44. type: ClusterIP
  45. `
  46. func main() {
  47. var kubeconfig *string
  48. if home := homedir.HomeDir(); home != "" {
  49. kubeconfig = flag.String("kubeconfig", filepath.Join(home, ".kube", "config"), "(optional) absolute path to the kubeconfig file")
  50. } else {
  51. kubeconfig = flag.String("kubeconfig", "", "absolute path to the kubeconfig file")
  52. }
  53. flag.Parse()
  54. // use the current context in kubeconfig
  55. config, err := clientcmd.BuildConfigFromFlags("", *kubeconfig)
  56. if err != nil {
  57. panic(err.Error())
  58. }
  59. dynamicClient, err := dynamic.NewForConfig(config)
  60. if err != nil {
  61. panic(err.Error())
  62. }
  63. discoveryClient, err := discovery.NewDiscoveryClientForConfig(config)
  64. if err != nil {
  65. panic(err.Error())
  66. }
  67. applyOptions := apply.NewApplyOptions(dynamicClient, discoveryClient)
  68. if err := applyOptions.Apply(context.TODO(), []byte(applyStr)); err != nil {
  69. log.Fatalf("apply error: %v", err)
  70. }
  71. }

huangapple
  • 本文由 发表于 2021年9月8日 02:44:31
  • 转载请务必保留本文链接:https://go.coder-hub.com/69093178.html
匿名

发表评论

匿名网友

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

确定