Skip to content

Commit

Permalink
feat(COR-1127): Add cluster lock/unlock
Browse files Browse the repository at this point in the history
  • Loading branch information
pggb25 committed Dec 27, 2024
1 parent c323e75 commit 3aedc34
Show file tree
Hide file tree
Showing 9 changed files with 180 additions and 132 deletions.
1 change: 1 addition & 0 deletions cmd/admin.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ var (
rootDns string
additionalClaims string
description string
lockTtlInDays int32
adminCmd = &cobra.Command{Use: "admin", Hidden: true}
)

Expand Down
33 changes: 0 additions & 33 deletions cmd/admin_lock.go

This file was deleted.

31 changes: 0 additions & 31 deletions cmd/admin_unlock.go

This file was deleted.

63 changes: 63 additions & 0 deletions cmd/cluster_lock.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
package cmd

import (
"context"
"fmt"
"github.com/qovery/qovery-cli/utils"
"github.com/qovery/qovery-client-go"
log "github.com/sirupsen/logrus"
"github.com/spf13/cobra"
"io"
"os"
)

var clusterLockCmd = &cobra.Command{
Use: "lock",
Short: "Lock a cluster",
Run: func(cmd *cobra.Command, args []string) {
lockCluster()
},
}

func init() {
clusterLockCmd.Flags().StringVarP(&clusterId, "cluster-id", "c", "", "Cluster ID")
clusterLockCmd.Flags().StringVarP(&lockReason, "reason", "r", "", "Reason")
clusterLockCmd.Flags().Int32VarP(&lockTtlInDays, "ttl-in-days", "d", -1, "TTL in days")

clusterLockCmd.MarkFlagRequired("cluster-id")

Check failure on line 27 in cmd/cluster_lock.go

View workflow job for this annotation

GitHub Actions / lint

Error return value of `clusterLockCmd.MarkFlagRequired` is not checked (errcheck)
clusterLockCmd.MarkFlagRequired("reason")

Check failure on line 28 in cmd/cluster_lock.go

View workflow job for this annotation

GitHub Actions / lint

Error return value of `clusterLockCmd.MarkFlagRequired` is not checked (errcheck)

clusterCmd.AddCommand(clusterLockCmd)
}

func lockCluster() {
var ttlInDays *int32 = nil
if lockTtlInDays != -1 {
ttlInDays = &lockTtlInDays
}

if utils.Validate("lock") {
tokenType, token, err := utils.GetAccessToken()
if err != nil {
utils.PrintlnError(err)
os.Exit(1)
}

client := utils.GetQoveryClient(tokenType, token)

lockClusterRequest := qovery.ClusterLockRequest{
Reason: lockReason,
TtlInDays: ttlInDays,
}

_, http, err := client.ClustersAPI.LockCluster(context.Background(), clusterId).ClusterLockRequest(lockClusterRequest).Execute()
if err != nil {
utils.PrintlnError(err)
result, _ := io.ReadAll(http.Body)
log.Error("%s", string(result))

Check failure on line 57 in cmd/cluster_lock.go

View workflow job for this annotation

GitHub Actions / test

github.com/sirupsen/logrus.Error call has possible Printf formatting directive %s

Check failure on line 57 in cmd/cluster_lock.go

View workflow job for this annotation

GitHub Actions / lint

printf: github.com/sirupsen/logrus.Error call has possible Printf formatting directive %s (govet)
os.Exit(1)
} else {
fmt.Println("Cluster locked.")
}
}
}
63 changes: 63 additions & 0 deletions cmd/cluster_locked.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
package cmd

import (
"context"
"fmt"
"github.com/qovery/qovery-cli/utils"
log "github.com/sirupsen/logrus"
"github.com/spf13/cobra"
"io"
"net/http"
"os"
"strconv"
"text/tabwriter"
"time"
)

var clusterLockedCmd = &cobra.Command{
Use: "locked",
Short: "List locked clusters",
Run: func(cmd *cobra.Command, args []string) {
clusterLocked()
},
}

func init() {
clusterLockedCmd.Flags().StringVarP(&organizationId, "organization-id", "o", "", "Organization ID")
clusterLockCmd.MarkFlagRequired("organization-id")

Check failure on line 27 in cmd/cluster_locked.go

View workflow job for this annotation

GitHub Actions / lint

Error return value of `clusterLockCmd.MarkFlagRequired` is not checked (errcheck)

clusterCmd.AddCommand(clusterLockedCmd)
}

func clusterLocked() {
tokenType, token, err := utils.GetAccessToken()
if err != nil {
utils.PrintlnError(err)
os.Exit(1)
}

client := utils.GetQoveryClient(tokenType, token)
lockedClusters, res, err := client.OrganizationClusterLockAPI.ListClusterLock(context.Background(), organizationId).Execute()
if res != nil && res.StatusCode != http.StatusOK {
result, _ := io.ReadAll(res.Body)
log.Errorf("Could not list locked clusters : %s. %s", res.Status, string(result))
return
}

if err != nil {
log.Fatal(err)
}

w := tabwriter.NewWriter(os.Stdout, 1, 1, 1, ' ', 0)
format := "%s\t | %s\t | %s\t | %s\t | %s\t | %s\n"
fmt.Fprintf(w, format, "", "cluster_id", "locked_at", "ttl_in_days", "locked_by", "reason")
for idx, lock := range lockedClusters.Results {
ttlInDays := "infinite"
if lock.TtlInDays != nil {
ttlInDays = strconv.Itoa(int(*lock.TtlInDays))
}

fmt.Fprintf(w, format, fmt.Sprintf("%d", idx+1), lock.ClusterId, lock.LockedAt.Format(time.RFC1123), ttlInDays, lock.OwnerName, lock.Reason)
}
w.Flush()
}
44 changes: 44 additions & 0 deletions cmd/cluster_unlock.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package cmd

import (
"context"
"fmt"
"github.com/qovery/qovery-cli/utils"
"github.com/spf13/cobra"
"os"
)

var clusterUnlockCmd = &cobra.Command{
Use: "unlock",
Short: "Unlock a cluster",
Run: func(cmd *cobra.Command, args []string) {
unlockCluster()
},
}

func init() {
clusterUnlockCmd.Flags().StringVarP(&clusterId, "cluster-id", "c", "", "Cluster ID")
clusterLockCmd.MarkFlagRequired("cluster-id")

clusterCmd.AddCommand(clusterUnlockCmd)
}

func unlockCluster() {
if utils.Validate("unlock") {
tokenType, token, err := utils.GetAccessToken()
if err != nil {
utils.PrintlnError(err)
os.Exit(1)
}

client := utils.GetQoveryClient(tokenType, token)

_, err = client.ClustersAPI.UnlockCluster(context.Background(), clusterId).Execute()
if err != nil {
utils.PrintlnError(err)
os.Exit(1)
} else {
fmt.Println("Cluster unlocked.")
}
}
}
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ require (
github.com/pkg/errors v0.9.1
github.com/posthog/posthog-go v1.2.24
github.com/pterm/pterm v0.12.79
github.com/qovery/qovery-client-go v0.0.0-20241203095515-b7bfff7f6c11
github.com/qovery/qovery-client-go v0.0.0-20241227140826-136a47d7533e
github.com/sirupsen/logrus v1.9.3
github.com/spf13/cobra v1.8.1
github.com/spf13/pflag v1.0.5
Expand Down
8 changes: 8 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,14 @@ github.com/qovery/qovery-client-go v0.0.0-20240918181134-faa9e7a86f41 h1:O31SMjm
github.com/qovery/qovery-client-go v0.0.0-20240918181134-faa9e7a86f41/go.mod h1:9eHj5a4EtXGIyfbvVL3HVYW9k7Xmiwi00OqHrP4dc10=
github.com/qovery/qovery-client-go v0.0.0-20241203095515-b7bfff7f6c11 h1:racjGI7jQTgKOZTrYFGwCtceW3aMhfCKQZdpjVENZqM=
github.com/qovery/qovery-client-go v0.0.0-20241203095515-b7bfff7f6c11/go.mod h1:hAMMDu1kk2rNJRf8MiYV0E+07DhDXpvNddj9q8YpbFs=
github.com/qovery/qovery-client-go v0.0.0-20241226152139-3a0a7f99a7d5 h1:CUMeRESypO2DZOC4FsO3z69QksM6p1njgoNxr4h9Uto=
github.com/qovery/qovery-client-go v0.0.0-20241226152139-3a0a7f99a7d5/go.mod h1:hAMMDu1kk2rNJRf8MiYV0E+07DhDXpvNddj9q8YpbFs=
github.com/qovery/qovery-client-go v0.0.0-20241227121330-2fc6e5c1306e h1:o4ANe0EYT6vyRyNgz3rnNKUTSoJmPdPlvWe4wiJeT7s=
github.com/qovery/qovery-client-go v0.0.0-20241227121330-2fc6e5c1306e/go.mod h1:hAMMDu1kk2rNJRf8MiYV0E+07DhDXpvNddj9q8YpbFs=
github.com/qovery/qovery-client-go v0.0.0-20241227132052-aa8926d349d7 h1:qBKc/1pdXeFJi5ZsMlxR0QUNFb64uSIQhBjFIj7VOhA=
github.com/qovery/qovery-client-go v0.0.0-20241227132052-aa8926d349d7/go.mod h1:hAMMDu1kk2rNJRf8MiYV0E+07DhDXpvNddj9q8YpbFs=
github.com/qovery/qovery-client-go v0.0.0-20241227140826-136a47d7533e h1:w9D5Z6b/8XpIPwICCbAqctOkFRIMZdI5tNJ6pWOHgYM=
github.com/qovery/qovery-client-go v0.0.0-20241227140826-136a47d7533e/go.mod h1:hAMMDu1kk2rNJRf8MiYV0E+07DhDXpvNddj9q8YpbFs=
github.com/rivo/uniseg v0.2.0/go.mod h1:J6wj4VEh+S6ZtnVlnTBMWIodfgj8LQOQFoIToxlJtxc=
github.com/rivo/uniseg v0.4.4 h1:8TfxU8dW6PdqD27gjM8MVNuicgxIjxpm4K7x4jp8sis=
github.com/rivo/uniseg v0.4.4/go.mod h1:FN3SvrM+Zdj16jyLfmOkMNblXMcoc8DfTHruCPUcx88=
Expand Down
67 changes: 0 additions & 67 deletions pkg/lock.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package pkg

import (
"bytes"
"encoding/json"
"fmt"
"io"
Expand Down Expand Up @@ -52,41 +51,6 @@ func LockedClusters() {
w.Flush()
}

func LockById(clusterId string, reason string) {
utils.GetAdminUrl()

if reason == "" {
log.Errorf("Lock reason is required")
return
}

if utils.Validate("lock") {
res := updateLockById(clusterId, reason, http.MethodPost)

if res.StatusCode != http.StatusOK {
result, _ := io.ReadAll(res.Body)
log.Errorf("Could not lock cluster : %s. %s", res.Status, string(result))
} else {
fmt.Println("Cluster locked.")
}
}
}

func UnockById(clusterId string) {
utils.GetAdminUrl()

if utils.Validate("unlock") {
res := updateLockById(clusterId, "", http.MethodDelete)

if res.StatusCode != http.StatusOK {
result, _ := io.ReadAll(res.Body)
log.Errorf("Could not unlock cluster : %s. %s", res.Status, string(result))
} else {
fmt.Println("Cluster unlocked.")
}
}
}

func listLockedClusters() *http.Response {
tokenType, token, err := utils.GetAccessToken()
if err != nil {
Expand All @@ -108,34 +72,3 @@ func listLockedClusters() *http.Response {
}
return res
}

func updateLockById(clusterId string, reason string, method string) *http.Response {
tokenType, token, err := utils.GetAccessToken()
if err != nil {
utils.PrintlnError(err)
os.Exit(0)
}

payload := map[string]string{}
if method == http.MethodPost {
payload["reason"] = reason
}
body, err := json.Marshal(payload)
if err != nil {
log.Fatal(err)
}

url := fmt.Sprintf("%s/cluster/lock/%s", utils.GetAdminUrl(), clusterId)
req, err := http.NewRequest(method, url, bytes.NewBuffer(body))
if err != nil {
log.Fatal(err)
}
req.Header.Set("Authorization", utils.GetAuthorizationHeaderValue(tokenType, token))
req.Header.Set("Content-Type", "application/json")

res, err := http.DefaultClient.Do(req)
if err != nil {
log.Fatal(err)
}
return res
}

0 comments on commit 3aedc34

Please sign in to comment.