-
Notifications
You must be signed in to change notification settings - Fork 295
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: killianmuldoon <kmuldoon@vmware.com>
- Loading branch information
1 parent
957d695
commit d288e6d
Showing
5 changed files
with
246 additions
and
57 deletions.
There are no files selected for viewing
This file contains 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 was deleted.
Oops, something went wrong.
This file contains 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 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 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,185 @@ | ||
package helpers | ||
|
||
import ( | ||
"fmt" | ||
"reflect" | ||
"sort" | ||
|
||
"github.com/pkg/errors" | ||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
"k8s.io/apimachinery/pkg/runtime/schema" | ||
kerrors "k8s.io/apimachinery/pkg/util/errors" | ||
clusterv1 "sigs.k8s.io/cluster-api/api/v1beta1" | ||
bootstrapv1 "sigs.k8s.io/cluster-api/bootstrap/kubeadm/api/v1beta1" | ||
controlplanev1 "sigs.k8s.io/cluster-api/controlplane/kubeadm/api/v1beta1" | ||
addonsv1 "sigs.k8s.io/cluster-api/exp/addons/api/v1beta1" | ||
|
||
infrav1 "sigs.k8s.io/cluster-api-provider-vsphere/apis/v1beta1" | ||
) | ||
|
||
// TODO: Two owners on vsphereMachines. | ||
// ClusterResourceSetBinding has multiple owners. | ||
// Missing the following types as they are not created as part of this test: | ||
// VSphereClusterIdentityKind = "VSphereClusterIdentity" | ||
// VSphereFailureDomainsKind = "VSphereFailureDomains" | ||
// vSphereDeploymentZonesKind = "vSphereDeploymentZones" | ||
// vSphereClusterTemplateKind = "VSphereClusterTemplate" | ||
|
||
// KubernetesReferenceAssertions maps Kubernetes types to functions which return an error if the passed OwnerReferences | ||
// aren't as expected. | ||
var ( | ||
VSphereKubernetesReferenceAssertions = map[string]func([]metav1.OwnerReference) error{ | ||
|
||
secretKind: func(owners []metav1.OwnerReference) error { | ||
// Secrets for cluster certificates must be owned by the KubeadmControlPlane. The bootstrap secret should be owned by a KubeadmControlPlane. | ||
return HasOneOfExactOwnersByGVK(owners, | ||
[]schema.GroupVersionKind{kubeadmControlPlaneGVK}, | ||
[]schema.GroupVersionKind{kubeadmConfigGVK}, | ||
[]schema.GroupVersionKind{clusterResourceSetGVK}, | ||
[]schema.GroupVersionKind{vSphereClusterGVK}) | ||
}, | ||
configMapKind: func(owners []metav1.OwnerReference) error { | ||
// The only configMaps considered here are those owned by a ClusterResourceSet. | ||
return HasOneOfExactOwnersByGVK(owners, []schema.GroupVersionKind{clusterResourceSetGVK}) | ||
}, | ||
} | ||
|
||
VSphereExpOwnerReferenceAssertions = map[string]func([]metav1.OwnerReference) error{ | ||
clusterResourceSetKind: func(owners []metav1.OwnerReference) error { | ||
// ClusterResourcesSet doesn't have ownerReferences (it is a clusterctl move-hierarchy root). | ||
return HasExactOwnersByGVK(owners, []schema.GroupVersionKind{}) | ||
}, | ||
// ClusterResourcesSetBinding has ClusterResourceSet set as owners on creation. | ||
clusterResourceSetBindingKind: func(owners []metav1.OwnerReference) error { | ||
return HasOneOfExactOwnersByGVK(owners, []schema.GroupVersionKind{clusterResourceSetGVK}, []schema.GroupVersionKind{clusterResourceSetGVK, clusterResourceSetGVK}) | ||
}, | ||
// MachinePool must be owned by a Cluster. | ||
machinePoolKind: func(owners []metav1.OwnerReference) error { | ||
// MachinePools must be owned by a Cluster. | ||
return HasExactOwnersByGVK(owners, []schema.GroupVersionKind{clusterGVK}) | ||
}, | ||
} | ||
) | ||
|
||
var ( | ||
VSphereClusterIdentityKind = "VSphereClusterIdentity" | ||
VSphereFailureDomainsKind = "VSphereFailureDomains" | ||
vSphereDeploymentZonesKind = "vSphereDeploymentZones" | ||
|
||
vSphereClusterKind = "VSphereCluster" | ||
vSphereClusterTemplateKind = "VSphereClusterTemplate" | ||
|
||
vSphereMachineKind = "VSphereMachine" | ||
vSphereMachineTemplateKind = "VSphereMachineTemplate" | ||
vSphereVMKind = "VSphereVM" | ||
|
||
vSphereMachineGVK = infrav1.GroupVersion.WithKind(vSphereMachineKind) | ||
|
||
vSphereClusterGVK = infrav1.GroupVersion.WithKind(vSphereClusterKind) | ||
VSphereReferenceAssertions = map[string]func([]metav1.OwnerReference) error{ | ||
vSphereClusterKind: func(owners []metav1.OwnerReference) error { | ||
// The only configMaps considered here are those owned by a ClusterResourceSet. | ||
return HasOneOfExactOwnersByGVK(owners, []schema.GroupVersionKind{clusterGVK}) | ||
}, | ||
vSphereClusterTemplateKind: func(owners []metav1.OwnerReference) error { | ||
return HasOneOfExactOwnersByGVK(owners, []schema.GroupVersionKind{clusterClassGVK}) | ||
}, | ||
vSphereMachineKind: func(owners []metav1.OwnerReference) error { | ||
// The only configMaps considered here are those owned by a ClusterResourceSet. | ||
return HasOneOfExactOwnersByGVK(owners, []schema.GroupVersionKind{vSphereClusterGVK, machineGVK}) | ||
}, | ||
vSphereMachineTemplateKind: func(owners []metav1.OwnerReference) error { | ||
// The only configMaps considered here are those owned by a ClusterResourceSet. | ||
return HasOneOfExactOwnersByGVK(owners, []schema.GroupVersionKind{clusterGVK}, []schema.GroupVersionKind{clusterClassGVK}) | ||
}, | ||
vSphereVMKind: func(owners []metav1.OwnerReference) error { | ||
// The only configMaps considered here are those owned by a ClusterResourceSet. | ||
return HasOneOfExactOwnersByGVK(owners, []schema.GroupVersionKind{vSphereMachineGVK}) | ||
}, | ||
VSphereClusterIdentityKind: func(owners []metav1.OwnerReference) error { return errors.New("IMPLEMENT ME") }, | ||
VSphereFailureDomainsKind: func(owners []metav1.OwnerReference) error { return errors.New("IMPLEMENT ME") }, | ||
vSphereDeploymentZonesKind: func(owners []metav1.OwnerReference) error { return errors.New("IMPLEMENT ME") }, | ||
} | ||
) | ||
|
||
func HasExactOwnersByGVK(refList []metav1.OwnerReference, wantGVKs []schema.GroupVersionKind) error { | ||
refGVKs := []schema.GroupVersionKind{} | ||
for _, ref := range refList { | ||
refGVK, err := ownerRefGVK(ref) | ||
if err != nil { | ||
return err | ||
} | ||
refGVKs = append(refGVKs, refGVK) | ||
} | ||
sort.SliceStable(refGVKs, func(i int, j int) bool { | ||
return refGVKs[i].String() > refGVKs[j].String() | ||
}) | ||
sort.SliceStable(wantGVKs, func(i int, j int) bool { | ||
return wantGVKs[i].String() > wantGVKs[j].String() | ||
}) | ||
if !reflect.DeepEqual(wantGVKs, refGVKs) { | ||
return fmt.Errorf("wanted %v, actual %v", wantGVKs, refGVKs) | ||
} | ||
return nil | ||
} | ||
|
||
// NOTE: we are using HasOneOfExactOwnersByGVK as a convenience approach for checking owner references on objects that | ||
Check failure on line 126 in test/helpers/ownerreference_helpers.go GitHub Actions / lint
|
||
// can have different owner references depending on the cluster topology. | ||
// In a follow-up iteration we can make improvements to check owner references according to the specific use cases vs checking generically "oneOf". | ||
func HasOneOfExactOwnersByGVK(refList []metav1.OwnerReference, possibleGVKS ...[]schema.GroupVersionKind) error { | ||
var allErrs []error | ||
for _, wantGVK := range possibleGVKS { | ||
err := HasExactOwnersByGVK(refList, wantGVK) | ||
if err != nil { | ||
allErrs = append(allErrs, err) | ||
continue | ||
} | ||
return nil | ||
} | ||
return kerrors.NewAggregate(allErrs) | ||
} | ||
|
||
func ownerRefGVK(ref metav1.OwnerReference) (schema.GroupVersionKind, error) { | ||
refGV, err := schema.ParseGroupVersion(ref.APIVersion) | ||
if err != nil { | ||
return schema.GroupVersionKind{}, err | ||
} | ||
return schema.GroupVersionKind{Version: refGV.Version, Group: refGV.Group, Kind: ref.Kind}, nil | ||
} | ||
|
||
var ( | ||
clusterKind = "Cluster" | ||
clusterClassKind = "ClusterClass" | ||
|
||
machineKind = "Machine" | ||
|
||
clusterGVK = clusterv1.GroupVersion.WithKind(clusterKind) | ||
clusterClassGVK = clusterv1.GroupVersion.WithKind(clusterClassKind) | ||
|
||
machineGVK = clusterv1.GroupVersion.WithKind(machineKind) | ||
) | ||
|
||
var ( | ||
clusterResourceSetKind = "ClusterResourceSet" | ||
clusterResourceSetBindingKind = "ClusterResourceSetBinding" | ||
machinePoolKind = "MachinePool" | ||
|
||
clusterResourceSetGVK = addonsv1.GroupVersion.WithKind(clusterResourceSetKind) | ||
) | ||
|
||
var ( | ||
configMapKind = "ConfigMap" | ||
secretKind = "Secret" | ||
) | ||
|
||
var ( | ||
kubeadmControlPlaneKind = "KubeadmControlPlane" | ||
|
||
kubeadmControlPlaneGVK = controlplanev1.GroupVersion.WithKind(kubeadmControlPlaneKind) | ||
) | ||
|
||
var ( | ||
kubeadmConfigKind = "KubeadmConfig" | ||
|
||
kubeadmConfigGVK = bootstrapv1.GroupVersion.WithKind(kubeadmConfigKind) | ||
) |