All Products
Search
Document Center

Alibaba Cloud Service Mesh:Use the SDK for Go to manage Istio resources

Last Updated:Sep 12, 2023

After you integrate the Service Mesh (ASM) SDK for a programming language, you can manage Istio resources in your code. This topic describes how to use ASM SDK for Go whose version is istio.io/client-go v1.14.0 to manage Istio resources in Go 1.18.

Prerequisites

Preparations

Install dependencies

  1. Open the CLI terminal in your project directory.

  2. Optional:If no go.mod file is created in the project, run the following command:

    go mod init [Module name]
  3. Run the following command to install dependencies:

    go get github.com/ghodss/yaml istio.io/client-go/pkg/apis/networking/v1beta1 istio.io/client-go/pkg/clientset/versioned k8s.io/apimachinery/pkg/apis/meta/v1 k8s.io/client-go/tools/clientcmd

Configure a connection

  1. Log on to the ASM console.

  2. In the left-side navigation pane, choose Service Mesh > Mesh Management.

  3. On the Mesh Management page, find the ASM instance that you want to configure. Click the name of the ASM instance or click Manage in the Actions column.

  4. On the details page of the ASM instance, click Base Information in the left-side navigation pane. On the Basic Information page, click Connection.

  5. In the Connection panel, copy the content of the system kubeconfig file to the $HOME/.kube/config directory of your machine.

Generate configuration files

Save the virtualService.yaml and gateway.yaml files created in Use ASM SDK to manage Istio resources in code to your project directory.

Procedure

Create a virtual service

Run the following code to create the virtual service that is defined by the virtualService.yaml file in the default namespace:

package main

import (
    "context"
    "fmt"
    "io/ioutil"

    "github.com/ghodss/yaml"
    "istio.io/client-go/pkg/apis/networking/v1beta1"
    versionedclient "istio.io/client-go/pkg/clientset/versioned"
    metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
    "k8s.io/client-go/tools/clientcmd"
)

func main() {
    const NAMESPACE = "default"
    restConfig, err := clientcmd.BuildConfigFromFlags("", "{path to .kube/config file}") // Enter the path to the .kube/config file in braces {}. Default path: $HOME/.kube/config. 
    if err != nil {
        fmt.Println("Failed to obtain the connection configuration.")
        return
    }
    clientset, err := versionedclient.NewForConfig(restConfig)
    if err != nil {
        fmt.Println("Failed to create a client.")
        return
    }
    // You can use YAML deserialization to construct the virtual service struct. You can also directly construct the struct. 
    virtualService := &v1beta1.VirtualService{}
    bytes, err := ioutil.ReadFile("virtualService.yaml")
    if err != nil {
        fmt.Println("Failed to read the YAML file.")
        return
    }
    err = yaml.Unmarshal(bytes, &virtualService)
    if err != nil {
        fmt.Println("Deserialization failed.")
        return
    }
    _, err = clientset.NetworkingV1beta1().VirtualServices(NAMESPACE).Create(context.TODO(), virtualService, metav1.CreateOptions{})
    if err != nil {
        fmt.Println("Failed to create a virtual service.")
    }
    printAllVisualServices(clientset, NAMESPACE)
}

// Display all virtual services in the namespace. 
func printAllVisualServices(clientset *versionedclient.Clientset, namespace string) {
    vsList, err := clientset.NetworkingV1beta1().VirtualServices(namespace).List(context.TODO(), metav1.ListOptions{})
    if err != nil {
        fmt.Println("Failed to obtain virtual services.")
    }
    for _, vs := range vsList.Items {
        fmt.Println(vs)
    }
}

Create an Istio gateway

Run the following code to create the Istio gateway that is defined by the gateway.yaml file in the default namespace:

package main

import (
    "context"
    "fmt"
    "io/ioutil"

    "github.com/ghodss/yaml"
    "istio.io/client-go/pkg/apis/networking/v1beta1"
    versionedclient "istio.io/client-go/pkg/clientset/versioned"
    metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
    "k8s.io/client-go/tools/clientcmd"
)

func main() {
    const NAMESPACE = "default"
    restConfig, err := clientcmd.BuildConfigFromFlags("", "{path to .kube/config file}") // Enter the path to the .kube/config file in braces {}. Default path: $HOME/.kube/config. 
    if err != nil {
        fmt.Println("Failed to obtain the connection configuration.")
        return
    }
    clientset, err := versionedclient.NewForConfig(restConfig)
    if err != nil {
        fmt.Println("Failed to create a client.")
        return
    }
    // You can use YAML deserialization to construct the gateway struct. You can also directly construct the struct. 
    gateway := &v1beta1.Gateway{}
    bytes, err := ioutil.ReadFile("gateway.yaml")
    if err != nil {
        fmt.Println("Failed to read the YAML file.")
        return
    }
    err = yaml.Unmarshal(bytes, &gateway)
    if err != nil {
        fmt.Println("Deserialization failed.")
        return
    }
    _, err = clientset.NetworkingV1beta1().Gateways(NAMESPACE).Create(context.TODO(), gateway, metav1.CreateOptions{})
    if err != nil {
        fmt.Println("Failed to create an Istio gateway.")
    }
    printAllGateways(clientset, NAMESPACE)
}

// Display all Istio gateways in the namespace. 
func printAllGateways(clientset *versionedclient.Clientset, namespace string) {
    gwList, err := clientset.NetworkingV1beta1().Gateways(namespace).List(context.TODO(), metav1.ListOptions{})
    if err != nil {
        fmt.Println("Failed to obtain Istio gateways.")
    }
    for _, gw := range gwList.Items {
        fmt.Println(gw)
    }
}
            

What to do next

After the virtual service and Istio gateway are created, you can visit http://{IP address of the ingress gateway service}/productpage in a browser to access the Bookinfo application.