Skip to content

Commit 281a674

Browse files
committed
Mostly cleanup + clarifications
1 parent 17bde37 commit 281a674

File tree

4 files changed

+84
-30
lines changed

4 files changed

+84
-30
lines changed

controllers/tests/controller/selfnoderemediation_controller_test.go

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1269,10 +1269,15 @@ func configureUnhealthyNodeAsControlNode() {
12691269
func configureSimulatedPeerResponses(simulateResponses bool) {
12701270
By("Start simulating peer responses", func() {
12711271
orgValue := apiCheck.ShouldSimulatePeerResponses
1272+
orgResponses := apiCheck.SnapshotSimulatedPeerResponses()
12721273
apiCheck.ShouldSimulatePeerResponses = simulateResponses
1274+
if simulateResponses {
1275+
apiCheck.ClearSimulatedPeerResponses()
1276+
}
12731277

12741278
DeferCleanup(func() {
12751279
apiCheck.ShouldSimulatePeerResponses = orgValue
1280+
apiCheck.RestoreSimulatedPeerResponses(orgResponses)
12761281
})
12771282
})
12781283
}
@@ -1357,21 +1362,13 @@ func addNodes(nodes []newNodeConfig) {
13571362
for _, pod := range np.pods {
13581363
By(fmt.Sprintf("Create pod '%s' under node '%s'", pod.name, np.nodeName), func() {
13591364
_ = createGenericSelfNodeRemediationPod(createdNode, pod.name)
1360-
apiCheck.SimulatePeerResponses = append(apiCheck.SimulatePeerResponses, pod.simulatedResponse)
1365+
apiCheck.AppendSimulatedPeerResponse(pod.simulatedResponse)
13611366
})
13621367

13631368
}
13641369

13651370
}
13661371

1367-
DeferCleanup(func() {
1368-
By("Removing the additional peer nodes when all relevant tests are complete", func() {
1369-
Expect(k8sClient.Delete(context.Background(), getNode(shared.Peer2NodeName))).To(Succeed())
1370-
Expect(k8sClient.Delete(context.Background(), getNode(shared.Peer3NodeName))).To(Succeed())
1371-
})
1372-
return
1373-
})
1374-
13751372
})
13761373
}
13771374

controllers/tests/controller/suite_test.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,9 @@ var _ = BeforeSuite(func() {
168168

169169
apiCheck = shared.NewApiConnectivityCheckWrapper(apiConnectivityCheckConfig, nil)
170170

171+
// default to simulation so the ApiConnectivityCheck doesn't try to dial peers before tests install handlers
172+
apiCheck.ShouldSimulatePeerResponses = true
173+
171174
err = k8sManager.Add(apiCheck)
172175
Expect(err).ToNot(HaveOccurred())
173176

controllers/tests/shared/shared.go

Lines changed: 62 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"context"
55
"errors"
66
"net"
7+
"sync"
78
"time"
89

910
"github.com/google/uuid"
@@ -57,11 +58,11 @@ type K8sClientWrapper struct {
5758
}
5859

5960
type ApiConnectivityCheckWrapper struct {
60-
apicheck.ApiConnectivityCheck
61+
*apicheck.ApiConnectivityCheck
6162
ShouldSimulatePeerResponses bool
6263

63-
// store responses that we should override for any peer responses
64-
SimulatePeerResponses []selfNodeRemediation.HealthCheckResponseCode
64+
responsesMu sync.Mutex
65+
simulatedPeerResponses []selfNodeRemediation.HealthCheckResponseCode
6566
}
6667

6768
func (kcw *K8sClientWrapper) List(ctx context.Context, list client.ObjectList, opts ...client.ListOption) (err error) {
@@ -107,29 +108,75 @@ func GetRandomIpAddress() (randomIP string) {
107108
}
108109

109110
func NewApiConnectivityCheckWrapper(ck *apicheck.ApiConnectivityCheckConfig, controlPlaneManager *controlplane.Manager) (ckw *ApiConnectivityCheckWrapper) {
111+
inner := apicheck.New(ck, controlPlaneManager)
110112
ckw = &ApiConnectivityCheckWrapper{
111-
ApiConnectivityCheck: *apicheck.New(ck, controlPlaneManager),
113+
ApiConnectivityCheck: inner,
112114
ShouldSimulatePeerResponses: false,
113-
SimulatePeerResponses: []selfNodeRemediation.HealthCheckResponseCode{},
115+
simulatedPeerResponses: []selfNodeRemediation.HealthCheckResponseCode{},
114116
}
115117

116-
ckw.ApiConnectivityCheck.SetHealthStatusFunc(func(endpointIp corev1.PodIP, results chan<- selfNodeRemediation.HealthCheckResponseCode) {
117-
switch {
118-
case ckw.ShouldSimulatePeerResponses:
119-
for _, code := range ckw.SimulatePeerResponses {
120-
results <- code
121-
}
122-
118+
inner.SetHealthStatusFunc(func(endpointIp corev1.PodIP, results chan<- selfNodeRemediation.HealthCheckResponseCode) {
119+
if ckw.ShouldSimulatePeerResponses {
120+
results <- ckw.nextSimulatedPeerResponse()
123121
return
124-
default:
125-
ckw.ApiConnectivityCheck.GetDefaultPeerHealthCheckFunc()(endpointIp, results)
126-
break
127122
}
123+
124+
ckw.GetDefaultPeerHealthCheckFunc()(endpointIp, results)
128125
})
129126

130127
return
131128
}
132129

130+
func (ckw *ApiConnectivityCheckWrapper) nextSimulatedPeerResponse() selfNodeRemediation.HealthCheckResponseCode {
131+
ckw.responsesMu.Lock()
132+
defer ckw.responsesMu.Unlock()
133+
134+
if len(ckw.simulatedPeerResponses) == 0 {
135+
return selfNodeRemediation.RequestFailed
136+
}
137+
138+
code := ckw.simulatedPeerResponses[0]
139+
if len(ckw.simulatedPeerResponses) > 1 {
140+
ckw.simulatedPeerResponses = append([]selfNodeRemediation.HealthCheckResponseCode{}, ckw.simulatedPeerResponses[1:]...)
141+
}
142+
143+
return code
144+
}
145+
146+
func (ckw *ApiConnectivityCheckWrapper) AppendSimulatedPeerResponse(code selfNodeRemediation.HealthCheckResponseCode) {
147+
ckw.responsesMu.Lock()
148+
defer ckw.responsesMu.Unlock()
149+
ckw.simulatedPeerResponses = append(ckw.simulatedPeerResponses, code)
150+
}
151+
152+
func (ckw *ApiConnectivityCheckWrapper) ClearSimulatedPeerResponses() {
153+
ckw.responsesMu.Lock()
154+
defer ckw.responsesMu.Unlock()
155+
ckw.simulatedPeerResponses = nil
156+
}
157+
158+
func (ckw *ApiConnectivityCheckWrapper) SnapshotSimulatedPeerResponses() []selfNodeRemediation.HealthCheckResponseCode {
159+
ckw.responsesMu.Lock()
160+
defer ckw.responsesMu.Unlock()
161+
if len(ckw.simulatedPeerResponses) == 0 {
162+
return nil
163+
}
164+
snapshot := make([]selfNodeRemediation.HealthCheckResponseCode, len(ckw.simulatedPeerResponses))
165+
copy(snapshot, ckw.simulatedPeerResponses)
166+
return snapshot
167+
}
168+
169+
func (ckw *ApiConnectivityCheckWrapper) RestoreSimulatedPeerResponses(codes []selfNodeRemediation.HealthCheckResponseCode) {
170+
ckw.responsesMu.Lock()
171+
defer ckw.responsesMu.Unlock()
172+
if len(codes) == 0 {
173+
ckw.simulatedPeerResponses = nil
174+
return
175+
}
176+
ckw.simulatedPeerResponses = make([]selfNodeRemediation.HealthCheckResponseCode, len(codes))
177+
copy(ckw.simulatedPeerResponses, codes)
178+
}
179+
133180
func GenerateTestConfig() *selfnoderemediationv1alpha1.SelfNodeRemediationConfig {
134181
return &selfnoderemediationv1alpha1.SelfNodeRemediationConfig{
135182
TypeMeta: metav1.TypeMeta{

pkg/apicheck/check.go

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -230,15 +230,22 @@ func (c *ApiConnectivityCheck) getPeersResponse(role peers.Role) peers.Response
230230
c.config.Log.Info("Ignoring api-server error, error count below threshold", "current count", c.errorCount, "threshold", c.config.MaxErrorsThreshold)
231231
return peers.Response{IsHealthy: true, Reason: peers.HealthyBecauseErrorsThresholdNotReached}
232232
}
233-
c.config.Log.Info("Error count was above threshold, we will continue and attempt to get the addressess" +
234-
" for our peers, I consider myself a WORKER at the moment")
233+
roleName := "worker"
234+
if role == peers.ControlPlane {
235+
roleName = "control-plane"
236+
}
237+
238+
c.config.Log.Info("Error count was above threshold, attempting to collect peer addresses",
239+
"role", roleName)
235240

236-
// MES: This gets called even if the current node is a control plane node. Hopefully
237-
// in an actual environment it is returning actual worker peers
241+
// MES: This gets called even if the current node is a control plane node. Hopefully
242+
// in an actual environment it is returning actual worker peers when the role is worker.
238243
peersToAsk := c.config.Peers.GetPeersAddresses(role)
239244

240-
c.config.Log.Info("Error count exceeds threshold, trying to ask other peer nodes if I'm healthy",
241-
"minPeersRequired", c.config.MinPeersForRemediation, "actualNumPeersFound", len(peersToAsk))
245+
c.config.Log.Info("Error count exceeds threshold, querying peer nodes for health",
246+
"role", roleName,
247+
"minPeersRequired", c.config.MinPeersForRemediation,
248+
"actualNumPeersFound", len(peersToAsk))
242249

243250
// We check to see if we have at least the number of peers that the user has configured as required.
244251
// If we don't have this many peers (for instance there are zero peers, and the default value is set

0 commit comments

Comments
 (0)