-
Notifications
You must be signed in to change notification settings - Fork 4
/
main.go
96 lines (79 loc) · 2.14 KB
/
main.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
package main
import (
"flag"
"os"
"path"
"github.com/AlecAivazis/survey/v2"
"github.com/cloudflare/cfssl/log"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/client-go/kubernetes"
"github.com/yahaa/gen-kubecfg/generate"
"github.com/yahaa/gen-kubecfg/generate/cert"
"github.com/yahaa/gen-kubecfg/generate/token"
"github.com/yahaa/gen-kubecfg/utils"
)
var (
caConfigMap = "extension-apiserver-authentication"
caFileName = "client-ca-file"
kubeConfig string
clientSet *kubernetes.Clientset
)
func init() {
flagSet := flag.CommandLine
flagSet.StringVar(&kubeConfig, "kubeconfig", path.Join(os.Getenv("HOME"), "/.kube/config"), "kubeconfig name")
flagSet.Parse(os.Args[1:])
}
func main() {
cfg, err := utils.NewClusterConfig(kubeConfig)
if err != nil {
log.Fatalf("create cluster config err: %v", err)
}
clientSet, err = utils.NewClientset(kubeConfig)
if err != nil {
log.Fatalf("create k8s client err: %v", err)
}
clusterName, err := generate.GetClusterName(kubeConfig)
if err != nil {
log.Fatalf("get cluster name from kubeConfig err: %v", err)
}
cm, err := clientSet.CoreV1().ConfigMaps("kube-system").Get(caConfigMap, metav1.GetOptions{})
if err != nil {
log.Fatalf("get config map %s err %w", caConfigMap, err)
}
ca, ok := cm.Data[caFileName]
if !ok {
log.Fatalf("not found %s", caFileName)
}
client := generate.NewClient(clientSet)
params := generate.Params{
ClusterEndpoint: cfg.Host,
ClusterName: clusterName,
ClusterCA: ca,
}
var typeQ = []*survey.Question{
{
Name: "type",
Prompt: &survey.Select{
Message: "Please choose an access type of kubeconfig:",
Options: []string{generate.TokenType, generate.ClientCertType},
Default: generate.TokenType,
},
},
}
if err := survey.Ask(typeQ, ¶ms); err != nil {
log.Fatalf("got questions answers err: %v", err)
}
var g generate.Generator
switch params.Type {
case generate.ClientCertType:
g = cert.New(*client)
case generate.TokenType:
g = token.New(*client)
default:
log.Fatalf("not support type: %v", params.Type)
}
g.ParseParams(¶ms)
g.PreGenerate(¶ms)
g.Generate(¶ms)
g.PostGenerate(¶ms)
}