-
Notifications
You must be signed in to change notification settings - Fork 1.3k
/
restart.go
83 lines (70 loc) · 2.88 KB
/
restart.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
/*
* This file is part of the KubeVirt project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* Copyright 2023 Red Hat, Inc.
*
*/
package vm
import (
"context"
"fmt"
"github.com/spf13/cobra"
"k8s.io/client-go/tools/clientcmd"
v1 "kubevirt.io/api/core/v1"
"kubevirt.io/kubevirt/pkg/virtctl/templates"
)
const COMMAND_RESTART = "restart"
func NewRestartCommand(clientConfig clientcmd.ClientConfig) *cobra.Command {
cmd := &cobra.Command{
Use: "restart (VM)",
Short: "Restart a virtual machine.",
Example: usage(COMMAND_RESTART),
Args: templates.ExactArgs("restart", 1),
RunE: func(cmd *cobra.Command, args []string) error {
c := Command{command: COMMAND_RESTART, clientConfig: clientConfig}
return c.restartRun(args, cmd)
},
}
cmd.Flags().BoolVar(&forceRestart, forceArg, false, "--force=false: Only used when grace-period=0. If true, immediately remove VMI pod from API and bypass graceful deletion. Note that immediate deletion of some resources may result in inconsistency or data loss and requires confirmation.")
cmd.Flags().Int64Var(&gracePeriod, gracePeriodArg, -1, "--grace-period=-1: Period of time in seconds given to the VMI to terminate gracefully. Can only be set to 0 when --force is true (force deletion). Currently only setting 0 is supported.")
cmd.Flags().BoolVar(&dryRun, dryRunArg, false, dryRunCommandUsage)
cmd.SetUsageTemplate(templates.UsageTemplate())
return cmd
}
func (o *Command) restartRun(args []string, cmd *cobra.Command) error {
vmiName := args[0]
virtClient, namespace, err := GetNamespaceAndClient(o.clientConfig)
if err != nil {
return err
}
dryRunOption := setDryRunOption(dryRun)
gracePeriodChanged := cmd.Flags().Changed(gracePeriodArg)
if gracePeriodChanged != forceRestart {
return fmt.Errorf("Must both use --force=true and set --grace-period.")
}
if forceRestart {
err = virtClient.VirtualMachine(namespace).ForceRestart(context.Background(), vmiName, &v1.RestartOptions{GracePeriodSeconds: &gracePeriod, DryRun: dryRunOption})
if err != nil {
return fmt.Errorf("Error force restarting VirtualMachine, %v", err)
}
} else {
err = virtClient.VirtualMachine(namespace).Restart(context.Background(), vmiName, &v1.RestartOptions{DryRun: dryRunOption})
if err != nil {
return fmt.Errorf("Error restarting VirtualMachine %v", err)
}
}
fmt.Printf("VM %s was scheduled to %s\n", vmiName, o.command)
return nil
}