Skip to content

Commit

Permalink
Fix issues deploying vsphere-csi-driver
Browse files Browse the repository at this point in the history
Signed-off-by: Kyle Squizzato <[email protected]>
  • Loading branch information
squizzi committed Oct 2, 2024
1 parent b95c38f commit 00088a6
Show file tree
Hide file tree
Showing 5 changed files with 63 additions and 50 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -114,8 +114,10 @@ spec:
images:
driver:
tag: v3.1.2
repo: "registry.k8s.io/csi-vsphere/driver"
syncer:
tag: v3.1.2
repo: "registry.k8s.io/csi-vsphere/syncer"
machineTemplate:
infrastructureRef:
apiVersion: infrastructure.cluster.x-k8s.io/v1beta1
Expand Down
30 changes: 17 additions & 13 deletions test/e2e/kubeclient/kubeclient.go
Original file line number Diff line number Diff line change
Expand Up @@ -136,34 +136,38 @@ func newKubeClient(configBytes []byte, namespace string) *KubeClient {
}

// GetDynamicClient returns a dynamic client for the given GroupVersionResource.
func (kc *KubeClient) GetDynamicClient(gvr schema.GroupVersionResource) dynamic.ResourceInterface {
//
//nolint:revive
func (kc *KubeClient) GetDynamicClient(gvr schema.GroupVersionResource, namespaced bool) dynamic.ResourceInterface {
GinkgoHelper()

client, err := dynamic.NewForConfig(kc.Config)
Expect(err).NotTo(HaveOccurred(), "failed to create dynamic client for resource: %s", gvr.String())

if !namespaced {
return client.Resource(gvr)
}

return client.Resource(gvr).Namespace(kc.Namespace)
}

func (kc *KubeClient) CreateOrUpdateUnstructuredObject(gvr schema.GroupVersionResource, obj *unstructured.Unstructured) {
func (kc *KubeClient) CreateOrUpdateUnstructuredObject(gvr schema.GroupVersionResource, obj *unstructured.Unstructured, namespaced bool) {
GinkgoHelper()

client := kc.GetDynamicClient(gvr)
client := kc.GetDynamicClient(gvr, namespaced)

kind, name := utils.ObjKindName(obj)

_, _ = fmt.Fprintf(GinkgoWriter, "Creating or updating %s: %s: %+v\n with kubeclient: %+v", kind, name, obj, *kc)

_, err := client.Create(context.Background(), obj, metav1.CreateOptions{})
if apierrors.IsAlreadyExists(err) {
resp, err := client.Get(context.Background(), name, metav1.GetOptions{})
resp, err := client.Get(context.Background(), name, metav1.GetOptions{})
if apierrors.IsNotFound(err) {
_, err = client.Create(context.Background(), obj, metav1.CreateOptions{})
Expect(err).NotTo(HaveOccurred(), "failed to create %s: %s", kind, name)
} else {
Expect(err).NotTo(HaveOccurred(), "failed to get existing %s: %s", kind, name)

obj.SetResourceVersion(resp.GetResourceVersion())
_, err = client.Update(context.Background(), obj, metav1.UpdateOptions{})
Expect(err).NotTo(HaveOccurred(), "failed to update existing %s: %s", kind, name)
} else {
Expect(err).NotTo(HaveOccurred(), "failed to create %s: %s", kind, name)
}
}

Expand All @@ -182,7 +186,7 @@ func (kc *KubeClient) CreateManagedCluster(
Group: "hmc.mirantis.com",
Version: "v1alpha1",
Resource: "managedclusters",
})
}, true)

_, err := client.Create(ctx, managedcluster, metav1.CreateOptions{})
if !apierrors.IsAlreadyExists(err) {
Expand All @@ -206,7 +210,7 @@ func (kc *KubeClient) GetCluster(ctx context.Context, clusterName string) (*unst
Resource: "clusters",
}

client := kc.GetDynamicClient(gvr)
client := kc.GetDynamicClient(gvr, true)

cluster, err := client.Get(ctx, clusterName, metav1.GetOptions{})
if err != nil {
Expand All @@ -221,7 +225,7 @@ func (kc *KubeClient) GetCluster(ctx context.Context, clusterName string) (*unst
func (kc *KubeClient) listResource(
ctx context.Context, gvr schema.GroupVersionResource, clusterName string,
) ([]unstructured.Unstructured, error) {
client := kc.GetDynamicClient(gvr)
client := kc.GetDynamicClient(gvr, true)

resources, err := client.List(ctx, metav1.ListOptions{
LabelSelector: "cluster.x-k8s.io/cluster-name=" + clusterName,
Expand Down
2 changes: 1 addition & 1 deletion test/e2e/managedcluster/aws/aws.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,5 +98,5 @@ func getAWSClusterClient(kc *kubeclient.KubeClient) dynamic.ResourceInterface {
Group: "infrastructure.cluster.x-k8s.io",
Version: "v1beta2",
Resource: "awsclusters",
})
}, true)
}
2 changes: 1 addition & 1 deletion test/e2e/managedcluster/azure/azure.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ func getAzureInfo(ctx context.Context, name string, kc *kubeclient.KubeClient) m
Resource: "azureclusters",
}

dc := kc.GetDynamicClient(resourceID)
dc := kc.GetDynamicClient(resourceID, true)
list, err := dc.List(ctx, metav1.ListOptions{
LabelSelector: labels.SelectorFromSet(map[string]string{hmc.FluxHelmChartNameKey: name}).String(),
})
Expand Down
77 changes: 42 additions & 35 deletions test/e2e/managedcluster/clusteridentity/clusteridentity.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ type ClusterIdentity struct {
SecretName string
IdentityName string
SecretData map[string]string
Spec map[string]any
Namespaced bool
}

// New creates a ClusterIdentity resource, credential and associated secret for
Expand All @@ -50,8 +52,13 @@ func New(kc *kubeclient.KubeClient, provider managedcluster.ProviderType) *Clust
kind string
version string
secretStringData map[string]string
spec map[string]any
namespaced bool
)

secretName := fmt.Sprintf("%s-cluster-identity-secret", provider)
identityName := fmt.Sprintf("%s-cluster-identity", provider)

switch provider {
case managedcluster.ProviderAWS:
resource = "awsclusterstaticidentities"
Expand All @@ -61,13 +68,32 @@ func New(kc *kubeclient.KubeClient, provider managedcluster.ProviderType) *Clust
"AccessKeyID": os.Getenv(managedcluster.EnvVarAWSAccessKeyID),
"SecretAccessKey": os.Getenv(managedcluster.EnvVarAWSSecretAccessKey),
}
spec = map[string]any{
"secretRef": secretName,
"allowedNamespaces": map[string]any{
"selector": map[string]any{
"matchLabels": map[string]any{},
},
},
}
case managedcluster.ProviderAzure:
resource = "azureclusteridentities"
kind = "AzureClusterIdentity"
version = "v1beta1"
secretStringData = map[string]string{
"clientSecret": os.Getenv(managedcluster.EnvVarAzureClientSecret),
}
spec = map[string]any{
"allowedNamespaces": map[string]any{},
"clientID": os.Getenv(managedcluster.EnvVarAzureClientID),
"clientSecret": map[string]any{
"name": secretName,
"namespace": kc.Namespace,
},
"tenantID": os.Getenv(managedcluster.EnvVarAzureTenantID),
"type": "ServicePrincipal",
}
namespaced = true
case managedcluster.ProviderVSphere:
resource = "vsphereclusteridentities"
kind = "VSphereClusterIdentity"
Expand All @@ -76,24 +102,33 @@ func New(kc *kubeclient.KubeClient, provider managedcluster.ProviderType) *Clust
"username": os.Getenv(managedcluster.EnvVarVSphereUser),
"password": os.Getenv(managedcluster.EnvVarVSpherePassword),
}
spec = map[string]any{
"secretName": secretName,
"allowedNamespaces": map[string]any{
"selector": map[string]any{
"matchLabels": map[string]any{},
},
},
}
default:
Fail(fmt.Sprintf("Unsupported provider: %s", provider))
}

validateSecretDataPopulated(secretStringData)

ci := ClusterIdentity{
GroupVersionResource: schema.GroupVersionResource{
Group: "infrastructure.cluster.x-k8s.io",
Version: version,
Resource: resource,
},
Kind: kind,
SecretName: fmt.Sprintf("%s-cluster-identity-secret", provider),
IdentityName: fmt.Sprintf("%s-cluster-identity", provider),
SecretName: secretName,
IdentityName: identityName,
SecretData: secretStringData,
Spec: spec,
Namespaced: namespaced,
}

validateSecretDataPopulated(secretStringData)
ci.waitForResourceCRD(kc)
ci.createSecret(kc)
ci.createClusterIdentity(kc)
Expand Down Expand Up @@ -193,7 +228,7 @@ func (ci *ClusterIdentity) createCredential(kc *kubeclient.KubeClient) {
Group: "hmc.mirantis.com",
Version: "v1alpha1",
Resource: "credentials",
}, cred)
}, cred, true)
}

// createClusterIdentity creates a ClusterIdentity resource.
Expand All @@ -210,37 +245,9 @@ func (ci *ClusterIdentity) createClusterIdentity(kc *kubeclient.KubeClient) {
"name": ci.IdentityName,
"namespace": kc.Namespace,
},
"spec": map[string]any{
"secretRef": ci.SecretName,
"allowedNamespaces": map[string]any{
"selector": map[string]any{
"matchLabels": map[string]any{},
},
},
},
"spec": ci.Spec,
},
}

if ci.GroupVersionResource.Resource == "azureclusteridentities" {
err := unstructured.SetNestedField(id.Object, os.Getenv(managedcluster.EnvVarAzureTenantID), "spec", "tenantID")
Expect(err).NotTo(HaveOccurred())

managedcluster.ValidateDeploymentVars([]string{
managedcluster.EnvVarAzureClientID,
managedcluster.EnvVarAzureTenantID,
})

id.Object["spec"] = map[string]any{
"allowedNamespaces": map[string]any{},
"clientID": os.Getenv(managedcluster.EnvVarAzureClientID),
"clientSecret": map[string]any{
"name": ci.SecretName,
"namespace": kc.Namespace,
},
"tenantID": os.Getenv(managedcluster.EnvVarAzureTenantID),
"type": "ServicePrincipal",
}
}

kc.CreateOrUpdateUnstructuredObject(ci.GroupVersionResource, id)
kc.CreateOrUpdateUnstructuredObject(ci.GroupVersionResource, id, ci.Namespaced)
}

0 comments on commit 00088a6

Please sign in to comment.