-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add option to choose Lease lock if coordination group is available
- Choose the Lease lock if lease.coordination.k8s.io is available otherwise, use ConfigMaps. - Add tests coverage for implemented logic using ginko & gomega - Remove outdated information about cert generation
- Loading branch information
Showing
7 changed files
with
127 additions
and
12 deletions.
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 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
This file contains 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
This file contains 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,13 @@ | ||
package leaderelection | ||
|
||
import ( | ||
"testing" | ||
|
||
. "github.com/onsi/ginkgo" | ||
. "github.com/onsi/gomega" | ||
) | ||
|
||
func TestLeaderElection(t *testing.T) { | ||
RegisterFailHandler(Fail) | ||
RunSpecs(t, "Leader Election Suite") | ||
} |
This file contains 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,79 @@ | ||
package leaderelection | ||
|
||
import ( | ||
"bytes" | ||
"encoding/json" | ||
"fmt" | ||
"io/ioutil" | ||
"net/http" | ||
|
||
tlog "github.com/go-logr/logr/testing" | ||
. "github.com/onsi/ginkgo" | ||
. "github.com/onsi/gomega" | ||
coordinationv1 "k8s.io/api/coordination/v1" | ||
v1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
"k8s.io/client-go/kubernetes/scheme" | ||
restclient "k8s.io/client-go/rest" | ||
"k8s.io/client-go/tools/leaderelection/resourcelock" | ||
"k8s.io/client-go/tools/record" | ||
"sigs.k8s.io/controller-runtime/pkg/internal/recorder" | ||
) | ||
|
||
var _ = Describe("Leader Election", func() { | ||
It("should use the Lease lock because coordination group is available.", func() { | ||
coordinationGroup := &v1.APIGroupList{ | ||
Groups: []v1.APIGroup{ | ||
{Name: coordinationv1.GroupName}, | ||
}, | ||
} | ||
|
||
clientConfig := &restclient.Config{ | ||
Transport: interceptAPIGroupCall(coordinationGroup), | ||
} | ||
|
||
rProvider, err := recorder.NewProvider(clientConfig, scheme.Scheme, tlog.NullLogger{}, record.NewBroadcaster()) | ||
Expect(err).ToNot(HaveOccurred()) | ||
|
||
lock, err := NewResourceLock(clientConfig, rProvider, Options{LeaderElection: true, LeaderElectionNamespace: "test-ns"}) | ||
Expect(err).ToNot(HaveOccurred()) | ||
Expect(lock).To(BeAssignableToTypeOf(&resourcelock.LeaseLock{})) | ||
}) | ||
|
||
It("should use the ConfigMap lock because coordination group is unavailable.", func() { | ||
clientConfig := &restclient.Config{ | ||
Transport: interceptAPIGroupCall(&v1.APIGroupList{ /* no coordination group */ }), | ||
} | ||
|
||
rProvider, err := recorder.NewProvider(clientConfig, scheme.Scheme, tlog.NullLogger{}, record.NewBroadcaster()) | ||
Expect(err).ToNot(HaveOccurred()) | ||
|
||
lock, err := NewResourceLock(clientConfig, rProvider, Options{LeaderElection: true, LeaderElectionNamespace: "test-ns"}) | ||
Expect(err).ToNot(HaveOccurred()) | ||
Expect(lock).To(BeAssignableToTypeOf(&resourcelock.ConfigMapLock{})) | ||
}) | ||
}) | ||
|
||
func interceptAPIGroupCall(returnApis *v1.APIGroupList) roundTripper { | ||
return roundTripper(func(req *http.Request) (*http.Response, error) { | ||
if req.Method == "GET" && (req.URL.Path == "/apis" || req.URL.Path == "/api") { | ||
return encode(returnApis) | ||
} | ||
return nil, fmt.Errorf("unexpected request: %v %#v\n%#v", req.Method, req.URL, req) | ||
}) | ||
} | ||
func encode(bodyStruct interface{}) (*http.Response, error) { | ||
jsonBytes, err := json.Marshal(bodyStruct) | ||
if err != nil { | ||
return nil, err | ||
} | ||
return &http.Response{ | ||
StatusCode: http.StatusOK, | ||
Body: ioutil.NopCloser(bytes.NewReader(jsonBytes)), | ||
}, nil | ||
} | ||
|
||
type roundTripper func(*http.Request) (*http.Response, error) | ||
|
||
func (f roundTripper) RoundTrip(req *http.Request) (*http.Response, error) { | ||
return f(req) | ||
} |
This file contains 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
This file contains 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