-
Notifications
You must be signed in to change notification settings - Fork 271
Expand file tree
/
Copy pathoffline_cluster_test.go
More file actions
167 lines (141 loc) · 5.11 KB
/
offline_cluster_test.go
File metadata and controls
167 lines (141 loc) · 5.11 KB
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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
package multicluster_test
import (
"encoding/json"
"os/exec"
"time"
. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
corev1 "k8s.io/api/core/v1"
"github.com/rancher/fleet/e2e/testenv"
"github.com/rancher/fleet/e2e/testenv/kubectl"
"github.com/rancher/wrangler/v3/pkg/genericcondition"
)
// This test uses two clusters to demonstrate offline cluster handling.
var _ = Describe("Offline cluster detection", func() {
var (
k kubectl.Command
asset string
name string
)
BeforeEach(func() {
k = env.Kubectl.Context(env.Upstream)
})
Context("cluster with deployed workload becomes offline", func() {
BeforeEach(func() {
asset = "multi-cluster/helmop.yaml"
name = "test-offline-cluster"
err := testenv.ApplyTemplate(k.Namespace(env.ClusterRegistrationNamespace), testenv.AssetPath(asset), struct {
Name string
Namespace string
Repo string
Chart string
Version string
PollingInterval time.Duration
HelmSecretName string
InsecureSkipTLSVerify bool
}{
name,
env.ClusterRegistrationNamespace,
"",
"https://github.com/rancher/fleet/raw/refs/heads/main/integrationtests/cli/assets/helmrepository/config-chart-0.1.0.tgz",
"",
0,
"",
false,
})
Expect(err).ToNot(HaveOccurred())
DeferCleanup(func() {
connectCmd := exec.Command("docker", "network", "connect", "fleet", "k3d-"+k3dDownstreamCluster+"-server-0")
_, _ = connectCmd.CombinedOutput() // will fail if the node is already connected.
_, _ = k.Namespace(env.ClusterRegistrationNamespace).Delete("helmop", name)
})
})
It("marks any offline cluster as such, along with its bundle deployments", func() {
By("checking the initial online state of the cluster")
// Cluster should be ready
Eventually(func(g Gomega) {
out, err := k.Get(
"cluster", "-n", env.ClusterRegistrationNamespace,
"-o", `jsonpath={.items[0].status.conditions}`,
)
g.Expect(err).ToNot(HaveOccurred(), out)
checkReadyCondition(g, out, "", "True")
}).To(Succeed())
// Bundle deployment should be ready
Eventually(func(g Gomega) {
out, err := k.Get(
"bundledeployments", "-A",
"-l", "fleet.cattle.io/bundle-name="+name,
"-l", "fleet.cattle.io/bundle-namespace="+env.ClusterRegistrationNamespace,
"-o", `jsonpath={.items[0].status.conditions}`,
)
g.Expect(err).ToNot(HaveOccurred(), out)
checkReadyCondition(g, out, "", "True")
}).To(Succeed())
By("taking the cluster offline")
disconnectCmd := exec.Command("docker", "network", "disconnect", "fleet", "k3d-"+k3dDownstreamCluster+"-server-0")
out, err := disconnectCmd.CombinedOutput()
Expect(err).ToNot(HaveOccurred(), string(out))
By("checking that the bundle deployment and the cluster appear offline")
// Cluster should be offline
Eventually(func(g Gomega) {
out, err := k.Get(
"cluster", "-n", env.ClusterRegistrationNamespace,
"-o", `jsonpath={.items[0].status.conditions}`,
)
g.Expect(err).ToNot(HaveOccurred(), out)
checkReadyCondition(g, out, "cluster is offline", "Unknown")
}).To(Succeed())
// Bundle deployment should be offline
Eventually(func(g Gomega) {
out, err := k.Get(
"bundledeployments", "-A",
"-l", "fleet.cattle.io/bundle-name="+name,
"-l", "fleet.cattle.io/bundle-namespace="+env.ClusterRegistrationNamespace,
"-o", `jsonpath={.items[0].status.conditions}`,
)
g.Expect(err).ToNot(HaveOccurred(), out)
checkReadyCondition(g, out, "cluster is offline", "Unknown")
}).To(Succeed())
By("taking the cluster back online")
connectCmd := exec.Command("docker", "network", "connect", "fleet", "k3d-"+k3dDownstreamCluster+"-server-0")
out, err = connectCmd.CombinedOutput()
Expect(err).ToNot(HaveOccurred(), string(out))
By("checking the new online state of the cluster")
// Cluster should be ready again
Eventually(func(g Gomega) {
out, err := k.Get(
"cluster", "-n", env.ClusterRegistrationNamespace,
"-o", `jsonpath={.items[0].status.conditions}`,
)
g.Expect(err).ToNot(HaveOccurred(), out)
checkReadyCondition(g, out, "", "True")
}).To(Succeed())
// Bundle deployment should be ready again
Eventually(func(g Gomega) {
out, err := k.Get(
"bundledeployments", "-A",
"-l", "fleet.cattle.io/bundle-name="+name,
"-l", "fleet.cattle.io/bundle-namespace="+env.ClusterRegistrationNamespace,
"-o", `jsonpath={.items[0].status.conditions}`,
)
g.Expect(err).ToNot(HaveOccurred(), out)
checkReadyCondition(g, out, "", "True")
}).To(Succeed())
})
})
})
func checkReadyCondition(g Gomega, out, msg, status string) {
conds := []genericcondition.GenericCondition{}
err := json.Unmarshal([]byte(out), &conds)
g.Expect(err).ToNot(HaveOccurred())
var readyCond *genericcondition.GenericCondition
for i, c := range conds {
if c.Type == "Ready" {
readyCond = &conds[i]
}
}
g.Expect(readyCond).NotTo(BeNil())
g.Expect(readyCond.Message).To(ContainSubstring(msg))
g.Expect(readyCond.Status).To(Equal(corev1.ConditionStatus(status)))
}