-
Notifications
You must be signed in to change notification settings - Fork 47
feature(rbg): support role instance index env #194
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -30,6 +30,7 @@ import ( | |||||||||||||||||||||||
| intstrutil "k8s.io/apimachinery/pkg/util/intstr" | ||||||||||||||||||||||||
| "k8s.io/apimachinery/pkg/util/sets" | ||||||||||||||||||||||||
| workloadsv1alpha2 "sigs.k8s.io/rbgs/api/workloads/v1alpha2" | ||||||||||||||||||||||||
| "sigs.k8s.io/rbgs/pkg/constants" | ||||||||||||||||||||||||
| ) | ||||||||||||||||||||||||
|
|
||||||||||||||||||||||||
| // statefulInstanceRegex is a regular expression that extracts the parent InstanceSet and ordinal from the Name of an Instance | ||||||||||||||||||||||||
|
|
@@ -96,8 +97,28 @@ func updateIdentity(set *workloadsv1alpha2.RoleInstanceSet, instance *workloadsv | |||||||||||||||||||||||
| if instance.Labels == nil { | ||||||||||||||||||||||||
| instance.Labels = make(map[string]string) | ||||||||||||||||||||||||
| } | ||||||||||||||||||||||||
| instance.Labels[apps.StatefulSetPodNameLabel] = instance.Name | ||||||||||||||||||||||||
| instance.Labels[apps.PodIndexLabel] = strconv.Itoa(ordinal) | ||||||||||||||||||||||||
| identityLabels := map[string]string{ | ||||||||||||||||||||||||
| apps.StatefulSetPodNameLabel: instance.Name, | ||||||||||||||||||||||||
| constants.RoleInstanceIndexLabelKey: strconv.Itoa(ordinal), | ||||||||||||||||||||||||
| } | ||||||||||||||||||||||||
| for k, v := range identityLabels { | ||||||||||||||||||||||||
| instance.Labels[k] = v | ||||||||||||||||||||||||
| } | ||||||||||||||||||||||||
| injectIdentityLabelsIntoComponents(instance, identityLabels) | ||||||||||||||||||||||||
|
||||||||||||||||||||||||
| injectIdentityLabelsIntoComponents(instance, identityLabels) | |
| // Only inject identity labels that are valid at the pod-template level. | |
| templateIdentityLabels := make(map[string]string, len(identityLabels)) | |
| for k, v := range identityLabels { | |
| if k == apps.StatefulSetPodNameLabel { | |
| // apps.StatefulSetPodNameLabel is per-pod and must not be set on pod templates. | |
| continue | |
| } | |
| templateIdentityLabels[k] = v | |
| } | |
| injectIdentityLabelsIntoComponents(instance, templateIdentityLabels) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This block of code for setting and injecting identity labels is duplicated from the updateIdentity function (lines 100-108). To improve maintainability and reduce redundancy, consider extracting this logic into a new helper function.
For example, you could create a function setAndInjectIdentityLabels(instance *workloadsv1alpha2.RoleInstance, ordinal int) that encapsulates this logic, and then call it from both updateIdentity and newVersionedInstance.
func setAndInjectIdentityLabels(instance *workloadsv1alpha2.RoleInstance, ordinal int) {
if instance.Labels == nil {
instance.Labels = make(map[string]string)
}
identityLabels := map[string]string{
apps.StatefulSetPodNameLabel: instance.Name,
apps.PodIndexLabel: strconv.Itoa(ordinal),
constants.RoleInstanceIndexLabelKey: strconv.Itoa(ordinal),
}
for k, v := range identityLabels {
instance.Labels[k] = v
}
injectIdentityLabelsIntoComponents(instance, identityLabels)
}
Copilot
AI
Mar 12, 2026
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Same issue as above: injecting apps.StatefulSetPodNameLabel into component pod templates will set "statefulset.kubernetes.io/pod-name" to the RoleInstance name for every pod created from the template. Since pod names differ per component replica, this label will be incorrect on the resulting pods. Consider not including apps.StatefulSetPodNameLabel in the set of labels propagated into component templates.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
EnvBuilder.buildLocalRoleVars now uses workloadsv1alpha2.IsStatefulRole(g.role), which changes behavior for RoleInstanceSet roles (e.g., injecting RBG_ROLE_INDEX for stateful RoleInstanceSet patterns). There are existing tests for EnvBuilder in this package, but they don’t cover RoleInstanceSet stateful vs stateless patterns, so this behavior change is currently untested. Please add/extend unit tests in env_builder_test.go to cover RoleInstanceSet roles (stateful + stateless) and assert whether RBG_ROLE_INDEX is included as expected.