forked from k3s-io/helm-controller
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
134 lines (113 loc) · 3.71 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"
helmv1 "github.com/chefmtt/helm-controller-riscv64/pkg/generated/controllers/helm.cattle.io"
helmcontroller "github.com/chefmtt/helm-controller-riscv64/pkg/helm"
batchv1 "github.com/rancher/wrangler-api/pkg/generated/controllers/batch"
corev1 "github.com/rancher/wrangler-api/pkg/generated/controllers/core"
rbacv1 "github.com/rancher/wrangler-api/pkg/generated/controllers/rbac"
"github.com/rancher/wrangler/pkg/apply"
"github.com/rancher/wrangler/pkg/signals"
"github.com/rancher/wrangler/pkg/start"
"github.com/urfave/cli"
"k8s.io/client-go/discovery"
"k8s.io/client-go/tools/clientcmd"
"k8s.io/klog"
)
var (
VERSION = "v0.0.0-dev"
)
func main() {
app := cli.NewApp()
app.Name = "helm-controller"
app.Version = VERSION
app.Usage = "Helm Controller, to help with Helm deployments. Options kubeconfig or masterurl are required."
app.Flags = []cli.Flag{
cli.StringFlag{
Name: "kubeconfig, k",
EnvVar: "KUBECONFIG",
Value: "",
Usage: "Kubernetes config files, e.g. $HOME/.kube/config",
},
cli.StringFlag{
Name: "master, m",
EnvVar: "MASTERURL",
Value: "",
Usage: "Kubernetes cluster master URL.",
},
cli.StringFlag{
Name: "namespace, n",
EnvVar: "NAMESPACE",
Value: "",
Usage: "Namespace to watch, empty means it will watch CRDs in all namespaces.",
},
cli.IntFlag{
Name: "threads, t",
EnvVar: "THREADS",
Value: 2,
Usage: "Threadiness level to set, defaults to 2.",
},
}
app.Action = run
if err := app.Run(os.Args); err != nil {
klog.Fatal(err)
}
}
func run(c *cli.Context) error {
masterURL := c.String("master")
kubeconfig := c.String("kubeconfig")
namespace := c.String("namespace")
threadiness := c.Int("threads")
if threadiness <= 0 {
klog.Infof("Can not start with thread count of %d, please pass a proper thread count.", threadiness)
return nil
}
klog.Infof("Starting helm controller with %d threads.", threadiness)
if namespace == "" {
klog.Info("Starting helm controller with no namespace.")
} else {
klog.Infof("Starting helm controller in namespace: %s.", namespace)
}
ctx := signals.SetupSignalHandler(context.Background())
cfg, err := clientcmd.BuildConfigFromFlags(masterURL, kubeconfig)
if err != nil {
klog.Fatalf("Error building config from flags: %s", err.Error())
}
helms, err := helmv1.NewFactoryFromConfigWithNamespace(cfg, namespace)
if err != nil {
klog.Fatalf("Error building sample controllers: %s", err.Error())
}
batches, err := batchv1.NewFactoryFromConfigWithNamespace(cfg, namespace)
if err != nil {
klog.Fatalf("Error building sample controllers: %s", err.Error())
}
rbacs, err := rbacv1.NewFactoryFromConfigWithNamespace(cfg, namespace)
if err != nil {
klog.Fatalf("Error building sample controllers: %s", err.Error())
}
cores, err := corev1.NewFactoryFromConfigWithNamespace(cfg, namespace)
if err != nil {
klog.Fatalf("Error building sample controllers: %s", err.Error())
}
discoverClient, err := discovery.NewDiscoveryClientForConfig(cfg)
if err != nil {
klog.Fatalf("Error building discovery client: %s", err.Error())
}
objectSetApply := apply.New(discoverClient, apply.NewClientFactory(cfg))
helmcontroller.Register(ctx, objectSetApply,
helms.Helm().V1().HelmChart(),
helms.Helm().V1().HelmChartConfig(),
batches.Batch().V1().Job(),
rbacs.Rbac().V1().ClusterRoleBinding(),
cores.Core().V1().ServiceAccount(),
cores.Core().V1().ConfigMap())
if err := start.All(ctx, threadiness, helms, batches, rbacs, cores); err != nil {
klog.Fatalf("Error starting: %s", err.Error())
}
<-ctx.Done()
return nil
}