@@ -28,11 +28,15 @@ import (
2828 "github.com/pkg/errors"
2929 corev1 "k8s.io/api/core/v1"
3030 metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
31+ "k8s.io/apimachinery/pkg/types"
3132 utilfeature "k8s.io/component-base/featuregate/testing"
3233 "k8s.io/utils/pointer"
3334 "sigs.k8s.io/controller-runtime/pkg/client"
35+ "sigs.k8s.io/controller-runtime/pkg/client/fake"
36+ "sigs.k8s.io/controller-runtime/pkg/reconcile"
3437
3538 clusterv1 "sigs.k8s.io/cluster-api/api/v1beta1"
39+ runtimev1 "sigs.k8s.io/cluster-api/exp/runtime/api/v1alpha1"
3640 runtimecatalog "sigs.k8s.io/cluster-api/exp/runtime/catalog"
3741 runtimehooksv1 "sigs.k8s.io/cluster-api/exp/runtime/hooks/api/v1alpha1"
3842 "sigs.k8s.io/cluster-api/feature"
@@ -545,3 +549,71 @@ func TestReconciler_reconcileVariables(t *testing.T) {
545549 })
546550 }
547551}
552+
553+ func TestReconciler_extensionConfigToClusterClass (t * testing.T ) {
554+ firstExtConfig := & runtimev1.ExtensionConfig {
555+ ObjectMeta : metav1.ObjectMeta {
556+ Name : "runtime1" ,
557+ },
558+ TypeMeta : metav1.TypeMeta {
559+ Kind : "ExtensionConfig" ,
560+ APIVersion : runtimev1 .GroupVersion .String (),
561+ },
562+ Spec : runtimev1.ExtensionConfigSpec {
563+ NamespaceSelector : & metav1.LabelSelector {},
564+ },
565+ }
566+ secondExtConfig := & runtimev1.ExtensionConfig {
567+ ObjectMeta : metav1.ObjectMeta {
568+ Name : "runtime2" ,
569+ },
570+ TypeMeta : metav1.TypeMeta {
571+ Kind : "ExtensionConfig" ,
572+ APIVersion : runtimev1 .GroupVersion .String (),
573+ },
574+ Spec : runtimev1.ExtensionConfigSpec {
575+ NamespaceSelector : & metav1.LabelSelector {},
576+ },
577+ }
578+
579+ // These ClusterClasses will be reconciled as they both reference the passed ExtensionConfig `runtime1`.
580+ onePatchClusterClass := builder .ClusterClass (metav1 .NamespaceDefault , "cc1" ).
581+ WithPatches ([]clusterv1.ClusterClassPatch {
582+ {External : & clusterv1.ExternalPatchDefinition {DiscoverVariablesExtension : pointer .String ("discover-variables.runtime1" )}}}).
583+ Build ()
584+ twoPatchClusterClass := builder .ClusterClass (metav1 .NamespaceDefault , "cc2" ).
585+ WithPatches ([]clusterv1.ClusterClassPatch {
586+ {External : & clusterv1.ExternalPatchDefinition {DiscoverVariablesExtension : pointer .String ("discover-variables.runtime1" )}},
587+ {External : & clusterv1.ExternalPatchDefinition {DiscoverVariablesExtension : pointer .String ("discover-variables.runtime2" )}}}).
588+ Build ()
589+
590+ // This ClusterClasses will not be reconciled as it does not reference the passed ExtensionConfig `runtime1`.
591+ notReconciledClusterClass := builder .ClusterClass (metav1 .NamespaceDefault , "cc3" ).
592+ WithPatches ([]clusterv1.ClusterClassPatch {
593+ {External : & clusterv1.ExternalPatchDefinition {DiscoverVariablesExtension : pointer .String ("discover-variables.other-runtime-class" )}}}).
594+ Build ()
595+
596+ t .Run ("test" , func (t * testing.T ) {
597+ fakeClient := fake .NewClientBuilder ().WithObjects (onePatchClusterClass , notReconciledClusterClass , twoPatchClusterClass ).Build ()
598+ r := & Reconciler {
599+ Client : fakeClient ,
600+ }
601+
602+ // Expect both onePatchClusterClass and twoPatchClusterClass to trigger a reconcile as both reference ExtensionCopnfig `runtime1`.
603+ firstExtConfigExpected := []reconcile.Request {
604+ {NamespacedName : types.NamespacedName {Namespace : onePatchClusterClass .Namespace , Name : onePatchClusterClass .Name }},
605+ {NamespacedName : types.NamespacedName {Namespace : twoPatchClusterClass .Namespace , Name : twoPatchClusterClass .Name }},
606+ }
607+ if got := r .extensionConfigToClusterClass (context .Background (), firstExtConfig ); ! reflect .DeepEqual (got , firstExtConfigExpected ) {
608+ t .Errorf ("extensionConfigToClusterClass() = %v, want %v" , got , firstExtConfigExpected )
609+ }
610+
611+ // Expect only twoPatchClusterClass to trigger a reconcile as it's the only class with a reference to ExtensionCopnfig `runtime2`.
612+ secondExtConfigExpected := []reconcile.Request {
613+ {NamespacedName : types.NamespacedName {Namespace : twoPatchClusterClass .Namespace , Name : twoPatchClusterClass .Name }},
614+ }
615+ if got := r .extensionConfigToClusterClass (context .Background (), secondExtConfig ); ! reflect .DeepEqual (got , secondExtConfigExpected ) {
616+ t .Errorf ("extensionConfigToClusterClass() = %v, want %v" , got , secondExtConfigExpected )
617+ }
618+ })
619+ }
0 commit comments