Skip to content

Commit

Permalink
aggregated annotations and labels. (#2027)
Browse files Browse the repository at this point in the history
  • Loading branch information
zyjhtangtang authored Apr 22, 2024
1 parent ee215e8 commit 1310535
Show file tree
Hide file tree
Showing 9 changed files with 250 additions and 224 deletions.
7 changes: 3 additions & 4 deletions pkg/apis/network/well_known_labels_annotations.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,7 @@ limitations under the License.
package network

const (
LabelServiceName = "openyurt.io/service-name"
LabelNodePoolName = "openyurt.io/pool-name"
AnnotationNodePoolSelector = "service.openyurt.io/nodepool-labelselector"
AggregateAnnotationsKeyPrefix = "service.openyurt.io"
LabelServiceName = "openyurt.io/service-name"
LabelNodePoolName = "openyurt.io/pool-name"
AnnotationNodePoolSelector = "service.openyurt.io/nodepool-labelselector"
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
/*
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 loadbalancerset

import (
"reflect"

corev1 "k8s.io/api/core/v1"

netv1alpha1 "github.com/openyurtio/openyurt/pkg/apis/network/v1alpha1"
)

func aggregatePoolServicesAnnotations(poolServices []netv1alpha1.PoolService) map[string]string {
var annotationsList []map[string]string
for _, ps := range poolServices {
annotationsList = append(annotationsList, ps.Status.AggregateToAnnotations)
}

return aggregatedMaps(annotationsList...)
}

func compareAndUpdateServiceAnnotations(svc *corev1.Service, aggregatedAnnotations map[string]string) bool {
currentAggregatedAnnotations := filterIgnoredKeys(svc.Annotations)

if reflect.DeepEqual(currentAggregatedAnnotations, aggregatedAnnotations) {
return false
}

svc.Annotations = mergeAndPurgeMap(svc.Annotations, aggregatedAnnotations)

return true
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
/*
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 loadbalancerset

import (
"reflect"

v1 "k8s.io/api/core/v1"

"github.com/openyurtio/openyurt/pkg/apis/network/v1alpha1"
)

func aggregatePoolServicesLabels(poolServices []v1alpha1.PoolService) map[string]string {
var labelList []map[string]string
for _, ps := range poolServices {
labelList = append(labelList, ps.Status.AggregateToLabels)
}

return aggregatedMaps(labelList...)
}

func compareAndUpdateServiceLabels(svc *v1.Service, aggregatedLabels map[string]string) bool {
currentAggregatedLabels := filterIgnoredKeys(svc.Labels)

if reflect.DeepEqual(currentAggregatedLabels, aggregatedLabels) {
return false
}

svc.Labels = mergeAndPurgeMap(svc.Labels, aggregatedLabels)

return true
}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -489,17 +489,19 @@ func (r *ReconcileLoadBalancerSet) syncService(svc *corev1.Service) error {
return errors.Wrapf(err, "failed to get current pool services for service %s/%s", svc.Namespace, svc.Name)
}

aggregatedLabels := aggregatePoolServicesLabels(poolServices)
aggregatedAnnotations := aggregatePoolServicesAnnotations(poolServices)
aggregatedLbStatus := aggregateLbStatus(poolServices)

return r.compareAndUpdateService(svc, aggregatedAnnotations, aggregatedLbStatus)
return r.compareAndUpdateService(svc, aggregatedLabels, aggregatedAnnotations, aggregatedLbStatus)
}

func (r *ReconcileLoadBalancerSet) compareAndUpdateService(svc *corev1.Service, annotations map[string]string, lbStatus corev1.LoadBalancerStatus) error {
func (r *ReconcileLoadBalancerSet) compareAndUpdateService(svc *corev1.Service, labels, annotations map[string]string, lbStatus corev1.LoadBalancerStatus) error {
isUpdatedAnnotations := compareAndUpdateServiceAnnotations(svc, annotations)
isUpdatedLbStatus := compareAndUpdateServiceLbStatus(svc, lbStatus)
isUpdatedLabels := compareAndUpdateServiceLabels(svc, labels)

if !isUpdatedLbStatus && !isUpdatedAnnotations {
if !isUpdatedLbStatus && !isUpdatedAnnotations && !isUpdatedLabels {
return nil
}

Expand Down
Loading

0 comments on commit 1310535

Please sign in to comment.