-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Add leader election to controller manager #118
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
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
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.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
/* | ||
Copyright 2018 The Kubernetes 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 leaderelection contains a constructors for a leader election resource lock | ||
*/ | ||
package leaderelection |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
/* | ||
Copyright 2018 The Kubernetes 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 fake mocks a resource lock for testing purposes. | ||
Always returns leadership. | ||
*/ | ||
package fake |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
/* | ||
Copyright 2018 The Kubernetes 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 fake | ||
|
||
import ( | ||
"os" | ||
"time" | ||
|
||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
"k8s.io/apimachinery/pkg/util/uuid" | ||
"k8s.io/client-go/rest" | ||
"k8s.io/client-go/tools/leaderelection/resourcelock" | ||
"sigs.k8s.io/controller-runtime/pkg/leaderelection" | ||
"sigs.k8s.io/controller-runtime/pkg/recorder" | ||
) | ||
|
||
// NewResourceLock creates a new ResourceLock for use in testing | ||
// leader election. | ||
func NewResourceLock(config *rest.Config, recorderProvider recorder.Provider, options leaderelection.Options) (resourcelock.Interface, error) { | ||
// Leader id, needs to be unique | ||
id, err := os.Hostname() | ||
if err != nil { | ||
return nil, err | ||
} | ||
id = id + "_" + string(uuid.NewUUID()) | ||
|
||
return &ResourceLock{ | ||
id: id, | ||
record: resourcelock.LeaderElectionRecord{ | ||
HolderIdentity: id, | ||
LeaseDurationSeconds: 15, | ||
AcquireTime: metav1.NewTime(time.Now()), | ||
RenewTime: metav1.NewTime(time.Now().Add(15 * time.Second)), | ||
LeaderTransitions: 1, | ||
}, | ||
}, nil | ||
} | ||
|
||
// ResourceLock implements the ResourceLockInterface. | ||
// By default returns that the current identity holds the lock. | ||
type ResourceLock struct { | ||
id string | ||
record resourcelock.LeaderElectionRecord | ||
} | ||
|
||
// Get implements the ResourceLockInterface. | ||
func (f *ResourceLock) Get() (*resourcelock.LeaderElectionRecord, error) { | ||
return &f.record, nil | ||
} | ||
|
||
// Create implements the ResourceLockInterface. | ||
func (f *ResourceLock) Create(ler resourcelock.LeaderElectionRecord) error { | ||
f.record = ler | ||
return nil | ||
} | ||
|
||
// Update implements the ResourceLockInterface. | ||
func (f *ResourceLock) Update(ler resourcelock.LeaderElectionRecord) error { | ||
f.record = ler | ||
return nil | ||
} | ||
|
||
// RecordEvent implements the ResourceLockInterface. | ||
func (f *ResourceLock) RecordEvent(s string) { | ||
return | ||
} | ||
|
||
// Identity implements the ResourceLockInterface. | ||
func (f *ResourceLock) Identity() string { | ||
return f.id | ||
} | ||
|
||
// Describe implements the ResourceLockInterface. | ||
func (f *ResourceLock) Describe() string { | ||
return f.id | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,109 @@ | ||
/* | ||
Copyright 2018 The Kubernetes 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 leaderelection | ||
|
||
import ( | ||
"fmt" | ||
"io/ioutil" | ||
"os" | ||
|
||
"k8s.io/apimachinery/pkg/util/uuid" | ||
"k8s.io/client-go/kubernetes" | ||
"k8s.io/client-go/rest" | ||
"k8s.io/client-go/tools/leaderelection/resourcelock" | ||
"sigs.k8s.io/controller-runtime/pkg/recorder" | ||
) | ||
|
||
const inClusterNamespacePath = "/var/run/secrets/kubernetes.io/serviceaccount/namespace" | ||
|
||
// Options provides the required configuration to create a new resource lock | ||
type Options struct { | ||
// LeaderElection determines whether or not to use leader election when | ||
// starting the manager. | ||
LeaderElection bool | ||
|
||
// LeaderElectionNamespace determines the namespace in which the leader | ||
// election configmap will be created. | ||
LeaderElectionNamespace string | ||
|
||
// LeaderElectionID determines the name of the configmap that leader election | ||
// will use for holding the leader lock. | ||
LeaderElectionID string | ||
} | ||
|
||
// NewResourceLock creates a new config map resource lock for use in a leader | ||
// election loop | ||
func NewResourceLock(config *rest.Config, recorderProvider recorder.Provider, options Options) (resourcelock.Interface, error) { | ||
if !options.LeaderElection { | ||
return nil, nil | ||
} | ||
|
||
// Default the LeaderElectionID | ||
if options.LeaderElectionID == "" { | ||
options.LeaderElectionID = "controller-leader-election-helper" | ||
} | ||
|
||
// Default the namespace (if running in cluster) | ||
if options.LeaderElectionNamespace == "" { | ||
var err error | ||
options.LeaderElectionNamespace, err = getInClusterNamespace() | ||
if err != nil { | ||
return nil, fmt.Errorf("unable to find leader election namespace: %v", err) | ||
} | ||
} | ||
|
||
// Leader id, needs to be unique | ||
id, err := os.Hostname() | ||
if err != nil { | ||
return nil, err | ||
} | ||
id = id + "_" + string(uuid.NewUUID()) | ||
|
||
// Construct client for leader election | ||
client, err := kubernetes.NewForConfig(config) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
// TODO(JoelSpeed): switch to leaderelection object in 1.12 | ||
return resourcelock.New(resourcelock.ConfigMapsResourceLock, | ||
options.LeaderElectionNamespace, | ||
options.LeaderElectionID, | ||
client.CoreV1(), | ||
resourcelock.ResourceLockConfig{ | ||
Identity: id, | ||
EventRecorder: recorderProvider.GetEventRecorderFor(id), | ||
}) | ||
} | ||
|
||
func getInClusterNamespace() (string, error) { | ||
// Check whether the namespace file exists. | ||
// If not, we are not running in cluster so can't guess the namespace. | ||
_, err := os.Stat(inClusterNamespacePath) | ||
if os.IsNotExist(err) { | ||
return "", fmt.Errorf("not running in-cluster, please specify LeaderElectionNamespace") | ||
} else if err != nil { | ||
return "", fmt.Errorf("error checking namespace file: %v", err) | ||
} | ||
|
||
// Load the namespace file and return itss content | ||
namespace, err := ioutil.ReadFile(inClusterNamespacePath) | ||
if err != nil { | ||
return "", fmt.Errorf("error reading namespace file: %v", err) | ||
} | ||
return string(namespace), nil | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
I'm curious to know more about this but am having a hard time finding other references to it. Do you have any links to info about this object?
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.
I asked about it in #sig-api-machinery, and got the info from there: https://kubernetes.slack.com/archives/C0EG7JC6T/p1535318981000100
I know nothing more about it, sorry
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.
I got this reference from that slack thread: https://github.com/kubernetes/kubernetes/blob/master/staging/src/k8s.io/api/coordination/v1beta1/types.go#L27