@@ -17,6 +17,7 @@ limitations under the License.
17
17
package v3
18
18
19
19
import (
20
+ "errors"
20
21
"fmt"
21
22
"io/ioutil"
22
23
"path/filepath"
@@ -44,6 +45,9 @@ type createWebhookSubcommand struct {
44
45
validation bool
45
46
conversion bool
46
47
48
+ // force indicates that the resource should be created even if it already exists
49
+ force bool
50
+
47
51
// runMake indicates whether to run make or not after scaffolding webhooks
48
52
runMake bool
49
53
}
@@ -79,6 +83,8 @@ func (p *createWebhookSubcommand) BindFlags(fs *pflag.FlagSet) {
79
83
"version of {Mutating,Validating}WebhookConfigurations to scaffold. Options: [v1, v1beta1]" )
80
84
81
85
fs .BoolVar (& p .runMake , "make" , true , "if true, run make after generating files" )
86
+ fs .BoolVar (& p .force , "force" , false ,
87
+ "attempt to create resource even if it already exists" )
82
88
83
89
fs .BoolVar (& p .defaulting , "defaulting" , false ,
84
90
"if set, scaffold the defaulting webhook" )
@@ -112,6 +118,10 @@ func (p *createWebhookSubcommand) Validate() error {
112
118
" kind and version provided" , p .commandName )
113
119
}
114
120
121
+ if p .config .HasWebhook (p .resource .Data ()) && ! p .force {
122
+ return errors .New ("webhook resource already exists" )
123
+ }
124
+
115
125
if ! p .config .IsWebhookVersionCompatible (p .resource .Webhooks .WebhookVersion ) {
116
126
return fmt .Errorf ("only one webhook version can be used for all resources, cannot add %q" ,
117
127
p .resource .Webhooks .WebhookVersion )
@@ -129,7 +139,8 @@ func (p *createWebhookSubcommand) GetScaffolder() (cmdutil.Scaffolder, error) {
129
139
130
140
// Create the actual resource from the resource options
131
141
res := p .resource .NewResource (p .config , false )
132
- return scaffolds .NewWebhookScaffolder (p .config , string (bp ), res , p .defaulting , p .validation , p .conversion ), nil
142
+ return scaffolds .NewWebhookScaffolder (p .config , string (bp ), res , p .defaulting , p .validation , p .conversion ,
143
+ p .force ), nil
133
144
}
134
145
135
146
func (p * createWebhookSubcommand ) PostScaffold () error {
0 commit comments