Skip to content

Commit

Permalink
Add the rest of the missing fields and stabilize config v3
Browse files Browse the repository at this point in the history
Signed-off-by: Adrian Orive Oneca <adrian.orive.oneca@gmail.com>
  • Loading branch information
Adirio committed Jan 26, 2021
1 parent 7b84f60 commit f94f8f0
Show file tree
Hide file tree
Showing 14 changed files with 229 additions and 121 deletions.
6 changes: 3 additions & 3 deletions cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import (

"sigs.k8s.io/kubebuilder/v3/pkg/cli"
cfgv2 "sigs.k8s.io/kubebuilder/v3/pkg/config/v2"
cfgv3alpha "sigs.k8s.io/kubebuilder/v3/pkg/config/v3alpha"
cfgv3 "sigs.k8s.io/kubebuilder/v3/pkg/config/v3"
pluginv2 "sigs.k8s.io/kubebuilder/v3/pkg/plugins/golang/v2"
pluginv3 "sigs.k8s.io/kubebuilder/v3/pkg/plugins/golang/v3"
)
Expand All @@ -30,13 +30,13 @@ func main() {
c, err := cli.New(
cli.WithCommandName("kubebuilder"),
cli.WithVersion(versionString()),
cli.WithDefaultProjectVersion(cfgv3alpha.Version),
cli.WithDefaultProjectVersion(cfgv3.Version),
cli.WithPlugins(
&pluginv2.Plugin{},
&pluginv3.Plugin{},
),
cli.WithDefaultPlugins(cfgv2.Version, &pluginv2.Plugin{}),
cli.WithDefaultPlugins(cfgv3alpha.Version, &pluginv3.Plugin{}),
cli.WithDefaultPlugins(cfgv3.Version, &pluginv3.Plugin{}),
cli.WithCompletion,
)
if err != nil {
Expand Down
4 changes: 2 additions & 2 deletions pkg/cli/cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ import (

internalconfig "sigs.k8s.io/kubebuilder/v3/pkg/cli/internal/config"
"sigs.k8s.io/kubebuilder/v3/pkg/config"
cfgv3alpha "sigs.k8s.io/kubebuilder/v3/pkg/config/v3alpha"
cfgv3 "sigs.k8s.io/kubebuilder/v3/pkg/config/v3"
"sigs.k8s.io/kubebuilder/v3/pkg/plugin"
)

Expand Down Expand Up @@ -131,7 +131,7 @@ func newCLI(opts ...Option) (*cli, error) {
// Default cli options.
c := &cli{
commandName: "kubebuilder",
defaultProjectVersion: cfgv3alpha.Version,
defaultProjectVersion: cfgv3.Version,
defaultPlugins: make(map[config.Version][]string),
plugins: make(map[string]plugin.Plugin),
}
Expand Down
4 changes: 2 additions & 2 deletions pkg/cli/cli_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ import (

"sigs.k8s.io/kubebuilder/v3/pkg/config"
cfgv2 "sigs.k8s.io/kubebuilder/v3/pkg/config/v2"
cfgv3alpha "sigs.k8s.io/kubebuilder/v3/pkg/config/v3alpha"
cfgv3 "sigs.k8s.io/kubebuilder/v3/pkg/config/v3"
"sigs.k8s.io/kubebuilder/v3/pkg/plugin"
)

Expand Down Expand Up @@ -203,7 +203,7 @@ var _ = Describe("CLI", func() {

When("having layout field", func() {
It("should succeed", func() {
projectConfig = cfgv3alpha.New()
projectConfig = cfgv3.New()
Expect(projectConfig.SetLayout("go.kubebuilder.io/v2")).To(Succeed())
projectVersion, plugins, err = getInfoFromConfig(projectConfig)
Expect(err).NotTo(HaveOccurred())
Expand Down
2 changes: 1 addition & 1 deletion pkg/config/v2/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ var _ = Describe("cfg", func() {
})
})

Context("Name", func() {
Context("ProjectName", func() {
It("GetProjectName should return an empty name", func() {
Expect(c.GetProjectName()).To(Equal(""))
})
Expand Down
54 changes: 27 additions & 27 deletions pkg/config/v3alpha/config.go → pkg/config/v3/config.go
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 v3alpha
package v3

import (
"fmt"
Expand All @@ -24,11 +24,10 @@ import (

"sigs.k8s.io/kubebuilder/v3/pkg/config"
"sigs.k8s.io/kubebuilder/v3/pkg/model/resource"
"sigs.k8s.io/kubebuilder/v3/pkg/model/stage"
)

// Version is the config.Version for project configuration 3-alpha
var Version = config.Version{Number: 3, Stage: stage.Alpha}
// Version is the config.Version for project configuration 3
var Version = config.Version{Number: 3}

type cfg struct {
// Version
Expand Down Expand Up @@ -157,8 +156,6 @@ func (c cfg) ResourcesLength() int {

// HasResource implements config.Config
func (c cfg) HasResource(gvk resource.GVK) bool {
gvk.Domain = "" // Version 3 alpha does not include domain per resource

for _, res := range c.Resources {
if gvk.IsEqualTo(res.GVK) {
return true
Expand All @@ -170,11 +167,16 @@ func (c cfg) HasResource(gvk resource.GVK) bool {

// GetResource implements config.Config
func (c cfg) GetResource(gvk resource.GVK) (resource.Resource, error) {
gvk.Domain = "" // Version 3 alpha does not include domain per resource

for _, res := range c.Resources {
if gvk.IsEqualTo(res.GVK) {
return res.Copy(), nil
r := res.Copy()

// Plural is only stored if irregular, so if it is empty recover the regular form
if r.Plural == "" {
r.Plural = resource.RegularPlural(r.Kind)
}

return r, nil
}
}

Expand All @@ -185,33 +187,28 @@ func (c cfg) GetResource(gvk resource.GVK) (resource.Resource, error) {
func (c cfg) GetResources() ([]resource.Resource, error) {
resources := make([]resource.Resource, 0, len(c.Resources))
for _, res := range c.Resources {
resources = append(resources, res.Copy())
}
r := res.Copy()

return resources, nil
}
// Plural is only stored if irregular, so if it is empty recover the regular form
if r.Plural == "" {
r.Plural = resource.RegularPlural(r.Kind)
}

func discardNonIncludedFields(res *resource.Resource) {
res.Domain = "" // Version 3 alpha does not include domain per resource
res.Plural = "" // Version 3 alpha does not include plural forms
res.Path = "" // Version 3 alpha does not include paths
if res.API != nil {
res.API.Namespaced = false // Version 3 alpha does not include if the api was namespaced
}
res.Controller = false // Version 3 alpha does not include if the controller was scaffolded
if res.Webhooks != nil {
res.Webhooks.Defaulting = false // Version 3 alpha does not include if the defaulting webhook was scaffolded
res.Webhooks.Validation = false // Version 3 alpha does not include if the validation webhook was scaffolded
res.Webhooks.Conversion = false // Version 3 alpha does not include if the conversion webhook was scaffolded
resources = append(resources, r)
}

return resources, nil
}

// AddResource implements config.Config
func (c *cfg) AddResource(res resource.Resource) error {
// As res is passed by value it is already a shallow copy, but we need to make a deep copy
res = res.Copy()

discardNonIncludedFields(&res) // Version 3 alpha does not include several fields from the Resource model
// Plural is only stored if irregular
if res.Plural == resource.RegularPlural(res.Kind) {
res.Plural = ""
}

if !c.HasResource(res.GVK) {
c.Resources = append(c.Resources, res)
Expand All @@ -224,7 +221,10 @@ func (c *cfg) UpdateResource(res resource.Resource) error {
// As res is passed by value it is already a shallow copy, but we need to make a deep copy
res = res.Copy()

discardNonIncludedFields(&res) // Version 3 alpha does not include several fields from the Resource model
// Plural is only stored if irregular
if res.Plural == resource.RegularPlural(res.Kind) {
res.Plural = ""
}

for i, r := range c.Resources {
if res.GVK.IsEqualTo(r.GVK) {
Expand Down
Loading

0 comments on commit f94f8f0

Please sign in to comment.