Skip to content

Commit 04d60e5

Browse files
committed
wip
1 parent 118b24a commit 04d60e5

File tree

4 files changed

+122
-11
lines changed

4 files changed

+122
-11
lines changed

internal/cli/cluster/cmds.go

+1
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ func Command() *cli.Command {
1717
func subCommands() []*cli.Command {
1818
return []*cli.Command{
1919
kubeconfigCmd(),
20+
kubeconfigV2Cmd(),
2021
listCmd(),
2122
}
2223
}

internal/cli/cluster/kubeconfig.go

+62
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,13 @@ package cluster
22

33
import (
44
"fmt"
5+
"path/filepath"
56
"strings"
67

78
"github.com/nais/narcos/internal/gcp"
89
"github.com/nais/narcos/internal/kubeconfig"
910
"github.com/urfave/cli/v2"
11+
kubeClient "k8s.io/client-go/tools/clientcmd"
1012
)
1113

1214
func kubeconfigCmd() *cli.Command {
@@ -74,3 +76,63 @@ gcloud auth login --update-adc`,
7476
},
7577
}
7678
}
79+
80+
func kubeconfigV2Cmd() *cli.Command {
81+
tenantConfigDir := filepath.Join(kubeClient.RecommendedConfigDir, "tenants")
82+
desc := fmt.Sprintf(`Create kubeconfig files for connecting to available clusters.
83+
Will create config files in %q.
84+
This requires that you have the gcloud command line tool installed, configured and logged in using:
85+
gcloud auth login --update-adc
86+
`, tenantConfigDir)
87+
return &cli.Command{
88+
Name: "kubeconfigv2",
89+
Aliases: []string{"kcv2"},
90+
Description: desc,
91+
Flags: []cli.Flag{
92+
&cli.BoolFlag{
93+
Name: "verbose",
94+
Aliases: []string{"v"},
95+
},
96+
},
97+
UseShortOptionHandling: true,
98+
Before: func(context *cli.Context) error {
99+
return gcp.ValidateUserLogin(context.Context)
100+
},
101+
Action: func(context *cli.Context) error {
102+
verbose := context.Bool("verbose")
103+
104+
fmt.Println("Getting clusters...")
105+
clusters, err := gcp.GetClusters(context.Context)
106+
if err != nil {
107+
return err
108+
}
109+
110+
if len(clusters) == 0 {
111+
return fmt.Errorf("no clusters found")
112+
}
113+
114+
fmt.Printf("Found %v clusters\n", len(clusters))
115+
116+
emails, err := gcp.GetUserEmails(context.Context)
117+
if err != nil {
118+
return err
119+
}
120+
121+
hasSuffix := func(emails []string, suffix string) string {
122+
for _, email := range emails {
123+
if strings.HasSuffix(email, suffix) {
124+
return email
125+
}
126+
}
127+
panic("no user with suffix " + suffix + " found")
128+
}
129+
130+
err = kubeconfig.RecreateCreateTenantKubeconfigs(hasSuffix(emails, "@nais.io"), clusters, verbose)
131+
if err != nil {
132+
return err
133+
}
134+
135+
return nil
136+
},
137+
}
138+
}

internal/kubeconfig/kubeconfig.go

+55-5
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"fmt"
55
"os"
66
"os/exec"
7+
"path/filepath"
78

89
"github.com/go-logr/logr"
910
"github.com/nais/narcos/internal/gcp"
@@ -30,11 +31,7 @@ func CreateKubeconfig(email string, clusters []gcp.Cluster, overwrite, clean, ve
3031
config.Clusters = map[string]*api.Cluster{}
3132
}
3233

33-
err = addUsers(config, clusters, email, overwrite, verbose)
34-
if err != nil {
35-
return err
36-
}
37-
34+
addUsers(config, clusters, email, overwrite, verbose)
3835
err = addClusters(config, clusters, email, overwrite, verbose)
3936
if err != nil {
4037
return err
@@ -59,3 +56,56 @@ func CreateKubeconfig(email string, clusters []gcp.Cluster, overwrite, clean, ve
5956
}
6057
return nil
6158
}
59+
60+
func RecreateCreateTenantKubeconfigs(email string, clusters []gcp.Cluster, verbose bool) error {
61+
overwrite := true
62+
clustersByTenant := make(map[string][]gcp.Cluster)
63+
for _, cluster := range clusters {
64+
clustersByTenant[cluster.Tenant] = append(clustersByTenant[cluster.Tenant], cluster)
65+
}
66+
67+
for tenant, clusters := range clustersByTenant {
68+
// first we wipe/create the config file
69+
dir := filepath.Join(kubeClient.RecommendedConfigDir, "tenants")
70+
err := os.MkdirAll(dir, 0500)
71+
if err != nil {
72+
return err
73+
}
74+
75+
path := filepath.Join(dir, tenant+".yaml")
76+
_, err = os.Create(path)
77+
if err != nil {
78+
return fmt.Errorf("make config file(%q): %w", path, err)
79+
}
80+
81+
config := api.NewConfig()
82+
config.AuthInfos = map[string]*api.AuthInfo{}
83+
config.Contexts = map[string]*api.Context{}
84+
config.Clusters = map[string]*api.Cluster{}
85+
86+
addUsers(config, clusters, email, overwrite, verbose)
87+
err = addClusters(config, clusters, email, overwrite, verbose)
88+
if err != nil {
89+
return err
90+
}
91+
92+
err = kubeClient.WriteToFile(*config, path)
93+
if err != nil {
94+
return err
95+
}
96+
97+
fmt.Println("Kubeconfig written to", path)
98+
99+
for _, user := range config.AuthInfos {
100+
if user == nil || user.Exec == nil {
101+
continue
102+
}
103+
_, err = exec.LookPath(user.Exec.Command)
104+
if err != nil {
105+
fmt.Printf("%v\nWARNING: %v not found in PATH.\n", os.Stderr, user.Exec.Command)
106+
fmt.Printf("%v\n%v\n", os.Stderr, user.Exec.InstallHint)
107+
}
108+
}
109+
}
110+
return nil
111+
}

internal/kubeconfig/user.go

+4-6
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,12 @@ import (
77
clientcmdapi "k8s.io/client-go/tools/clientcmd/api"
88
)
99

10-
func addUsers(config *clientcmdapi.Config, clusters []gcp.Cluster, email string, overwrite, verbose bool) error {
10+
func addUsers(config *clientcmdapi.Config, clusters []gcp.Cluster, email string, overwrite, verbose bool) {
1111
addGCPUser(config, email, overwrite, verbose)
12-
13-
return addOnpremUser(config, clusters, overwrite, verbose)
12+
addOnpremUser(config, clusters, overwrite, verbose)
1413
}
1514

16-
func addOnpremUser(config *clientcmdapi.Config, clusters []gcp.Cluster, overwrite, verbose bool) error {
15+
func addOnpremUser(config *clientcmdapi.Config, clusters []gcp.Cluster, overwrite, verbose bool) {
1716
for _, cluster := range clusters {
1817
if cluster.Kind == gcp.KindOnprem {
1918
user := cluster.User
@@ -48,10 +47,9 @@ func addOnpremUser(config *clientcmdapi.Config, clusters []gcp.Cluster, overwrit
4847

4948
fmt.Printf("Added user %v to config\n", user.UserName)
5049

51-
return nil
50+
return
5251
}
5352
}
54-
return nil
5553
}
5654

5755
func addGCPUser(config *clientcmdapi.Config, email string, overwrite, verbose bool) {

0 commit comments

Comments
 (0)