forked from openshift/machine-api-operator
/
start.go
93 lines (76 loc) · 2.39 KB
/
start.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
package main
import (
"flag"
"github.com/golang/glog"
"github.com/openshift/machine-api-operator/pkg/operator"
"github.com/openshift/machine-api-operator/pkg/version"
"github.com/spf13/cobra"
"k8s.io/client-go/tools/leaderelection"
)
var (
startCmd = &cobra.Command{
Use: "start",
Short: "Starts Machine API Operator",
Long: "",
Run: runStartCmd,
}
startOpts struct {
kubeconfig string
imagesFile string
}
)
func init() {
rootCmd.AddCommand(startCmd)
startCmd.PersistentFlags().StringVar(&startOpts.kubeconfig, "kubeconfig", "", "Kubeconfig file to access a remote cluster (testing only)")
startCmd.PersistentFlags().StringVar(&startOpts.imagesFile, "images-json", "", "images.json file for MAO.")
}
func runStartCmd(cmd *cobra.Command, args []string) {
flag.Set("logtostderr", "true")
flag.Parse()
// To help debugging, immediately log version
glog.Infof("Version: %+v", version.Version)
if startOpts.imagesFile == "" {
glog.Fatalf("--images-json should not be empty")
}
cb, err := NewClientBuilder(startOpts.kubeconfig)
if err != nil {
glog.Fatalf("error creating clients: %v", err)
}
stopCh := make(chan struct{})
run := func(stop <-chan struct{}) {
ctx := CreateControllerContext(cb, stopCh, componentNamespace)
if err := startControllers(ctx); err != nil {
glog.Fatalf("error starting controllers: %v", err)
}
ctx.KubeNamespacedInformerFactory.Start(ctx.Stop)
close(ctx.InformersStarted)
select {}
}
leaderelection.RunOrDie(leaderelection.LeaderElectionConfig{
Lock: CreateResourceLock(cb, componentNamespace, componentName),
LeaseDuration: LeaseDuration,
RenewDeadline: RenewDeadline,
RetryPeriod: RetryPeriod,
Callbacks: leaderelection.LeaderCallbacks{
OnStartedLeading: run,
OnStoppedLeading: func() {
glog.Fatalf("leaderelection lost")
},
},
})
panic("unreachable")
}
func startControllers(ctx *ControllerContext) error {
go operator.New(
componentNamespace, componentName,
startOpts.imagesFile,
config,
ctx.KubeNamespacedInformerFactory.Core().V1().ServiceAccounts(),
ctx.KubeNamespacedInformerFactory.Apps().V1().Deployments(),
ctx.KubeNamespacedInformerFactory.Rbac().V1().ClusterRoles(),
ctx.KubeNamespacedInformerFactory.Rbac().V1().ClusterRoleBindings(),
ctx.ClientBuilder.KubeClientOrDie(componentName),
ctx.ClientBuilder.OpenshiftClientOrDie(componentName),
).Run(2, ctx.Stop)
return nil
}