Skip to content

Commit 0e13ffa

Browse files
authored
Merge pull request #702 from mengqiy/webhook
✨ fix webhook related scaffolding
2 parents d8ad6ed + ee11b10 commit 0e13ffa

28 files changed

+150
-100
lines changed

cmd/main.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,11 @@ limitations under the License.
1717
package main
1818

1919
import (
20+
"encoding/json"
2021
"fmt"
22+
"log"
2123
"os"
2224
"os/exec"
23-
"log"
24-
"encoding/json"
2525

2626
"github.com/spf13/cobra"
2727
"golang.org/x/tools/go/packages"
@@ -77,7 +77,7 @@ func findCurrentRepo() (string, error) {
7777

7878
// next, check if we've got a package in the current directory
7979
pkgCfg := &packages.Config{
80-
Mode: packages.NeedName, // name gives us path as well
80+
Mode: packages.NeedName, // name gives us path as well
8181
}
8282
pkgs, err := packages.Load(pkgCfg, ".")
8383
if err == nil && len(pkgs) > 0 {

cmd/webhook.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -43,10 +43,11 @@ func newWebhookCmd() *cobra.Command {
4343
Short: "Scaffold a webhook server",
4444
Long: `Scaffold a webhook server if there is no existing server.
4545
Scaffolds webhook handlers based on group, version, kind and other user inputs.
46+
This command is only available for v1 scaffolding project.
4647
`,
4748
Example: ` # Create webhook for CRD of group crew, version v1 and kind FirstMate.
4849
# Set type to be mutating and operations to be create and update.
49-
kubebuilder webhook --group crew --version v1 --kind FirstMate --type=mutating --operations=create,update
50+
kubebuilder alpha webhook --group crew --version v1 --kind FirstMate --type=mutating --operations=create,update
5051
`,
5152
Run: func(cmd *cobra.Command, args []string) {
5253
dieIfNoProject()

generated_golden.sh

-3
Original file line numberDiff line numberDiff line change
@@ -64,13 +64,10 @@ scaffold_test_project() {
6464
$kb init --project-version $version --domain testproject.org --license apache2 --owner "The Kubernetes authors"
6565
$kb create api --group crew --version v1 --kind Captain --controller=true --resource=true --make=false
6666
$kb create api --group crew --version v1 --kind FirstMate --controller=true --resource=true --make=false
67-
$kb alpha webhook --group crew --version v1 --kind FirstMate --type=mutating --operations=create,update --make=false
68-
$kb alpha webhook --group crew --version v1 --kind FirstMate --type=mutating --operations=delete --make=false
6967
# TODO(droot): Adding a second group is a valid test case and kubebuilder is expected to report an error in this case. It
7068
# doesn't do that currently so leaving it commented so that we can enable it later.
7169
# $kb create api --group ship --version v1beta1 --kind Frigate --example=false --controller=true --resource=true --make=false
7270
$kb create api --group core --version v1 --kind Namespace --example=false --controller=true --resource=false --namespaced=false --make=false
73-
$kb alpha webhook --group core --version v1 --kind Namespace --type=mutating --operations=update --make=false
7471
# $kb create api --group policy --version v1beta1 --kind HealthCheckPolicy --example=false --controller=true --resource=true --namespaced=false --make=false
7572
fi
7673
make all test # v2 doesn't test by default

pkg/scaffold/project.go

+6-6
Original file line numberDiff line numberDiff line change
@@ -17,21 +17,21 @@ limitations under the License.
1717
package scaffold
1818

1919
import (
20+
"bufio"
21+
"fmt"
2022
"os"
2123
"os/exec"
22-
"fmt"
2324
"strings"
24-
"bufio"
2525

2626
"sigs.k8s.io/kubebuilder/pkg/scaffold/input"
2727
"sigs.k8s.io/kubebuilder/pkg/scaffold/project"
2828
"sigs.k8s.io/kubebuilder/pkg/scaffold/v1/manager"
2929

30+
"sigs.k8s.io/kubebuilder/cmd/util"
3031
scaffoldv2 "sigs.k8s.io/kubebuilder/pkg/scaffold/v2"
3132
"sigs.k8s.io/kubebuilder/pkg/scaffold/v2/certmanager"
3233
managerv2 "sigs.k8s.io/kubebuilder/pkg/scaffold/v2/manager"
3334
"sigs.k8s.io/kubebuilder/pkg/scaffold/v2/webhook"
34-
"sigs.k8s.io/kubebuilder/cmd/util"
3535
)
3636

3737
type ProjectScaffolder interface {
@@ -41,10 +41,10 @@ type ProjectScaffolder interface {
4141
}
4242

4343
type V1Project struct {
44-
Project project.Project
44+
Project project.Project
4545
Boilerplate project.Boilerplate
4646

47-
DepArgs []string
47+
DepArgs []string
4848
DefinitelyEnsure *bool
4949
}
5050

@@ -129,7 +129,7 @@ func (p *V1Project) Scaffold() error {
129129
}
130130

131131
type V2Project struct {
132-
Project project.Project
132+
Project project.Project
133133
Boilerplate project.Boilerplate
134134
}
135135

pkg/scaffold/v2/certmanager/kustomize.go

+9
Original file line numberDiff line numberDiff line change
@@ -39,13 +39,22 @@ func (p *Kustomization) GetInput() (input.Input, error) {
3939
var kustomizationTemplate = `resources:
4040
- certificate.yaml
4141
42+
# the following config is for teaching kustomize how to do var substitution
4243
vars:
4344
- name: CERTIFICATENAME
4445
objref:
4546
kind: Certificate
4647
group: certmanager.k8s.io
4748
version: v1alpha1
4849
name: serving-cert # this name should match the one in certificate.yaml
50+
- name: CERTIFICATENAMESPACE
51+
objref:
52+
kind: Certificate
53+
group: certmanager.k8s.io
54+
version: v1alpha1
55+
name: serving-cert # this name should match the one in certificate.yaml
56+
fieldref:
57+
fieldpath: metadata.namespace
4958
5059
configurations:
5160
- kustomizeconfig.yaml

pkg/scaffold/v2/crd/enablewebhook_patch.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ spec:
6262
webhookClientConfig:
6363
# this is "\n" used as a placeholder, otherwise it will be rejected by the apiserver for being blank,
6464
# but we're going to set it later using the cert-manager (or potentially a patch if not using cert-manager)
65-
caBundle: XG4=
65+
caBundle: Cg==
6666
service:
6767
namespace: $(NAMESPACE)
6868
name: webhook-service

pkg/scaffold/v2/dockerfile.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ RUN CGO_ENABLED=0 GOOS=linux GOARCH=amd64 GO111MODULE=on go build -a -o manager
5454
5555
# Use distroless as minimal base image to package the manager binary
5656
# Refer to https://github.com/GoogleContainerTools/distroless for more details
57-
FROM gcr.io/distroless/base
57+
FROM gcr.io/distroless/static:latest
5858
WORKDIR /
5959
COPY --from=builder /workspace/manager .
6060
ENTRYPOINT ["/manager"]

pkg/scaffold/v2/kustomize.go

+9-7
Original file line numberDiff line numberDiff line change
@@ -69,9 +69,10 @@ bases:
6969
- ../crd
7070
- ../rbac
7171
- ../manager
72-
# - ../webhook
73-
# Comment the next line if you want to disable cert-manager
74-
# - ../certmanager
72+
# [WEBHOOK] Uncomment all the sections with [WEBHOOK] prefix to enable webhook.
73+
#- ../webhook
74+
# [CERTMANAGER] Uncomment next line to enable cert-manager
75+
#- ../certmanager
7576
7677
patches:
7778
- manager_image_patch.yaml
@@ -86,9 +87,10 @@ patches:
8687
# manager_prometheus_metrics_patch.yaml should be enabled.
8788
#- manager_prometheus_metrics_patch.yaml
8889
89-
# Uncomment the following patch to enable the CA injection in the admission webhooks.
90-
#- webhookcainjection_patch.yaml
91-
92-
# Uncomment the following patch to enable the webhook for the manager.
90+
# [WEBHOOK] Uncomment all the sections with [WEBHOOK] prefix to enable webhook.
9391
#- manager_webhook_patch.yaml
92+
93+
# [CAINJECTION] Uncomment next line to enable the CA injection in the admission webhooks. [CERTMANAGER] needs to be
94+
# enabled to use ca injection
95+
#- webhookcainjection_patch.yaml
9496
`

pkg/scaffold/v2/manager/config.go

-4
Original file line numberDiff line numberDiff line change
@@ -98,9 +98,5 @@ spec:
9898
requests:
9999
cpu: 100m
100100
memory: 20Mi
101-
ports:
102-
- containerPort: 9876
103-
name: webhook-server
104-
protocol: TCP
105101
terminationGracePeriodSeconds: 10
106102
`

pkg/scaffold/v2/manager/kustomization.go

-15
Original file line numberDiff line numberDiff line change
@@ -41,19 +41,4 @@ func (c *Kustomization) GetInput() (input.Input, error) {
4141

4242
var kustomizeManagerTemplate = `resources:
4343
- manager.yaml
44-
45-
# the following config is for teaching kustomize how to do var substitution
46-
# vars:
47-
# - name: NAMESPACE
48-
# objref:
49-
# kind: Service
50-
# version: v1
51-
# name: webhook-service
52-
# fieldref:
53-
# fieldpath: metadata.namespace
54-
# - name: SERVICENAME
55-
# objref:
56-
# kind: Service
57-
# version: v1
58-
# name: webhook-service
5944
`

pkg/scaffold/v2/webhook/enablecainection_patch.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -46,12 +46,12 @@ kind: MutatingWebhookConfiguration
4646
metadata:
4747
name: mutating-webhook-configuration
4848
annotations:
49-
certmanager.k8s.io/inject-ca-from: $(NAMESPACE)/$(CERTIFICATENAME)
49+
certmanager.k8s.io/inject-ca-from: $(CERTIFICATENAMESPACE)/$(CERTIFICATENAME)
5050
---
5151
apiVersion: admissionregistration.k8s.io/v1beta1
5252
kind: ValidatingWebhookConfiguration
5353
metadata:
5454
name: validating-webhook-configuration
5555
annotations:
56-
certmanager.k8s.io/inject-ca-from: $(NAMESPACE)/$(CERTIFICATENAME)
56+
certmanager.k8s.io/inject-ca-from: $(CERTIFICATENAMESPACE)/$(CERTIFICATENAME)
5757
`

pkg/scaffold/v2/webhook/kustomization.go

+17-1
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,24 @@ func (c *Kustomization) GetInput() (input.Input, error) {
4040
}
4141

4242
var KustomizeWebhookTemplate = `resources:
43-
- webhookmanifests.yaml # disabled till v2 has webhook support
43+
- webhookmanifests.yaml
44+
- service.yaml
4445
4546
configurations:
4647
- kustomizeconfig.yaml
48+
49+
# the following config is for teaching kustomize how to do var substitution
50+
vars:
51+
- name: NAMESPACE
52+
objref:
53+
kind: Service
54+
version: v1
55+
name: webhook-service
56+
fieldref:
57+
fieldpath: metadata.namespace
58+
- name: SERVICENAME
59+
objref:
60+
kind: Service
61+
version: v1
62+
name: webhook-service
4763
`

pkg/scaffold/v2/webhook/service.go

+52
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
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 webhook
18+
19+
import (
20+
"path/filepath"
21+
22+
"sigs.k8s.io/kubebuilder/pkg/scaffold/input"
23+
)
24+
25+
var _ input.File = &Service{}
26+
27+
// Service scaffolds the Service file in manager folder.
28+
type Service struct {
29+
input.Input
30+
}
31+
32+
// GetInput implements input.File
33+
func (c *Service) GetInput() (input.Input, error) {
34+
if c.Path == "" {
35+
c.Path = filepath.Join("config", "webhook", "service.yaml")
36+
}
37+
c.TemplateBody = ServiceTemplate
38+
c.Input.IfExistsAction = input.Error
39+
return c.Input, nil
40+
}
41+
42+
var ServiceTemplate = `
43+
apiVersion: v1
44+
kind: Service
45+
metadata:
46+
name: webhook-service
47+
namespace: system
48+
spec:
49+
ports:
50+
- port: 443
51+
targetPort: 443
52+
`

pkg/scaffold/v2/webhook_manager_patch.go

+3-12
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ package v2
1818

1919
import (
2020
"path/filepath"
21+
2122
"sigs.k8s.io/kubebuilder/pkg/scaffold/input"
2223
)
2324

@@ -50,22 +51,12 @@ spec:
5051
name: webhook-server
5152
protocol: TCP
5253
volumeMounts:
53-
- mountPath: /tmp/cert
54+
- mountPath: /tmp/k8s-webhook-server/serving-certs
5455
name: cert
5556
readOnly: true
5657
volumes:
5758
- name: cert
5859
secret:
5960
defaultMode: 420
60-
secretName: webhook-server-secret
61-
---
62-
apiVersion: v1
63-
kind: Service
64-
metadata:
65-
name: webhook-service
66-
namespace: system
67-
spec:
68-
ports:
69-
- port: 443
70-
targetPort: 443
61+
secretName: webhook-server-cert
7162
`

testdata/project-v2/Dockerfile

+1-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ RUN CGO_ENABLED=0 GOOS=linux GOARCH=amd64 GO111MODULE=on go build -a -o manager
1616

1717
# Use distroless as minimal base image to package the manager binary
1818
# Refer to https://github.com/GoogleContainerTools/distroless for more details
19-
FROM gcr.io/distroless/base
19+
FROM gcr.io/distroless/static:latest
2020
WORKDIR /
2121
COPY --from=builder /workspace/manager .
2222
ENTRYPOINT ["/manager"]
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,22 @@
11
resources:
22
- certificate.yaml
33

4+
# the following config is for teaching kustomize how to do var substitution
45
vars:
56
- name: CERTIFICATENAME
67
objref:
78
kind: Certificate
89
group: certmanager.k8s.io
910
version: v1alpha1
1011
name: serving-cert # this name should match the one in certificate.yaml
12+
- name: CERTIFICATENAMESPACE
13+
objref:
14+
kind: Certificate
15+
group: certmanager.k8s.io
16+
version: v1alpha1
17+
name: serving-cert # this name should match the one in certificate.yaml
18+
fieldref:
19+
fieldpath: metadata.namespace
1120

1221
configurations:
1322
- kustomizeconfig.yaml

testdata/project-v2/config/crd/bases/crew.testproject.org_captains.yaml

+1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
---
12
apiVersion: apiextensions.k8s.io/v1beta1
23
kind: CustomResourceDefinition
34
metadata:

testdata/project-v2/config/crd/bases/crew.testproject.org_firstmates.yaml

+1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
---
12
apiVersion: apiextensions.k8s.io/v1beta1
23
kind: CustomResourceDefinition
34
metadata:

testdata/project-v2/config/crd/patches/webhook_in_captain.yaml

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ spec:
1111
webhookClientConfig:
1212
# this is "\n" used as a placeholder, otherwise it will be rejected by the apiserver for being blank,
1313
# but we're going to set it later using the cert-manager (or potentially a patch if not using cert-manager)
14-
caBundle: XG4=
14+
caBundle: Cg==
1515
service:
1616
namespace: $(NAMESPACE)
1717
name: webhook-service

testdata/project-v2/config/crd/patches/webhook_in_firstmate.yaml

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ spec:
1111
webhookClientConfig:
1212
# this is "\n" used as a placeholder, otherwise it will be rejected by the apiserver for being blank,
1313
# but we're going to set it later using the cert-manager (or potentially a patch if not using cert-manager)
14-
caBundle: XG4=
14+
caBundle: Cg==
1515
service:
1616
namespace: $(NAMESPACE)
1717
name: webhook-service

0 commit comments

Comments
 (0)