diff --git a/api/xset_controller_types.go b/api/xset_controller_types.go index 235fba4..cd76907 100644 --- a/api/xset_controller_types.go +++ b/api/xset_controller_types.go @@ -137,3 +137,12 @@ type DecorationAdapter interface { type TargetPrefixGetter interface { GetTargetPrefix(xset XSetObject) string } + +// XPostCreateTarget is an optional interface that XSetController can implement +// to apply additional modifications to targets after their names are assigned +// (e.g., setting pod hostname based on the pod name). +// The functions returned by GetXPostCreateTargetFuncs are called after the target +// object is constructed but before it is created on the API server. +type XPostCreateTarget interface { + GetXPostCreateTargetFuncs(revision *appsv1.ControllerRevision) []func(client.Object) error +} diff --git a/synccontrols/sync_control.go b/synccontrols/sync_control.go index 7e48f61..f55c8b8 100644 --- a/synccontrols/sync_control.go +++ b/synccontrols/sync_control.go @@ -562,6 +562,14 @@ func (r *RealSyncControl) Scale(ctx context.Context, xsetObject api.XSetObject, if err != nil { return apierrors.NewInvalid(schema.GroupKind{Group: r.targetGVK.Group, Kind: r.targetGVK.Kind}, target.GetGenerateName(), []*field.Error{{Detail: err.Error()}}) } + // apply post-create target funcs (e.g., set pod hostname based on pod name) + if postCreateAdapter, ok := r.xsetController.(api.XPostCreateTarget); ok { + for _, fn := range postCreateAdapter.GetXPostCreateTargetFuncs(revision) { + if err = fn(target); err != nil { + return fmt.Errorf("fail to apply post-create target func for target %s: %w", target.GetName(), err) + } + } + } // create subresources for targets if r.subResourceControl != nil { if err = r.subResourceControl.CreateTargetResources(ctx, xsetObject, target, syncContext.ExistingSubResources); err != nil { diff --git a/synccontrols/x_replace.go b/synccontrols/x_replace.go index f95412c..b1ac67a 100644 --- a/synccontrols/x_replace.go +++ b/synccontrols/x_replace.go @@ -172,6 +172,15 @@ func (r *RealSyncControl) replaceOriginTargets( return err } + // apply post-create target funcs (e.g., set pod hostname based on pod name) + if postCreateAdapter, ok := r.xsetController.(api.XPostCreateTarget); ok { + for _, fn := range postCreateAdapter.GetXPostCreateTargetFuncs(replaceRevision) { + if err = fn(newTarget); err != nil { + return fmt.Errorf("fail to apply post-create target func for replace target %s: %w", newTarget.GetName(), err) + } + } + } + r.xsetLabelAnnoMgr.Set(newTarget, api.XReplacePairOriginName, originTarget.GetName()) r.xsetLabelAnnoMgr.Set(newTarget, api.XCreatingLabel, strconv.FormatInt(time.Now().UnixNano(), 10)) r.resourceContextControl.Put(newTargetContext, api.EnumRevisionContextDataKey, replaceRevision.GetName())