-
Notifications
You must be signed in to change notification settings - Fork 103
/
main.go
102 lines (96 loc) · 3.2 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
package main
import (
"flag"
"os"
"time"
configv1alpha1 "github.com/liqotech/liqo/apis/config/v1alpha1"
discoveryv1alpha1 "github.com/liqotech/liqo/apis/discovery/v1alpha1"
"github.com/liqotech/liqo/internal/crdReplicator"
util "github.com/liqotech/liqo/pkg/liqonet"
"github.com/liqotech/liqo/pkg/mapperUtils"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/util/wait"
"k8s.io/client-go/dynamic"
"k8s.io/client-go/dynamic/dynamicinformer"
"k8s.io/client-go/kubernetes"
clientgoscheme "k8s.io/client-go/kubernetes/scheme"
"k8s.io/klog/v2"
ctrl "sigs.k8s.io/controller-runtime"
)
var (
scheme = runtime.NewScheme()
clusterIDConfMap = "cluster-id"
)
func init() {
_ = clientgoscheme.AddToScheme(scheme)
_ = discoveryv1alpha1.AddToScheme(scheme)
// +kubebuilder:scaffold:scheme
}
func main() {
flag.Parse()
cfg := ctrl.GetConfigOrDie()
mgr, err := ctrl.NewManager(cfg, ctrl.Options{
MapperProvider: mapperUtils.LiqoMapperProvider(scheme),
Scheme: scheme,
Port: 9443,
LeaderElection: false,
})
if err != nil {
klog.Error(err, "unable to start manager")
os.Exit(-1)
}
//create a clientSet
k8sClient := kubernetes.NewForConfigOrDie(cfg)
//get namespace where the operator is running
namespaceName, found := os.LookupEnv("NAMESPACE")
if !found {
klog.Errorf("namespace env variable not set, please set it in manifest file of the operator")
os.Exit(-1)
}
// 7 attempts with 30 seconds sleep between one another
// for a total of 3 minutes
backoff := wait.Backoff{
Steps: 7,
Duration: 30 * time.Second,
Factor: 1.0,
Jitter: 0,
}
clusterID, err := util.GetClusterID(k8sClient, clusterIDConfMap, namespaceName, backoff)
if err != nil {
klog.Errorf("an error occurred while retrieving the clusterID: %s", err)
os.Exit(-1)
} else {
klog.Infof("setting local clusterID to: %s", clusterID)
}
dynClient := dynamic.NewForConfigOrDie(cfg)
dynFac := dynamicinformer.NewFilteredDynamicSharedInformerFactory(dynClient, crdReplicator.ResyncPeriod, metav1.NamespaceAll, crdReplicator.SetLabelsForLocalResources)
d := &crdReplicator.Controller{
Scheme: mgr.GetScheme(),
Client: mgr.GetClient(),
ClientSet: k8sClient,
ClusterID: clusterID,
RemoteDynClients: make(map[string]dynamic.Interface),
LocalDynClient: dynClient,
LocalDynSharedInformerFactory: dynFac,
RegisteredResources: nil,
UnregisteredResources: nil,
LocalWatchers: make(map[string]chan struct{}),
RemoteWatchers: make(map[string]map[string]chan struct{}),
RemoteDynSharedInformerFactory: make(map[string]dynamicinformer.DynamicSharedInformerFactory),
}
if err = d.SetupWithManager(mgr); err != nil {
klog.Error(err, "unable to setup the crdreplicator-operator")
os.Exit(1)
}
err = d.WatchConfiguration(cfg, &configv1alpha1.GroupVersion)
if err != nil {
klog.Error(err)
os.Exit(-1)
}
klog.Info("Starting crdreplicator-operator")
if err := mgr.Start(ctrl.SetupSignalHandler()); err != nil {
klog.Error(err, "problem running manager")
os.Exit(1)
}
}