-
Notifications
You must be signed in to change notification settings - Fork 1.4k
/
webhook.go
151 lines (116 loc) · 4.95 KB
/
webhook.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
/*
Copyright 2020 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.
*/
//go:deprecated This package has been deprecated in favor of v4
package v3
import (
"fmt"
"github.com/spf13/pflag"
"sigs.k8s.io/kubebuilder/v3/pkg/config"
"sigs.k8s.io/kubebuilder/v3/pkg/machinery"
"sigs.k8s.io/kubebuilder/v3/pkg/model/resource"
"sigs.k8s.io/kubebuilder/v3/pkg/plugin"
pluginutil "sigs.k8s.io/kubebuilder/v3/pkg/plugin/util"
goPlugin "sigs.k8s.io/kubebuilder/v3/pkg/plugins/golang"
"sigs.k8s.io/kubebuilder/v3/pkg/plugins/golang/v3/scaffolds"
)
// defaultWebhookVersion is the default mutating/validating webhook config API version to scaffold.
const defaultWebhookVersion = "v1"
var _ plugin.CreateWebhookSubcommand = &createWebhookSubcommand{}
type createWebhookSubcommand struct {
config config.Config
// For help text.
commandName string
options *goPlugin.Options
resource *resource.Resource
// force indicates that the resource should be created even if it already exists
force bool
}
func (p *createWebhookSubcommand) UpdateMetadata(cliMeta plugin.CLIMetadata, subcmdMeta *plugin.SubcommandMetadata) {
p.commandName = cliMeta.CommandName
subcmdMeta.Description = `Scaffold a webhook for an API resource. You can choose to scaffold defaulting,
validating and/or conversion webhooks.
`
subcmdMeta.Examples = fmt.Sprintf(` # Create defaulting and validating webhooks for Group: ship, Version: v1beta1
# and Kind: Frigate
%[1]s create webhook --group ship --version v1beta1 --kind Frigate --defaulting --programmatic-validation
# Create conversion webhook for Group: ship, Version: v1beta1
# and Kind: Frigate
%[1]s create webhook --group ship --version v1beta1 --kind Frigate --conversion
`, cliMeta.CommandName)
}
func (p *createWebhookSubcommand) BindFlags(fs *pflag.FlagSet) {
p.options = &goPlugin.Options{}
fs.StringVar(&p.options.Plural, "plural", "", "resource irregular plural form")
fs.StringVar(&p.options.WebhookVersion, "webhook-version", defaultWebhookVersion,
"version of {Mutating,Validating}WebhookConfigurations to scaffold. Options: [v1, v1beta1]")
fs.BoolVar(&p.options.DoDefaulting, "defaulting", false,
"if set, scaffold the defaulting webhook")
fs.BoolVar(&p.options.DoValidation, "programmatic-validation", false,
"if set, scaffold the validating webhook")
fs.BoolVar(&p.options.DoConversion, "conversion", false,
"if set, scaffold the conversion webhook")
fs.BoolVar(&p.force, "force", false,
"attempt to create resource even if it already exists")
// (not required raise an error in this case)
// nolint:errcheck,gosec
fs.MarkDeprecated("webhook-version", deprecateMsg)
}
func (p *createWebhookSubcommand) InjectConfig(c config.Config) error {
p.config = c
return nil
}
func (p *createWebhookSubcommand) InjectResource(res *resource.Resource) error {
p.resource = res
p.options.UpdateResource(p.resource, p.config)
if err := p.resource.Validate(); err != nil {
return err
}
if !p.resource.HasDefaultingWebhook() && !p.resource.HasValidationWebhook() && !p.resource.HasConversionWebhook() {
return fmt.Errorf("%s create webhook requires at least one of --defaulting,"+
" --programmatic-validation and --conversion to be true", p.commandName)
}
// check if resource exist to create webhook
if r, err := p.config.GetResource(p.resource.GVK); err != nil {
return fmt.Errorf("%s create webhook requires a previously created API ", p.commandName)
} else if r.Webhooks != nil && !r.Webhooks.IsEmpty() && !p.force {
return fmt.Errorf("webhook resource already exists")
}
// nolint:staticcheck
if pluginutil.HasDifferentWebhookVersion(p.config, p.resource.Webhooks.WebhookVersion) {
return fmt.Errorf("only one webhook version can be used for all resources, cannot add %q",
p.resource.Webhooks.WebhookVersion)
}
return nil
}
func (p *createWebhookSubcommand) Scaffold(fs machinery.Filesystem) error {
scaffolder := scaffolds.NewWebhookScaffolder(p.config, *p.resource, p.force)
scaffolder.InjectFS(fs)
return scaffolder.Scaffold()
}
func (p *createWebhookSubcommand) PostScaffold() error {
if p.resource.Webhooks.WebhookVersion == "v1beta1" {
if err := applyScaffoldCustomizationsForVbeta1(); err != nil {
return err
}
}
err := pluginutil.RunCmd("Update dependencies", "go", "mod", "tidy")
if err != nil {
return err
}
err = pluginutil.RunCmd("Running make", "make", "generate")
if err != nil {
return err
}
fmt.Print("Next: implement your new Webhook and generate the manifests with:\n$ make manifests\n")
return nil
}