Skip to content

Commit ea17ec4

Browse files
committed
improve: add meaningful context to error returns
1 parent 26ed279 commit ea17ec4

File tree

5 files changed

+16
-13
lines changed

5 files changed

+16
-13
lines changed

pkg/cloudprovider/cloudprovider.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -251,7 +251,7 @@ func (c *CloudProvider) GetInstanceTypes(ctx context.Context, nodePool *karpv1.N
251251

252252
nodeClass, err := c.resolveNodeClassFromNodePool(ctx, nodePool)
253253
if err != nil {
254-
return nil, err
254+
return nil, fmt.Errorf("unable to resolve NodeClass from NodePool %s: %w", nodePool.Name, err)
255255
}
256256

257257
capiInstanceTypes, err := c.findInstanceTypesForNodeClass(ctx, nodeClass)
@@ -299,7 +299,7 @@ func (c *CloudProvider) List(ctx context.Context) ([]*karpv1.NodeClaim, error) {
299299
for _, machine := range machines {
300300
nodeClaim, err := c.machineToNodeClaim(ctx, machine)
301301
if err != nil {
302-
return []*karpv1.NodeClaim{}, err
302+
return []*karpv1.NodeClaim{}, fmt.Errorf("unable to convert Machine %s to NodeClaim: %w", machine.Name, err)
303303
}
304304
nodeClaims = append(nodeClaims, nodeClaim)
305305
}
@@ -326,7 +326,7 @@ func (c *CloudProvider) machineDeploymentFromMachine(ctx context.Context, machin
326326

327327
machineDeployment, err := c.machineDeploymentProvider.Get(ctx, mdName, machine.GetNamespace())
328328
if err != nil {
329-
return nil, err
329+
return nil, fmt.Errorf("unable to get MachineDeployment %s for Machine %s: %w", mdName, machine.GetName(), err)
330330
}
331331

332332
return machineDeployment, nil
@@ -341,7 +341,7 @@ func (c *CloudProvider) findInstanceTypesForNodeClass(ctx context.Context, nodeC
341341

342342
machineDeployments, err := c.machineDeploymentProvider.List(ctx, nodeClass.Spec.ScalableResourceSelector)
343343
if err != nil {
344-
return instanceTypes, err
344+
return instanceTypes, fmt.Errorf("unable to list MachineDeployments for NodeClass %s: %w", nodeClass.Name, err)
345345
}
346346

347347
for _, md := range machineDeployments {
@@ -458,7 +458,7 @@ func (c *CloudProvider) resolveNodeClassFromNodeClaim(ctx context.Context, nodeC
458458
// if the kind and version differ from the included api then we will need to load differently.
459459
// NodeClass and NodeClaim are not namespace scoped, this call can probably be changed.
460460
if err := c.kubeClient.Get(ctx, client.ObjectKey{Name: name, Namespace: nodeClaim.Namespace}, nodeClass); err != nil {
461-
return nil, err
461+
return nil, fmt.Errorf("unable to get NodeClass %s for NodeClaim %s: %w", name, nodeClaim.Name, err)
462462
}
463463

464464
return nodeClass, nil
@@ -483,7 +483,7 @@ func (c *CloudProvider) resolveNodeClassFromNodePool(ctx context.Context, nodePo
483483
// TODO (elmiko) add extra logic to get different resources from the class ref
484484
// if the kind and version differ from the included api then we will need to load differently.
485485
if err := c.kubeClient.Get(ctx, client.ObjectKey{Name: name, Namespace: nodePool.Namespace}, nodeClass); err != nil {
486-
return nil, err
486+
return nil, fmt.Errorf("unable to get NodeClass %s for NodePool %s: %w", name, nodePool.Name, err)
487487
}
488488

489489
return nodeClass, nil

pkg/controllers/nodeclass/status/controller.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ package status
1818

1919
import (
2020
"context"
21+
"fmt"
2122

2223
"github.com/awslabs/operatorpkg/status"
2324
"k8s.io/apimachinery/pkg/api/equality"
@@ -63,7 +64,7 @@ func (c *Controller) Reconcile(ctx context.Context, nodeClass *v1alpha1.ClusterA
6364
if errors.IsConflict(err) {
6465
return reconcile.Result{Requeue: true}, nil
6566
}
66-
return reconcile.Result{}, err
67+
return reconcile.Result{}, fmt.Errorf("unable to patch NodeClass status for %s: %w", nodeClass.Name, err)
6768
}
6869
}
6970

pkg/operator/operator.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ package operator
1818

1919
import (
2020
"context"
21+
"fmt"
2122
"log"
2223

2324
"github.com/samber/lo"
@@ -73,18 +74,18 @@ func NewOperator(ctx context.Context, operator *operator.Operator) (context.Cont
7374
func buildManagementClusterKubeClient(ctx context.Context, operator *operator.Operator) (client.Client, error) {
7475
clusterAPIKubeConfig, err := buildClusterCAPIKubeConfig(ctx)
7576
if err != nil {
76-
return nil, err
77+
return nil, fmt.Errorf("unable to build cluster API kube config: %w", err)
7778
}
7879

7980
if clusterAPIKubeConfig != nil {
8081
mgmtCluster, err := cluster.New(clusterAPIKubeConfig, func(o *cluster.Options) {
8182
o.Scheme = operator.GetScheme()
8283
})
8384
if err != nil {
84-
return nil, err
85+
return nil, fmt.Errorf("unable to create new cluster for management cluster: %w", err)
8586
}
8687
if err = operator.Add(mgmtCluster); err != nil {
87-
return nil, err
88+
return nil, fmt.Errorf("unable to add management cluster to operator: %w", err)
8889
}
8990
return mgmtCluster.GetClient(), nil
9091
}

pkg/providers/machine/machine.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ func (p *DefaultProvider) List(ctx context.Context, selector *metav1.LabelSelect
7979
machineList := &capiv1beta1.MachineList{}
8080
err := p.kubeClient.List(ctx, machineList, listOptions...)
8181
if err != nil {
82-
return nil, err
82+
return nil, fmt.Errorf("unable to list machines with selector: %w", err)
8383
}
8484

8585
for _, m := range machineList.Items {

pkg/providers/machinedeployment/machinedeployment.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,8 +47,9 @@ func (p *DefaultProvider) Get(ctx context.Context, name string, namespace string
4747
err := p.kubeClient.Get(ctx, client.ObjectKey{Name: name, Namespace: namespace}, machineDeployment)
4848
if err != nil {
4949
machineDeployment = nil
50+
return machineDeployment, fmt.Errorf("unable to get MachineDeployment %s in namespace %s: %w", name, namespace, err)
5051
}
51-
return machineDeployment, err
52+
return machineDeployment, nil
5253
}
5354

5455
func (p *DefaultProvider) List(ctx context.Context, selector *metav1.LabelSelector) ([]*capiv1beta1.MachineDeployment, error) {
@@ -70,7 +71,7 @@ func (p *DefaultProvider) List(ctx context.Context, selector *metav1.LabelSelect
7071
machineDeploymentList := &capiv1beta1.MachineDeploymentList{}
7172
err := p.kubeClient.List(ctx, machineDeploymentList, listOptions...)
7273
if err != nil {
73-
return nil, err
74+
return nil, fmt.Errorf("unable to list MachineDeployments with selector: %w", err)
7475
}
7576

7677
for _, m := range machineDeploymentList.Items {

0 commit comments

Comments
 (0)