-
Notifications
You must be signed in to change notification settings - Fork 4
/
main.go
99 lines (83 loc) · 2.94 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
package main
import (
"flag"
"os"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/client-go/discovery"
cacheddiscovery "k8s.io/client-go/discovery/cached"
"k8s.io/client-go/dynamic"
"k8s.io/client-go/kubernetes"
_ "k8s.io/client-go/plugin/pkg/client/auth" // Needed for misc auth.
_ "k8s.io/client-go/plugin/pkg/client/auth/gcp"
"k8s.io/client-go/tools/clientcmd"
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/log/zap"
workv1alpha1 "github.com/vllry/cluster-reconciler/pkg/api/v1alpha1"
"github.com/vllry/cluster-reconciler/pkg/controllers"
"github.com/vllry/cluster-reconciler/pkg/restmapper"
)
var (
scheme = runtime.NewScheme()
setupLog = ctrl.Log.WithName("setup")
)
func init() {
_ = workv1alpha1.AddToScheme(scheme)
}
func main() {
ctrl.SetLogger(zap.New(zap.UseDevMode(true)))
var metricsAddr string
flag.StringVar(&metricsAddr, "metrics-addr", ":8080", "The address the metric endpoint binds to.")
var spokeKubeconfig string
flag.StringVar(&spokeKubeconfig, "spoke-kubeconfig", "", "The kubeconfig to connect to spoke cluster to apply resources")
flag.Parse()
config, err := clientcmd.BuildConfigFromFlags("", spokeKubeconfig)
if err != nil {
setupLog.Error(err, "Unable to get spoke kube config.")
os.Exit(1)
}
client, err := kubernetes.NewForConfig(config)
if err != nil {
setupLog.Error(err, "Unable to create spoke kube client.")
os.Exit(1)
}
dynamicClient, err := dynamic.NewForConfig(config)
if err != nil {
setupLog.Error(err, "Unable to create spoke dynamic client.")
os.Exit(1)
}
discoveryClient, err := discovery.NewDiscoveryClientForConfig(config)
if err != nil {
setupLog.Error(err, "Unable to create spoke dynamic client.")
os.Exit(1)
}
startManager(metricsAddr, client, dynamicClient, discoveryClient)
}
func startManager(metricsAddr string, kubeClient kubernetes.Interface, dynamicClient dynamic.Interface, discoveryClient discovery.DiscoveryInterface) {
mgr, err := ctrl.NewManager(ctrl.GetConfigOrDie(), ctrl.Options{Scheme: scheme, MetricsBindAddress: metricsAddr})
if err != nil {
setupLog.Error(err, "unable to start manager")
os.Exit(1)
}
stopCh := ctrl.SetupSignalHandler()
cachedSpokeDiscoveryClient := cacheddiscovery.NewMemCacheClient(discoveryClient)
restMapper := restmapper.NewMapper(cachedSpokeDiscoveryClient)
go restMapper.Run(stopCh)
// Add controller into manager
workReconciler := &controllers.WorkReconciler{
Client: mgr.GetClient(),
Log: ctrl.Log.WithName("controllers").WithName("Work"),
Scheme: mgr.GetScheme(),
SpokeKubeClient: kubeClient,
SpokeDynamicClient: dynamicClient,
RestMapper: restMapper,
}
if err = workReconciler.SetupWithManager(mgr); err != nil {
setupLog.Error(err, "unable to create controller", "controller", "Work")
os.Exit(1)
}
setupLog.Info("starting manager")
if err := mgr.Start(stopCh); err != nil {
setupLog.Error(err, "problem running manager")
os.Exit(1)
}
}