-
Notifications
You must be signed in to change notification settings - Fork 637
Add an API struct representing a single Secret value #4100
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
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
52 changes: 52 additions & 0 deletions
52
pkg/apis/postgres-operator.crunchydata.com/v1beta1/config_types.go
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,52 @@ | ||
| // Copyright 2021 - 2025 Crunchy Data Solutions, Inc. | ||
| // | ||
| // SPDX-License-Identifier: Apache-2.0 | ||
|
|
||
| package v1beta1 | ||
|
|
||
| import ( | ||
| corev1 "k8s.io/api/core/v1" | ||
| ) | ||
|
|
||
| // +structType=atomic | ||
| type OptionalSecretKeyRef struct { | ||
| SecretKeyRef `json:",inline"` | ||
|
|
||
| // Whether or not the Secret or its data must be defined. Defaults to false. | ||
| // +optional | ||
| Optional *bool `json:"optional,omitempty"` | ||
| } | ||
|
|
||
| // AsProjection returns a copy of this as a [corev1.SecretProjection]. | ||
| func (in *OptionalSecretKeyRef) AsProjection(path string) corev1.SecretProjection { | ||
| out := in.SecretKeyRef.AsProjection(path) | ||
| if in.Optional != nil { | ||
| v := *in.Optional | ||
| out.Optional = &v | ||
| } | ||
| return out | ||
| } | ||
|
|
||
| // +structType=atomic | ||
| type SecretKeyRef struct { | ||
| // Name of the Secret. | ||
| // --- | ||
| // https://pkg.go.dev/k8s.io/kubernetes/pkg/apis/core/validation#ValidateSecretName | ||
| // +required | ||
| Name DNS1123Subdomain `json:"name"` | ||
|
|
||
| // Name of the data field within the Secret. | ||
| // --- | ||
| // https://releases.k8s.io/v1.32.0/pkg/apis/core/validation/validation.go#L2867 | ||
| // https://pkg.go.dev/k8s.io/apimachinery/pkg/util/validation#IsConfigMapKey | ||
| // +required | ||
| Key ConfigDataKey `json:"key"` | ||
| } | ||
|
|
||
| // AsProjection returns a copy of this as a [corev1.SecretProjection]. | ||
| func (in *SecretKeyRef) AsProjection(path string) corev1.SecretProjection { | ||
| var out corev1.SecretProjection | ||
| out.Name = in.Name | ||
| out.Items = []corev1.KeyToPath{{Key: in.Key, Path: path}} | ||
| return out | ||
| } |
80 changes: 80 additions & 0 deletions
80
pkg/apis/postgres-operator.crunchydata.com/v1beta1/config_types_test.go
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,80 @@ | ||
| // Copyright 2021 - 2025 Crunchy Data Solutions, Inc. | ||
| // | ||
| // SPDX-License-Identifier: Apache-2.0 | ||
|
|
||
| package v1beta1_test | ||
|
|
||
| import ( | ||
| "strings" | ||
| "testing" | ||
|
|
||
| "gotest.tools/v3/assert" | ||
| "sigs.k8s.io/yaml" | ||
|
|
||
| "github.com/crunchydata/postgres-operator/pkg/apis/postgres-operator.crunchydata.com/v1beta1" | ||
| ) | ||
|
|
||
| func TestOptionalSecretKeyRefAsProjection(t *testing.T) { | ||
| t.Run("Null", func(t *testing.T) { | ||
| in := v1beta1.OptionalSecretKeyRef{} | ||
| in.Name, in.Key = "one", "two" | ||
|
|
||
| out := in.AsProjection("three") | ||
| b, err := yaml.Marshal(out) | ||
| assert.NilError(t, err) | ||
| assert.DeepEqual(t, string(b), strings.TrimSpace(` | ||
| items: | ||
| - key: two | ||
| path: three | ||
| name: one | ||
| `)+"\n") | ||
| }) | ||
|
|
||
| t.Run("True", func(t *testing.T) { | ||
| True := true | ||
| in := v1beta1.OptionalSecretKeyRef{Optional: &True} | ||
| in.Name, in.Key = "one", "two" | ||
|
|
||
| out := in.AsProjection("three") | ||
| b, err := yaml.Marshal(out) | ||
| assert.NilError(t, err) | ||
| assert.DeepEqual(t, string(b), strings.TrimSpace(` | ||
| items: | ||
| - key: two | ||
| path: three | ||
| name: one | ||
| optional: true | ||
| `)+"\n") | ||
| }) | ||
|
|
||
| t.Run("False", func(t *testing.T) { | ||
| False := false | ||
| in := v1beta1.OptionalSecretKeyRef{Optional: &False} | ||
| in.Name, in.Key = "one", "two" | ||
|
|
||
| out := in.AsProjection("three") | ||
| b, err := yaml.Marshal(out) | ||
| assert.NilError(t, err) | ||
| assert.DeepEqual(t, string(b), strings.TrimSpace(` | ||
| items: | ||
| - key: two | ||
| path: three | ||
| name: one | ||
| optional: false | ||
| `)+"\n") | ||
| }) | ||
| } | ||
|
|
||
| func TestSecretKeyRefAsProjection(t *testing.T) { | ||
| in := v1beta1.SecretKeyRef{Name: "asdf", Key: "foobar"} | ||
| out := in.AsProjection("some-path") | ||
|
|
||
| b, err := yaml.Marshal(out) | ||
| assert.NilError(t, err) | ||
| assert.DeepEqual(t, string(b), strings.TrimSpace(` | ||
| items: | ||
| - key: foobar | ||
| path: some-path | ||
| name: asdf | ||
| `)+"\n") | ||
| } |
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
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
📝 Having a CEL rule felt like a bit much, but if we allow these values up-front, then we'll error trying to send them into volume source, secret ref, etc during reconcile.