Skip to content

Commit 46be1c4

Browse files
authored
Merge pull request #206 from Fedosin/core-provider-name
🌱 Add a preflight check to validate core provider name
2 parents 468ca98 + b555767 commit 46be1c4

File tree

3 files changed

+66
-12
lines changed

3 files changed

+66
-12
lines changed

api/v1alpha1/conditions_consts.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,9 @@ const (
2929
// IncorrectVersionFormatReason documents that the provider version is in the incorrect format.
3030
IncorrectVersionFormatReason = "IncorrectVersionFormat"
3131

32+
// IncorrectCoreProviderNameReason documents that the provider name is incorrect.
33+
IncorrectCoreProviderNameReason = "IncorrectCoreProviderNameReason"
34+
3235
// EmptyVersionReason documents that the provider version is in the incorrect format.
3336
EmptyVersionReason = "EmptyVersionReason"
3437

internal/controller/preflight_checks.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ var (
4646
capiVersionIncompatibilityMessage = "CAPI operator is only compatible with %s providers, detected %s for provider %s."
4747
invalidGithubTokenMessage = "Invalid github token, please check your github token value and its permissions" //nolint:gosec
4848
waitingForCoreProviderReadyMessage = "Waiting for the core provider to be installed."
49+
incorrectCoreProviderNameMessage = "Incorrect CoreProvider name: %s. It should be %s"
4950
)
5051

5152
// preflightChecks performs preflight checks before installing provider.
@@ -71,6 +72,20 @@ func preflightChecks(ctx context.Context, c client.Client, provider genericprovi
7172
}
7273
}
7374

75+
// Ensure that the CoreProvider is called "cluster-api".
76+
if util.IsCoreProvider(provider) {
77+
if provider.GetName() != configclient.ClusterAPIProviderName {
78+
conditions.Set(provider, conditions.FalseCondition(
79+
operatorv1.PreflightCheckCondition,
80+
operatorv1.IncorrectCoreProviderNameReason,
81+
clusterv1.ConditionSeverityError,
82+
fmt.Sprintf(incorrectCoreProviderNameMessage, provider.GetName(), configclient.ClusterAPIProviderName),
83+
))
84+
85+
return ctrl.Result{}, fmt.Errorf("incorrect CoreProvider name: %s, it should be %s", provider.GetName(), configclient.ClusterAPIProviderName)
86+
}
87+
}
88+
7489
// Check that if a predefined provider is being installed, and if it's not - ensure that FetchConfig is specified.
7590
isPredefinedProvider, err := isPredefinedProvider(provider.GetName(), util.ClusterctlProviderType(provider))
7691
if err != nil {

internal/controller/preflight_checks_test.go

Lines changed: 48 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,42 @@ func TestPreflightChecks(t *testing.T) {
7474
CoreProviderList: &operatorv1.CoreProviderList{},
7575
},
7676
},
77+
{
78+
name: "core provider with incorrect name, preflight check failed",
79+
expectedError: true,
80+
providers: []genericprovider.GenericProvider{
81+
&genericprovider.CoreProviderWrapper{
82+
CoreProvider: &operatorv1.CoreProvider{
83+
ObjectMeta: metav1.ObjectMeta{
84+
Name: "my-fancy-cluster-api",
85+
Namespace: namespaceName1,
86+
},
87+
TypeMeta: metav1.TypeMeta{
88+
Kind: "CoreProvider",
89+
APIVersion: "operator.cluster.x-k8s.io/v1alpha1",
90+
},
91+
Spec: operatorv1.CoreProviderSpec{
92+
ProviderSpec: operatorv1.ProviderSpec{
93+
Version: "v1.0.0",
94+
FetchConfig: &operatorv1.FetchConfiguration{
95+
URL: "https://example.com",
96+
},
97+
},
98+
},
99+
},
100+
},
101+
},
102+
expectedCondition: clusterv1.Condition{
103+
Type: operatorv1.PreflightCheckCondition,
104+
Reason: operatorv1.IncorrectCoreProviderNameReason,
105+
Severity: clusterv1.ConditionSeverityError,
106+
Message: "Incorrect CoreProvider name: my-fancy-cluster-api. It should be cluster-api",
107+
Status: corev1.ConditionFalse,
108+
},
109+
providerList: &genericprovider.CoreProviderListWrapper{
110+
CoreProviderList: &operatorv1.CoreProviderList{},
111+
},
112+
},
77113
{
78114
name: "two core providers were created, preflight check failed",
79115
expectedError: true,
@@ -562,20 +598,20 @@ func TestPreflightChecks(t *testing.T) {
562598
},
563599
},
564600
{
565-
name: "custom Core Provider without fetch config, preflight check failed",
601+
name: "custom Infrastructure Provider without fetch config, preflight check failed",
566602
expectedError: true,
567603
providers: []genericprovider.GenericProvider{
568-
&genericprovider.CoreProviderWrapper{
569-
CoreProvider: &operatorv1.CoreProvider{
604+
&genericprovider.InfrastructureProviderWrapper{
605+
InfrastructureProvider: &operatorv1.InfrastructureProvider{
570606
ObjectMeta: metav1.ObjectMeta{
571-
Name: "my-custom-cluster-api",
607+
Name: "my-custom-aws",
572608
Namespace: namespaceName1,
573609
},
574610
TypeMeta: metav1.TypeMeta{
575-
Kind: "CoreProvider",
611+
Kind: "InfrastructureProvider",
576612
APIVersion: "operator.cluster.x-k8s.io/v1alpha1",
577613
},
578-
Spec: operatorv1.CoreProviderSpec{
614+
Spec: operatorv1.InfrastructureProviderSpec{
579615
ProviderSpec: operatorv1.ProviderSpec{
580616
Version: "v1.0.0",
581617
},
@@ -595,20 +631,20 @@ func TestPreflightChecks(t *testing.T) {
595631
},
596632
},
597633
{
598-
name: "custom Core Provider with fetch config with empty values, preflight check failed",
634+
name: "custom Infrastructure Provider with fetch config with empty values, preflight check failed",
599635
expectedError: true,
600636
providers: []genericprovider.GenericProvider{
601-
&genericprovider.CoreProviderWrapper{
602-
CoreProvider: &operatorv1.CoreProvider{
637+
&genericprovider.InfrastructureProviderWrapper{
638+
InfrastructureProvider: &operatorv1.InfrastructureProvider{
603639
ObjectMeta: metav1.ObjectMeta{
604-
Name: "my-custom-cluster-api",
640+
Name: "my-custom-aws",
605641
Namespace: namespaceName1,
606642
},
607643
TypeMeta: metav1.TypeMeta{
608-
Kind: "CoreProvider",
644+
Kind: "InfrastructureProvider",
609645
APIVersion: "operator.cluster.x-k8s.io/v1alpha1",
610646
},
611-
Spec: operatorv1.CoreProviderSpec{
647+
Spec: operatorv1.InfrastructureProviderSpec{
612648
ProviderSpec: operatorv1.ProviderSpec{
613649
Version: "v1.0.0",
614650
FetchConfig: &operatorv1.FetchConfiguration{

0 commit comments

Comments
 (0)