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)
//}
}
相关文章
- 10-18【AUTOSAR-COM】-1-Frame/Message/CAN_ID变周期Periodic发送--API实现
- 10-18windows API实现用户选择文件路径的对话框
- 10-18Kaggle API下载与安装
- 10-18“用Kaggle API”下载比赛部分数据文件夹 | 总结与翻译外网相关资源
- 10-18net.sf.json的常用api
- 10-18使用腾讯开放api定位ip地址
- 10-18使用Youtube IFrame API时,getPlaylist返回一个仅包含一个视频的数组
- 10-18Android YouTube api v3 – 方向
- 10-18Youtube Python API
- 10-18Youtube API-设置视频缩略图(python)