Skip to content

Commit 7253283

Browse files
authored
Fix the web-hook to ignore resources with a deletion timestamp set (#655)
1 parent b8c03ae commit 7253283

File tree

2 files changed

+46
-1
lines changed

2 files changed

+46
-1
lines changed

api/v1/coherence_webhook.go

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,14 @@ func SetCommonDefaults(in CoherenceResource) {
6161
logger := webhookLogger.WithValues("namespace", in.GetNamespace(), "name", in.GetName())
6262
status := in.GetStatus()
6363
spec := in.GetSpec()
64+
dt := in.GetDeletionTimestamp()
65+
66+
if dt != nil {
67+
// the deletion timestamp is set so do nothing
68+
logger.Info("Skipping updating defaults for deleted resource", "deletionTimestamp", *dt)
69+
return
70+
}
71+
6472
if status.Phase == "" {
6573
logger.Info("Setting defaults for new resource")
6674

@@ -105,8 +113,8 @@ func SetCommonDefaults(in CoherenceResource) {
105113
// Set the features supported by this version
106114
in.AddAnnotation(AnnotationFeatureSuspend, "true")
107115
} else {
108-
logger.Info("Updating defaults for existing resource")
109116
// this is an update
117+
logger.Info("Updating defaults for existing resource")
110118
}
111119

112120
// apply the Operator version annotation
@@ -134,8 +142,16 @@ var commonWebHook = CommonWebHook{}
134142
// The optional warnings will be added to the response as warning messages.
135143
// Return an error if the object is invalid.
136144
func (in *Coherence) ValidateCreate() (admission.Warnings, error) {
145+
logger := webhookLogger.WithValues("namespace", in.GetNamespace(), "name", in.GetName())
137146
var warnings admission.Warnings
138147

148+
dt := in.GetDeletionTimestamp()
149+
if dt != nil {
150+
// the deletion timestamp is set so do nothing
151+
logger.Info("Skipping validation for deleted resource", "deletionTimestamp", *dt)
152+
return warnings, nil
153+
}
154+
139155
webhookLogger.Info("validate create", "name", in.Name)
140156
if err := commonWebHook.validateReplicas(in); err != nil {
141157
return warnings, err
@@ -154,8 +170,16 @@ func (in *Coherence) ValidateCreate() (admission.Warnings, error) {
154170
// Return an error if the object is invalid.
155171
func (in *Coherence) ValidateUpdate(previous runtime.Object) (admission.Warnings, error) {
156172
webhookLogger.Info("validate update", "name", in.Name)
173+
logger := webhookLogger.WithValues("namespace", in.GetNamespace(), "name", in.GetName())
157174
var warnings admission.Warnings
158175

176+
dt := in.GetDeletionTimestamp()
177+
if dt != nil {
178+
// the deletion timestamp is set so do nothing
179+
logger.Info("Skipping validation for deleted resource", "deletionTimestamp", *dt)
180+
return warnings, nil
181+
}
182+
159183
if err := commonWebHook.validateReplicas(in); err != nil {
160184
return warnings, err
161185
}

api/v1/coherence_webhook_test.go

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import (
1616
"k8s.io/apimachinery/pkg/util/intstr"
1717
"k8s.io/utils/ptr"
1818
"testing"
19+
"time"
1920
)
2021

2122
func TestDefaultReplicasIsSet(t *testing.T) {
@@ -77,6 +78,26 @@ func TestShouldNotRemoveFinalizersAlreadyPresent(t *testing.T) {
7778
g.Expect(finalizers).To(ContainElement(coh.CoherenceFinalizer))
7879
}
7980

81+
func TestNoNotAddFinalizerToDeletedResource(t *testing.T) {
82+
g := NewGomegaWithT(t)
83+
84+
dt := &metav1.Time{
85+
Time: time.Now(),
86+
}
87+
88+
c := &coh.Coherence{
89+
ObjectMeta: metav1.ObjectMeta{
90+
Name: "foo",
91+
DeletionTimestamp: dt,
92+
},
93+
Spec: coh.CoherenceStatefulSetResourceSpec{},
94+
}
95+
96+
c.Default()
97+
finalizers := c.GetFinalizers()
98+
g.Expect(finalizers).To(BeNil())
99+
}
100+
80101
func TestDefaultReplicasIsNotOverriddenWhenAlreadySet(t *testing.T) {
81102
g := NewGomegaWithT(t)
82103

0 commit comments

Comments
 (0)