Skip to content

Commit c4f7401

Browse files
committed
use errors package of Go stdlib
Signed-off-by: sivchari <shibuuuu5@gmail.com>
1 parent 850efe7 commit c4f7401

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
@@ -194,7 +194,7 @@ func (r *KubeadmConfigReconciler) Reconcile(ctx context.Context, req ctrl.Reques
194194
// Lookup the cluster the config owner is associated with
195195
cluster, err := util.GetClusterByName(ctx, r.Client, configOwner.GetNamespace(), configOwner.ClusterName())
196196
if err != nil {
197-
if errors.Cause(err) == util.ErrNoCluster {
197+
if errors.Is(errors.Cause(err), util.ErrNoCluster) {
198198
log.Info(fmt.Sprintf("%s does not belong to a cluster yet, waiting until it's part of a cluster", configOwner.GetKind()))
199199
return ctrl.Result{}, nil
200200
}

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
@@ -308,7 +308,7 @@ func (r *ClusterResourceSetReconciler) ApplyClusterResourceSet(ctx context.Conte
308308
for i, resource := range clusterResourceSet.Spec.Resources {
309309
unstructuredObj, err := r.getResource(ctx, resource, cluster.GetNamespace())
310310
if err != nil {
311-
if err == ErrSecretTypeNotSupported {
311+
if errors.Is(err, ErrSecretTypeNotSupported) {
312312
conditions.MarkFalse(clusterResourceSet, addonsv1.ResourcesAppliedCondition, addonsv1.WrongSecretTypeReason, clusterv1.ConditionSeverityWarning, err.Error())
313313
v1beta2conditions.Set(clusterResourceSet, metav1.Condition{
314314
Type: addonsv1.ResourcesAppliedV1Beta2Condition,

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
@@ -300,7 +300,7 @@ func (r *MachinePoolReconciler) reconcileInfrastructure(ctx context.Context, s *
300300
// Get and set Status.Replicas from the infrastructure provider.
301301
err = util.UnstructuredUnmarshalField(infraConfig, &mp.Status.Replicas, "status", "replicas")
302302
if err != nil {
303-
if err != util.ErrUnstructuredFieldNotFound {
303+
if !errors.Is(err, util.ErrUnstructuredFieldNotFound) {
304304
return ctrl.Result{}, errors.Wrapf(err, "failed to retrieve replicas from infrastructure provider for MachinePool %q in namespace %q", mp.Name, mp.Namespace)
305305
}
306306
}

internal/controllers/cluster/cluster_controller_phases.go

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

237237
// Get and parse Status.FailureDomains from the infrastructure provider.
238238
failureDomains := clusterv1.FailureDomains{}
239-
if err := util.UnstructuredUnmarshalField(s.infraCluster, &failureDomains, "status", "failureDomains"); err != nil && err != util.ErrUnstructuredFieldNotFound {
239+
if err := util.UnstructuredUnmarshalField(s.infraCluster, &failureDomains, "status", "failureDomains"); err != nil && !errors.Is(err, util.ErrUnstructuredFieldNotFound) {
240240
return ctrl.Result{}, errors.Wrapf(err, "failed to retrieve Status.FailureDomains from infrastructure provider for Cluster %q in namespace %q",
241241
cluster.Name, cluster.Namespace)
242242
}
@@ -362,7 +362,7 @@ func (r *Reconciler) reconcileKubeconfig(ctx context.Context, s *scope) (ctrl.Re
362362
switch {
363363
case apierrors.IsNotFound(err):
364364
if err := kubeconfig.CreateSecret(ctx, r.Client, cluster); err != nil {
365-
if err == kubeconfig.ErrDependentCertificateNotFound {
365+
if errors.Is(err, kubeconfig.ErrDependentCertificateNotFound) {
366366
log.Info("Could not find secret for cluster, requeuing", "Secret", secret.ClusterCA)
367367
return ctrl.Result{RequeueAfter: 30 * time.Second}, nil
368368
}

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
@@ -292,15 +292,15 @@ func (r *Reconciler) reconcileInfrastructure(ctx context.Context, s *scope) (ctr
292292

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

299299
// Get and set the failure domain from the infrastructure provider.
300300
var failureDomain string
301301
err = util.UnstructuredUnmarshalField(s.infraMachine, &failureDomain, "spec", "failureDomain")
302302
switch {
303-
case err == util.ErrUnstructuredFieldNotFound: // no-op
303+
case errors.Is(err, util.ErrUnstructuredFieldNotFound): // no-op
304304
case err != nil:
305305
return ctrl.Result{}, errors.Wrapf(err, "failed to retrieve failure domain from infrastructure provider for Machine %q in namespace %q", m.Name, m.Namespace)
306306
default:

internal/runtime/client/client.go

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

0 commit comments

Comments
 (0)