Skip to content

Commit

Permalink
aggregated annotations and labels.
Browse files Browse the repository at this point in the history
  • Loading branch information
zyjhtangtang committed Apr 18, 2024
1 parent 08ab7e4 commit ca9c52b
Show file tree
Hide file tree
Showing 9 changed files with 308 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,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 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 {
aggregatedAnnotations := make(map[string]string)
for _, ps := range poolServices {
aggregatedAnnotations = mergeMap(aggregatedAnnotations, ps.Status.AggregateToAnnotations)
}

return addAggregatePrefix(aggregatedAnnotations)
}

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

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

update, deletion := diffMap(currentAggregatedServiceAnnotations, aggregatedAnnotations)
svc.Annotations = updateMap(svc.Annotations, update, deletion)

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 {
aggregatedLabels := make(map[string]string)
for _, ps := range poolServices {
aggregatedLabels = mergeMap(aggregatedLabels, ps.Status.AggregateToLabels)
}
return addAggregatePrefix(aggregatedLabels)
}

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

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

update, deletion := diffMap(currentAggregatedLabels, labels)
svc.Labels = updateMap(svc.Labels, update, deletion)

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 ca9c52b

Please sign in to comment.