Skip to content

Commit

Permalink
Merge pull request #1313 from Adirio/scaffold-enhancement/project-in-…
Browse files Browse the repository at this point in the history
…universe

Include PROJECT file information into model.Universe
  • Loading branch information
k8s-ci-robot authored Jan 28, 2020
2 parents 5aaea23 + 6534660 commit dc5ea0c
Show file tree
Hide file tree
Showing 10 changed files with 356 additions and 134 deletions.
14 changes: 11 additions & 3 deletions cmd/vendor_update.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,13 +39,21 @@ kubebuilder update vendor
Run: func(cmd *cobra.Command, args []string) {
internal.DieIfNotConfigured()

err := (&scaffold.Scaffold{}).Execute(
&model.Universe{},
universe, err := model.NewUniverse(
model.WithConfigFrom("PROJECT"),
model.WithoutBoilerplate,
)
if err != nil {
log.Fatalf("error updating vendor dependencies: %v", err)
}

err = (&scaffold.Scaffold{}).Execute(
universe,
input.Options{},
&project.GopkgToml{},
)
if err != nil {
log.Fatalf("error updating vendor dependecies %v", err)
log.Fatalf("error updating vendor dependencies: %v", err)
}
},
}
Expand Down
15 changes: 13 additions & 2 deletions cmd/webhook_v1.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,9 +66,20 @@ This command is only available for v1 scaffolding project.
}

fmt.Println("Writing scaffold for you to edit...")

universe, err := model.NewUniverse(
model.WithConfig(projectConfig),
// TODO: missing model.WithBoilerplate[From], needs boilerplate or path
model.WithResource(o.res, projectConfig),
)
if err != nil {
log.Fatalf("error scaffolding webhook: %v", err)
}

webhookConfig := webhook.Config{Server: o.server, Type: o.webhookType, Operations: o.operations}

err = (&scaffold.Scaffold{}).Execute(
&model.Universe{},
universe,
input.Options{},
&manager.Webhook{},
&webhook.AdmissionHandler{Resource: o.res, Config: webhookConfig},
Expand All @@ -79,7 +90,7 @@ This command is only available for v1 scaffolding project.
&webhook.AddServer{Config: webhookConfig},
)
if err != nil {
log.Fatal(err)
log.Fatalf("error scaffolding webhook: %v", err)
}

if o.doMake {
Expand Down
24 changes: 16 additions & 8 deletions cmd/webhook_v2.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,19 +87,27 @@ func newWebhookV2Cmd() *cobra.Command {
fmt.Println(`Webhook server has been set up for you.
You need to implement the conversion.Hub and conversion.Convertible interfaces for your CRD types.`)
}
webhookScaffolder := &webhook.Webhook{
Resource: o.res,
Defaulting: o.defaulting,
Validating: o.validation,

universe, err := model.NewUniverse(
model.WithConfig(projectConfig),
// TODO: missing model.WithBoilerplate[From], needs boilerplate or path
model.WithResource(o.res, projectConfig),
)
if err != nil {
log.Fatalf("error scaffolding webhook: %v", err)
}

err = (&scaffold.Scaffold{}).Execute(
&model.Universe{},
universe,
input.Options{},
webhookScaffolder,
&webhook.Webhook{
Resource: o.res,
Defaulting: o.defaulting,
Validating: o.validation,
},
)
if err != nil {
fmt.Printf("error scaffolding webhook: %v", err)
os.Exit(1)
log.Fatalf("error scaffolding webhook: %v", err)
}

err = (&scaffoldv2.Main{}).Update(
Expand Down
34 changes: 34 additions & 0 deletions pkg/model/file.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
/*
Copyright 2020 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

import (
"sigs.k8s.io/kubebuilder/pkg/scaffold/input"
)

// File describes a file that will be written
type File struct {
// Path is the file to write
Path string `json:"path,omitempty"`

// Contents is the generated output
Contents string `json:"contents,omitempty"`

// TODO: Move input.IfExistsAction into model
// IfExistsAction determines what to do if the file exists
IfExistsAction input.IfExistsAction `json:"ifExistsAction,omitempty"`
}
31 changes: 1 addition & 30 deletions pkg/model/types.go → pkg/model/resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,24 +16,8 @@ limitations under the License.

package model

import (
"sigs.k8s.io/kubebuilder/pkg/scaffold/input"
)

// Universe describes the entire state of file generation
type Universe struct {
Boilerplate string `json:"boilerplate,omitempty"`

Resource *Resource `json:"resource,omitempty"`

Files []*File `json:"files,omitempty"`

// Multigroup tracks if the project has more than one group
MultiGroup bool `json:"multigroup,omitempty"`
}

// Resource describes the resource currently being generated
// TODO: Just use the resource type?
// TODO: unify with pkg/scaffold/resource.Resource
type Resource struct {
// Namespaced is true if the resource is namespaced
Namespaced bool `json:"namespaces,omitempty"`
Expand All @@ -59,16 +43,3 @@ type Resource struct {
// GroupDomain is the Group + "." + Domain for the Resource
GroupDomain string `json:"groupDomain,omitempty"`
}

// File describes a file that will be written
type File struct {
// Path is the file to write
Path string `json:"path,omitempty"`

// Contents is the generated output
Contents string `json:"contents,omitempty"`

// TODO: Move input.IfExistsAction into model
// IfExistsAction determines what to do if the file exists
IfExistsAction input.IfExistsAction `json:"ifExistsAction,omitempty"`
}
133 changes: 133 additions & 0 deletions pkg/model/universe.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,133 @@
/*
Copyright 2020 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

import (
"io/ioutil"
"strings"

"github.com/gobuffalo/flect"

internalconfig "sigs.k8s.io/kubebuilder/internal/config"
"sigs.k8s.io/kubebuilder/pkg/model/config"
"sigs.k8s.io/kubebuilder/pkg/scaffold/resource"
"sigs.k8s.io/kubebuilder/pkg/scaffold/util"
)

// Universe describes the entire state of file generation
type Universe struct {
// Config stores the project configuration
Config *config.Config `json:"config,omitempty"`

// Boilerplate is the copyright comment added at the top of scaffolded files
Boilerplate string `json:"boilerplate,omitempty"`

// Resource contains the information of the API that is being scaffolded
Resource *Resource `json:"resource,omitempty"`

// Files contains the model of the files that are being scaffolded
Files []*File `json:"files,omitempty"`
}

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

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

return universe, nil
}

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

// WithConfigFrom loads the project configuration from the provided path
func WithConfigFrom(path string) UniverseOption {
return func(universe *Universe) error {
projectConfig, err := internalconfig.ReadFrom(path)
if err != nil {
return err
}

universe.Config = projectConfig
return nil
}
}

// WithConfig stores the already loaded project configuration
func WithConfig(projectConfig *config.Config) UniverseOption {
return func(universe *Universe) error {
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 {
universe.Boilerplate = string(boilerplate)
return nil
}
}

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

// WithResource stores the provided resource
func WithResource(resource *resource.Resource, project *config.Config) UniverseOption {
return func(universe *Universe) error {
resourceModel := &Resource{
Namespaced: resource.Namespaced,
Group: resource.Group,
Version: resource.Version,
Kind: resource.Kind,
Resource: resource.Resource,
Plural: flect.Pluralize(strings.ToLower(resource.Kind)),
}

resourceModel.GoPackage, resourceModel.GroupDomain = util.GetResourceInfo(
resource,
project.Repo,
project.Domain,
project.MultiGroup,
)

universe.Resource = resourceModel
return nil
}
}
Loading

0 comments on commit dc5ea0c

Please sign in to comment.