This repository has been archived by the owner on Apr 25, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 532
/
leaderelection.go
96 lines (79 loc) · 3.13 KB
/
leaderelection.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
/*
Copyright 2019 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 e2e
import (
"fmt"
"io"
"io/ioutil"
"os"
"os/exec"
"sigs.k8s.io/kubefed/test/common"
"sigs.k8s.io/kubefed/test/e2e/framework"
. "github.com/onsi/ginkgo" //nolint:stylecheck
)
var _ = Describe("Leader Elector", func() {
f := framework.NewKubeFedFramework("leaderelection")
tl := framework.NewE2ELogger()
It("should chose secondary instance, primary goes down", func() {
if framework.TestContext.LimitedScope {
framework.Skipf("Testing of leader election requires an isolated test namespace which is only possible with a cluster-scoped control plane")
}
const leaderIdentifier = "promoted as leader"
kubeConfigPath := framework.TestContext.KubeConfig
systemNamespace := f.TestNamespaceName()
primaryControllerManager, primaryLogStream, err := spawnControllerManagerProcess(tl, kubeConfigPath, systemNamespace)
framework.ExpectNoError(err)
if framework.WaitUntilLogStreamContains(tl, primaryLogStream, leaderIdentifier) {
tl.Log("Primary controller manager became leader")
} else {
_ = primaryControllerManager.Process.Kill()
tl.Fatal("Primary controller manager failed to become leader")
}
done := make(chan bool, 1)
secondaryControllerManager, secondaryLogStream, err := spawnControllerManagerProcess(tl, kubeConfigPath, systemNamespace)
framework.ExpectNoError(err)
go func() {
if framework.WaitUntilLogStreamContains(tl, secondaryLogStream, leaderIdentifier) {
tl.Log("Secondary controller manager became leader")
done <- true
} else {
_ = secondaryControllerManager.Process.Kill()
tl.Fatal("Secondary controller manager failed to become leader")
}
}()
err = primaryControllerManager.Process.Kill()
framework.ExpectNoError(err)
<-done
err = secondaryControllerManager.Process.Kill()
framework.ExpectNoError(err)
})
})
func spawnControllerManagerProcess(tl common.TestLogger, kubeConfigPath, namespace string) (*exec.Cmd, io.ReadCloser, error) {
kubeFedConfigAsset := common.MustAsset("config/kubefedconfig.yaml")
tmpFile, err := ioutil.TempFile("", "leaderelectionconfig.yaml")
if err != nil {
tl.Fatalf("Error creating the temp file for KubeFedConfig resource: %v", err)
}
_, err = tmpFile.Write(kubeFedConfigAsset)
if err != nil {
tl.Fatalf("Error writing the temp file content for KubeFedConfig resource: %v", err)
}
framework.AddCleanupAction(func() {
os.Remove(tmpFile.Name())
})
args := []string{fmt.Sprintf("--kubeconfig=%s", kubeConfigPath),
fmt.Sprintf("--kubefed-namespace=%s", namespace),
fmt.Sprintf("--kubefed-config=%s", tmpFile.Name()),
}
return framework.StartControllerManager(args)
}