-
Notifications
You must be signed in to change notification settings - Fork 408
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: improve hostNetwork mode of NodePool by adding NodeAffinity to …
…pods with specified annotation (#1935)
- Loading branch information
1 parent
367e2b5
commit eb92417
Showing
6 changed files
with
261 additions
and
0 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,60 @@ | ||
/* | ||
Copyright 2024 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 v1alpha1 | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
|
||
corev1 "k8s.io/api/core/v1" | ||
apierrors "k8s.io/apimachinery/pkg/api/errors" | ||
"k8s.io/apimachinery/pkg/runtime" | ||
"sigs.k8s.io/controller-runtime/pkg/webhook/admission" | ||
|
||
"github.com/openyurtio/openyurt/pkg/apis/apps" | ||
) | ||
|
||
// Default implements builder.CustomDefaulter. | ||
func (webhook *PodHandler) Default(ctx context.Context, obj runtime.Object, req admission.Request) error { | ||
pod, ok := obj.(*corev1.Pod) | ||
if !ok { | ||
return apierrors.NewBadRequest(fmt.Sprintf("expected a Pod but got a %T", obj)) | ||
} | ||
|
||
// Add NodeAffinity to pods in order to avoid pods to be scheduled on the nodes in the hostNetwork mode NodePool | ||
annotations := pod.ObjectMeta.GetAnnotations() | ||
if annotations != nil && annotations[apps.AnnotationExcludeHostNetworkPool] == "true" { | ||
pod.Spec.Affinity = &corev1.Affinity{ | ||
NodeAffinity: &corev1.NodeAffinity{ | ||
RequiredDuringSchedulingIgnoredDuringExecution: &corev1.NodeSelector{ | ||
NodeSelectorTerms: []corev1.NodeSelectorTerm{ | ||
{ | ||
MatchExpressions: []corev1.NodeSelectorRequirement{ | ||
{ | ||
Key: "nodepool.openyurt.io/hostnetwork", | ||
Operator: corev1.NodeSelectorOpNotIn, | ||
Values: []string{"true"}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
} | ||
} | ||
return nil | ||
} |
115 changes: 115 additions & 0 deletions
115
pkg/yurtmanager/webhook/pod/v1alpha1/pod_default_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,115 @@ | ||
/* | ||
Copyright 2024 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 v1alpha1 | ||
|
||
import ( | ||
"context" | ||
"reflect" | ||
"testing" | ||
|
||
corev1 "k8s.io/api/core/v1" | ||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
"k8s.io/apimachinery/pkg/runtime" | ||
"sigs.k8s.io/controller-runtime/pkg/webhook/admission" | ||
|
||
"github.com/openyurtio/openyurt/pkg/apis/apps" | ||
) | ||
|
||
func TestDefault(t *testing.T) { | ||
testcases := map[string]struct { | ||
obj runtime.Object | ||
errHappened bool | ||
wantedPod *corev1.Pod | ||
}{ | ||
"it is not a pod": { | ||
obj: &corev1.Node{}, | ||
errHappened: true, | ||
}, | ||
"pod with annotation['apps.openyurt.io/exclude-host-network-pool'] = true": { | ||
obj: &corev1.Pod{ | ||
ObjectMeta: metav1.ObjectMeta{ | ||
Name: "pod0", | ||
Namespace: metav1.NamespaceDefault, | ||
Annotations: map[string]string{ | ||
apps.AnnotationExcludeHostNetworkPool: "true", | ||
}, | ||
}, | ||
}, | ||
wantedPod: &corev1.Pod{ | ||
ObjectMeta: metav1.ObjectMeta{ | ||
Name: "pod0", | ||
Namespace: metav1.NamespaceDefault, | ||
Annotations: map[string]string{ | ||
apps.AnnotationExcludeHostNetworkPool: "true", | ||
}, | ||
}, | ||
Spec: corev1.PodSpec{ | ||
Affinity: &corev1.Affinity{ | ||
NodeAffinity: &corev1.NodeAffinity{ | ||
RequiredDuringSchedulingIgnoredDuringExecution: &corev1.NodeSelector{ | ||
NodeSelectorTerms: []corev1.NodeSelectorTerm{ | ||
{ | ||
MatchExpressions: []corev1.NodeSelectorRequirement{ | ||
{ | ||
Key: "nodepool.openyurt.io/hostnetwork", | ||
Operator: corev1.NodeSelectorOpNotIn, | ||
Values: []string{"true"}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
"pod without annotation['apps.openyurt.io/exclude-host-network-pool'] = true": { | ||
obj: &corev1.Pod{ | ||
ObjectMeta: metav1.ObjectMeta{ | ||
Name: "pod1", | ||
Namespace: metav1.NamespaceDefault, | ||
}, | ||
}, | ||
wantedPod: &corev1.Pod{ | ||
ObjectMeta: metav1.ObjectMeta{ | ||
Name: "pod1", | ||
Namespace: metav1.NamespaceDefault, | ||
}, | ||
}, | ||
}, | ||
} | ||
|
||
for k, tc := range testcases { | ||
t.Run(k, func(t *testing.T) { | ||
h := PodHandler{} | ||
err := h.Default(context.TODO(), tc.obj, admission.Request{}) | ||
if tc.errHappened { | ||
if err == nil { | ||
t.Errorf("expect error, got nil") | ||
} | ||
} else if err != nil { | ||
t.Errorf("expect no error, but got %v", err) | ||
} else { | ||
currentPod := tc.obj.(*corev1.Pod) | ||
if !reflect.DeepEqual(currentPod, tc.wantedPod) { | ||
t.Errorf("expect %#+v, got %#+v", tc.wantedPod, currentPod) | ||
} | ||
} | ||
}) | ||
} | ||
} |
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 2024 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 v1alpha1 | ||
|
||
import ( | ||
corev1 "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/yurtmanager/webhook/builder" | ||
"github.com/openyurtio/openyurt/pkg/yurtmanager/webhook/util" | ||
) | ||
|
||
const ( | ||
WebhookName = "pod" | ||
) | ||
|
||
// SetupWebhookWithManager sets up Cluster webhooks. mutate path, validate path, error | ||
func (webhook *PodHandler) SetupWebhookWithManager(mgr ctrl.Manager) (string, string, error) { | ||
// init | ||
webhook.Client = mgr.GetClient() | ||
|
||
gvk, err := apiutil.GVKForObject(&corev1.Pod{}, mgr.GetScheme()) | ||
if err != nil { | ||
return "", "", err | ||
} | ||
return util.GenerateMutatePath(gvk), | ||
util.GenerateValidatePath(gvk), | ||
builder.WebhookManagedBy(mgr). | ||
For(&corev1.Pod{}). | ||
WithDefaulter(webhook). | ||
Complete() | ||
} | ||
|
||
// +kubebuilder:webhook:path=/mutate-core-openyurt-io-v1-pod,mutating=true,failurePolicy=ignore,sideEffects=None,admissionReviewVersions=v1;v1beta1,groups="",resources=pods,verbs=create;update,versions=v1,name=mutate.core.v1.pod.openyurt.io | ||
|
||
// PodHandler implements a validating and defaulting webhook for Cluster. | ||
type PodHandler struct { | ||
Client client.Client | ||
} | ||
|
||
var _ builder.CustomDefaulter = &PodHandler{} |
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