This repository has been archived by the owner on May 6, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 382
/
Copy pathunbind_cmd.go
210 lines (176 loc) · 5.51 KB
/
unbind_cmd.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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
/*
Copyright 2018 The Kubernetes Authors.
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.
*/
package binding
import (
"bufio"
"fmt"
"os"
"strings"
"sync"
"github.com/pkg/errors"
"github.com/kubernetes-incubator/service-catalog/cmd/svcat/command"
"github.com/kubernetes-incubator/service-catalog/cmd/svcat/output"
"github.com/spf13/cobra"
apierrors "k8s.io/apimachinery/pkg/api/errors"
"k8s.io/apimachinery/pkg/types"
)
type unbindCmd struct {
*command.Namespaced
*command.Waitable
instanceName string
bindingNames []string
abandon bool
skipPrompt bool
}
// NewUnbindCmd builds a "svcat unbind" command
func NewUnbindCmd(cxt *command.Context) *cobra.Command {
unbindCmd := &unbindCmd{
Namespaced: command.NewNamespaced(cxt),
Waitable: command.NewWaitable(),
}
cmd := &cobra.Command{
Use: "unbind INSTANCE_NAME",
Short: "Unbinds an instance. When an instance name is specified, all of its bindings are removed, otherwise use --name to remove a specific binding",
Example: command.NormalizeExamples(`
svcat unbind wordpress-mysql-instance
svcat unbind --name wordpress-mysql-binding
svcat unbind --abandon wordpress-mysql-instance
`),
PreRunE: command.PreRunE(unbindCmd),
RunE: command.RunE(unbindCmd),
}
unbindCmd.AddNamespaceFlags(cmd.Flags(), false)
cmd.Flags().StringSliceVar(
&unbindCmd.bindingNames,
"name",
[]string{},
"The name of the binding to remove",
)
cmd.Flags().BoolVar(
&unbindCmd.abandon,
"abandon",
false,
"Forcefully and immediately delete the resource from Service Catalog ONLY, potentially abandoning any broker resources that you may continue to be charged for.",
)
cmd.Flags().BoolVarP(
&unbindCmd.skipPrompt,
"yes",
"y",
false,
`Automatic yes to prompts. Assume "yes" as answer to all prompts and run non-interactively.`,
)
unbindCmd.AddWaitFlags(cmd)
return cmd
}
// Validate checks that the required arguments have been provided
func (c *unbindCmd) Validate(args []string) error {
if len(args) == 0 {
if len(c.bindingNames) == 0 {
return fmt.Errorf("an instance or binding name is required")
}
} else {
c.instanceName = args[0]
}
return nil
}
// Run delete bindings by the name of the instance.
func (c *unbindCmd) Run() error {
// Indicates an error occurred and that a non-zero exit code should be used
var hasErrors bool
var bindings []types.NamespacedName
var err error
if c.abandon {
fmt.Fprintln(c.Output, "This action is not reversible and may cause you to be charged for the broker resources that are abandoned.")
if !c.skipPrompt {
fmt.Fprintln(c.Output, "Are you sure? [y|n]: ")
s := bufio.NewScanner(os.Stdin)
s.Scan()
err = s.Err()
fmt.Fprintln(c.Output, err)
if strings.ToLower(s.Text()) != "y" {
err = fmt.Errorf("aborted abandon operation")
return err
}
}
if c.instanceName != "" {
_, err = c.App.RemoveBindingFinalizerByInstance(c.Namespace, c.instanceName)
} else {
retrievedBindings := c.getBindingsToDelete()
_, err = c.App.RemoveFinalizerForBindings(retrievedBindings)
}
if err != nil {
return err
}
}
if c.instanceName != "" {
bindings, err = c.App.Unbind(c.Namespace, c.instanceName)
} else {
bindings, err = c.App.DeleteBindings(c.getBindingsToDelete())
}
if err != nil {
// Do not return immediately as we still need to potentially wait or print the deleted bindings
hasErrors = true
fmt.Fprintln(c.Output, err)
}
if c.Wait {
hasErrors = c.waitForBindingDeletes("waiting for the binding(s) to be deleted...", bindings...) || hasErrors
} else {
for _, binding := range bindings {
output.WriteDeletedResourceName(c.Output, binding.Name)
}
}
if hasErrors {
return errors.New("could not remove all bindings")
}
return nil
}
func (c *unbindCmd) getBindingsToDelete() []types.NamespacedName {
bindings := []types.NamespacedName{}
for _, name := range c.bindingNames {
bindings = append(bindings, types.NamespacedName{Namespace: c.Namespace, Name: name})
}
return bindings
}
// waitForBindingDeletes waits for the bindings to be deleted and prints either
// and error message or the name of the deleted binding.
func (c *unbindCmd) waitForBindingDeletes(waitMessage string, bindings ...types.NamespacedName) bool {
if len(bindings) == 0 {
return false
}
// Indicates an error occurred and that a non-zero exit code should be used
var hasErrors bool
// Used to prevent concurrent writes to c.Output
var mutex sync.Mutex
fmt.Fprintln(c.Output, waitMessage)
var g sync.WaitGroup
for _, binding := range bindings {
g.Add(1)
go func(ns, name string) {
defer g.Done()
binding, err := c.App.WaitForBinding(ns, name, c.Interval, c.Timeout)
mutex.Lock()
defer mutex.Unlock()
if err != nil && !apierrors.IsNotFound(errors.Cause(err)) {
hasErrors = true
fmt.Fprintln(c.Output, err)
} else if c.App.IsBindingFailed(binding) {
hasErrors = true
fmt.Fprintf(c.Output, "could not delete binding %s/%s\n", ns, name)
} else {
output.WriteDeletedResourceName(c.Output, name)
}
}(binding.Namespace, binding.Name)
}
g.Wait()
return hasErrors
}