Skip to content

Commit a81b3df

Browse files
init
Signed-off-by: Yaroslav Borbat <[email protected]>
1 parent 4b4a98e commit a81b3df

File tree

32 files changed

+1520
-228
lines changed

32 files changed

+1520
-228
lines changed
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
module github.com/deckhouse/virtualization/api/client/examples/resourceclaim
2+
3+
go 1.24.7
4+
5+
require (
6+
github.com/deckhouse/virtualization/api v1.0.0
7+
github.com/spf13/cobra v1.10.1
8+
github.com/spf13/pflag v1.0.10
9+
)
10+
11+
require (
12+
github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc // indirect
13+
github.com/emicklei/go-restful/v3 v3.11.0 // indirect
14+
github.com/fxamacker/cbor/v2 v2.7.0 // indirect
15+
github.com/go-logr/logr v1.4.2 // indirect
16+
github.com/go-openapi/jsonpointer v0.21.0 // indirect
17+
github.com/go-openapi/jsonreference v0.20.2 // indirect
18+
github.com/go-openapi/swag v0.23.0 // indirect
19+
github.com/gogo/protobuf v1.3.2 // indirect
20+
github.com/google/gnostic-models v0.7.0 // indirect
21+
github.com/google/go-cmp v0.7.0 // indirect
22+
github.com/google/uuid v1.6.0 // indirect
23+
github.com/gorilla/websocket v1.5.4-0.20250319132907-e064f32e3674 // indirect
24+
github.com/inconshreveable/mousetrap v1.1.0 // indirect
25+
github.com/josharian/intern v1.0.0 // indirect
26+
github.com/json-iterator/go v1.1.12 // indirect
27+
github.com/mailru/easyjson v0.7.7 // indirect
28+
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
29+
github.com/modern-go/reflect2 v1.0.2 // indirect
30+
github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822 // indirect
31+
github.com/openshift/custom-resource-status v1.1.2 // indirect
32+
github.com/pkg/errors v0.9.1 // indirect
33+
github.com/x448/float16 v0.8.4 // indirect
34+
go.yaml.in/yaml/v2 v2.4.2 // indirect
35+
go.yaml.in/yaml/v3 v3.0.3 // indirect
36+
golang.org/x/net v0.39.0 // indirect
37+
golang.org/x/oauth2 v0.27.0 // indirect
38+
golang.org/x/sys v0.32.0 // indirect
39+
golang.org/x/term v0.31.0 // indirect
40+
golang.org/x/text v0.24.0 // indirect
41+
golang.org/x/time v0.9.0 // indirect
42+
google.golang.org/protobuf v1.36.5 // indirect
43+
gopkg.in/evanphx/json-patch.v4 v4.12.0 // indirect
44+
gopkg.in/inf.v0 v0.9.1 // indirect
45+
gopkg.in/yaml.v3 v3.0.1 // indirect
46+
k8s.io/api v0.33.3 // indirect
47+
k8s.io/apiextensions-apiserver v0.33.3 // indirect
48+
k8s.io/apimachinery v0.33.3 // indirect
49+
k8s.io/client-go v0.33.3 // indirect
50+
k8s.io/klog/v2 v2.130.1 // indirect
51+
k8s.io/kube-openapi v0.0.0-20250701173324-9bd5c66d9911 // indirect
52+
k8s.io/utils v0.0.0-20241104100929-3ea5e8cea738 // indirect
53+
kubevirt.io/api v1.6.2 // indirect
54+
kubevirt.io/containerized-data-importer-api v1.60.3-0.20241105012228-50fbed985de9 // indirect
55+
kubevirt.io/controller-lifecycle-operator-sdk/api v0.0.0-20220329064328-f3cc58c6ed90 // indirect
56+
sigs.k8s.io/controller-runtime v0.21.0 // indirect
57+
sigs.k8s.io/json v0.0.0-20241010143419-9aa6b5e7a4b3 // indirect
58+
sigs.k8s.io/randfill v1.0.0 // indirect
59+
sigs.k8s.io/structured-merge-diff/v4 v4.6.0 // indirect
60+
sigs.k8s.io/yaml v1.4.0 // indirect
61+
)
62+
63+
replace github.com/deckhouse/virtualization/api => ./../../../../api

api/client/examples/resourceclaim/go.sum

Lines changed: 392 additions & 0 deletions
Large diffs are not rendered by default.
Lines changed: 217 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,217 @@
1+
/*
2+
Copyright 2025 Flant JSC
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+
17+
package main
18+
19+
import (
20+
"context"
21+
"fmt"
22+
"os"
23+
"os/signal"
24+
"syscall"
25+
26+
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
27+
28+
"github.com/deckhouse/virtualization/api/client/kubeclient"
29+
subv1alpha2 "github.com/deckhouse/virtualization/api/subresources/v1alpha2"
30+
31+
"github.com/spf13/cobra"
32+
"github.com/spf13/pflag"
33+
)
34+
35+
func main() {
36+
ctx, _ := signal.NotifyContext(context.Background(), syscall.SIGTERM, os.Interrupt)
37+
if err := NewResourceClaimCommand().ExecuteContext(ctx); err != nil {
38+
_, _ = fmt.Fprintln(os.Stderr, err)
39+
os.Exit(1)
40+
}
41+
}
42+
43+
func NewResourceClaimCommand() *cobra.Command {
44+
cmd := cobra.Command{
45+
Use: "resourceclaim (VirtualMachine)",
46+
Short: "add/remove resource claim to/from a VirtualMachine.",
47+
Args: cobra.ExactArgs(1),
48+
SilenceErrors: true,
49+
SilenceUsage: true,
50+
}
51+
52+
cmd.AddCommand(
53+
NewAddResourceClaimCommand(),
54+
NewRemoveResourceClaimCommand(),
55+
)
56+
57+
return &cmd
58+
}
59+
60+
func NewAddResourceClaimCommand() *cobra.Command {
61+
opts := addResourceClaimOptions{}
62+
63+
cmd := cobra.Command{
64+
Use: "add (VirtualMachine)",
65+
Short: "add resource claim to a VirtualMachine.",
66+
Args: cobra.ExactArgs(1),
67+
SilenceErrors: true,
68+
SilenceUsage: true,
69+
RunE: opts.Run,
70+
}
71+
72+
opts.AddFlags(&cmd)
73+
74+
return &cmd
75+
}
76+
77+
type addResourceClaimOptions struct {
78+
Namespace string
79+
HotplugName string
80+
RequestName string
81+
ResourceClaimTemplateName string
82+
DryRun bool
83+
}
84+
85+
func (o *addResourceClaimOptions) AddFlags(cmd *cobra.Command) {
86+
fs := cmd.Flags()
87+
fs.StringVarP(&o.Namespace, "namespace", "n", "", "namespace of virtual machine")
88+
fs.StringVarP(&o.HotplugName, "hotplug-name", "h", "", "name of the hotplug device")
89+
fs.StringVarP(&o.RequestName, "request-name", "r", "", "name of the resource claim request")
90+
fs.StringVarP(&o.ResourceClaimTemplateName, "resource-claim-template-name", "t", "", "name of the resource claim template")
91+
fs.BoolVarP(&o.DryRun, "dry-run", "d", false, "dry run")
92+
93+
_ = cmd.MarkFlagRequired("hotplug-name")
94+
_ = cmd.MarkFlagRequired("request-name")
95+
_ = cmd.MarkFlagRequired("resource-claim-template-name")
96+
}
97+
98+
func (o *addResourceClaimOptions) Validate() error {
99+
if o.HotplugName == "" {
100+
return fmt.Errorf("hotplug-name is required")
101+
}
102+
if o.RequestName == "" {
103+
return fmt.Errorf("request-name is required")
104+
}
105+
if o.ResourceClaimTemplateName == "" {
106+
return fmt.Errorf("resource-claim-template-name is required")
107+
}
108+
return nil
109+
}
110+
111+
func (o *addResourceClaimOptions) Run(cmd *cobra.Command, args []string) error {
112+
err := o.Validate()
113+
if err != nil {
114+
return err
115+
}
116+
117+
client, namespace, err := getClientAndNamespace(o.Namespace)
118+
if err != nil {
119+
return err
120+
}
121+
122+
name := args[0]
123+
options := subv1alpha2.VirtualMachineAddResourceClaim{
124+
Name: o.HotplugName,
125+
ResourceClaimTemplateName: o.ResourceClaimTemplateName,
126+
RequestName: o.RequestName,
127+
DryRun: dryRun(o.DryRun),
128+
}
129+
130+
return client.VirtualMachines(namespace).AddResourceClaim(cmd.Context(), name, options)
131+
}
132+
133+
func NewRemoveResourceClaimCommand() *cobra.Command {
134+
opts := removeResourceClaimOptions{}
135+
136+
cmd := cobra.Command{
137+
Use: "remove (VirtualMachine)",
138+
Short: "remove resource claim from a VirtualMachine.",
139+
Args: cobra.ExactArgs(1),
140+
SilenceErrors: true,
141+
SilenceUsage: true,
142+
RunE: opts.Run,
143+
}
144+
145+
opts.AddFlags(&cmd)
146+
147+
return &cmd
148+
}
149+
150+
type removeResourceClaimOptions struct {
151+
Namespace string
152+
HotplugName string
153+
DryRun bool
154+
}
155+
156+
func (o *removeResourceClaimOptions) AddFlags(cmd *cobra.Command) {
157+
fs := cmd.Flags()
158+
fs.StringVarP(&o.Namespace, "namespace", "n", "", "namespace of virtual machine")
159+
fs.StringVarP(&o.HotplugName, "hotplug-name", "h", "", "name of the hotplug device")
160+
fs.BoolVarP(&o.DryRun, "dry-run", "d", false, "dry run")
161+
162+
_ = cmd.MarkFlagRequired("hotplug-name")
163+
}
164+
165+
func (o *removeResourceClaimOptions) Validate() error {
166+
if o.HotplugName == "" {
167+
return fmt.Errorf("hotplug-name is required")
168+
}
169+
return nil
170+
}
171+
172+
func (o *removeResourceClaimOptions) Run(cmd *cobra.Command, args []string) error {
173+
err := o.Validate()
174+
if err != nil {
175+
return err
176+
}
177+
178+
client, namespace, err := getClientAndNamespace(o.Namespace)
179+
if err != nil {
180+
return err
181+
}
182+
183+
name := args[0]
184+
options := subv1alpha2.VirtualMachineRemoveResourceClaim{
185+
Name: o.HotplugName,
186+
DryRun: dryRun(o.DryRun),
187+
}
188+
189+
return client.VirtualMachines(namespace).RemoveResourceClaim(cmd.Context(), name, options)
190+
}
191+
192+
func getClientAndNamespace(defaultNamespace string) (kubeclient.Client, string, error) {
193+
namespace := defaultNamespace
194+
clientConfig := kubeclient.DefaultClientConfig(&pflag.FlagSet{})
195+
196+
if namespace == "" {
197+
ns, _, err := clientConfig.Namespace()
198+
if err != nil {
199+
return nil, "", err
200+
}
201+
namespace = ns
202+
}
203+
204+
client, err := kubeclient.GetClientFromClientConfig(clientConfig)
205+
if err != nil {
206+
return nil, "", err
207+
}
208+
209+
return client, namespace, nil
210+
}
211+
212+
func dryRun(should bool) []string {
213+
if should {
214+
return []string{metav1.DryRunAll}
215+
}
216+
return nil
217+
}

api/client/generated/clientset/versioned/typed/core/v1alpha2/fake/fake_virtualmachine_expansion.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,3 +54,11 @@ func (c *fakeVirtualMachines) RemoveVolume(ctx context.Context, name string, opt
5454
func (c *fakeVirtualMachines) CancelEvacuation(ctx context.Context, name string, dryRun []string) error {
5555
return nil
5656
}
57+
58+
func (c *fakeVirtualMachines) AddResourceClaim(ctx context.Context, name string, opts v1alpha2.VirtualMachineAddResourceClaim) error {
59+
return nil
60+
}
61+
62+
func (c *fakeVirtualMachines) RemoveResourceClaim(ctx context.Context, name string, opts v1alpha2.VirtualMachineRemoveResourceClaim) error {
63+
return nil
64+
}

api/client/generated/clientset/versioned/typed/core/v1alpha2/virtualmachine_expansion.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,8 @@ type VirtualMachineExpansion interface {
3535
AddVolume(ctx context.Context, name string, opts v1alpha2.VirtualMachineAddVolume) error
3636
RemoveVolume(ctx context.Context, name string, opts v1alpha2.VirtualMachineRemoveVolume) error
3737
CancelEvacuation(ctx context.Context, name string, dryRun []string) error
38+
AddResourceClaim(ctx context.Context, name string, opts v1alpha2.VirtualMachineAddResourceClaim) error
39+
RemoveResourceClaim(ctx context.Context, name string, opts v1alpha2.VirtualMachineRemoveResourceClaim) error
3840
}
3941

4042
type SerialConsoleOptions struct {
@@ -81,3 +83,11 @@ func (c *virtualMachines) RemoveVolume(ctx context.Context, name string, opts v1
8183
func (c *virtualMachines) CancelEvacuation(ctx context.Context, name string, dryRun []string) error {
8284
return fmt.Errorf("not implemented")
8385
}
86+
87+
func (c *virtualMachines) AddResourceClaim(ctx context.Context, name string, opts v1alpha2.VirtualMachineAddResourceClaim) error {
88+
return fmt.Errorf("not implemented")
89+
}
90+
91+
func (c *virtualMachines) RemoveResourceClaim(ctx context.Context, name string, opts v1alpha2.VirtualMachineRemoveResourceClaim) error {
92+
return fmt.Errorf("not implemented")
93+
}

api/client/kubeclient/vm.go

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -165,3 +165,25 @@ func (v vm) CancelEvacuation(ctx context.Context, name string, dryRun []string)
165165
}
166166
return c.Do(ctx).Error()
167167
}
168+
169+
func (v vm) AddResourceClaim(ctx context.Context, name string, opts subv1alpha2.VirtualMachineAddResourceClaim) error {
170+
path := fmt.Sprintf(subresourceURLTpl, v.namespace, v.resource, name, "addresourceclaim")
171+
return v.restClient.
172+
Put().
173+
AbsPath(path).
174+
Param("name", opts.Name).
175+
Param("resourceClaimTemplateName", opts.ResourceClaimTemplateName).
176+
Param("requestName", opts.RequestName).
177+
Do(ctx).
178+
Error()
179+
}
180+
181+
func (v vm) RemoveResourceClaim(ctx context.Context, name string, opts subv1alpha2.VirtualMachineRemoveResourceClaim) error {
182+
path := fmt.Sprintf(subresourceURLTpl, v.namespace, v.resource, name, "removeresourceclaim")
183+
return v.restClient.
184+
Put().
185+
AbsPath(path).
186+
Param("name", opts.Name).
187+
Do(ctx).
188+
Error()
189+
}

api/subresources/register.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,8 @@ func addKnownTypes(scheme *runtime.Scheme) error {
5757
&VirtualMachineFreeze{},
5858
&VirtualMachineUnfreeze{},
5959
&VirtualMachineCancelEvacuation{},
60+
&VirtualMachineAddResourceClaim{},
61+
&VirtualMachineRemoveResourceClaim{},
6062
)
6163
return nil
6264
}

api/subresources/types.go

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,3 +88,24 @@ type VirtualMachineCancelEvacuation struct {
8888

8989
DryRun []string
9090
}
91+
92+
// +k8s:deepcopy-gen:interfaces=k8s.io/apimachinery/pkg/runtime.Object
93+
94+
type VirtualMachineAddResourceClaim struct {
95+
metav1.TypeMeta
96+
97+
Name string
98+
ResourceClaimTemplateName string
99+
RequestName string
100+
101+
DryRun []string
102+
}
103+
104+
// +k8s:deepcopy-gen:interfaces=k8s.io/apimachinery/pkg/runtime.Object
105+
106+
type VirtualMachineRemoveResourceClaim struct {
107+
metav1.TypeMeta
108+
109+
Name string
110+
DryRun []string
111+
}

api/subresources/v1alpha2/register.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,8 @@ func addKnownTypes(scheme *runtime.Scheme) error {
5959
&VirtualMachineFreeze{},
6060
&VirtualMachineUnfreeze{},
6161
&VirtualMachineCancelEvacuation{},
62+
&VirtualMachineAddResourceClaim{},
63+
&VirtualMachineRemoveResourceClaim{},
6264
)
6365
return nil
6466
}

0 commit comments

Comments
 (0)