Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion cmd/admin_k9s.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ func launchK9s(args []string) {
}

clusterId := args[0]
kubeconfig := pkg.GetKubeconfigByClusterId(clusterId)
kubeconfig := pkg.GetKubeconfigByClusterId(clusterId, false)
filePath := utils.WriteInFile(clusterId, "kubeconfig", []byte(kubeconfig))
if err := os.Setenv("KUBECONFIG", filePath); err != nil {
log.Fatal(err)
Expand Down
9 changes: 6 additions & 3 deletions cmd/cluster_get_token.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,20 @@ import (
"github.com/spf13/cobra"
)

var getTokenReadOnly bool

var getTokenCommand = &cobra.Command{
Use: "get-token",
Short: "Get token for a cluster ID",
Run: func(cmd *cobra.Command, args []string) {
validateGetTokenFlags()
getToken()
getToken(getTokenReadOnly)
},
}

func init() {
getTokenCommand.Flags().StringVarP(&clusterId, "cluster-id", "c", "", "Cluster ID")
getTokenCommand.Flags().BoolVarP(&getTokenReadOnly, "read-only", "r", false, "Get a read-only service account token instead of an admin token")
clusterCmd.AddCommand(getTokenCommand)
}

Expand All @@ -27,7 +30,7 @@ func validateGetTokenFlags() {
}
}

func getToken() {
response := pkg.GetTokenByClusterId(clusterId)
func getToken(readOnly bool) {
response := pkg.GetTokenByClusterId(clusterId, readOnly)
utils.Println(response)
}
21 changes: 14 additions & 7 deletions cmd/cluster_kubeconfig.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,19 +12,25 @@ import (
"github.com/spf13/cobra"
)

var readOnlyKubeconfig bool

var downloadKubeconfigCmd = &cobra.Command{
Use: "kubeconfig",
Short: "Retrieve kubeconfig with a cluster ID",
Run: func(cmd *cobra.Command, args []string) {
validateKubeconfigFlags()
kubeconfigFilename := downloadKubeconfig(clusterId)
kubeconfigFilename := downloadKubeconfig(clusterId, readOnlyKubeconfig)
log.Info("Kubeconfig file created in the current directory.")
log.Info("Execute `export KUBECONFIG=" + kubeconfigFilename + "` to use it.")
if readOnlyKubeconfig {
log.Info("This kubeconfig uses read-only access (ServiceAccount with view ClusterRole).")
}
},
}

func init() {
downloadKubeconfigCmd.Flags().StringVarP(&clusterId, "cluster-id", "c", "", "Cluster ID")
downloadKubeconfigCmd.Flags().BoolVarP(&readOnlyKubeconfig, "read-only", "r", false, "Download a read-only kubeconfig backed by a Kubernetes service account with the view ClusterRole")
clusterCmd.AddCommand(downloadKubeconfigCmd)
}

Expand All @@ -35,20 +41,21 @@ func validateKubeconfigFlags() {
}
}

func downloadKubeconfig(clusterId string) string {
// download kubeconfig
kubeconfig := pkg.GetKubeconfigByClusterId(clusterId)
func downloadKubeconfig(clusterId string, readOnly bool) string {
kubeconfig := pkg.GetKubeconfigByClusterId(clusterId, readOnly)

// get current working directory
dir, err := os.Getwd()

if err != nil {
utils.PrintlnError(err)
os.Exit(1)
}

kubeconfigFilename := filepath.Join(dir, "kubeconfig-"+clusterId+".yaml")
// create a file in the current folder
suffix := ""
if readOnly {
suffix = "-readonly"
}
kubeconfigFilename := filepath.Join(dir, "kubeconfig"+suffix+"-"+clusterId+".yaml")
writeError := os.WriteFile(kubeconfigFilename, []byte(kubeconfig), 0600)
if writeError != nil {
utils.PrintlnError(writeError)
Expand Down
2 changes: 1 addition & 1 deletion cmd/terraform_setup_backend.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ var terraformSetupBackendCmd = &cobra.Command{
utils.Println(fmt.Sprintf("Preparing backend.tf file for terraform `%s` of environment `%s`", terraform.Name, env.Name))

// Download kubeconfig to connect to the cluster
kubeconfigPath := downloadKubeconfig(env.ClusterId)
kubeconfigPath := downloadKubeconfig(env.ClusterId, false)

// Create kubeclient to retrieve the namespace of the tfstate secret
kubeconfig, err := clientcmd.BuildConfigFromFlags("", kubeconfigPath)
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ require (
github.com/pkg/errors v0.9.1
github.com/posthog/posthog-go v1.12.5
github.com/pterm/pterm v0.12.83
github.com/qovery/qovery-client-go v0.0.0-20260609072636-f548ebe903f2
github.com/qovery/qovery-client-go v0.0.0-20260610095547-986d768ca7f9
github.com/sirupsen/logrus v1.9.4
github.com/spf13/cobra v1.10.2
github.com/spf13/pflag v1.0.10
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -189,8 +189,8 @@ github.com/posthog/posthog-go v1.12.5 h1:l/x3mpqisXJ0sTOyyRutsTQAgiWYuJT1uhN4cQr
github.com/posthog/posthog-go v1.12.5/go.mod h1:xsVOW9YImilUcazwPNEq4PJDqEZf2KeCS758zXjwkPg=
github.com/pterm/pterm v0.12.83 h1:ie+YmGmA727VuhxBlyGr74Ks+7McV6kT99IB8EU80aA=
github.com/pterm/pterm v0.12.83/go.mod h1:xlgc6bFWyJIMtmLJvGim+L7jhSReilOlOnodeIYe4Tk=
github.com/qovery/qovery-client-go v0.0.0-20260609072636-f548ebe903f2 h1:R6lG3dFH9/N7k7Hz+MMMCY1y3c0N9rmY6NAAjy1dkos=
github.com/qovery/qovery-client-go v0.0.0-20260609072636-f548ebe903f2/go.mod h1:mcXeQtxR4AIGIBaWLhy52S16UwL8/1fcDywDuSK1BZ4=
github.com/qovery/qovery-client-go v0.0.0-20260610095547-986d768ca7f9 h1:vYYPlj1RNfR/8RXTE8ATWsguR25mVxktq3HNIUqPhyA=
github.com/qovery/qovery-client-go v0.0.0-20260610095547-986d768ca7f9/go.mod h1:mcXeQtxR4AIGIBaWLhy52S16UwL8/1fcDywDuSK1BZ4=
github.com/rogpeppe/go-internal v1.14.1 h1:UQB4HGPB6osV0SQTLymcB4TgvyWu6ZyliaW0tI/otEQ=
github.com/rogpeppe/go-internal v1.14.1/go.mod h1:MaRKkUm5W0goXpeCfT7UZI6fk/L7L7so1lCWt35ZSgc=
github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=
Expand Down
2 changes: 1 addition & 1 deletion pkg/admin_load_credentials.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ func LoadCredentials(clusterId string, doNotConnectToBastion bool) error {
}
utils.PrintlnInfo(fmt.Sprintf("Set environment variable %s for child process", cred.Key))
}
kubeconfig := GetKubeconfigByClusterId(clusterId)
kubeconfig := GetKubeconfigByClusterId(clusterId, false)
filePath := utils.WriteInFile(clusterId, "kubeconfig", []byte(kubeconfig))
if err := os.Setenv("KUBECONFIG", filePath); err != nil {
return fmt.Errorf("failed to set KUBECONFIG: %w", err)
Expand Down
12 changes: 10 additions & 2 deletions pkg/cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,19 @@ import (
"github.com/qovery/qovery-client-go"
)

func GetKubeconfigByClusterId(clusterId string) string {
func GetKubeconfigByClusterId(clusterId string, readOnly bool) string {
qoveryClient := GetQoveryClientInstance()

request := qoveryClient.ClustersAPI.GetClusterKubeconfig(
context.Background(),
"00000000-0000-0000-000000000000",
clusterId,
).WithTokenFromCli(true)

if readOnly {
request = request.ReadOnly(true)
}

response, httpResponse, err := qoveryClient.ClustersAPI.GetClusterKubeconfigExecute(request)
if err != nil {
utils.PrintlnError(err)
Expand Down Expand Up @@ -50,10 +55,13 @@ func UpdateClusterKubeconfig(organizationId string, clusterId string, kubeconfig
return nil
}

func GetTokenByClusterId(clusterId string) string {
func GetTokenByClusterId(clusterId string, readOnly bool) string {
qoveryClient := GetQoveryClientInstance()

request := qoveryClient.DefaultAPI.GetClusterTokenByClusterId(context.Background(), clusterId)
if readOnly {
request = request.ReadOnly(true)
}
_, response, err := qoveryClient.DefaultAPI.GetClusterTokenByClusterIdExecute(request)
if err != nil {
utils.PrintlnError(err)
Expand Down
Loading