app

package
v0.1.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Apr 16, 2023 License: MIT Imports: 15 Imported by: 0

Documentation

Index

Constants

View Source
const MinDisk = "1G"

MinDisk is the minimal disk size that can be assigned to a VM instance

View Source
const MinMemory = "2G"

MinMemory is the minimal acceptable memory for a VM instance

Variables

View Source
var (
	ErrMinControlNodes      = errors.New("control nodes number should be odd and minimum 3")
	ErrMinComputeNodes      = errors.New("compute nodes number should be minimum 1")
	ErrGetHomeDirectory     = errors.New("cannot get user's home directory")
	ErrLoadTemplate         = errors.New("cannot load template from embed")
	ErrCreateFile           = errors.New("cannot create new file")
	ErrParseTemplate        = errors.New("cannot parse template")
	ErrBase64Encode         = errors.New("cannot encode file to b64")
	ErrMemFormat            = errors.New("memory format should be like <number>K|M|G|k|m|g")
	ErrInvalidIPV4Address   = errors.New("invalid IP address provided")
	ErrVMNotRunning         = errors.New("VM is not running")
	ErrVMAlreadyExist       = errors.New("VM already exist")
	ErrCloudInitGeneration  = errors.New("unable to generate cloud init file")
	ErrClusterConfiguration = errors.New("wrong value in cluster configuration")
	ErrOddNumberCtrlNode    = errors.New("number of control nodes should be odd")
)

Application wide custom error types.

View Source
var Logger = slog.New(h)

Functions

func EncodeFileB64

func EncodeFileB64(path string) (string, error)

EncodeFileB64 encode a file in b64

func GenerateConfigCloudInit

func GenerateConfigCloudInit(cluster *Cluster) (string, error)

GenerateConfigCloudInit generates configuration files used to bootstrap the kubernetes nodes. This cloud init file is used to prepare the nodes for k8s.

func GenerateConfigKubeadm

func GenerateConfigKubeadm(cluster *Cluster) (string, error)

GenerateConfigKubeadm generates configuration files used to bootstrap the using KubeAdmin.

func GenerateConfigLB

func GenerateConfigLB(cluster *Cluster) (string, error)

GenerateConfigLB generates configuration files used to bootstrap the cluster load balancer.

func RunCmd

func RunCmd(vmName string, args []string) (string, error)

RunCmd run commands in a VM. It leverages multipass exec command. Returns the combined output (stderr + stdout) of the command and an error.

func SetLogLevel

func SetLogLevel(level LogLevel)

SetLogLevel sets the application log level dynamically. Defaut level in info.

func Transfer

func Transfer(vmName string, src string, dst string) error

Transfer transfers a file to the temp folder of an Instance. It leverages multipass transfer command. dest is the name of the dest file. It will appear in the VM as /tmp/dest

Types

type BootstrapConfig

type BootstrapConfig struct {
	// Base64 encoded node bootstrap script. Can leverage EncodeFileB64 function for that
	NodeBootstrapScript string
}

BootstrapConfig will hold k8s cluster independent configuration like nodes bootstrap scripts, LB config files and kubernetes version to deploy

type Cluster

type Cluster struct {
	Name              string
	PublicAPIEndpoint string
	PodSubnet         string
	// List of IPs for the compute node. Minimum 1. They are updated by vm create command. The reason is that at this time,
	// multipass does not support setting VM IP address at creation time. So it has to be retrieved after VM creation time
	// and the cluster info needs to be updated.
	CmpNodesIPs      []string
	CmpNodesMemory   string
	CmpNodesCores    int
	CmpNodesNumber   int
	CmpNodesDiskSize string
	// List of IPs for the control node. Minimum 3.
	CtrlNodesIPs      []string
	CtrlNodesMemory   string
	CtrlNodesCores    int
	CtrlNodesNumber   int
	CtrlNodesDiskSize string
	LBNodeMemory      string
	LBNodeCore        int
	LBNodeDiskSize    string
	// OS image
	Image string
	Mux   sync.Mutex
	// Bootstrap Tokens take the form of abcdef.0123456789abcdef.
	// More formally, they must match the regular expression [a-z0-9]{6}\.[a-z0-9]{16}.
	// They can also be created using the command kubeadm token create.
	BootstrapToken    string
	KubernetesCertKey string
}

Cluster is a struct representing a kubernetes cluster. The minimum number of control nodes is 3 and the minimum of compute nodes is 1.

func (*Cluster) AddComputeIP

func (cluster *Cluster) AddComputeIP(IP string)

AddComputeIP add the IP address of a newly created compute machine to compute nodes IP list. This is because multipass does not allow to set static IP on a node. So we have to fetch them dynamically and update the cluster configurations.

func (*Cluster) AddControlIP

func (cluster *Cluster) AddControlIP(IP string)

AddControlIP add the IP address of a newly created control machine to control nodes IP list. This is because multipass does not allow to set static IP on a node. So we have to fetch them dynamically and update the cluster configurations.

func (*Cluster) CreateKubeVMs

func (cluster *Cluster) CreateKubeVMs(cloudInitPath string, numWorkers int)

CreateKubeVMs creates a VM for the kubernetes cluster. parallel param is the number of vms to create in parallel. This method use the worker concurrency pattern to create and run VMs. Creating a high number of VMs will incur network traffic and hypervisor cpu load, so the number of workers should be planned wisely.

func (*Cluster) CreateLB

func (cluster *Cluster) CreateLB(cloudInitPath string, lbConfPath string) (*Instance, error)

CreateLB creates the LB associated with the cluster and run it. After running this method, you'll have a LB deployed and ready to server traffic. Returns a pointer to an instance of VM and an error. If the VM already exist, an error will be thrown but the VM instance that will be returned will be valid.

func (*Cluster) GetCertHash

func (cluster *Cluster) GetCertHash() (string, error)

GetCertHash retrieves the bootstrap CA certificate hash to populate kubeadm yaml configuration file. The hash is generated and stored in the first control node at the path /tmp/kubeadm-cert-hash. The hash is directly set as a Cluster attribute. This should be run after the control-0 VM is alive.

func (*Cluster) GetControlVM

func (cluster *Cluster) GetControlVM(vmName string, cloudInitPath string) (*Instance, error)

GetControlVM return a VM instance populated with a control node parameters. This is useful to generate the configuration of an existing vm instance to apply specific instance related methods on them (for instance, file transfer)

func (*Cluster) GetMasterJoinCMD

func (cluster *Cluster) GetMasterJoinCMD() ([]string, error)

GetMasterJoinCMD build the master join command to join other master nodes

func (*Cluster) GetWorkerJoinCMD

func (cluster *Cluster) GetWorkerJoinCMD() ([]string, error)

GetWorkerJoinCMD build the master join command to join other master nodes

func (*Cluster) InstallCNI

func (cluster *Cluster) InstallCNI() error

InstallCNI installs the cluster CNI. For now, only cilium is supported and is directly hardcoded in the tool.

func (*Cluster) KubeInit

func (cluster *Cluster) KubeInit(remoteHomeDir string) error

KubeInit runs kubeadm init cmd from control plane node0

func (*Cluster) ValidateConfig

func (cluster *Cluster) ValidateConfig() error

ValidateConfig checks if cluster configuration is valid. It checks Disk sizes, memory sizes, and validity of IP addresses @TODO; add more validation (duplicate IPs, PodSubnet, memory and disk sizes, number of nodes)

type Instance

type Instance struct {
	Cores int
	// Memory in bytes. Could be prefixed with K, M or G.
	// Should be more than min memory
	Memory string
	// Disk space in bytes. Could be prefixed with K, M or G.
	// Should be more than min disk
	Disk string
	Name string
	// CloudInitFile is a path to a cloud init file. Not mandatory.
	CloudInitFile string
	// Name of the cluster in which this instance belongs to
	Cluster string
	// Image is the name of the image to use, on 20.04 works with our k8s script for now
	Image string
}

Instance represents a multipass VM, typically created using the launch subcommand. Try multipass launch --help for more info.

func NewInstanceConfig

func NewInstanceConfig(cores int, memory string, disk string, image string, name string, cloudinit string) (*Instance, error)

NewInstanceConfig returns a valid configuration of an instance or an error cloudinit is the path of a cloud init script to pass to the method

func (*Instance) Create

func (vm *Instance) Create() error

Create creates a multipass instance

func (*Instance) Exist

func (vm *Instance) Exist() bool

Exist checks whether a VM is already created on the host

func (*Instance) GetIP

func (vm *Instance) GetIP() (string, error)

GetIP gets the IP address of an instance

func (*Instance) IsRunning

func (vm *Instance) IsRunning() bool

IsRunning checks whether a VM is running

func (*Instance) IsStopped

func (vm *Instance) IsStopped() bool

IsStopped checks whether a VM is stopped

type LogLevel

type LogLevel int
const (
	Info LogLevel = iota
	Warn
	Error
	Debug
)

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL
JackTT - Gopher 🇻🇳