-
Notifications
You must be signed in to change notification settings - Fork 295
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
⚠️ Allow specifying a different credentials per VSphereMachine #1743
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -554,29 +554,25 @@ func (r vmReconciler) retrieveVcenterSession(ctx goctx.Context, vsphereVM *infra | |
params := session.NewParams(). | ||
WithServer(vsphereVM.Spec.Server). | ||
WithDatacenter(vsphereVM.Spec.Datacenter). | ||
WithUserInfo(r.ControllerContext.Username, r.ControllerContext.Password). | ||
WithThumbprint(vsphereVM.Spec.Thumbprint). | ||
WithFeatures(session.Feature{ | ||
EnableKeepAlive: r.EnableKeepAlive, | ||
KeepAliveDuration: r.KeepAliveDuration, | ||
}) | ||
cluster, err := clusterutilv1.GetClusterFromMetadata(r.ControllerContext, r.Client, vsphereVM.ObjectMeta) | ||
|
||
vsphereCluster, err := r.retrieveCluster(vsphereVM) | ||
if err != nil { | ||
r.Logger.Info("VsphereVM is missing cluster label or cluster does not exist") | ||
return session.GetOrCreate(r.Context, | ||
params) | ||
return nil, err | ||
} | ||
|
||
key := ctrlclient.ObjectKey{ | ||
Namespace: cluster.Namespace, | ||
Name: cluster.Spec.InfrastructureRef.Name, | ||
} | ||
vsphereCluster := &infrav1.VSphereCluster{} | ||
err = r.Client.Get(r, key, vsphereCluster) | ||
if err != nil { | ||
r.Logger.Info("VSphereCluster couldn't be retrieved") | ||
return session.GetOrCreate(r.Context, | ||
params) | ||
if vsphereVM.Spec.IdentityRef != nil { | ||
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. Could we document the behaviour at the (not existing) function godoc? |
||
creds, err := identity.GetCredentialsWithExternalIdentity(ctx, r.Client, vsphereCluster, vsphereVM.Spec.IdentityRef, r.Namespace) | ||
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. Wouldn't this get overwritten below in l 578 when there is a definition on the cluster level already? |
||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
params = params.WithUserInfo(creds.Username, creds.Password) | ||
return session.GetOrCreate(ctx, params) | ||
} | ||
|
||
if vsphereCluster.Spec.IdentityRef != nil { | ||
|
@@ -589,11 +585,32 @@ func (r vmReconciler) retrieveVcenterSession(ctx goctx.Context, vsphereVM *infra | |
params) | ||
} | ||
|
||
params = params.WithUserInfo(r.ControllerContext.Username, r.ControllerContext.Password) | ||
// Fallback to using credentials provided to the manager | ||
return session.GetOrCreate(r.Context, | ||
params) | ||
} | ||
|
||
func (r vmReconciler) retrieveCluster(vsphereVM *infrav1.VSphereVM) (*infrav1.VSphereCluster, error) { | ||
cluster, err := clusterutilv1.GetClusterFromMetadata(r.ControllerContext, r.Client, vsphereVM.ObjectMeta) | ||
if err != nil { | ||
r.Logger.Info("VsphereVM is missing cluster label or cluster does not exist") | ||
return nil, err | ||
} | ||
|
||
key := ctrlclient.ObjectKey{ | ||
Namespace: cluster.Namespace, | ||
Name: cluster.Spec.InfrastructureRef.Name, | ||
} | ||
vsphereCluster := &infrav1.VSphereCluster{} | ||
err = r.Client.Get(r, key, vsphereCluster) | ||
if err != nil { | ||
r.Logger.Info("VSphereCluster couldn't be retrieved") | ||
return nil, err | ||
} | ||
return vsphereCluster, nil | ||
} | ||
|
||
func (r vmReconciler) fetchClusterModuleInfo(clusterModInput fetchClusterModuleInput) (*string, error) { | ||
var ( | ||
owner ctrlclient.Object | ||
|
Original file line number | Diff line number | Diff line change | ||||||||
---|---|---|---|---|---|---|---|---|---|---|
@@ -0,0 +1,39 @@ | ||||||||||
# Multi VCenter support | ||||||||||
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. Is most of this better suited to Also should ahve some explanation how it works in https://github.com/kubernetes-sigs/cluster-api-provider-vsphere/blob/main/docs/identity_management.md ? |
||||||||||
|
||||||||||
Cluster API Provider vSphere (CAPV) supports multiple VCenter for a single. Therefore CAPV is allowing to define the used identity for each machine. CAPV will check on every Machine first, if there is a local identity otherwise it fallback on the default selection method. | ||||||||||
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.
Suggested change
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. Also what does the first sentence mean?
|
||||||||||
|
||||||||||
In order to run a CAPV cluster in multiple VCenter, you have to configure CPI & CSI to support multi VCenter, see [guide](https://docs.vmware.com/en/VMware-vSphere-Container-Storage-Plug-in/3.0/vmware-vsphere-csp-getting-started/GUID-8B3B9004-DE37-4E6B-9AA1-234CDA1BD7F9.html). Trivia, `VSphereCluster` can be only in single VCenter. This will just used as a fallback, if you haven't configured a different identity for a `VSphereMachine``. | ||||||||||
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. This only links csi, are there also instructions for CPI? |
||||||||||
|
||||||||||
## Examples | ||||||||||
|
||||||||||
Deploy a `VSphereMachine` with a custom identityRef: | ||||||||||
|
||||||||||
```yaml | ||||||||||
apiVersion: infrastructure.cluster.x-k8s.io/v1beta1 | ||||||||||
kind: VSphereMachine | ||||||||||
metadata: | ||||||||||
name: new-workload-cluster | ||||||||||
spec: | ||||||||||
server: vcenter | ||||||||||
identityRef: | ||||||||||
kind: VSphereClusterIdentity | ||||||||||
name: identityName | ||||||||||
... | ||||||||||
``` | ||||||||||
|
||||||||||
Deploy a `VSphereMachineTemplate` with a custom identityRef: | ||||||||||
|
||||||||||
```yaml | ||||||||||
apiVersion: infrastructure.cluster.x-k8s.io/v1beta1 | ||||||||||
kind: VSphereMachineTemplate | ||||||||||
metadata: | ||||||||||
name: new-workload-cluster | ||||||||||
spec: | ||||||||||
template: | ||||||||||
spec: | ||||||||||
server: vcenter | ||||||||||
identityRef: | ||||||||||
Comment on lines
+34
to
+35
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.
Suggested change
|
||||||||||
kind: VSphereClusterIdentity | ||||||||||
name: identityName | ||||||||||
... | ||||||||||
``` |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -40,12 +40,11 @@ type Credentials struct { | |
Password string | ||
} | ||
|
||
func GetCredentials(ctx context.Context, c client.Client, cluster *infrav1.VSphereCluster, controllerNamespace string) (*Credentials, error) { | ||
if err := validateInputs(c, cluster); err != nil { | ||
func GetCredentialsWithExternalIdentity(ctx context.Context, c client.Client, cluster *infrav1.VSphereCluster, ref *infrav1.VSphereIdentityReference, controllerNamespace string) (*Credentials, error) { | ||
if err := validateInputs(c, cluster, ref); err != nil { | ||
return nil, err | ||
} | ||
|
||
ref := cluster.Spec.IdentityRef | ||
secret := &apiv1.Secret{} | ||
var secretKey client.ObjectKey | ||
|
||
|
@@ -108,15 +107,21 @@ func GetCredentials(ctx context.Context, c client.Client, cluster *infrav1.VSphe | |
return credentials, nil | ||
} | ||
|
||
func validateInputs(c client.Client, cluster *infrav1.VSphereCluster) error { | ||
func GetCredentials(ctx context.Context, c client.Client, cluster *infrav1.VSphereCluster, controllerNamespace string) (*Credentials, error) { | ||
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. unit tests would be helpful |
||
if err := validateInputs(c, cluster, cluster.Spec.IdentityRef); err != nil { | ||
return nil, err | ||
} | ||
return GetCredentialsWithExternalIdentity(ctx, c, cluster, cluster.Spec.IdentityRef, controllerNamespace) | ||
} | ||
|
||
func validateInputs(c client.Client, cluster *infrav1.VSphereCluster, identityRef *infrav1.VSphereIdentityReference) error { | ||
if c == nil { | ||
return errors.New("kubernetes client is required") | ||
} | ||
if cluster == nil { | ||
return errors.New("vsphere cluster is required") | ||
} | ||
ref := cluster.Spec.IdentityRef | ||
if ref == nil { | ||
if identityRef == nil { | ||
return errors.New("IdentityRef is required") | ||
} | ||
return nil | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -236,19 +236,19 @@ var _ = Describe("validateInputs", func() { | |
|
||
Context("If the client is missing", func() { | ||
It("should error if client is missing", func() { | ||
Expect(validateInputs(nil, cluster)).NotTo(Succeed()) | ||
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. would need test cases for the new behavior |
||
Expect(validateInputs(nil, cluster, nil)).NotTo(Succeed()) | ||
}) | ||
}) | ||
|
||
Context("If the cluster is missing", func() { | ||
It("should error if cluster is missing", func() { | ||
Expect(validateInputs(k8sclient, nil)).NotTo(Succeed()) | ||
Expect(validateInputs(k8sclient, nil, nil)).NotTo(Succeed()) | ||
}) | ||
}) | ||
|
||
Context("If the identityRef is missing on cluster", func() { | ||
It("should error if identityRef is missing on cluster", func() { | ||
Expect(validateInputs(k8sclient, cluster)).NotTo(Succeed()) | ||
Expect(validateInputs(k8sclient, cluster, nil)).NotTo(Succeed()) | ||
}) | ||
}) | ||
}) | ||
|
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.
Should we also add information about the fallback here?