-
Notifications
You must be signed in to change notification settings - Fork 15
/
kubernetes_issue.go
71 lines (62 loc) · 1.9 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
66
67
68
69
70
71
package cmd
import (
"context"
"errors"
"fmt"
"net"
"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
GroupName string
UserName string
}
// kubernetesIssueCmd represents the "kubernetes issue" command
var kubernetesIssueCmd = &cobra.Command{
Use: "issue",
Short: "issue client certificates for k8s user",
Long: `Issue TLS client certificates for k8s 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
}
server := "https://kubernetes.default.svc"
if _, err := net.LookupHost("kubernetes.default.svc"); err != nil {
cpNodes := cke.ControlPlanes(cluster.Nodes)
if len(cpNodes) == 0 {
return errors.New("no control plane")
}
server = "https://" + cpNodes[0].Address + ":6443"
}
cacert, err := storage.GetCACertificate(ctx, cke.CAKubernetes)
if err != nil {
return err
}
cert, key, err := cke.KubernetesCA{}.IssueUserCert(ctx, inf, kubernetesIssueOpts.UserName, kubernetesIssueOpts.GroupName, kubernetesIssueOpts.TTL)
if err != nil {
return err
}
cfg := cke.UserKubeconfig(cluster.Name, kubernetesIssueOpts.UserName, 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")
fs.StringVarP(&kubernetesIssueOpts.GroupName, "group", "g", cke.AdminGroup, "Group name of the issuing config")
fs.StringVarP(&kubernetesIssueOpts.UserName, "user", "u", cke.RoleAdmin, "User name of the issuing config")
kubernetesCmd.AddCommand(kubernetesIssueCmd)
}