Skip to content

Commit 07dc54c

Browse files
authored
Merge pull request #855 from Danil-Grigorev/fix-preflight-check-core
🐛 Fix preflight check for generic provider overlap with core
2 parents 47c4e4f + 7399ae7 commit 07dc54c

File tree

2 files changed

+125
-3
lines changed

2 files changed

+125
-3
lines changed

internal/controller/preflight_checks.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,7 @@ func preflightChecks(ctx context.Context, c client.Client, provider genericprovi
154154
)
155155

156156
// CoreProvider is a singleton resource, more than one instances should not exist
157-
if mapper(p) == clusterctlv1.CoreProviderType {
157+
if mapper(provider) == clusterctlv1.CoreProviderType && mapper(p) == clusterctlv1.CoreProviderType {
158158
log.Info(moreThanOneCoreProviderInstanceExistsMessage)
159159
preflightFalseCondition.Message = moreThanOneCoreProviderInstanceExistsMessage
160160
conditions.Set(provider, preflightFalseCondition)
@@ -163,7 +163,7 @@ func preflightChecks(ctx context.Context, c client.Client, provider genericprovi
163163
}
164164

165165
// For any other provider we should check that instances with similar name exist in any namespace
166-
if mapper(p) != clusterctlv1.CoreProviderType && p.GetName() == provider.GetName() {
166+
if mapper(p) != clusterctlv1.CoreProviderType && p.GetName() == provider.GetName() && mapper(p) == mapper(provider) {
167167
preflightFalseCondition.Message = fmt.Sprintf(moreThanOneProviderInstanceExistsMessage, p.GetName(), p.GetNamespace())
168168
log.Info(preflightFalseCondition.Message)
169169
conditions.Set(provider, preflightFalseCondition)

internal/controller/preflight_checks_test.go

Lines changed: 123 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ import (
2525
corev1 "k8s.io/api/core/v1"
2626
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
2727
clusterv1 "sigs.k8s.io/cluster-api/api/v1beta1"
28+
clusterctlv1 "sigs.k8s.io/cluster-api/cmd/clusterctl/api/v1alpha3"
2829
"sigs.k8s.io/controller-runtime/pkg/client/fake"
2930

3031
operatorv1 "sigs.k8s.io/cluster-api-operator/api/v1alpha2"
@@ -40,6 +41,7 @@ func TestPreflightChecks(t *testing.T) {
4041
name string
4142
providers []operatorv1.GenericProvider
4243
providerList genericprovider.GenericProviderList
44+
mapper ProviderTypeMapper
4345
expectedCondition clusterv1.Condition
4446
expectedError bool
4547
}{
@@ -147,6 +149,53 @@ func TestPreflightChecks(t *testing.T) {
147149
},
148150
providerList: &operatorv1.CoreProviderList{},
149151
},
152+
{
153+
name: "two core providers were created, but accepted due to custom mapper logic",
154+
providers: []operatorv1.GenericProvider{
155+
&operatorv1.CoreProvider{
156+
ObjectMeta: metav1.ObjectMeta{
157+
Name: "cluster-api",
158+
Namespace: namespaceName1,
159+
},
160+
TypeMeta: metav1.TypeMeta{
161+
Kind: "CoreProvider",
162+
APIVersion: "operator.cluster.x-k8s.io/v1alpha1",
163+
},
164+
Spec: operatorv1.CoreProviderSpec{
165+
ProviderSpec: operatorv1.ProviderSpec{
166+
Version: "v1.0.0",
167+
},
168+
},
169+
},
170+
&operatorv1.CoreProvider{
171+
ObjectMeta: metav1.ObjectMeta{
172+
Name: "core-3",
173+
Namespace: namespaceName1,
174+
},
175+
TypeMeta: metav1.TypeMeta{
176+
Kind: "CoreProvider",
177+
APIVersion: "operator.cluster.x-k8s.io/v1alpha1",
178+
},
179+
Spec: operatorv1.CoreProviderSpec{
180+
ProviderSpec: operatorv1.ProviderSpec{
181+
Version: "v1.0.0",
182+
},
183+
},
184+
},
185+
},
186+
expectedCondition: clusterv1.Condition{
187+
Type: operatorv1.PreflightCheckCondition,
188+
Status: corev1.ConditionTrue,
189+
},
190+
mapper: func(provider operatorv1.GenericProvider) clusterctlv1.ProviderType {
191+
if provider.GetName() == "core-3" {
192+
return clusterctlv1.ProviderTypeUnknown
193+
}
194+
195+
return clusterctlv1.CoreProviderType
196+
},
197+
providerList: &operatorv1.CoreProviderList{},
198+
},
150199
{
151200
name: "two core providers in two different namespaces were created, preflight check failed",
152201
expectedError: true,
@@ -473,6 +522,74 @@ func TestPreflightChecks(t *testing.T) {
473522
},
474523
providerList: &operatorv1.InfrastructureProviderList{},
475524
},
525+
{
526+
name: "two similarly named infra provider exist in different namespaces, but custom mapper returns differentiats types, preflight check passed",
527+
providers: []operatorv1.GenericProvider{
528+
&operatorv1.CoreProvider{
529+
ObjectMeta: metav1.ObjectMeta{
530+
Name: "cluster-api",
531+
Namespace: namespaceName1,
532+
},
533+
TypeMeta: metav1.TypeMeta{
534+
Kind: "CoreProvider",
535+
APIVersion: "operator.cluster.x-k8s.io/v1alpha1",
536+
},
537+
Spec: operatorv1.CoreProviderSpec{
538+
ProviderSpec: operatorv1.ProviderSpec{
539+
FetchConfig: &operatorv1.FetchConfiguration{
540+
URL: "https://example.com",
541+
},
542+
},
543+
},
544+
},
545+
&operatorv1.InfrastructureProvider{
546+
ObjectMeta: metav1.ObjectMeta{
547+
Name: "aws",
548+
Namespace: namespaceName1,
549+
},
550+
TypeMeta: metav1.TypeMeta{
551+
Kind: "InfrastructureProvider",
552+
APIVersion: "operator.cluster.x-k8s.io/v1alpha1",
553+
},
554+
Spec: operatorv1.InfrastructureProviderSpec{
555+
ProviderSpec: operatorv1.ProviderSpec{
556+
Version: "v1.0.0",
557+
},
558+
},
559+
},
560+
&operatorv1.InfrastructureProvider{
561+
ObjectMeta: metav1.ObjectMeta{
562+
Name: "aws",
563+
Namespace: namespaceName2,
564+
},
565+
TypeMeta: metav1.TypeMeta{
566+
Kind: "InfrastructureProvider",
567+
APIVersion: "operator.cluster.x-k8s.io/v1alpha1",
568+
},
569+
Spec: operatorv1.InfrastructureProviderSpec{
570+
ProviderSpec: operatorv1.ProviderSpec{
571+
Version: "v1.0.0",
572+
},
573+
},
574+
},
575+
},
576+
mapper: func(provider operatorv1.GenericProvider) clusterctlv1.ProviderType {
577+
if provider.GetNamespace() == namespaceName2 {
578+
return clusterctlv1.ProviderTypeUnknown
579+
}
580+
581+
if provider.GetName() == "cluster-api" {
582+
return clusterctlv1.CoreProviderType
583+
}
584+
585+
return clusterctlv1.InfrastructureProviderType
586+
},
587+
expectedCondition: clusterv1.Condition{
588+
Type: operatorv1.PreflightCheckCondition,
589+
Status: corev1.ConditionTrue,
590+
},
591+
providerList: &operatorv1.InfrastructureProviderList{},
592+
},
476593
{
477594
name: "wrong version, preflight check failed",
478595
expectedError: true,
@@ -667,7 +784,12 @@ func TestPreflightChecks(t *testing.T) {
667784
Client: fakeClient,
668785
}
669786

670-
err := preflightChecks(context.Background(), fakeClient, tc.providers[0], tc.providerList, util.ClusterctlProviderType, r.listProviders)
787+
mapper := tc.mapper
788+
if mapper == nil {
789+
mapper = util.ClusterctlProviderType
790+
}
791+
792+
err := preflightChecks(context.Background(), fakeClient, tc.providers[0], tc.providerList, mapper, r.listProviders)
671793
if tc.expectedError {
672794
gs.Expect(err).To(HaveOccurred())
673795
} else {

0 commit comments

Comments
 (0)