forked from rancher/gitjob
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
99 lines (85 loc) · 2.21 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
//go:generate go run pkg/codegen/cleanup/main.go
//go:generate go run pkg/codegen/main.go
package main
import (
"context"
"fmt"
"net/http"
_ "net/http/pprof"
"os"
"github.com/rancher/gitjob/pkg/controller"
"github.com/rancher/gitjob/pkg/hooks"
"github.com/rancher/gitjob/pkg/types"
"github.com/rancher/wrangler/pkg/leader"
"github.com/rancher/wrangler/pkg/ratelimit"
"github.com/rancher/wrangler/pkg/resolvehome"
"github.com/rancher/wrangler/pkg/signals"
"github.com/sirupsen/logrus"
"github.com/urfave/cli"
"k8s.io/apimachinery/pkg/util/runtime"
"k8s.io/client-go/tools/clientcmd"
)
var (
Version = "v0.0.0-dev"
GitCommit = "HEAD"
)
func main() {
app := cli.NewApp()
app.Name = "gitjob"
app.Version = fmt.Sprintf("%s (%s)", Version, GitCommit)
app.Flags = []cli.Flag{
cli.StringFlag{
Name: "kubeconfig",
EnvVar: "KUBECONFIG",
},
cli.StringFlag{
Name: "namespace",
EnvVar: "NAMESPACE",
Value: "kube-system",
},
cli.StringFlag{
Name: "listen",
Value: ":8080",
},
cli.StringFlag{
Name: "tekton-image",
Value: "rancher/tekton-utils:dev",
},
}
app.Action = run
if err := app.Run(os.Args); err != nil {
logrus.Fatal(err)
}
}
func run(c *cli.Context) {
logrus.Info("Starting controller")
ctx := signals.SetupSignalHandler(context.Background())
kubeconfig, err := resolvehome.Resolve(c.String("kubeconfig"))
if err != nil {
logrus.Fatalf("Error resolving home dir: %s", err.Error())
}
cfg, err := clientcmd.BuildConfigFromFlags("", kubeconfig)
if err != nil {
logrus.Fatalf("Error building kubeconfig: %s", err.Error())
}
cfg.RateLimiter = ratelimit.None
ctx, cont := types.BuildContext(ctx, c.String("namespace"), cfg)
if err := cont.Start(ctx); err != nil {
logrus.Fatal(err)
}
cont.Image = c.String("tekton-image")
go func() {
leader.RunOrDie(ctx, c.String("namespace"), "gitjob", cont.K8s, func(ctx context.Context) {
controller.Register(ctx, cont)
runtime.Must(cont.Start(ctx))
<-ctx.Done()
})
}()
logrus.Info("Setting up webhook listener")
handler := hooks.HandleHooks(cont)
addr := c.String("listen")
if err := http.ListenAndServe(addr, handler); err != nil {
logrus.Fatalf("Failed to listen on %s: %v", addr, err)
}
<-ctx.Done()
}