Skip to content

Commit

Permalink
Add scaffold to controllers with the api cor external resources if we…
Browse files Browse the repository at this point in the history
… do know the path

Signed-off-by: Adrian Orive <adrian.orive.oneca@gmail.com>
  • Loading branch information
Adirio committed Feb 10, 2021
1 parent bc933c3 commit 8c55b84
Show file tree
Hide file tree
Showing 15 changed files with 59 additions and 44 deletions.
2 changes: 1 addition & 1 deletion generate_testdata.sh
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ scaffold_test_project() {

$kb create api --group foo.policy --version v1 --kind HealthCheckPolicy --controller=true --resource=true --make=false

$kb create api --group apps --version v1 --kind Pod --controller=true --resource=false --make=false
$kb create api --group apps --version v1 --kind Deployment --controller=true --resource=false --make=false

if [ $project == "project-v3-multigroup" ]; then
$kb create api --version v1 --kind Lakers --controller=true --resource=true --make=false
Expand Down
12 changes: 9 additions & 3 deletions pkg/model/file/funcmap.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,18 @@ import (
// DefaultFuncMap returns the default template.FuncMap for rendering the template.
func DefaultFuncMap() template.FuncMap {
return template.FuncMap{
"title": strings.Title,
"lower": strings.ToLower,
"hashFNV": hashFNV,
"title": strings.Title,
"lower": strings.ToLower,
"isEmptyStr": isEmptyString,
"hashFNV": hashFNV,
}
}

// isEmptyString returns whether the string is empty
func isEmptyString(s string) bool {
return s == ""
}

// hashFNV will generate a random string useful for generating a unique string
func hashFNV(s string) (string, error) {
hasher := fnv.New32a()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ import (
"k8s.io/apimachinery/pkg/runtime"
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/client"
{{ if .Resource.HasAPI -}}
{{ if not (isEmptyStr .Resource.Path) -}}
{{ .Resource.ImportAlias }} "{{ .Resource.Path }}"
{{- end }}
)
Expand Down Expand Up @@ -108,7 +108,7 @@ func (r *{{ .Resource.Kind }}Reconciler) Reconcile(req ctrl.Request) (ctrl.Resul
// SetupWithManager sets up the controller with the Manager.
func (r *{{ .Resource.Kind }}Reconciler) SetupWithManager(mgr ctrl.Manager) error {
return ctrl.NewControllerManagedBy(mgr).
{{ if .Resource.HasAPI -}}
{{ if not (isEmptyStr .Resource.Path) -}}
For(&{{ .Resource.ImportAlias }}.{{ .Resource.Kind }}{}).
{{- else -}}
// Uncomment the following line adding a pointer to an instance of the controlled resource as an argument
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -98,13 +98,13 @@ func (f *SuiteTest) GetCodeFragments() file.CodeFragmentsMap {

// Generate import code fragments
imports := make([]string, 0)
if f.Resource.HasAPI() {
if f.Resource.Path != "" {
imports = append(imports, fmt.Sprintf(apiImportCodeFragment, f.Resource.ImportAlias(), f.Resource.Path))
}

// Generate add scheme code fragments
addScheme := make([]string, 0)
if f.Resource.HasAPI() {
if f.Resource.Path != "" {
addScheme = append(addScheme, fmt.Sprintf(addschemeCodeFragment, f.Resource.ImportAlias()))
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ import (
"k8s.io/apimachinery/pkg/runtime"
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/client"
{{ if .Resource.HasAPI -}}
{{ if not (isEmptyStr .Resource.Path) -}}
{{ .Resource.ImportAlias }} "{{ .Resource.Path }}"
{{- end }}
)
Expand Down Expand Up @@ -108,7 +108,7 @@ func (r *{{ .Resource.Kind }}Reconciler) Reconcile(ctx context.Context, req ctrl
// SetupWithManager sets up the controller with the Manager.
func (r *{{ .Resource.Kind }}Reconciler) SetupWithManager(mgr ctrl.Manager) error {
return ctrl.NewControllerManagedBy(mgr).
{{ if .Resource.HasAPI -}}
{{ if not (isEmptyStr .Resource.Path) -}}
For(&{{ .Resource.ImportAlias }}.{{ .Resource.Kind }}{}).
{{- else -}}
// Uncomment the following line adding a pointer to an instance of the controlled resource as an argument
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -98,13 +98,13 @@ func (f *SuiteTest) GetCodeFragments() file.CodeFragmentsMap {

// Generate import code fragments
imports := make([]string, 0)
if f.Resource.HasAPI() {
if f.Resource.Path != "" {
imports = append(imports, fmt.Sprintf(apiImportCodeFragment, f.Resource.ImportAlias(), f.Resource.Path))
}

// Generate add scheme code fragments
addScheme := make([]string, 0)
if f.Resource.HasAPI() {
if f.Resource.Path != "" {
addScheme = append(addScheme, fmt.Sprintf(addschemeCodeFragment, f.Resource.ImportAlias()))
}

Expand Down
4 changes: 2 additions & 2 deletions testdata/project-v2-multigroup/config/rbac/role.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ rules:
- apiGroups:
- apps
resources:
- pods
- deployments
verbs:
- create
- delete
Expand All @@ -21,7 +21,7 @@ rules:
- apiGroups:
- apps
resources:
- pods/status
- deployments/status
verbs:
- get
- patch
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,43 +20,43 @@ import (
"context"

"github.com/go-logr/logr"
appsv1 "k8s.io/api/apps/v1"
"k8s.io/apimachinery/pkg/runtime"
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/client"
)

// PodReconciler reconciles a Pod object
type PodReconciler struct {
// DeploymentReconciler reconciles a Deployment object
type DeploymentReconciler struct {
client.Client
Log logr.Logger
Scheme *runtime.Scheme
}

//+kubebuilder:rbac:groups=apps,resources=pods,verbs=get;list;watch;create;update;patch;delete
//+kubebuilder:rbac:groups=apps,resources=pods/status,verbs=get;update;patch
//+kubebuilder:rbac:groups=apps,resources=deployments,verbs=get;list;watch;create;update;patch;delete
//+kubebuilder:rbac:groups=apps,resources=deployments/status,verbs=get;update;patch

// Reconcile is part of the main kubernetes reconciliation loop which aims to
// move the current state of the cluster closer to the desired state.
// TODO(user): Modify the Reconcile function to compare the state specified by
// the Pod object against the actual cluster state, and then
// the Deployment object against the actual cluster state, and then
// perform operations to make the cluster state reflect the state specified by
// the user.
//
// For more details, check Reconcile and its Result here:
// - https://pkg.go.dev/sigs.k8s.io/controller-runtime@v0.6.4/pkg/reconcile
func (r *PodReconciler) Reconcile(req ctrl.Request) (ctrl.Result, error) {
func (r *DeploymentReconciler) Reconcile(req ctrl.Request) (ctrl.Result, error) {
_ = context.Background()
_ = r.Log.WithValues("pod", req.NamespacedName)
_ = r.Log.WithValues("deployment", req.NamespacedName)

// your logic here

return ctrl.Result{}, nil
}

// SetupWithManager sets up the controller with the Manager.
func (r *PodReconciler) SetupWithManager(mgr ctrl.Manager) error {
func (r *DeploymentReconciler) SetupWithManager(mgr ctrl.Manager) error {
return ctrl.NewControllerManagedBy(mgr).
// Uncomment the following line adding a pointer to an instance of the controlled resource as an argument
// For().
For(&appsv1.Deployment{}).
Complete(r)
}
4 changes: 4 additions & 0 deletions testdata/project-v2-multigroup/controllers/apps/suite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (

. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
appsv1 "k8s.io/api/apps/v1"
"k8s.io/client-go/kubernetes/scheme"
"k8s.io/client-go/rest"
"sigs.k8s.io/controller-runtime/pkg/client"
Expand Down Expand Up @@ -60,6 +61,9 @@ var _ = BeforeSuite(func(done Done) {
Expect(err).ToNot(HaveOccurred())
Expect(cfg).ToNot(BeNil())

err = appsv1.AddToScheme(scheme.Scheme)
Expect(err).NotTo(HaveOccurred())

//+kubebuilder:scaffold:scheme

k8sClient, err = client.New(cfg, client.Options{Scheme: scheme.Scheme})
Expand Down
1 change: 1 addition & 0 deletions testdata/project-v2-multigroup/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ require (
github.com/go-logr/logr v0.1.0
github.com/onsi/ginkgo v1.12.1
github.com/onsi/gomega v1.10.1
k8s.io/api v0.18.6
k8s.io/apimachinery v0.18.6
k8s.io/client-go v0.18.6
sigs.k8s.io/controller-runtime v0.6.4
Expand Down
6 changes: 3 additions & 3 deletions testdata/project-v2-multigroup/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -155,12 +155,12 @@ func main() {
setupLog.Error(err, "unable to create controller", "controller", "HealthCheckPolicy")
os.Exit(1)
}
if err = (&appscontroller.PodReconciler{
if err = (&appscontroller.DeploymentReconciler{
Client: mgr.GetClient(),
Log: ctrl.Log.WithName("controllers").WithName("Pod"),
Log: ctrl.Log.WithName("controllers").WithName("Deployment"),
Scheme: mgr.GetScheme(),
}).SetupWithManager(mgr); err != nil {
setupLog.Error(err, "unable to create controller", "controller", "Pod")
setupLog.Error(err, "unable to create controller", "controller", "Deployment")
os.Exit(1)
}
//+kubebuilder:scaffold:builder
Expand Down
6 changes: 3 additions & 3 deletions testdata/project-v3-multigroup/config/rbac/role.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ rules:
- apiGroups:
- apps
resources:
- pods
- deployments
verbs:
- create
- delete
Expand All @@ -21,13 +21,13 @@ rules:
- apiGroups:
- apps
resources:
- pods/finalizers
- deployments/finalizers
verbs:
- update
- apiGroups:
- apps
resources:
- pods/status
- deployments/status
verbs:
- get
- patch
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,43 +20,43 @@ import (
"context"

"github.com/go-logr/logr"
appsv1 "k8s.io/api/apps/v1"
"k8s.io/apimachinery/pkg/runtime"
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/client"
)

// PodReconciler reconciles a Pod object
type PodReconciler struct {
// DeploymentReconciler reconciles a Deployment object
type DeploymentReconciler struct {
client.Client
Log logr.Logger
Scheme *runtime.Scheme
}

//+kubebuilder:rbac:groups=apps,resources=pods,verbs=get;list;watch;create;update;patch;delete
//+kubebuilder:rbac:groups=apps,resources=pods/status,verbs=get;update;patch
//+kubebuilder:rbac:groups=apps,resources=pods/finalizers,verbs=update
//+kubebuilder:rbac:groups=apps,resources=deployments,verbs=get;list;watch;create;update;patch;delete
//+kubebuilder:rbac:groups=apps,resources=deployments/status,verbs=get;update;patch
//+kubebuilder:rbac:groups=apps,resources=deployments/finalizers,verbs=update

// Reconcile is part of the main kubernetes reconciliation loop which aims to
// move the current state of the cluster closer to the desired state.
// TODO(user): Modify the Reconcile function to compare the state specified by
// the Pod object against the actual cluster state, and then
// the Deployment object against the actual cluster state, and then
// perform operations to make the cluster state reflect the state specified by
// the user.
//
// For more details, check Reconcile and its Result here:
// - https://pkg.go.dev/sigs.k8s.io/controller-runtime@v0.7.0/pkg/reconcile
func (r *PodReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.Result, error) {
_ = r.Log.WithValues("pod", req.NamespacedName)
func (r *DeploymentReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.Result, error) {
_ = r.Log.WithValues("deployment", req.NamespacedName)

// your logic here

return ctrl.Result{}, nil
}

// SetupWithManager sets up the controller with the Manager.
func (r *PodReconciler) SetupWithManager(mgr ctrl.Manager) error {
func (r *DeploymentReconciler) SetupWithManager(mgr ctrl.Manager) error {
return ctrl.NewControllerManagedBy(mgr).
// Uncomment the following line adding a pointer to an instance of the controlled resource as an argument
// For().
For(&appsv1.Deployment{}).
Complete(r)
}
4 changes: 4 additions & 0 deletions testdata/project-v3-multigroup/controllers/apps/suite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (

. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
appsv1 "k8s.io/api/apps/v1"
"k8s.io/client-go/kubernetes/scheme"
"k8s.io/client-go/rest"
"sigs.k8s.io/controller-runtime/pkg/client"
Expand Down Expand Up @@ -60,6 +61,9 @@ var _ = BeforeSuite(func() {
Expect(err).NotTo(HaveOccurred())
Expect(cfg).NotTo(BeNil())

err = appsv1.AddToScheme(scheme.Scheme)
Expect(err).NotTo(HaveOccurred())

//+kubebuilder:scaffold:scheme

k8sClient, err = client.New(cfg, client.Options{Scheme: scheme.Scheme})
Expand Down
6 changes: 3 additions & 3 deletions testdata/project-v3-multigroup/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -169,12 +169,12 @@ func main() {
setupLog.Error(err, "unable to create controller", "controller", "HealthCheckPolicy")
os.Exit(1)
}
if err = (&appscontrollers.PodReconciler{
if err = (&appscontrollers.DeploymentReconciler{
Client: mgr.GetClient(),
Log: ctrl.Log.WithName("controllers").WithName("apps").WithName("Pod"),
Log: ctrl.Log.WithName("controllers").WithName("apps").WithName("Deployment"),
Scheme: mgr.GetScheme(),
}).SetupWithManager(mgr); err != nil {
setupLog.Error(err, "unable to create controller", "controller", "Pod")
setupLog.Error(err, "unable to create controller", "controller", "Deployment")
os.Exit(1)
}
if err = (&controllers.LakersReconciler{
Expand Down

0 comments on commit 8c55b84

Please sign in to comment.