2022-10-03 16:56:47 +03:00
package k8s
import (
"context"
"net/http"
"strings"
2023-03-24 17:09:30 +03:00
"repository.basistech.ru/BASIS/decort-golang-sdk/internal/validators"
2022-10-03 16:56:47 +03:00
)
2023-10-25 17:37:18 +03:00
// CreateRequest struct to create kubernetes cluster
2022-10-03 16:56:47 +03:00
type CreateRequest struct {
2022-12-22 17:56:47 +03:00
// Name of Kubernetes cluster
// Required: true
2023-03-24 17:09:30 +03:00
Name string ` url:"name" json:"name" validate:"required" `
2022-12-22 17:56:47 +03:00
// Resource Group ID for cluster placement
// Required: true
2023-03-24 17:09:30 +03:00
RGID uint64 ` url:"rgId" json:"rgId" validate:"required" `
2022-12-22 17:56:47 +03:00
// ID of Kubernetes catalog item (k8sci) for cluster
// Required: true
2023-03-24 17:09:30 +03:00
K8SCIID uint64 ` url:"k8ciId" json:"k8ciId" validate:"required" `
2022-12-22 17:56:47 +03:00
// Name for first worker group created with cluster
// Required: true
2023-08-09 19:33:50 +03:00
WorkerGroupName string ` url:"workerGroupName" json:"workerGroupName" validate:"required,workerGroupName" `
2022-12-22 17:56:47 +03:00
2023-04-28 11:46:58 +03:00
// Network plugin
2024-03-06 16:50:27 +03:00
// Must be one of these values: flannel, weavenet, calico
2023-04-28 11:46:58 +03:00
// Required: true
NetworkPlugin string ` url:"networkPlugin" json:"networkPlugin" validate:"required,networkPlugin" `
2025-08-29 12:51:25 +03:00
// ID of the chosen storage policy
// Required: true
StoragePolicyID uint64 ` url:"storage_policy_id" json:"storage_policy_id" validate:"required" `
2023-01-23 15:39:41 +03:00
// ID of SEP to create boot disks for master nodes. Uses images SEP ID if not set
// Required: false
2023-03-01 19:05:53 +03:00
MasterSEPID uint64 ` url:"masterSepId,omitempty" json:"masterSepId,omitempty" `
2023-01-23 15:39:41 +03:00
// Pool to use if master SEP ID is set, can be also empty if needed to be chosen by system
// Required: false
2023-03-01 19:05:53 +03:00
MasterSEPPool string ` url:"masterSepPool,omitempty" json:"masterSepPool,omitempty" `
2023-01-23 15:39:41 +03:00
// ID of SEP to create boot disks for default worker nodes group. Uses images SEP ID if not set
// Required: false
2023-03-01 19:05:53 +03:00
WorkerSEPID uint64 ` url:"workerSepId,omitempty" json:"workerSepId,omitempty" `
2023-01-23 15:39:41 +03:00
// Pool to use if worker SEP ID is set, can be also empty if needed to be chosen by system
// Required: false
2023-03-01 19:05:53 +03:00
WorkerSEPPool string ` url:"workerSepPool,omitempty" json:"workerSepPool,omitempty" `
2023-01-23 15:39:41 +03:00
2022-12-22 17:56:47 +03:00
// List of strings with labels for default worker group
// i.e: ["label1=value1", "label2=value2"]
// Required: false
2023-03-01 19:05:53 +03:00
Labels [ ] string ` url:"labels,omitempty" json:"labels,omitempty" `
2022-12-22 17:56:47 +03:00
// List of strings with taints for default worker group
// i.e: ["key1=value1:NoSchedule", "key2=value2:NoExecute"]
// Required: false
2023-03-01 19:05:53 +03:00
Taints [ ] string ` url:"taints,omitempty" json:"taints,omitempty" `
2022-12-22 17:56:47 +03:00
// List of strings with annotations for worker group
// i.e: ["key1=value1", "key2=value2"]
// Required: false
2023-03-01 19:05:53 +03:00
Annotations [ ] string ` url:"annotations,omitempty" json:"annotations,omitempty" `
2022-12-22 17:56:47 +03:00
// Number of master nodes to create
// Required: false
2023-03-01 19:05:53 +03:00
MasterNum uint ` url:"masterNum,omitempty" json:"masterNum,omitempty" `
2022-12-22 17:56:47 +03:00
// Master node CPU count
// Required: false
2023-03-01 19:05:53 +03:00
MasterCPU uint ` url:"masterCpu,omitempty" json:"masterCpu,omitempty" `
2022-12-22 17:56:47 +03:00
// Master node RAM volume in MB
// Required: false
2024-04-16 14:26:06 +03:00
MasterRAM uint64 ` url:"masterRam,omitempty" json:"masterRam,omitempty" `
2022-12-22 17:56:47 +03:00
// Master node boot disk size in GB If 0 is specified, size is defined by the OS image size
// Required: false
2023-03-01 19:05:53 +03:00
MasterDisk uint ` url:"masterDisk,omitempty" json:"masterDisk,omitempty" `
2022-12-22 17:56:47 +03:00
// Number of worker nodes to create in default worker group
// Required: false
2023-03-01 19:05:53 +03:00
WorkerNum uint ` url:"workerNum,omitempty" json:"workerNum,omitempty" `
2022-12-22 17:56:47 +03:00
// Worker node CPU count
// Required: false
2023-03-01 19:05:53 +03:00
WorkerCPU uint ` url:"workerCpu,omitempty" json:"workerCpu,omitempty" `
2022-12-22 17:56:47 +03:00
// Worker node RAM volume in MB
// Required: false
2024-04-16 14:26:06 +03:00
WorkerRAM uint64 ` url:"workerRam,omitempty" json:"workerRam,omitempty" `
2022-12-22 17:56:47 +03:00
// Worker node boot disk size in GB. If 0 is specified, size is defined by the OS image size
// Required: false
2023-03-01 19:05:53 +03:00
WorkerDisk uint ` url:"workerDisk,omitempty" json:"workerDisk,omitempty" `
2022-12-22 17:56:47 +03:00
// ID of the external network to connect load balancer and cluster ViNS. If 0 is specified, external network selects automatically to
// Required: false
2023-03-01 19:05:53 +03:00
ExtNetID uint64 ` url:"extnetId,omitempty" json:"extnetId,omitempty" `
2022-12-22 17:56:47 +03:00
2023-09-24 12:11:31 +03:00
// ID of the ViNS to connect k8s cluster. If nothing is specified, ViNS will be created automatically
// Required: false
VinsId uint64 ` url:"vinsId,omitempty" json:"vinsId,omitempty" `
2022-12-22 17:56:47 +03:00
// Create Kubernetes cluster with masters nodes behind load balancer if true.
// Otherwise give all cluster nodes direct external addresses from selected ExtNet
// Required: false
2023-06-19 15:06:31 +03:00
WithLB bool ` url:"withLB" json:"withLB" `
2022-12-22 17:56:47 +03:00
2023-09-24 12:11:31 +03:00
// Custom sysctl values for Load Balancer instance. Applied on boot
// Required: false
2024-04-16 14:26:06 +03:00
LbSysctlParams [ ] map [ string ] interface { } ` url:"lbSysctlParams,omitempty" json:"lbSysctlParams,omitempty" `
2023-09-24 12:11:31 +03:00
// Use Highly Available schema for LB deploy
// Required: false
2023-09-28 19:34:23 +03:00
HighlyAvailable bool ` url:"highlyAvailableLB,omitempty" json:"highlyAvailableLB,omitempty" `
2023-09-24 12:11:31 +03:00
// Optional extra Subject Alternative Names (SANs) to use for the API Server serving certificate. Can be both IP addresses and DNS names
// Required: false
AdditionalSANs [ ] string ` url:"additionalSANs,omitempty" json:"additionalSANs,omitempty" `
// Is used to define settings and actions that should be performed before any other component in the cluster starts.
// It allows you to configure things like node registration, network setup, and other initialization tasks. insert a valid JSON string with all levels of nesting
// Required: false
InitConfiguration string ` url:"initConfiguration,omitempty" json:"initConfiguration,omitempty" `
// Is used to define global settings and configurations for the entire cluster.
// It includes parameters such as cluster name, DNS settings, authentication methods, and other cluster-wide configurations.
// Insert a valid JSON string with all levels of nesting
// Required: false
ClusterConfiguration string ` url:"clusterConfiguration,omitempty" json:"clusterConfiguration,omitempty" `
// Is used to configure the behavior and settings of the Kubelet, which is the primary node agent that runs on each node in the cluster.
// It includes parameters such as node IP address, resource allocation, pod eviction policies, and other Kubelet-specific configurations.
// Insert a valid JSON string with all levels of nesting
// Required: false
KubeletConfiguration string ` url:"kubeletConfiguration,omitempty" json:"kubeletConfiguration,omitempty" `
// Is used to configure the behavior and settings of the Kube-proxy, which is responsible for network proxying and load balancing within the cluster.
// It includes parameters such as proxy mode, cluster IP ranges, and other Kube-proxy specific configurations.
// Insert a valid JSON string with all levels of nesting
// Required: false
KubeProxyConfiguration string ` url:"kubeProxyConfiguration,omitempty" json:"kubeProxyConfiguration,omitempty" `
// Is used to configure the behavior and settings for joining a node to a cluster.
// It includes parameters such as the cluster's control plane endpoint, token, and certificate key. insert a valid JSON string with all levels of nesting
// Required: false
JoinConfiguration string ` url:"joinConfiguration,omitempty" json:"joinConfiguration,omitempty" `
2022-12-22 17:56:47 +03:00
// Text description of this Kubernetes cluster
// Required: false
2023-03-01 19:05:53 +03:00
Description string ` url:"desc,omitempty" json:"desc,omitempty" `
2023-09-24 12:11:31 +03:00
// Meta data for working group computes, format YAML "user_data": 1111
// Required: false
UserData string ` url:"userData,omitempty" json:"userData,omitempty" `
// Use only selected ExtNet for infrastructure connections
// Required: false
ExtNetOnly bool ` url:"extnetOnly,omitempty" json:"extnetOnly,omitempty" `
2023-09-25 19:11:33 +03:00
2023-09-24 14:41:21 +03:00
// Insert ssl certificate in x509 pem format
// Required: false
2023-09-25 19:11:33 +03:00
OidcCertificate string ` url:"oidcCertificate,omitempty" json:"oidcCertificate,omitempty" `
2024-11-12 12:51:21 +03:00
// Type of the emulated system, Q35 or i440fx
// Required: false
2025-12-08 16:16:35 +03:00
// Default: Q35
2024-11-22 12:09:50 +03:00
Chipset string ` url:"chipset,omitempty" json:"chipset,omitempty" validate:"omitempty,chipset" `
2025-07-15 17:39:18 +03:00
// Zone ID
// Required: false
ZoneID uint64 ` url:"zoneId,omitempty" json:"zoneId,omitempty" `
2022-10-03 16:56:47 +03:00
}
2024-04-16 14:26:06 +03:00
// GetRAM returns RAM field values
func ( r CreateRequest ) GetRAM ( ) map [ string ] uint64 {
res := make ( map [ string ] uint64 , 2 )
res [ "MasterRAM" ] = r . MasterRAM
res [ "WorkerRAM" ] = r . WorkerRAM
return res
}
2023-09-24 12:11:31 +03:00
2022-12-22 17:56:47 +03:00
// Create creates a new Kubernetes cluster in the specified Resource Group
2022-10-03 16:56:47 +03:00
func ( k8s K8S ) Create ( ctx context . Context , req CreateRequest ) ( string , error ) {
2023-03-24 17:09:30 +03:00
err := validators . ValidateRequest ( req )
2022-10-03 16:56:47 +03:00
if err != nil {
2023-10-25 17:37:18 +03:00
return "" , validators . ValidationErrors ( validators . GetErrors ( err ) )
2022-10-03 16:56:47 +03:00
}
url := "/cloudapi/k8s/create"
2024-03-14 14:52:56 +03:00
res , err := k8s . client . DecortApiCallMP ( ctx , http . MethodPost , url , req )
2022-10-03 16:56:47 +03:00
if err != nil {
return "" , err
}
2022-12-22 17:56:47 +03:00
result := strings . ReplaceAll ( string ( res ) , "\"" , "" )
2022-10-03 16:56:47 +03:00
2022-12-22 17:56:47 +03:00
return result , nil
2022-10-03 16:56:47 +03:00
}