Skip to content

Unify common code#1130

Open
amartyasinha wants to merge 8 commits into
openstack-k8s-operators:mainfrom
amartyasinha:unify_code
Open

Unify common code#1130
amartyasinha wants to merge 8 commits into
openstack-k8s-operators:mainfrom
amartyasinha:unify_code

Conversation

@amartyasinha

@amartyasinha amartyasinha commented Jun 18, 2026

Copy link
Copy Markdown
Contributor

Introduce internal/common for shared nova/placement
controller code: reconciler base, ensure helpers,
config generation, watch mappers, Kolla constants,
and static errors. Nova and placement controllers
call these helpers; nova-only logic stays in
internal/controller/nova/common.go.

This is a move-first refactor: logic, comments, and
naming are preserved where possible. Only
unavoidable mechanical changes are included (e.g.
exported names for cross-package use, import
updates, r.Client.Update because Client is a named
field on ReconcilerBase).

Co-authored-by: Cursor cursoragent@cursor.com

@openshift-ci

openshift-ci Bot commented Jun 18, 2026

Copy link
Copy Markdown
Contributor

Skipping CI for Draft Pull Request.
If you want CI signal for your change, please convert it to an actual PR.
You can still manually trigger a test run with /test all

@openshift-ci

openshift-ci Bot commented Jun 18, 2026

Copy link
Copy Markdown
Contributor

[APPROVALNOTIFIER] This PR is APPROVED

This pull-request has been approved by: amartyasinha

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:

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

@amartyasinha amartyasinha force-pushed the unify_code branch 10 times, most recently from 6aab9a1 to 1ef05f9 Compare June 22, 2026 10:20
}

// ReconcilerBase provides a common set of clients scheme and loggers for all reconcilers.
type ReconcilerBase struct {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

so ReconcilerBase is intened to be the thing that hold the common code

so you shoudl be moving this as well if your going to do this.

the orginal inte was for this to become the common bas for the nova placment and evnetully cyborg contolers.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Thanks @SeanMooney! Sure, I'll be moving ReconcilerBase soon to common code. I had it in the initial draft, but removed it because it was getting overwhelming for me to self-review. Once I believe the current code I'm moving are well-placed, I'll move ReconcilerBase as well.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

I've tried moving ReconcilerBase and other related func to internal/common. PTAL.

h.GetClient(),
&instance.Status.Conditions)
&instance.Status.Conditions,
time.Second*10,

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I think thanks to unifying ReconcilerBase, we can use r.RequeueTimeout

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

done

Comment thread internal/controller/placement/api_controller.go
Comment thread internal/common/network.go Outdated
// networks to attach to
nadList := []networkv1.NetworkAttachmentDefinition{}
for _, netAtt := range networkAttachments {
nad, err := nad.GetNADWithName(ctx, h, netAtt, h.GetBeforeObject().GetNamespace())

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

maybe nadObject (nad is a name of package already)

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Done. Thanks!

Comment thread internal/common/errors.go
var (
// ErrACSecretNotFound indicates that the ApplicationCredential secret was not found.
ErrACSecretNotFound = errors.New("ApplicationCredential secret not found")
// ErrACSecretMissingKeys indicates that the ApplicationCredential secret is missing required keys.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I think we missed a few errors, like errUnexpectedObjectType or ErrInvalidObjectType (of course, assuming we want to deduplicate all similar errors in the future, e.g., all errors like errExpectedNovaObject)

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

moved all remaining errors here.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Cool thanks, now when we have all errors in one place they can be unified like ErrInvalidObjectType and ErrUnexpectedObjectType probably can be one error in code. Maybe similar thing can be with more specific one eg ErrExpectedNovaAPIObject

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

done. PTAL

@amartyasinha amartyasinha force-pushed the unify_code branch 2 times, most recently from fc8d155 to 7d82edc Compare June 23, 2026 11:34
@amartyasinha amartyasinha requested a review from mrkisaolamb June 23, 2026 11:38

@mrkisaolamb mrkisaolamb left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Something else we can try: unifying FindObjectsForSrcByField and findObjectsWithAppSelectorLabelInNamespace

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I think this can be fix Log := r.GetLogger(ctx)

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

done

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

can we fix all this kind of timeouts using r.RequeueTimeout?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

updated

Comment thread internal/common/errors.go
var (
// ErrACSecretNotFound indicates that the ApplicationCredential secret was not found.
ErrACSecretNotFound = errors.New("ApplicationCredential secret not found")
// ErrACSecretMissingKeys indicates that the ApplicationCredential secret is missing required keys.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Cool thanks, now when we have all errors in one place they can be unified like ErrInvalidObjectType and ErrUnexpectedObjectType probably can be one error in code. Maybe similar thing can be with more specific one eg ErrExpectedNovaAPIObject

return result, nil
}

err = r.generateServiceConfigMaps(ctx, h, instance, secret, &configMapVars, db)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Do you think we can generalize the existing nova generateConfigsGeneric and use it here?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Yes, we can do that. Though it won't be a clean move of nova's generateConfigsGeneric to internal/common, and there will be a few changes, thus will required some tests. I'll try to do it tomorrow, or if you want, we can have it in a follow up PR too, whatever you prefer.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

added a new commit for this. PTAL

@amartyasinha amartyasinha force-pushed the unify_code branch 5 times, most recently from 2444cca to 3a8b6c6 Compare June 24, 2026 15:22
@amartyasinha amartyasinha marked this pull request as ready for review June 29, 2026 03:38
@openshift-ci openshift-ci Bot requested a review from amoralej June 29, 2026 03:38
@amartyasinha amartyasinha changed the title [WIP] Unify common code Unify common code Jun 29, 2026
amartyasinha and others added 8 commits June 29, 2026 09:43
…et names

Move shared Kolla volume and secret name helpers out of internal/nova so
nova and placement deployments can reuse them.
Move shared static errors into internal/common/errors.go and update webhook
and controller call sites to reference the shared definitions.
Extract ReconcilerBase, Manageable, Reconciler, NewReconcilerBase,
SetRequeueTimeout, Reconcilers, Setup, and OverrideRequeueTimeout into
internal/common. Wire nova, placement, and main to use the shared base.
Move EnsureSecret, EnsureNetworkAttachments, EnsureTopology, and
ConditionUpdater into internal/common. Switch nova and placement
controllers to use the shared helpers.
Remove the duplicate allSubConditionIsTrue helper from nova common; logic
matches condition.Conditions.AllSubConditionIsTrue() from lib-common.
Add unit tests for internal/common.EnsureSecret and extend placement
functional coverage for missing and incomplete input secrets.
Replace duplicated controller watch map functions with generic helpers in
internal/common/watch.go.
Share config secret generation between nova and placement controllers.
Nova passes service-specific templates via NovaAdditionalTemplates in
nova/common.go; placement uses GenerateConfigsWithScripts in
generateServiceConfigMaps.

Co-authored-by: Cursor <cursoragent@cursor.com>
Comment thread internal/common/errors.go
// Static errors shared across operator services.
var (
// ErrACSecretNotFound indicates that the ApplicationCredential secret was not found.
ErrACSecretNotFound = errors.New("ApplicationCredential secret not found")

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

question: wdyt about removing this error and use in placement similar condition+log+nil error like in nova controllers?

cmLabels, additionalTemplates, commonTemplates, templateDir, true,
)
// NovaAdditionalTemplates are the default extra config templates for nova services.
var NovaAdditionalTemplates = map[string]string{

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I don't think this should be global, as it can be mutated by any caller during execution. It's better to scope this locally inside the functions


// EnsureNetworkAttachments - checks the requested network attachments exists and
// returns the annotation to be set on the deployment objects.
func EnsureNetworkAttachments(

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

can we add some tests for this?

// EnsureTopology - when a Topology CR is referenced, remove the
// finalizer from a previous referenced Topology (if any), and retrieve the
// newly referenced topology object
func EnsureTopology(

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

pls add some unit tests

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants