Client-go -- dynamicClient 操作CRD 资源

操作CRD资源

package main

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

	"k8s.io/apimachinery/pkg/api/meta"
	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
	"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
	"k8s.io/apimachinery/pkg/runtime/schema"
	"k8s.io/apimachinery/pkg/runtime/serializer/yaml"
	"k8s.io/client-go/discovery"
	"k8s.io/client-go/discovery/cached/memory"
	"k8s.io/client-go/dynamic"
	"k8s.io/client-go/rest"
	"k8s.io/client-go/restmapper"
	"k8s.io/client-go/tools/clientcmd"
	"k8s.io/client-go/util/homedir"
	"k8s.io/client-go/util/retry"
)
 // 自定义数据
const metaCRD = `
apiVersion: "cs.handpay.cn/v1"
kind: Redis
metadata:
  name: test
  namespace: default
spec:
  replicas: 20
  name: laoshu
`


func GetK8sConfig() (config *rest.Config, err error) {
   
	// 获取k8s rest config
	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()

	config, err = clientcmd.BuildConfigFromFlags("", *kubeconfig)
	if err != nil {
   
		panic(err)
	}
	return
}

func GetGVRdyClient(gvk *schema.GroupVersionKind,namespace string) (dr dynamic.ResourceInterface,err error)  {
   

	config,err := GetK8sConfig()
	if err 

你可能感兴趣的:(kubernetes)