-
-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(cmd/scripts/kubernetes): Add
slu s k create-cluster-admin
- Loading branch information
1 parent
a784bb7
commit be392c9
Showing
2 changed files
with
60 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
59 changes: 59 additions & 0 deletions
59
cmd/scripts/kubernetes/create_cluster_admin/create_cluster_admin.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
package create_cluster_admin | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"log" | ||
"strconv" | ||
"time" | ||
|
||
parent_cmd "github.com/sikalabs/slu/cmd/scripts/kubernetes" | ||
"github.com/sikalabs/slu/utils/k8s" | ||
"github.com/sikalabs/slu/utils/k8s_scripts" | ||
"github.com/spf13/cobra" | ||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
) | ||
|
||
var FlagDry bool | ||
|
||
var Cmd = &cobra.Command{ | ||
Use: "create-cluster-admin", | ||
Short: "Create Cluster Admin (RBAC)", | ||
Aliases: []string{"cca"}, | ||
Args: cobra.NoArgs, | ||
Run: func(c *cobra.Command, args []string) { | ||
suffix := strconv.Itoa(int(time.Now().Unix())) | ||
k8s_scripts.CreateClusterAdmin(suffix, FlagDry) | ||
token := getTokenOrDie("kube-system", "cluster-admin-"+suffix) | ||
fmt.Println("cluster-admin-" + suffix) | ||
fmt.Println(token) | ||
}, | ||
} | ||
|
||
func init() { | ||
parent_cmd.Cmd.AddCommand(Cmd) | ||
Cmd.Flags().BoolVar( | ||
&FlagDry, | ||
"dry", | ||
false, | ||
"Dry run", | ||
) | ||
} | ||
|
||
func getTokenOrDie(namespace string, serviceAccount string) string { | ||
clientset, _, _ := k8s.KubernetesClient() | ||
|
||
saClient := clientset.CoreV1().ServiceAccounts(namespace) | ||
secretClient := clientset.CoreV1().Secrets(namespace) | ||
|
||
sa, err := saClient.Get(context.TODO(), serviceAccount, metav1.GetOptions{}) | ||
if err != nil { | ||
log.Fatal(err) | ||
} | ||
secret, err := secretClient.Get(context.TODO(), sa.Secrets[0].Name, metav1.GetOptions{}) | ||
if err != nil { | ||
log.Fatal(err) | ||
} | ||
|
||
return string(secret.Data["token"]) | ||
} |