From 6cd028a0e4469d9f56dc3785d0ffc77a340c16a5 Mon Sep 17 00:00:00 2001 From: yowenter Date: Wed, 26 Apr 2023 20:14:28 +0800 Subject: [PATCH] Pod name using generated name Signed-off-by: yowenter --- pkg/controller.v1/common/pod.go | 2 +- pkg/controller.v1/common/service.go | 1 + pkg/controller.v1/common/util.go | 5 +++++ pkg/controller.v1/control/pod_control.go | 5 ++++- pkg/controller.v1/control/utils.go | 1 + 5 files changed, 12 insertions(+), 2 deletions(-) diff --git a/pkg/controller.v1/common/pod.go b/pkg/controller.v1/common/pod.go index 6c7816a9..7557c962 100644 --- a/pkg/controller.v1/common/pod.go +++ b/pkg/controller.v1/common/pod.go @@ -396,7 +396,7 @@ func (jc *JobController) createNewPod(job interface{}, rt string, index int, spe idxStr := strconv.Itoa(index) // Set name for the template. - podTemplate.Name = GenGeneralName(metaObject.GetName(), rt, idxStr) + podTemplate.GenerateName = GenGeneralNamePrefix(metaObject.GetName(), rt, idxStr) if podTemplate.Labels == nil { podTemplate.Labels = make(map[string]string) diff --git a/pkg/controller.v1/common/service.go b/pkg/controller.v1/common/service.go index 6601cb43..c1a8d72d 100644 --- a/pkg/controller.v1/common/service.go +++ b/pkg/controller.v1/common/service.go @@ -239,6 +239,7 @@ func (jc *JobController) CreateNewService(job metav1.Object, rtype apiv1.Replica service.Spec.Ports = append(service.Spec.Ports, svcPort) } + //service name can't use the generated name, tensorflow using svc name for discovery. service.Name = GenGeneralName(job.GetName(), rt, index) service.Labels = labels // Create OwnerReference. diff --git a/pkg/controller.v1/common/util.go b/pkg/controller.v1/common/util.go index 9e084925..2073b58d 100644 --- a/pkg/controller.v1/common/util.go +++ b/pkg/controller.v1/common/util.go @@ -47,6 +47,11 @@ func (p ReplicasPriority) Swap(i, j int) { p[i], p[j] = p[j], p[i] } +func GenGeneralNamePrefix(jobName string, rtype string, index string) string { + n := jobName + "-" + strings.ToLower(rtype) + "-" + index + "-" + return strings.Replace(n, "/", "-", -1) +} + func GenGeneralName(jobName string, rtype string, index string) string { n := jobName + "-" + strings.ToLower(rtype) + "-" + index return strings.Replace(n, "/", "-", -1) diff --git a/pkg/controller.v1/control/pod_control.go b/pkg/controller.v1/control/pod_control.go index 257625b7..00bfa64d 100644 --- a/pkg/controller.v1/control/pod_control.go +++ b/pkg/controller.v1/control/pod_control.go @@ -21,7 +21,7 @@ import ( commonutil "github.com/kubeflow/common/pkg/util" - "k8s.io/api/core/v1" + v1 "k8s.io/api/core/v1" "k8s.io/apimachinery/pkg/api/errors" "k8s.io/apimachinery/pkg/api/meta" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" @@ -130,6 +130,9 @@ func GetPodFromTemplate(template *v1.PodTemplateSpec, parentObject runtime.Objec Finalizers: desiredFinalizers, }, } + if template.GenerateName != "" { + pod.ObjectMeta.GenerateName = template.GenerateName + } if controllerRef != nil { pod.OwnerReferences = append(pod.OwnerReferences, *controllerRef) } diff --git a/pkg/controller.v1/control/utils.go b/pkg/controller.v1/control/utils.go index 62b910d3..de639c59 100644 --- a/pkg/controller.v1/control/utils.go +++ b/pkg/controller.v1/control/utils.go @@ -18,6 +18,7 @@ package control import ( "fmt" + v1 "k8s.io/api/core/v1" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" "k8s.io/apimachinery/pkg/runtime"