Skip to content

Commit b12371c

Browse files
author
Mengqi Yu
authored
Merge pull request #864 from droot/scaffold-resource-marker
✨ Add support for the resource scope marker
2 parents 5833c64 + a9f290d commit b12371c

14 files changed

+317
-0
lines changed

generated_golden.sh

+1
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@ scaffold_test_project() {
6767
$kb create webhook --group crew --version v1 --kind Captain --defaulting --programmatic-validation
6868
$kb create api --group crew --version v1 --kind FirstMate --controller=true --resource=true --make=false
6969
$kb create webhook --group crew --version v1 --kind FirstMate --conversion
70+
$kb create api --group crew --version v1 --kind Admiral --controller=true --resource=true --namespaced=false --make=false
7071
# TODO(droot): Adding a second group is a valid test case and kubebuilder is expected to report an error in this case. It
7172
# doesn't do that currently so leaving it commented so that we can enable it later.
7273
# $kb create api --group ship --version v1beta1 --kind Frigate --example=false --controller=true --resource=true --make=false

pkg/scaffold/v2/types.go

+1
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,7 @@ type {{.Resource.Kind}}Status struct {
7575
}
7676
7777
// +kubebuilder:object:root=true
78+
{{ if not .Resource.Namespaced }} // +kubebuilder:resource:scope=Cluster {{ end }}
7879
7980
// {{.Resource.Kind}} is the Schema for the {{ .Resource.Resource }} API
8081
type {{.Resource.Kind}} struct {

testdata/project-v2/PROJECT

+3
Original file line numberDiff line numberDiff line change
@@ -8,3 +8,6 @@ resources:
88
- group: crew
99
version: v1
1010
kind: FirstMate
11+
- group: crew
12+
version: v1
13+
kind: Admiral
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
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 v1
18+
19+
import (
20+
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
21+
)
22+
23+
// EDIT THIS FILE! THIS IS SCAFFOLDING FOR YOU TO OWN!
24+
// NOTE: json tags are required. Any new fields you add must have json tags for the fields to be serialized.
25+
26+
// AdmiralSpec defines the desired state of Admiral
27+
type AdmiralSpec struct {
28+
// INSERT ADDITIONAL SPEC FIELDS - desired state of cluster
29+
// Important: Run "make" to regenerate code after modifying this file
30+
}
31+
32+
// AdmiralStatus defines the observed state of Admiral
33+
type AdmiralStatus struct {
34+
// INSERT ADDITIONAL STATUS FIELD - define observed state of cluster
35+
// Important: Run "make" to regenerate code after modifying this file
36+
}
37+
38+
// +kubebuilder:object:root=true
39+
// +kubebuilder:resource:scope=Cluster
40+
41+
// Admiral is the Schema for the admirals API
42+
type Admiral struct {
43+
metav1.TypeMeta `json:",inline"`
44+
metav1.ObjectMeta `json:"metadata,omitempty"`
45+
46+
Spec AdmiralSpec `json:"spec,omitempty"`
47+
Status AdmiralStatus `json:"status,omitempty"`
48+
}
49+
50+
// +kubebuilder:object:root=true
51+
52+
// AdmiralList contains a list of Admiral
53+
type AdmiralList struct {
54+
metav1.TypeMeta `json:",inline"`
55+
metav1.ListMeta `json:"metadata,omitempty"`
56+
Items []Admiral `json:"items"`
57+
}
58+
59+
func init() {
60+
SchemeBuilder.Register(&Admiral{}, &AdmiralList{})
61+
}

testdata/project-v2/api/v1/zz_generated.deepcopy.go

+89
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,95 @@ import (
2424
"k8s.io/apimachinery/pkg/runtime"
2525
)
2626

27+
// DeepCopyInto is an autogenerated deepcopy function, copying the receiver, writing into out. in must be non-nil.
28+
func (in *Admiral) DeepCopyInto(out *Admiral) {
29+
*out = *in
30+
out.TypeMeta = in.TypeMeta
31+
in.ObjectMeta.DeepCopyInto(&out.ObjectMeta)
32+
out.Spec = in.Spec
33+
out.Status = in.Status
34+
}
35+
36+
// DeepCopy is an autogenerated deepcopy function, copying the receiver, creating a new Admiral.
37+
func (in *Admiral) DeepCopy() *Admiral {
38+
if in == nil {
39+
return nil
40+
}
41+
out := new(Admiral)
42+
in.DeepCopyInto(out)
43+
return out
44+
}
45+
46+
// DeepCopyObject is an autogenerated deepcopy function, copying the receiver, creating a new runtime.Object.
47+
func (in *Admiral) DeepCopyObject() runtime.Object {
48+
if c := in.DeepCopy(); c != nil {
49+
return c
50+
}
51+
return nil
52+
}
53+
54+
// DeepCopyInto is an autogenerated deepcopy function, copying the receiver, writing into out. in must be non-nil.
55+
func (in *AdmiralList) DeepCopyInto(out *AdmiralList) {
56+
*out = *in
57+
out.TypeMeta = in.TypeMeta
58+
out.ListMeta = in.ListMeta
59+
if in.Items != nil {
60+
in, out := &in.Items, &out.Items
61+
*out = make([]Admiral, len(*in))
62+
for i := range *in {
63+
(*in)[i].DeepCopyInto(&(*out)[i])
64+
}
65+
}
66+
}
67+
68+
// DeepCopy is an autogenerated deepcopy function, copying the receiver, creating a new AdmiralList.
69+
func (in *AdmiralList) DeepCopy() *AdmiralList {
70+
if in == nil {
71+
return nil
72+
}
73+
out := new(AdmiralList)
74+
in.DeepCopyInto(out)
75+
return out
76+
}
77+
78+
// DeepCopyObject is an autogenerated deepcopy function, copying the receiver, creating a new runtime.Object.
79+
func (in *AdmiralList) DeepCopyObject() runtime.Object {
80+
if c := in.DeepCopy(); c != nil {
81+
return c
82+
}
83+
return nil
84+
}
85+
86+
// DeepCopyInto is an autogenerated deepcopy function, copying the receiver, writing into out. in must be non-nil.
87+
func (in *AdmiralSpec) DeepCopyInto(out *AdmiralSpec) {
88+
*out = *in
89+
}
90+
91+
// DeepCopy is an autogenerated deepcopy function, copying the receiver, creating a new AdmiralSpec.
92+
func (in *AdmiralSpec) DeepCopy() *AdmiralSpec {
93+
if in == nil {
94+
return nil
95+
}
96+
out := new(AdmiralSpec)
97+
in.DeepCopyInto(out)
98+
return out
99+
}
100+
101+
// DeepCopyInto is an autogenerated deepcopy function, copying the receiver, writing into out. in must be non-nil.
102+
func (in *AdmiralStatus) DeepCopyInto(out *AdmiralStatus) {
103+
*out = *in
104+
}
105+
106+
// DeepCopy is an autogenerated deepcopy function, copying the receiver, creating a new AdmiralStatus.
107+
func (in *AdmiralStatus) DeepCopy() *AdmiralStatus {
108+
if in == nil {
109+
return nil
110+
}
111+
out := new(AdmiralStatus)
112+
in.DeepCopyInto(out)
113+
return out
114+
}
115+
27116
// DeepCopyInto is an autogenerated deepcopy function, copying the receiver, writing into out. in must be non-nil.
28117
func (in *Captain) DeepCopyInto(out *Captain) {
29118
*out = *in
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
2+
---
3+
apiVersion: apiextensions.k8s.io/v1beta1
4+
kind: CustomResourceDefinition
5+
metadata:
6+
creationTimestamp: null
7+
name: admirals.crew.testproject.org
8+
spec:
9+
group: crew.testproject.org
10+
names:
11+
kind: Admiral
12+
plural: admirals
13+
scope: Cluster
14+
validation:
15+
openAPIV3Schema:
16+
description: Admiral is the Schema for the admirals API
17+
properties:
18+
apiVersion:
19+
description: 'APIVersion defines the versioned schema of this representation
20+
of an object. Servers should convert recognized schemas to the latest
21+
internal value, and may reject unrecognized values. More info: https://git.k8s.io/community/contributors/devel/api-conventions.md#resources'
22+
type: string
23+
kind:
24+
description: 'Kind is a string value representing the REST resource this
25+
object represents. Servers may infer this from the endpoint the client
26+
submits requests to. Cannot be updated. In CamelCase. More info: https://git.k8s.io/community/contributors/devel/api-conventions.md#types-kinds'
27+
type: string
28+
metadata:
29+
type: object
30+
spec:
31+
description: AdmiralSpec defines the desired state of Admiral
32+
type: object
33+
status:
34+
description: AdmiralStatus defines the observed state of Admiral
35+
type: object
36+
type: object
37+
versions:
38+
- name: v1
39+
served: true
40+
storage: true
41+
status:
42+
acceptedNames:
43+
kind: ""
44+
plural: ""
45+
conditions: []
46+
storedVersions: []

testdata/project-v2/config/crd/kustomization.yaml

+3
Original file line numberDiff line numberDiff line change
@@ -4,19 +4,22 @@
44
resources:
55
- bases/crew.testproject.org_captains.yaml
66
- bases/crew.testproject.org_firstmates.yaml
7+
- bases/crew.testproject.org_admirals.yaml
78
# +kubebuilder:scaffold:crdkustomizeresource
89

910
patches:
1011
# [WEBHOOK] To enable webhook, uncomment all the sections with [WEBHOOK] prefix.
1112
# patches here are for enabling the conversion webhook for each CRD
1213
#- patches/webhook_in_captains.yaml
1314
#- patches/webhook_in_firstmates.yaml
15+
#- patches/webhook_in_admirals.yaml
1416
# +kubebuilder:scaffold:crdkustomizewebhookpatch
1517

1618
# [CERTMANAGER] To enable webhook, uncomment all the sections with [CERTMANAGER] prefix.
1719
# patches here are for enabling the CA injection for each CRD
1820
#- patches/cainjection_in_captains.yaml
1921
#- patches/cainjection_in_firstmates.yaml
22+
#- patches/cainjection_in_admirals.yaml
2023
# +kubebuilder:scaffold:crdkustomizecainjectionpatch
2124

2225
# the following config is for teaching kustomize how to do kustomization for CRDs.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
# The following patch adds a directive for certmanager to inject CA into the CRD
2+
# CRD conversion requires k8s 1.13 or later.
3+
apiVersion: apiextensions.k8s.io/v1beta1
4+
kind: CustomResourceDefinition
5+
metadata:
6+
annotations:
7+
certmanager.k8s.io/inject-ca-from: $(CERTIFICATE_NAMESPACE)/$(CERTIFICATE_NAME)
8+
name: admirals.crew.testproject.org
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
# The following patch enables conversion webhook for CRD
2+
# CRD conversion requires k8s 1.13 or later.
3+
apiVersion: apiextensions.k8s.io/v1beta1
4+
kind: CustomResourceDefinition
5+
metadata:
6+
name: admirals.crew.testproject.org
7+
spec:
8+
conversion:
9+
strategy: Webhook
10+
webhookClientConfig:
11+
# this is "\n" used as a placeholder, otherwise it will be rejected by the apiserver for being blank,
12+
# but we're going to set it later using the cert-manager (or potentially a patch if not using cert-manager)
13+
caBundle: Cg==
14+
service:
15+
namespace: system
16+
name: webhook-service
17+
path: /convert

testdata/project-v2/config/rbac/role.yaml

+20
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,26 @@ rules:
2626
- get
2727
- patch
2828
- update
29+
- apiGroups:
30+
- crew.testproject.org
31+
resources:
32+
- admirals
33+
verbs:
34+
- create
35+
- delete
36+
- get
37+
- list
38+
- patch
39+
- update
40+
- watch
41+
- apiGroups:
42+
- crew.testproject.org
43+
resources:
44+
- admirals/status
45+
verbs:
46+
- get
47+
- patch
48+
- update
2949
- apiGroups:
3050
- crew.testproject.org
3151
resources:
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
apiVersion: crew.testproject.org/v1
2+
kind: Admiral
3+
metadata:
4+
name: admiral-sample
5+
spec:
6+
# Add fields here
7+
foo: bar
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
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 controllers
18+
19+
import (
20+
"context"
21+
22+
"github.com/go-logr/logr"
23+
ctrl "sigs.k8s.io/controller-runtime"
24+
"sigs.k8s.io/controller-runtime/pkg/client"
25+
26+
crewv1 "sigs.k8s.io/kubebuilder/testdata/project-v2/api/v1"
27+
)
28+
29+
// AdmiralReconciler reconciles a Admiral object
30+
type AdmiralReconciler struct {
31+
client.Client
32+
Log logr.Logger
33+
}
34+
35+
// +kubebuilder:rbac:groups=crew.testproject.org,resources=admirals,verbs=get;list;watch;create;update;patch;delete
36+
// +kubebuilder:rbac:groups=crew.testproject.org,resources=admirals/status,verbs=get;update;patch
37+
38+
func (r *AdmiralReconciler) Reconcile(req ctrl.Request) (ctrl.Result, error) {
39+
_ = context.Background()
40+
_ = r.Log.WithValues("admiral", req.NamespacedName)
41+
42+
// your logic here
43+
44+
return ctrl.Result{}, nil
45+
}
46+
47+
func (r *AdmiralReconciler) SetupWithManager(mgr ctrl.Manager) error {
48+
return ctrl.NewControllerManagedBy(mgr).
49+
For(&crewv1.Admiral{}).
50+
Complete(r)
51+
}

testdata/project-v2/controllers/suite_test.go

+3
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,9 @@ var _ = BeforeSuite(func(done Done) {
6868
err = crewv1.AddToScheme(scheme.Scheme)
6969
Expect(err).NotTo(HaveOccurred())
7070

71+
err = crewv1.AddToScheme(scheme.Scheme)
72+
Expect(err).NotTo(HaveOccurred())
73+
7174
err = corev1.AddToScheme(scheme.Scheme)
7275
Expect(err).NotTo(HaveOccurred())
7376

testdata/project-v2/main.go

+7
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,13 @@ func main() {
8686
setupLog.Error(err, "unable to create webhook", "webhook", "FirstMate")
8787
os.Exit(1)
8888
}
89+
if err = (&controllers.AdmiralReconciler{
90+
Client: mgr.GetClient(),
91+
Log: ctrl.Log.WithName("controllers").WithName("Admiral"),
92+
}).SetupWithManager(mgr); err != nil {
93+
setupLog.Error(err, "unable to create controller", "controller", "Admiral")
94+
os.Exit(1)
95+
}
8996
if err = (&controllers.NamespaceReconciler{
9097
Client: mgr.GetClient(),
9198
Log: ctrl.Log.WithName("controllers").WithName("Namespace"),

0 commit comments

Comments
 (0)