-
Notifications
You must be signed in to change notification settings - Fork 407
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: prevent node movement by label modification (#1444)
* feat: prevent node movement by label modification Signed-off-by: ricky <[email protected]> * comment on nodepool related e2e test temporarily Signed-off-by: ricky <[email protected]> --------- Signed-off-by: ricky <[email protected]>
- Loading branch information
Showing
9 changed files
with
648 additions
and
511 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
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,57 @@ | ||
/* | ||
Copyright 2023 The OpenYurt 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 v1 | ||
|
||
import ( | ||
v1 "k8s.io/api/core/v1" | ||
ctrl "sigs.k8s.io/controller-runtime" | ||
"sigs.k8s.io/controller-runtime/pkg/client" | ||
"sigs.k8s.io/controller-runtime/pkg/client/apiutil" | ||
|
||
"github.com/openyurtio/openyurt/pkg/webhook/builder" | ||
"github.com/openyurtio/openyurt/pkg/webhook/util" | ||
) | ||
|
||
const ( | ||
WebhookName = "node" | ||
) | ||
|
||
// SetupWebhookWithManager sets up Cluster webhooks. mutate path, validatepath, error | ||
func (webhook *NodeHandler) SetupWebhookWithManager(mgr ctrl.Manager) (string, string, error) { | ||
// init | ||
webhook.Client = mgr.GetClient() | ||
|
||
gvk, err := apiutil.GVKForObject(&v1.Node{}, mgr.GetScheme()) | ||
if err != nil { | ||
return "", "", err | ||
} | ||
return util.GenerateMutatePath(gvk), | ||
util.GenerateValidatePath(gvk), | ||
builder.WebhookManagedBy(mgr). | ||
For(&v1.Node{}). | ||
WithValidator(webhook). | ||
Complete() | ||
} | ||
|
||
// +kubebuilder:webhook:path=/validate-core-openyurt-io-v1-node,mutating=false,failurePolicy=fail,sideEffects=None,admissionReviewVersions=v1;v1beta1,groups="",resources=nodes,verbs=update,versions=v1,name=validate.core.v1.node.openyurt.io | ||
|
||
// Cluster implements a validating and defaulting webhook for Cluster. | ||
type NodeHandler struct { | ||
Client client.Client | ||
} | ||
|
||
var _ builder.CustomValidator = &NodeHandler{} |
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,78 @@ | ||
/* | ||
Copyright 2023 The OpenYurt 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 v1 | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
|
||
v1 "k8s.io/api/core/v1" | ||
apierrors "k8s.io/apimachinery/pkg/api/errors" | ||
"k8s.io/apimachinery/pkg/runtime" | ||
"k8s.io/apimachinery/pkg/util/validation/field" | ||
"sigs.k8s.io/controller-runtime/pkg/webhook/admission" | ||
|
||
"github.com/openyurtio/openyurt/pkg/apis/apps" | ||
) | ||
|
||
// ValidateCreate implements webhook.CustomValidator so a webhook will be registered for the type. | ||
func (webhook *NodeHandler) ValidateCreate(_ context.Context, obj runtime.Object, req admission.Request) error { | ||
return nil | ||
} | ||
|
||
// ValidateUpdate implements webhook.CustomValidator so a webhook will be registered for the type. | ||
func (webhook *NodeHandler) ValidateUpdate(ctx context.Context, oldObj, newObj runtime.Object, req admission.Request) error { | ||
newNode, ok := newObj.(*v1.Node) | ||
if !ok { | ||
return apierrors.NewBadRequest(fmt.Sprintf("expected a Node but got a %T", newObj)) | ||
} | ||
oldNode, ok := oldObj.(*v1.Node) | ||
if !ok { | ||
return apierrors.NewBadRequest(fmt.Sprintf("expected a Node} but got a %T", oldObj)) | ||
} | ||
|
||
if allErrs := validateNodeUpdate(newNode, oldNode, req); len(allErrs) > 0 { | ||
return apierrors.NewInvalid(v1.SchemeGroupVersion.WithKind("Node").GroupKind(), newNode.Name, allErrs) | ||
} | ||
|
||
return nil | ||
} | ||
|
||
// ValidateDelete implements webhook.CustomValidator so a webhook will be registered for the type. | ||
func (webhook *NodeHandler) ValidateDelete(_ context.Context, obj runtime.Object, req admission.Request) error { | ||
return nil | ||
} | ||
|
||
func validateNodeUpdate(newNode, oldNode *v1.Node, req admission.Request) field.ErrorList { | ||
oldNp := oldNode.Labels[apps.LabelDesiredNodePool] | ||
newNp := newNode.Labels[apps.LabelDesiredNodePool] | ||
|
||
if len(oldNp) == 0 { | ||
return nil | ||
} | ||
|
||
// can not change LabelDesiredNodePool if it has been set | ||
if oldNp != newNp { | ||
return field.ErrorList([]*field.Error{ | ||
field.Forbidden( | ||
field.NewPath("metadata").Child("labels").Child(apps.LabelDesiredNodePool), | ||
"apps.openyurt.io/desired-nodepool can not be changed"), | ||
}) | ||
} | ||
|
||
return nil | ||
} |
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
Oops, something went wrong.