Skip to content

Commit

Permalink
fix bug with getting pg from pod
Browse files Browse the repository at this point in the history
Signed-off-by: lucming <[email protected]>
  • Loading branch information
lucming committed Jan 24, 2022
1 parent 169d1aa commit 3a20d12
Showing 1 changed file with 58 additions and 6 deletions.
64 changes: 58 additions & 6 deletions pkg/apis/helpers/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,17 @@ package helpers
import (
"context"
"fmt"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/runtime/schema"
"k8s.io/client-go/dynamic"
"k8s.io/client-go/restmapper"
"net"
"net/http"
"os"
"os/signal"
"reflect"
"strings"
"syscall"
"time"

Expand Down Expand Up @@ -163,23 +169,69 @@ func DeleteSecret(job *vcbatch.Job, kubeClients kubernetes.Interface, secretName
}

// GeneratePodgroupName generate podgroup name of normal pod.
func GeneratePodgroupName(pod *v1.Pod) string {
func GeneratePodgroupName(dyClient dynamic.Interface, kubeClient kubernetes.Interface, pod *v1.Pod) string {
unstructuredPod, err := runtime.DefaultUnstructuredConverter.ToUnstructured(pod)
if err != nil {
klog.Errorf("failed to convert pod to unstructed.error=%v", err)
return ""
}
owners := make([]metav1.OwnerReference, 0)
GetOutestOwner(dyClient, kubeClient, &unstructured.Unstructured{unstructuredPod}, &owners)
pgName := vcbatch.PodgroupNamePrefix

if len(pod.OwnerReferences) != 0 {
for _, ownerReference := range pod.OwnerReferences {
if ownerReference.Controller != nil && *ownerReference.Controller {
pgName += string(ownerReference.UID)
for _, owner := range owners {
if owner.Controller != nil && *owner.Controller {
pgName += string(owner.UID)
return pgName
}
}
}

pgName += string(pod.UID)

return pgName
}

func GetOutestOwner(dyClient dynamic.Interface, kubeClient kubernetes.Interface, object *unstructured.Unstructured, result *[]metav1.OwnerReference) {
owners := object.GetOwnerReferences()
if len(owners) == 0 {
ref := metav1.NewControllerRef(object, object.GroupVersionKind())
*result = append(*result, *ref)
}

for _, owner := range owners {
if owner.Controller == nil {
continue
}
apiversionList := strings.Split(owner.APIVersion, "/")
var group, version string
if len(apiversionList) == 2 {
group, version = apiversionList[0], apiversionList[1]
} else if len(apiversionList) == 1 {
version = apiversionList[0]
}
gk := schema.GroupKind{
Group: group,
Kind: owner.Kind,
}

groupResources, err := restmapper.GetAPIGroupResources(kubeClient.Discovery())
if err != nil {
klog.Errorf("failed to get resouces(%s:%s)in ns(%s),error=%v", gk.String(), owner.Name, object.GetNamespace())
return
}
rm := restmapper.NewDiscoveryRESTMapper(groupResources)
mapping, err := rm.RESTMapping(gk, version)
parentObj, err := dyClient.Resource(mapping.Resource).
Namespace(object.GetNamespace()).
Get(context.Background(), owner.Name, metav1.GetOptions{})
if err != nil {
klog.Errorf("failed to get resouces(%s:%s)in ns(%s),error=%v", gk.String(), owner.Name, object.GetNamespace())
return
}
GetOutestOwner(dyClient, kubeClient, parentObj, result)
}
}

// StartHealthz register healthz interface.
func StartHealthz(healthzBindAddress, name string) error {
listener, err := net.Listen("tcp", healthzBindAddress)
Expand Down

0 comments on commit 3a20d12

Please sign in to comment.