Reputation: 395
I am working on Opersator-SDK. In my operator controller, I try to create an Istio Custom resource -- virtualservices. The definition of virtualservices looks like as following:
kind: CustomResourceDefinition
apiVersion: apiextensions.k8s.io/v1beta1
metadata:
name: virtualservices.networking.istio.io
Here I think there are a few of things that have to do first:
My question is how to do them? Because I never do that before, so have to ask for help how to do that.
Upvotes: 4
Views: 1420
Reputation: 44559
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 main
import (
"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{})
}
Upvotes: 2
Reputation: 128807
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
Upvotes: 1