0

Create a New Kubernetes Cluster

by
Published Dec 20, 2024

To create a new Kubernetes cluster, send a POST request to `/v2/kubernetes/clusters`. The request must contain at least one node pool with at least one worker. The request may contain a maintenance window policy describing a time period when disruptive maintenance tasks may be carried out. Omitting the policy implies that a window will be chosen automatically. See [here](https://docs.digitalocean.com/products/kubernetes/how-to/upgrade-cluster/) for details.

Script digitalocean Verified

The script

Submitted by hugo697 Bun
Verified 536 days ago
1
//native
2
type Digitalocean = {
3
  token: string;
4
};
5
/**
6
 * Create a New Kubernetes Cluster
7
 * To create a new Kubernetes cluster, send a POST request to
8
`/v2/kubernetes/clusters`. The request must contain at least one node pool
9
with at least one worker.
10

11
The request may contain a maintenance window policy describing a time period
12
when disruptive maintenance tasks may be carried out. Omitting the policy
13
implies that a window will be chosen automatically. See
14
[here](https://docs.digitalocean.com/products/kubernetes/how-to/upgrade-cluster/)
15
for details.
16

17
 */
18
export async function main(
19
  auth: Digitalocean,
20
  body: {
21
    id?: string;
22
    name: string;
23
    region: string;
24
    version: string;
25
    cluster_subnet?: string;
26
    service_subnet?: string;
27
    vpc_uuid?: string;
28
    ipv4?: string;
29
    endpoint?: string;
30
    tags?: string[];
31
    node_pools: { size?: string } & {
32
      id?: string;
33
      name?: string;
34
      count?: number;
35
      tags?: string[];
36
      labels?: {};
37
      taints?: {
38
        key?: string;
39
        value?: string;
40
        effect?: "NoSchedule" | "PreferNoSchedule" | "NoExecute";
41
      }[];
42
      auto_scale?: false | true;
43
      min_nodes?: number;
44
      max_nodes?: number;
45
      nodes?: {
46
        id?: string;
47
        name?: string;
48
        status?: {
49
          state?: "provisioning" | "running" | "draining" | "deleting";
50
        };
51
        droplet_id?: string;
52
        created_at?: string;
53
        updated_at?: string;
54
      }[];
55
    }[];
56
    maintenance_policy?: {
57
      start_time?: string;
58
      duration?: string;
59
      day?:
60
        | "any"
61
        | "monday"
62
        | "tuesday"
63
        | "wednesday"
64
        | "thursday"
65
        | "friday"
66
        | "saturday"
67
        | "sunday";
68
    };
69
    auto_upgrade?: false | true;
70
    status?: {
71
      state?:
72
        | "provisioning"
73
        | "running"
74
        | "deleting"
75
        | "degraded"
76
        | "error"
77
        | "deleted"
78
        | "upgrading";
79
      message?: string;
80
    };
81
    created_at?: string;
82
    updated_at?: string;
83
    surge_upgrade?: false | true;
84
    ha?: false | true;
85
    registry_enabled?: false | true;
86
    control_plane_firewall?: {
87
      enable?: false | true;
88
      allowed_addresses?: string[];
89
    };
90
  },
91
) {
92
  const url = new URL(`https://api.digitalocean.com/v2/kubernetes/clusters`);
93

94
  const response = await fetch(url, {
95
    method: "POST",
96
    headers: {
97
      "Content-Type": "application/json",
98
      Authorization: "Bearer " + auth.token,
99
    },
100
    body: JSON.stringify(body),
101
  });
102
  if (!response.ok) {
103
    const text = await response.text();
104
    throw new Error(`${response.status} ${text}`);
105
  }
106
  return await response.json();
107
}
108