-
Notifications
You must be signed in to change notification settings - Fork 9
/
kubectl.go
142 lines (114 loc) · 3.65 KB
/
kubectl.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
// Copyright 2022 Namespace Labs Inc; All rights reserved.
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
package tools
import (
"context"
"fmt"
"os"
"os/exec"
"strings"
"github.com/spf13/cobra"
"k8s.io/client-go/tools/clientcmd"
clientcmdapi "k8s.io/client-go/tools/clientcmd/api"
"namespacelabs.dev/foundation/internal/cli/fncobra"
"namespacelabs.dev/foundation/internal/console"
"namespacelabs.dev/foundation/internal/fnerrors"
"namespacelabs.dev/foundation/internal/localexec"
"namespacelabs.dev/foundation/internal/runtime/kubernetes"
"namespacelabs.dev/foundation/internal/sdk/host"
"namespacelabs.dev/foundation/internal/sdk/kubectl"
"namespacelabs.dev/foundation/internal/workspace/dirs"
"namespacelabs.dev/foundation/std/cfg"
)
func NewKubeCtlCmd(hidden bool) *cobra.Command {
cmd := &cobra.Command{
Use: "kubectl -- ...",
Short: "Run kubectl, configured for the specified environment.",
Example: "ns tools kubectl --env=dev get pod -- -A",
Hidden: hidden,
}
keepConfig := cmd.Flags().Bool("keep_config", false, "If set to true, does not delete the generated configuration.")
return fncobra.CmdWithEnv(cmd, func(ctx context.Context, env cfg.Context, args []string) error {
cfg, err := writeKubeconfig(ctx, env, *keepConfig)
if err != nil {
return err
}
defer func() {
_ = cfg.Cleanup()
}()
cmdLine := append(cfg.BaseArgs(), args...)
if *keepConfig {
fmt.Fprintf(console.Stderr(ctx), "Running kubectl %s\n", strings.Join(cmdLine, " "))
}
kubectlBin, err := kubectl.EnsureSDK(ctx, host.HostPlatform())
if err != nil {
return fnerrors.New("failed to download Kubernetes SDK: %w", err)
}
kubectl := exec.CommandContext(ctx, string(kubectlBin), cmdLine...)
return localexec.RunInteractive(ctx, kubectl)
})
}
type Kubeconfig struct {
Kubeconfig string
Context string
Namespace string
keepConfig bool
}
func writeKubeconfig(ctx context.Context, env cfg.Context, keepConfig bool) (*Kubeconfig, error) {
cluster, err := kubernetes.ConnectToNamespace(ctx, env)
if err != nil {
return nil, err
}
kluster := cluster.Cluster().(*kubernetes.Cluster)
k8sconfig := cluster.KubeConfig()
rawConfig, err := kluster.PreparedClient().ClientConfig.RawConfig()
if err != nil {
return nil, fnerrors.New("failed to generate kubeconfig: %w", err)
}
c, err := WriteRawKubeconfig(ctx, rawConfig, keepConfig)
if err != nil {
return nil, err
}
c.Namespace = k8sconfig.Namespace
c.Context = k8sconfig.Context
return c, nil
}
func WriteRawKubeconfig(ctx context.Context, rawConfig clientcmdapi.Config, keepConfig bool) (*Kubeconfig, error) {
configBytes, err := clientcmd.Write(rawConfig)
if err != nil {
return nil, fnerrors.New("failed to serialize kubeconfig: %w", err)
}
tmpFile, err := dirs.CreateUserTemp("kubeconfig", "*.yaml")
if err != nil {
return nil, fnerrors.New("failed to create temp file: %w", err)
}
if _, err := tmpFile.Write(configBytes); err != nil {
return nil, fnerrors.New("failed to write kubeconfig: %w", err)
}
if err := tmpFile.Close(); err != nil {
return nil, fnerrors.New("failed to close kubeconfig: %w", err)
}
return &Kubeconfig{
Kubeconfig: tmpFile.Name(),
keepConfig: keepConfig,
}, nil
}
func (kc *Kubeconfig) BaseArgs() []string {
baseArgs := []string{
"--kubeconfig=" + kc.Kubeconfig,
}
if kc.Namespace != "" {
baseArgs = append(baseArgs, "-n", kc.Namespace)
}
if kc.Context != "" {
baseArgs = append(baseArgs, "--context", kc.Context)
}
return baseArgs
}
func (kc *Kubeconfig) Cleanup() error {
if kc.keepConfig {
return nil
}
return os.Remove(kc.Kubeconfig)
}