forked from openshift/api
-
Notifications
You must be signed in to change notification settings - Fork 0
feat(vsphere): Add VSphereCredentialsMode and ComponentCredentials to VSpherePlatformSpec #2
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
splatypus-bot
wants to merge
1
commit into
master
Choose a base branch
from
story-36-vsphere-credentials-mode
base: master
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.
Open
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,231 @@ | ||
| package v1 | ||
|
|
||
| import ( | ||
| "encoding/json" | ||
| "testing" | ||
| ) | ||
|
|
||
| // AC1: VSphereCredentialsMode zero-value defaults to Passthrough | ||
|
|
||
| func TestVSphereCredentialsMode_ZeroValue(t *testing.T) { | ||
| var mode VSphereCredentialsMode | ||
| if mode != VSphereCredentialsModePassthrough { | ||
| t.Errorf("zero value of VSphereCredentialsMode = %q; want %q (Passthrough)", | ||
| mode, VSphereCredentialsModePassthrough) | ||
| } | ||
| } | ||
|
|
||
| func TestVSpherePlatformSpec_CredentialsModeUnset_DefaultsPassthrough(t *testing.T) { | ||
| spec := VSpherePlatformSpec{} | ||
| if spec.CredentialsMode != VSphereCredentialsModePassthrough { | ||
| t.Errorf("unset CredentialsMode = %q; want Passthrough", spec.CredentialsMode) | ||
| } | ||
| } | ||
|
|
||
| func TestVSpherePlatformSpec_CredentialsModePassthrough(t *testing.T) { | ||
| spec := VSpherePlatformSpec{ | ||
| CredentialsMode: VSphereCredentialsModePassthrough, | ||
| } | ||
| if spec.CredentialsMode != VSphereCredentialsModePassthrough { | ||
| t.Errorf("CredentialsMode = %q; want Passthrough", spec.CredentialsMode) | ||
| } | ||
| } | ||
|
|
||
| func TestVSpherePlatformSpec_CredentialsModePerComponent(t *testing.T) { | ||
| spec := VSpherePlatformSpec{ | ||
| CredentialsMode: VSphereCredentialsModePerComponent, | ||
| } | ||
| if spec.CredentialsMode != VSphereCredentialsModePerComponent { | ||
| t.Errorf("CredentialsMode = %q; want PerComponent", spec.CredentialsMode) | ||
| } | ||
| } | ||
|
|
||
| // AC2: Existing cluster without credentialsMode → Passthrough, no regression | ||
|
|
||
| func TestVSpherePlatformSpec_JSONRoundTrip_NoCredentialsMode(t *testing.T) { | ||
| raw := `{"vcenters": [{"server": "vcenter.example.com", "port": 443, "datacenters": ["DC1"]}]}` | ||
|
|
||
| var spec VSpherePlatformSpec | ||
| if err := json.Unmarshal([]byte(raw), &spec); err != nil { | ||
| t.Fatalf("unexpected unmarshal error: %v", err) | ||
| } | ||
| if spec.CredentialsMode != VSphereCredentialsModePassthrough { | ||
| t.Errorf("credentialsMode after unmarshal of legacy JSON = %q; want Passthrough", spec.CredentialsMode) | ||
| } | ||
| if spec.ComponentCredentials != nil { | ||
| t.Errorf("ComponentCredentials should be nil for legacy JSON; got %+v", spec.ComponentCredentials) | ||
| } | ||
| } | ||
|
|
||
| func TestVSpherePlatformSpec_JSONRoundTrip_ExplicitPassthrough(t *testing.T) { | ||
| // VSphereCredentialsModePassthrough == "" so the canonical representation is omitting the field. | ||
| // An explicitly empty credentialsMode field is equally valid and must round-trip as Passthrough. | ||
| raw := `{"credentialsMode": ""}` | ||
|
|
||
| var spec VSpherePlatformSpec | ||
| if err := json.Unmarshal([]byte(raw), &spec); err != nil { | ||
| t.Fatalf("unexpected unmarshal error: %v", err) | ||
| } | ||
| if spec.CredentialsMode != VSphereCredentialsModePassthrough { | ||
| t.Errorf("credentialsMode = %q; want Passthrough (empty string)", spec.CredentialsMode) | ||
| } | ||
| } | ||
|
|
||
| // AC3: PerComponent mode → component credentials accessible | ||
|
|
||
| func TestVSpherePlatformSpec_PerComponent_AllComponents(t *testing.T) { | ||
| spec := VSpherePlatformSpec{ | ||
| CredentialsMode: VSphereCredentialsModePerComponent, | ||
| ComponentCredentials: &VSphereComponentCredentials{ | ||
| MachineAPI: &VSphereComponentSecretRef{ | ||
| Name: "vsphere-machine-api-creds", | ||
| Namespace: "openshift-machine-api", | ||
| }, | ||
| CSIDriver: &VSphereComponentSecretRef{ | ||
| Name: "vsphere-storage-creds", | ||
| Namespace: "openshift-cluster-csi-drivers", | ||
| }, | ||
| CloudController: &VSphereComponentSecretRef{ | ||
| Name: "vsphere-cloud-controller-creds", | ||
| Namespace: "openshift-cloud-controller-manager", | ||
| }, | ||
| VSphereProblemDetector: &VSphereComponentSecretRef{ | ||
| Name: "vsphere-problem-detector-creds", | ||
| Namespace: "openshift-cluster-storage-operator", | ||
| }, | ||
| }, | ||
| } | ||
|
|
||
| cc := spec.ComponentCredentials | ||
| if cc == nil { | ||
| t.Fatal("ComponentCredentials is nil; want non-nil") | ||
| } | ||
| if cc.MachineAPI == nil || cc.MachineAPI.Name != "vsphere-machine-api-creds" { | ||
| t.Error("MachineAPI credential missing or wrong name") | ||
| } | ||
| if cc.CSIDriver == nil || cc.CSIDriver.Name != "vsphere-storage-creds" { | ||
| t.Error("CSIDriver credential missing or wrong name") | ||
| } | ||
| if cc.CloudController == nil || cc.CloudController.Name != "vsphere-cloud-controller-creds" { | ||
| t.Error("CloudController credential missing or wrong name") | ||
| } | ||
| if cc.VSphereProblemDetector == nil || cc.VSphereProblemDetector.Name != "vsphere-problem-detector-creds" { | ||
| t.Error("VSphereProblemDetector credential missing or wrong name") | ||
| } | ||
| } | ||
|
|
||
| func TestVSpherePlatformSpec_PerComponent_PartialComponents(t *testing.T) { | ||
| spec := VSpherePlatformSpec{ | ||
| CredentialsMode: VSphereCredentialsModePerComponent, | ||
| ComponentCredentials: &VSphereComponentCredentials{ | ||
| MachineAPI: &VSphereComponentSecretRef{ | ||
| Name: "vsphere-machine-api-creds", | ||
| Namespace: "openshift-machine-api", | ||
| }, | ||
| }, | ||
| } | ||
| cc := spec.ComponentCredentials | ||
| if cc.MachineAPI == nil { | ||
| t.Error("MachineAPI should be non-nil") | ||
| } | ||
| if cc.CSIDriver != nil { | ||
| t.Errorf("CSIDriver should be nil; got %+v", cc.CSIDriver) | ||
| } | ||
| if cc.CloudController != nil { | ||
| t.Errorf("CloudController should be nil; got %+v", cc.CloudController) | ||
| } | ||
| if cc.VSphereProblemDetector != nil { | ||
| t.Errorf("VSphereProblemDetector should be nil; got %+v", cc.VSphereProblemDetector) | ||
| } | ||
| } | ||
|
|
||
| func TestVSpherePlatformSpec_PerComponent_JSONRoundTrip(t *testing.T) { | ||
| original := VSpherePlatformSpec{ | ||
| CredentialsMode: VSphereCredentialsModePerComponent, | ||
| ComponentCredentials: &VSphereComponentCredentials{ | ||
| MachineAPI: &VSphereComponentSecretRef{ | ||
| Name: "vsphere-machine-api-creds", | ||
| Namespace: "openshift-machine-api", | ||
| }, | ||
| }, | ||
| } | ||
|
|
||
| data, err := json.Marshal(original) | ||
| if err != nil { | ||
| t.Fatalf("marshal error: %v", err) | ||
| } | ||
|
|
||
| var roundTripped VSpherePlatformSpec | ||
| if err := json.Unmarshal(data, &roundTripped); err != nil { | ||
| t.Fatalf("unmarshal error: %v", err) | ||
| } | ||
| if roundTripped.CredentialsMode != VSphereCredentialsModePerComponent { | ||
| t.Errorf("credentialsMode after round-trip = %q; want PerComponent", roundTripped.CredentialsMode) | ||
| } | ||
| if roundTripped.ComponentCredentials == nil || roundTripped.ComponentCredentials.MachineAPI == nil { | ||
| t.Error("MachineAPI not preserved in round-trip") | ||
| } | ||
| if roundTripped.ComponentCredentials.MachineAPI.Name != "vsphere-machine-api-creds" { | ||
| t.Errorf("Name after round-trip = %q; want vsphere-machine-api-creds", | ||
| roundTripped.ComponentCredentials.MachineAPI.Name) | ||
| } | ||
| } | ||
|
|
||
| // Adversarial cases | ||
|
|
||
| func TestVSpherePlatformSpec_EmptyStringCredentialsMode(t *testing.T) { | ||
| raw := `{"credentialsMode": ""}` | ||
| var spec VSpherePlatformSpec | ||
| if err := json.Unmarshal([]byte(raw), &spec); err != nil { | ||
| t.Fatalf("unmarshal error: %v", err) | ||
| } | ||
| if spec.CredentialsMode != VSphereCredentialsModePassthrough { | ||
| t.Errorf("empty credentialsMode = %q; want Passthrough", spec.CredentialsMode) | ||
| } | ||
| } | ||
|
|
||
| func TestVSpherePlatformSpec_ComponentCredentials_NilPointerSafety(t *testing.T) { | ||
| spec := VSpherePlatformSpec{ | ||
| CredentialsMode: VSphereCredentialsModePassthrough, | ||
| ComponentCredentials: nil, | ||
| } | ||
| if spec.ComponentCredentials != nil { | ||
| t.Fatalf("expected nil ComponentCredentials") | ||
| } | ||
| var machineAPICreds *VSphereComponentSecretRef | ||
| if spec.ComponentCredentials != nil { | ||
| machineAPICreds = spec.ComponentCredentials.MachineAPI | ||
| } | ||
| _ = machineAPICreds | ||
| } | ||
|
|
||
| func TestVSphereComponentSecretRef_EmptyNamespace(t *testing.T) { | ||
| ref := VSphereComponentSecretRef{Name: "some-secret", Namespace: ""} | ||
| if ref.Name != "some-secret" { | ||
| t.Errorf("Name = %q; want some-secret", ref.Name) | ||
| } | ||
| } | ||
|
|
||
| func TestVSphereComponentSecretRef_EmptyName(t *testing.T) { | ||
| ref := VSphereComponentSecretRef{Name: "", Namespace: "openshift-machine-api"} | ||
| if ref.Namespace != "openshift-machine-api" { | ||
| t.Errorf("Namespace = %q; want openshift-machine-api", ref.Namespace) | ||
| } | ||
| } | ||
|
|
||
| func TestVSpherePlatformSpec_DeepCopy_PointerIsolation(t *testing.T) { | ||
| original := &VSpherePlatformSpec{ | ||
| CredentialsMode: VSphereCredentialsModePerComponent, | ||
| ComponentCredentials: &VSphereComponentCredentials{ | ||
| MachineAPI: &VSphereComponentSecretRef{ | ||
| Name: "vsphere-machine-api-creds", | ||
| Namespace: "openshift-machine-api", | ||
| }, | ||
| }, | ||
| } | ||
| copied := original.DeepCopy() | ||
| original.ComponentCredentials.MachineAPI.Name = "mutated" | ||
| if copied.ComponentCredentials.MachineAPI.Name == "mutated" { | ||
| t.Error("DeepCopy shares pointer with original: mutation of original affected copy") | ||
| } | ||
| } |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
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.
Enforce non-empty secret reference fields
NameandNamespaceare marked required, but empty strings are still valid without length constraints. That allows invalid secret refs to pass schema validation and fail later at runtime. Please addMinLength=1on both fields (Line 1676, Line 1680).Suggested diff
type VSphereComponentSecretRef struct { // name is the name of the secret. // +kubebuilder:validation:Required + // +kubebuilder:validation:MinLength=1 Name string `json:"name"` // namespace is the namespace of the secret. // +kubebuilder:validation:Required + // +kubebuilder:validation:MinLength=1 Namespace string `json:"namespace"` }📝 Committable suggestion
🤖 Prompt for AI Agents