简体   繁体   English

使用 k8s SDK 应用 yaml 文件

[英]Apply yaml file using k8s SDK

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?我有以下 yaml,我需要使用 K8S go sdk(而不是 k8s cli)应用我没有找到使用 go sdk 的方法,因为它是自定义资源,知道如何通过代码将它应用到 k8s ?

This is the file这是文件

Any example will be very helpful!任何例子都会很有帮助!

apiVersion: aps.dp.com/v1alpha1
kind: Edtack
metadata:
  name: ed01
  namespace: ctr
spec:
  intRef:
    name: evr
  stack:
  - name: vectnt
    namespace: aps
    path: https://packages.timber.io/helm/latest/vect-0.11.0.tgz
    valuesRef:
      name: vecvalues
  - name: ek
    namespace: lg
    path: rescharts/bing
  - name: apigw-gloo-ee
    namespace: apw
    path: https://common.cdn.repositories.cloud.sap/api-gateway/apigw-gloo-ee/apigw-gloo-ee-0.3.0.tgz
    pullSecretRef:
      name: svr-secret
    valuesSecretRef:
      name: apis
  - name: kuback
    namespace: kube-prom
    path: https://github.com/prometheus-community/helm-charts/releases/download/kube-prometheus-stack-16.12.0/kube-prometheus-stack-16.12.0.tgz
    valuesSecretRef:
      name: kubes

You can use the k8sutil repo, see the apply example:您可以使用k8sutil 存储库,请参阅应用示例:

package main

import (
    "context"
    "flag"
    "log"
    "path/filepath"

    "github.com/pytimer/k8sutil/apply"

    "k8s.io/client-go/discovery"
    "k8s.io/client-go/dynamic"
    "k8s.io/client-go/tools/clientcmd"
    "k8s.io/client-go/util/homedir"
)

const applyStr = `
apiVersion: apps/v1
kind: Deployment
metadata:
  name: nginx
spec:
  replicas: 1
  selector:
    matchLabels:
      app: nginx
  template:
    metadata:
      labels:
        app: nginx
    spec:
      containers:
      - name: nginx
        image: nginx
---
apiVersion: v1
kind: Service
metadata:
  name: nginx-svc
spec:
  ports:
  - name: web
    port: 80
    protocol: TCP
    targetPort: 80
  selector:
    app: nginx
  type: ClusterIP
`

func main() {
    var kubeconfig *string
    if home := homedir.HomeDir(); home != "" {
        kubeconfig = flag.String("kubeconfig", filepath.Join(home, ".kube", "config"), "(optional) absolute path to the kubeconfig file")
    } else {
        kubeconfig = flag.String("kubeconfig", "", "absolute path to the kubeconfig file")
    }
    flag.Parse()

    // use the current context in kubeconfig
    config, err := clientcmd.BuildConfigFromFlags("", *kubeconfig)
    if err != nil {
        panic(err.Error())
    }

    dynamicClient, err := dynamic.NewForConfig(config)
    if err != nil {
        panic(err.Error())
    }
    discoveryClient, err := discovery.NewDiscoveryClientForConfig(config)
    if err != nil {
        panic(err.Error())
    }

    applyOptions := apply.NewApplyOptions(dynamicClient, discoveryClient)
    if err := applyOptions.Apply(context.TODO(), []byte(applyStr)); err != nil {
        log.Fatalf("apply error: %v", err)
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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