Skip to content

Commit 6b71c78

Browse files
committed
style(engine): satisfy the linter in the ported Talos helpers
Static errors instead of dynamic ones, the resource walk split so the per-node half stands on its own, and the reassignments and stale nolint directives cleaned up. No behaviour change. Assisted-by: LLM Signed-off-by: Andrei Kvapil <[email protected]>
1 parent dc2815f commit 6b71c78

1 file changed

Lines changed: 51 additions & 39 deletions

File tree

‎pkg/engine/talos_helpers.go‎

Lines changed: 51 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,15 @@ import (
2020
// --skip-verify since the fork was dropped. Behaviour matches what the helpers
2121
// did in v1.14.0-alpha.1, which is what the callers in engine.go expect.
2222

23+
// ErrMultiNodeUnsupported is returned for a command that only makes sense
24+
// against a single node when the context names more than one.
25+
var ErrMultiNodeUnsupported = errors.New("command is not supported with multiple nodes")
26+
27+
// ErrNoResourceType is returned when no resource type was given to walk.
28+
var ErrNoResourceType = errors.New("not enough arguments: at least 1 is expected")
29+
2330
// failIfMultiNodes reports an error when the context carries more than one node
24-
// in its outgoing metadata, for commands that only make sense against one.
31+
// in its outgoing metadata.
2532
func failIfMultiNodes(ctx context.Context, command string) error {
2633
md, ok := metadata.FromOutgoingContext(ctx)
2734
if !ok {
@@ -32,7 +39,7 @@ func failIfMultiNodes(ctx context.Context, command string) error {
3239
return nil
3340
}
3441

35-
return fmt.Errorf("command %q is not supported with multiple nodes", command)
42+
return fmt.Errorf("%w: %q", ErrMultiNodeUnsupported, command)
3643
}
3744

3845
// forEachResource resolves a resource kind and runs callback for every resource
@@ -43,8 +50,6 @@ func failIfMultiNodes(ctx context.Context, command string) error {
4350
// is handed to the callback rather than returned, which is what lets a caller
4451
// report an unreachable node and carry on with the rest; an error the callback
4552
// itself returns stops the walk.
46-
//
47-
//nolint:gocyclo
4853
func forEachResource(
4954
ctx context.Context,
5055
c *client.Client,
@@ -54,7 +59,7 @@ func forEachResource(
5459
args ...string,
5560
) error {
5661
if len(args) == 0 {
57-
return errors.New("not enough arguments: at least 1 is expected")
62+
return ErrNoResourceType
5863
}
5964

6065
resourceType := args[0]
@@ -65,63 +70,70 @@ func forEachResource(
6570
resourceID = args[1]
6671
}
6772

68-
md, _ := metadata.FromOutgoingContext(ctx) //nolint:errcheck // absent metadata means the single implicit node below
73+
md, _ := metadata.FromOutgoingContext(ctx)
6974

7075
nodes := md.Get("nodes")
7176
if len(nodes) == 0 {
7277
nodes = []string{""}
7378
}
7479

75-
rd, err := c.ResolveResourceKind(client.WithNode(ctx, nodes[0]), &namespace, resourceType)
80+
resourceDefinition, err := c.ResolveResourceKind(client.WithNode(ctx, nodes[0]), &namespace, resourceType)
7681
if err != nil {
77-
return err
82+
return fmt.Errorf("resolving resource kind %q: %w", resourceType, err)
7883
}
7984

8085
if callbackRD != nil {
81-
if err = callbackRD(rd); err != nil {
82-
return err
86+
if cbErr := callbackRD(resourceDefinition); cbErr != nil {
87+
return cbErr
8388
}
8489
}
8590

86-
resourceType = rd.TypedSpec().Type
91+
resourceType = resourceDefinition.TypedSpec().Type
8792

8893
for _, node := range nodes {
89-
nodeCtx := ctx
90-
if node != "" {
91-
nodeCtx = client.WithNode(ctx, node)
94+
if err := walkNodeResources(ctx, c, callback, namespace, resourceType, resourceID, node); err != nil {
95+
return err
9296
}
97+
}
9398

94-
if resourceID != "" {
95-
r, callErr := c.COSI.Get(
96-
nodeCtx,
97-
resource.NewMetadata(namespace, resourceType, resourceID, resource.VersionUndefined),
98-
state.WithGetUnmarshalOptions(state.WithSkipProtobufUnmarshal()),
99-
)
100-
101-
if err = callback(ctx, node, r, callErr); err != nil {
102-
return err
103-
}
99+
return nil
100+
}
104101

105-
continue
106-
}
102+
// walkNodeResources runs callback over one node's resources of a single kind:
103+
// the one named by resourceID, or every resource of the kind when it is empty.
104+
func walkNodeResources(
105+
ctx context.Context,
106+
c *client.Client,
107+
callback func(ctx context.Context, hostname string, r resource.Resource, callError error) error,
108+
namespace, resourceType, resourceID, node string,
109+
) error {
110+
nodeCtx := ctx
111+
if node != "" {
112+
nodeCtx = client.WithNode(ctx, node)
113+
}
107114

108-
items, callErr := c.COSI.List(
115+
if resourceID != "" {
116+
r, callErr := c.COSI.Get(
109117
nodeCtx,
110-
resource.NewMetadata(namespace, resourceType, "", resource.VersionUndefined),
111-
state.WithListUnmarshalOptions(state.WithSkipProtobufUnmarshal()),
118+
resource.NewMetadata(namespace, resourceType, resourceID, resource.VersionUndefined),
119+
state.WithGetUnmarshalOptions(state.WithSkipProtobufUnmarshal()),
112120
)
113-
if callErr != nil {
114-
if err = callback(ctx, node, nil, callErr); err != nil {
115-
return err
116-
}
117121

118-
continue
119-
}
122+
return callback(ctx, node, r, callErr)
123+
}
120124

121-
for _, r := range items.Items {
122-
if err = callback(ctx, node, r, nil); err != nil {
123-
return err
124-
}
125+
items, callErr := c.COSI.List(
126+
nodeCtx,
127+
resource.NewMetadata(namespace, resourceType, "", resource.VersionUndefined),
128+
state.WithListUnmarshalOptions(state.WithSkipProtobufUnmarshal()),
129+
)
130+
if callErr != nil {
131+
return callback(ctx, node, nil, callErr)
132+
}
133+
134+
for _, r := range items.Items {
135+
if err := callback(ctx, node, r, nil); err != nil {
136+
return err
125137
}
126138
}
127139

0 commit comments

Comments
 (0)