|
| 1 | +package cmd |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "crypto/x509" |
| 6 | + "encoding/pem" |
| 7 | + "errors" |
| 8 | + "fmt" |
| 9 | + "io/fs" |
| 10 | + "os" |
| 11 | + "path/filepath" |
| 12 | + "time" |
| 13 | + |
| 14 | + "github.com/outscale/octl/pkg/debug" |
| 15 | + "github.com/outscale/octl/pkg/messages" |
| 16 | + "github.com/outscale/osc-sdk-go/v3/pkg/oks" |
| 17 | + "github.com/spf13/cobra" |
| 18 | + "k8s.io/client-go/tools/clientcmd" |
| 19 | + kubecmd "k8s.io/kubectl/pkg/cmd" |
| 20 | +) |
| 21 | + |
| 22 | +var kubectlCmd = &cobra.Command{ |
| 23 | + Use: "kubectl cluster_name", |
| 24 | + DisableFlagParsing: true, |
| 25 | + Run: kubectl, |
| 26 | +} |
| 27 | + |
| 28 | +func kubectl(cmd *cobra.Command, args []string) { |
| 29 | + p := loadProfile(cmd) |
| 30 | + cl, err := oks.NewClient(p, sdkOptions(cmd)...) |
| 31 | + if err != nil { |
| 32 | + messages.ExitErr(err) |
| 33 | + } |
| 34 | + cluster := args[0] |
| 35 | + kubeconfig, err := getKubeconfig(cmd.Context(), cluster, cl) |
| 36 | + if err != nil { |
| 37 | + messages.ExitErr(err) |
| 38 | + } |
| 39 | + newArgs := make([]string, 1, len(args)+2) |
| 40 | + newArgs[0] = "kubectl" |
| 41 | + newArgs = append(newArgs, args[1:]...) |
| 42 | + newArgs = append(newArgs, "--kubeconfig", kubeconfig) |
| 43 | + os.Args = newArgs |
| 44 | + |
| 45 | + kubectlCmd := kubecmd.NewDefaultKubectlCommand() |
| 46 | + err = kubectlCmd.ExecuteContext(cmd.Context()) |
| 47 | + if err != nil { |
| 48 | + messages.ExitErr(err) |
| 49 | + } |
| 50 | +} |
| 51 | + |
| 52 | +func getKubeconfig(ctx context.Context, cluster string, cl *oks.Client) (string, error) { |
| 53 | + id, err := clusterNameToID(ctx, cluster, cl) |
| 54 | + if err != nil { |
| 55 | + return "", err |
| 56 | + } |
| 57 | + filename, err := kubeconfigPath(id) |
| 58 | + if err != nil { |
| 59 | + return "", err |
| 60 | + } |
| 61 | + debug.Println("kubeconfig path", filename) |
| 62 | + if _, err := os.Stat(filename); errors.Is(err, fs.ErrNotExist) { |
| 63 | + debug.Println("no kubeconfig; refreshing") |
| 64 | + err = refreshKubeconfig(ctx, id, filename, cl) |
| 65 | + if err != nil { |
| 66 | + return "", err |
| 67 | + } |
| 68 | + return filename, nil |
| 69 | + } |
| 70 | + config, err := clientcmd.LoadFromFile(filename) |
| 71 | + if err != nil { |
| 72 | + return "", err |
| 73 | + } |
| 74 | + for _, user := range config.AuthInfos { |
| 75 | + b, _ := pem.Decode([]byte(user.ClientCertificateData)) |
| 76 | + if b == nil { |
| 77 | + return "", errors.New("invalid kubeconfig certificate") |
| 78 | + } |
| 79 | + var decoded *x509.Certificate |
| 80 | + decoded, err = x509.ParseCertificate(b.Bytes) |
| 81 | + debug.Println("kubeconfig valid until", decoded.NotAfter) |
| 82 | + if err == nil && time.Since(decoded.NotAfter) > 0 { |
| 83 | + debug.Println("expired kubeconfig certificate; refreshing") |
| 84 | + err = refreshKubeconfig(ctx, id, filename, cl) |
| 85 | + } |
| 86 | + if err != nil { |
| 87 | + return "", err |
| 88 | + } |
| 89 | + break |
| 90 | + } |
| 91 | + return filename, nil |
| 92 | +} |
| 93 | + |
| 94 | +func kubeconfigPath(id string) (string, error) { |
| 95 | + dir, err := os.UserConfigDir() |
| 96 | + if err != nil { |
| 97 | + return "", fmt.Errorf("config dir: %w", err) |
| 98 | + } |
| 99 | + path := filepath.Join(dir, "octl", "kube", "kubeconfig") |
| 100 | + if _, err := os.Stat(path); errors.Is(err, fs.ErrNotExist) { |
| 101 | + err = os.MkdirAll(path, 0o700) |
| 102 | + if err != nil { |
| 103 | + return "", fmt.Errorf("config dir: %w", err) |
| 104 | + } |
| 105 | + } |
| 106 | + return filepath.Join(path, id+".kubeconfig"), nil |
| 107 | +} |
| 108 | + |
| 109 | +func refreshKubeconfig(ctx context.Context, id, path string, cl *oks.Client) error { |
| 110 | + messages.Info("Refreshing kubeconfig") |
| 111 | + res, err := cl.GetKubeconfig(ctx, id, &oks.GetKubeconfigParams{}) |
| 112 | + if err != nil { |
| 113 | + return fmt.Errorf("fetch Kubeconfig: %w", err) |
| 114 | + } |
| 115 | + return os.WriteFile(path, []byte(res.Cluster.Data.Kubeconfig), 0o600) |
| 116 | +} |
0 commit comments