forked from ibuildthecloud/klum
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
132 lines (116 loc) · 3.16 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
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
//go:generate go run pkg/codegen/cleanup/main.go
//go:generate go run pkg/codegen/main.go
package main
import (
"fmt"
"os"
"github.com/ibuildthecloud/klum/pkg/controllers/user"
"github.com/ibuildthecloud/klum/pkg/crd"
"github.com/ibuildthecloud/klum/pkg/generated/controllers/klum.cattle.io"
"github.com/rancher/wrangler-api/pkg/generated/controllers/core"
"github.com/rancher/wrangler-api/pkg/generated/controllers/rbac"
"github.com/rancher/wrangler/pkg/apply"
"github.com/rancher/wrangler/pkg/kubeconfig"
"github.com/rancher/wrangler/pkg/signals"
"github.com/rancher/wrangler/pkg/start"
"github.com/sirupsen/logrus"
"github.com/urfave/cli"
)
var (
Version = "v0.0.0-dev"
GitCommit = "HEAD"
cfg user.Config
kubeConfig string
)
func main() {
app := cli.NewApp()
app.Name = "klum"
app.Version = fmt.Sprintf("%s (%s)", Version, GitCommit)
app.Usage = "Kubernetes Lazy User Manager"
app.Flags = []cli.Flag{
cli.StringFlag{
Name: "kubeconfig",
EnvVar: "KUBECONFIG",
Destination: &kubeConfig,
},
cli.StringFlag{
Name: "namespace",
EnvVar: "NAMESPACE",
Usage: "Namespace to create secrets and SAs in",
Value: "klum",
Destination: &cfg.Namespace,
},
cli.StringFlag{
Name: "context-name",
Usage: "Context name to put in Kubeconfigs",
EnvVar: "CONTEXT_NAME",
Value: "default",
Destination: &cfg.ContextName,
},
cli.StringFlag{
Name: "server",
Usage: "The external server field to put in the Kubeconfigs",
EnvVar: "SERVER_NAME",
Value: "https://localhost:6443",
Destination: &cfg.Server,
},
cli.StringFlag{
Name: "ca",
Usage: "The value of the CA data to put in the Kubeconfig",
EnvVar: "CA",
Destination: &cfg.CA,
},
cli.StringFlag{
Name: "default-cluster-role",
Usage: "Default cluster-role to assign to users with no roles",
EnvVar: "DEFAULT_CLUSTER_ROLE",
Value: "cluster-admin",
Destination: &cfg.DefaultClusterRole,
},
}
app.Action = run
if err := app.Run(os.Args); err != nil {
logrus.Fatal(err)
}
}
func run(c *cli.Context) error {
logrus.Info("Starting klum controller")
ctx := signals.SetupSignalContext()
restConfig, err := kubeconfig.GetNonInteractiveClientConfig(kubeConfig).ClientConfig()
if err != nil {
return err
}
if err := crd.Create(ctx, restConfig); err != nil {
return err
}
core, err := core.NewFactoryFromConfig(restConfig)
if err != nil {
return err
}
klum, err := klum.NewFactoryFromConfigWithNamespace(restConfig, cfg.Namespace)
if err != nil {
return err
}
rbac, err := rbac.NewFactoryFromConfig(restConfig)
if err != nil {
return err
}
apply, err := apply.NewForConfig(restConfig)
if err != nil {
return nil
}
user.Register(ctx,
cfg,
apply,
core.Core().V1().ServiceAccount(),
rbac.Rbac().V1().ClusterRoleBinding(),
rbac.Rbac().V1().RoleBinding(),
core.Core().V1().Secret(),
klum.Klum().V1alpha1().Kubeconfig(),
klum.Klum().V1alpha1().User())
if err := start.All(ctx, 2, klum, core, rbac); err != nil {
logrus.Fatalf("Error starting: %s", err.Error())
}
<-ctx.Done()
return nil
}