ASM provides SDK and API definitions for Istio resources starting from version 1.17, and supports configuring ASM custom resources from version 1.24. This topic describes how to use ASM SDK for Go to manage ASM resources.
Import ASM SDK for Go into your project
Run the following commands in the root directory of the project to import the ASM SDK for Go into your project.
go get github.com/aliyun/alibabacloud-servicemesh-go-client@release-1.24.1
go get github.com/aliyun/alibabacloud-servicemesh-api@release-1.24.1
Where release-1.24.1
is the branch of the repository, corresponding to version 1.24. You can choose a version based on your current ASM version:
If your ASM instance version is earlier than 1.17, obtain dependencies from the Istio community repository:
Preparations
The following sample code demonstrates how to use SDK for Go to manage ASM resources.
Obtain codes
Copy the asm-labs code repository to your local computer.
git clone https://github.com/AliyunContainerService/asm-labs.git
Go to the directory where the sample code is stored.
cd clients-demo/client-go
Configure a connection
Log on to the ASM console. In the left-side navigation pane, choose .
On the Mesh Management page, click the name of the ASM instance. In the left-side navigation pane, choose .
On the right side of the page, click Connection and copy the configuration content to the
$HOME/.kube/config
path.
Run the sample code
Run the sample code of SDK for Go.
go run main.go
Expected output:
I0311 16:39:14.367128 650926 main.go:24] successfully create rest config
I0311 16:39:14.367296 650926 main.go:31] successfully create clientset
I0311 16:39:14.367301 650926 main.go:33] run cluster scope clients demo
I0311 16:39:14.496240 650926 main.go:60] successfully create asmswimlanegroup
I0311 16:39:14.547445 650926 main.go:67] successfully get asmswimlanegroup
I0311 16:39:14.655944 650926 main.go:74] successfully list asmswimlanegroups
I0311 16:39:14.724114 650926 main.go:81] successfully delete asmswimlanegroup
I0311 16:39:14.724124 650926 main.go:36] run namespaced scope clients demo
I0311 16:39:14.779630 650926 main.go:97] successfully create asmlocalratelimiter
I0311 16:39:14.826832 650926 main.go:104] successfully get asmlocalratelimiter
I0311 16:39:14.881993 650926 main.go:111] successfully list asmlocalratelimiters
I0311 16:39:14.947009 650926 main.go:118] successfully delete asmlocalratelimiter
I0311 16:39:14.947020 650926 main.go:39] run istio resource demo
I0311 16:39:15.002439 650926 main.go:134] successfully create virtualservice
I0311 16:39:15.052896 650926 main.go:141] successfully get virtualservice
I0311 16:39:16.258057 650926 main.go:148] successfully list virtualservices
I0311 16:39:16.323547 650926 main.go:155] successfully delete virtualservice
Code explanation
The following explains the main.go
code.
Read Kubeconfig file and initialize clientset
Read the configuration into the kubeconfig file of the current environment. The default path for which you want to store configurations is ${HOME}/.kube/config
.
kubeconfigPath := os.Getenv("HOME") + "/.kube/config"
cfg, err := clientcmd.BuildConfigFromFlags("", kubeconfigPath)
if err != nil {
klog.Errorf("failed to create rest config, err: %+v", err)
os.Exit(1)
}
klog.Info("successfully create rest config")
Initialize the clientset for ASM resources.
clientset, err := asmversionedclient.NewForConfig(cfg)
if err != nil {
klog.Errorf("failed to create clientset, err: %+v", err)
os.Exit(1)
}
klog.Info("successfully create asm clientset")
Initialize the clientset for Istio resources.
istioclientset, err := istioversionedclient.NewForConfig(cfg)
if err != nil {
klog.Errorf("failed to create istio clientset, err: %+v", err)
os.Exit(1)
}
klog.Info("successfully create istio clientset")
Manage cluster-level resources
Create, query, and delete cluster-level resource ASMSwimLaneGroup
.
func runClusterScopeDemo(clientset *asmversionedclient.Clientset) {
asmswimlanegroup := &asmv1.ASMSwimLaneGroup{}
err := yaml.Unmarshal([]byte(swimlanegroupYaml), &asmswimlanegroup)
if err != nil {
klog.Errorf("failed to unmarshal yaml bytes, err: %+v", err)
os.Exit(1)
}
_, err = clientset.IstioV1().ASMSwimLaneGroups().Create(context.TODO(), asmswimlanegroup, metav1.CreateOptions{})
if err != nil {
klog.Errorf("failed to create asmswimlanegroup, err: %+v", err)
os.Exit(1)
}
klog.Info("successfully create asmswimlanegroup")
_, err = clientset.IstioV1().ASMSwimLaneGroups().Get(context.TODO(), asmswimlanegroup.Name, metav1.GetOptions{})
if err != nil {
klog.Errorf("failed to get asmswimlanegroup, err: %+v", err)
os.Exit(1)
}
klog.Info("successfully get asmswimlanegroup")
_, err = clientset.IstioV1().ASMSwimLaneGroups().List(context.TODO(), metav1.ListOptions{})
if err != nil {
klog.Errorf("failed to list asmswimlanegroups, err: %+v", err)
os.Exit(1)
}
klog.Info("successfully list asmswimlanegroups")
err = clientset.IstioV1().ASMSwimLaneGroups().Delete(context.TODO(), asmswimlanegroup.Name, metav1.DeleteOptions{})
if err != nil {
klog.Errorf("failed to delete asmswimlanegroup, err: %+v", err)
os.Exit(1)
}
klog.Info("successfully delete asmswimlanegroup")
}
Manage namespace-level resources
Create, query, and delete namespace-level resource ASMLocalRateLimiter
.
func runNamespacedScopeDemo(clientset *asmversionedclient.Clientset) {
asmlocalratelimiter := &asmv1.ASMLocalRateLimiter{}
err := yaml.Unmarshal([]byte(localratelimiterYaml), &asmlocalratelimiter)
if err != nil {
klog.Errorf("failed to unmarshal yaml bytes, err: %+v", err)
os.Exit(1)
}
_, err = clientset.IstioV1().ASMLocalRateLimiters(asmlocalratelimiter.Namespace).Create(context.TODO(), asmlocalratelimiter, metav1.CreateOptions{})
if err != nil {
klog.Errorf("failed to create asmlocalratelimiter, err: %+v", err)
os.Exit(1)
}
klog.Info("successfully create asmlocalratelimiter")
_, err = clientset.IstioV1().ASMLocalRateLimiters(asmlocalratelimiter.Namespace).Get(context.TODO(), asmlocalratelimiter.Name, metav1.GetOptions{})
if err != nil {
klog.Errorf("failed to get asmlocalratelimiter, err: %+v", err)
os.Exit(1)
}
klog.Info("successfully get asmlocalratelimiter")
_, err = clientset.IstioV1().ASMLocalRateLimiters(asmlocalratelimiter.Namespace).List(context.TODO(), metav1.ListOptions{})
if err != nil {
klog.Errorf("failed to list asmlocalratelimiters, err: %+v", err)
os.Exit(1)
}
klog.Info("successfully list asmlocalratelimiters")
err = clientset.IstioV1().ASMLocalRateLimiters(asmlocalratelimiter.Namespace).Delete(context.TODO(), asmlocalratelimiter.Name, metav1.DeleteOptions{})
if err != nil {
klog.Errorf("failed to delete asmlocalratelimiter, err: %+v", err)
os.Exit(1)
}
klog.Info("successfully delete asmlocalratelimiter")
}
Manage Istio resources
Create, query, and delete namespace-level resource VirtualService
.
func runIstioResourceDemo(clientset *istioversionedclient.Clientset) {
vs := &networkingv1.VirtualService{}
err := yaml.Unmarshal([]byte(virtualserviceYaml), &vs)
if err != nil {
klog.Errorf("failed to unmarshal yaml bytes, err: %+v", err)
os.Exit(1)
}
_, err = clientset.NetworkingV1().VirtualServices(vs.Namespace).Create(context.TODO(), vs, metav1.CreateOptions{})
if err != nil {
klog.Errorf("failed to create virtualservice, err: %+v", err)
os.Exit(1)
}
klog.Info("successfully create virtualservice")
_, err = clientset.NetworkingV1().VirtualServices(vs.Namespace).Get(context.TODO(), vs.Name, metav1.GetOptions{})
if err != nil {
klog.Errorf("failed to get virtualservice, err: %+v", err)
os.Exit(1)
}
klog.Info("successfully get virtualservice")
_, err = clientset.NetworkingV1().VirtualServices(vs.Namespace).List(context.TODO(), metav1.ListOptions{})
if err != nil {
klog.Errorf("failed to list virtualservices, err: %+v", err)
os.Exit(1)
}
klog.Info("successfully list virtualservices")
err = clientset.NetworkingV1().VirtualServices(vs.Namespace).Delete(context.TODO(), vs.Name, metav1.DeleteOptions{})
if err != nil {
klog.Errorf("failed to delete virtualservice, err: %+v", err)
os.Exit(1)
}
klog.Info("successfully delete virtualservice")
}