• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

NVIDIA / skyhook / 20353746630

18 Dec 2025 10:50PM UTC coverage: 75.716% (-0.2%) from 75.958%
20353746630

Pull #133

github

web-flow
Merge a731af90a into 19dce4787
Pull Request #133: fix: cleanup cli code

29 of 63 new or added lines in 9 files covered. (46.03%)

11 existing lines in 6 files now uncovered.

5818 of 7684 relevant lines covered (75.72%)

1.12 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

85.27
/operator/cmd/cli/app/node/node_list.go
1
/*
2
 * SPDX-FileCopyrightText: Copyright (c) 2025 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
3
 * SPDX-License-Identifier: Apache-2.0
4
 *
5
 *
6
 * Licensed under the Apache License, Version 2.0 (the "License");
7
 * you may not use this file except in compliance with the License.
8
 * You may obtain a copy of the License at
9
 *
10
 * http://www.apache.org/licenses/LICENSE-2.0
11
 *
12
 * Unless required by applicable law or agreed to in writing, software
13
 * distributed under the License is distributed on an "AS IS" BASIS,
14
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15
 * See the License for the specific language governing permissions and
16
 * limitations under the License.
17
 */
18

19
package node
20

21
import (
22
        "context"
23
        "encoding/json"
24
        "fmt"
25
        "io"
26
        "sort"
27
        "strings"
28

29
        "github.com/spf13/cobra"
30
        metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
31

32
        "github.com/NVIDIA/skyhook/operator/api/v1alpha1"
33
        "github.com/NVIDIA/skyhook/operator/internal/cli/client"
34
        cliContext "github.com/NVIDIA/skyhook/operator/internal/cli/context"
35
        "github.com/NVIDIA/skyhook/operator/internal/cli/utils"
36
)
37

38
// nodeListOptions holds the options for the node list command
39
type nodeListOptions struct {
40
        skyhookName string
41
}
42

43
// BindToCmd binds the options to the command flags
44
func (o *nodeListOptions) BindToCmd(cmd *cobra.Command) {
1✔
45
        cmd.Flags().StringVar(&o.skyhookName, "skyhook", "", "Name of the Skyhook CR (required)")
1✔
46

1✔
47
        _ = cmd.MarkFlagRequired("skyhook")
1✔
48
}
1✔
49

50
// NewListCmd creates the node list command
51
func NewListCmd(ctx *cliContext.CLIContext) *cobra.Command {
1✔
52
        opts := &nodeListOptions{}
1✔
53

1✔
54
        cmd := &cobra.Command{
1✔
55
                Use:   "list",
1✔
56
                Short: "List all nodes targeted by a Skyhook",
1✔
57
                Long: `List all nodes that have activity for a specific Skyhook.
1✔
58

1✔
59
This command shows all nodes that have Skyhook state annotations for the
1✔
60
specified Skyhook CR, along with a summary of package completion status.`,
1✔
61
                Example: `  # List all nodes targeted by gpu-init Skyhook
1✔
62
  kubectl skyhook node list --skyhook gpu-init
1✔
63

1✔
64
  # Output as JSON
1✔
65
  kubectl skyhook node list --skyhook gpu-init -o json`,
1✔
66
                RunE: func(cmd *cobra.Command, args []string) error {
1✔
67
                        if opts.skyhookName == "" {
×
68
                                return fmt.Errorf("--skyhook flag is required")
×
69
                        }
×
70

71
                        clientFactory := client.NewFactory(ctx.GlobalFlags.ConfigFlags)
×
72
                        kubeClient, err := clientFactory.Client()
×
73
                        if err != nil {
×
74
                                return fmt.Errorf("initializing kubernetes client: %w", err)
×
75
                        }
×
76

NEW
77
                        return runNodeList(cmd.Context(), kubeClient, opts, ctx)
×
78
                },
79
        }
80

81
        opts.BindToCmd(cmd)
1✔
82

1✔
83
        return cmd
1✔
84
}
85

86
// nodeListEntry represents a node in the list output
87
type nodeListEntry struct {
88
        NodeName         string `json:"nodeName"`
89
        Status           string `json:"status"`
90
        PackagesComplete int    `json:"packagesComplete"`
91
        PackagesTotal    int    `json:"packagesTotal"`
92
        Restarts         int32  `json:"restarts"`
93
}
94

95
func runNodeList(ctx context.Context, kubeClient *client.Client, opts *nodeListOptions, cliCtx *cliContext.CLIContext) error {
1✔
96
        out := cliCtx.Config().OutputWriter
1✔
97
        // Get all nodes
1✔
98
        nodeList, err := kubeClient.Kubernetes().CoreV1().Nodes().List(ctx, metav1.ListOptions{})
1✔
99
        if err != nil {
1✔
100
                return fmt.Errorf("listing nodes: %w", err)
×
101
        }
×
102

103
        annotationKey := nodeStateAnnotationPrefix + opts.skyhookName
1✔
104
        entries := make([]nodeListEntry, 0, len(nodeList.Items))
1✔
105

1✔
106
        for _, node := range nodeList.Items {
2✔
107
                annotation, ok := node.Annotations[annotationKey]
1✔
108
                if !ok {
2✔
109
                        continue
1✔
110
                }
111

112
                var nodeState v1alpha1.NodeState
1✔
113
                if err := json.Unmarshal([]byte(annotation), &nodeState); err != nil {
1✔
NEW
114
                        if cliCtx.GlobalFlags.Verbose {
×
NEW
115
                                _, _ = fmt.Fprintf(cliCtx.Config().ErrorWriter, "Warning: skipping node %q - invalid annotation: %v\n", node.Name, err)
×
NEW
116
                        }
×
UNCOV
117
                        continue
×
118
                }
119

120
                completeCount := 0
1✔
121
                hasError := false
1✔
122
                hasInProgress := false
1✔
123
                var totalRestarts int32
1✔
124

1✔
125
                for _, pkgStatus := range nodeState {
2✔
126
                        totalRestarts += pkgStatus.Restarts
1✔
127
                        switch pkgStatus.State {
1✔
128
                        case v1alpha1.StateComplete:
1✔
129
                                completeCount++
1✔
130
                        case v1alpha1.StateErroring:
1✔
131
                                hasError = true
1✔
132
                        case v1alpha1.StateInProgress:
1✔
133
                                hasInProgress = true
1✔
134
                        }
135
                }
136

137
                // Determine overall status
138
                status := string(v1alpha1.StateUnknown)
1✔
139
                if hasError {
2✔
140
                        status = string(v1alpha1.StateErroring)
1✔
141
                } else if completeCount == len(nodeState) && len(nodeState) > 0 {
3✔
142
                        status = string(v1alpha1.StateComplete)
1✔
143
                } else if hasInProgress || completeCount > 0 {
3✔
144
                        status = string(v1alpha1.StateInProgress)
1✔
145
                }
1✔
146

147
                entries = append(entries, nodeListEntry{
1✔
148
                        NodeName:         node.Name,
1✔
149
                        Status:           status,
1✔
150
                        PackagesComplete: completeCount,
1✔
151
                        PackagesTotal:    len(nodeState),
1✔
152
                        Restarts:         totalRestarts,
1✔
153
                })
1✔
154
        }
155

156
        // Sort by node name
157
        sort.Slice(entries, func(i, j int) bool {
2✔
158
                return entries[i].NodeName < entries[j].NodeName
1✔
159
        })
1✔
160

161
        if len(entries) == 0 {
2✔
162
                _, _ = fmt.Fprintf(out, "No nodes found for Skyhook %q\n", opts.skyhookName)
1✔
163
                return nil
1✔
164
        }
1✔
165

166
        // Output based on format
167
        output := nodeListOutput{SkyhookName: opts.skyhookName, Nodes: entries}
1✔
168
        switch cliCtx.GlobalFlags.OutputFormat {
1✔
169
        case utils.OutputFormatJSON:
1✔
170
                return utils.OutputJSON(out, output)
1✔
NEW
171
        case utils.OutputFormatYAML:
×
172
                return utils.OutputYAML(out, output)
×
NEW
173
        case utils.OutputFormatWide:
×
174
                return outputNodeListTableOrWide(out, opts.skyhookName, entries, true)
×
175
        default:
1✔
176
                return outputNodeListTableOrWide(out, opts.skyhookName, entries, false)
1✔
177
        }
178
}
179

180
// nodeListOutput is the structured output for JSON/YAML
181
type nodeListOutput struct {
182
        SkyhookName string          `json:"skyhookName" yaml:"skyhookName"`
183
        Nodes       []nodeListEntry `json:"nodes" yaml:"nodes"`
184
}
185

186
// nodeListTableConfig returns the table configuration for node list output
187
func nodeListTableConfig() utils.TableConfig[nodeListEntry] {
1✔
188
        return utils.TableConfig[nodeListEntry]{
1✔
189
                Headers: []string{"NODE", "STATUS", "PACKAGES"},
1✔
190
                Extract: func(e nodeListEntry) []string {
2✔
191
                        status := e.Status
1✔
192
                        if e.Status == string(v1alpha1.StateErroring) {
2✔
193
                                status = strings.ToUpper(status)
1✔
194
                        }
1✔
195
                        return []string{e.NodeName, status, fmt.Sprintf("%d/%d", e.PackagesComplete, e.PackagesTotal)}
1✔
196
                },
197
                WideHeaders: []string{"RESTARTS"},
198
                WideExtract: func(e nodeListEntry) []string {
1✔
199
                        return []string{fmt.Sprintf("%d", e.Restarts)}
1✔
200
                },
1✔
201
        }
202
}
203

204
func formatNodeListSummary(entries []nodeListEntry) string {
1✔
205
        totalNodes := len(entries)
1✔
206
        completeNodes := 0
1✔
207
        errorNodes := 0
1✔
208
        for _, e := range entries {
2✔
209
                switch e.Status {
1✔
210
                case string(v1alpha1.StateComplete):
1✔
211
                        completeNodes++
1✔
212
                case string(v1alpha1.StateErroring):
1✔
213
                        errorNodes++
1✔
214
                }
215
        }
216
        return fmt.Sprintf("Summary: %d nodes (%d complete, %d erroring, %d in progress)",
1✔
217
                totalNodes, completeNodes, errorNodes, totalNodes-completeNodes-errorNodes)
1✔
218
}
219

220
func outputNodeListTableOrWide(out io.Writer, skyhookName string, entries []nodeListEntry, wide bool) error {
1✔
221
        headerLine := fmt.Sprintf("Skyhook: %s\n\n%s", skyhookName, formatNodeListSummary(entries))
1✔
222
        if wide {
2✔
223
                return utils.OutputWideWithHeader(out, headerLine, nodeListTableConfig(), entries)
1✔
224
        }
1✔
225
        return utils.OutputTableWithHeader(out, headerLine, nodeListTableConfig(), entries)
1✔
226
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc