Skip to content

Commit 09f13b4

Browse files
author
Mengqi Yu
committed
✨ use Deployment for v2 scaffolding
1 parent 71237a9 commit 09f13b4

Some content is hidden

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

44 files changed

+551
-147
lines changed

pkg/scaffold/api.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,7 @@ func (api *API) scaffoldV2() error {
162162
&resourcev2.Group{Resource: r},
163163
&resourcev2.CRDSample{Resource: r},
164164
&crdv2.EnableWebhookPatch{Resource: r},
165+
&crdv2.EnableCAInjectionPatch{Resource: r},
165166
)
166167
if err != nil {
167168
return fmt.Errorf("error scaffolding APIs: %v", err)

pkg/scaffold/project.go

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -23,14 +23,16 @@ import (
2323
"os/exec"
2424
"strings"
2525

26+
"sigs.k8s.io/kubebuilder/cmd/util"
2627
"sigs.k8s.io/kubebuilder/pkg/scaffold/input"
2728
"sigs.k8s.io/kubebuilder/pkg/scaffold/project"
29+
scaffoldv1 "sigs.k8s.io/kubebuilder/pkg/scaffold/v1"
2830
"sigs.k8s.io/kubebuilder/pkg/scaffold/v1/manager"
29-
30-
"sigs.k8s.io/kubebuilder/cmd/util"
31+
metricsauthv1 "sigs.k8s.io/kubebuilder/pkg/scaffold/v1/metricsauth"
3132
scaffoldv2 "sigs.k8s.io/kubebuilder/pkg/scaffold/v2"
3233
"sigs.k8s.io/kubebuilder/pkg/scaffold/v2/certmanager"
3334
managerv2 "sigs.k8s.io/kubebuilder/pkg/scaffold/v2/manager"
35+
metricsauthv2 "sigs.k8s.io/kubebuilder/pkg/scaffold/v2/metricsauth"
3436
"sigs.k8s.io/kubebuilder/pkg/scaffold/v2/webhook"
3537
)
3638

@@ -110,9 +112,9 @@ func (p *V1Project) Scaffold() error {
110112
input.Options{ProjectPath: projectInput.Path, BoilerplatePath: bpInput.Path},
111113
&project.GitIgnore{},
112114
&project.KustomizeRBAC{},
113-
&project.KustomizeImagePatch{},
114-
&project.KustomizePrometheusMetricsPatch{},
115-
&project.KustomizeAuthProxyPatch{},
115+
&scaffoldv1.KustomizeImagePatch{},
116+
&metricsauthv1.KustomizePrometheusMetricsPatch{},
117+
&metricsauthv1.KustomizeAuthProxyPatch{},
116118
&project.AuthProxyService{},
117119
&project.AuthProxyRole{},
118120
&project.AuthProxyRoleBinding{},
@@ -179,9 +181,9 @@ func (p *V2Project) Scaffold() error {
179181
return s.Execute(
180182
input.Options{ProjectPath: projectInput.Path, BoilerplatePath: bpInput.Path},
181183
&project.GitIgnore{},
182-
&project.KustomizeImagePatch{},
183-
&project.KustomizePrometheusMetricsPatch{},
184-
&project.KustomizeAuthProxyPatch{},
184+
&scaffoldv2.KustomizeImagePatch{},
185+
&metricsauthv2.KustomizePrometheusMetricsPatch{},
186+
&metricsauthv2.KustomizeAuthProxyPatch{},
185187
&project.AuthProxyService{},
186188
&project.AuthProxyRole{},
187189
&project.AuthProxyRoleBinding{},
@@ -193,6 +195,8 @@ func (p *V2Project) Scaffold() error {
193195
&scaffoldv2.Kustomize{},
194196
&scaffoldv2.ManagerWebhookPatch{},
195197
&scaffoldv2.ManagerRoleBinding{},
198+
&scaffoldv2.LeaderElectionRole{},
199+
&scaffoldv2.LeaderElectionRoleBinding{},
196200
&scaffoldv2.KustomizeRBAC{},
197201
&managerv2.Kustomization{},
198202
&webhook.Kustomization{},

pkg/scaffold/project/project_test.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,9 @@ import (
1414
"sigs.k8s.io/kubebuilder/pkg/scaffold"
1515
"sigs.k8s.io/kubebuilder/pkg/scaffold/input"
1616
"sigs.k8s.io/kubebuilder/pkg/scaffold/project"
17+
scaffoldv1 "sigs.k8s.io/kubebuilder/pkg/scaffold/v1"
1718
"sigs.k8s.io/kubebuilder/pkg/scaffold/scaffoldtest"
19+
"sigs.k8s.io/kubebuilder/pkg/scaffold/v1/metricsauth"
1820
)
1921

2022
var _ = Describe("Project", func() {
@@ -247,7 +249,7 @@ Copyright %s Example Owners.
247249
})
248250
Context("with defaults ", func() {
249251
It("should match the golden file", func() {
250-
instance := &project.KustomizeImagePatch{}
252+
instance := &scaffoldv1.KustomizeImagePatch{}
251253
instance.Repo = "project"
252254
Expect(s.Execute(input.Options{}, instance)).NotTo(HaveOccurred())
253255

@@ -264,7 +266,7 @@ Copyright %s Example Owners.
264266
})
265267
Context("with defaults ", func() {
266268
It("should match the golden file", func() {
267-
instance := &project.KustomizePrometheusMetricsPatch{}
269+
instance := &metricsauth.KustomizePrometheusMetricsPatch{}
268270
instance.Repo = "project"
269271
Expect(s.Execute(input.Options{}, instance)).NotTo(HaveOccurred())
270272

pkg/scaffold/project/kustomize_image_patch.go renamed to pkg/scaffold/v1/kustomize_image_patch.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ See the License for the specific language governing permissions and
1414
limitations under the License.
1515
*/
1616

17-
package project
17+
package v1
1818

1919
import (
2020
"path/filepath"

pkg/scaffold/project/kustomize_auth_proxy_patch.go renamed to pkg/scaffold/v1/metricsauth/kustomize_auth_proxy_patch.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ See the License for the specific language governing permissions and
1414
limitations under the License.
1515
*/
1616

17-
package project
17+
package metricsauth
1818

1919
import (
2020
"path/filepath"

pkg/scaffold/project/kustomize_metrics_patch.go renamed to pkg/scaffold/v1/metricsauth/kustomize_metrics_patch.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ See the License for the specific language governing permissions and
1414
limitations under the License.
1515
*/
1616

17-
package project
17+
package metricsauth
1818

1919
import (
2020
"path/filepath"

pkg/scaffold/v2/certmanager/kustomize.go

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -41,20 +41,24 @@ var kustomizationTemplate = `resources:
4141
4242
# the following config is for teaching kustomize how to do var substitution
4343
vars:
44+
- name: NAMESPACE # namespace of the service and the certificate CR
45+
objref:
46+
kind: Service
47+
version: v1
48+
name: webhook-service
49+
fieldref:
50+
fieldpath: metadata.namespace
4451
- name: CERTIFICATENAME
4552
objref:
4653
kind: Certificate
4754
group: certmanager.k8s.io
4855
version: v1alpha1
4956
name: serving-cert # this name should match the one in certificate.yaml
50-
- name: CERTIFICATENAMESPACE
57+
- name: SERVICENAME
5158
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
59+
kind: Service
60+
version: v1
61+
name: webhook-service
5862
5963
configurations:
6064
- kustomizeconfig.yaml
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
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 v2
18+
19+
import (
20+
"fmt"
21+
"path/filepath"
22+
"strings"
23+
24+
"github.com/markbates/inflect"
25+
26+
"sigs.k8s.io/kubebuilder/pkg/scaffold/input"
27+
"sigs.k8s.io/kubebuilder/pkg/scaffold/v1/resource"
28+
)
29+
30+
// EnableCAInjectionPatch scaffolds a EnableCAInjectionPatch for a Resource
31+
type EnableCAInjectionPatch struct {
32+
input.Input
33+
34+
// Resource is the Resource to make the EnableCAInjectionPatch for
35+
Resource *resource.Resource
36+
}
37+
38+
// GetInput implements input.File
39+
func (p *EnableCAInjectionPatch) GetInput() (input.Input, error) {
40+
if p.Path == "" {
41+
rs := inflect.NewDefaultRuleset()
42+
plural := rs.Pluralize(strings.ToLower(p.Resource.Kind))
43+
p.Path = filepath.Join("config", "crd", "patches",
44+
fmt.Sprintf("cainjection_in_%s.yaml", plural))
45+
}
46+
p.TemplateBody = EnableCAInjectionPatchTemplate
47+
return p.Input, nil
48+
}
49+
50+
// Validate validates the values
51+
func (g *EnableCAInjectionPatch) Validate() error {
52+
return g.Resource.Validate()
53+
}
54+
55+
var EnableCAInjectionPatchTemplate = `# The following patch adds a directive for certmanager to inject CA into the CRD
56+
# CRD conversion requires k8s 1.13 or later.
57+
apiVersion: apiextensions.k8s.io/v1beta1
58+
kind: CustomResourceDefinition
59+
metadata:
60+
annotations:
61+
certmanager.k8s.io/inject-ca-from: $(NAMESPACE)/$(CERTIFICATENAME)
62+
name: {{ .Resource.Resource }}.{{ .Resource.Group }}.{{ .Domain }}
63+
`

pkg/scaffold/v2/crd/enablewebhook_patch.go

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -52,13 +52,11 @@ func (g *EnableWebhookPatch) Validate() error {
5252
return g.Resource.Validate()
5353
}
5454

55-
// TODO(mengqiy): plural of the resources
56-
var enableWebhookPatchTemplate = `# The following patch enables conversion webhook for CRDw
55+
var enableWebhookPatchTemplate = `# The following patch enables conversion webhook for CRD
56+
# CRD conversion requires k8s 1.13 or later.
5757
apiVersion: apiextensions.k8s.io/v1beta1
5858
kind: CustomResourceDefinition
5959
metadata:
60-
annotations:
61-
certmanager.k8s.io/inject-ca-from: $(NAMESPACE)/$(CERTIFICATENAME)
6260
name: {{ .Resource.Resource }}.{{ .Resource.Group }}.{{ .Domain }}
6361
spec:
6462
conversion:
@@ -68,7 +66,7 @@ spec:
6866
# but we're going to set it later using the cert-manager (or potentially a patch if not using cert-manager)
6967
caBundle: Cg==
7068
service:
71-
namespace: $(NAMESPACE)
69+
namespace: system
7270
name: webhook-service
73-
path: /convert-{{ lower .Resource.Kind }}
71+
path: /convert
7472
`

pkg/scaffold/v2/crd/kustomization.go

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,9 @@ import (
2929
)
3030

3131
const (
32-
kustomizeResourceScaffoldMarker = "# +kubebuilder:scaffold:kustomizeresource"
33-
kustomizePatchScaffoldMarker = "# +kubebuilder:scaffold:kustomizepatch"
32+
kustomizeResourceScaffoldMarker = "# +kubebuilder:scaffold:crdkustomizeresource"
33+
kustomizeWebhookPatchScaffoldMarker = "# +kubebuilder:scaffold:crdkustomizewebhookpatch"
34+
kustomizeCAInjectionPatchScaffoldMarker = "# +kubebuilder:scaffold:crdkustomizecainjectionpatch"
3435
)
3536

3637
var _ input.File = &Kustomization{}
@@ -64,12 +65,14 @@ func (c *Kustomization) Update() error {
6465
plural := rs.Pluralize(strings.ToLower(c.Resource.Kind))
6566

6667
kustomizeResourceCodeFragment := fmt.Sprintf("- bases/%s.%s_%s.yaml\n", c.Resource.Group, c.Domain, plural)
67-
kustomizePatchCodeFragment := fmt.Sprintf("#- patches/webhook_in_%s.yaml\n", plural)
68+
kustomizeWebhookPatchCodeFragment := fmt.Sprintf("#- patches/webhook_in_%s.yaml\n", plural)
69+
kustomizeCAInjectionPatchCodeFragment := fmt.Sprintf("#- patches/cainjection_in_%s.yaml\n", plural)
6870

6971
return internal.InsertStringsInFile(c.Path,
7072
map[string][]string{
71-
kustomizeResourceScaffoldMarker: []string{kustomizeResourceCodeFragment},
72-
kustomizePatchScaffoldMarker: []string{kustomizePatchCodeFragment},
73+
kustomizeResourceScaffoldMarker: {kustomizeResourceCodeFragment},
74+
kustomizeWebhookPatchScaffoldMarker: {kustomizeWebhookPatchCodeFragment},
75+
kustomizeCAInjectionPatchScaffoldMarker: {kustomizeCAInjectionPatchCodeFragment},
7376
})
7477
}
7578

@@ -80,10 +83,13 @@ resources:
8083
%s
8184
8285
patches:
83-
# patches here are for enabling the conversion webhook for each CRD
86+
# [WEBHOOK] patches here are for enabling the conversion webhook for each CRD
87+
%s
88+
89+
# [CAINJECTION] patches here are for enabling the CA injection for each CRD
8490
%s
8591
8692
# the following config is for teaching kustomize how to do kustomization for CRDs.
8793
configurations:
8894
- kustomizeconfig.yaml
89-
`, kustomizeResourceScaffoldMarker, kustomizePatchScaffoldMarker)
95+
`, kustomizeResourceScaffoldMarker, kustomizeWebhookPatchScaffoldMarker, kustomizeCAInjectionPatchScaffoldMarker)

pkg/scaffold/v2/crd/kustomizeconfig.go

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,9 +48,12 @@ nameReference:
4848
group: apiextensions.k8s.io
4949
path: spec/conversion/webhookClientConfig/service/name
5050
51-
varReference:
52-
- path: metadata/annotations
51+
namespace:
5352
- kind: CustomResourceDefinition
5453
group: apiextensions.k8s.io
5554
path: spec/conversion/webhookClientConfig/service/namespace
55+
create: false
56+
57+
varReference:
58+
- path: metadata/annotations
5659
`

pkg/scaffold/v2/kustomize.go

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -69,9 +69,9 @@ bases:
6969
- ../crd
7070
- ../rbac
7171
- ../manager
72-
# [WEBHOOK] Uncomment all the sections with [WEBHOOK] prefix to enable webhook.
72+
# [WEBHOOK] To enable webhook, uncomment all the sections with [WEBHOOK] prefix including the one in crd/kustomization.yaml
7373
#- ../webhook
74-
# [CERTMANAGER] Uncomment next line to enable cert-manager
74+
# [CERTMANAGER] To enable cert-manager, uncomment next line. 'WEBHOOK' components are required.
7575
#- ../certmanager
7676
7777
patches:
@@ -87,10 +87,11 @@ patches:
8787
# manager_prometheus_metrics_patch.yaml should be enabled.
8888
#- manager_prometheus_metrics_patch.yaml
8989
90-
# [WEBHOOK] Uncomment all the sections with [WEBHOOK] prefix to enable webhook.
90+
# [WEBHOOK] To enable webhook, uncomment all the sections with [WEBHOOK] prefix including the one in crd/kustomization.yaml
9191
#- manager_webhook_patch.yaml
9292
93-
# [CAINJECTION] Uncomment next line to enable the CA injection in the admission webhooks. [CERTMANAGER] needs to be
94-
# enabled to use ca injection
93+
# [CAINJECTION] Uncomment next line to enable the CA injection in the admission webhooks.
94+
# Uncomment 'CAINJECTION' in crd/kustomization.yaml to enable the CA injection in the admission webhooks.
95+
# 'CERTMANAGER' needs to be enabled to use ca injection
9596
#- webhookcainjection_patch.yaml
9697
`
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
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 v2
18+
19+
import (
20+
"path/filepath"
21+
22+
"sigs.k8s.io/kubebuilder/pkg/scaffold/input"
23+
)
24+
25+
var _ input.File = &KustomizeImagePatch{}
26+
27+
// KustomizeImagePatch scaffolds the patch file for customizing image URL
28+
// manifest file for manager resource.
29+
type KustomizeImagePatch struct {
30+
input.Input
31+
32+
// ImageURL to use for controller image in manager's manifest.
33+
ImageURL string
34+
}
35+
36+
// GetInput implements input.File
37+
func (c *KustomizeImagePatch) GetInput() (input.Input, error) {
38+
if c.Path == "" {
39+
c.Path = filepath.Join("config", "default", "manager_image_patch.yaml")
40+
}
41+
if c.ImageURL == "" {
42+
c.ImageURL = "IMAGE_URL"
43+
}
44+
c.TemplateBody = kustomizeImagePatchTemplate
45+
c.Input.IfExistsAction = input.Error
46+
return c.Input, nil
47+
}
48+
49+
var kustomizeImagePatchTemplate = `apiVersion: apps/v1
50+
kind: Deployment
51+
metadata:
52+
name: controller-manager
53+
namespace: system
54+
spec:
55+
template:
56+
spec:
57+
containers:
58+
# Change the value of image field below to your controller image URL
59+
- image: {{ .ImageURL }}
60+
name: manager
61+
`

0 commit comments

Comments
 (0)