-
Notifications
You must be signed in to change notification settings - Fork 404
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
make servicetopology filter in yurthub work properly when service or …
…nodepool change
- Loading branch information
Showing
13 changed files
with
1,364 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
/* | ||
Copyright 2022 The OpenYurt Authors. | ||
Copyright 2017 The Kubernetes 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 adapter | ||
|
||
import ( | ||
"fmt" | ||
"time" | ||
|
||
corev1 "k8s.io/api/core/v1" | ||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
"k8s.io/apimachinery/pkg/labels" | ||
"k8s.io/apimachinery/pkg/util/runtime" | ||
"k8s.io/apimachinery/pkg/util/sets" | ||
"k8s.io/client-go/tools/cache" | ||
|
||
"github.com/openyurtio/openyurt/pkg/yurthub/filter/servicetopology" | ||
) | ||
|
||
type Adapter interface { | ||
GetEnqueueKeysBySvc(svc *corev1.Service) []string | ||
GetEnqueueKeysByNodePool(svcTopologyTypes map[string]string, allNpNodes sets.String) []string | ||
UpdateTriggerAnnotations(namespace, name string) error | ||
} | ||
|
||
func getSvcSelector(key, value string) labels.Selector { | ||
return labels.SelectorFromSet( | ||
map[string]string{ | ||
key: value, | ||
}, | ||
) | ||
} | ||
|
||
func appendKeys(keys []string, obj interface{}) []string { | ||
key, err := cache.MetaNamespaceKeyFunc(obj) | ||
if err != nil { | ||
runtime.HandleError(err) | ||
return nil | ||
} | ||
keys = append(keys, key) | ||
return keys | ||
} | ||
|
||
func isNodePoolTypeSvc(namespace, name string, svcTopologyTypes map[string]string) bool { | ||
svc := &corev1.Service{ | ||
ObjectMeta: metav1.ObjectMeta{ | ||
Namespace: namespace, | ||
Name: name, | ||
}, | ||
} | ||
key, err := cache.MetaNamespaceKeyFunc(svc) | ||
if err != nil { | ||
runtime.HandleError(err) | ||
return false | ||
} | ||
|
||
return isNodePoolTypeTopology(svcTopologyTypes[key]) | ||
} | ||
|
||
// TODO: if service topology need to support multi types, | ||
// like openyurt.io/topologyKeys: "kubernetes.io/hostname, openyurt.io/nodepool, *". | ||
// For simplicity,as long as the value of service topology annotation contains nodepool type, | ||
// then this topology is recognized as nodepool type | ||
func isNodePoolTypeTopology(topologyType string) bool { | ||
if topologyType == servicetopology.AnnotationServiceTopologyValueNodePool { | ||
return true | ||
} | ||
if topologyType == servicetopology.AnnotationServiceTopologyValueZone { | ||
return true | ||
} | ||
return false | ||
} | ||
|
||
func getUpdateTriggerPatch() []byte { | ||
patch := fmt.Sprintf(`{"metadata":{"annotations": {"openyurt.io/update-trigger": "%d"}}}`, time.Now().Unix()) | ||
return []byte(patch) | ||
} |
86 changes: 86 additions & 0 deletions
86
pkg/controller/servicetopology/adapter/endpoints_adapter.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
/* | ||
Copyright 2022 The OpenYurt Authors. | ||
Copyright 2017 The Kubernetes 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 adapter | ||
|
||
import ( | ||
"context" | ||
|
||
corev1 "k8s.io/api/core/v1" | ||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
"k8s.io/apimachinery/pkg/labels" | ||
"k8s.io/apimachinery/pkg/types" | ||
"k8s.io/apimachinery/pkg/util/sets" | ||
"k8s.io/client-go/kubernetes" | ||
corelisters "k8s.io/client-go/listers/core/v1" | ||
"k8s.io/klog/v2" | ||
) | ||
|
||
func NewEndpointsAdapter(client kubernetes.Interface, epLister corelisters.EndpointsLister) Adapter { | ||
return &endpoints{ | ||
client: client, | ||
epLister: epLister, | ||
} | ||
} | ||
|
||
type endpoints struct { | ||
client kubernetes.Interface | ||
epLister corelisters.EndpointsLister | ||
} | ||
|
||
func (s *endpoints) GetEnqueueKeysBySvc(svc *corev1.Service) []string { | ||
var keys []string | ||
return appendKeys(keys, svc) | ||
} | ||
|
||
func (s *endpoints) GetEnqueueKeysByNodePool(svcTopologyTypes map[string]string, allNpNodes sets.String) []string { | ||
var keys []string | ||
endpointsList, err := s.epLister.List(labels.Everything()) | ||
if err != nil { | ||
klog.V(4).Infof("Error listing endpoints sets: %v", err) | ||
return keys | ||
} | ||
|
||
for _, ep := range endpointsList { | ||
if !isNodePoolTypeSvc(ep.Namespace, ep.Name, svcTopologyTypes) { | ||
continue | ||
} | ||
|
||
if s.getNodesInEp(ep).Intersection(allNpNodes).Len() == 0 { | ||
continue | ||
} | ||
keys = appendKeys(keys, ep) | ||
} | ||
return keys | ||
} | ||
|
||
func (s *endpoints) getNodesInEp(ep *corev1.Endpoints) sets.String { | ||
nodes := sets.NewString() | ||
for _, subset := range ep.Subsets { | ||
for _, addr := range subset.Addresses { | ||
if addr.NodeName != nil { | ||
nodes.Insert(*addr.NodeName) | ||
} | ||
} | ||
} | ||
return nodes | ||
} | ||
|
||
func (s *endpoints) UpdateTriggerAnnotations(namespace, name string) error { | ||
patch := getUpdateTriggerPatch() | ||
_, err := s.client.CoreV1().Endpoints(namespace).Patch(context.Background(), name, types.StrategicMergePatchType, patch, metav1.PatchOptions{}) | ||
return err | ||
} |
Oops, something went wrong.