-
Notifications
You must be signed in to change notification settings - Fork 356
Use leader election library from csi-lib-utils #261
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -17,6 +17,7 @@ limitations under the License. | |
package main | ||
|
||
import ( | ||
"context" | ||
goflag "flag" | ||
"fmt" | ||
"math/rand" | ||
|
@@ -28,6 +29,7 @@ import ( | |
flag "github.com/spf13/pflag" | ||
|
||
"github.com/kubernetes-csi/csi-lib-utils/deprecatedflags" | ||
"github.com/kubernetes-csi/csi-lib-utils/leaderelection" | ||
ctrl "github.com/kubernetes-csi/external-provisioner/pkg/controller" | ||
snapclientset "github.com/kubernetes-csi/external-snapshotter/pkg/client/clientset/versioned" | ||
"sigs.k8s.io/sig-storage-lib-external-provisioner/controller" | ||
|
@@ -52,19 +54,25 @@ var ( | |
volumeNamePrefix = flag.String("volume-name-prefix", "pvc", "Prefix to apply to the name of a created volume.") | ||
volumeNameUUIDLength = flag.Int("volume-name-uuid-length", -1, "Truncates generated UUID of a created volume to this length. Defaults behavior is to NOT truncate.") | ||
showVersion = flag.Bool("version", false, "Show version.") | ||
enableLeaderElection = flag.Bool("enable-leader-election", false, "Enables leader election. If leader election is enabled, additional RBAC rules are required. Please refer to the Kubernetes CSI documentation for instructions on setting up these RBAC rules.") | ||
retryIntervalStart = flag.Duration("retry-interval-start", time.Second, "Initial retry interval of failed provisioning or deletion. It doubles with each failure, up to retry-interval-max.") | ||
retryIntervalMax = flag.Duration("retry-interval-max", 5*time.Minute, "Maximum retry interval of failed provisioning or deletion.") | ||
workerThreads = flag.Uint("worker-threads", 100, "Number of provisioner worker threads, in other words nr. of simultaneous CSI calls.") | ||
operationTimeout = flag.Duration("timeout", 10*time.Second, "Timeout for waiting for creation or deletion of a volume") | ||
_ = deprecatedflags.Add("provisioner") | ||
|
||
enableLeaderElection = flag.Bool("enable-leader-election", false, "Enables leader election. If leader election is enabled, additional RBAC rules are required. Please refer to the Kubernetes CSI documentation for instructions on setting up these RBAC rules.") | ||
leaderElectionType = flag.String("leader-election-type", "endpoints", "the type of leader election, options are 'endpoints' (default) or 'leases' (strongly recommended). The 'endpoints' option is deprecated in favor of 'leases'.") | ||
|
||
featureGates map[string]bool | ||
provisionController *controller.ProvisionController | ||
version = "unknown" | ||
) | ||
|
||
func init() { | ||
type leaderElection interface { | ||
Run() error | ||
} | ||
|
||
func main() { | ||
var config *rest.Config | ||
var err error | ||
|
||
|
@@ -178,8 +186,32 @@ func init() { | |
serverVersion.GitVersion, | ||
provisionerOptions..., | ||
) | ||
} | ||
|
||
func main() { | ||
provisionController.Run(wait.NeverStop) | ||
run := func(context.Context) { | ||
provisionController.Run(wait.NeverStop) | ||
} | ||
|
||
if !*enableLeaderElection { | ||
run(context.TODO()) | ||
} else { | ||
// this lock name pattern is also copied from sigs.k8s.io/sig-storage-lib-external-provisioner/controller | ||
// to preserve backwards compatibility | ||
lockName := strings.Replace(provisionerName, "/", "-", -1) | ||
|
||
var le leaderElection | ||
if *leaderElectionType == "endpoints" { | ||
klog.Warning("The 'endpoints' leader election type is deprecated and will be removed in a future release. Use '--leader-election-type=leases' instead.") | ||
le = leaderelection.NewLeaderElectionWithEndpoints(clientset, lockName, run) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. And deprecation log message here |
||
} else if *leaderElectionType == "leases" { | ||
le = leaderelection.NewLeaderElection(clientset, lockName, run) | ||
} else { | ||
klog.Error("--leader-election-type must be either 'endpoints' or 'lease'") | ||
os.Exit(1) | ||
} | ||
|
||
if err := le.Run(); err != nil { | ||
klog.Fatalf("failed to initialize leader election: %v", err) | ||
} | ||
} | ||
|
||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -76,9 +76,14 @@ metadata: | |
namespace: default | ||
name: external-provisioner-cfg | ||
rules: | ||
# Only one of the following rules for endpoints or leases is required based on | ||
# what is set for `--leader-election-type`. Endpoints are deprecated in favor of Leases. | ||
- apiGroups: [""] | ||
resources: ["endpoints"] | ||
verbs: ["get", "watch", "list", "delete", "update", "create"] | ||
- apiGroups: ["coordination.k8s.io"] | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. And a comment that only one of endpoints or lease rules is needed |
||
resources: ["leases"] | ||
verbs: ["get", "watch", "list", "delete", "update", "create"] | ||
|
||
--- | ||
kind: RoleBinding | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Add deprecation notice here
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Did you mean for
leaderElectionType
?