-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(webhook): add garcedelete webhook for PodOpsLifecycle (#134)
* feat(webhook): add garcedelete webhook for PodOpsLifecycle * refactor(garcedeletewebhook): reuse PodDeleteOpsLifecycleAdapter, improved garcedeletewebhook return message * feat(gracedelete webhook): add featuregate for gracedelete webhook * style(gracedelete): optimize klog and annotation
- Loading branch information
Showing
9 changed files
with
249 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -76,6 +76,7 @@ webhooks: | |
operations: | ||
- CREATE | ||
- UPDATE | ||
- DELETE | ||
resources: | ||
- pods | ||
scope: '*' | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
/* | ||
Copyright 2023 The KusionStack Authors. | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
package gracedelete | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"strings" | ||
|
||
admissionv1 "k8s.io/api/admission/v1" | ||
corev1 "k8s.io/api/core/v1" | ||
"k8s.io/apimachinery/pkg/api/errors" | ||
"k8s.io/apimachinery/pkg/types" | ||
"k8s.io/klog/v2" | ||
"kusionstack.io/operating/apis/apps/v1alpha1" | ||
"kusionstack.io/operating/pkg/controllers/poddeletion" | ||
"kusionstack.io/operating/pkg/controllers/utils/podopslifecycle" | ||
"kusionstack.io/operating/pkg/features" | ||
"kusionstack.io/operating/pkg/utils" | ||
"kusionstack.io/operating/pkg/utils/feature" | ||
"sigs.k8s.io/controller-runtime/pkg/client" | ||
) | ||
|
||
type GraceDelete struct { | ||
} | ||
|
||
func New() *GraceDelete { | ||
return &GraceDelete{} | ||
} | ||
|
||
func (gd *GraceDelete) Name() string { | ||
return "GraceDeleteWebhook" | ||
} | ||
|
||
func (gd *GraceDelete) Validating(ctx context.Context, c client.Client, oldPod, newPod *corev1.Pod, operation admissionv1.Operation) error { | ||
// GraceDeleteWebhook FeatureGate defaults to false | ||
// Add '--feature-gates=GraceDeleteWebhook=true' to container args, to enable gracedelete webhook | ||
if !feature.DefaultFeatureGate.Enabled(features.GraceDeleteWebhook) || operation != admissionv1.Delete { | ||
return nil | ||
} | ||
|
||
pod := &corev1.Pod{} | ||
if err := c.Get(ctx, types.NamespacedName{Namespace: oldPod.Namespace, Name: oldPod.Name}, pod); err != nil { | ||
if !errors.IsNotFound(err) { | ||
klog.Error(err, "failed to find pod") | ||
return err | ||
} | ||
|
||
klog.V(2).Info("pod is deleted") | ||
return nil | ||
} | ||
if !utils.ControlledByKusionStack(pod) { | ||
return nil | ||
} | ||
|
||
// if Pod is not begin a deletion PodOpsLifecycle, trigger it | ||
if !podopslifecycle.IsDuringOps(poddeletion.OpsLifecycleAdapter, pod) { | ||
if _, err := podopslifecycle.Begin(c, poddeletion.OpsLifecycleAdapter, pod); err != nil { | ||
return fmt.Errorf("fail to begin PodOpsLifecycle to delete Pod %s: %s", pod.Name, err) | ||
} | ||
} | ||
|
||
// if Pod is allow to operate, delete it | ||
if _, allowed := podopslifecycle.AllowOps(poddeletion.OpsLifecycleAdapter, 0, pod); !allowed { | ||
var finalizers []string | ||
for _, f := range pod.Finalizers { | ||
if strings.HasPrefix(f, v1alpha1.PodOperationProtectionFinalizerPrefix) { | ||
finalizers = append(finalizers, f) | ||
} | ||
} | ||
return fmt.Errorf("podOpsLifecycle denied delete request, since related resources and finalizers have not been processed. Waiting for removing finalizers: %v", finalizers) | ||
} | ||
return nil | ||
} | ||
|
||
func (gd *GraceDelete) Mutating(ctx context.Context, c client.Client, oldPod, newPod *corev1.Pod, operation admissionv1.Operation) error { | ||
return nil | ||
} |
142 changes: 142 additions & 0 deletions
142
pkg/webhook/server/generic/pod/gracedelete/webhook_test.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,142 @@ | ||
/* | ||
Copyright 2023 The KusionStack Authors. | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
package gracedelete | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
"k8s.io/apimachinery/pkg/util/runtime" | ||
"k8s.io/kubectl/pkg/scheme" | ||
"kusionstack.io/operating/apis/apps/v1alpha1" | ||
"kusionstack.io/operating/pkg/controllers/poddeletion" | ||
"kusionstack.io/operating/pkg/utils/feature" | ||
"sigs.k8s.io/controller-runtime/pkg/client/fake" | ||
|
||
admissionv1 "k8s.io/api/admission/v1" | ||
corev1 "k8s.io/api/core/v1" | ||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
"k8s.io/apimachinery/pkg/types" | ||
) | ||
|
||
func TestGraceDelete(t *testing.T) { | ||
|
||
inputs := []struct { | ||
keyWords string // used to check the error message | ||
fakePod corev1.Pod | ||
oldPod corev1.Pod | ||
|
||
podLabels map[string]string | ||
expectedLabels map[string]string | ||
|
||
reqOperation admissionv1.Operation | ||
}{ | ||
{ | ||
reqOperation: admissionv1.Update, | ||
}, | ||
{ | ||
reqOperation: admissionv1.Delete, | ||
}, | ||
{ | ||
fakePod: corev1.Pod{ | ||
ObjectMeta: metav1.ObjectMeta{ | ||
Namespace: "default", | ||
Name: "test", | ||
}, | ||
}, | ||
oldPod: corev1.Pod{ | ||
ObjectMeta: metav1.ObjectMeta{ | ||
Namespace: "default", | ||
Name: "test2", | ||
}, | ||
}, | ||
reqOperation: admissionv1.Delete, | ||
}, | ||
{ | ||
fakePod: corev1.Pod{ | ||
ObjectMeta: metav1.ObjectMeta{ | ||
Namespace: "default", | ||
Name: "test", | ||
Labels: map[string]string{ | ||
fmt.Sprintf(v1alpha1.ControlledByKusionStackLabelKey): "true", | ||
}, | ||
}, | ||
}, | ||
oldPod: corev1.Pod{ | ||
ObjectMeta: metav1.ObjectMeta{ | ||
Namespace: "default", | ||
Name: "test", | ||
Labels: map[string]string{ | ||
fmt.Sprintf(v1alpha1.ControlledByKusionStackLabelKey): "true", | ||
}, | ||
}, | ||
}, | ||
expectedLabels: map[string]string{ | ||
fmt.Sprintf("%s/%s", v1alpha1.PodOperatingLabelPrefix, poddeletion.OpsLifecycleAdapter.GetID()): "testvalue", | ||
fmt.Sprintf("%s/%s", v1alpha1.PodOperationTypeLabelPrefix, poddeletion.OpsLifecycleAdapter.GetID()): "testvalue", | ||
}, | ||
keyWords: "podOpsLifecycle denied", | ||
reqOperation: admissionv1.Delete, | ||
}, | ||
{ | ||
fakePod: corev1.Pod{ | ||
ObjectMeta: metav1.ObjectMeta{ | ||
Namespace: "default", | ||
Name: "test", | ||
Labels: map[string]string{ | ||
fmt.Sprintf(v1alpha1.ControlledByKusionStackLabelKey): "true", | ||
fmt.Sprintf("%s/%s", v1alpha1.PodOperateLabelPrefix, poddeletion.OpsLifecycleAdapter.GetID()): "true", | ||
}, | ||
}, | ||
}, | ||
oldPod: corev1.Pod{ | ||
ObjectMeta: metav1.ObjectMeta{ | ||
Namespace: "default", | ||
Name: "test", | ||
Labels: map[string]string{ | ||
fmt.Sprintf(v1alpha1.ControlledByKusionStackLabelKey): "true", | ||
fmt.Sprintf("%s/%s", v1alpha1.PodOperateLabelPrefix, poddeletion.OpsLifecycleAdapter.GetID()): "true", | ||
}, | ||
}, | ||
}, | ||
reqOperation: admissionv1.Delete, | ||
}, | ||
} | ||
|
||
gd := New() | ||
runtime.Must(feature.DefaultMutableFeatureGate.Set("GraceDeleteWebhook=true")) | ||
for _, v := range inputs { | ||
client := fake.NewClientBuilder().WithScheme(scheme.Scheme).WithObjects(&v.fakePod).Build() | ||
err := gd.Validating(context.Background(), client, &v.oldPod, nil, v.reqOperation) | ||
if v.keyWords == "" { | ||
assert.Nil(t, err) | ||
} else { | ||
assert.NotNil(t, err) | ||
assert.Contains(t, err.Error(), v.keyWords) | ||
} | ||
if len(v.expectedLabels) != 0 { | ||
pod := &corev1.Pod{} | ||
client.Get(context.Background(), types.NamespacedName{Namespace: v.oldPod.Namespace, Name: v.oldPod.Name}, pod) | ||
for k := range v.expectedLabels { | ||
_, exist := pod.Labels[k] | ||
assert.True(t, exist) | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters