Skip to content

Commit 68bd811

Browse files
committed
refactor: remove ops and improve error msgs
1 parent 1382b4c commit 68bd811

1 file changed

Lines changed: 21 additions & 22 deletions

File tree

hcloud/routes.go

Lines changed: 21 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ func (r *routes) CreateRoute(ctx context.Context, _ string, _ string, route *clo
111111

112112
node, gateway, err := r.resolveRouteTarget(ctx, string(route.TargetNode))
113113
if err != nil {
114-
return fmt.Errorf("%s: %w", op, err)
114+
return fmt.Errorf("%s: error resolving route target: %w", op, err)
115115
}
116116

117117
if !slices.ContainsFunc(route.TargetNodeAddresses, func(target corev1.NodeAddress) bool {
@@ -127,7 +127,11 @@ func (r *routes) CreateRoute(ctx context.Context, _ string, _ string, route *clo
127127

128128
r.warnCIDRMismatch(cidr, node)
129129

130-
return r.upsertRoute(ctx, gateway, cidr, string(route.TargetNode))
130+
if err := r.upsertRoute(ctx, gateway, cidr, string(route.TargetNode)); err != nil {
131+
return fmt.Errorf("error upserting route %q via %q: %w", cidr.String(), gateway.String(), err)
132+
}
133+
134+
return nil
131135
}
132136

133137
// resolveRouteTarget returns the k8s node and the hcloud server's private IP on the routes
@@ -137,44 +141,41 @@ func (r *routes) CreateRoute(ctx context.Context, _ string, _ string, route *clo
137141
// ID changes (e.g. server recreated). Refreshes the cache once if the private-net attachment
138142
// isn't yet reflected.
139143
func (r *routes) resolveRouteTarget(ctx context.Context, nodeName string) (*corev1.Node, net.IP, error) {
140-
const op = "hcloud/resolveRouteTarget"
141-
metrics.OperationCalled.WithLabelValues(op).Inc()
142-
143144
node, err := r.nodeLister.Get(nodeName)
144145
if err != nil {
145-
return nil, nil, fmt.Errorf("%s: %w", op, err)
146+
return nil, nil, fmt.Errorf("error fetching node %s by name: %w", nodeName, err)
146147
}
147148

148149
if node.Spec.ProviderID == "" {
149-
return nil, nil, fmt.Errorf("%s: node %q not yet initialized", op, node.Name)
150+
return nil, nil, fmt.Errorf("node %s not yet initialized", node.Name)
150151
}
151152

152153
id, isCloudServer, err := providerid.ToServerID(node.Spec.ProviderID)
153154
if err != nil {
154-
return nil, nil, fmt.Errorf("%s: %w", op, err)
155+
return nil, nil, fmt.Errorf("error parsing providerID %q for node %s: %w", node.Spec.ProviderID, nodeName, err)
155156
}
156157
if !isCloudServer {
157-
return nil, nil, fmt.Errorf("%s: node %q is not a Cloud server, routes are only supported for Cloud servers", op, node.Name)
158+
return nil, nil, fmt.Errorf("node %s is not a Cloud server, routes are only supported for Cloud servers", node.Name)
158159
}
159160

160161
server, err := r.serverCache.ByID(ctx, id)
161162
if errors.Is(err, hcops.ErrNotFound) {
162163
server, err = r.serverCache.ByName(ctx, node.Name)
163164
}
164165
if err != nil {
165-
return nil, nil, fmt.Errorf("%s: %w", op, err)
166+
return nil, nil, fmt.Errorf("error fetching node %s by ID: %w", nodeName, err)
166167
}
167168

168169
privNet, ok := findServerPrivateNetByID(server, r.network.ID)
169170
if !ok {
170171
r.serverCache.InvalidateCache()
171172
server, err = r.serverCache.ByID(ctx, server.ID)
172173
if err != nil {
173-
return nil, nil, fmt.Errorf("%s: %w", op, err)
174+
return nil, nil, fmt.Errorf("error fetching node %s by ID: %w", nodeName, err)
174175
}
175176
privNet, ok = findServerPrivateNetByID(server, r.network.ID)
176177
if !ok {
177-
return nil, nil, fmt.Errorf("%s: server %q: network with id %d not attached to this server", op, node.Name, r.network.ID)
178+
return nil, nil, fmt.Errorf("server %s (%d): network with id %d not attached to this server", server.Name, server.ID, r.network.ID)
178179
}
179180
}
180181

@@ -185,11 +186,8 @@ func (r *routes) resolveRouteTarget(ctx context.Context, nodeName string) (*core
185186
// route is a no-op; a stale route with a different gateway is replaced in place. nodeName is
186187
// used only for logging and for surfacing API conflicts against the right k8s object.
187188
func (r *routes) upsertRoute(ctx context.Context, gateway net.IP, cidr *net.IPNet, nodeName string) error {
188-
const op = "hcloud/upsertRoute"
189-
metrics.OperationCalled.WithLabelValues(op).Inc()
190-
191189
if err := r.reloadNetwork(ctx); err != nil {
192-
return fmt.Errorf("%s: %w", op, err)
190+
return fmt.Errorf("error reloading network: %w", err)
193191
}
194192

195193
destination := cidr.String()
@@ -211,15 +209,16 @@ func (r *routes) upsertRoute(ctx context.Context, gateway net.IP, cidr *net.IPNe
211209
Route: existing,
212210
})
213211
if err != nil {
214-
return fmt.Errorf("%s: %w", op, err)
212+
return fmt.Errorf("error deleting route for %q via %q: %w", cidr.String(), gateway.String(), err)
215213
}
216214
if err := r.client.Action.WaitFor(ctx, action); err != nil {
217-
return fmt.Errorf("%s: %w", op, err)
215+
return fmt.Errorf("error deleting route for %q via %q: %w", cidr.String(), gateway.String(), err)
218216
}
219217
klog.InfoS(
220218
"deleted stale route with wrong gateway; recreating",
221-
"target-node", nodeName,
222-
"destination-cidr", destination,
219+
"node", nodeName,
220+
"gateway", gateway,
221+
"cidr", destination,
223222
)
224223
}
225224

@@ -238,11 +237,11 @@ func (r *routes) upsertRoute(ctx context.Context, gateway net.IP, cidr *net.IPNe
238237
err,
239238
)
240239
}
241-
return fmt.Errorf("%s: %w", op, err)
240+
return fmt.Errorf("error adding route for %q via %q: %w", cidr.String(), gateway.String(), err)
242241
}
243242

244243
if err := r.client.Action.WaitFor(ctx, action); err != nil {
245-
return fmt.Errorf("%s: %w", op, err)
244+
return fmt.Errorf("error adding route for %q via %q: %w", cidr.String(), gateway.String(), err)
246245
}
247246

248247
return nil

0 commit comments

Comments
 (0)