How to access 3rd-party Custom Resource Definition? How to access 3rd-party Custom Resource Definition? kubernetes kubernetes

How to access 3rd-party Custom Resource Definition?


Here is an example of creating a istio virtual service using istio client-go. Here we are using istio.io/api/networking/v1alpha3 and istio.io/client-go/pkg/apis/networking/v1alpha3 to create the VirtualService custom resource spec. After that using istio.io/client-go/pkg/clientset/versioned to actually sending this spec to kubernetes API server.

package mainimport (    "context"    "log"    "os"    v1alpha3Spec "istio.io/api/networking/v1alpha3"    "istio.io/client-go/pkg/apis/networking/v1alpha3"    versionedclient "istio.io/client-go/pkg/clientset/versioned"    metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"    "k8s.io/client-go/tools/clientcmd")func main() {    kubeconfig := os.Getenv("KUBECONFIG")    namespace := os.Getenv("NAMESPACE")    if len(kubeconfig) == 0 || len(namespace) == 0 {        log.Fatalf("Environment variables KUBECONFIG and NAMESPACE need to be set")    }    restConfig, err := clientcmd.BuildConfigFromFlags("", kubeconfig)    if err != nil {        log.Fatalf("Failed to create k8s rest client: %s", err)    }    ic, err := versionedclient.NewForConfig(restConfig)    if err != nil {        log.Fatalf("Failed to create istio client: %s", err)    }    var host []string    host[0] = "abc.com"    virtualServiceCrd := &v1alpha3.VirtualService{        TypeMeta: metav1.TypeMeta{            APIVersion: "networking.istio.io/v1alpha3",            Kind:       "Virtualservice",        },        ObjectMeta: metav1.ObjectMeta{            Name: "default",        },        Spec: v1alpha3Spec.VirtualService{            Hosts: host,        },    }    ic.NetworkingV1alpha3().VirtualServices(namespace).Create(context.TODO(), virtualServiceCrd, metav1.CreateOptions{})}


import istio virtualservices package

Yes, you should import the Istio package, and every CRD is an api, so it comes with a client for "creating", "deleting", "updating" and "watching" programmatically using the library.

If you are using Golang, this is a good start: https://github.com/istio/client-go

And the api client:https://github.com/istio/client-go/tree/master/pkg