Skip to content

Commit 5752ac2

Browse files
authored
Merge pull request #136 from mengqiy/webhookgenerator
✨ Move webhook generator
2 parents c3cd575 + cb5b319 commit 5752ac2

File tree

6,750 files changed

+22699
-2205744
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

6,750 files changed

+22699
-2205744
lines changed

Gopkg.lock

+75-810
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Gopkg.toml

+4-41
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,6 @@
1-
required = [
2-
"github.com/emicklei/go-restful",
3-
"github.com/go-openapi/spec",
4-
"github.com/onsi/ginkgo", # for integration testing
5-
"github.com/spf13/pflag",
6-
"k8s.io/client-go/plugin/pkg/client/auth/gcp", # for development against gcp
7-
"k8s.io/code-generator/cmd/deepcopy-gen", # for go generate
8-
"sigs.k8s.io/testing_frameworks/integration", # for integration testing
9-
]
10-
11-
[[constraint]]
12-
name="sigs.k8s.io/controller-runtime"
13-
version="v0.1.1"
1+
ignored = [
2+
"sigs.k8s.io/controller-tools/testData/*", # it's just testdata
3+
]
144

155
[[constraint]]
166
name="k8s.io/api"
@@ -24,34 +14,10 @@ required = [
2414
name="k8s.io/apimachinery"
2515
version="kubernetes-1.13.1"
2616

27-
[[constraint]]
28-
name="k8s.io/code-generator"
29-
version="kubernetes-1.13.1"
30-
31-
[[constraint]]
32-
name="k8s.io/client-go"
33-
version="kubernetes-1.13.1"
34-
35-
[[constraint]]
36-
name = "github.com/onsi/ginkgo"
37-
version = "v1.5.0"
38-
39-
[[constraint]]
40-
name = "github.com/onsi/gomega"
41-
version = "v1.4.0"
42-
4317
[[constraint]]
4418
name = "github.com/spf13/afero"
4519
version = "v1.1.1"
4620

47-
[[constraint]]
48-
name = "gopkg.in/yaml.v2"
49-
version = "v2.2.1"
50-
51-
[[constraint]]
52-
name = "github.com/emicklei/go-restful"
53-
version = "v2.7.1"
54-
5521
[[constraint]]
5622
name = "github.com/spf13/cobra"
5723
version = "v0.0.3"
@@ -60,10 +26,6 @@ required = [
6026
name = "github.com/spf13/pflag"
6127
version = "v1.0.1"
6228

63-
[[constraint]]
64-
name = "github.com/spf13/viper"
65-
version = "v1.0.2"
66-
6729
[[constraint]]
6830
name = "github.com/ghodss/yaml"
6931
version = "1.0.0"
@@ -83,3 +45,4 @@ required = [
8345

8446
[prune]
8547
go-tests = true
48+
unused-packages = true

cmd/controller-gen/main.go

+14-1
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,19 @@ Usage:
158158
log.Fatal(err)
159159
}
160160
fmt.Printf("RBAC manifests generated under '%s' \n", rbacOptions.OutputDir)
161+
162+
o := &webhook.Options{
163+
WriterOptions: webhook.WriterOptions{
164+
InputDir: filepath.Join(projectDir, "pkg"),
165+
OutputDir: filepath.Join(projectDir, "config", "webhook"),
166+
PatchOutputDir: filepath.Join(projectDir, "config", "default"),
167+
},
168+
}
169+
o.SetDefaults()
170+
if err := webhook.Generate(o); err != nil {
171+
log.Fatal(err)
172+
}
173+
fmt.Printf("webhook manifests generated under '%s' directory\n", o.OutputDir)
161174
},
162175
}
163176
f := cmd.Flags()
@@ -167,7 +180,7 @@ Usage:
167180
}
168181

169182
func newWebhookCmd() *cobra.Command {
170-
o := &webhook.ManifestOptions{}
183+
o := &webhook.Options{}
171184
o.SetDefaults()
172185

173186
cmd := &cobra.Command{

pkg/crd/generator/generator_test.go

+3-2
Original file line numberDiff line numberDiff line change
@@ -32,12 +32,13 @@ func TestGenerator(t *testing.T) {
3232
if err != nil {
3333
t.Fatalf("unable to get current directory: %v", err)
3434
}
35+
testDataDir := filepath.Join(currDir, "..", "..", "..", "testData")
3536
// in-memory file system for storing the generated CRDs
3637
outFs := afero.NewMemMapFs()
3738
g := &crdgenerator.Generator{
3839
OutFs: outFs,
3940
OutputDir: "/tmp",
40-
RootPath: filepath.Join(currDir, "testData"),
41+
RootPath: testDataDir,
4142
}
4243
err = g.ValidateAndInitFields()
4344
if err != nil {
@@ -54,7 +55,7 @@ func TestGenerator(t *testing.T) {
5455
if err != nil {
5556
t.Fatalf("reading file failed %v", err)
5657
}
57-
expectedContent, err := ioutil.ReadFile(filepath.Join(currDir, "testData", "config", "crds", f))
58+
expectedContent, err := ioutil.ReadFile(filepath.Join(testDataDir, "config", "crds", f))
5859
if err != nil {
5960
t.Fatalf("reading file failed %v", err)
6061
}

pkg/crd/generator/testData/vendor

-1
This file was deleted.

pkg/webhook/admission.go

+89
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
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 webhook
18+
19+
import (
20+
"errors"
21+
"fmt"
22+
"regexp"
23+
"strings"
24+
25+
admissionregistrationv1beta1 "k8s.io/api/admissionregistration/v1beta1"
26+
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
27+
)
28+
29+
// admissionWebhook contains bits needed for generating a admissionWebhook Configuration
30+
type admissionWebhook struct {
31+
// name is the name of the webhook
32+
name string
33+
// typ is the webhook type, i.e. mutating, validating
34+
typ webhookType
35+
// path is the path this webhook will serve.
36+
path string
37+
// rules maps to the rules field in admissionregistrationv1beta1.admissionWebhook
38+
rules []admissionregistrationv1beta1.RuleWithOperations
39+
// failurePolicy maps to the failurePolicy field in admissionregistrationv1beta1.admissionWebhook
40+
// This optional. If not set, will be defaulted to Ignore (fail-open) by the server.
41+
// More details: https://github.com/kubernetes/api/blob/f5c295feaba2cbc946f0bbb8b535fc5f6a0345ee/admissionregistration/v1beta1/types.go#L144-L147
42+
failurePolicy *admissionregistrationv1beta1.FailurePolicyType
43+
// namespaceSelector maps to the namespaceSelector field in admissionregistrationv1beta1.admissionWebhook
44+
// This optional.
45+
namespaceSelector *metav1.LabelSelector
46+
}
47+
48+
func (w *admissionWebhook) setDefaults() {
49+
if len(w.path) == 0 {
50+
if len(w.rules) == 0 || len(w.rules[0].Resources) == 0 {
51+
// can't do defaulting, skip it.
52+
return
53+
}
54+
if w.typ == mutatingWebhook {
55+
w.path = "/mutate-" + w.rules[0].Resources[0]
56+
} else if w.typ == validatingWebhook {
57+
w.path = "/validate-" + w.rules[0].Resources[0]
58+
}
59+
}
60+
if len(w.name) == 0 {
61+
reg := regexp.MustCompile("[^a-zA-Z0-9]+")
62+
processedPath := strings.ToLower(reg.ReplaceAllString(w.path, ""))
63+
w.name = processedPath + ".example.com"
64+
}
65+
}
66+
67+
var _ webhook = &admissionWebhook{}
68+
69+
// GetType returns the type of the webhook.
70+
func (w *admissionWebhook) GetType() webhookType {
71+
return w.typ
72+
}
73+
74+
// Validate validates if the webhook is valid.
75+
func (w *admissionWebhook) Validate() error {
76+
if len(w.rules) == 0 {
77+
return errors.New("field rules should not be empty")
78+
}
79+
if len(w.name) == 0 {
80+
return errors.New("field name should not be empty")
81+
}
82+
if w.typ != mutatingWebhook && w.typ != validatingWebhook {
83+
return fmt.Errorf("unsupported Type: %v, only mutatingWebhook and validatingWebhook are supported", w.typ)
84+
}
85+
if len(w.path) == 0 {
86+
return errors.New("field path should not be empty")
87+
}
88+
return nil
89+
}

0 commit comments

Comments
 (0)