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 11, 2024
1 parent 839a52a commit 9c222f4
Show file tree
Hide file tree
Showing 22 changed files with 2,684 additions and 358 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
2 changes: 1 addition & 1 deletion cmd/yurt-manager/app/options/poolservicecontroller.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ package options
import (
"github.com/spf13/pflag"

"github.com/openyurtio/openyurt/pkg/yurtmanager/controller/poolservice/config"
"github.com/openyurtio/openyurt/pkg/yurtmanager/controller/loadbalancerset/loadbalancerset/config"
)

type PoolServiceControllerOptions struct {
Expand Down
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: 1 addition & 1 deletion pkg/yurtmanager/controller/apis/config/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,10 @@ import (

csrapproverconfig "github.com/openyurtio/openyurt/pkg/yurtmanager/controller/csrapprover/config"
daemonpodupdaterconfig "github.com/openyurtio/openyurt/pkg/yurtmanager/controller/daemonpodupdater/config"
poolserviceconfig "github.com/openyurtio/openyurt/pkg/yurtmanager/controller/loadbalancerset/loadbalancerset/config"
nodebucketconfig "github.com/openyurtio/openyurt/pkg/yurtmanager/controller/nodebucket/config"
nodepoolconfig "github.com/openyurtio/openyurt/pkg/yurtmanager/controller/nodepool/config"
platformadminconfig "github.com/openyurtio/openyurt/pkg/yurtmanager/controller/platformadmin/config"
poolserviceconfig "github.com/openyurtio/openyurt/pkg/yurtmanager/controller/poolservice/config"
gatewaypickupconfig "github.com/openyurtio/openyurt/pkg/yurtmanager/controller/raven/gatewaypickup/config"
yurtappdaemonconfig "github.com/openyurtio/openyurt/pkg/yurtmanager/controller/yurtappdaemon/config"
yurtappoverriderconfig "github.com/openyurtio/openyurt/pkg/yurtmanager/controller/yurtappoverrider/config"
Expand Down
2 changes: 2 additions & 0 deletions pkg/yurtmanager/controller/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ import (
"github.com/openyurtio/openyurt/cmd/yurt-manager/names"
"github.com/openyurtio/openyurt/pkg/yurtmanager/controller/csrapprover"
"github.com/openyurtio/openyurt/pkg/yurtmanager/controller/daemonpodupdater"
"github.com/openyurtio/openyurt/pkg/yurtmanager/controller/loadbalancerset/loadbalancerset"
"github.com/openyurtio/openyurt/pkg/yurtmanager/controller/nodebucket"
"github.com/openyurtio/openyurt/pkg/yurtmanager/controller/nodelifecycle"
"github.com/openyurtio/openyurt/pkg/yurtmanager/controller/nodepool"
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, loadbalancerset.Add)

return controllers
}
Expand Down
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 loadbalancerset

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)
}

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
}

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
}
if desiredAnnotations == nil {
return nil, currentAnnotations
}

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
}
if annotations == nil {
annotations = make(map[string]string)
}
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 9c222f4

Please sign in to comment.