Skip to content

fix: key LaunchTemplate by Name so adopt-or-create works - #347

Merged
ack-prow[bot] merged 1 commit into
aws-controllers-k8s:mainfrom
michaelhtm:fix/launch-template-adopt-by-name
Nov 19, 2026
Merged

ack-prow[bot] merged 1 commit into
aws-controllers-k8s:mainfrom
michaelhtm:fix/launch-template-adopt-by-name

Conversation

@michaelhtm

Copy link
Copy Markdown
Member

Description of changes:
LaunchTemplate was keyed by Status.ID, so sdkFind short-circuited to
NotFound whenever the ID was unset and adopt-or-create looped on
CreateLaunchTemplate AlreadyExistsException. Make the unique Name the
primary key (immutable, since AWS cannot rename templates) so sdkFind
reads by name and adopts a pre-existing template. Also stop
DescribeLaunchTemplateVersions from running unfiltered when the template
identifier is unknown, which would clobber Spec.Data with unrelated data.

Resolves aws-controllers-k8s/community#2932

By submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 license.

@ack-prow
ack-prow Bot requested review from gustavodiaz7722 and knottnt June 24, 2026 18:25
@ack-prow ack-prow Bot added the approved label Jun 24, 2026
@michaelhtm
michaelhtm force-pushed the fix/launch-template-adopt-by-name branch from 3a993ca to 3a36dae Compare June 24, 2026 20:31
@michaelhtm

Copy link
Copy Markdown
Member Author

/retest

@michaelhtm
michaelhtm force-pushed the fix/launch-template-adopt-by-name branch from 3a36dae to 9845060 Compare June 25, 2026 16:46
@michaelhtm

Copy link
Copy Markdown
Member Author

/label release/patch

@ack-prow ack-prow Bot added the release/patch Indicates this PR should trigger a patch version release on merge. label Jun 25, 2026
@michaelhtm
michaelhtm force-pushed the fix/launch-template-adopt-by-name branch from 9845060 to 53de950 Compare June 25, 2026 20:19
@michaelhtm

Copy link
Copy Markdown
Member Author

/retest

@michaelhtm

Copy link
Copy Markdown
Member Author

/label release/patch

@jlbutler

Copy link
Copy Markdown
Member

/retest

@gustavodiaz7722 gustavodiaz7722 left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Comment, with one MUST FIX. Reading by name fixes aws-controllers-k8s/community#2932 live. However, a CR whose spec.name was edited on an older release creates a second template on upgrade, or takes over an existing template that has that name.

I tested the merge base, v1.15.0 (the issue reporter's version) and this PR on a dev EKS cluster. The controller was scoped to one namespace with a 60 s resync, and I counted AWS writes in CloudTrail.

Scenario Base / v1.15.0 PR
spec.name edited on base, then upgrade No write New template created, or another template taken over
adopt with {"name": …}, no spec Terminal required field missing: id Synced, spec filled from AWS, 0 writes
adopt with {"id": …} Fails as in aws-controllers-k8s/community#3169 Terminal required field missing: name

MUST FIX: renamed CRs on upgrade (inline on hooks.go). Released versions accept a spec.name edit and ignore it. After the upgrade the controller reads by the new name. It then either creates a new template and orphans the original, or it takes over an unmanaged template that has that name, retags it, and deletes it when the CR is deleted. I reproduced both cases live.

SHOULD FIX: adoption by {"id": …} stops working (inline on generator.yaml).

Not verified: ec2-crd-compat-check printed "no changes" for this CRD, so it did not check the new CEL rule on spec.name.

Comment thread pkg/resource/launch_template/hooks.go Outdated
// gate on Status.ID, which is nil until the first successful read.
func (rm *resourceManager) checkForMissingRequiredFields(r *resource) bool {
return r.ko.Status.ID == nil
return r.ko.Spec.Name == nil

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

MUST FIX (reproduced live, upgrade path). On the base, spec.name is mutable and has no effect after create. The read goes by Status.ID and never compares the name, so a spec.name edit stays Synced, makes no AWS write, and the template keeps its old name. After upgrading to this PR, the read goes by the new name:

  • No template has the new name. The controller makes one CreateLaunchTemplate call for the new name and replaces status.id. The original template is orphaned, and deleting the CR removes only the new one.
  • An unmanaged template already has the new name. The CR takes it over. On a dev cluster the controller deleted that template's own owner tag and added the ACK tags. Deleting the CR then deleted that template, and the original was orphaned.

In both cases the new CEL rule rejects patching the name back.

Options (I haven't tested these):

  • In the sdk_read_many_post_set_output hook, return a Terminal error when r.ko.Status.ID is set and differs from the ID found by name. That stops the takeover. Deleting such a CR then waits on the error instead of deleting another template. The check can't live inside setLatestLaunchTemplateAttributes, because the hook drops that function's error.
  • When the name lookup misses and Status.ID is set, read by ID. That tells a renamed template apart from one deleted out of band ([ec2-controller] LaunchTemplate: the not-found code doesn't match a read by ID community#3172), which should still be recreated.
  • At minimum, add a release note: before upgrading, check that each LaunchTemplate CR's spec.name matches its template's name in AWS.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

good catch, thanks for reproducing both of these! reads now go by status.id once it's set and only fall back to spec.name before the first read, so a CR renamed on an older release keeps reading its own template. tested the upgrade path live (main -> this branch) for both cases, no new template and the other template's tags/lifecycle were untouched.

Comment thread generator.yaml
fields:
ID:
Name:
is_primary_key: true

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

SHOULD FIX (reproduced live, behavior change). Making Name the primary key also changes the adoption key. The generated PopulateResourceFromAnnotation now reads name. With adoption-policy: adopt, an annotation of services.k8s.aws/adoption-fields: {"id": "lt-…"} goes Terminal with Error populating adoption fields: required field missing: name and makes no AWS call. With adopt-or-create the runtime ignores that error, and the read by spec.name still adopts. I confirmed that live too.

{"name": …} works: with no spec at all, the CR adopted the template, filled its spec from AWS, and made no writes. That fixes the adopt-by-name form of aws-controllers-k8s/community#3169, but that issue's own repro adopts by ID.

Two ways to handle it:

  • State the change in the PR body: adopt with {"name": …}, because {"id": …} is no longer accepted.
  • Keep id working. That needs a pre_populate_resource_from_annotation hook that accepts id and returns early, plus a read by ID when spec.name is unset, because DescribeLaunchTemplatesInput.LaunchTemplateIds is now ignored. I haven't tested this.

LaunchTemplate was keyed by Status.ID, so sdkFind short-circuited to
NotFound whenever the ID was unset and adopt-or-create looped on
CreateLaunchTemplate AlreadyExistsException. Make the unique Name the
primary key (immutable, since AWS cannot rename templates) so sdkFind
can find a pre-existing template by name, but read by Status.ID once it
is known so a CR renamed on an older release keeps its own template.
Adoption accepts either {"name": ...} or {"id": ...}. Also stop
DescribeLaunchTemplateVersions from running unfiltered when the
template identifier is unknown, which would clobber Spec.Data with
unrelated data.

Resolves aws-controllers-k8s/community#2932
@michaelhtm
michaelhtm force-pushed the fix/launch-template-adopt-by-name branch from ad03c27 to f0370f7 Compare November 19, 2026 00:31
@gustavodiaz7722

Copy link
Copy Markdown
Member

/LGTM

@ack-prow ack-prow Bot added the lgtm Indicates that a PR is ready to be merged. label Nov 19, 2026
@gustavodiaz7722

Copy link
Copy Markdown
Member

/label release/patch

@ack-prow

ack-prow Bot commented Nov 19, 2026

Copy link
Copy Markdown

[APPROVALNOTIFIER] This PR is APPROVED

This pull-request has been approved by: gustavodiaz7722, michaelhtm

The full list of commands accepted by this bot can be found here.

The pull request process is described here

Details Needs approval from an approver in each of these files:
  • OWNERS [gustavodiaz7722,michaelhtm]

Approvers can indicate their approval by writing /approve in a comment
Approvers can cancel approval by writing /approve cancel in a comment

@ack-prow
ack-prow Bot merged commit 6e5ff11 into aws-controllers-k8s:main Nov 19, 2026
10 checks passed
ack-prow Bot pushed a commit that referenced this pull request Nov 19, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

approved lgtm Indicates that a PR is ready to be merged. release/patch Indicates this PR should trigger a patch version release on merge.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[ec2-controller] LaunchTemplate adopt-or-create silently fails with AlreadyExistsException when Status.ID is unset

3 participants