Skip to content

Commit d2f16c2

Browse files
fix scaffold of controllers for Core Types
By using the CLI we are able to scaffold Controllers for core types. We have been testing within the testdata samples the generation of the case scanarios which allow us scaffold the controller for Deployment where group is not empty. Therefore, its instroduce tests with Pod and ConfigMap which are endpoints with empty groups and fixing the issue faced to do its scaffold.
1 parent 944fd9b commit d2f16c2

File tree

11 files changed

+365
-0
lines changed

11 files changed

+365
-0
lines changed

pkg/plugins/golang/v4/scaffolds/internal/templates/controllers/controller_suitetest.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,7 @@ package controller
136136
{{end}}
137137
138138
import (
139+
"context"
139140
"fmt"
140141
"path/filepath"
141142
"runtime"

test/testdata/generate.sh

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,10 @@ function scaffold_test_project {
6868
$kb create api --group apps --version v1 --kind Deployment --controller=true --resource=false --make=false
6969
$kb create api --group foo --version v1 --kind Bar --controller=true --resource=true --make=false
7070
$kb create api --group fiz --version v1 --kind Bar --controller=true --resource=true --make=false
71+
72+
# Create controller for resources which has empty group
73+
$kb create api --group "" --version v1 --kind Pod --controller=true --resource=false --make=false
74+
$kb create api --group "" --version v1 --kind ConfigMap --controller=true --resource=false --make=false
7175
fi
7276

7377
if [[ $project =~ multigroup ]] || [[ $project =~ with-plugins ]] ; then

testdata/project-v4-multigroup/PROJECT

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,14 @@ resources:
125125
kind: Bar
126126
path: sigs.k8s.io/kubebuilder/testdata/project-v4-multigroup/api/fiz/v1
127127
version: v1
128+
- controller: true
129+
domain: testproject.org
130+
kind: Pod
131+
version: v1
132+
- controller: true
133+
domain: testproject.org
134+
kind: ConfigMap
135+
version: v1
128136
- api:
129137
crdVersion: v1
130138
namespaced: true

testdata/project-v4-multigroup/cmd/main.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ import (
4545
shipv1 "sigs.k8s.io/kubebuilder/testdata/project-v4-multigroup/api/ship/v1"
4646
shipv1beta1 "sigs.k8s.io/kubebuilder/testdata/project-v4-multigroup/api/ship/v1beta1"
4747
shipv2alpha1 "sigs.k8s.io/kubebuilder/testdata/project-v4-multigroup/api/ship/v2alpha1"
48+
"sigs.k8s.io/kubebuilder/testdata/project-v4-multigroup/internal/controller"
4849
appscontroller "sigs.k8s.io/kubebuilder/testdata/project-v4-multigroup/internal/controller/apps"
4950
crewcontroller "sigs.k8s.io/kubebuilder/testdata/project-v4-multigroup/internal/controller/crew"
5051
examplecomcontroller "sigs.k8s.io/kubebuilder/testdata/project-v4-multigroup/internal/controller/example.com"
@@ -267,6 +268,20 @@ func main() {
267268
setupLog.Error(err, "unable to create controller", "controller", "Bar")
268269
os.Exit(1)
269270
}
271+
if err = (&controller.PodReconciler{
272+
Client: mgr.GetClient(),
273+
Scheme: mgr.GetScheme(),
274+
}).SetupWithManager(mgr); err != nil {
275+
setupLog.Error(err, "unable to create controller", "controller", "Pod")
276+
os.Exit(1)
277+
}
278+
if err = (&controller.ConfigMapReconciler{
279+
Client: mgr.GetClient(),
280+
Scheme: mgr.GetScheme(),
281+
}).SetupWithManager(mgr); err != nil {
282+
setupLog.Error(err, "unable to create controller", "controller", "ConfigMap")
283+
os.Exit(1)
284+
}
270285
if err = (&examplecomcontroller.MemcachedReconciler{
271286
Client: mgr.GetClient(),
272287
Scheme: mgr.GetScheme(),

testdata/project-v4-multigroup/config/rbac/role.yaml

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -216,3 +216,32 @@ rules:
216216
- get
217217
- patch
218218
- update
219+
- apiGroups:
220+
- testproject.org
221+
resources:
222+
- configmaps
223+
- pods
224+
verbs:
225+
- create
226+
- delete
227+
- get
228+
- list
229+
- patch
230+
- update
231+
- watch
232+
- apiGroups:
233+
- testproject.org
234+
resources:
235+
- configmaps/finalizers
236+
- pods/finalizers
237+
verbs:
238+
- update
239+
- apiGroups:
240+
- testproject.org
241+
resources:
242+
- configmaps/status
243+
- pods/status
244+
verbs:
245+
- get
246+
- patch
247+
- update

testdata/project-v4-multigroup/dist/install.yaml

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1347,6 +1347,35 @@ rules:
13471347
- get
13481348
- patch
13491349
- update
1350+
- apiGroups:
1351+
- testproject.org
1352+
resources:
1353+
- configmaps
1354+
- pods
1355+
verbs:
1356+
- create
1357+
- delete
1358+
- get
1359+
- list
1360+
- patch
1361+
- update
1362+
- watch
1363+
- apiGroups:
1364+
- testproject.org
1365+
resources:
1366+
- configmaps/finalizers
1367+
- pods/finalizers
1368+
verbs:
1369+
- update
1370+
- apiGroups:
1371+
- testproject.org
1372+
resources:
1373+
- configmaps/status
1374+
- pods/status
1375+
verbs:
1376+
- get
1377+
- patch
1378+
- update
13501379
---
13511380
apiVersion: rbac.authorization.k8s.io/v1
13521381
kind: ClusterRole
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
/*
2+
Copyright 2024 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 controller
18+
19+
import (
20+
"context"
21+
22+
"k8s.io/apimachinery/pkg/runtime"
23+
ctrl "sigs.k8s.io/controller-runtime"
24+
"sigs.k8s.io/controller-runtime/pkg/client"
25+
"sigs.k8s.io/controller-runtime/pkg/log"
26+
)
27+
28+
// ConfigMapReconciler reconciles a ConfigMap object
29+
type ConfigMapReconciler struct {
30+
client.Client
31+
Scheme *runtime.Scheme
32+
}
33+
34+
// +kubebuilder:rbac:groups=testproject.org,resources=configmaps,verbs=get;list;watch;create;update;patch;delete
35+
// +kubebuilder:rbac:groups=testproject.org,resources=configmaps/status,verbs=get;update;patch
36+
// +kubebuilder:rbac:groups=testproject.org,resources=configmaps/finalizers,verbs=update
37+
38+
// Reconcile is part of the main kubernetes reconciliation loop which aims to
39+
// move the current state of the cluster closer to the desired state.
40+
// TODO(user): Modify the Reconcile function to compare the state specified by
41+
// the ConfigMap object against the actual cluster state, and then
42+
// perform operations to make the cluster state reflect the state specified by
43+
// the user.
44+
//
45+
// For more details, check Reconcile and its Result here:
46+
// - https://pkg.go.dev/sigs.k8s.io/[email protected]/pkg/reconcile
47+
func (r *ConfigMapReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.Result, error) {
48+
_ = log.FromContext(ctx)
49+
50+
// TODO(user): your logic here
51+
52+
return ctrl.Result{}, nil
53+
}
54+
55+
// SetupWithManager sets up the controller with the Manager.
56+
func (r *ConfigMapReconciler) SetupWithManager(mgr ctrl.Manager) error {
57+
return ctrl.NewControllerManagedBy(mgr).
58+
// Uncomment the following line adding a pointer to an instance of the controlled resource as an argument
59+
// For().
60+
Named("configmap").
61+
Complete(r)
62+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
/*
2+
Copyright 2024 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 controller
18+
19+
import (
20+
. "github.com/onsi/ginkgo/v2"
21+
)
22+
23+
var _ = Describe("ConfigMap Controller", func() {
24+
Context("When reconciling a resource", func() {
25+
26+
It("should successfully reconcile the resource", func() {
27+
28+
// TODO(user): Add more specific assertions depending on your controller's reconciliation logic.
29+
// Example: If you expect a certain status condition after reconciliation, verify it here.
30+
})
31+
})
32+
})
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
/*
2+
Copyright 2024 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 controller
18+
19+
import (
20+
"context"
21+
22+
"k8s.io/apimachinery/pkg/runtime"
23+
ctrl "sigs.k8s.io/controller-runtime"
24+
"sigs.k8s.io/controller-runtime/pkg/client"
25+
"sigs.k8s.io/controller-runtime/pkg/log"
26+
)
27+
28+
// PodReconciler reconciles a Pod object
29+
type PodReconciler struct {
30+
client.Client
31+
Scheme *runtime.Scheme
32+
}
33+
34+
// +kubebuilder:rbac:groups=testproject.org,resources=pods,verbs=get;list;watch;create;update;patch;delete
35+
// +kubebuilder:rbac:groups=testproject.org,resources=pods/status,verbs=get;update;patch
36+
// +kubebuilder:rbac:groups=testproject.org,resources=pods/finalizers,verbs=update
37+
38+
// Reconcile is part of the main kubernetes reconciliation loop which aims to
39+
// move the current state of the cluster closer to the desired state.
40+
// TODO(user): Modify the Reconcile function to compare the state specified by
41+
// the Pod object against the actual cluster state, and then
42+
// perform operations to make the cluster state reflect the state specified by
43+
// the user.
44+
//
45+
// For more details, check Reconcile and its Result here:
46+
// - https://pkg.go.dev/sigs.k8s.io/[email protected]/pkg/reconcile
47+
func (r *PodReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.Result, error) {
48+
_ = log.FromContext(ctx)
49+
50+
// TODO(user): your logic here
51+
52+
return ctrl.Result{}, nil
53+
}
54+
55+
// SetupWithManager sets up the controller with the Manager.
56+
func (r *PodReconciler) SetupWithManager(mgr ctrl.Manager) error {
57+
return ctrl.NewControllerManagedBy(mgr).
58+
// Uncomment the following line adding a pointer to an instance of the controlled resource as an argument
59+
// For().
60+
Named("pod").
61+
Complete(r)
62+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
/*
2+
Copyright 2024 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 controller
18+
19+
import (
20+
. "github.com/onsi/ginkgo/v2"
21+
)
22+
23+
var _ = Describe("Pod Controller", func() {
24+
Context("When reconciling a resource", func() {
25+
26+
It("should successfully reconcile the resource", func() {
27+
28+
// TODO(user): Add more specific assertions depending on your controller's reconciliation logic.
29+
// Example: If you expect a certain status condition after reconciliation, verify it here.
30+
})
31+
})
32+
})

0 commit comments

Comments
 (0)