Skip to content

Commit 1a1f80c

Browse files
author
Mengqi Yu
committed
add alpha webhook cmd
1 parent 1a1e719 commit 1a1f80c

File tree

3 files changed

+155
-2
lines changed

3 files changed

+155
-2
lines changed

cmd/kubebuilder/alpha.go

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
/*
2+
Copyright 2018 The Kubernetes Authors.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
package main
18+
19+
import (
20+
"github.com/spf13/cobra"
21+
)
22+
23+
// newAlphaCommand returns alpha subcommand which will be mounted
24+
// at the root command by the caller.
25+
func newAlphaCommand() *cobra.Command {
26+
cmd := &cobra.Command{
27+
Use: "alpha",
28+
Short: "Exposes commands which are in experimental or early stages of development",
29+
Long: `Command group for commands which are either experimental or in early stages of development`,
30+
Example: `
31+
# scaffolds webhook server
32+
kubebuilder alpha webhook <params>
33+
`,
34+
}
35+
36+
cmd.AddCommand(
37+
newWebhookCmd(),
38+
)
39+
return cmd
40+
}

cmd/kubebuilder/main.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ func main() {
5656
version.NewVersionCmd(),
5757
newDocsCmd(),
5858
newVendorUpdateCmd(),
59+
newAlphaCommand(),
5960
)
6061

6162
if err := rootCmd.Execute(); err != nil {
@@ -91,9 +92,9 @@ After the scaffold is written, api will run make on the project.
9192
`,
9293
Example: `
9394
# Initialize your project
94-
kubebuilder init --domain example.com --license apache2 --owner "The Kubernetes authors"
95+
kubebuilder init --domain example.com --license apache2 --owner "The Kubernetes authors"
9596
96-
# Create a frigates API with Group: ship, Version: v1beta1 and Kind: Frigate
97+
# Create a frigates API with Group: ship, Version: v1beta1 and Kind: Frigate
9798
kubebuilder create api --group ship --version v1beta1 --kind Frigate
9899
99100
# Edit the API Scheme

cmd/kubebuilder/webhook.go

Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
/*
2+
Copyright 2018 The Kubernetes Authors.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
package main
18+
19+
import (
20+
"fmt"
21+
"log"
22+
"os"
23+
"os/exec"
24+
25+
"github.com/spf13/cobra"
26+
flag "github.com/spf13/pflag"
27+
28+
"k8s.io/apimachinery/pkg/api/meta"
29+
"k8s.io/apimachinery/pkg/runtime/schema"
30+
"sigs.k8s.io/controller-tools/pkg/scaffold"
31+
"sigs.k8s.io/controller-tools/pkg/scaffold/input"
32+
"sigs.k8s.io/controller-tools/pkg/scaffold/resource"
33+
"sigs.k8s.io/controller-tools/pkg/scaffold/webhook"
34+
)
35+
36+
func newWebhookCmd() *cobra.Command {
37+
o := webhookOptions{}
38+
39+
cmd := &cobra.Command{
40+
Use: "webhook",
41+
Short: "Scaffold a webhook server",
42+
Long: `Scaffold a webhook server if there is no existing server.
43+
Scaffolds webhook handlers based on group, version, kind and other user inputs.
44+
`,
45+
Example: ` # Create webhook for CRD of group crew, version v1 and kind FirstMate.
46+
# Set type to be mutating and operations to be create and update.
47+
kubebuilder webhook --group crew --version v1 --kind FirstMate --type=mutating --operations=create,update
48+
`,
49+
Run: func(cmd *cobra.Command, args []string) {
50+
dieIfNoProject()
51+
52+
fmt.Println("Writing scaffold for you to edit...")
53+
54+
if len(o.res.Resource) == 0 {
55+
gvr, _ := meta.UnsafeGuessKindToResource(schema.GroupVersionKind{
56+
Group: o.res.Group, Version: o.res.Version, Kind: o.res.Kind})
57+
o.res.Resource = gvr.Resource
58+
}
59+
60+
err := (&scaffold.Scaffold{}).Execute(input.Options{},
61+
&webhook.AdmissionHandler{Resource: o.res, Config: webhook.Config{Server: o.server, Type: o.webhookType, Operations: o.operations}},
62+
&webhook.AdmissionWebhookBuilder{Resource: o.res, Config: webhook.Config{Server: o.server, Type: o.webhookType, Operations: o.operations}},
63+
&webhook.AdmissionWebhooks{Resource: o.res, Config: webhook.Config{Server: o.server, Type: o.webhookType, Operations: o.operations}},
64+
&webhook.AddAdmissionWebhookBuilderHandler{Resource: o.res, Config: webhook.Config{Server: o.server, Type: o.webhookType, Operations: o.operations}},
65+
&webhook.Server{Resource: o.res, Config: webhook.Config{Server: o.server, Type: o.webhookType, Operations: o.operations}},
66+
&webhook.AddServer{Resource: o.res, Config: webhook.Config{Server: o.server, Type: o.webhookType, Operations: o.operations}},
67+
)
68+
if err != nil {
69+
log.Fatal(err)
70+
}
71+
72+
if o.doMake {
73+
fmt.Println("Running make...")
74+
cm := exec.Command("make") // #nosec
75+
cm.Stderr = os.Stderr
76+
cm.Stdout = os.Stdout
77+
if err := cm.Run(); err != nil {
78+
log.Fatal(err)
79+
}
80+
}
81+
},
82+
}
83+
cmd.Flags().StringVar(&o.server, "server", "default",
84+
"name of the server")
85+
cmd.Flags().StringVar(&o.webhookType, "type", "",
86+
"webhook type, e.g. mutating or validating")
87+
cmd.Flags().StringSliceVar(&o.operations, "operations", []string{"create"},
88+
"the operations that the webhook will intercept, e.g. create, update, delete and connect")
89+
cmd.Flags().BoolVar(&o.doMake, "make", true,
90+
"if true, run make after generating files")
91+
o.res = gvkForFlags(cmd.Flags())
92+
return cmd
93+
}
94+
95+
// webhookOptions represents commandline options for scaffolding a webhook.
96+
type webhookOptions struct {
97+
res *resource.Resource
98+
operations []string
99+
server string
100+
webhookType string
101+
doMake bool
102+
}
103+
104+
// gvkForFlags registers flags for Resource fields and returns the Resource
105+
func gvkForFlags(f *flag.FlagSet) *resource.Resource {
106+
r := &resource.Resource{}
107+
f.StringVar(&r.Group, "group", "", "resource Group")
108+
f.StringVar(&r.Version, "version", "", "resource Version")
109+
f.StringVar(&r.Kind, "kind", "", "resource Kind")
110+
f.StringVar(&r.Resource, "resource", "", "resource Resource")
111+
return r
112+
}

0 commit comments

Comments
 (0)