Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add endpoints webhook for node autonomy #2211

Merged
merged 4 commits into from
Dec 2, 2024
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions charts/yurt-manager/templates/yurt-manager-auto-generated.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -1419,6 +1419,25 @@ webhooks:
resources:
- deployments
sideEffects: None
- admissionReviewVersions:
- v1
clientConfig:
service:
name: yurt-manager-webhook-service
namespace: {{ .Release.Namespace }}
path: /mutate-core-openyurt-io-v1-endpoints
failurePolicy: Ignore
name: mutate.core.v1.endpoints.openyurt.io
rules:
- apiGroups:
- ""
apiVersions:
- v1
operations:
- UPDATE
resources:
- endpoints
sideEffects: None
- admissionReviewVersions:
- v1
- v1beta1
Expand Down
10 changes: 10 additions & 0 deletions pkg/yurtmanager/controller/util/pod/pod_util.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,16 @@ func IsPodReadyConditionTrue(status v1.PodStatus) bool {
return condition != nil && condition.Status == v1.ConditionTrue
}

// IsPodCrashLoopBackOff returns true if a pod is in CrashLoopBackOff state; false otherwise.
func IsPodCrashLoopBackOff(status v1.PodStatus) bool {
for _, c := range status.ContainerStatuses {
if c.State.Waiting != nil && c.State.Waiting.Reason == "CrashLoopBackOff" {
return true
}
}
return false
}

// GetPodReadyCondition extracts the pod ready condition from the given status and returns that.
// Returns nil if the condition is not present.
func GetPodReadyCondition(status v1.PodStatus) *v1.PodCondition {
Expand Down
47 changes: 47 additions & 0 deletions pkg/yurtmanager/controller/util/pod/pod_util_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"testing"
"time"

"github.com/stretchr/testify/require"
v1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/util/rand"
Expand Down Expand Up @@ -168,3 +169,49 @@ func TestUpdatePodCondition(t *testing.T) {
})
}
}

func TestIsPodCrashLoopBackoff(t *testing.T) {
testCases := []struct {
name string
status v1.PodStatus
expect bool
}{
{
name: "yes",
status: v1.PodStatus{
ContainerStatuses: []v1.ContainerStatus{
{
State: v1.ContainerState{
Waiting: &v1.ContainerStateWaiting{
Reason: "CrashLoopBackOff",
},
},
},
},
},
expect: true,
},
{
name: "no",
status: v1.PodStatus{
ContainerStatuses: []v1.ContainerStatus{
{
State: v1.ContainerState{},
},
},
},
expect: false,
},
{
name: "empty",
status: v1.PodStatus{},
expect: false,
},
}

for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
require.Equal(t, tc.expect, IsPodCrashLoopBackOff(tc.status))
})
}
}
130 changes: 130 additions & 0 deletions pkg/yurtmanager/webhook/endpoints/v1/endpoints_default.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,130 @@
/*
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 v1

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/client"

nodeutil "github.com/openyurtio/openyurt/pkg/yurtmanager/controller/util/node"
podutil "github.com/openyurtio/openyurt/pkg/yurtmanager/controller/util/pod"
)

// Default satisfies the defaulting webhook interface.
func (webhook *EndpointsHandler) Default(ctx context.Context, obj runtime.Object) error {
endpoints, ok := obj.(*corev1.Endpoints)
if !ok {
return fmt.Errorf("expected an Endpoints object but got %T", obj)

Check warning on line 36 in pkg/yurtmanager/webhook/endpoints/v1/endpoints_default.go

View check run for this annotation

Codecov / codecov/patch

pkg/yurtmanager/webhook/endpoints/v1/endpoints_default.go#L36

Added line #L36 was not covered by tests
tnsimon marked this conversation as resolved.
Show resolved Hide resolved
}

return remapAutonomyEndpoints(ctx, webhook.Client, endpoints)
}

// isNodeAutonomous checks if the node has autonomy annotations
// and returns true if it does, false otherwise.
func isNodeAutonomous(ctx context.Context, c client.Client, nodeName string) (bool, error) {
node := &corev1.Node{}
err := c.Get(ctx, client.ObjectKey{Name: nodeName}, node)
if err != nil {
// If node doesn't exist, it doesn't have autonomy
if apierrors.IsNotFound(err) {
return false, nil
}
return false, err

Check warning on line 52 in pkg/yurtmanager/webhook/endpoints/v1/endpoints_default.go

View check run for this annotation

Codecov / codecov/patch

pkg/yurtmanager/webhook/endpoints/v1/endpoints_default.go#L52

Added line #L52 was not covered by tests
}

return nodeutil.IsPodBoundenToNode(node), nil
}

// isPodCrashLoopBackOff checks if the pod is crashloopbackoff
// and returns true if it is, false otherwise.
func isPodCrashLoopBackOff(ctx context.Context, c client.Client, podName, namespace string) (bool, error) {
pod := &corev1.Pod{}
err := c.Get(ctx, client.ObjectKey{Name: podName, Namespace: namespace}, pod)
if err != nil {
if apierrors.IsNotFound(err) {
return false, nil
}
return false, err

Check warning on line 67 in pkg/yurtmanager/webhook/endpoints/v1/endpoints_default.go

View check run for this annotation

Codecov / codecov/patch

pkg/yurtmanager/webhook/endpoints/v1/endpoints_default.go#L67

Added line #L67 was not covered by tests
}

return podutil.IsPodCrashLoopBackOff(pod.Status), nil
}

// remapAutonomyEndpoints remaps the notReadyAddresses to the readyAddresses
// for the subsets scheduled to nodes that have autonomy annotations.
// The function checks the pod status and if the pod is not in crashloopbackoff,
// it remaps the address to readyAddresses.
func remapAutonomyEndpoints(ctx context.Context, client client.Client, endpoints *corev1.Endpoints) error {
// Track nodes with autonomy to avoid repeated checks
nodesWithAutonomy := make(map[string]bool)

// Get all the notReadyAddresses for subsets
for i, s := range endpoints.Subsets {
// Create a zero-length slice with the same underlying array
newNotReadyAddresses := s.NotReadyAddresses[:0]

for _, a := range s.NotReadyAddresses {
if a.NodeName == nil || a.TargetRef == nil {
newNotReadyAddresses = append(newNotReadyAddresses, a)
continue

Check warning on line 89 in pkg/yurtmanager/webhook/endpoints/v1/endpoints_default.go

View check run for this annotation

Codecov / codecov/patch

pkg/yurtmanager/webhook/endpoints/v1/endpoints_default.go#L88-L89

Added lines #L88 - L89 were not covered by tests
}

// Get the node and check autonomy annotations
hasAutonomy, ok := nodesWithAutonomy[*a.NodeName]
if !ok {
isAutonomous, err := isNodeAutonomous(ctx, client, *a.NodeName)
if err != nil {
return err

Check warning on line 97 in pkg/yurtmanager/webhook/endpoints/v1/endpoints_default.go

View check run for this annotation

Codecov / codecov/patch

pkg/yurtmanager/webhook/endpoints/v1/endpoints_default.go#L97

Added line #L97 was not covered by tests
}
// Store autonomy status for future checks
nodesWithAutonomy[*a.NodeName] = isAutonomous
hasAutonomy = isAutonomous
}

// If the node doesn't have autonomy, skip
if !hasAutonomy {
newNotReadyAddresses = append(newNotReadyAddresses, a)
continue
}

// Get the pod
isPodCrashLoopBackOff, err := isPodCrashLoopBackOff(ctx, client, a.TargetRef.Name, a.TargetRef.Namespace)
if err != nil {
return err

Check warning on line 113 in pkg/yurtmanager/webhook/endpoints/v1/endpoints_default.go

View check run for this annotation

Codecov / codecov/patch

pkg/yurtmanager/webhook/endpoints/v1/endpoints_default.go#L113

Added line #L113 was not covered by tests
}

if isPodCrashLoopBackOff {
rambohe-ch marked this conversation as resolved.
Show resolved Hide resolved
newNotReadyAddresses = append(newNotReadyAddresses, a)
continue
}

// Move the address to the ready addresses in the subset
endpoints.Subsets[i].Addresses = append(endpoints.Subsets[i].Addresses, *a.DeepCopy())
}

// Update the subset with the new notReadyAddresses
endpoints.Subsets[i].NotReadyAddresses = newNotReadyAddresses
}

return nil
}
Loading
Loading