/
main.go
121 lines (111 loc) · 4.03 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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
package main
import (
"flag"
"os"
"time"
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"
configv1alpha1 "github.com/liqotech/liqo/apis/config/v1alpha1"
discoveryv1alpha1 "github.com/liqotech/liqo/apis/discovery/v1alpha1"
crdreplicator "github.com/liqotech/liqo/internal/crdReplicator"
"github.com/liqotech/liqo/pkg/clusterid"
identitymanager "github.com/liqotech/liqo/pkg/identityManager"
"github.com/liqotech/liqo/pkg/liqonet/utils"
"github.com/liqotech/liqo/pkg/mapperUtils"
tenantcontrolnamespace "github.com/liqotech/liqo/pkg/tenantControlNamespace"
)
var (
scheme = runtime.NewScheme()
clusterIDConfMap = "cluster-id"
)
func init() {
_ = clientgoscheme.AddToScheme(scheme)
_ = discoveryv1alpha1.AddToScheme(scheme)
// +kubebuilder:scaffold:scheme
}
func main() {
var useNewAuth bool
flag.BoolVar(&useNewAuth, "useNewAuth", false, "Enable the new authentication flow, with certificates and namespaced resources")
klog.InitFlags(nil)
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 := utils.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)
}
clusterIDInterface := clusterid.NewStaticClusterID(clusterID)
namespaceManager := tenantcontrolnamespace.NewTenantControlNamespaceManager(k8sClient)
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),
UseNewAuth: useNewAuth,
NamespaceManager: namespaceManager,
IdentityManager: identitymanager.NewCertificateIdentityManager(
k8sClient, clusterIDInterface, namespaceManager),
LocalToRemoteNamespaceMapper: map[string]string{},
RemoteToLocalNamespaceMapper: map[string]string{},
}
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)
}
}