Skip to content

Commit d6d028a

Browse files
committed
Apply Feedback
- clarify messages, remove unused method - additional unit tests Signed-off-by: Michael Shitrit <[email protected]>
1 parent cd95e49 commit d6d028a

File tree

4 files changed

+92
-17
lines changed

4 files changed

+92
-17
lines changed

api/v1alpha1/selfnoderemediationconfig_webhook.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -212,15 +212,15 @@ func (r *SelfNodeRemediationConfig) validatePeerTimeoutSafety() admission.Warnin
212212
"PeerRequestTimeout (%s) is less than ApiServerTimeout + MinimumBuffer (%s + %s = %s). "+
213213
"This configuration may lead to race conditions where peer health checks time out "+
214214
"before API server checks complete, potentially causing premature remediation. "+
215-
"Consider increasing PeerRequestTimeout to at least %s for safer operation.",
215+
"Overriding PeerRequestTimeout to %s for safer operation.",
216216
peerRequestTimeoutDuration,
217217
apiServerTimeoutDuration,
218218
MinimumBuffer,
219219
minimumSafePeerTimeout,
220220
minimumSafePeerTimeout,
221221
)
222222
warnings = append(warnings, warningMsg)
223-
selfNodeRemediationConfigLog.Info("PeerRequestTimeout safety warning",
223+
selfNodeRemediationConfigLog.Info("PeerRequestTimeout safety warning, overriding PeerRequestTimeout to minimumSafeTimeout",
224224
"peerRequestTimeout", peerRequestTimeoutDuration,
225225
"apiServerTimeout", apiServerTimeoutDuration,
226226
"minimumSafeTimeout", minimumSafePeerTimeout)

pkg/apicheck/check_test.go

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
package apicheck
2+
3+
import (
4+
"testing"
5+
"time"
6+
7+
"github.com/go-logr/logr"
8+
. "github.com/onsi/ginkgo/v2"
9+
. "github.com/onsi/gomega"
10+
11+
"k8s.io/client-go/tools/record"
12+
ctrl "sigs.k8s.io/controller-runtime"
13+
14+
"github.com/medik8s/self-node-remediation/api/v1alpha1"
15+
)
16+
17+
func TestApiCheck(t *testing.T) {
18+
RegisterFailHandler(Fail)
19+
RunSpecs(t, "ApiCheck Suite")
20+
}
21+
22+
var _ = Describe("ApiConnectivityCheck", func() {
23+
var (
24+
apiCheck *ApiConnectivityCheck
25+
config *ApiConnectivityCheckConfig
26+
fakeRecorder *record.FakeRecorder
27+
log logr.Logger
28+
)
29+
30+
BeforeEach(func() {
31+
log = ctrl.Log.WithName("test")
32+
fakeRecorder = record.NewFakeRecorder(10)
33+
34+
config = &ApiConnectivityCheckConfig{
35+
Log: log,
36+
MyNodeName: "test-node",
37+
ApiServerTimeout: 5 * time.Second,
38+
PeerRequestTimeout: 7 * time.Second,
39+
Recorder: fakeRecorder,
40+
}
41+
42+
apiCheck = &ApiConnectivityCheck{
43+
config: config,
44+
}
45+
})
46+
47+
Describe("getEffectivePeerRequestTimeout", func() {
48+
Context("when PeerRequestTimeout is safe", func() {
49+
It("should return the configured PeerRequestTimeout", func() {
50+
// ApiServerTimeout=5s, PeerRequestTimeout=7s, MinimumBuffer=2s
51+
// 7s >= (5s + 2s), so it's safe
52+
effectiveTimeout := apiCheck.getEffectivePeerRequestTimeout()
53+
54+
Expect(effectiveTimeout).To(Equal(7 * time.Second))
55+
56+
// Should not emit any events
57+
Expect(len(fakeRecorder.Events)).To(Equal(0))
58+
})
59+
})
60+
61+
Context("when PeerRequestTimeout is unsafe", func() {
62+
It("should return adjusted timeout and emit warning event", func() {
63+
config.PeerRequestTimeout = 6 * time.Second // Less than 5s + 2s = 7s
64+
65+
effectiveTimeout := apiCheck.getEffectivePeerRequestTimeout()
66+
67+
expectedMinimumTimeout := config.ApiServerTimeout + v1alpha1.MinimumBuffer // 7s
68+
Expect(effectiveTimeout).To(Equal(expectedMinimumTimeout))
69+
70+
// Should emit a warning event
71+
Expect(len(fakeRecorder.Events)).To(Equal(1))
72+
event := <-fakeRecorder.Events
73+
Expect(event).To(ContainSubstring("Warning"))
74+
Expect(event).To(ContainSubstring("PeerTimeoutAdjusted"))
75+
Expect(event).To(ContainSubstring("6s")) // configured timeout
76+
Expect(event).To(ContainSubstring("5s")) // API server timeout
77+
Expect(event).To(ContainSubstring("7s")) // safe timeout
78+
})
79+
80+
})
81+
82+
})
83+
})

pkg/peerhealth/client_server_test.go

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,9 @@ var _ = Describe("Checking health using grpc client and server", func() {
2323
var phServer *Server
2424
var cancel context.CancelFunc
2525
var phClient *Client
26+
var apiServerTimeout = 5 * time.Second
2627

27-
BeforeEach(func() {
28+
JustBeforeEach(func() {
2829

2930
By("creating test node")
3031
node := &v1.Node{
@@ -49,7 +50,7 @@ var _ = Describe("Checking health using grpc client and server", func() {
4950
}
5051

5152
By("Creating server")
52-
phServer, err = NewServer(k8sClient, reader, ctrl.Log.WithName("peerhealth test").WithName("phServer"), 9000, certReader, 7*time.Second)
53+
phServer, err = NewServer(k8sClient, reader, ctrl.Log.WithName("peerhealth test").WithName("phServer"), 9000, certReader, apiServerTimeout)
5354
Expect(err).ToNot(HaveOccurred())
5455

5556
By("Starting server")
@@ -132,15 +133,18 @@ var _ = Describe("Checking health using grpc client and server", func() {
132133

133134
BeforeEach(func() {
134135
reader.delay = &apiCallDelay
136+
apiServerTimeout = 3 * time.Second
137+
135138
})
136139

137140
AfterEach(func() {
138141
reader.delay = nil
142+
apiServerTimeout = 5 * time.Second
139143
})
140144

141145
It("should return API error", func() {
142146
By("calling isHealthy")
143-
// The health server code has a hardcoded timeout of 3s for the API call!
147+
// The health server code has a timeout of 3s for the API call!
144148
// When we add a delay of > 3s to the API call, that 3s timeout needs to be respected,
145149
// to not exceed the peerRequestTimeout (5s) of the client.
146150
ctx, cancel := context.WithTimeout(context.Background(), peerRequestTimeout)

pkg/peerhealth/server.go

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -154,18 +154,6 @@ func (s *Server) listWithTimeoutHandling(apiCtx context.Context, snrs *v1alpha1.
154154
}
155155
}
156156

157-
func (s *Server) getNode(ctx context.Context, nodeName string) (*corev1.Node, error) {
158-
apiCtx, cancelFunc := context.WithTimeout(ctx, s.apiServerTimeout)
159-
defer cancelFunc()
160-
161-
node := &corev1.Node{}
162-
if err := s.c.Get(apiCtx, client.ObjectKey{Name: nodeName}, node); err != nil {
163-
s.log.Error(err, "api error")
164-
return nil, err
165-
}
166-
return node, nil
167-
}
168-
169157
func toResponse(status selfNodeRemediationApis.HealthCheckResponseCode) (*HealthResponse, error) {
170158
return &HealthResponse{
171159
Status: int32(status),

0 commit comments

Comments
 (0)