Skip to content
Open
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
9 changes: 9 additions & 0 deletions api/xset_controller_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
8 changes: 8 additions & 0 deletions synccontrols/sync_control.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
9 changes: 9 additions & 0 deletions synccontrols/x_replace.go
Original file line number Diff line number Diff line change
Expand Up @@ -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())
Expand Down
Loading