-
Notifications
You must be signed in to change notification settings - Fork 61
/
Copy pathcontroller.go
111 lines (92 loc) · 3.27 KB
/
controller.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
/*
Copyright 2020 The OpenEBS Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package provisioner
import (
"context"
"os"
"strings"
"github.com/pkg/errors"
"k8s.io/klog/v2"
mKube "github.com/openebs/dynamic-nfs-provisioner/pkg/kubernetes/client"
menv "github.com/openebs/maya/pkg/env/v1alpha1"
analytics "github.com/openebs/maya/pkg/usage"
pvController "sigs.k8s.io/sig-storage-lib-external-provisioner/v7/controller"
)
var (
provisionerName = "openebs.io/nfsrwx"
// LeaderElectionKey represents ENV for disable/enable leaderElection for
// nfs provisioner
LeaderElectionKey = "LEADER_ELECTION_ENABLED"
)
// Start will initialize and run the dynamic provisioner daemon
func Start(ctx context.Context) error {
klog.Infof("Starting Provisioner...")
// Dynamic Provisioner can run successfully if it can establish
// connection to the Kubernetes Cluster. mKube helps with
// establishing the connection either via InCluster or
// OutOfCluster by using the following ENV variables:
// OPENEBS_IO_K8S_MASTER - Kubernetes master IP address
// OPENEBS_IO_KUBE_CONFIG - Path to the kubeConfig file.
kubeClient, err := mKube.New().Clientset()
if err != nil {
return errors.Wrap(err, "unable to get k8s client")
}
err = performPreupgradeTasks(kubeClient)
if err != nil {
return errors.Wrap(err, "failure in preupgrade tasks")
}
//Create an instance of ProvisionerHandler to handle PV
// create and delete events.
provisioner, err := NewProvisioner(ctx, kubeClient)
if err != nil {
return err
}
//Create an instance of the Dynamic Provisioner Controller
// that has the reconciliation loops for PVC create and delete
// events and invokes the Provisioner Handler.
pc := pvController.NewProvisionController(
kubeClient,
provisionerName,
provisioner,
pvController.LeaderElection(isLeaderElectionEnabled()),
)
klog.V(4).Info("Provisioner started")
//Run the provisioner till a shutdown signal is received.
go pc.Run(ctx)
if menv.Truthy(menv.OpenEBSEnableAnalytics) {
analytics.New().Build().InstallBuilder(true).Send()
go analytics.PingCheck()
}
<-ctx.Done()
klog.V(4).Info("Provisioner stopped")
return nil
}
// isLeaderElectionEnabled returns true/false based on the ENV
// LEADER_ELECTION_ENABLED set via provisioner deployment.
// Defaults to true, means leaderElection enabled by default.
func isLeaderElectionEnabled() bool {
leaderElection := os.Getenv(LeaderElectionKey)
var leader bool
switch strings.ToLower(leaderElection) {
default:
klog.Info("Leader election enabled for nfs-provisioner")
leader = true
case "y", "yes", "true":
klog.Info("Leader election enabled for nfs-provisioner via leaderElectionKey")
leader = true
case "n", "no", "false":
klog.Info("Leader election disabled for nfs-provisioner via leaderElectionKey")
leader = false
}
return leader
}