Skip to content

Commit

Permalink
Added kubernetes releases and upgrades
Browse files Browse the repository at this point in the history
  • Loading branch information
fvanmaldegem committed Nov 6, 2023
1 parent 14171e5 commit 9dc7354
Show file tree
Hide file tree
Showing 6 changed files with 258 additions and 22 deletions.
2 changes: 1 addition & 1 deletion configuration.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (
)

const (
libraryVersion = "6.22.1"
libraryVersion = "6.23.0"
defaultBasePath = "https://api.transip.nl/v6"
userAgent = "go-client-gotransip/" + libraryVersion
)
Expand Down
46 changes: 46 additions & 0 deletions examples/kubernetes.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package main

import (
"fmt"
"log"

"github.com/transip/gotransip/v6"
"github.com/transip/gotransip/v6/kubernetes"
)

func main() {
client, err := gotransip.NewClient(gotransip.DemoClientConfiguration)
if err != nil {
panic(err)
}

k8sRepo := kubernetes.Repository{Client: client}

// List all clusters with nodepools
log.Println("List all clusters and the acccompanying nodepools")
clusters, err := k8sRepo.GetClusters()
if err != nil {
panic(err)
}

for _, c := range clusters {
fmt.Printf("Cluster: %s\n", c.Name)
nodepools, err := k8sRepo.GetNodePools(c.Name)
if err != nil {
continue
}
for _, n := range nodepools {
fmt.Printf(
"\tNodePool: [description=%s, zone=%s, spec=%s, nodeCount=%s]\n",
n.Description, n.AvailabilityZone, n.NodeSpec, n.DesiredNodeCount,
)
}

}

log.Println("Create new cluster and wait for it to be available")
clusters, err = k8sRepo.GetClusters()
if err != nil {
panic(err)
}
}
15 changes: 11 additions & 4 deletions kubernetes/cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,17 @@ type clusterWrapper struct {
Cluster Cluster `json:"cluster"`
}

// handoverRequest is used to request a handover, this is solely used for marshalling
type handoverRequest struct {
Action string `json:"action"`
TargetCustomerName string `json:"targetCustomerName"`
// resetRequest is used to request a cluster reset, this is solely used for marshalling
type resetRequest struct {
Action string `json:"action"`
Confirmation string `json:"confirmation"`
}

// upgradeRequest is used to request a cluster upgrade, this is solely used for marshalling

type upgradeRequest struct {
Action string `json:"action"`
Version string `json:"version"`
}

// Cluster struct for a Kubernetes cluster
Expand Down
21 changes: 21 additions & 0 deletions kubernetes/release.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package kubernetes

import (
"github.com/transip/gotransip/v6/rest"
)

// Release is a Kubernetes release version
type Release struct {
Version string `json:"version"`
ReleaseDate rest.Date `json:"releaseDate"`
MaintenanceModeDate rest.Date `json:"maintenanceModeDate"`
EndOfLifeDate rest.Date `json:"endOfLifeDate"`
}

type releaseWrapper struct {
Release Release `json:"release"`
}

type releasesWrapper struct {
Releases []Release `json:"releases"`
}
53 changes: 48 additions & 5 deletions kubernetes/repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import (
// this repository allows you to manage all Kubernetes services for your TransIP account
type Repository repository.RestRepository

// GetClusters returns a list of all your VPSs
// GetClusters returns a list of all your Clusters
func (r *Repository) GetClusters() ([]Cluster, error) {
var response clustersWrapper
restRequest := rest.Request{Endpoint: "/kubernetes/clusters"}
Expand Down Expand Up @@ -48,10 +48,17 @@ func (r *Repository) UpdateCluster(cluster Cluster) error {
return r.Client.Put(restRequest)
}

// HandoverCluster will handover a cluster to another TransIP Account. This call will initiate the handover process.
// The actual handover will be done when the target customer accepts the handover
func (r *Repository) HandoverCluster(clusterName string, targetCustomerName string) error {
requestBody := handoverRequest{Action: "handover", TargetCustomerName: targetCustomerName}
// UpgradeCluster performs an upgrade of the Kubernetes version of your cluster
func (r *Repository) UpgradeCluster(clusterName, version string) error {
requestBody := upgradeRequest{Action: "upgrade", Version: version}
restRequest := rest.Request{Endpoint: fmt.Sprintf("/kubernetes/clusters/%s", clusterName), Body: &requestBody}

return r.Client.Patch(restRequest)
}

// ResetCluster performs a reset of the Kubernetes, bringing it back to the initial state it got ordered in
func (r *Repository) ResetCluster(clusterName, confirmation string) error {
requestBody := resetRequest{Action: "reset", Confirmation: confirmation}
restRequest := rest.Request{Endpoint: fmt.Sprintf("/kubernetes/clusters/%s", clusterName), Body: &requestBody}

return r.Client.Patch(restRequest)
Expand Down Expand Up @@ -272,3 +279,39 @@ func (r *Repository) SetLabels(clusterName, nodePoolUUID string, labels []Label)

return r.Client.Put(restRequest)
}

// GetReleases returns the available releases on the platform
func (r *Repository) GetReleases() ([]Release, error) {
var response releasesWrapper
restRequest := rest.Request{Endpoint: "/kubernetes/releases"}
err := r.Client.Get(restRequest, &response)

return response.Releases, err
}

// GetRelease returns an available releases on the platform
func (r *Repository) GetRelease(version string) (Release, error) {
var response releaseWrapper
restRequest := rest.Request{Endpoint: fmt.Sprintf("/kubernetes/releases/%s", version)}
err := r.Client.Get(restRequest, &response)

return response.Release, err
}

// GetCompatibleReleases returns the releases a cluster can upgrade to
func (r *Repository) GetCompatibleReleases(clusterName string) ([]Release, error) {
var response releasesWrapper
restRequest := rest.Request{Endpoint: fmt.Sprintf("/kubernetes/clusters/%s/releases", clusterName)}
err := r.Client.Get(restRequest, &response)

return response.Releases, err
}

// GetCompatibleRelease returns the release a cluster can upgrade to
func (r *Repository) GetCompatibleRelease(clusterName, version string) (Release, error) {
var response releaseWrapper
restRequest := rest.Request{Endpoint: fmt.Sprintf("/kubernetes/clusters/%s/releases/%s", clusterName, version)}
err := r.Client.Get(restRequest, &response)

return response.Release, err
}
143 changes: 131 additions & 12 deletions kubernetes/repository_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@ import (
"github.com/transip/gotransip/v6/internal/testutil"
)

// To be compatible with < Go 1.20
const dateOnlyFormat = "2006-01-02"

func TestRepository_GetClusters(t *testing.T) {
const apiResponse = `{"clusters":[{"name":"k888k","description":"production cluster","isLocked":true,"isBlocked": false},{"name":"aiceayoo","description":"development cluster","isLocked":false,"isBlocked":true}]}`

Expand Down Expand Up @@ -86,18 +89,6 @@ func TestRepository_UpdateCluster(t *testing.T) {
require.NoError(t, err)
}

func TestRepository_HandoverCluster(t *testing.T) {
const expectedRequest = `{"action":"handover","targetCustomerName":"bobexample"}`

server := testutil.MockServer{T: t, ExpectedURL: "/kubernetes/clusters/k888k", ExpectedMethod: "PATCH", StatusCode: 204, ExpectedRequest: expectedRequest}
client, tearDown := server.GetClient()
defer tearDown()
repo := Repository{Client: *client}

err := repo.HandoverCluster("k888k", "bobexample")
require.NoError(t, err)
}

func TestRepository_RemoveCluster(t *testing.T) {
server := testutil.MockServer{T: t, ExpectedURL: "/kubernetes/clusters/k888k", ExpectedMethod: "DELETE", StatusCode: 204}
client, tearDown := server.GetClient()
Expand Down Expand Up @@ -538,3 +529,131 @@ func TestRepository_SetNodePoolTaints(t *testing.T) {
err := repo.SetTaints("k888k", "402c2f84-c37d-9388-634d-00002b7c6a82", []Taint{{Key: "test-key", Value: "test-value", Effect: "NoSchedule"}})
require.NoError(t, err)
}

func TestRepository_UpgradeCluster(t *testing.T) {
const apiRequest = `{"action":"upgrade","version":"1.27.0"}`
server := testutil.MockServer{
T: t,
ExpectedURL: "/kubernetes/clusters/k888k",
ExpectedMethod: "PATCH",
StatusCode: 204,
ExpectedRequest: apiRequest,
}

client, teardown := server.GetClient()
defer teardown()

repo := Repository{Client: *client}
err := repo.UpgradeCluster("k888k", "1.27.0")
require.NoError(t, err)
}

func TestRepository_ResetCluster(t *testing.T) {
const apiRequest = `{"action":"reset","confirmation":"k888k"}`
server := testutil.MockServer{
T: t,
ExpectedURL: "/kubernetes/clusters/k888k",
ExpectedMethod: "PATCH",
StatusCode: 204,
ExpectedRequest: apiRequest,
}

client, teardown := server.GetClient()
defer teardown()

repo := Repository{Client: *client}
err := repo.ResetCluster("k888k", "k888k")
require.NoError(t, err)
}

func TestRepository_TestGetReleases(t *testing.T) {
const apiResponse = `{"releases":[{"version": "1.23.5","releaseDate": "2022-03-11","maintenanceModeDate": "2022-12-28","endOfLifeDate": "2023-02-28"}]}`
server := testutil.MockServer{
T: t,
ExpectedURL: "/kubernetes/releases",
ExpectedMethod: "GET",
StatusCode: 200,
Response: apiResponse,
}

client, teardown := server.GetClient()
defer teardown()

repo := Repository{Client: *client}
releases, err := repo.GetReleases()
if assert.Equal(t, 1, len(releases)) {
assert.Equal(t, "1.23.5", releases[0].Version)
assert.Equal(t, "2022-03-11", releases[0].ReleaseDate.Format(dateOnlyFormat))
assert.Equal(t, "2022-12-28", releases[0].MaintenanceModeDate.Format(dateOnlyFormat))
assert.Equal(t, "2023-02-28", releases[0].EndOfLifeDate.Format(dateOnlyFormat))
}
require.NoError(t, err)
}

func TestRepository_TestGetRelease(t *testing.T) {
const apiResponse = `{"release":{"version": "1.23.5","releaseDate": "2022-03-11","maintenanceModeDate": "2022-12-28","endOfLifeDate": "2023-02-28"}}`
server := testutil.MockServer{
T: t,
ExpectedURL: "/kubernetes/releases/1.23.5",
ExpectedMethod: "GET",
StatusCode: 200,
Response: apiResponse,
}

client, teardown := server.GetClient()
defer teardown()

repo := Repository{Client: *client}
release, err := repo.GetRelease("1.23.5")
assert.Equal(t, "1.23.5", release.Version)
assert.Equal(t, "2022-03-11", release.ReleaseDate.Format(dateOnlyFormat))
assert.Equal(t, "2022-12-28", release.MaintenanceModeDate.Format(dateOnlyFormat))
assert.Equal(t, "2023-02-28", release.EndOfLifeDate.Format(dateOnlyFormat))
require.NoError(t, err)
}

func TestRepository_TestGetCompatibleReleases(t *testing.T) {
const apiResponse = `{"releases":[{"isCompatibleUpgrade":true,"version": "1.23.5","releaseDate": "2022-03-11","maintenanceModeDate": "2022-12-28","endOfLifeDate": "2023-02-28"}]}`
server := testutil.MockServer{
T: t,
ExpectedURL: "/kubernetes/clusters/k888k/releases",
ExpectedMethod: "GET",
StatusCode: 200,
Response: apiResponse,
}

client, teardown := server.GetClient()
defer teardown()

repo := Repository{Client: *client}
releases, err := repo.GetCompatibleReleases("k888k")
if assert.Equal(t, 1, len(releases)) {
assert.Equal(t, "1.23.5", releases[0].Version)
assert.Equal(t, "2022-03-11", releases[0].ReleaseDate.Format(dateOnlyFormat))
assert.Equal(t, "2022-12-28", releases[0].MaintenanceModeDate.Format(dateOnlyFormat))
assert.Equal(t, "2023-02-28", releases[0].EndOfLifeDate.Format(dateOnlyFormat))
}
require.NoError(t, err)
}

func TestRepository_TestGetCompatibleRelease(t *testing.T) {
const apiResponse = `{"release":{"isCompatibleUpgrade":true,"version": "1.23.5","releaseDate": "2022-03-11","maintenanceModeDate": "2022-12-28","endOfLifeDate": "2023-02-28"}}`
server := testutil.MockServer{
T: t,
ExpectedURL: "/kubernetes/clusters/k888k/releases/1.23.5",
ExpectedMethod: "GET",
StatusCode: 200,
Response: apiResponse,
}

client, teardown := server.GetClient()
defer teardown()

repo := Repository{Client: *client}
release, err := repo.GetCompatibleRelease("k888k", "1.23.5")
assert.Equal(t, "1.23.5", release.Version)
assert.Equal(t, "2022-03-11", release.ReleaseDate.Format(dateOnlyFormat))
assert.Equal(t, "2022-12-28", release.MaintenanceModeDate.Format(dateOnlyFormat))
assert.Equal(t, "2023-02-28", release.EndOfLifeDate.Format(dateOnlyFormat))
require.NoError(t, err)
}

0 comments on commit 9dc7354

Please sign in to comment.