Skip to content

Commit 25642df

Browse files
author
alexander-demicev
committed
Add preflight condition helper
Signed-off-by: alexander-demicev <alexandr.demicev@suse.com>
1 parent 8584c8c commit 25642df

File tree

1 file changed

+26
-69
lines changed

1 file changed

+26
-69
lines changed

internal/controller/preflight_checks.go

Lines changed: 26 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,18 @@ var (
5050
errCoreProviderWait = errors.New(waitingForCoreProviderReadyMessage)
5151
)
5252

53+
// setPreflightFailed sets a failed preflight check condition on the provider and returns the message as an error.
54+
func setPreflightFailed(provider genericprovider.GenericProvider, reason, message string) error {
55+
conditions.Set(provider, metav1.Condition{
56+
Type: operatorv1.PreflightCheckCondition,
57+
Status: metav1.ConditionFalse,
58+
Reason: reason,
59+
Message: message,
60+
})
61+
62+
return errors.New(message)
63+
}
64+
5365
// preflightChecks performs preflight checks before installing provider.
5466
func preflightChecks(ctx context.Context, c client.Client, provider genericprovider.GenericProvider, providerList genericprovider.GenericProviderList, mapper ProviderTypeMapper, lister ProviderLister) error {
5567
log := ctrl.LoggerFrom(ctx)
@@ -68,14 +80,8 @@ func preflightChecks(ctx context.Context, c client.Client, provider genericprovi
6880
// Ensure that the CoreProvider is called "cluster-api".
6981
if mapper(provider) == clusterctlv1.CoreProviderType {
7082
if provider.ProviderName() != configclient.ClusterAPIProviderName {
71-
conditions.Set(provider, metav1.Condition{
72-
Type: operatorv1.PreflightCheckCondition,
73-
Status: metav1.ConditionFalse,
74-
Reason: operatorv1.IncorrectCoreProviderNameReason,
75-
Message: fmt.Sprintf(incorrectCoreProviderNameMessage, provider.ProviderName(), configclient.ClusterAPIProviderName),
76-
})
77-
78-
return fmt.Errorf("incorrect CoreProvider name: %s, it should be %s", provider.ProviderName(), configclient.ClusterAPIProviderName)
83+
return setPreflightFailed(provider, operatorv1.IncorrectCoreProviderNameReason,
84+
fmt.Sprintf(incorrectCoreProviderNameMessage, provider.ProviderName(), configclient.ClusterAPIProviderName))
7985
}
8086
}
8187

@@ -87,27 +93,15 @@ func preflightChecks(ctx context.Context, c client.Client, provider genericprovi
8793

8894
if !isPredefinedProvider {
8995
if spec.FetchConfig == nil || spec.FetchConfig.Selector == nil && spec.FetchConfig.URL == "" && spec.FetchConfig.OCI == "" {
90-
conditions.Set(provider, metav1.Condition{
91-
Type: operatorv1.PreflightCheckCondition,
92-
Status: metav1.ConditionFalse,
93-
Reason: operatorv1.FetchConfigValidationErrorReason,
94-
Message: "Either Selector, OCI URL or provider URL must be provided for a not predefined provider",
95-
})
96-
97-
return fmt.Errorf("either selector, OCI URL or provider URL must be provided for a not predefined provider %s", provider.GetName())
96+
return setPreflightFailed(provider, operatorv1.FetchConfigValidationErrorReason,
97+
"Either Selector, OCI URL or provider URL must be provided for a not predefined provider")
9898
}
9999
}
100100

101101
if spec.FetchConfig != nil && spec.FetchConfig.Selector != nil && spec.FetchConfig.URL != "" {
102102
// If FetchConfiguration is not nil, exactly one of `URL` or `Selector` must be specified.
103-
conditions.Set(provider, metav1.Condition{
104-
Type: operatorv1.PreflightCheckCondition,
105-
Status: metav1.ConditionFalse,
106-
Reason: operatorv1.FetchConfigValidationErrorReason,
107-
Message: "Only one of Selector and URL must be provided, not both",
108-
})
109-
110-
return fmt.Errorf("only one of Selector and URL must be provided for provider %s", provider.GetName())
103+
return setPreflightFailed(provider, operatorv1.FetchConfigValidationErrorReason,
104+
"Only one of Selector and URL must be provided, not both")
111105
}
112106

113107
// Validate that provided GitHub token works and has repository access.
@@ -124,12 +118,7 @@ func preflightChecks(ctx context.Context, c client.Client, provider genericprovi
124118
&oauth2.Token{AccessToken: string(token)},
125119
)))
126120
if _, _, err := githubClient.Organizations.List(ctx, "kubernetes-sigs", nil); err != nil {
127-
conditions.Set(provider, metav1.Condition{
128-
Type: operatorv1.PreflightCheckCondition,
129-
Status: metav1.ConditionFalse,
130-
Reason: operatorv1.InvalidGithubTokenReason,
131-
Message: invalidGithubTokenMessage,
132-
})
121+
_ = setPreflightFailed(provider, operatorv1.InvalidGithubTokenReason, invalidGithubTokenMessage)
133122

134123
return fmt.Errorf("failed to validate provided github token: %w", err)
135124
}
@@ -151,29 +140,16 @@ func preflightChecks(ctx context.Context, c client.Client, provider genericprovi
151140
if mapper(provider) == clusterctlv1.CoreProviderType && mapper(p) == clusterctlv1.CoreProviderType {
152141
log.Info(moreThanOneCoreProviderInstanceExistsMessage)
153142

154-
conditions.Set(provider, metav1.Condition{
155-
Type: operatorv1.PreflightCheckCondition,
156-
Status: metav1.ConditionFalse,
157-
Reason: operatorv1.MoreThanOneProviderInstanceExistsReason,
158-
Message: moreThanOneCoreProviderInstanceExistsMessage,
159-
})
160-
161-
return fmt.Errorf("only one instance of CoreProvider is allowed")
143+
return setPreflightFailed(provider, operatorv1.MoreThanOneProviderInstanceExistsReason,
144+
moreThanOneCoreProviderInstanceExistsMessage)
162145
}
163146

164147
// For any other provider we should check that instances with similar name exist in any namespace
165148
if mapper(p) != clusterctlv1.CoreProviderType && p.GetName() == provider.GetName() && mapper(p) == mapper(provider) {
166149
message := fmt.Sprintf(moreThanOneProviderInstanceExistsMessage, p.GetName(), p.GetNamespace())
167150
log.Info(message)
168151

169-
conditions.Set(provider, metav1.Condition{
170-
Type: operatorv1.PreflightCheckCondition,
171-
Status: metav1.ConditionFalse,
172-
Reason: operatorv1.MoreThanOneProviderInstanceExistsReason,
173-
Message: message,
174-
})
175-
176-
return fmt.Errorf("only one %s provider is allowed in the cluster", p.GetName())
152+
return setPreflightFailed(provider, operatorv1.MoreThanOneProviderInstanceExistsReason, message)
177153
}
178154
}
179155

@@ -186,13 +162,7 @@ func preflightChecks(ctx context.Context, c client.Client, provider genericprovi
186162

187163
if !ready {
188164
log.Info(waitingForCoreProviderReadyMessage)
189-
190-
conditions.Set(provider, metav1.Condition{
191-
Type: operatorv1.PreflightCheckCondition,
192-
Status: metav1.ConditionFalse,
193-
Reason: operatorv1.WaitingForCoreProviderReadyReason,
194-
Message: waitingForCoreProviderReadyMessage,
195-
})
165+
_ = setPreflightFailed(provider, operatorv1.WaitingForCoreProviderReadyReason, waitingForCoreProviderReadyMessage)
196166

197167
return errCoreProviderWait
198168
}
@@ -219,14 +189,7 @@ func checkProviderVersion(ctx context.Context, providerVersion string, provider
219189
if err != nil {
220190
log.Info("Version contains invalid value")
221191

222-
conditions.Set(provider, metav1.Condition{
223-
Type: operatorv1.PreflightCheckCondition,
224-
Status: metav1.ConditionFalse,
225-
Reason: operatorv1.IncorrectVersionFormatReason,
226-
Message: err.Error(),
227-
})
228-
229-
return fmt.Errorf("version contains invalid value for provider %q", provider.GetName())
192+
return setPreflightFailed(provider, operatorv1.IncorrectVersionFormatReason, err.Error())
230193
}
231194

232195
// Cluster API doesn't support downgrades by design. We need to report that for the user.
@@ -237,14 +200,8 @@ func checkProviderVersion(ctx context.Context, providerVersion string, provider
237200
}
238201

239202
if targetVersion.Major() < installedVersion.Major() || targetVersion.Major() == installedVersion.Major() && targetVersion.Minor() < installedVersion.Minor() {
240-
conditions.Set(provider, metav1.Condition{
241-
Type: operatorv1.PreflightCheckCondition,
242-
Status: metav1.ConditionFalse,
243-
Reason: operatorv1.UnsupportedProviderDowngradeReason,
244-
Message: unsupportedProviderDowngradeMessage,
245-
})
246-
247-
return fmt.Errorf("downgrade is not supported for provider %q", provider.GetName())
203+
return setPreflightFailed(provider, operatorv1.UnsupportedProviderDowngradeReason,
204+
unsupportedProviderDowngradeMessage)
248205
}
249206
}
250207

0 commit comments

Comments
 (0)