-
Notifications
You must be signed in to change notification settings - Fork 3
Add utility metadata package for Kubernetes labels #18
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
Merged
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
478cf55
Add utility metadata package for Kubernetes labels
rytswd 2f7e403
Add part-of label
rytswd ed84b5b
Add default cell label
rytswd 4ddea7d
Update to label part-of and multigres cell
rytswd c766a0a
Ensure test aligns with new labels
rytswd 8069af2
Add version label for future usage
rytswd b2632ea
Move metadata test into its own test package
rytswd 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,6 @@ | ||
| // Package metadata provides utilities for building Kubernetes resource metadata | ||
| // such as labels, annotations, and owner references used across all Multigres components. | ||
| // | ||
| // This package contains generic, reusable functions that are component-agnostic. | ||
| // Component-specific logic should be provided by callers through function parameters. | ||
| package metadata |
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,106 @@ | ||
| package metadata | ||
|
|
||
| import "maps" | ||
|
|
||
| // Standard Kubernetes label keys following kubernetes.io conventions. | ||
| // | ||
| // See: https://kubernetes.io/docs/concepts/overview/working-with-objects/common-labels/ | ||
| const ( | ||
| // LabelAppName is the standard label key for the application name. | ||
| LabelAppName = "app.kubernetes.io/name" | ||
|
|
||
| // LabelAppInstance is the standard label key for the unique instance name. | ||
| LabelAppInstance = "app.kubernetes.io/instance" | ||
|
|
||
| // LabelAppVersion is the standard label key for the application version. | ||
| LabelAppVersion = "app.kubernetes.io/version" | ||
|
|
||
| // LabelAppComponent is the standard label key for the component within the | ||
| // application. | ||
| LabelAppComponent = "app.kubernetes.io/component" | ||
|
|
||
| // LabelAppPartOf is the standard label key for the name of a higher level | ||
| // application this one is part of. | ||
| LabelAppPartOf = "app.kubernetes.io/part-of" | ||
|
|
||
| // LabelAppManagedBy is the standard label key for the tool managing the | ||
| // resource. | ||
| LabelAppManagedBy = "app.kubernetes.io/managed-by" | ||
| ) | ||
|
|
||
| const ( | ||
| // AppNameMultigres is the fixed application name for all Multigres resources. | ||
| AppNameMultigres = "multigres" | ||
|
|
||
| // ManagedByMultigres identifies the operator managing these resources. | ||
| ManagedByMultigres = "multigres-operator" | ||
| ) | ||
|
|
||
| const ( | ||
| // LabelMultigresCell identifies which cell a resource belongs to. | ||
| LabelMultigresCell = "multigres.com/cell" | ||
|
|
||
| // DefaultCellName is the default cell name when none is specified. | ||
| DefaultCellName = "multigres-global-topo" | ||
| ) | ||
|
|
||
| // BuildStandardLabels builds the standard Kubernetes labels for a Multigres | ||
| // component. These labels are applied to all resources managed by the operator. | ||
| // | ||
| // Parameters: | ||
| // - resourceName: The name of the custom resource instance (e.g., "my-etcd-cluster") | ||
| // - componentName: The component type (e.g., "etcd", "gateway", "orch", "pooler") | ||
| // - cellName: Optional cell name. If empty, no cell label is added. | ||
fernando-villalba marked this conversation as resolved.
Show resolved
Hide resolved
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Maybe remove this comment now we are doing a default? |
||
| // | ||
| // Standard labels include: | ||
| // - app.kubernetes.io/name: "multigres" | ||
| // - app.kubernetes.io/instance: <resourceName> | ||
| // - app.kubernetes.io/component: <componentName> | ||
| // - app.kubernetes.io/part-of: "multigres" | ||
| // - app.kubernetes.io/managed-by: "multigres-operator" | ||
| // - multigres.com/cell: <cellName> (uses "multigres-global-topo" if empty) | ||
| // | ||
| // Example usage: | ||
| // | ||
| // labels := BuildStandardLabels("my-etcd", "etcd", "cell-1") | ||
| // // Returns: { | ||
| // // "app.kubernetes.io/name": "multigres", | ||
| // // "app.kubernetes.io/instance": "my-etcd", | ||
| // // "app.kubernetes.io/component": "etcd", | ||
| // // "app.kubernetes.io/part-of": "multigres", | ||
| // // "app.kubernetes.io/managed-by": "multigres-operator", | ||
| // // "multigres.com/cell": "cell-1" | ||
| // // } | ||
| func BuildStandardLabels(resourceName, componentName, cellName string) map[string]string { | ||
| // Use default cell name if not provided | ||
| if cellName == "" { | ||
| cellName = DefaultCellName | ||
| } | ||
|
|
||
| labels := map[string]string{ | ||
| LabelAppName: AppNameMultigres, | ||
| LabelAppInstance: resourceName, | ||
| LabelAppComponent: componentName, | ||
| LabelAppPartOf: AppNameMultigres, | ||
| LabelAppManagedBy: ManagedByMultigres, | ||
| LabelMultigresCell: cellName, | ||
| } | ||
|
|
||
| return labels | ||
| } | ||
|
|
||
| // MergeLabels merges custom labels with standard labels. | ||
| // | ||
| // Note that standard labels take precedence over custom labels to prevent users | ||
| // from overriding critical operator-managed labels. | ||
| func MergeLabels(standardLabels, customLabels map[string]string) map[string]string { | ||
| merged := make(map[string]string) | ||
|
|
||
| // Copy custom labels first (if provided) | ||
| maps.Copy(merged, customLabels) | ||
|
|
||
| // Copy standard labels (overwriting any duplicates from custom) | ||
| maps.Copy(merged, standardLabels) | ||
|
|
||
| return merged | ||
| } | ||
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.