-
Notifications
You must be signed in to change notification settings - Fork 15
/
kubernetes_issue.go
65 lines (56 loc) · 1.54 KB
/
kubernetes_issue.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
package cmd
import (
"context"
"errors"
"fmt"
"github.com/cybozu-go/cke"
"github.com/cybozu-go/well"
"github.com/spf13/cobra"
"k8s.io/client-go/tools/clientcmd"
)
var kubernetesIssueOpts struct {
TTL string
}
// kubernetesIssueCmd represents the "kubernetes issue" command
var kubernetesIssueCmd = &cobra.Command{
Use: "issue",
Short: "issue client certificates for k8s admin",
Long: `Issue TLS client certificates for k8s admin user.`,
RunE: func(cmd *cobra.Command, args []string) error {
well.Go(func(ctx context.Context) error {
cluster, err := storage.GetCluster(ctx)
if err != nil {
return err
}
cpNodes := cke.ControlPlanes(cluster.Nodes)
if len(cpNodes) == 0 {
return errors.New("no control plane")
}
// TODO: Replace `server` from node IP to Ingress address.
// Since there is no Ingress yet, use the node IP.
server := "https://" + cpNodes[0].Address + ":6443"
cacert, err := storage.GetCACertificate(ctx, "kubernetes")
if err != nil {
return err
}
cert, key, err := cke.KubernetesCA{}.IssueAdminCert(ctx, inf, kubernetesIssueOpts.TTL)
if err != nil {
return err
}
cfg := cke.AdminKubeconfig(cluster.Name, cacert, cert, key, server)
src, err := clientcmd.Write(*cfg)
if err != nil {
return err
}
_, err = fmt.Println(string(src))
return err
})
well.Stop()
return well.Wait()
},
}
func init() {
fs := kubernetesIssueCmd.Flags()
fs.StringVar(&kubernetesIssueOpts.TTL, "ttl", "2h", "TTL of the certificate")
kubernetesCmd.AddCommand(kubernetesIssueCmd)
}