Skip to content

Commit bed80cc

Browse files
authored
chore: move event reasons to the package and as constants (#1915)
Signed-off-by: flavono123 <[email protected]>
1 parent f9881a6 commit bed80cc

File tree

9 files changed

+87
-38
lines changed

9 files changed

+87
-38
lines changed

pkg/controllers/disruption/consolidation_test.go

+6-6
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ var _ = Describe("Consolidation", func() {
112112
ExpectSingletonReconciled(ctx, disruptionController)
113113
wg.Wait()
114114

115-
Expect(recorder.Calls("Unconsolidatable")).To(Equal(0))
115+
Expect(recorder.Calls(events.Unconsolidatable)).To(Equal(0))
116116
})
117117
It("should fire an event for ConsolidationDisabled when the NodePool has consolidation set to WhenEmpty", func() {
118118
pod := test.Pod()
@@ -123,7 +123,7 @@ var _ = Describe("Consolidation", func() {
123123

124124
ExpectMakeNodesAndNodeClaimsInitializedAndStateUpdated(ctx, env.Client, nodeStateController, nodeClaimStateController, []*corev1.Node{node}, []*v1.NodeClaim{nodeClaim})
125125
ExpectSingletonReconciled(ctx, disruptionController)
126-
Expect(recorder.Calls("Unconsolidatable")).To(Equal(4))
126+
Expect(recorder.Calls(events.Unconsolidatable)).To(Equal(4))
127127
})
128128
It("should fire an event for ConsolidationDisabled when the NodePool has consolidateAfter set to 'Never'", func() {
129129
pod := test.Pod()
@@ -135,7 +135,7 @@ var _ = Describe("Consolidation", func() {
135135
ExpectSingletonReconciled(ctx, disruptionController)
136136
// We get six calls here because we have Nodes and NodeClaims that fired for this event
137137
// and each of the consolidation mechanisms specifies that this event should be fired
138-
Expect(recorder.Calls("Unconsolidatable")).To(Equal(6))
138+
Expect(recorder.Calls(events.Unconsolidatable)).To(Equal(6))
139139
})
140140
It("should fire an event when a candidate does not have a resolvable instance type", func() {
141141
pod := test.Pod()
@@ -148,7 +148,7 @@ var _ = Describe("Consolidation", func() {
148148
ExpectMakeNodesAndNodeClaimsInitializedAndStateUpdated(ctx, env.Client, nodeStateController, nodeClaimStateController, []*corev1.Node{node}, []*v1.NodeClaim{nodeClaim})
149149
ExpectSingletonReconciled(ctx, disruptionController)
150150
// We get four calls since we only care about this since we don't emit for empty node consolidation
151-
Expect(recorder.Calls("Unconsolidatable")).To(Equal(4))
151+
Expect(recorder.Calls(events.Unconsolidatable)).To(Equal(4))
152152
})
153153
It("should fire an event when a candidate does not have the capacity type label", func() {
154154
pod := test.Pod()
@@ -161,7 +161,7 @@ var _ = Describe("Consolidation", func() {
161161
ExpectMakeNodesAndNodeClaimsInitializedAndStateUpdated(ctx, env.Client, nodeStateController, nodeClaimStateController, []*corev1.Node{node}, []*v1.NodeClaim{nodeClaim})
162162
ExpectSingletonReconciled(ctx, disruptionController)
163163
// We get four calls since we only care about this since we don't emit for empty node consolidation
164-
Expect(recorder.Calls("Unconsolidatable")).To(Equal(4))
164+
Expect(recorder.Calls(events.Unconsolidatable)).To(Equal(4))
165165
})
166166
It("should fire an event when a candidate does not have the zone label", func() {
167167
pod := test.Pod()
@@ -174,7 +174,7 @@ var _ = Describe("Consolidation", func() {
174174
ExpectMakeNodesAndNodeClaimsInitializedAndStateUpdated(ctx, env.Client, nodeStateController, nodeClaimStateController, []*corev1.Node{node}, []*v1.NodeClaim{nodeClaim})
175175
ExpectSingletonReconciled(ctx, disruptionController)
176176
// We get four calls since we only care about this since we don't emit for empty node consolidation
177-
Expect(recorder.Calls("Unconsolidatable")).To(Equal(4))
177+
Expect(recorder.Calls(events.Unconsolidatable)).To(Equal(4))
178178
})
179179
})
180180
Context("Metrics", func() {

pkg/controllers/disruption/events/events.go

+10-10
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ func Launching(nodeClaim *v1.NodeClaim, reason string) events.Event {
3232
return events.Event{
3333
InvolvedObject: nodeClaim,
3434
Type: corev1.EventTypeNormal,
35-
Reason: "DisruptionLaunching",
35+
Reason: events.DisruptionLaunching,
3636
Message: fmt.Sprintf("Launching NodeClaim: %s", cases.Title(language.Und, cases.NoLower).String(reason)),
3737
DedupeValues: []string{string(nodeClaim.UID), reason},
3838
}
@@ -42,7 +42,7 @@ func WaitingOnReadiness(nodeClaim *v1.NodeClaim) events.Event {
4242
return events.Event{
4343
InvolvedObject: nodeClaim,
4444
Type: corev1.EventTypeNormal,
45-
Reason: "DisruptionWaitingReadiness",
45+
Reason: events.DisruptionWaitingReadiness,
4646
Message: "Waiting on readiness to continue disruption",
4747
DedupeValues: []string{string(nodeClaim.UID)},
4848
}
@@ -53,14 +53,14 @@ func Terminating(node *corev1.Node, nodeClaim *v1.NodeClaim, reason string) []ev
5353
{
5454
InvolvedObject: node,
5555
Type: corev1.EventTypeNormal,
56-
Reason: "DisruptionTerminating",
56+
Reason: events.DisruptionTerminating,
5757
Message: fmt.Sprintf("Disrupting Node: %s", cases.Title(language.Und, cases.NoLower).String(reason)),
5858
DedupeValues: []string{string(node.UID), reason},
5959
},
6060
{
6161
InvolvedObject: nodeClaim,
6262
Type: corev1.EventTypeNormal,
63-
Reason: "DisruptionTerminating",
63+
Reason: events.DisruptionTerminating,
6464
Message: fmt.Sprintf("Disrupting NodeClaim: %s", cases.Title(language.Und, cases.NoLower).String(reason)),
6565
DedupeValues: []string{string(nodeClaim.UID), reason},
6666
},
@@ -74,15 +74,15 @@ func Unconsolidatable(node *corev1.Node, nodeClaim *v1.NodeClaim, msg string) []
7474
{
7575
InvolvedObject: node,
7676
Type: corev1.EventTypeNormal,
77-
Reason: "Unconsolidatable",
77+
Reason: events.Unconsolidatable,
7878
Message: msg,
7979
DedupeValues: []string{string(node.UID)},
8080
DedupeTimeout: time.Minute * 15,
8181
},
8282
{
8383
InvolvedObject: nodeClaim,
8484
Type: corev1.EventTypeNormal,
85-
Reason: "Unconsolidatable",
85+
Reason: events.Unconsolidatable,
8686
Message: msg,
8787
DedupeValues: []string{string(nodeClaim.UID)},
8888
DedupeTimeout: time.Minute * 15,
@@ -97,7 +97,7 @@ func Blocked(node *corev1.Node, nodeClaim *v1.NodeClaim, msg string) (evs []even
9797
evs = append(evs, events.Event{
9898
InvolvedObject: node,
9999
Type: corev1.EventTypeNormal,
100-
Reason: "DisruptionBlocked",
100+
Reason: events.DisruptionBlocked,
101101
Message: msg,
102102
DedupeValues: []string{string(node.UID)},
103103
})
@@ -106,7 +106,7 @@ func Blocked(node *corev1.Node, nodeClaim *v1.NodeClaim, msg string) (evs []even
106106
evs = append(evs, events.Event{
107107
InvolvedObject: nodeClaim,
108108
Type: corev1.EventTypeNormal,
109-
Reason: "DisruptionBlocked",
109+
Reason: events.DisruptionBlocked,
110110
Message: msg,
111111
DedupeValues: []string{string(nodeClaim.UID)},
112112
})
@@ -118,7 +118,7 @@ func NodePoolBlockedForDisruptionReason(nodePool *v1.NodePool, reason v1.Disrupt
118118
return events.Event{
119119
InvolvedObject: nodePool,
120120
Type: corev1.EventTypeNormal,
121-
Reason: "DisruptionBlocked",
121+
Reason: events.DisruptionBlocked,
122122
Message: fmt.Sprintf("No allowed disruptions for disruption reason %s due to blocking budget", reason),
123123
DedupeValues: []string{string(nodePool.UID), string(reason)},
124124
DedupeTimeout: 1 * time.Minute,
@@ -129,7 +129,7 @@ func NodePoolBlocked(nodePool *v1.NodePool) events.Event {
129129
return events.Event{
130130
InvolvedObject: nodePool,
131131
Type: corev1.EventTypeNormal,
132-
Reason: "DisruptionBlocked",
132+
Reason: events.DisruptionBlocked,
133133
Message: "No allowed disruptions due to blocking budget",
134134
DedupeValues: []string{string(nodePool.UID)},
135135
// Set a small timeout as a NodePool's disruption budget can change every minute.

pkg/controllers/node/health/events.go

+5-5
Original file line numberDiff line numberDiff line change
@@ -30,23 +30,23 @@ func NodeRepairBlocked(node *corev1.Node, nodeClaim *v1.NodeClaim, nodePool *v1.
3030
{
3131
InvolvedObject: node,
3232
Type: corev1.EventTypeWarning,
33-
Reason: "NodeRepairBlocked",
33+
Reason: events.NodeRepairBlocked,
3434
Message: reason,
3535
DedupeValues: []string{string(node.UID)},
3636
DedupeTimeout: time.Minute * 15,
3737
},
3838
{
3939
InvolvedObject: node,
4040
Type: corev1.EventTypeWarning,
41-
Reason: "NodeRepairBlocked",
41+
Reason: events.NodeRepairBlocked,
4242
Message: reason,
4343
DedupeValues: []string{string(nodeClaim.UID)},
4444
DedupeTimeout: time.Minute * 15,
4545
},
4646
{
4747
InvolvedObject: node,
4848
Type: corev1.EventTypeWarning,
49-
Reason: "NodeRepairBlocked",
49+
Reason: events.NodeRepairBlocked,
5050
Message: reason,
5151
DedupeValues: []string{string(nodePool.UID)},
5252
DedupeTimeout: time.Minute * 15,
@@ -59,15 +59,15 @@ func NodeRepairBlockedUnmanagedNodeClaim(node *corev1.Node, nodeClaim *v1.NodeCl
5959
{
6060
InvolvedObject: node,
6161
Type: corev1.EventTypeWarning,
62-
Reason: "NodeRepairBlocked",
62+
Reason: events.NodeRepairBlocked,
6363
Message: reason,
6464
DedupeValues: []string{string(node.UID)},
6565
DedupeTimeout: time.Minute * 15,
6666
},
6767
{
6868
InvolvedObject: node,
6969
Type: corev1.EventTypeWarning,
70-
Reason: "NodeRepairBlocked",
70+
Reason: events.NodeRepairBlocked,
7171
Message: reason,
7272
DedupeValues: []string{string(nodeClaim.UID)},
7373
DedupeTimeout: time.Minute * 15,

pkg/controllers/node/termination/terminator/events/events.go

+5-5
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ func EvictPod(pod *corev1.Pod, message string) events.Event {
3030
return events.Event{
3131
InvolvedObject: pod,
3232
Type: corev1.EventTypeNormal,
33-
Reason: "Evicted",
33+
Reason: events.Evicted,
3434
Message: "Evicted pod: " + message,
3535
DedupeValues: []string{pod.Name},
3636
}
@@ -40,7 +40,7 @@ func DisruptPodDelete(pod *corev1.Pod, gracePeriodSeconds *int64, nodeGracePerio
4040
return events.Event{
4141
InvolvedObject: pod,
4242
Type: corev1.EventTypeNormal,
43-
Reason: "Disrupted",
43+
Reason: events.Disrupted,
4444
Message: fmt.Sprintf("Deleting the pod to accommodate the terminationTime %v of the node. The pod was granted %v seconds of grace-period of its %v terminationGracePeriodSeconds. This bypasses the PDB of the pod and the do-not-disrupt annotation.", *nodeGracePeriodTerminationTime, *gracePeriodSeconds, pod.Spec.TerminationGracePeriodSeconds),
4545
DedupeValues: []string{pod.Name},
4646
}
@@ -50,7 +50,7 @@ func NodeFailedToDrain(node *corev1.Node, err error) events.Event {
5050
return events.Event{
5151
InvolvedObject: node,
5252
Type: corev1.EventTypeWarning,
53-
Reason: "FailedDraining",
53+
Reason: events.FailedDraining,
5454
Message: fmt.Sprintf("Failed to drain node, %s", err),
5555
DedupeValues: []string{node.Name},
5656
}
@@ -60,7 +60,7 @@ func NodeTerminationGracePeriodExpiring(node *corev1.Node, terminationTime strin
6060
return events.Event{
6161
InvolvedObject: node,
6262
Type: corev1.EventTypeWarning,
63-
Reason: "TerminationGracePeriodExpiring",
63+
Reason: events.TerminationGracePeriodExpiring,
6464
Message: fmt.Sprintf("All pods will be deleted by %s", terminationTime),
6565
DedupeValues: []string{node.Name},
6666
}
@@ -70,7 +70,7 @@ func NodeClaimTerminationGracePeriodExpiring(nodeClaim *v1.NodeClaim, terminatio
7070
return events.Event{
7171
InvolvedObject: nodeClaim,
7272
Type: corev1.EventTypeWarning,
73-
Reason: "TerminationGracePeriodExpiring",
73+
Reason: events.TerminationGracePeriodExpiring,
7474
Message: fmt.Sprintf("All pods will be deleted by %s", terminationTime),
7575
DedupeValues: []string{nodeClaim.Name},
7676
}

pkg/controllers/node/termination/terminator/suite_test.go

+7-6
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ import (
3535

3636
"sigs.k8s.io/karpenter/pkg/apis"
3737
"sigs.k8s.io/karpenter/pkg/controllers/node/termination/terminator"
38+
"sigs.k8s.io/karpenter/pkg/events"
3839
"sigs.k8s.io/karpenter/pkg/operator/options"
3940
"sigs.k8s.io/karpenter/pkg/test"
4041
. "sigs.k8s.io/karpenter/pkg/test/expectations"
@@ -112,7 +113,7 @@ var _ = Describe("Eviction/Queue", func() {
112113
ExpectApplied(ctx, env.Client, pod)
113114
Expect(queue.Evict(ctx, terminator.NewQueueKey(pod, node.Spec.ProviderID))).To(BeTrue())
114115
ExpectMetricCounterValue(terminator.NodesEvictionRequestsTotal, 1, map[string]string{terminator.CodeLabel: "200"})
115-
Expect(recorder.Calls("Evicted")).To(Equal(1))
116+
Expect(recorder.Calls(events.Evicted)).To(Equal(1))
116117
})
117118
It("should succeed with no event when there are PDBs that allow an eviction", func() {
118119
pdb = test.PodDisruptionBudget(test.PDBOptions{
@@ -121,12 +122,12 @@ var _ = Describe("Eviction/Queue", func() {
121122
})
122123
ExpectApplied(ctx, env.Client, pod)
123124
Expect(queue.Evict(ctx, terminator.NewQueueKey(pod, node.Spec.ProviderID))).To(BeTrue())
124-
Expect(recorder.Calls("Evicted")).To(Equal(1))
125+
Expect(recorder.Calls(events.Evicted)).To(Equal(1))
125126
})
126127
It("should return a NodeDrainError event when a PDB is blocking", func() {
127128
ExpectApplied(ctx, env.Client, pdb, pod)
128129
Expect(queue.Evict(ctx, terminator.NewQueueKey(pod, node.Spec.ProviderID))).To(BeFalse())
129-
Expect(recorder.Calls("FailedDraining")).To(Equal(1))
130+
Expect(recorder.Calls(events.FailedDraining)).To(Equal(1))
130131
})
131132
It("should fail when two PDBs refer to the same pod", func() {
132133
pdb2 := test.PodDisruptionBudget(test.PDBOptions{
@@ -171,7 +172,7 @@ var _ = Describe("Eviction/Queue", func() {
171172

172173
Expect(terminatorInstance.DeleteExpiringPods(ctx, []*corev1.Pod{pod}, nil)).To(Succeed())
173174
ExpectExists(ctx, env.Client, pod)
174-
Expect(recorder.Calls("Disrupted")).To(Equal(0))
175+
Expect(recorder.Calls(events.Disrupted)).To(Equal(0))
175176
})
176177
It("should not delete a pod with terminationGracePeriodSeconds still remaining before nodeTerminationTime", func() {
177178
pod.Spec.TerminationGracePeriodSeconds = lo.ToPtr[int64](60)
@@ -180,7 +181,7 @@ var _ = Describe("Eviction/Queue", func() {
180181
nodeTerminationTime := time.Now().Add(time.Minute * 5)
181182
Expect(terminatorInstance.DeleteExpiringPods(ctx, []*corev1.Pod{pod}, &nodeTerminationTime)).To(Succeed())
182183
ExpectExists(ctx, env.Client, pod)
183-
Expect(recorder.Calls("Disrupted")).To(Equal(0))
184+
Expect(recorder.Calls(events.Disrupted)).To(Equal(0))
184185
})
185186
It("should delete a pod with less than terminationGracePeriodSeconds remaining before nodeTerminationTime", func() {
186187
pod.Spec.TerminationGracePeriodSeconds = lo.ToPtr[int64](120)
@@ -189,7 +190,7 @@ var _ = Describe("Eviction/Queue", func() {
189190
nodeTerminationTime := time.Now().Add(time.Minute * 1)
190191
Expect(terminatorInstance.DeleteExpiringPods(ctx, []*corev1.Pod{pod}, &nodeTerminationTime)).To(Succeed())
191192
ExpectNotFound(ctx, env.Client, pod)
192-
Expect(recorder.Calls("Disrupted")).To(Equal(1))
193+
Expect(recorder.Calls(events.Disrupted)).To(Equal(1))
193194
})
194195
})
195196
})

pkg/controllers/nodeclaim/consistency/events.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ func FailedConsistencyCheckEvent(nodeClaim *v1.NodeClaim, message string) events
2727
return events.Event{
2828
InvolvedObject: nodeClaim,
2929
Type: corev1.EventTypeWarning,
30-
Reason: "FailedConsistencyCheck",
30+
Reason: events.FailedConsistencyCheck,
3131
Message: message,
3232
DedupeValues: []string{string(nodeClaim.UID), message},
3333
}

pkg/controllers/nodeclaim/lifecycle/events.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ func InsufficientCapacityErrorEvent(nodeClaim *v1.NodeClaim, err error) events.E
2929
return events.Event{
3030
InvolvedObject: nodeClaim,
3131
Type: corev1.EventTypeWarning,
32-
Reason: "InsufficientCapacityError",
32+
Reason: events.InsufficientCapacityError,
3333
Message: fmt.Sprintf("NodeClaim %s event: %s", nodeClaim.Name, truncateMessage(err.Error())),
3434
DedupeValues: []string{string(nodeClaim.UID)},
3535
}
@@ -39,7 +39,7 @@ func NodeClassNotReadyEvent(nodeClaim *v1.NodeClaim, err error) events.Event {
3939
return events.Event{
4040
InvolvedObject: nodeClaim,
4141
Type: corev1.EventTypeWarning,
42-
Reason: "NodeClassNotReady",
42+
Reason: events.NodeClassNotReady,
4343
Message: fmt.Sprintf("NodeClaim %s event: %s", nodeClaim.Name, truncateMessage(err.Error())),
4444
DedupeValues: []string{string(nodeClaim.UID)},
4545
}

pkg/controllers/provisioning/scheduling/events.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ func NominatePodEvent(pod *corev1.Pod, node *corev1.Node, nodeClaim *v1.NodeClai
4242
return events.Event{
4343
InvolvedObject: pod,
4444
Type: corev1.EventTypeNormal,
45-
Reason: "Nominated",
45+
Reason: events.Nominated,
4646
Message: fmt.Sprintf("Pod should schedule on: %s", strings.Join(info, ", ")),
4747
DedupeValues: []string{string(pod.UID)},
4848
RateLimiter: PodNominationRateLimiter,
@@ -53,7 +53,7 @@ func NoCompatibleInstanceTypes(np *v1.NodePool) events.Event {
5353
return events.Event{
5454
InvolvedObject: np,
5555
Type: corev1.EventTypeWarning,
56-
Reason: "NoCompatibleInstanceTypes",
56+
Reason: events.NoCompatibleInstanceTypes,
5757
Message: "NodePool requirements filtered out all compatible available instance types",
5858
DedupeValues: []string{string(np.UID)},
5959
DedupeTimeout: 1 * time.Minute,
@@ -64,7 +64,7 @@ func PodFailedToScheduleEvent(pod *corev1.Pod, err error) events.Event {
6464
return events.Event{
6565
InvolvedObject: pod,
6666
Type: corev1.EventTypeWarning,
67-
Reason: "FailedScheduling",
67+
Reason: events.FailedScheduling,
6868
Message: fmt.Sprintf("Failed to schedule pod, %s", err),
6969
DedupeValues: []string{string(pod.UID)},
7070
DedupeTimeout: 5 * time.Minute,

pkg/events/reason.go

+48
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
/*
2+
Copyright The Kubernetes Authors.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
package events
18+
19+
// Reasons of events controllers emit
20+
const (
21+
// disruption
22+
DisruptionBlocked = "DisruptionBlocked"
23+
DisruptionLaunching = "DisruptionLaunching"
24+
DisruptionTerminating = "DisruptionTerminating"
25+
DisruptionWaitingReadiness = "DisruptionWaitingReadiness"
26+
Unconsolidatable = "Unconsolidatable"
27+
28+
// provisioning/scheduling
29+
FailedScheduling = "FailedScheduling"
30+
NoCompatibleInstanceTypes = "NoCompatibleInstanceTypes"
31+
Nominated = "Nominated"
32+
33+
// node/health
34+
NodeRepairBlocked = "NodeRepairBlocked"
35+
36+
// node/termination/terminator
37+
Disrupted = "Disrupted"
38+
Evicted = "Evicted"
39+
FailedDraining = "FailedDraining"
40+
TerminationGracePeriodExpiring = "TerminationGracePeriodExpiring"
41+
42+
// nodeclaim/consistency
43+
FailedConsistencyCheck = "FailedConsistencyCheck"
44+
45+
// nodeclaim/lifecycle
46+
InsufficientCapacityError = "InsufficientCapacityError"
47+
NodeClassNotReady = "NodeClassNotReady"
48+
)

0 commit comments

Comments
 (0)