Skip to content

Commit 11dd6c7

Browse files
committed
use errors package of Go stdlib
Signed-off-by: sivchari <shibuuuu5@gmail.com>
1 parent 76328ed commit 11dd6c7

File tree

10 files changed

+24
-22
lines changed

10 files changed

+24
-22
lines changed

bootstrap/kubeadm/internal/controllers/kubeadmconfig_controller.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -186,7 +186,7 @@ func (r *KubeadmConfigReconciler) Reconcile(ctx context.Context, req ctrl.Reques
186186
// Lookup the cluster the config owner is associated with
187187
cluster, err := util.GetClusterByName(ctx, r.Client, configOwner.GetNamespace(), configOwner.ClusterName())
188188
if err != nil {
189-
if errors.Cause(err) == util.ErrNoCluster {
189+
if errors.Is(errors.Cause(err), util.ErrNoCluster) {
190190
log.Info(fmt.Sprintf("%s does not belong to a cluster yet, waiting until it's part of a cluster", configOwner.GetKind()))
191191
return ctrl.Result{}, nil
192192
}

cmd/clusterctl/client/cluster/template.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -305,7 +305,8 @@ func getGitHubClient(ctx context.Context, configVariablesClient config.Variables
305305

306306
// handleGithubErr wraps error messages.
307307
func handleGithubErr(err error, message string, args ...interface{}) error {
308-
if _, ok := err.(*github.RateLimitError); ok {
308+
var rateLimitErr *github.RateLimitError
309+
if errors.As(err, &rateLimitErr) {
309310
return errors.New("rate limit for github api has been reached. Please wait one hour or get a personal API token and assign it to the GITHUB_TOKEN environment variable")
310311
}
311312
return errors.Wrapf(err, message, args...)

cmd/clusterctl/client/repository/repository_github.go

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,8 @@ var (
6161
retryableOperationTimeout = 1 * time.Minute
6262
)
6363

64+
var rateLimitError github.RateLimitError
65+
6466
// gitHubRepository provides support for providers hosted on GitHub.
6567
//
6668
// We support GitHub repositories that use the release feature to publish artifacts and versions.
@@ -319,7 +321,7 @@ func (g *gitHubRepository) getVersions(ctx context.Context) ([]string, error) {
319321
if listReleasesErr != nil {
320322
retryError = g.handleGithubErr(listReleasesErr, "failed to get the list of releases")
321323
// Return immediately if we are rate limited.
322-
if _, ok := listReleasesErr.(*github.RateLimitError); ok {
324+
if errors.As(listReleasesErr, &rateLimitError) {
323325
return false, retryError
324326
}
325327
return false, nil
@@ -334,7 +336,7 @@ func (g *gitHubRepository) getVersions(ctx context.Context) ([]string, error) {
334336
if listReleasesErr != nil {
335337
retryError = g.handleGithubErr(listReleasesErr, "failed to get the list of releases")
336338
// Return immediately if we are rate limited.
337-
if _, ok := listReleasesErr.(*github.RateLimitError); ok {
339+
if errors.As(listReleasesErr, &rateLimitError) {
338340
return false, retryError
339341
}
340342
return false, nil
@@ -384,7 +386,7 @@ func (g *gitHubRepository) getReleaseByTag(ctx context.Context, tag string) (*gi
384386
return false, retryError
385387
}
386388
// Return immediately if we are rate limited.
387-
if _, ok := getReleasesErr.(*github.RateLimitError); ok {
389+
if errors.As(getReleasesErr, &rateLimitError) {
388390
return false, retryError
389391
}
390392
return false, nil
@@ -466,7 +468,7 @@ func (g *gitHubRepository) downloadFilesFromRelease(ctx context.Context, release
466468
if downloadReleaseError != nil {
467469
retryError = g.handleGithubErr(downloadReleaseError, "failed to download file %q from %q release", *release.TagName, fileName)
468470
// Return immediately if we are rate limited.
469-
if _, ok := downloadReleaseError.(*github.RateLimitError); ok {
471+
if errors.As(downloadReleaseError, &rateLimitError) {
470472
return false, retryError
471473
}
472474
return false, nil
@@ -499,10 +501,13 @@ func (g *gitHubRepository) downloadFilesFromRelease(ctx context.Context, release
499501

500502
// handleGithubErr wraps error messages.
501503
func (g *gitHubRepository) handleGithubErr(err error, message string, args ...interface{}) error {
502-
if _, ok := err.(*github.RateLimitError); ok {
504+
if errors.As(err, &rateLimitError) {
503505
return errors.New("rate limit for github api has been reached. Please wait one hour or get a personal API token and assign it to the GITHUB_TOKEN environment variable")
504506
}
505-
if ghErr, ok := err.(*github.ErrorResponse); ok {
507+
508+
var errorResponse github.ErrorResponse
509+
if errors.As(err, &errorResponse) {
510+
ghErr := err.(*github.ErrorResponse)
506511
if ghErr.Response.StatusCode == http.StatusNotFound {
507512
return errNotFound
508513
}

exp/addons/internal/controllers/clusterresourceset_controller.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -303,7 +303,7 @@ func (r *ClusterResourceSetReconciler) ApplyClusterResourceSet(ctx context.Conte
303303
for i, resource := range clusterResourceSet.Spec.Resources {
304304
unstructuredObj, err := r.getResource(ctx, resource, cluster.GetNamespace())
305305
if err != nil {
306-
if err == ErrSecretTypeNotSupported {
306+
if errors.Is(err, ErrSecretTypeNotSupported) {
307307
conditions.MarkFalse(clusterResourceSet, addonsv1.ResourcesAppliedCondition, addonsv1.WrongSecretTypeReason, clusterv1.ConditionSeverityWarning, err.Error())
308308
} else {
309309
conditions.MarkFalse(clusterResourceSet, addonsv1.ResourcesAppliedCondition, addonsv1.RetrievingResourceFailedReason, clusterv1.ConditionSeverityWarning, err.Error())

exp/internal/controllers/machinepool_controller_noderef.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ func (r *MachinePoolReconciler) reconcileNodeRefs(ctx context.Context, s *scope)
9292
// Get the Node references.
9393
nodeRefsResult, err := r.getNodeReferences(ctx, mp.Spec.ProviderIDList, mp.Spec.MinReadySeconds, s.nodeRefMap)
9494
if err != nil {
95-
if err == errNoAvailableNodes {
95+
if errors.Is(err, errNoAvailableNodes) {
9696
log.Info("Cannot assign NodeRefs to MachinePool, no matching Nodes")
9797
// No need to requeue here. Nodes emit an event that triggers reconciliation.
9898
return ctrl.Result{}, nil

exp/internal/controllers/machinepool_controller_phases.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -314,7 +314,7 @@ func (r *MachinePoolReconciler) reconcileInfrastructure(ctx context.Context, s *
314314
// Get and set Status.Replicas from the infrastructure provider.
315315
err = util.UnstructuredUnmarshalField(infraConfig, &mp.Status.Replicas, "status", "replicas")
316316
if err != nil {
317-
if err != util.ErrUnstructuredFieldNotFound {
317+
if !errors.Is(err, util.ErrUnstructuredFieldNotFound) {
318318
return ctrl.Result{}, errors.Wrapf(err, "failed to retrieve replicas from infrastructure provider for MachinePool %q in namespace %q", mp.Name, mp.Namespace)
319319
}
320320
}

internal/controllers/cluster/cluster_controller_phases.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -210,7 +210,7 @@ func (r *Reconciler) reconcileInfrastructure(ctx context.Context, cluster *clust
210210

211211
// Get and parse Status.FailureDomains from the infrastructure provider.
212212
failureDomains := clusterv1.FailureDomains{}
213-
if err := util.UnstructuredUnmarshalField(infraConfig, &failureDomains, "status", "failureDomains"); err != nil && err != util.ErrUnstructuredFieldNotFound {
213+
if err := util.UnstructuredUnmarshalField(infraConfig, &failureDomains, "status", "failureDomains"); err != nil && !errors.Is(err, util.ErrUnstructuredFieldNotFound) {
214214
return ctrl.Result{}, errors.Wrapf(err, "failed to retrieve Status.FailureDomains from infrastructure provider for Cluster %q in namespace %q",
215215
cluster.Name, cluster.Namespace)
216216
}
@@ -313,7 +313,7 @@ func (r *Reconciler) reconcileKubeconfig(ctx context.Context, cluster *clusterv1
313313
switch {
314314
case apierrors.IsNotFound(err):
315315
if err := kubeconfig.CreateSecret(ctx, r.Client, cluster); err != nil {
316-
if err == kubeconfig.ErrDependentCertificateNotFound {
316+
if errors.Is(err, kubeconfig.ErrDependentCertificateNotFound) {
317317
log.Info("Could not find secret for cluster, requeuing", "Secret", secret.ClusterCA)
318318
return ctrl.Result{RequeueAfter: 30 * time.Second}, nil
319319
}

internal/controllers/machine/machine_controller_noderef.go

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -73,12 +73,7 @@ func (r *Reconciler) reconcileNode(ctx context.Context, s *scope) (ctrl.Result,
7373
// Even if Status.NodeRef exists, continue to do the following checks to make sure Node is healthy
7474
node, err := r.getNode(ctx, remoteClient, *machine.Spec.ProviderID)
7575
if err != nil {
76-
if err == ErrNodeNotFound {
77-
if !s.machine.DeletionTimestamp.IsZero() {
78-
// Tolerate node not found when the machine is being deleted.
79-
return ctrl.Result{}, nil
80-
}
81-
76+
if errors.Is(err, ErrNodeNotFound) {
8277
// While a NodeRef is set in the status, failing to get that node means the node is deleted.
8378
// If Status.NodeRef is not set before, node still can be in the provisioning state.
8479
if machine.Status.NodeRef != nil {

internal/controllers/machine/machine_controller_phases.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -276,15 +276,15 @@ func (r *Reconciler) reconcileInfrastructure(ctx context.Context, s *scope) (ctr
276276

277277
// Get and set Status.Addresses from the infrastructure provider.
278278
err = util.UnstructuredUnmarshalField(s.infraMachine, &m.Status.Addresses, "status", "addresses")
279-
if err != nil && err != util.ErrUnstructuredFieldNotFound {
279+
if err != nil && !errors.Is(err, util.ErrUnstructuredFieldNotFound) {
280280
return ctrl.Result{}, errors.Wrapf(err, "failed to retrieve addresses from infrastructure provider for Machine %q in namespace %q", m.Name, m.Namespace)
281281
}
282282

283283
// Get and set the failure domain from the infrastructure provider.
284284
var failureDomain string
285285
err = util.UnstructuredUnmarshalField(s.infraMachine, &failureDomain, "spec", "failureDomain")
286286
switch {
287-
case err == util.ErrUnstructuredFieldNotFound: // no-op
287+
case errors.Is(err, util.ErrUnstructuredFieldNotFound): // no-op
288288
case err != nil:
289289
return ctrl.Result{}, errors.Wrapf(err, "failed to retrieve failure domain from infrastructure provider for Machine %q in namespace %q", m.Name, m.Namespace)
290290
default:

internal/runtime/client/client.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -344,7 +344,8 @@ func (c *client) CallExtension(ctx context.Context, hook runtimecatalog.Hook, fo
344344
// If the error is errCallingExtensionHandler then apply failure policy to calculate
345345
// the effective result of the operation.
346346
ignore := *registration.FailurePolicy == runtimev1.FailurePolicyIgnore
347-
if _, ok := err.(errCallingExtensionHandler); ok && ignore {
347+
var errTyp errCallingExtensionHandler
348+
if errors.As(err, &errTyp) && ignore {
348349
// Update the response to a default success response and return.
349350
log.Error(err, fmt.Sprintf("Ignoring error calling extension handler because of FailurePolicy %q", *registration.FailurePolicy))
350351
response.SetStatus(runtimehooksv1.ResponseStatusSuccess)

0 commit comments

Comments
 (0)