/* Copyright 2021 The Karmada 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 helper import ( "context" discoveryv1 "k8s.io/api/discovery/v1" apierrors "k8s.io/apimachinery/pkg/api/errors" "k8s.io/apimachinery/pkg/labels" "k8s.io/apimachinery/pkg/util/errors" "k8s.io/apimachinery/pkg/util/sets" "k8s.io/client-go/util/retry" "k8s.io/klog/v2" "sigs.k8s.io/controller-runtime/pkg/client" "sigs.k8s.io/controller-runtime/pkg/controller/controllerutil" networkingv1alpha1 "github.com/karmada-io/karmada/pkg/apis/networking/v1alpha1" "github.com/karmada-io/karmada/pkg/util" ) // CreateOrUpdateEndpointSlice creates a EndpointSlice object if not exist, or updates if it already exists. func CreateOrUpdateEndpointSlice(client client.Client, endpointSlice *discoveryv1.EndpointSlice) error { runtimeObject := endpointSlice.DeepCopy() var operationResult controllerutil.OperationResult err := retry.RetryOnConflict(retry.DefaultRetry, func() (err error) { operationResult, err = controllerutil.CreateOrUpdate(context.TODO(), client, runtimeObject, func() error { runtimeObject.AddressType = endpointSlice.AddressType runtimeObject.Endpoints = endpointSlice.Endpoints runtimeObject.Labels = endpointSlice.Labels runtimeObject.Ports = endpointSlice.Ports return nil }) if err != nil { return err } return nil }) if err != nil { klog.Errorf("Failed to create/update EndpointSlice %s/%s. Error: %v", endpointSlice.GetNamespace(), endpointSlice.GetName(), err) return err } if operationResult == controllerutil.OperationResultCreated { klog.V(2).Infof("Create EndpointSlice %s/%s successfully.", endpointSlice.GetNamespace(), endpointSlice.GetName()) } else if operationResult == controllerutil.OperationResultUpdated { klog.V(2).Infof("Update EndpointSlice %s/%s successfully.", endpointSlice.GetNamespace(), endpointSlice.GetName()) } else { klog.V(2).Infof("EndpointSlice %s/%s is up to date.", endpointSlice.GetNamespace(), endpointSlice.GetName()) } return nil } // GetEndpointSlices returns a EndpointSliceList by labels func GetEndpointSlices(c client.Client, ls labels.Set) (*discoveryv1.EndpointSliceList, error) { endpointSlices := &discoveryv1.EndpointSliceList{} listOpt := &client.ListOptions{LabelSelector: labels.SelectorFromSet(ls)} return endpointSlices, c.List(context.TODO(), endpointSlices, listOpt) } // DeleteEndpointSlice will delete all EndpointSlice objects by labels. func DeleteEndpointSlice(c client.Client, selector labels.Set) error { endpointSliceList, err := GetEndpointSlices(c, selector) if err != nil { if apierrors.IsNotFound(err) { return nil } klog.Errorf("Failed to get endpointslices by label %v: %v", selector, err) return err } var errs []error for index, work := range endpointSliceList.Items { if err := c.Delete(context.TODO(), &endpointSliceList.Items[index]); err != nil { klog.Errorf("Failed to delete endpointslice(%s/%s): %v", work.Namespace, work.Name, err) errs = append(errs, err) } } return errors.NewAggregate(errs) } func MultiClusterServiceCrossClusterEnabled(mcs *networkingv1alpha1.MultiClusterService) bool { for _, svcType := range mcs.Spec.Types { if svcType == networkingv1alpha1.ExposureTypeCrossCluster { return true } } return false } func GetProvisionClusters(client client.Client, mcs *networkingv1alpha1.MultiClusterService) (sets.Set[string], error) { provisionClusters := sets.New[string](mcs.Spec.ServiceProvisionClusters...) existClusters, err := util.GetClusterSet(client) if err != nil { klog.Errorf("Failed to get cluster set, Error: %v", err) return nil, err } if len(provisionClusters) == 0 { return existClusters, nil } return provisionClusters.Intersection(existClusters), nil } func GetConsumptionClustres(client client.Client, mcs *networkingv1alpha1.MultiClusterService) (sets.Set[string], error) { consumptionClusters := sets.New[string](mcs.Spec.ServiceConsumptionClusters...) existClusters, err := util.GetClusterSet(client) if err != nil { klog.Errorf("Failed to get cluster set, Error: %v", err) return nil, err } if len(consumptionClusters) == 0 { return existClusters, nil } return consumptionClusters.Intersection(existClusters), nil }