istio API

package main

import (
    "context"
    "flag"
    "fmt"
    networkingv1beta1 "istio.io/api/networking/v1beta1"
    "istio.io/client-go/pkg/apis/networking/v1beta1"
    versionedclient "istio.io/client-go/pkg/clientset/versioned"
    "istio.io/client-go/pkg/informers/externalversions"
    v1 "k8s.io/apimachinery/pkg/apis/meta/v1"
    "k8s.io/client-go/tools/cache"
    "k8s.io/client-go/tools/clientcmd"
    "k8s.io/client-go/util/homedir"
    "log"
    "path/filepath"
)

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

    restConfig, err := clientcmd.BuildConfigFromFlags("", *kubeconfig)
    if err != nil {
        log.Fatalf("Failed to create k8s rest client: %s", err)
    }

    clientSet, err := versionedclient.NewForConfig(restConfig)
    if err != nil {
        log.Fatalf("Failed to create istio client: %s", err)
    }

    factory := externalversions.NewSharedInformerFactory(clientSet, 0)
    Stopper := make(chan struct{})
    // 启动 informer,list & watch
    go factory.Start(Stopper)

    virtualServiceInformer := factory.Networking().V1beta1().VirtualServices()

    virtualServiceIndexer := virtualServiceInformer.Informer()

    // 从 apiserver 同步资源,即 list
    if !cache.WaitForCacheSync(Stopper, virtualServiceIndexer.HasSynced) {
        panic("scaled_object informer timed out waiting for caches to sync")
    }
    vs, err := virtualServiceInformer.Lister().VirtualServices("default").Get("bookinfo")
    if err != nil {
        log.Fatalf("Failed to get virtualService: %s", err)
    }
    fmt.Println("virtualService--------", vs.Name)
    vsNew := &v1beta1.VirtualService{

        ObjectMeta: v1.ObjectMeta{
            Name: "bookinfo2",
        },
        Spec: networkingv1beta1.VirtualService{
            Hosts:    []string{"*"},
            Gateways: []string{"bookinfo-gateway"},
            Http: []*networkingv1beta1.HTTPRoute{
                {
                    Match: []*networkingv1beta1.HTTPMatchRequest{
                        {
                            Uri: &networkingv1beta1.StringMatch{
                                MatchType: &networkingv1beta1.StringMatch_Prefix{
                                    Prefix: "/static",
                                },
                            },
                        },
                    },
                    Route: []*networkingv1beta1.HTTPRouteDestination{
                        {
                            Destination: &networkingv1beta1.Destination{
                                Host: "productpage",
                                Port: &networkingv1beta1.PortSelector{
                                    Number: 8090,
                                },
                            },
                        },
                    },
                },
            },
        },
    }

    _, err = clientSet.NetworkingV1beta1().VirtualServices("default").Create(context.Background(), vsNew, v1.CreateOptions{})
    if err != nil {
        log.Fatalf("Failed to create virtualService: %s", err)
    }
    fmt.Println("创建成功")
    // Print all VirtualServices
    //vsList, err := clientSet.NetworkingV1alpha3().VirtualServices("default").List(context.Background(), metav1.ListOptions{})
    //if err != nil {
    //  log.Fatalf("Failed to get VirtualService in %s namespace: %s", "default", err)
    //}
    //for i := range vsList.Items {
    //  vs := vsList.Items[i]
    //  log.Printf("Index: %d VirtualService Hosts: %+v\n", i, vs.Name)
    //}
}
上一篇:无需本地环境,云上一键体验Istio 1.0


下一篇:idou老师教你学Istio 04:Istio性能及扩展性介绍