lke_node_pools.go raw
1 package linodego
2
3 import (
4 "context"
5 )
6
7 // LKELinodeStatus constants start with LKELinode and include
8 // Linode API LKENodePool Linode Status values
9 type LKELinodeStatus string
10
11 // LKENodePoolStatus constants reflect the current status of an LKENodePool
12 const (
13 LKELinodeReady LKELinodeStatus = "ready"
14 LKELinodeNotReady LKELinodeStatus = "not_ready"
15 )
16
17 // LKENodePoolUpdateStrategy constants start with LKENodePool and include
18 // LKE Node Pool upgrade strategy values
19 type LKENodePoolUpdateStrategy string
20
21 // LKENodePoolUpdateStrategy constants describe the available upgrade strategies for LKE Enterprise only
22 const (
23 LKENodePoolRollingUpdate LKENodePoolUpdateStrategy = "rolling_update"
24 LKENodePoolOnRecycle LKENodePoolUpdateStrategy = "on_recycle"
25 )
26
27 // LKENodePoolDisk represents a Node disk in an LKENodePool object
28 type LKENodePoolDisk struct {
29 Size int `json:"size"`
30 Type string `json:"type"`
31 }
32
33 type LKENodePoolAutoscaler struct {
34 Enabled bool `json:"enabled"`
35 Min int `json:"min"`
36 Max int `json:"max"`
37 }
38
39 // LKENodePoolLinode represents a LKENodePoolLinode object
40 type LKENodePoolLinode struct {
41 ID string `json:"id"`
42 InstanceID int `json:"instance_id"`
43 Status LKELinodeStatus `json:"status"`
44 }
45
46 // LKENodePoolTaintEffect represents the effect value of a taint
47 type LKENodePoolTaintEffect string
48
49 const (
50 LKENodePoolTaintEffectNoSchedule LKENodePoolTaintEffect = "NoSchedule"
51 LKENodePoolTaintEffectPreferNoSchedule LKENodePoolTaintEffect = "PreferNoSchedule"
52 LKENodePoolTaintEffectNoExecute LKENodePoolTaintEffect = "NoExecute"
53 )
54
55 // LKENodePoolTaint represents a corev1.Taint to add to an LKENodePool
56 type LKENodePoolTaint struct {
57 Key string `json:"key"`
58 Value string `json:"value,omitempty"`
59 Effect LKENodePoolTaintEffect `json:"effect"`
60 }
61
62 // LKENodePoolLabels represents Kubernetes labels to add to an LKENodePool
63 type LKENodePoolLabels map[string]string
64
65 // LKENodePool represents a LKENodePool object
66 type LKENodePool struct {
67 ID int `json:"id"`
68 Count int `json:"count"`
69 Type string `json:"type"`
70 Disks []LKENodePoolDisk `json:"disks"`
71 Linodes []LKENodePoolLinode `json:"nodes"`
72 Tags []string `json:"tags"`
73 Labels LKENodePoolLabels `json:"labels"`
74 Taints []LKENodePoolTaint `json:"taints"`
75 Label *string `json:"label"`
76
77 Autoscaler LKENodePoolAutoscaler `json:"autoscaler"`
78 FirewallID *int `json:"firewall_id,omitempty"`
79
80 // NOTE: Disk encryption may not currently be available to all users.
81 DiskEncryption InstanceDiskEncryption `json:"disk_encryption,omitempty"`
82
83 // K8sVersion and UpdateStrategy are only for LKE Enterprise to support node pool upgrades.
84 // It may not currently be available to all users and is under v4beta.
85 K8sVersion *string `json:"k8s_version,omitempty"`
86 UpdateStrategy *LKENodePoolUpdateStrategy `json:"update_strategy,omitempty"`
87 }
88
89 // LKENodePoolCreateOptions fields are those accepted by CreateLKENodePool
90 type LKENodePoolCreateOptions struct {
91 Count int `json:"count"`
92 Type string `json:"type"`
93 Disks []LKENodePoolDisk `json:"disks"`
94 Tags []string `json:"tags"`
95 Labels LKENodePoolLabels `json:"labels"`
96 Taints []LKENodePoolTaint `json:"taints"`
97 Label *string `json:"label,omitempty"`
98
99 Autoscaler *LKENodePoolAutoscaler `json:"autoscaler,omitempty"`
100 FirewallID *int `json:"firewall_id,omitempty"`
101
102 // K8sVersion and UpdateStrategy only works for LKE Enterprise to support node pool upgrades.
103 // It may not currently be available to all users and is under v4beta.
104 K8sVersion *string `json:"k8s_version,omitempty"`
105 UpdateStrategy *LKENodePoolUpdateStrategy `json:"update_strategy,omitempty"`
106 }
107
108 // LKENodePoolUpdateOptions fields are those accepted by UpdateLKENodePoolUpdate
109 type LKENodePoolUpdateOptions struct {
110 Count int `json:"count,omitempty"`
111 Tags *[]string `json:"tags,omitempty"`
112 Labels *LKENodePoolLabels `json:"labels,omitempty"`
113 Taints *[]LKENodePoolTaint `json:"taints,omitempty"`
114 Label *string `json:"label,omitempty"`
115
116 Autoscaler *LKENodePoolAutoscaler `json:"autoscaler,omitempty"`
117 FirewallID *int `json:"firewall_id,omitempty"`
118
119 // K8sVersion and UpdateStrategy only works for LKE Enterprise to support node pool upgrades.
120 // It may not currently be available to all users and is under v4beta.
121 K8sVersion *string `json:"k8s_version,omitempty"`
122 UpdateStrategy *LKENodePoolUpdateStrategy `json:"update_strategy,omitempty"`
123 }
124
125 // GetCreateOptions converts a LKENodePool to LKENodePoolCreateOptions for
126 // use in CreateLKENodePool
127 func (l LKENodePool) GetCreateOptions() (o LKENodePoolCreateOptions) {
128 o.Count = l.Count
129 o.Disks = l.Disks
130 o.Tags = l.Tags
131 o.Labels = l.Labels
132 o.Taints = l.Taints
133 o.Autoscaler = &l.Autoscaler
134 o.K8sVersion = l.K8sVersion
135 o.UpdateStrategy = l.UpdateStrategy
136 o.Label = l.Label
137 o.FirewallID = l.FirewallID
138
139 return o
140 }
141
142 // GetUpdateOptions converts a LKENodePool to LKENodePoolUpdateOptions for use in UpdateLKENodePoolUpdate
143 func (l LKENodePool) GetUpdateOptions() (o LKENodePoolUpdateOptions) {
144 o.Count = l.Count
145 o.Tags = &l.Tags
146 o.Labels = &l.Labels
147 o.Taints = &l.Taints
148 o.Autoscaler = &l.Autoscaler
149 o.K8sVersion = l.K8sVersion
150 o.UpdateStrategy = l.UpdateStrategy
151 o.Label = l.Label
152 o.FirewallID = l.FirewallID
153
154 return o
155 }
156
157 // ListLKENodePools lists LKENodePools
158 func (c *Client) ListLKENodePools(ctx context.Context, clusterID int, opts *ListOptions) ([]LKENodePool, error) {
159 return getPaginatedResults[LKENodePool](ctx, c, formatAPIPath("lke/clusters/%d/pools", clusterID), opts)
160 }
161
162 // GetLKENodePool gets the LKENodePool with the provided ID
163 func (c *Client) GetLKENodePool(ctx context.Context, clusterID, poolID int) (*LKENodePool, error) {
164 e := formatAPIPath("lke/clusters/%d/pools/%d", clusterID, poolID)
165 return doGETRequest[LKENodePool](ctx, c, e)
166 }
167
168 // CreateLKENodePool creates a LKENodePool
169 func (c *Client) CreateLKENodePool(ctx context.Context, clusterID int, opts LKENodePoolCreateOptions) (*LKENodePool, error) {
170 e := formatAPIPath("lke/clusters/%d/pools", clusterID)
171 return doPOSTRequest[LKENodePool](ctx, c, e, opts)
172 }
173
174 // RecycleLKENodePool recycles a LKENodePool
175 func (c *Client) RecycleLKENodePool(ctx context.Context, clusterID, poolID int) error {
176 e := formatAPIPath("lke/clusters/%d/pools/%d/recycle", clusterID, poolID)
177 return doPOSTRequestNoRequestResponseBody(ctx, c, e)
178 }
179
180 // UpdateLKENodePool updates the LKENodePool with the specified id
181 func (c *Client) UpdateLKENodePool(ctx context.Context, clusterID, poolID int, opts LKENodePoolUpdateOptions) (*LKENodePool, error) {
182 e := formatAPIPath("lke/clusters/%d/pools/%d", clusterID, poolID)
183 return doPUTRequest[LKENodePool](ctx, c, e, opts)
184 }
185
186 // DeleteLKENodePool deletes the LKENodePool with the specified id
187 func (c *Client) DeleteLKENodePool(ctx context.Context, clusterID, poolID int) error {
188 e := formatAPIPath("lke/clusters/%d/pools/%d", clusterID, poolID)
189 return doDELETERequest(ctx, c, e)
190 }
191
192 // GetLKENodePoolNode gets the LKENodePoolLinode with the provided ID
193 func (c *Client) GetLKENodePoolNode(ctx context.Context, clusterID int, nodeID string) (*LKENodePoolLinode, error) {
194 e := formatAPIPath("lke/clusters/%d/nodes/%s", clusterID, nodeID)
195 return doGETRequest[LKENodePoolLinode](ctx, c, e)
196 }
197
198 // RecycleLKENodePoolNode recycles a LKENodePoolLinode
199 func (c *Client) RecycleLKENodePoolNode(ctx context.Context, clusterID int, nodeID string) error {
200 e := formatAPIPath("lke/clusters/%d/nodes/%s/recycle", clusterID, nodeID)
201 return doPOSTRequestNoRequestResponseBody(ctx, c, e)
202 }
203
204 // DeleteLKENodePoolNode deletes a given node from a node pool
205 func (c *Client) DeleteLKENodePoolNode(ctx context.Context, clusterID int, nodeID string) error {
206 e := formatAPIPath("lke/clusters/%d/nodes/%s", clusterID, nodeID)
207 return doDELETERequest(ctx, c, e)
208 }
209