forked from openshift/machine-config-operator
/
start.go
103 lines (89 loc) · 3.58 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
94
95
96
97
98
99
100
101
102
103
package main
import (
"context"
"flag"
"github.com/golang/glog"
"github.com/openshift/machine-config-operator/cmd/common"
"github.com/openshift/machine-config-operator/internal/clients"
controllercommon "github.com/openshift/machine-config-operator/pkg/controller/common"
"github.com/openshift/machine-config-operator/pkg/operator"
"github.com/openshift/machine-config-operator/pkg/version"
"github.com/spf13/cobra"
"k8s.io/client-go/tools/leaderelection"
)
var (
startCmd = &cobra.Command{
Use: "start",
Short: "Starts Machine Config 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 MCO.")
}
func runStartCmd(cmd *cobra.Command, args []string) {
flag.Set("logtostderr", "true")
flag.Parse()
// To help debugging, immediately log version
glog.Infof("Version: %+v (%s)", version.Version, version.Hash)
if startOpts.imagesFile == "" {
glog.Fatal("--images-json cannot be empty")
}
cb, err := clients.NewBuilder(startOpts.kubeconfig)
if err != nil {
glog.Fatalf("error creating clients: %v", err)
}
run := func(ctx context.Context) {
ctrlctx := controllercommon.CreateControllerContext(cb, ctx.Done(), componentNamespace)
controller := operator.New(
componentNamespace, componentName,
startOpts.imagesFile,
ctrlctx.NamespacedInformerFactory.Machineconfiguration().V1().MachineConfigPools(),
ctrlctx.NamespacedInformerFactory.Machineconfiguration().V1().MachineConfigs(),
ctrlctx.NamespacedInformerFactory.Machineconfiguration().V1().ControllerConfigs(),
ctrlctx.KubeNamespacedInformerFactory.Core().V1().ServiceAccounts(),
ctrlctx.APIExtInformerFactory.Apiextensions().V1beta1().CustomResourceDefinitions(),
ctrlctx.KubeNamespacedInformerFactory.Apps().V1().Deployments(),
ctrlctx.KubeNamespacedInformerFactory.Apps().V1().DaemonSets(),
ctrlctx.KubeNamespacedInformerFactory.Rbac().V1().ClusterRoles(),
ctrlctx.KubeNamespacedInformerFactory.Rbac().V1().ClusterRoleBindings(),
ctrlctx.KubeNamespacedInformerFactory.Core().V1().ConfigMaps(),
ctrlctx.KubeInformerFactory.Core().V1().ConfigMaps(),
ctrlctx.ConfigInformerFactory.Config().V1().Infrastructures(),
ctrlctx.ConfigInformerFactory.Config().V1().Networks(),
ctrlctx.ConfigInformerFactory.Config().V1().Proxies(),
ctrlctx.ClientBuilder.MachineConfigClientOrDie(componentName),
ctrlctx.ClientBuilder.KubeClientOrDie(componentName),
ctrlctx.ClientBuilder.APIExtClientOrDie(componentName),
ctrlctx.ClientBuilder.ConfigClientOrDie(componentName),
)
ctrlctx.NamespacedInformerFactory.Start(ctrlctx.Stop)
ctrlctx.KubeInformerFactory.Start(ctrlctx.Stop)
ctrlctx.KubeNamespacedInformerFactory.Start(ctrlctx.Stop)
ctrlctx.APIExtInformerFactory.Start(ctrlctx.Stop)
ctrlctx.ConfigInformerFactory.Start(ctrlctx.Stop)
close(ctrlctx.InformersStarted)
go controller.Run(2, ctrlctx.Stop)
select {}
}
leaderelection.RunOrDie(context.TODO(), leaderelection.LeaderElectionConfig{
Lock: common.CreateResourceLock(cb, componentNamespace, componentName),
LeaseDuration: common.LeaseDuration,
RenewDeadline: common.RenewDeadline,
RetryPeriod: common.RetryPeriod,
Callbacks: leaderelection.LeaderCallbacks{
OnStartedLeading: run,
OnStoppedLeading: func() {
glog.Fatalf("leaderelection lost")
},
},
})
panic("unreachable")
}