使用k8s SDK应用yaml文件

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

Apply yaml file using k8s SDK

问题

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

这是文件的内容:

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

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
英文:

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!

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

 

答案1

得分: 1

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

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)
	}
}
英文:

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

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

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:

确定