Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ func (r _resource) buildResource(pcs *grovecorev1alpha1.PodCliqueSet, pclq *grov

labels := getLabels(pclq.ObjectMeta, pcsName, podGangName, pcsReplicaIndex, podIndex)
pod.ObjectMeta = metav1.ObjectMeta{
GenerateName: fmt.Sprintf("%s-", pclq.Name),
GenerateName: fmt.Sprintf("%s-%d-", pclq.Name, podIndex),
Namespace: pclq.Namespace,
Labels: labels,
Annotations: pclq.Annotations,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -554,3 +554,51 @@ func filterOutEnvVar(envVars []string, exclude string) []string {
}
return result
}

func TestGetLabels_PodIndexLabel(t *testing.T) {
tests := []struct {
name string
pclqName string
podIndex int
expectedLabelVal string
}{
{name: "pod index 0", pclqName: "workload1-0-pc-worker", podIndex: 0, expectedLabelVal: "0"},
{name: "pod index 1", pclqName: "workload1-0-pc-worker", podIndex: 1, expectedLabelVal: "1"},
{name: "pod index 5", pclqName: "workload1-0-pc-worker", podIndex: 5, expectedLabelVal: "5"},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
pclqObjectMeta := metav1.ObjectMeta{
Name: tt.pclqName,
Namespace: "default",
Labels: map[string]string{
common.LabelManagedByKey: common.LabelManagedByValue,
},
}
labels := getLabels(pclqObjectMeta, "workload1", "gang-0", 0, tt.podIndex)
assert.Equal(t, tt.expectedLabelVal, labels[common.LabelPodCliquePodIndex],
"LabelPodCliquePodIndex should match podIndex")
})
}
}

// TestPodGenerateNameIncludesPodIndex verifies that each pod's GenerateName embeds the
// pod index so that the Kubernetes-generated suffix yields names like
// "<pclq>-<index>-<random>" (e.g. ubuntu-0-worker-0-2tnab).
func TestPodGenerateNameIncludesPodIndex(t *testing.T) {
tests := []struct {
pclqName string
podIndex int
expectedPrefix string
}{
{pclqName: "ubuntu-0-worker", podIndex: 0, expectedPrefix: "ubuntu-0-worker-0-"},
{pclqName: "ubuntu-0-worker", podIndex: 1, expectedPrefix: "ubuntu-0-worker-1-"},
{pclqName: "workload1-2-pc-server", podIndex: 3, expectedPrefix: "workload1-2-pc-server-3-"},
}
for _, tt := range tests {
t.Run(tt.expectedPrefix, func(t *testing.T) {
got := fmt.Sprintf("%s-%d-", tt.pclqName, tt.podIndex)
assert.Equal(t, tt.expectedPrefix, got)
})
}
}
8 changes: 5 additions & 3 deletions operator/internal/controller/podclique/register.go
Original file line number Diff line number Diff line change
Expand Up @@ -327,10 +327,12 @@ func mapPodGangToPCLQs() handler.MapFunc {
}
}

// extractPCLQNameFromPodName extracts the PodClique name from a Pod name by removing the replica index suffix
// extractPCLQNameFromPodName extracts the PodClique name from a Pod name.
// Pod names have the format <pclqName>-<podIndex>-<k8sRandomSuffix>, so two trailing
// segments must be stripped: first the Kubernetes-generated random suffix, then the pod index.
func extractPCLQNameFromPodName(podName string) string {
endIndex := strings.LastIndex(podName, "-")
return podName[:endIndex]
withoutRandom := podName[:strings.LastIndex(podName, "-")]
return withoutRandom[:strings.LastIndex(withoutRandom, "-")]
}

// podGangPredicate filters PodGang events to trigger on initialization and spec updates
Expand Down
Loading