11
11
except ImportError :
12
12
logging .error ("Couldn\' t import kubernetes.client.exceptions.ApiException - not found or not accessible" )
13
13
14
- API_VERSION = 'kubevirt.io/v1'
15
-
16
14
def get_nodes_list (conn , options ):
17
15
logging .debug ("Starting list/monitor operation" )
18
16
result = {}
19
17
try :
18
+ apiversion = options .get ("--apiversion" )
20
19
namespace = options .get ("--namespace" )
21
20
include_uninitialized = True
22
- vm_api = conn .resources .get (api_version = API_VERSION , kind = 'VirtualMachine' )
21
+ vm_api = conn .resources .get (api_version = apiversion , kind = 'VirtualMachine' )
23
22
vm_list = vm_api .get (namespace = namespace )
24
23
for vm in vm_list .items :
25
24
result [vm .metadata .name ] = ("" , None )
@@ -30,9 +29,10 @@ def get_nodes_list(conn, options):
30
29
def get_power_status (conn , options ):
31
30
logging .debug ("Starting get status operation" )
32
31
try :
32
+ apiversion = options .get ("--apiversion" )
33
33
namespace = options .get ("--namespace" )
34
34
name = options .get ("--plug" )
35
- vmi_api = conn .resources .get (api_version = API_VERSION ,
35
+ vmi_api = conn .resources .get (api_version = apiversion ,
36
36
kind = 'VirtualMachineInstance' )
37
37
vmi = vmi_api .get (name = name , namespace = namespace )
38
38
if vmi is not None :
@@ -52,35 +52,45 @@ def get_power_status(conn, options):
52
52
def set_power_status (conn , options ):
53
53
logging .debug ("Starting set status operation" )
54
54
try :
55
+ apiversion = options .get ("--apiversion" )
55
56
namespace = options .get ("--namespace" )
56
57
name = options .get ("--plug" )
57
58
action = 'start' if options ["--action" ] == "on" else 'stop'
58
- virtctl_vm_action (conn , action , namespace , name )
59
+ virtctl_vm_action (conn , action , namespace , name , apiversion )
59
60
except Exception as e :
60
61
logging .error ("Failed to set power status, with Exception: %s" , e )
61
62
fail (EC_STATUS )
62
63
63
64
def define_new_opts ():
64
- all_opt ["namespace" ] = {
65
- "getopt" : ":" ,
66
- "longopt" : "namespace" ,
67
- "help" : "--namespace=[namespace] Namespace of the KubeVirt machine" ,
68
- "shortdesc" : "Namespace of the KubeVirt machine." ,
69
- "required" : "1" ,
70
- "order" : 2
71
- }
72
- all_opt ["kubeconfig" ] = {
73
- "getopt" : ":" ,
74
- "longopt" : "kubeconfig" ,
75
- "help" : "--kubeconfig=[kubeconfig] Kubeconfig file path" ,
76
- "shortdesc" : "Kubeconfig file path" ,
77
- "required" : "0" ,
78
- "order" : 4
79
- }
80
-
81
- def virtctl_vm_action (conn , action , namespace , name ):
65
+ all_opt ["namespace" ] = {
66
+ "getopt" : ":" ,
67
+ "longopt" : "namespace" ,
68
+ "help" : "--namespace=[namespace] Namespace of the KubeVirt machine" ,
69
+ "shortdesc" : "Namespace of the KubeVirt machine." ,
70
+ "required" : "1" ,
71
+ "order" : 2
72
+ }
73
+ all_opt ["kubeconfig" ] = {
74
+ "getopt" : ":" ,
75
+ "longopt" : "kubeconfig" ,
76
+ "help" : "--kubeconfig=[kubeconfig] Kubeconfig file path" ,
77
+ "shortdesc" : "Kubeconfig file path" ,
78
+ "required" : "0" ,
79
+ "order" : 4
80
+ }
81
+ all_opt ["apiversion" ] = {
82
+ "getopt" : ":" ,
83
+ "longopt" : "apiversion" ,
84
+ "help" : "--apiversion=[apiversion] Version of the KubeVirt API" ,
85
+ "shortdesc" : "Version of the KubeVirt API." ,
86
+ "required" : "0" ,
87
+ "default" : "kubevirt.io/v1" ,
88
+ "order" : 5
89
+ }
90
+
91
+ def virtctl_vm_action (conn , action , namespace , name , apiversion ):
82
92
path = '/apis/subresources.{api_version}/namespaces/{namespace}/virtualmachines/{name}/{action}'
83
- path = path .format (api_version = API_VERSION , namespace = namespace , name = name , action = action )
93
+ path = path .format (api_version = apiversion , namespace = namespace , name = name , action = action )
84
94
return conn .request ('put' , path , header_params = {'accept' : '*/*' })
85
95
86
96
def validate_options (required_options_list , options ):
@@ -92,7 +102,7 @@ def validate_options(required_options_list, options):
92
102
def main ():
93
103
conn = None
94
104
95
- device_opt = ["port" , "namespace" , "kubeconfig" , "ssl_insecure" , "no_password" ]
105
+ device_opt = ["port" , "namespace" , "kubeconfig" , "ssl_insecure" , "no_password" , "apiversion" ]
96
106
define_new_opts ()
97
107
options = check_input (device_opt , process_input (device_opt ))
98
108
0 commit comments