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

kubevirt / kubevirt / 4183aeac-648c-4455-a489-517326f70747

24 Jun 2025 11:50PM UTC coverage: 70.758%. Remained the same
4183aeac-648c-4455-a489-517326f70747

push

prow

web-flow
Merge pull request #14935 from alromeros/virtctl-object-graph

VEP 41: Add virtctl objectgraph command

75 of 84 new or added lines in 5 files covered. (89.29%)

12 existing lines in 1 file now uncovered.

66438 of 93895 relevant lines covered (70.76%)

0.79 hits per line

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

92.5
/pkg/virtctl/objectgraph/objectgraph.go
1
/*
2
 * This file is part of the KubeVirt project
3
 *
4
 * Licensed under the Apache License, Version 2.0 (the "License");
5
 * you may not use this file except in compliance with the License.
6
 * You may obtain a copy of the License at
7
 *
8
 *     http://www.apache.org/licenses/LICENSE-2.0
9
 *
10
 * Unless required by applicable law or agreed to in writing, software
11
 * distributed under the License is distributed on an "AS IS" BASIS,
12
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
 * See the License for the specific language governing permissions and
14
 * limitations under the License.
15
 *
16
 * Copyright The KubeVirt Authors.
17
 *
18
 */
19

20
package objectgraph
21

22
import (
23
        "encoding/json"
24
        "fmt"
25

26
        "github.com/spf13/cobra"
27
        metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
28
        v1 "kubevirt.io/api/core/v1"
29
        "sigs.k8s.io/yaml"
30

31
        "kubevirt.io/kubevirt/pkg/virtctl/clientconfig"
32
        "kubevirt.io/kubevirt/pkg/virtctl/templates"
33
)
34

35
type command struct {
36
        vmi            bool
37
        shouldExclude  bool
38
        labelSelectors map[string]string
39
        outputFormat   string
40
}
41

42
func NewCommand() *cobra.Command {
1✔
43
        c := command{}
1✔
44
        cmd := &cobra.Command{
1✔
45
                Use:     "objectgraph (VM|VMI)",
1✔
46
                Short:   "Returns dependency graph related to a VM|VMI.",
1✔
47
                Example: usageObjectGraph(),
1✔
48
                Args:    cobra.ExactArgs(1),
1✔
49
                RunE:    c.objectGraphRun,
1✔
50
        }
1✔
51

1✔
52
        cmd.Flags().BoolVar(&c.vmi, "vmi", false, "Returns the object graph from the VMI instead of the VM.")
1✔
53
        cmd.Flags().BoolVar(&c.shouldExclude, "exclude-optional", false, "Exclude optional nodes from the object graph.")
1✔
54
        cmd.Flags().StringToStringVar(&c.labelSelectors, "selector", map[string]string{}, "Label selectors to filter the object graph (multiple labels can be specified).")
1✔
55
        cmd.Flags().StringVarP(&c.outputFormat, "output", "o", "json", "Output format. One of: json|yaml")
1✔
56
        cmd.SetUsageTemplate(templates.UsageTemplate())
1✔
57

1✔
58
        return cmd
1✔
59
}
1✔
60

61
// usageObjectGraph provides several valid usage examples of objectgraph
62
func usageObjectGraph() string {
1✔
63
        return `
1✔
64
  # Get the object graph for a VirtualMachine named 'my-vm'
1✔
65
  {{ProgramName}} objectgraph my-vm
1✔
66

1✔
67
  # Get the object graph for a VirtualMachineInstance named 'my-vmi'
1✔
68
  {{ProgramName}} objectgraph --vmi my-vmi
1✔
69

1✔
70
  # Exclude optional nodes in the object graph for a VM
1✔
71
  {{ProgramName}} objectgraph my-vm --exclude-optional
1✔
72

1✔
73
  # Filter the object graph by label selector
1✔
74
  {{ProgramName}} objectgraph my-vm --selector="kubevirt.io/dependency-type=storage,kubevirt.io/dependency-type=compute"
1✔
75

1✔
76
  # Get the object graph in YAML format
1✔
77
  {{ProgramName}} objectgraph my-vm --output yaml
1✔
78
`
1✔
79
}
1✔
80

81
func (c *command) objectGraphRun(cmd *cobra.Command, args []string) error {
1✔
82
        vmName := args[0]
1✔
83

1✔
84
        virtClient, namespace, _, err := clientconfig.ClientAndNamespaceFromContext(cmd.Context())
1✔
85
        if err != nil {
1✔
NEW
86
                return err
×
NEW
87
        }
×
88

89
        include := !c.shouldExclude
1✔
90
        opts := &v1.ObjectGraphOptions{
1✔
91
                IncludeOptionalNodes: &include,
1✔
92
        }
1✔
93

1✔
94
        if len(c.labelSelectors) > 0 {
2✔
95
                opts.LabelSelector = &metav1.LabelSelector{
1✔
96
                        MatchLabels: c.labelSelectors,
1✔
97
                }
1✔
98
        }
1✔
99

100
        var objectGraph v1.ObjectGraphNode
1✔
101
        if c.vmi {
2✔
102
                objectGraph, err = virtClient.VirtualMachineInstance(namespace).ObjectGraph(cmd.Context(), vmName, opts)
1✔
103
                if err != nil {
2✔
104
                        return fmt.Errorf("error listing object graph of VirtualMachineInstance %s: %v", vmName, err)
1✔
105
                }
1✔
106
        } else {
1✔
107
                objectGraph, err = virtClient.VirtualMachine(namespace).ObjectGraph(cmd.Context(), vmName, opts)
1✔
108
                if err != nil {
2✔
109
                        return fmt.Errorf("error listing object graph of VirtualMachine %s: %v", vmName, err)
1✔
110
                }
1✔
111
        }
112

113
        var output []byte
1✔
114
        switch c.outputFormat {
1✔
115
        case "json":
1✔
116
                output, err = json.MarshalIndent(objectGraph, "", "  ")
1✔
117
                if err != nil {
1✔
NEW
118
                        return fmt.Errorf("cannot marshal object graph to JSON: %v", err)
×
NEW
119
                }
×
120
        case "yaml":
1✔
121
                output, err = yaml.Marshal(objectGraph)
1✔
122
                if err != nil {
1✔
NEW
123
                        return fmt.Errorf("cannot marshal object graph to YAML: %v", err)
×
NEW
124
                }
×
125
        default:
1✔
126
                return fmt.Errorf("unsupported output format: %s (must be 'json' or 'yaml')", c.outputFormat)
1✔
127
        }
128

129
        cmd.Println(string(output))
1✔
130
        return nil
1✔
131
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE TRIAL · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc