Skip to content

Commit

Permalink
Move templates to internal package
Browse files Browse the repository at this point in the history
Signed-off-by: Adrian Orive <adrian.orive.oneca@gmail.com>
  • Loading branch information
Adirio committed Feb 6, 2020
1 parent 48ea4cb commit 66baebf
Show file tree
Hide file tree
Showing 85 changed files with 361 additions and 91 deletions.
24 changes: 12 additions & 12 deletions pkg/scaffold/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,11 @@ import (
"sigs.k8s.io/kubebuilder/pkg/model"
"sigs.k8s.io/kubebuilder/pkg/model/resource"
"sigs.k8s.io/kubebuilder/pkg/scaffold/internal/machinery"
controllerv1 "sigs.k8s.io/kubebuilder/pkg/scaffold/v1/controller"
crdv1 "sigs.k8s.io/kubebuilder/pkg/scaffold/v1/crd"
scaffoldv2 "sigs.k8s.io/kubebuilder/pkg/scaffold/v2"
controllerv2 "sigs.k8s.io/kubebuilder/pkg/scaffold/v2/controller"
crdv2 "sigs.k8s.io/kubebuilder/pkg/scaffold/v2/crd"
controllerv1 "sigs.k8s.io/kubebuilder/pkg/scaffold/internal/templates/v1/controller"
crdv1 "sigs.k8s.io/kubebuilder/pkg/scaffold/internal/templates/v1/crd"
templatesv2 "sigs.k8s.io/kubebuilder/pkg/scaffold/internal/templates/v2"
controllerv2 "sigs.k8s.io/kubebuilder/pkg/scaffold/internal/templates/v2/controller"
crdv2 "sigs.k8s.io/kubebuilder/pkg/scaffold/internal/templates/v2/crd"
)

// apiScaffolder contains configuration for generating scaffolding for Go type
Expand Down Expand Up @@ -151,11 +151,11 @@ func (s *apiScaffolder) scaffoldV2() error {

if err := machinery.NewScaffold(s.plugins...).Execute(
s.newUniverse(),
&scaffoldv2.Types{Resource: s.resource},
&scaffoldv2.Group{Resource: s.resource},
&scaffoldv2.CRDSample{Resource: s.resource},
&scaffoldv2.CRDEditorRole{Resource: s.resource},
&scaffoldv2.CRDViewerRole{Resource: s.resource},
&templatesv2.Types{Resource: s.resource},
&templatesv2.Group{Resource: s.resource},
&templatesv2.CRDSample{Resource: s.resource},
&templatesv2.CRDEditorRole{Resource: s.resource},
&templatesv2.CRDViewerRole{Resource: s.resource},
&crdv2.EnableWebhookPatch{Resource: s.resource},
&crdv2.EnableCAInjectionPatch{Resource: s.resource},
); err != nil {
Expand Down Expand Up @@ -206,8 +206,8 @@ func (s *apiScaffolder) scaffoldV2() error {
}
}

if err := (&scaffoldv2.Main{}).Update(
&scaffoldv2.MainUpdateOptions{
if err := (&templatesv2.Main{}).Update(
&templatesv2.MainUpdateOptions{
Config: &s.config.Config,
WireResource: s.doResource,
WireController: s.doController,
Expand Down
122 changes: 66 additions & 56 deletions pkg/scaffold/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,16 +25,15 @@ import (
"sigs.k8s.io/kubebuilder/pkg/model"
"sigs.k8s.io/kubebuilder/pkg/model/file"
"sigs.k8s.io/kubebuilder/pkg/scaffold/internal/machinery"
"sigs.k8s.io/kubebuilder/pkg/scaffold/project"
scaffoldv1 "sigs.k8s.io/kubebuilder/pkg/scaffold/v1"
managerv1 "sigs.k8s.io/kubebuilder/pkg/scaffold/v1/manager"
metricsauthv1 "sigs.k8s.io/kubebuilder/pkg/scaffold/v1/metricsauth"
scaffoldv2 "sigs.k8s.io/kubebuilder/pkg/scaffold/v2"
certmanagerv2 "sigs.k8s.io/kubebuilder/pkg/scaffold/v2/certmanager"
managerv2 "sigs.k8s.io/kubebuilder/pkg/scaffold/v2/manager"
metricsauthv2 "sigs.k8s.io/kubebuilder/pkg/scaffold/v2/metricsauth"
prometheusv2 "sigs.k8s.io/kubebuilder/pkg/scaffold/v2/prometheus"
webhookv2 "sigs.k8s.io/kubebuilder/pkg/scaffold/v2/webhook"
templatesv1 "sigs.k8s.io/kubebuilder/pkg/scaffold/internal/templates/v1"
managerv1 "sigs.k8s.io/kubebuilder/pkg/scaffold/internal/templates/v1/manager"
metricsauthv1 "sigs.k8s.io/kubebuilder/pkg/scaffold/internal/templates/v1/metricsauth"
templatesv2 "sigs.k8s.io/kubebuilder/pkg/scaffold/internal/templates/v2"
certmanagerv2 "sigs.k8s.io/kubebuilder/pkg/scaffold/internal/templates/v2/certmanager"
managerv2 "sigs.k8s.io/kubebuilder/pkg/scaffold/internal/templates/v2/manager"
metricsauthv2 "sigs.k8s.io/kubebuilder/pkg/scaffold/internal/templates/v2/metricsauth"
prometheusv2 "sigs.k8s.io/kubebuilder/pkg/scaffold/internal/templates/v2/prometheus"
webhookv2 "sigs.k8s.io/kubebuilder/pkg/scaffold/internal/templates/v2/webhook"
)

const (
Expand All @@ -49,7 +48,6 @@ const (
type initScaffolder struct {
config *config.Config
boilerplatePath string
boilerplate string
license string
owner string
}
Expand All @@ -63,10 +61,10 @@ func NewInitScaffolder(config *config.Config, license, owner string) Scaffolder
}
}

func (s *initScaffolder) newUniverse() *model.Universe {
func (s *initScaffolder) newUniverse(boilerplate string) *model.Universe {
return model.NewUniverse(
model.WithConfig(&s.config.Config),
model.WithBoilerplate(s.boilerplate),
model.WithBoilerplate(boilerplate),
)
}

Expand All @@ -77,9 +75,20 @@ func (s *initScaffolder) Scaffold() error {
return err
}

switch {
case s.config.IsV1():
return s.scaffoldV1()
case s.config.IsV2():
return s.scaffoldV2()
default:
return fmt.Errorf("unknown project version %v", s.config.Version)
}
}

func (s *initScaffolder) scaffoldV1() error {
if err := machinery.NewScaffold().Execute(
s.newUniverse(), // Boilerplate is still empty by this call as desired
&project.Boilerplate{
s.newUniverse(""),
&templatesv1.Boilerplate{
Input: file.Input{Path: s.boilerplatePath},
License: s.license,
Owner: s.owner,
Expand All @@ -88,45 +97,27 @@ func (s *initScaffolder) Scaffold() error {
return err
}

boilerplateBytes, err := ioutil.ReadFile(s.boilerplatePath) // nolint:gosec
boilerplate, err := ioutil.ReadFile(s.boilerplatePath) // nolint:gosec
if err != nil {
return err
}
s.boilerplate = string(boilerplateBytes)

if err := machinery.NewScaffold().Execute(
s.newUniverse(),
&project.GitIgnore{},
&project.AuthProxyRole{},
&project.AuthProxyRoleBinding{},
); err != nil {
return err
}

switch {
case s.config.IsV1():
return s.scaffoldV1()
case s.config.IsV2():
return s.scaffoldV2()
default:
return fmt.Errorf("unknown project version %v", s.config.Version)
}
}

func (s *initScaffolder) scaffoldV1() error {
return machinery.NewScaffold().Execute(
s.newUniverse(),
&project.KustomizeRBAC{},
&scaffoldv1.KustomizeImagePatch{},
s.newUniverse(string(boilerplate)),
&templatesv1.GitIgnore{},
&templatesv1.AuthProxyRole{},
&templatesv1.AuthProxyRoleBinding{},
&templatesv1.KustomizeRBAC{},
&templatesv1.KustomizeImagePatch{},
&metricsauthv1.KustomizePrometheusMetricsPatch{},
&metricsauthv1.KustomizeAuthProxyPatch{},
&scaffoldv1.AuthProxyService{},
&templatesv1.AuthProxyService{},
&managerv1.Config{Image: ImageName},
&project.Makefile{Image: ImageName},
&project.GopkgToml{},
&templatesv1.Makefile{Image: ImageName},
&templatesv1.GopkgToml{},
&managerv1.Dockerfile{},
&project.Kustomize{},
&project.KustomizeManager{},
&templatesv1.Kustomize{},
&templatesv1.KustomizeManager{},
&managerv1.APIs{BoilerplatePath: s.boilerplatePath},
&managerv1.Controller{},
&managerv1.Webhook{},
Expand All @@ -135,26 +126,45 @@ func (s *initScaffolder) scaffoldV1() error {
}

func (s *initScaffolder) scaffoldV2() error {
if err := machinery.NewScaffold().Execute(
s.newUniverse(""),
&templatesv2.Boilerplate{
Input: file.Input{Path: s.boilerplatePath},
License: s.license,
Owner: s.owner,
},
); err != nil {
return err
}

boilerplate, err := ioutil.ReadFile(s.boilerplatePath) // nolint:gosec
if err != nil {
return err
}

return machinery.NewScaffold().Execute(
s.newUniverse(),
s.newUniverse(string(boilerplate)),
&templatesv2.GitIgnore{},
&templatesv2.AuthProxyRole{},
&templatesv2.AuthProxyRoleBinding{},
&metricsauthv2.AuthProxyPatch{},
&metricsauthv2.AuthProxyService{},
&metricsauthv2.ClientClusterRole{},
&managerv2.Config{Image: ImageName},
&scaffoldv2.Main{},
&scaffoldv2.GoMod{ControllerRuntimeVersion: ControllerRuntimeVersion},
&scaffoldv2.Makefile{
&templatesv2.Main{},
&templatesv2.GoMod{ControllerRuntimeVersion: ControllerRuntimeVersion},
&templatesv2.Makefile{
Image: ImageName,
BoilerplatePath: s.boilerplatePath,
ControllerToolsVersion: ControllerToolsVersion,
},
&scaffoldv2.Dockerfile{},
&scaffoldv2.Kustomize{},
&scaffoldv2.ManagerWebhookPatch{},
&scaffoldv2.ManagerRoleBinding{},
&scaffoldv2.LeaderElectionRole{},
&scaffoldv2.LeaderElectionRoleBinding{},
&scaffoldv2.KustomizeRBAC{},
&templatesv2.Dockerfile{},
&templatesv2.Kustomize{},
&templatesv2.ManagerWebhookPatch{},
&templatesv2.ManagerRoleBinding{},
&templatesv2.LeaderElectionRole{},
&templatesv2.LeaderElectionRoleBinding{},
&templatesv2.KustomizeRBAC{},
&managerv2.Kustomization{},
&webhookv2.Kustomization{},
&webhookv2.KustomizeConfigWebhook{},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ See the License for the specific language governing permissions and
limitations under the License.
*/

package project
package v1

import (
"path/filepath"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ See the License for the specific language governing permissions and
limitations under the License.
*/

package project
package v1

import (
"path/filepath"
Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ See the License for the specific language governing permissions and
limitations under the License.
*/

package project
package v1

import (
"fmt"
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ See the License for the specific language governing permissions and
limitations under the License.
*/

package project
package v1

import (
"sigs.k8s.io/kubebuilder/pkg/model/file"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ See the License for the specific language governing permissions and
limitations under the License.
*/

package project
package v1

import (
"bufio"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ See the License for the specific language governing permissions and
limitations under the License.
*/

package project
package v1

import (
"os"
Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ See the License for the specific language governing permissions and
limitations under the License.
*/

package project
package v1

import (
"path/filepath"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ See the License for the specific language governing permissions and
limitations under the License.
*/

package project
package v1

import (
"path/filepath"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ See the License for the specific language governing permissions and
limitations under the License.
*/

package project
package v1

import (
"sigs.k8s.io/kubebuilder/pkg/model/file"
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
54 changes: 54 additions & 0 deletions pkg/scaffold/internal/templates/v2/authproxyrole.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
/*
Copyright 2018 The Kubernetes Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package v2

import (
"path/filepath"

"sigs.k8s.io/kubebuilder/pkg/model/file"
)

var _ file.Template = &AuthProxyRole{}

// AuthProxyRole scaffolds the config/rbac/auth_proxy_role.yaml file
type AuthProxyRole struct {
file.Input
}

// GetInput implements input.Template
func (f *AuthProxyRole) GetInput() (file.Input, error) {
if f.Path == "" {
f.Path = filepath.Join("config", "rbac", "auth_proxy_role.yaml")
}
f.TemplateBody = proxyRoleTemplate
return f.Input, nil
}

const proxyRoleTemplate = `apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRole
metadata:
name: proxy-role
rules:
- apiGroups: ["authentication.k8s.io"]
resources:
- tokenreviews
verbs: ["create"]
- apiGroups: ["authorization.k8s.io"]
resources:
- subjectaccessreviews
verbs: ["create"]
`
Loading

0 comments on commit 66baebf

Please sign in to comment.