Skip to content

Go SDK for Managed Kubernetes Service

License

Notifications You must be signed in to change notification settings

selectel/mks-go

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

93 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

mks-go: Go SDK for Managed Kubernetes Service

Go.dev reference Go Report Card Build Status Coverage Status

Package mks-go provides Go SDK to work with the Selectel Managed Kubernetes Service.

Documentation

The Go library documentation is available at go.dev.

What this library is capable of

You can use this library to work with the following objects of the Selectel Managed Kubernetes Service:

Getting started

Installation

You can install needed mks-go packages via go get command:

go get github.com/selectel/mks-go/pkg/v1/cluster github.com/selectel/mks-go/pkg/v1/task

Authentication

To work with the Selectel Managed Kubernetes Service API you first need to:

Endpoints

Selectel Managed Kubernetes Service currently has the following API endpoints:

URL Region
https://ru-1.mks.selcloud.ru/v1 ru-1
https://ru-2.mks.selcloud.ru/v1 ru-2
https://ru-3.mks.selcloud.ru/v1 ru-3
https://ru-7.mks.selcloud.ru/v1 ru-7
https://ru-8.mks.selcloud.ru/v1 ru-8
https://ru-9.mks.selcloud.ru/v1 ru-9
https://uz-1.mks.selcloud.ru/v1 uz-1

You can also retrieve all available API endpoints from the Identity catalog.

Usage example

package main

import (
	"context"
	"fmt"
	"log"

	v1 "github.com/selectel/mks-go/pkg/v1"
	"github.com/selectel/mks-go/pkg/v1/cluster"
	"github.com/selectel/mks-go/pkg/v1/kubeversion"
	"github.com/selectel/mks-go/pkg/v1/nodegroup"
	"github.com/selectel/mks-go/pkg/v1/task"
)

func main() {
	// Token to work with Selectel Cloud project.
	token := "gAAAAABeVNzu-..."

	// MKS endpoint to work with.
	endpoint := "https://ru-3.mks.selcloud.ru/v1"

	// Initialize the MKS V1 client.
	mksClient := v1.NewMKSClientV1(token, endpoint)

	// Prepare empty context.
	ctx := context.Background()

	// Get supported Kubernetes versions.
	kubeVersions, _, err := kubeversion.List(ctx, mksClient)
	if err != nil {
		log.Fatal(err)
	}
	if len(kubeVersions) == 0 {
		log.Fatal("There are no available Kubernetes versions")
	}

	// Use the first version in list.
	kubeVersion := kubeVersions[0]

	// Nodegroup with nodes based on network volumes for root partition.
	firstNodegroup := &nodegroup.CreateOpts{
		Count:            3,
		CPUs:             1,
		RAMMB:            2048,
		VolumeGB:         50,
		VolumeType:       "fast.ru-3a",
		AvailabilityZone: "ru-3a",
	}

	// Nodegroup with nodes based on local volumes for root partition.
	secondNodegroup := &nodegroup.CreateOpts{
		Count:            2,
		CPUs:             2,
		RAMMB:            4096,
		VolumeGB:         20,
		LocalVolume:      true,
		AvailabilityZone: "ru-3a",
	}

	// Build final options for a new cluster.
	createOpts := &cluster.CreateOpts{
		Name:        "test-cluster",
		KubeVersion: kubeVersion.Version,
		Region:      "ru-3",
		Nodegroups: []*nodegroup.CreateOpts{
			firstNodegroup,
			secondNodegroup,
		},
	}

	// Create a cluster.
	newCluster, _, err := cluster.Create(ctx, mksClient, createOpts)
	if err != nil {
		log.Fatal(err)
	}

	// Print cluster fields.
	fmt.Printf("Created cluster: %+v\n", newCluster)

	// Get cluster tasks.
	tasks, _, err := task.List(ctx, mksClient, newCluster.ID)
	if err != nil {
		log.Fatal(err)
	}

	// Print cluster tasks.
	for _, t := range tasks {
		fmt.Printf("Cluster task: %+v\n", t)
	}
}