Skip to content

Commit

Permalink
feat: support service webhook to add service topology annotation for …
Browse files Browse the repository at this point in the history
…vip-loadbalance

Signed-off-by: wangxye <[email protected]>
  • Loading branch information
wangxye committed May 21, 2024
1 parent 51b2104 commit 38eba42
Show file tree
Hide file tree
Showing 6 changed files with 276 additions and 227 deletions.

This file was deleted.

This file was deleted.

4 changes: 2 additions & 2 deletions pkg/yurtmanager/webhook/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ import (
v1alpha1platformadmin "github.com/openyurtio/openyurt/pkg/yurtmanager/webhook/platformadmin/v1alpha1"
v1alpha2platformadmin "github.com/openyurtio/openyurt/pkg/yurtmanager/webhook/platformadmin/v1alpha2"
v1alpha1pod "github.com/openyurtio/openyurt/pkg/yurtmanager/webhook/pod/v1alpha1"
v1alpha1poolservice "github.com/openyurtio/openyurt/pkg/yurtmanager/webhook/poolservice/v1alpha1"
corev1service "github.com/openyurtio/openyurt/pkg/yurtmanager/webhook/service/corev1"
"github.com/openyurtio/openyurt/pkg/yurtmanager/webhook/util"
webhookcontroller "github.com/openyurtio/openyurt/pkg/yurtmanager/webhook/util/controller"
v1alpha1yurtappdaemon "github.com/openyurtio/openyurt/pkg/yurtmanager/webhook/yurtappdaemon/v1alpha1"
Expand Down Expand Up @@ -81,10 +81,10 @@ func init() {
addControllerWebhook(names.PlatformAdminController, &v1alpha2platformadmin.PlatformAdminHandler{})
addControllerWebhook(names.YurtAppOverriderController, &v1alpha1yurtappoverrider.YurtAppOverriderHandler{})
addControllerWebhook(names.YurtAppOverriderController, &v1alpha1deploymentrender.DeploymentRenderHandler{})
addControllerWebhook(names.VipLoadBalancerController, &v1alpha1poolservice.PoolServiceHandler{})

independentWebhooks[v1node.WebhookName] = &v1node.NodeHandler{}
independentWebhooks[v1alpha1pod.WebhookName] = &v1alpha1pod.PodHandler{}
independentWebhooks[corev1service.WebhookName] = &corev1service.ServiceHandler{}
}

// Note !!! @kadisi
Expand Down
47 changes: 47 additions & 0 deletions pkg/yurtmanager/webhook/service/corev1/service_default.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
/*
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 corev1

import (
"context"
"fmt"

corev1 "k8s.io/api/core/v1"
apierrors "k8s.io/apimachinery/pkg/api/errors"
"k8s.io/apimachinery/pkg/runtime"

viplb "github.com/openyurtio/openyurt/pkg/yurtmanager/controller/loadbalancerset/viploadbalancer"
)

// Default satisfies the defaulting webhook interface.
func (webhook *ServiceHandler) Default(ctx context.Context, obj runtime.Object) error {
svc, ok := obj.(*corev1.Service)
if !ok {
return apierrors.NewBadRequest(fmt.Sprintf("expected a Service but got a %T", obj))

Check warning on line 31 in pkg/yurtmanager/webhook/service/corev1/service_default.go

View check run for this annotation

Codecov / codecov/patch

pkg/yurtmanager/webhook/service/corev1/service_default.go#L31

Added line #L31 was not covered by tests
}

if svc.Spec.Type == corev1.ServiceTypeLoadBalancer {
if svc.Spec.LoadBalancerClass != nil && *svc.Spec.LoadBalancerClass == viplb.VipLoadBalancerClass {
if svc.Annotations == nil {
svc.Annotations = make(map[string]string)
}

if _, ok := svc.Annotations[viplb.AnnotationServiceTopologyKey]; !ok {
svc.Annotations[viplb.AnnotationServiceTopologyKey] = viplb.AnnotationServiceTopologyValueNodePool
}
}
}

return nil
}
Loading

0 comments on commit 38eba42

Please sign in to comment.