cluster

package
v0.19.0 Latest Latest
Warning

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

Go to latest
Published: Jan 17, 2025 License: Apache-2.0 Imports: 10 Imported by: 0

Documentation

Overview

Package cluster provides the ability to retrieve and manage Kubernetes clusters through the MKS V1 API.

Example of getting a single cluster referenced by its id

mksCluster, _, err := cluster.Get(ctx, mksClient, clusterID)
if err != nil {
  log.Fatal(err)
}
fmt.Printf("%+v\n", mksCluster)

Example of getting all clusters

mksClusters, _, err := cluster.List(ctx, mksClient)
if err != nil {
  log.Fatal(err)
}
for _, mksCluster := range mksClusters {
  fmt.Printf("%+v\n", mksCluster)
}

Example of creating a new cluster

createOpts := &cluster.CreateOpts{
  Name:        "test-cluster-0",
  KubeVersion: "1.15.7",
  Region:      "ru-1",
  Nodegroups: []*nodegroup.CreateOpts{
    {
      Count:            1,
      CPUs:             1,
      RAMMB:            2048,
      VolumeGB:         10,
      VolumeType:       "fast.ru-3a",
      KeypairName:      "ssh-key",
      AvailabilityZone: "ru-3a",
      Labels: map[string]string{
        "label-key0": "label-value0",
        "label-key1": "label-value1",
        "label-key2": "label-value2",
      },
      Taints: []nodegroup.Taint{
        {
          Key:    "test-key-0",
          Value:  "test-value-0",
          Effect: nodegroup.NoScheduleEffect,
        },
      },
    },
  },
}
mksCluster, _, err := cluster.Create(ctx, mksClient, createOpts)
if err != nil {
  log.Fatal(err)
}
fmt.Printf("%+v\n", mksCluster)

Example of updating an existing cluster

updateOpts := &cluster.UpdateOpts{
  MaintenanceWindowStart: "07:00:00",
  KubernetesOptions: &cluster.KubernetesOptions{
    EnablePodSecurityPolicy: false,
    FeatureGates: []string{
      "TTLAfterFinished",
    },
    AdmissionControllers: []string{
        "NamespaceLifecycle",
    },
  },
}
mksCluster, _, err := cluster.Update(ctx, mksClient, clusterID, updateOpts)
if err != nil {
  log.Fatal(err)
}
fmt.Printf("%+v\n", mksCluster)

Example of deleting a single cluster

_, err := cluster.Delete(ctx, mksClient, clusterID)
if err != nil {
  log.Fatal(err)
}

Example of getting a kubeconfig referenced by cluster id

kubeconfig, _, err := cluster.GetKubeconfig(ctx, mksClient, clusterID)
if err != nil {
  log.Fatal(err)
}
fmt.Print(string(kubeconfig))

Example of getting fields from Kubeconfig referenced by cluster id

parsedKubeconfig, _, err := cluster.GetParsedKubeconfig(ctx, mksClient, clusterID)
if err != nil {
  log.Fatal(err)
}
fmt.Println("Server IP:", string(parsedKubeconfig.Server))
fmt.Println("Cluster CA:", string(parsedKubeconfig.ClusterCA))
fmt.Println("Client cert:", string(parsedKubeconfig.ClientCert))
fmt.Println("Client key:", string(parsedKubeconfig.ClientKey))
fmt.Println("Raw kubeconfig:", string(parsedKubeconfig.KubeconfigRaw))

Example of rotating certificates by cluster id

_, err := cluster.RotateCerts(ctx, mksClient, clusterID)
if err != nil {
  log.Fatal(err)
}

Example of upgrading Kubernetes patch version by cluster id

mksCluster, _, err := cluster.UpgradePatchVersion(ctx, mksClient, clusterID)
if err != nil {
  log.Fatal(err)
}
fmt.Printf("%+v\n", mksCluster)

Example of upgrading Kubernetes minor version by cluster id

mksCluster, _, err := cluster.UpgradeMinorVersion(ctx, mksClient, clusterID)
if err != nil {
  log.Fatal(err)
}
fmt.Printf("%+v\n", mksCluster)

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Delete

func Delete(ctx context.Context, client *v1.ServiceClient, clusterID string) (*v1.ResponseResult, error)

Delete deletes a single cluster by its id.

func GetKubeconfig

func GetKubeconfig(ctx context.Context, client *v1.ServiceClient, clusterID string) ([]byte, *v1.ResponseResult, error)

GetKubeconfig returns a kubeconfig by cluster id.

func RotateCerts

func RotateCerts(ctx context.Context, client *v1.ServiceClient, clusterID string) (*v1.ResponseResult, error)

RotateCerts requests a rotation of cluster certificates by cluster id.

Types

type AuditLogs added in v0.16.0

type AuditLogs struct {
	// Enabled indicates whether kubernetes audit logs should be collected
	// and pushed into SIEM system (e.g. logstash).
	// False by default.
	Enabled bool `json:"enabled"`

	// SecretName contains name of the kubernetes secret in namespace kube-system
	// with credentials of SIEM system where logs should be pushed.
	// Fields of the secret: host, port, username (optional), password (optional), ca.crt (optional).
	// This field is optional. By default, used "mks-audit-logs".
	// Secret name should be as a DNS subdomain name as defined in RFC 1123.
	// More: https://kubernetes.io/docs/concepts/overview/working-with-objects/names/#dns-subdomain-names
	SecretName string `json:"secret_name"`
}

type CreateOpts

type CreateOpts struct {
	// Name represent the needed name of the cluster.
	// It shouldn't contain more than 32 symbols and can contain latin letters
	// with numbers and hyphens and start with a letter or a number.
	Name string `json:"name,omitempty"`

	// NetworkID contains a reference to the network of the cluster.
	// It can be set in cases where network is pre-created.
	NetworkID string `json:"network_id,omitempty"`

	// SubnetID contains a reference to the subnet of the cluster.
	// It can be set in cases where subnet is pre-created.
	SubnetID string `json:"subnet_id,omitempty"`

	// KubeVersion represents the needed Kubernetes version of the cluster.
	// It should be in x.y.z format.
	KubeVersion string `json:"kube_version,omitempty"`

	// Region represents the needed region.
	Region string `json:"region,omitempty"`

	// Nodegroups contains groups of nodes with their parameters.
	Nodegroups []*nodegroup.CreateOpts `json:"nodegroups,omitempty"`

	// AdditionalSoftware represents parameters of additional software that can be installed
	// in the Kubernetes cluster.
	AdditionalSoftware map[string]interface{} `json:"additional_software,omitempty"`

	// MaintenanceWindowStart represents UTC time of when the cluster will start its maintenance tasks.
	// It should be in hh:mm:ss format if provided.
	MaintenanceWindowStart string `json:"maintenance_window_start,omitempty"`

	// EnableAutorepair reflects if worker nodes are allowed to be reinstalled automatically
	// in case of their unavailability or unhealthiness. Enabled by default.
	EnableAutorepair *bool `json:"enable_autorepair,omitempty"`

	// EnablePatchVersionAutoUpgrade specifies if Kubernetes patch version of the cluster is allowed to be upgraded
	// automatically. Enabled by default.
	EnablePatchVersionAutoUpgrade *bool `json:"enable_patch_version_auto_upgrade,omitempty"`

	// Zonal specifies that only a single zonal master will be created.
	// It is needed if highly available control-plane is not required.
	Zonal *bool `json:"zonal,omitempty"`

	// KubernetesOptions represents additional k8s options such as pod security policy,
	// feature gates, admission controllers, audit logs and oidc.
	KubernetesOptions *KubernetesOptions `json:"kubernetes_options,omitempty"`

	// PrivateKubeAPI specifies if kube API should be available from the Internet or not.
	// By default false so kube API available from the Internet.
	PrivateKubeAPI *bool `json:"private_kube_api,omitempty"`
}

CreateOpts represents options for the cluster Create request.

type KubeconfigFields added in v0.9.0

type KubeconfigFields struct {
	ClusterCA     string
	Server        string
	ClientCert    string
	ClientKey     string
	KubeconfigRaw string
}

KubeconfigFields is a struct that contains Kubeconfigs parsed fields and raw kubeconfig.

func GetParsedKubeconfig added in v0.9.0

func GetParsedKubeconfig(ctx context.Context, client *v1.ServiceClient, clusterID string) (*KubeconfigFields, *v1.ResponseResult, error)

GetParsedKubeconfig is a small helper function to get KubeconfigFields struct.

type KubernetesOptions added in v0.5.0

type KubernetesOptions struct {
	// EnablePodSecurityPolicy indicates if PodSecurityPolicy admission controller
	// must be turned on/off.
	EnablePodSecurityPolicy bool `json:"enable_pod_security_policy"`

	// FeatureGates represents feature gates that should be enabled.
	FeatureGates []string `json:"feature_gates"`

	// AdmissionControllers represents admission controllers that should be enabled.
	AdmissionControllers []string `json:"admission_controllers"`

	// AuditLogs represents configuration of kubernetes audit logs in the cluster.
	// More: https://docs.selectel.ru/en/cloud/managed-kubernetes/clusters/logs/#configure-integration-with-external-system
	AuditLogs AuditLogs `json:"audit_logs"`

	// OIDC represents configuration to enable authorization via OpenID Connect in kubernetes cluster.
	// More: https://kubernetes.io/docs/reference/access-authn-authz/authentication/#openid-connect-tokens
	OIDC OIDC `json:"oidc"`
}

KubernetesOptions represents additional k8s options such as pod security policy, feature gates, admission controllers, audit logs and oidc.

type OIDC added in v0.18.0

type OIDC struct {
	// Enabled indicates whether OIDC should be turned on in the cluster.
	// False by default.
	Enabled bool `json:"enabled"`

	// ProviderName represents custom user defined name of the provider. It is not used in the cluster directly.
	// It is required when enabled = true.
	ProviderName string `json:"provider_name"`

	// IssuerURL represents URL of the provider that allows the API server to discover public signing keys.
	// Will be placed in `--oidc-issuer-url` flag.
	// It is required when enabled = true.
	IssuerURL string `json:"issuer_url"`

	// ClientID represents required client id that all tokens must be issued for.
	// Will be placed in `--oidc-client-id` flag.
	// It is required when enabled = true.
	ClientID string `json:"client_id"`

	// UsernameClaim represents optional JWT claim to use as the username. By default, `sub`.
	// Will be placed in `--oidc-username-claim` flag.
	UsernameClaim string `json:"username_claim"`

	// GroupsClaim represents optional JWT claim to use as the user's group. By default, `groups`.
	// Will be placed in `--oidc-groups-claim` flag.
	GroupsClaim string `json:"groups_claim"`
}

OIDC represents parameters to connect client's OIDC provider with kubernetes.

type Status

type Status string

Status represents custom type for various cluster statuses.

const (
	StatusActive                             Status = "ACTIVE"
	StatusPendingCreate                      Status = "PENDING_CREATE"
	StatusPendingUpdate                      Status = "PENDING_UPDATE"
	StatusPendingUpgrade                     Status = "PENDING_UPGRADE"
	StatusPendingRotateCerts                 Status = "PENDING_ROTATE_CERTS"
	StatusPendingDelete                      Status = "PENDING_DELETE"
	StatusPendingResize                      Status = "PENDING_RESIZE"
	StatusPendingNodeReinstall               Status = "PENDING_NODE_REINSTALL"
	StatusPendingUpgradePatchVersion         Status = "PENDING_UPGRADE_PATCH_VERSION"
	StatusPendingUpgradeMinorVersion         Status = "PENDING_UPGRADE_MINOR_VERSION"
	StatusPendingUpdateNodegroup             Status = "PENDING_UPDATE_NODEGROUP"
	StatusPendingUpgradeMastersConfiguration Status = "PENDING_UPGRADE_MASTERS_CONFIGURATION"
	StatusPendingUpgradeClusterConfiguration Status = "PENDING_UPGRADE_CLUSTER_CONFIGURATION"
	StatusMaintenance                        Status = "MAINTENANCE"
	StatusError                              Status = "ERROR"
	StatusUnknown                            Status = "UNKNOWN"
)

type UpdateOpts added in v0.2.0

type UpdateOpts struct {
	// MaintenanceWindowStart represents UTC time of when the cluster will start its maintenance tasks.
	// It should be in hh:mm:ss format if provided.
	MaintenanceWindowStart string `json:"maintenance_window_start,omitempty"`

	// EnableAutorepair reflects if worker nodes are allowed to be reinstalled automatically
	// in case of their unavailability or unhealthiness. Enabled by default.
	EnableAutorepair *bool `json:"enable_autorepair,omitempty"`

	// EnablePatchVersionAutoUpgrade specifies if Kubernetes patch version of the cluster is allowed to be upgraded
	// automatically. Enabled by default.
	EnablePatchVersionAutoUpgrade *bool `json:"enable_patch_version_auto_upgrade,omitempty"`

	// KubernetesOptions represents additional k8s options such as pod security policy,
	// feature gates, admission controllers, audit logs and oidc.
	KubernetesOptions *KubernetesOptions `json:"kubernetes_options,omitempty"`
}

UpdateOpts represents options for the cluster Update request.

type View

type View struct {
	// ID is the identifier of the cluster.
	ID string `json:"id"`

	// CreatedAt is the timestamp in UTC timezone of when the cluster has been created.
	CreatedAt *time.Time `json:"created_at"`

	// UpdatedAt is the timestamp in UTC timezone of when the cluster has been updated.
	UpdatedAt *time.Time `json:"updated_at"`

	// Name represents the name of the cluster.
	Name string `json:"name"`

	// Status represents current status of the cluster.
	Status Status `json:"-"`

	// ProjectID contains reference to the project of the cluster.
	ProjectID string `json:"project_id"`

	// NetworkID contains reference to the network of the cluster.
	NetworkID string `json:"network_id"`

	// SubnetID contains reference to the subnet of the cluster.
	SubnetID string `json:"subnet_id"`

	// KubeAPIIP represents the IP of the Kubernetes API.
	KubeAPIIP string `json:"kube_api_ip"`

	// KubeVersion represents the current Kubernetes version of the cluster.
	KubeVersion string `json:"kube_version"`

	// Region represents the region of where the cluster is located.
	Region string `json:"region"`

	// AdditionalSoftware represents information about additional software installed in the cluster.
	AdditionalSoftware map[string]interface{} `json:"additional_software"`

	// PKITreeUpdatedAt represents the timestamp in UTC timezone of when the PKI-tree of the cluster
	// has been updated.
	PKITreeUpdatedAt *time.Time `json:"pki_tree_updated_at"`

	// MaintenanceWindowStart represents UTC time in "hh:mm:ss" format of when the cluster will start its
	// maintenance tasks.
	MaintenanceWindowStart string `json:"maintenance_window_start"`

	// MaintenanceWindowEnd represents UTC time in "hh:mm:ss" format of when the cluster will end its
	// maintenance tasks.
	MaintenanceWindowEnd string `json:"maintenance_window_end"`

	// MaintenanceLastStart is the timestamp in UTC timezone of the last cluster maintenance start.
	MaintenanceLastStart *time.Time `json:"maintenance_last_start"`

	// EnableAutorepair reflects if worker nodes are allowed to be reinstalled automatically
	// in case of their unavailability or unhealthiness.
	EnableAutorepair bool `json:"enable_autorepair"`

	// EnablePatchVersionAutoUpgrade specifies if Kubernetes patch version of the cluster is allowed to be upgraded
	// automatically.
	EnablePatchVersionAutoUpgrade bool `json:"enable_patch_version_auto_upgrade"`

	// Zonal specifies that cluster has only a single master and that
	// control-plane is not in highly available mode.
	Zonal bool `json:"zonal"`

	// KubernetesOptions represents additional k8s options such as pod security policy,
	// feature gates, admission controllers, audit logs and oidc.
	KubernetesOptions *KubernetesOptions `json:"kubernetes_options,omitempty"`

	PrivateKubeAPI bool `json:"private_kube_api"`
}

View represents an unmarshalled cluster body from an API response.

func Create

func Create(ctx context.Context, client *v1.ServiceClient, opts *CreateOpts) (*View, *v1.ResponseResult, error)

Create requests a creation of a new cluster.

func Get

func Get(ctx context.Context, client *v1.ServiceClient, clusterID string) (*View, *v1.ResponseResult, error)

Get returns a single cluster by its id.

func List

func List(ctx context.Context, client *v1.ServiceClient) ([]*View, *v1.ResponseResult, error)

List gets a list of all clusters.

func Update added in v0.2.0

func Update(ctx context.Context, client *v1.ServiceClient, clusterID string, opts *UpdateOpts) (*View, *v1.ResponseResult, error)

Update requests an update of an existing cluster.

func UpgradeMinorVersion added in v0.4.0

func UpgradeMinorVersion(ctx context.Context, client *v1.ServiceClient, clusterID string) (*View, *v1.ResponseResult, error)

UpgradeMinorVersion requests a Kubernetes minor version upgrade by cluster id.

func UpgradePatchVersion added in v0.2.0

func UpgradePatchVersion(ctx context.Context, client *v1.ServiceClient, clusterID string) (*View, *v1.ResponseResult, error)

UpgradePatchVersion requests a Kubernetes patch version upgrade by cluster id.

func (*View) UnmarshalJSON

func (result *View) UnmarshalJSON(b []byte) error

Directories

Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL