Skip to content

Commit

Permalink
add pool service controller.
Browse files Browse the repository at this point in the history
  • Loading branch information
zyjhtangtang committed Apr 9, 2024
1 parent c137221 commit cd9ed01
Show file tree
Hide file tree
Showing 18 changed files with 2,574 additions and 248 deletions.
46 changes: 2 additions & 44 deletions charts/yurt-manager/templates/yurt-manager-auto-generated.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -500,7 +500,7 @@ rules:
- patch
- update
- apiGroups:
- net.openyurt.io
- network.openyurt.io
resources:
- poolservices
verbs:
Expand All @@ -512,7 +512,7 @@ rules:
- update
- watch
- apiGroups:
- net.openyurt.io
- network.openyurt.io
resources:
- poolservices/status
verbs:
Expand Down Expand Up @@ -756,27 +756,6 @@ webhooks:
resources:
- yurtstaticsets
sideEffects: None
- admissionReviewVersions:
- v1
- v1beta1
clientConfig:
service:
name: yurt-manager-webhook-service
namespace: {{ .Release.Namespace }}
path: /mutate-net-openyurt-io-poolservice
failurePolicy: Fail
name: mutate.net.v1alpha1.poolservice.openyurt.io
rules:
- apiGroups:
- net.openyurt.io
apiVersions:
- v1alpha1
operations:
- CREATE
- UPDATE
resources:
- poolservices
sideEffects: None
---
apiVersion: admissionregistration.k8s.io/v1
kind: ValidatingWebhookConfiguration
Expand Down Expand Up @@ -951,24 +930,3 @@ webhooks:
resources:
- yurtstaticsets
sideEffects: None
- admissionReviewVersions:
- v1
- v1beta1
clientConfig:
service:
name: yurt-manager-webhook-service
namespace: {{ .Release.Namespace }}
path: /validate-net-openyurt-io-poolservice
failurePolicy: Fail
name: validate.net.v1alpha1.poolservice.openyurt.io
rules:
- apiGroups:
- net.openyurt.io
apiVersions:
- v1alpha1
operations:
- CREATE
- UPDATE
resources:
- poolservices
sideEffects: None
1 change: 1 addition & 0 deletions cmd/yurt-manager/names/controller_names.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,5 +61,6 @@ func YurtManagerControllerAliases() map[string]string {
"gatewaydns": GatewayDNSController,
"nodelifecycle": NodeLifeCycleController,
"nodebucket": NodeBucketController,
"poolservice": PoolServiceController,
}
}
4 changes: 2 additions & 2 deletions pkg/apis/network/v1alpha1/groupversion_info.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,9 @@ limitations under the License.

package v1alpha1

// Package v1alpha1 contains API Schema definitions for the net v1alpha1API group
// Package v1alpha1 contains API Schema definitions for the network v1alpha1API group
// +kubebuilder:object:generate=true
// +groupName=net.openyurt.io
// +groupName=network.openyurt.io

import (
"k8s.io/apimachinery/pkg/runtime/schema"
Expand Down
2 changes: 1 addition & 1 deletion pkg/apis/network/v1alpha1/poolservice_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ type PoolServiceSpec struct {
// PoolServiceStatus defines the observed state of PoolService
type PoolServiceStatus struct {
// LoadBalancer contains the current status of the load-balancer in the current nodepool
LoadBalancer *v1.LoadBalancerStatus `json:"loadBalancer,omitempty"`
LoadBalancer v1.LoadBalancerStatus `json:"loadBalancer,omitempty"`

// Current poolService state
Conditions []metav1.Condition `json:"conditions,omitempty"`
Expand Down
13 changes: 4 additions & 9 deletions pkg/apis/network/v1alpha1/zz_generated.deepcopy.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions pkg/yurtmanager/controller/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ import (
"github.com/openyurtio/openyurt/pkg/yurtmanager/controller/nodelifecycle"
"github.com/openyurtio/openyurt/pkg/yurtmanager/controller/nodepool"
"github.com/openyurtio/openyurt/pkg/yurtmanager/controller/platformadmin"
"github.com/openyurtio/openyurt/pkg/yurtmanager/controller/poolservice"
"github.com/openyurtio/openyurt/pkg/yurtmanager/controller/raven/dns"
"github.com/openyurtio/openyurt/pkg/yurtmanager/controller/raven/gatewayinternalservice"
"github.com/openyurtio/openyurt/pkg/yurtmanager/controller/raven/gatewaypickup"
Expand Down Expand Up @@ -97,6 +98,7 @@ func NewControllerInitializers() map[string]InitFunc {
register(names.GatewayPublicServiceController, gatewaypublicservice.Add)
register(names.NodeLifeCycleController, nodelifecycle.Add)
register(names.NodeBucketController, nodebucket.Add)
register(names.PoolServiceController, poolservice.Add)

return controllers
}
Expand Down
167 changes: 167 additions & 0 deletions pkg/yurtmanager/controller/poolservice/annotations.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,167 @@
/*
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 poolservice

import (
"reflect"
"sort"
"strings"

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

"github.com/openyurtio/openyurt/pkg/apis/network"
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 = mergeAnnotations(aggregatedAnnotations, filterIgnoredKeys(ps.Annotations))
}

return aggregatedAnnotations
}

func filterIgnoredKeys(annotations map[string]string) map[string]string {
newAnnotations := make(map[string]string)
for key, value := range annotations {
if key == network.AnnotationNodePoolSelector {
continue
}
if !strings.HasPrefix(key, network.AggregateAnnotationsKeyPrefix) {
continue
}
newAnnotations[key] = value
}
return newAnnotations
}

func mergeAnnotations(m map[string]string, elem map[string]string) map[string]string {
if len(elem) == 0 {
return m
}

if m == nil {
m = make(map[string]string)

Check warning on line 59 in pkg/yurtmanager/controller/poolservice/annotations.go

View check run for this annotation

Codecov / codecov/patch

pkg/yurtmanager/controller/poolservice/annotations.go#L59

Added line #L59 was not covered by tests
}

for k, v := range elem {
m[k] = mergeAnnotationValue(m[k], v)
}

return m
}

func mergeAnnotationValue(originalValue, addValue string) string {
if len(originalValue) == 0 {
return addValue
}

if len(addValue) == 0 {
return originalValue

Check warning on line 75 in pkg/yurtmanager/controller/poolservice/annotations.go

View check run for this annotation

Codecov / codecov/patch

pkg/yurtmanager/controller/poolservice/annotations.go#L75

Added line #L75 was not covered by tests
}

splitOriginalValues := strings.Split(originalValue, ",")
if valueIsExist(splitOriginalValues, addValue) {
return originalValue
}

return joinNewValue(splitOriginalValues, addValue)
}

func valueIsExist(originalValueList []string, addValue string) bool {
for _, oldValue := range originalValueList {
if addValue == oldValue {
return true
}
}
return false
}

func joinNewValue(originalValueList []string, addValue string) string {
originalValueList = append(originalValueList, addValue)
sort.Strings(originalValueList)

return strings.Join(originalValueList, ",")
}

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

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

update, deletion := diffAnnotations(currentAggregatedServiceAnnotations, aggregatedAnnotations)
updateAnnotations(svc.Annotations, update, deletion)

return true
}

func diffAnnotations(currentAnnotations, desiredAnnotations map[string]string) (update map[string]string, deletion map[string]string) {
if currentAnnotations == nil {
return desiredAnnotations, nil

Check warning on line 117 in pkg/yurtmanager/controller/poolservice/annotations.go

View check run for this annotation

Codecov / codecov/patch

pkg/yurtmanager/controller/poolservice/annotations.go#L117

Added line #L117 was not covered by tests
}
if desiredAnnotations == nil {
return nil, currentAnnotations

Check warning on line 120 in pkg/yurtmanager/controller/poolservice/annotations.go

View check run for this annotation

Codecov / codecov/patch

pkg/yurtmanager/controller/poolservice/annotations.go#L120

Added line #L120 was not covered by tests
}

update = make(map[string]string)
for key, value := range desiredAnnotations {
if currentAnnotations[key] != value {
update[key] = value
}
}

deletion = make(map[string]string)
for key, value := range currentAnnotations {
if _, exist := desiredAnnotations[key]; !exist {
deletion[key] = value
}
}
return
}

func updateAnnotations(annotations, update, deletion map[string]string) {
if len(update) == 0 && len(deletion) == 0 {
return

Check warning on line 141 in pkg/yurtmanager/controller/poolservice/annotations.go

View check run for this annotation

Codecov / codecov/patch

pkg/yurtmanager/controller/poolservice/annotations.go#L141

Added line #L141 was not covered by tests
}
if annotations == nil {
annotations = make(map[string]string)

Check warning on line 144 in pkg/yurtmanager/controller/poolservice/annotations.go

View check run for this annotation

Codecov / codecov/patch

pkg/yurtmanager/controller/poolservice/annotations.go#L144

Added line #L144 was not covered by tests
}
for key, value := range update {
annotations[key] = value
}

for key := range deletion {
delete(annotations, key)
}
}

func annotationValueIsEqual(oldAnnotations, newAnnotations map[string]string, key string) bool {
var oldValue string
if oldAnnotations != nil {
oldValue = oldAnnotations[key]
}

var newValue string
if newAnnotations != nil {
newValue = newAnnotations[key]
}

return oldValue == newValue
}
Loading

0 comments on commit cd9ed01

Please sign in to comment.