forked from rancher/terraform-controller
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
134 lines (111 loc) · 3.33 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
133
134
//go:generate go run pkg/codegen/cleanup/main.go
//go:generate /bin/rm -rf pkg/generated
//go:generate go run pkg/codegen/main.go
package main
import (
"context"
"os"
"github.com/rancher/terraform-controller/pkg/generated/controllers/terraformcontroller.cattle.io"
"github.com/rancher/terraform-controller/pkg/terraform"
"github.com/rancher/wrangler/pkg/generated/controllers/batch"
"github.com/rancher/wrangler/pkg/generated/controllers/core"
"github.com/rancher/wrangler/pkg/generated/controllers/rbac"
"github.com/rancher/wrangler/pkg/resolvehome"
"github.com/rancher/wrangler/pkg/signals"
"github.com/rancher/wrangler/pkg/start"
"github.com/sirupsen/logrus"
"github.com/urfave/cli"
"k8s.io/client-go/tools/clientcmd"
)
var (
VERSION = "v0.0.0-dev"
)
func main() {
app := cli.NewApp()
app.Name = "terraform-controller"
app.Version = VERSION
app.Flags = []cli.Flag{
cli.IntFlag{
Name: "threads",
EnvVar: "THREADS",
Value: 2,
},
cli.BoolFlag{
Name: "debug",
EnvVar: "DEBUG",
},
cli.StringFlag{
Name: "kubeconfig",
EnvVar: "KUBECONFIG",
Value: "${HOME}/.kube/config",
},
cli.StringFlag{
Name: "namespace",
EnvVar: "NAMESPACE",
Value: "default",
},
cli.StringFlag{
Name: "masterurl",
EnvVar: "MASTERURL",
Value: "",
},
}
app.Action = run
if err := app.Run(os.Args); err != nil {
logrus.Fatal(err)
}
}
func run(c *cli.Context) {
if c.Bool("debug") {
logrus.SetLevel(logrus.DebugLevel)
logrus.SetReportCaller(true)
}
logrus.Info("Starting Terraform Controller")
kubeconfig, err := resolvehome.Resolve(c.String("kubeconfig"))
if err != nil {
logrus.Info("Resolving home dir failed.")
}
if _, err := os.Stat(kubeconfig); os.IsNotExist(err) {
kubeconfig = ""
}
threadiness := c.Int("threads")
masterurl := c.String("masterurl")
ns := c.String("namespace")
logrus.Printf("Booting Terraform Controller, namespace: %s", ns)
ctx := signals.SetupSignalHandler(context.Background())
cfg, err := clientcmd.BuildConfigFromFlags(masterurl, kubeconfig)
if err != nil {
logrus.Fatalf("Error building kubeconfig: %s", err.Error())
}
tfFactory, err := terraformcontroller.NewFactoryFromConfigWithNamespace(cfg, ns)
if err != nil {
logrus.Fatalf("Error building terraform controllers: %s", err.Error())
}
coreFactory, err := core.NewFactoryFromConfigWithNamespace(cfg, ns)
if err != nil {
logrus.Fatalf("Error building core controllers: %s", err.Error())
}
rbacFactory, err := rbac.NewFactoryFromConfigWithNamespace(cfg, ns)
if err != nil {
logrus.Fatalf("Error building rbac controllers: %s", err.Error())
}
batchFactory, err := batch.NewFactoryFromConfigWithNamespace(cfg, ns)
if err != nil {
logrus.Fatalf("Error building rbac controllers: %s", err.Error())
}
terraform.Register(ctx,
tfFactory.Terraformcontroller().V1().Module(),
tfFactory.Terraformcontroller().V1().State(),
tfFactory.Terraformcontroller().V1().Execution(),
rbacFactory.Rbac().V1().ClusterRole(),
rbacFactory.Rbac().V1().ClusterRoleBinding(),
coreFactory.Core().V1().Secret(),
coreFactory.Core().V1().ConfigMap(),
coreFactory.Core().V1().ServiceAccount(),
batchFactory.Batch().V1().Job(),
)
if err := start.All(ctx, threadiness, tfFactory, coreFactory, rbacFactory, batchFactory); err != nil {
logrus.Fatalf("Error starting: %s", err.Error())
}
<-ctx.Done()
}