Skip to content

Commit

Permalink
Move scaffold machinery to its own 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 Jan 31, 2020
1 parent 5fb03db commit 885d82e
Show file tree
Hide file tree
Showing 17 changed files with 602 additions and 289 deletions.
3 changes: 2 additions & 1 deletion cmd/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import (

"sigs.k8s.io/kubebuilder/cmd/internal"
"sigs.k8s.io/kubebuilder/internal/config"
"sigs.k8s.io/kubebuilder/pkg/model"
"sigs.k8s.io/kubebuilder/pkg/model/resource"
"sigs.k8s.io/kubebuilder/pkg/scaffold"
"sigs.k8s.io/kubebuilder/plugins/addon"
Expand Down Expand Up @@ -214,7 +215,7 @@ func (o *apiOptions) scaffolder(c *config.Config) (scaffold.Scaffolder, error) {
}

// Load the requested plugins
plugins := make([]scaffold.Plugin, 0)
plugins := make([]model.Plugin, 0)
switch strings.ToLower(o.pattern) {
case "":
// Default pattern
Expand Down
24 changes: 24 additions & 0 deletions pkg/model/plugin.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
/*
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 model

// Plugin is the interface that a plugin must implement
// We will (later) have an ExecPlugin that implements this by exec-ing a binary
type Plugin interface {
// Pipe is the core plugin interface, that transforms a UniverseModel
Pipe(*Universe) error
}
38 changes: 8 additions & 30 deletions pkg/model/universe.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,6 @@ limitations under the License.
package model

import (
"io/ioutil"

"sigs.k8s.io/kubebuilder/pkg/model/config"
"sigs.k8s.io/kubebuilder/pkg/model/resource"
)
Expand All @@ -39,62 +37,42 @@ type Universe struct {
}

// NewUniverse creates a new Universe
func NewUniverse(options ...UniverseOption) (*Universe, error) {
func NewUniverse(options ...UniverseOption) *Universe {
universe := &Universe{}

// Apply options
for _, option := range options {
if err := option(universe); err != nil {
return nil, err
}
option(universe)
}

return universe, nil
return universe
}

// UniverseOption configure Universe
type UniverseOption func(*Universe) error
type UniverseOption func(*Universe)

// WithConfig stores the already loaded project configuration
func WithConfig(projectConfig *config.Config) UniverseOption {
return func(universe *Universe) error {
return func(universe *Universe) {
universe.Config = projectConfig
return nil
}
}

// WithBoilerplateFrom loads the boilerplate from the provided path
func WithBoilerplateFrom(path string) UniverseOption {
return func(universe *Universe) error {
boilerplate, err := ioutil.ReadFile(path)
if err != nil {
return err
}

universe.Boilerplate = string(boilerplate)
return nil
}
}

// WithBoilerplate stores the already loaded project configuration
func WithBoilerplate(boilerplate string) UniverseOption {
return func(universe *Universe) error {
return func(universe *Universe) {
universe.Boilerplate = boilerplate
return nil
}
}

// WithoutBoilerplate is used for files that do not require a boilerplate
func WithoutBoilerplate(universe *Universe) error {
func WithoutBoilerplate(universe *Universe) {
universe.Boilerplate = "-"
return nil
}

// WithResource stores the provided resource
func WithResource(resource *resource.Resource) UniverseOption {
return func(universe *Universe) error {
return func(universe *Universe) {
universe.Resource = resource

return nil
}
}
54 changes: 15 additions & 39 deletions pkg/scaffold/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import (
"sigs.k8s.io/kubebuilder/pkg/model"
"sigs.k8s.io/kubebuilder/pkg/model/resource"
"sigs.k8s.io/kubebuilder/pkg/scaffold/input"
"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"
Expand All @@ -38,7 +39,7 @@ type apiScaffolder struct {
config *config.Config
resource *resource.Resource
// plugins is the list of plugins we should allow to transform our generated scaffolding
plugins []Plugin
plugins []model.Plugin
// doResource indicates whether to scaffold API Resource or not
doResource bool
// doController indicates whether to scaffold controller files or not
Expand All @@ -49,7 +50,7 @@ func NewAPIScaffolder(
config *config.Config,
res *resource.Resource,
doResource, doController bool,
plugins []Plugin,
plugins []model.Plugin,
) Scaffolder {
return &apiScaffolder{
plugins: plugins,
Expand All @@ -73,10 +74,10 @@ func (s *apiScaffolder) Scaffold() error {
}
}

func (s *apiScaffolder) buildUniverse() (*model.Universe, error) {
func (s *apiScaffolder) buildUniverse() *model.Universe {
return model.NewUniverse(
model.WithConfig(&s.config.Config),
// TODO: missing model.WithBoilerplate[From], needs boilerplate or path
// TODO: missing model.WithBoilerplate, needs boilerplate
model.WithResource(s.resource),
)
}
Expand All @@ -88,13 +89,8 @@ func (s *apiScaffolder) scaffoldV1() error {
fmt.Println(filepath.Join("pkg", "apis", s.resource.GroupPackageName, s.resource.Version,
fmt.Sprintf("%s_types_test.go", strings.ToLower(s.resource.Kind))))

universe, err := s.buildUniverse()
if err != nil {
return fmt.Errorf("error building API scaffold: %v", err)
}

if err := (&Scaffold{}).Execute(
universe,
if err := (&machinery.Scaffold{}).Execute(
s.buildUniverse(),
input.Options{},
&crdv1.Register{Resource: s.resource},
&crdv1.Types{Resource: s.resource},
Expand All @@ -121,13 +117,8 @@ func (s *apiScaffolder) scaffoldV1() error {
fmt.Println(filepath.Join("pkg", "controller", strings.ToLower(s.resource.Kind),
fmt.Sprintf("%s_controller_test.go", strings.ToLower(s.resource.Kind))))

universe, err := s.buildUniverse()
if err != nil {
return fmt.Errorf("error building controller scaffold: %v", err)
}

if err := (&Scaffold{}).Execute(
universe,
if err := (&machinery.Scaffold{}).Execute(
s.buildUniverse(),
input.Options{},
&controllerv1.Controller{Resource: s.resource},
&controllerv1.AddController{Resource: s.resource},
Expand Down Expand Up @@ -158,13 +149,8 @@ func (s *apiScaffolder) scaffoldV2() error {
fmt.Sprintf("%s_types.go", strings.ToLower(s.resource.Kind))))
}

universe, err := s.buildUniverse()
if err != nil {
return fmt.Errorf("error building API scaffold: %v", err)
}

if err := (&Scaffold{Plugins: s.plugins}).Execute(
universe,
if err := (&machinery.Scaffold{Plugins: s.plugins}).Execute(
s.buildUniverse(),
input.Options{},
&scaffoldv2.Types{Resource: s.resource},
&scaffoldv2.Group{Resource: s.resource},
Expand All @@ -177,14 +163,9 @@ func (s *apiScaffolder) scaffoldV2() error {
return fmt.Errorf("error scaffolding APIs: %v", err)
}

universe, err = s.buildUniverse()
if err != nil {
return fmt.Errorf("error building kustomization scaffold: %v", err)
}

kustomizationFile := &crdv2.Kustomization{Resource: s.resource}
if err := (&Scaffold{}).Execute(
universe,
if err := (&machinery.Scaffold{}).Execute(
s.buildUniverse(),
input.Options{},
kustomizationFile,
&crdv2.KustomizeConfig{},
Expand Down Expand Up @@ -213,14 +194,9 @@ func (s *apiScaffolder) scaffoldV2() error {
fmt.Sprintf("%s_controller.go", strings.ToLower(s.resource.Kind))))
}

universe, err := s.buildUniverse()
if err != nil {
return fmt.Errorf("error building controller scaffold: %v", err)
}

suiteTestFile := &controllerv2.SuiteTest{Resource: s.resource}
if err := (&Scaffold{Plugins: s.plugins}).Execute(
universe,
if err := (&machinery.Scaffold{Plugins: s.plugins}).Execute(
s.buildUniverse(),
input.Options{},
suiteTestFile,
&controllerv2.Controller{Resource: s.resource},
Expand Down
63 changes: 26 additions & 37 deletions pkg/scaffold/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (
"sigs.k8s.io/kubebuilder/internal/config"
"sigs.k8s.io/kubebuilder/pkg/model"
"sigs.k8s.io/kubebuilder/pkg/scaffold/input"
"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"
Expand All @@ -47,6 +48,7 @@ const (
type initScaffolder struct {
config *config.Config
boilerplatePath string
boilerplate string
license string
owner string
}
Expand All @@ -67,16 +69,11 @@ func (s *initScaffolder) Scaffold() error {
return err
}

universe, err := model.NewUniverse(
model.WithConfig(&s.config.Config),
model.WithoutBoilerplate,
)
if err != nil {
return fmt.Errorf("error initializing project: %v", err)
}

if err := (&Scaffold{BoilerplateOptional: true}).Execute(
universe,
if err := (&machinery.Scaffold{BoilerplateOptional: true}).Execute(
model.NewUniverse(
model.WithConfig(&s.config.Config),
model.WithoutBoilerplate,
),
input.Options{ProjectPath: s.config.Path(), BoilerplatePath: s.boilerplatePath},
&project.Boilerplate{
Input: input.Input{Path: s.boilerplatePath},
Expand All @@ -87,16 +84,18 @@ func (s *initScaffolder) Scaffold() error {
return err
}

universe, err = model.NewUniverse(
model.WithConfig(&s.config.Config),
model.WithBoilerplateFrom(s.boilerplatePath),
)
var boilerplateBytes []byte
boilerplateBytes, err := ioutil.ReadFile(s.boilerplatePath) // nolint:gosec
if err != nil {
return fmt.Errorf("error initializing project: %v", err)
return err
}
s.boilerplate = string(boilerplateBytes)

if err := (&Scaffold{}).Execute(
universe,
if err := (&machinery.Scaffold{}).Execute(
model.NewUniverse(
model.WithConfig(&s.config.Config),
model.WithBoilerplate(s.boilerplate),
),
input.Options{ProjectPath: s.config.Path(), BoilerplatePath: s.boilerplatePath},
&project.GitIgnore{},
&project.AuthProxyRole{},
Expand All @@ -116,16 +115,11 @@ func (s *initScaffolder) Scaffold() error {
}

func (s *initScaffolder) scaffoldV1() error {
universe, err := model.NewUniverse(
model.WithConfig(&s.config.Config),
model.WithBoilerplateFrom(s.boilerplatePath),
)
if err != nil {
return fmt.Errorf("error initializing project: %v", err)
}

return (&Scaffold{}).Execute(
universe,
return (&machinery.Scaffold{}).Execute(
model.NewUniverse(
model.WithConfig(&s.config.Config),
model.WithBoilerplate(s.boilerplate),
),
input.Options{ProjectPath: s.config.Path(), BoilerplatePath: s.boilerplatePath},
&project.KustomizeRBAC{},
&scaffoldv1.KustomizeImagePatch{},
Expand All @@ -146,16 +140,11 @@ func (s *initScaffolder) scaffoldV1() error {
}

func (s *initScaffolder) scaffoldV2() error {
universe, err := model.NewUniverse(
model.WithConfig(&s.config.Config),
model.WithBoilerplateFrom(s.boilerplatePath),
)
if err != nil {
return fmt.Errorf("error initializing project: %v", err)
}

return (&Scaffold{}).Execute(
universe,
return (&machinery.Scaffold{}).Execute(
model.NewUniverse(
model.WithConfig(&s.config.Config),
model.WithBoilerplate(s.boilerplate),
),
input.Options{ProjectPath: s.config.Path(), BoilerplatePath: s.boilerplatePath},
&metricsauthv2.AuthProxyPatch{},
&metricsauthv2.AuthProxyService{},
Expand Down
Loading

0 comments on commit 885d82e

Please sign in to comment.