-
Notifications
You must be signed in to change notification settings - Fork 75
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add pod owner attributes for RDMA metrics
Signed-off-by: lou-lan <loulan@loulan.me>
- Loading branch information
Showing
9 changed files
with
407 additions
and
29 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
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,171 @@ | ||
// Copyright 2024 Authors of spidernet-io | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
package podownercache | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"github.com/spidernet-io/spiderpool/pkg/lock" | ||
"github.com/spidernet-io/spiderpool/pkg/logutils" | ||
"go.uber.org/zap" | ||
corev1 "k8s.io/api/core/v1" | ||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured" | ||
"k8s.io/apimachinery/pkg/types" | ||
"k8s.io/client-go/kubernetes" | ||
"k8s.io/client-go/tools/cache" | ||
"sigs.k8s.io/controller-runtime/pkg/client" | ||
) | ||
|
||
type PodOwnerCache struct { | ||
ctx context.Context | ||
k8ClientSet *kubernetes.Clientset | ||
Check failure on line 23 in pkg/podownercache/pod_owner_cache.go GitHub Actions / lint-golang
|
||
apiReader client.Reader | ||
|
||
cacheLock lock.RWMutex | ||
pods map[types.NamespacedName]Pod | ||
ipToPod map[string]types.NamespacedName | ||
} | ||
|
||
type Pod struct { | ||
types.NamespacedName | ||
OwnerInfo OwnerInfo | ||
IPs []string | ||
} | ||
|
||
type OwnerInfo struct { | ||
APIVersion string | ||
Kind string | ||
Namespace string | ||
Name string | ||
} | ||
|
||
type CacheInterface interface { | ||
GetPodByIP(ip string) *Pod | ||
} | ||
|
||
var logger *zap.Logger | ||
|
||
func New(ctx context.Context, podInformer cache.SharedIndexInformer, apiReader client.Reader) (CacheInterface, error) { | ||
logger = logutils.Logger.Named("PodOwnerCache") | ||
logger.Info("create PodOwnerCache informer") | ||
|
||
res := &PodOwnerCache{ | ||
ctx: ctx, | ||
apiReader: apiReader, | ||
cacheLock: lock.RWMutex{}, | ||
pods: make(map[types.NamespacedName]Pod), | ||
ipToPod: make(map[string]types.NamespacedName), | ||
} | ||
|
||
_, err := podInformer.AddEventHandler(cache.ResourceEventHandlerFuncs{ | ||
AddFunc: res.onPodAdd, | ||
UpdateFunc: res.onPodUpdate, | ||
DeleteFunc: res.onPodDel, | ||
}) | ||
if nil != err { | ||
logger.Error(err.Error()) | ||
return nil, err | ||
} | ||
|
||
return res, nil | ||
} | ||
|
||
func (s *PodOwnerCache) onPodAdd(obj interface{}) { | ||
if pod, ok := obj.(*corev1.Pod); ok { | ||
if len(pod.Status.PodIPs) > 0 { | ||
ips := make([]string, 0, len(pod.Status.PodIPs)) | ||
for _, p := range pod.Status.PodIPs { | ||
ips = append(ips, p.IP) | ||
} | ||
owner, err := s.getFinalOwner(pod) | ||
if err != nil { | ||
logger.Warn("", zap.Error(err)) | ||
return | ||
} | ||
s.cacheLock.Lock() | ||
defer s.cacheLock.Unlock() | ||
key := types.NamespacedName{Namespace: pod.Namespace, Name: pod.Name} | ||
s.pods[key] = Pod{ | ||
NamespacedName: key, | ||
OwnerInfo: *owner, | ||
IPs: ips, | ||
} | ||
for _, ip := range ips { | ||
s.ipToPod[ip] = key | ||
} | ||
} | ||
} | ||
} | ||
|
||
func (s *PodOwnerCache) onPodUpdate(oldObj, newObj interface{}) { | ||
s.onPodAdd(newObj) | ||
} | ||
|
||
func (s *PodOwnerCache) onPodDel(obj interface{}) { | ||
if pod, ok := obj.(*corev1.Pod); ok { | ||
s.cacheLock.Lock() | ||
defer s.cacheLock.Unlock() | ||
|
||
key := types.NamespacedName{Namespace: pod.Namespace, Name: pod.Name} | ||
if _, ok := s.pods[key]; !ok { | ||
return | ||
} | ||
for _, ip := range s.pods[key].IPs { | ||
delete(s.ipToPod, ip) | ||
} | ||
delete(s.pods, key) | ||
} | ||
} | ||
|
||
func (s *PodOwnerCache) getFinalOwner(obj metav1.Object) (*OwnerInfo, error) { | ||
var finalOwner *OwnerInfo | ||
|
||
for { | ||
ownerRefs := obj.GetOwnerReferences() | ||
if len(ownerRefs) == 0 { | ||
break | ||
} | ||
|
||
ownerRef := ownerRefs[0] // Assuming the first owner reference | ||
finalOwner = &OwnerInfo{ | ||
APIVersion: ownerRef.APIVersion, | ||
Kind: ownerRef.Kind, | ||
Namespace: obj.GetNamespace(), | ||
Name: ownerRef.Name, | ||
} | ||
|
||
// Prepare an empty object of the owner kind | ||
ownerObj := &unstructured.Unstructured{} | ||
ownerObj.SetAPIVersion(ownerRef.APIVersion) | ||
ownerObj.SetKind(ownerRef.Kind) | ||
|
||
err := s.apiReader.Get(s.ctx, client.ObjectKey{ | ||
Namespace: obj.GetNamespace(), | ||
Name: ownerRef.Name, | ||
}, ownerObj) | ||
if err != nil { | ||
return nil, fmt.Errorf("error fetching owner: %v", err) | ||
} | ||
|
||
// Set obj to the current owner to continue the loop | ||
obj = ownerObj | ||
} | ||
|
||
return finalOwner, nil | ||
} | ||
|
||
func (s *PodOwnerCache) GetPodByIP(ip string) *Pod { | ||
s.cacheLock.RLock() | ||
defer s.cacheLock.RUnlock() | ||
item, exists := s.ipToPod[ip] | ||
if !exists { | ||
return nil | ||
} | ||
pod, exists := s.pods[item] | ||
if !exists { | ||
return nil | ||
} | ||
return &pod | ||
} |
Oops, something went wrong.