-
Notifications
You must be signed in to change notification settings - Fork 62
Unify common code #1130
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
Open
amartyasinha
wants to merge
8
commits into
openstack-k8s-operators:main
Choose a base branch
from
amartyasinha:unify_code
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+1,765
−1,283
Open
Unify common code #1130
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
87e7cde
Add internal/common kolla helpers for shared Kolla constants and secr…
amartyasinha 521aa7f
Consolidate operator static errors in internal/common
amartyasinha 1f8737c
Move shared reconciler infrastructure to internal/common
amartyasinha 7aaf4a2
Extract shared ensure helpers into internal/common
amartyasinha 3f7c090
Use lib-common AllSubConditionIsTrue in nova controllers
amartyasinha cfcdcea
Add unit tests for internal/common helpers
amartyasinha 4844382
Extract shared watch map helpers into internal/common
amartyasinha 2d4ed82
Move GenerateConfigs helpers to internal/common
amartyasinha File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,102 @@ | ||
| /* | ||
| Copyright 2026. | ||
|
|
||
| Licensed under the Apache License, Version 2.0 (the "License"); | ||
| you may not use this file except in compliance with the License. | ||
| You may obtain a copy of the License at | ||
|
|
||
| http://www.apache.org/licenses/LICENSE-2.0 | ||
|
|
||
| Unless required by applicable law or agreed to in writing, software | ||
| distributed under the License is distributed on an "AS IS" BASIS, | ||
| WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| See the License for the specific language governing permissions and | ||
| limitations under the License. | ||
| */ | ||
|
|
||
| package common //nolint:revive // common is the established package name for multi-group shared code | ||
|
|
||
| import ( | ||
| "context" | ||
|
|
||
| "github.com/openstack-k8s-operators/lib-common/modules/common/env" | ||
| helper "github.com/openstack-k8s-operators/lib-common/modules/common/helper" | ||
| "github.com/openstack-k8s-operators/lib-common/modules/common/secret" | ||
| util "github.com/openstack-k8s-operators/lib-common/modules/common/util" | ||
| "sigs.k8s.io/controller-runtime/pkg/client" | ||
| ) | ||
|
|
||
| // GenerateConfigs helper function to generate config maps | ||
| func GenerateConfigs( | ||
| ctx context.Context, h *helper.Helper, | ||
| instance client.Object, configName string, envVars *map[string]env.Setter, | ||
| templateParameters map[string]any, | ||
| extraData map[string]string, cmLabels map[string]string, | ||
| additionalTemplates map[string]string, | ||
| commonTemplates []string, | ||
| templateDir string, | ||
| ) error { | ||
| return generateConfigsGeneric(ctx, h, instance, configName, envVars, templateParameters, extraData, | ||
| cmLabels, additionalTemplates, commonTemplates, templateDir, false) | ||
| } | ||
|
|
||
| // GenerateConfigsWithScripts helper function to generate config maps | ||
| // for service configs and scripts | ||
| func GenerateConfigsWithScripts( | ||
| ctx context.Context, h *helper.Helper, | ||
| instance client.Object, envVars *map[string]env.Setter, | ||
| templateParameters map[string]any, | ||
| extraData map[string]string, cmLabels map[string]string, | ||
| additionalTemplates map[string]string, | ||
| commonTemplates []string, | ||
| templateDir string, | ||
| ) error { | ||
| return generateConfigsGeneric(ctx, h, instance, GetServiceConfigSecretName(instance.GetName()), | ||
| envVars, templateParameters, extraData, | ||
| cmLabels, additionalTemplates, commonTemplates, templateDir, true) | ||
| } | ||
|
|
||
| // generateConfigsGeneric helper function to generate config maps | ||
| func generateConfigsGeneric( | ||
| ctx context.Context, h *helper.Helper, | ||
| instance client.Object, configName string, envVars *map[string]env.Setter, | ||
| templateParameters map[string]any, | ||
| extraData map[string]string, cmLabels map[string]string, | ||
| additionalTemplates map[string]string, | ||
| commonTemplates []string, | ||
| templateDir string, | ||
| withScripts bool, | ||
| ) error { | ||
| if templateDir == "" { | ||
| return ErrTemplateDirUnset | ||
| } | ||
|
|
||
| cms := []util.Template{ | ||
| { | ||
| Name: configName, | ||
| Namespace: instance.GetNamespace(), | ||
| Type: util.TemplateTypeConfig, | ||
| InstanceType: instance.GetObjectKind().GroupVersionKind().Kind, | ||
| MultiTemplateDir: templateDir, | ||
| ConfigOptions: templateParameters, | ||
| Labels: cmLabels, | ||
| CustomData: extraData, | ||
| Annotations: map[string]string{}, | ||
| AdditionalTemplate: additionalTemplates, | ||
| CommonTemplates: commonTemplates, | ||
| }, | ||
| } | ||
| if withScripts { | ||
| cms = append(cms, util.Template{ | ||
| Name: GetScriptSecretName(instance.GetName()), | ||
| Namespace: instance.GetNamespace(), | ||
| Type: util.TemplateTypeScripts, | ||
| InstanceType: instance.GetObjectKind().GroupVersionKind().Kind, | ||
| MultiTemplateDir: templateDir, | ||
| AdditionalTemplate: map[string]string{}, | ||
| Annotations: map[string]string{}, | ||
| Labels: cmLabels, | ||
| }) | ||
| } | ||
| return secret.EnsureSecrets(ctx, h, instance, cms, envVars) | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,30 @@ | ||
| /* | ||
| Copyright 2026. | ||
|
|
||
| Licensed under the Apache License, Version 2.0 (the "License"); | ||
| you may not use this file except in compliance with the License. | ||
| You may obtain a copy of the License at | ||
|
|
||
| http://www.apache.org/licenses/LICENSE-2.0 | ||
|
|
||
| Unless required by applicable law or agreed to in writing, software | ||
| distributed under the License is distributed on an "AS IS" BASIS, | ||
| WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| See the License for the specific language governing permissions and | ||
| limitations under the License. | ||
| */ | ||
|
|
||
| package common //nolint:revive // common is the established package name for multi-group shared code | ||
|
|
||
| import "errors" | ||
|
|
||
| // Static errors shared across operator services. | ||
| var ( | ||
| // ErrACSecretMissingKeys indicates that the ApplicationCredential secret is missing required keys. | ||
| ErrACSecretMissingKeys = errors.New("ApplicationCredential secret missing required keys") | ||
| // ErrTemplateDirUnset indicates that no template directory was provided. | ||
| ErrTemplateDirUnset = errors.New("templateDir must be set") | ||
|
|
||
| // ErrUnexpectedObjectType is returned when a webhook receives an unexpected object type. | ||
| ErrUnexpectedObjectType = errors.New("unexpected object type") | ||
| ) | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,37 @@ | ||
| /* | ||
| Copyright 2026. | ||
|
|
||
| Licensed under the Apache License, Version 2.0 (the "License"); | ||
| you may not use this file except in compliance with the License. | ||
| You may obtain a copy of the License at | ||
|
|
||
| http://www.apache.org/licenses/LICENSE-2.0 | ||
|
|
||
| Unless required by applicable law or agreed to in writing, software | ||
| distributed under the License is distributed on an "AS IS" BASIS, | ||
| WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| See the License for the specific language governing permissions and | ||
| limitations under the License. | ||
| */ | ||
|
|
||
| // Package common provides shared helpers for nova, placement, and cyborg controllers. | ||
| package common //nolint:revive // common is the established package name for multi-group shared code | ||
|
|
||
| import "fmt" | ||
|
|
||
| const ( | ||
| // ServiceCommand - the command to start the service binary in the kolla container | ||
| ServiceCommand = "/usr/local/bin/kolla_start" | ||
| ) | ||
|
|
||
| // GetScriptSecretName returns the name of the Secret used for the | ||
| // db sync scripts | ||
| func GetScriptSecretName(crName string) string { | ||
| return fmt.Sprintf("%s-scripts", crName) | ||
| } | ||
|
|
||
| // GetServiceConfigSecretName returns the name of the Secret used to | ||
| // store the service configuration files | ||
| func GetServiceConfigSecretName(crName string) string { | ||
| return fmt.Sprintf("%s-config-data", crName) | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,82 @@ | ||
| /* | ||
| Copyright 2026. | ||
|
|
||
| Licensed under the Apache License, Version 2.0 (the "License"); | ||
| you may not use this file except in compliance with the License. | ||
| You may obtain a copy of the License at | ||
|
|
||
| http://www.apache.org/licenses/LICENSE-2.0 | ||
|
|
||
| Unless required by applicable law or agreed to in writing, software | ||
| distributed under the License is distributed on an "AS IS" BASIS, | ||
| WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| See the License for the specific language governing permissions and | ||
| limitations under the License. | ||
| */ | ||
|
|
||
| package common //nolint:revive // common is the established package name for multi-group shared code | ||
|
|
||
| import ( | ||
| "context" | ||
| "fmt" | ||
| "time" | ||
|
|
||
| networkv1 "github.com/k8snetworkplumbingwg/network-attachment-definition-client/pkg/apis/k8s.cni.cncf.io/v1" | ||
| condition "github.com/openstack-k8s-operators/lib-common/modules/common/condition" | ||
| helper "github.com/openstack-k8s-operators/lib-common/modules/common/helper" | ||
| nad "github.com/openstack-k8s-operators/lib-common/modules/common/networkattachment" | ||
| k8s_errors "k8s.io/apimachinery/pkg/api/errors" | ||
| ctrl "sigs.k8s.io/controller-runtime" | ||
| "sigs.k8s.io/controller-runtime/pkg/log" | ||
| ) | ||
|
|
||
| // EnsureNetworkAttachments - checks the requested network attachments exists and | ||
| // returns the annotation to be set on the deployment objects. | ||
| func EnsureNetworkAttachments( | ||
|
mrkisaolamb marked this conversation as resolved.
|
||
| ctx context.Context, | ||
| h *helper.Helper, | ||
| networkAttachments []string, | ||
| conditionUpdater ConditionUpdater, | ||
| requeueTimeout time.Duration, | ||
| ) (map[string]string, ctrl.Result, error) { | ||
| var nadAnnotations map[string]string | ||
|
|
||
| // networks to attach to | ||
| nadList := []networkv1.NetworkAttachmentDefinition{} | ||
| for _, netAtt := range networkAttachments { | ||
| nadObject, err := nad.GetNADWithName(ctx, h, netAtt, h.GetBeforeObject().GetNamespace()) | ||
| if err != nil { | ||
| if k8s_errors.IsNotFound(err) { | ||
| // Since the net-attach-def CR should have been manually created by the user and referenced in the spec, | ||
| // we treat this as a warning because it means that the service will not be able to start. | ||
| log.FromContext(ctx).Info(fmt.Sprintf("network-attachment-definition %s not found", netAtt)) | ||
| conditionUpdater.Set(condition.FalseCondition( | ||
| condition.NetworkAttachmentsReadyCondition, | ||
| condition.ErrorReason, | ||
| condition.SeverityWarning, | ||
| condition.NetworkAttachmentsReadyWaitingMessage, | ||
| netAtt)) | ||
| return nadAnnotations, ctrl.Result{RequeueAfter: requeueTimeout}, nil | ||
| } | ||
| conditionUpdater.Set(condition.FalseCondition( | ||
| condition.NetworkAttachmentsReadyCondition, | ||
| condition.ErrorReason, | ||
| condition.SeverityWarning, | ||
| condition.NetworkAttachmentsErrorMessage, | ||
| err.Error())) | ||
| return nadAnnotations, ctrl.Result{}, err | ||
| } | ||
|
|
||
| if nadObject != nil { | ||
| nadList = append(nadList, *nadObject) | ||
| } | ||
| } | ||
|
|
||
| nadAnnotations, err := nad.EnsureNetworksAnnotation(nadList) | ||
| if err != nil { | ||
| return nadAnnotations, ctrl.Result{}, fmt.Errorf("failed create network annotation from %s: %w", | ||
| networkAttachments, err) | ||
| } | ||
|
|
||
| return nadAnnotations, ctrl.Result{}, nil | ||
| } | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.