|
| 1 | +/* |
| 2 | +Copyright 2019 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 | + "path/filepath" |
| 24 | + "strings" |
| 25 | + |
| 26 | + "github.com/gobuffalo/flect" |
| 27 | + "github.com/spf13/cobra" |
| 28 | + |
| 29 | + "sigs.k8s.io/kubebuilder/pkg/scaffold" |
| 30 | + "sigs.k8s.io/kubebuilder/pkg/scaffold/input" |
| 31 | + "sigs.k8s.io/kubebuilder/pkg/scaffold/project" |
| 32 | + "sigs.k8s.io/kubebuilder/pkg/scaffold/v1/resource" |
| 33 | + resourcev2 "sigs.k8s.io/kubebuilder/pkg/scaffold/v2" |
| 34 | + "sigs.k8s.io/kubebuilder/pkg/scaffold/v2/webhook" |
| 35 | +) |
| 36 | + |
| 37 | +func newWebhookV2Cmd() *cobra.Command { |
| 38 | + o := webhookV2Options{} |
| 39 | + |
| 40 | + cmd := &cobra.Command{ |
| 41 | + Use: "webhook", |
| 42 | + Short: "Scaffold a webhook for an API resource.", |
| 43 | + Long: `Scaffold a webhook for an API resource. You can choose to scaffold defaulting, validating and (or) conversion webhooks.`, |
| 44 | + Example: ` # Create defaulting and validating webhooks for CRD of group crew, version v1 and kind FirstMate. |
| 45 | + kubebuilder create webhook --group crew --version v1 --kind FirstMate --defaulting --programmatic-validation |
| 46 | +
|
| 47 | + # Create conversion webhook for CRD of group crew, version v1 and kind FirstMate. |
| 48 | + kubebuilder create webhook --group crew --version v1 --kind FirstMate --conversion |
| 49 | +`, |
| 50 | + Run: func(cmd *cobra.Command, args []string) { |
| 51 | + dieIfNoProject() |
| 52 | + |
| 53 | + projectInfo, err := scaffold.LoadProjectFile("PROJECT") |
| 54 | + if err != nil { |
| 55 | + log.Fatalf("failed to read the PROJECT file: %v", err) |
| 56 | + } |
| 57 | + |
| 58 | + if projectInfo.Version != project.Version2 { |
| 59 | + fmt.Printf("kubebuilder webhook is for project version: 2, the version of this project is: %s \n", projectInfo.Version) |
| 60 | + os.Exit(1) |
| 61 | + } |
| 62 | + |
| 63 | + if !o.defaulting && !o.validation && !o.conversion { |
| 64 | + fmt.Printf("kubebuilder webhook requires at least one of --defaulting, --programmatic-validation and --conversion to be true") |
| 65 | + os.Exit(1) |
| 66 | + } |
| 67 | + |
| 68 | + if len(o.res.Resource) == 0 { |
| 69 | + o.res.Resource = flect.Pluralize(strings.ToLower(o.res.Kind)) |
| 70 | + } |
| 71 | + |
| 72 | + fmt.Println("Writing scaffold for you to edit...") |
| 73 | + fmt.Println(filepath.Join("api", o.res.Version, |
| 74 | + fmt.Sprintf("%s_webhook.go", strings.ToLower(o.res.Kind)))) |
| 75 | + if o.conversion { |
| 76 | + fmt.Println(`Webhook server has been set up for you. |
| 77 | +You need to implement the conversion.Hub and conversion.Convertible interfaces for your CRD types.`) |
| 78 | + } |
| 79 | + webhookScaffolder := &webhook.Webhook{ |
| 80 | + Resource: o.res, |
| 81 | + Defaulting: o.defaulting, |
| 82 | + Validating: o.validation, |
| 83 | + } |
| 84 | + err = (&scaffold.Scaffold{}).Execute( |
| 85 | + input.Options{}, |
| 86 | + webhookScaffolder, |
| 87 | + ) |
| 88 | + if err != nil { |
| 89 | + fmt.Printf("error scaffolding webhook: %v", err) |
| 90 | + os.Exit(1) |
| 91 | + } |
| 92 | + |
| 93 | + err = (&resourcev2.Main{}).Update( |
| 94 | + &resourcev2.MainUpdateOptions{ |
| 95 | + Project: &projectInfo, |
| 96 | + WireResource: false, |
| 97 | + WireController: false, |
| 98 | + WireWebhook: true, |
| 99 | + Resource: o.res, |
| 100 | + }) |
| 101 | + if err != nil { |
| 102 | + fmt.Printf("error updating main.go: %v", err) |
| 103 | + os.Exit(1) |
| 104 | + } |
| 105 | + |
| 106 | + }, |
| 107 | + } |
| 108 | + o.res = gvkForFlags(cmd.Flags()) |
| 109 | + cmd.Flags().BoolVar(&o.defaulting, "defaulting", false, |
| 110 | + "if set, scaffold the defaulting webhook") |
| 111 | + cmd.Flags().BoolVar(&o.validation, "programmatic-validation", false, |
| 112 | + "if set, scaffold the validating webhook") |
| 113 | + cmd.Flags().BoolVar(&o.validation, "conversion", false, |
| 114 | + "if set, scaffold the conversion webhook") |
| 115 | + |
| 116 | + return cmd |
| 117 | +} |
| 118 | + |
| 119 | +// webhookOptions represents commandline options for scaffolding a webhook. |
| 120 | +type webhookV2Options struct { |
| 121 | + res *resource.Resource |
| 122 | + defaulting bool |
| 123 | + validation bool |
| 124 | + conversion bool |
| 125 | +} |
0 commit comments