1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312
|
package linodego
import (
"context"
"encoding/json"
"fmt"
"time"
"github.com/linode/linodego/internal/parseabletime"
)
// LKEClusterStatus represents the status of an LKECluster
type LKEClusterStatus string
// LKEClusterStatus enums start with LKECluster
const (
LKEClusterReady LKEClusterStatus = "ready"
LKEClusterNotReady LKEClusterStatus = "not_ready"
)
// LKECluster represents a LKECluster object
type LKECluster struct {
ID int `json:"id"`
Created *time.Time `json:"-"`
Updated *time.Time `json:"-"`
Label string `json:"label"`
Region string `json:"region"`
Status LKEClusterStatus `json:"status"`
K8sVersion string `json:"k8s_version"`
Tags []string `json:"tags"`
ControlPlane LKEClusterControlPlane `json:"control_plane"`
// NOTE: Tier may not currently be available to all users and can only be used with v4beta.
Tier string `json:"tier"`
// NOTE: APLEnabled is currently in beta and may only function with API version v4beta.
APLEnabled bool `json:"apl_enabled"`
}
// LKEClusterCreateOptions fields are those accepted by CreateLKECluster
type LKEClusterCreateOptions struct {
NodePools []LKENodePoolCreateOptions `json:"node_pools"`
Label string `json:"label"`
Region string `json:"region"`
K8sVersion string `json:"k8s_version"`
Tags []string `json:"tags,omitempty"`
ControlPlane *LKEClusterControlPlaneOptions `json:"control_plane,omitempty"`
// NOTE: Tier may not currently be available to all users and can only be used with v4beta.
Tier string `json:"tier,omitempty"`
// NOTE: APLEnabled is currently in beta and may only function with API version v4beta.
APLEnabled bool `json:"apl_enabled,omitempty"`
}
// LKEClusterUpdateOptions fields are those accepted by UpdateLKECluster
type LKEClusterUpdateOptions struct {
K8sVersion string `json:"k8s_version,omitempty"`
Label string `json:"label,omitempty"`
Tags *[]string `json:"tags,omitempty"`
ControlPlane *LKEClusterControlPlaneOptions `json:"control_plane,omitempty"`
}
// LKEClusterAPIEndpoint fields are those returned by ListLKEClusterAPIEndpoints
type LKEClusterAPIEndpoint struct {
Endpoint string `json:"endpoint"`
}
// LKEClusterKubeconfig fields are those returned by GetLKEClusterKubeconfig
type LKEClusterKubeconfig struct {
KubeConfig string `json:"kubeconfig"` // Base64-encoded Kubeconfig file for this Cluster.
}
// LKEClusterDashboard fields are those returned by GetLKEClusterDashboard
type LKEClusterDashboard struct {
URL string `json:"url"`
}
// LKEVersion fields are those returned by GetLKEVersion
type LKEVersion struct {
ID string `json:"id"`
}
// LKETierVersion fields are those returned by GetLKETierVersion
// NOTE: It may not currently be available to all users and can only be used with v4beta.
type LKETierVersion struct {
ID string `json:"id"`
Tier LKEVersionTier `json:"tier"`
}
// LKEVersionTier enums represents different LKE tiers
type LKEVersionTier string
// LKEVersionTier enums start with LKEVersion
const (
LKEVersionStandard LKEVersionTier = "standard"
LKEVersionEnterprise LKEVersionTier = "enterprise"
)
// LKEClusterRegenerateOptions fields are those accepted by RegenerateLKECluster
type LKEClusterRegenerateOptions struct {
KubeConfig bool `json:"kubeconfig"`
ServiceToken bool `json:"servicetoken"`
}
// UnmarshalJSON implements the json.Unmarshaler interface
func (i *LKECluster) UnmarshalJSON(b []byte) error {
type Mask LKECluster
p := struct {
*Mask
Created *parseabletime.ParseableTime `json:"created"`
Updated *parseabletime.ParseableTime `json:"updated"`
}{
Mask: (*Mask)(i),
}
if err := json.Unmarshal(b, &p); err != nil {
return err
}
i.Created = (*time.Time)(p.Created)
i.Updated = (*time.Time)(p.Updated)
return nil
}
// GetCreateOptions converts a LKECluster to LKEClusterCreateOptions for use in CreateLKECluster
func (i LKECluster) GetCreateOptions() (o LKEClusterCreateOptions) {
o.Label = i.Label
o.Region = i.Region
o.K8sVersion = i.K8sVersion
o.Tags = i.Tags
isHA := i.ControlPlane.HighAvailability
o.ControlPlane = &LKEClusterControlPlaneOptions{
HighAvailability: &isHA,
// ACL will not be populated in the control plane response
}
// @TODO copy NodePools?
return
}
// GetUpdateOptions converts a LKECluster to LKEClusterUpdateOptions for use in UpdateLKECluster
func (i LKECluster) GetUpdateOptions() (o LKEClusterUpdateOptions) {
o.K8sVersion = i.K8sVersion
o.Label = i.Label
o.Tags = &i.Tags
isHA := i.ControlPlane.HighAvailability
o.ControlPlane = &LKEClusterControlPlaneOptions{
HighAvailability: &isHA,
// ACL will not be populated in the control plane response
}
return
}
// ListLKEVersions lists the Kubernetes versions available through LKE. This endpoint is cached by default.
func (c *Client) ListLKEVersions(ctx context.Context, opts *ListOptions) ([]LKEVersion, error) {
e := "lke/versions"
endpoint, err := generateListCacheURL(e, opts)
if err != nil {
return nil, err
}
if result := c.getCachedResponse(endpoint); result != nil {
return result.([]LKEVersion), nil
}
response, err := getPaginatedResults[LKEVersion](ctx, c, e, opts)
if err != nil {
return nil, err
}
c.addCachedResponse(endpoint, response, &cacheExpiryTime)
return response, nil
}
// GetLKEVersion gets details about a specific LKE Version. This endpoint is cached by default.
func (c *Client) GetLKEVersion(ctx context.Context, version string) (*LKEVersion, error) {
e := formatAPIPath("lke/versions/%s", version)
if result := c.getCachedResponse(e); result != nil {
result := result.(LKEVersion)
return &result, nil
}
response, err := doGETRequest[LKEVersion](ctx, c, e)
if err != nil {
return nil, err
}
c.addCachedResponse(e, response, &cacheExpiryTime)
return response, nil
}
// ListLKETierVersions lists all Kubernetes versions available given tier through LKE.
// NOTE: This endpoint may not currently be available to all users and can only be used with v4beta.
func (c *Client) ListLKETierVersions(ctx context.Context, tier string, opts *ListOptions) ([]LKETierVersion, error) {
return getPaginatedResults[LKETierVersion](ctx, c, formatAPIPath("lke/tiers/%s/versions", tier), opts)
}
// GetLKETierVersion gets the details of a specific LKE tier version.
// NOTE: This endpoint may not currently be available to all users and can only be used with v4beta.
func (c *Client) GetLKETierVersion(ctx context.Context, tier string, versionID string) (*LKETierVersion, error) {
return doGETRequest[LKETierVersion](ctx, c, formatAPIPath("lke/tiers/%s/versions/%s", tier, versionID))
}
// ListLKEClusterAPIEndpoints gets the API Endpoint for the LKE Cluster specified
func (c *Client) ListLKEClusterAPIEndpoints(ctx context.Context, clusterID int, opts *ListOptions) ([]LKEClusterAPIEndpoint, error) {
return getPaginatedResults[LKEClusterAPIEndpoint](ctx, c, formatAPIPath("lke/clusters/%d/api-endpoints", clusterID), opts)
}
// ListLKEClusters lists LKEClusters
func (c *Client) ListLKEClusters(ctx context.Context, opts *ListOptions) ([]LKECluster, error) {
return getPaginatedResults[LKECluster](ctx, c, "lke/clusters", opts)
}
// GetLKECluster gets the lkeCluster with the provided ID
func (c *Client) GetLKECluster(ctx context.Context, clusterID int) (*LKECluster, error) {
e := formatAPIPath("lke/clusters/%d", clusterID)
return doGETRequest[LKECluster](ctx, c, e)
}
// CreateLKECluster creates a LKECluster
func (c *Client) CreateLKECluster(ctx context.Context, opts LKEClusterCreateOptions) (*LKECluster, error) {
return doPOSTRequest[LKECluster](ctx, c, "lke/clusters", opts)
}
// UpdateLKECluster updates the LKECluster with the specified id
func (c *Client) UpdateLKECluster(ctx context.Context, clusterID int, opts LKEClusterUpdateOptions) (*LKECluster, error) {
e := formatAPIPath("lke/clusters/%d", clusterID)
return doPUTRequest[LKECluster](ctx, c, e, opts)
}
// DeleteLKECluster deletes the LKECluster with the specified id
func (c *Client) DeleteLKECluster(ctx context.Context, clusterID int) error {
e := formatAPIPath("lke/clusters/%d", clusterID)
return doDELETERequest(ctx, c, e)
}
// GetLKEClusterKubeconfig gets the Kubeconfig for the LKE Cluster specified
func (c *Client) GetLKEClusterKubeconfig(ctx context.Context, clusterID int) (*LKEClusterKubeconfig, error) {
e := formatAPIPath("lke/clusters/%d/kubeconfig", clusterID)
return doGETRequest[LKEClusterKubeconfig](ctx, c, e)
}
// DeleteLKEClusterKubeconfig deletes the Kubeconfig for the LKE Cluster specified
func (c *Client) DeleteLKEClusterKubeconfig(ctx context.Context, clusterID int) error {
e := formatAPIPath("lke/clusters/%d/kubeconfig", clusterID)
return doDELETERequest(ctx, c, e)
}
// GetLKEClusterDashboard gets information about the dashboard for an LKE cluster
func (c *Client) GetLKEClusterDashboard(ctx context.Context, clusterID int) (*LKEClusterDashboard, error) {
e := formatAPIPath("lke/clusters/%d/dashboard", clusterID)
return doGETRequest[LKEClusterDashboard](ctx, c, e)
}
// RecycleLKEClusterNodes recycles all nodes in all pools of the specified LKE Cluster.
func (c *Client) RecycleLKEClusterNodes(ctx context.Context, clusterID int) error {
e := formatAPIPath("lke/clusters/%d/recycle", clusterID)
return doPOSTRequestNoRequestResponseBody(ctx, c, e)
}
// RegenerateLKECluster regenerates the Kubeconfig file and/or the service account token for the specified LKE Cluster.
func (c *Client) RegenerateLKECluster(ctx context.Context, clusterID int, opts LKEClusterRegenerateOptions) (*LKECluster, error) {
e := formatAPIPath("lke/clusters/%d/regenerate", clusterID)
return doPOSTRequest[LKECluster](ctx, c, e, opts)
}
// DeleteLKEClusterServiceToken deletes and regenerate the service account token for a Cluster.
func (c *Client) DeleteLKEClusterServiceToken(ctx context.Context, clusterID int) error {
e := formatAPIPath("lke/clusters/%d/servicetoken", clusterID)
return doDELETERequest(ctx, c, e)
}
// GetLKEClusterAPLConsoleURL gets the URL of this cluster's APL installation if this cluster is APL-enabled.
func (c *Client) GetLKEClusterAPLConsoleURL(ctx context.Context, clusterID int) (string, error) {
cluster, err := c.GetLKECluster(ctx, clusterID)
if err != nil {
return "", err
}
if cluster.APLEnabled {
return fmt.Sprintf("https://console.lke%d.akamai-apl.net", cluster.ID), nil
}
return "", nil
}
// GetLKEClusterAPLHealthCheckURL gets the URL of this cluster's APL health check endpoint if this cluster is APL-enabled.
func (c *Client) GetLKEClusterAPLHealthCheckURL(ctx context.Context, clusterID int) (string, error) {
cluster, err := c.GetLKECluster(ctx, clusterID)
if err != nil {
return "", err
}
if cluster.APLEnabled {
return fmt.Sprintf("https://auth.lke%d.akamai-apl.net/ready", cluster.ID), nil
}
return "", nil
}
|