From 5695c8e5328248e0e721226def90270eff9657e7 Mon Sep 17 00:00:00 2001 From: Eric Stroczynski Date: Tue, 7 May 2019 16:02:47 -0700 Subject: [PATCH 1/5] internal/pkg/scaffold: add stub.go scaffold to prevent deepcopy parse errors --- cmd/operator-sdk/add/api.go | 1 + internal/pkg/scaffold/stub.go | 40 +++++++++++++++++++++++++++++++++++ 2 files changed, 41 insertions(+) create mode 100644 internal/pkg/scaffold/stub.go diff --git a/cmd/operator-sdk/add/api.go b/cmd/operator-sdk/add/api.go index 2b11ba04965..47f20310f5b 100644 --- a/cmd/operator-sdk/add/api.go +++ b/cmd/operator-sdk/add/api.go @@ -105,6 +105,7 @@ func apiRun(cmd *cobra.Command, args []string) error { s := &scaffold.Scaffold{} err = s.Execute(cfg, + &scaffold.Stub{Resource: r}, &scaffold.Types{Resource: r}, &scaffold.AddToScheme{Resource: r}, &scaffold.Register{Resource: r}, diff --git a/internal/pkg/scaffold/stub.go b/internal/pkg/scaffold/stub.go new file mode 100644 index 00000000000..ee2a7b63d96 --- /dev/null +++ b/internal/pkg/scaffold/stub.go @@ -0,0 +1,40 @@ +// Copyright 2019 The Operator-SDK 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 scaffold + +import ( + "path/filepath" + + "github.com/operator-framework/operator-sdk/internal/pkg/scaffold/input" +) + +const StubFile = "stub.go" + +type Stub struct { + input.Input + + Resource *Resource +} + +func (s *Stub) GetInput() (input.Input, error) { + if s.Path == "" { + s.Path = filepath.Join(ApisDir, s.Resource.GoImportGroup, StubFile) + } + s.TemplateBody = stubTmpl + return s.Input, nil +} + +const stubTmpl = `package {{.Resource.GoImportGroup}} +` From 87da655932055b3ed933a82c5b79b0174fe867f3 Mon Sep 17 00:00:00 2001 From: Eric Stroczynski Date: Wed, 8 May 2019 10:09:33 -0700 Subject: [PATCH 2/5] cmd/operator-sdk/add/api.go: check if go file exists in pkg/apis/ before scaffolding stub --- cmd/operator-sdk/add/api.go | 37 +++++++++++++++++++++++++++++++++-- internal/pkg/scaffold/stub.go | 2 ++ 2 files changed, 37 insertions(+), 2 deletions(-) diff --git a/cmd/operator-sdk/add/api.go b/cmd/operator-sdk/add/api.go index 47f20310f5b..261e9eca631 100644 --- a/cmd/operator-sdk/add/api.go +++ b/cmd/operator-sdk/add/api.go @@ -16,12 +16,16 @@ package add import ( "fmt" + "io/ioutil" + "os" + "path/filepath" "github.com/operator-framework/operator-sdk/cmd/operator-sdk/internal/genutil" "github.com/operator-framework/operator-sdk/internal/pkg/scaffold" "github.com/operator-framework/operator-sdk/internal/pkg/scaffold/input" "github.com/operator-framework/operator-sdk/internal/util/projutil" + "github.com/pkg/errors" log "github.com/sirupsen/logrus" "github.com/spf13/cobra" ) @@ -102,10 +106,16 @@ func apiRun(cmd *cobra.Command, args []string) error { Repo: projutil.CheckAndGetProjectGoPkg(), AbsProjectPath: absProjectPath, } - s := &scaffold.Scaffold{} + + // Check if any package files for this API group dir exist, and if not + // scaffold a stub.go to prevent erroneous gengo parse errors. + stub := &scaffold.Stub{Resource: r} + if err := scaffoldIfNoPkgFileExists(s, cfg, stub); err != nil { + return errors.Wrap(err, "scaffold stub file") + } + err = s.Execute(cfg, - &scaffold.Stub{Resource: r}, &scaffold.Types{Resource: r}, &scaffold.AddToScheme{Resource: r}, &scaffold.Register{Resource: r}, @@ -135,3 +145,26 @@ func apiRun(cmd *cobra.Command, args []string) error { log.Info("API generation complete.") return nil } + +// scaffoldIfNoPkgFileExists executes f using s and cfg if no go files +// in f's directory exist. +func scaffoldIfNoPkgFileExists(s *scaffold.Scaffold, cfg *input.Config, f input.File) error { + i, err := f.GetInput() + if err != nil { + return errors.Wrapf(err, "error getting file %s input", i.Path) + } + groupDir := filepath.Dir(i.Path) + gdInfos, err := ioutil.ReadDir(groupDir) + if err != nil && !os.IsNotExist(err) { + return errors.Wrapf(err, "error reading dir %s", groupDir) + } + if err == nil { + for _, info := range gdInfos { + if !info.IsDir() && filepath.Ext(info.Name()) == ".go" { + return nil + } + } + } + // err must be a non-existence error or no go files exist, so execute f. + return s.Execute(cfg, f) +} diff --git a/internal/pkg/scaffold/stub.go b/internal/pkg/scaffold/stub.go index ee2a7b63d96..5bf47d876ea 100644 --- a/internal/pkg/scaffold/stub.go +++ b/internal/pkg/scaffold/stub.go @@ -28,6 +28,8 @@ type Stub struct { Resource *Resource } +var _ input.File = &Stub{} + func (s *Stub) GetInput() (input.Input, error) { if s.Path == "" { s.Path = filepath.Join(ApisDir, s.Resource.GoImportGroup, StubFile) From 2c86c45ca7e326eba1e8254262b8db0670e2eab7 Mon Sep 17 00:00:00 2001 From: Eric Stroczynski Date: Fri, 14 Jun 2019 09:49:06 -0700 Subject: [PATCH 3/5] rename [Ss]tub -> [Gg]roup --- cmd/operator-sdk/add/api.go | 2 +- internal/pkg/scaffold/{stub.go => group.go} | 13 +++++++------ 2 files changed, 8 insertions(+), 7 deletions(-) rename internal/pkg/scaffold/{stub.go => group.go} (71%) diff --git a/cmd/operator-sdk/add/api.go b/cmd/operator-sdk/add/api.go index 261e9eca631..999df7b2a22 100644 --- a/cmd/operator-sdk/add/api.go +++ b/cmd/operator-sdk/add/api.go @@ -110,7 +110,7 @@ func apiRun(cmd *cobra.Command, args []string) error { // Check if any package files for this API group dir exist, and if not // scaffold a stub.go to prevent erroneous gengo parse errors. - stub := &scaffold.Stub{Resource: r} + stub := &scaffold.Group{Resource: r} if err := scaffoldIfNoPkgFileExists(s, cfg, stub); err != nil { return errors.Wrap(err, "scaffold stub file") } diff --git a/internal/pkg/scaffold/stub.go b/internal/pkg/scaffold/group.go similarity index 71% rename from internal/pkg/scaffold/stub.go rename to internal/pkg/scaffold/group.go index 5bf47d876ea..57e5f09b66b 100644 --- a/internal/pkg/scaffold/stub.go +++ b/internal/pkg/scaffold/group.go @@ -20,23 +20,24 @@ import ( "github.com/operator-framework/operator-sdk/internal/pkg/scaffold/input" ) -const StubFile = "stub.go" +const GroupFile = "group.go" -type Stub struct { +type Group struct { input.Input Resource *Resource } -var _ input.File = &Stub{} +var _ input.File = &Group{} -func (s *Stub) GetInput() (input.Input, error) { +func (s *Group) GetInput() (input.Input, error) { if s.Path == "" { - s.Path = filepath.Join(ApisDir, s.Resource.GoImportGroup, StubFile) + s.Path = filepath.Join(ApisDir, s.Resource.GoImportGroup, GroupFile) } s.TemplateBody = stubTmpl return s.Input, nil } -const stubTmpl = `package {{.Resource.GoImportGroup}} +const stubTmpl = `// Package {{.Resource.GoImportGroup}} contains {{.Resource.GoImportGroup}} API versions +package {{.Resource.GoImportGroup}} ` From f5b74ed1f9815285d3799975478544ad242d4a5b Mon Sep 17 00:00:00 2001 From: Eric Stroczynski Date: Fri, 14 Jun 2019 10:00:28 -0700 Subject: [PATCH 4/5] changes based on PR comments --- CHANGELOG.md | 1 + cmd/operator-sdk/add/api.go | 8 ++++---- internal/pkg/scaffold/group.go | 4 ++-- 3 files changed, 7 insertions(+), 6 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 718abb9f36e..2067e266e92 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,7 @@ - Enables controller-runtime metrics in Helm operator projects. ([#1482](https://github.com/operator-framework/operator-sdk/pull/1482)) - New flags `--vendor` and `--skip-validation` for [`operator-sdk new`](https://github.com/operator-framework/operator-sdk/blob/master/doc/sdk-cli-reference.md#new) that direct the SDK to initialize a new project with a `vendor/` directory, and without validating project dependencies. `vendor/` is not written by default. ([#1519](https://github.com/operator-framework/operator-sdk/pull/1519)) - Generating and serving info metrics about each custom resource. By default these metrics are exposed on port 8686. ([#1277](https://github.com/operator-framework/operator-sdk/pull/1277)) +- Scaffold a `pkg/apis//group.go` package file to avoid `go/build` errors when running Kubernetes code generators. ([#1401](https://github.com/operator-framework/operator-sdk/pull/1401)) ### Changed diff --git a/cmd/operator-sdk/add/api.go b/cmd/operator-sdk/add/api.go index 999df7b2a22..4be4934544f 100644 --- a/cmd/operator-sdk/add/api.go +++ b/cmd/operator-sdk/add/api.go @@ -109,10 +109,10 @@ func apiRun(cmd *cobra.Command, args []string) error { s := &scaffold.Scaffold{} // Check if any package files for this API group dir exist, and if not - // scaffold a stub.go to prevent erroneous gengo parse errors. - stub := &scaffold.Group{Resource: r} - if err := scaffoldIfNoPkgFileExists(s, cfg, stub); err != nil { - return errors.Wrap(err, "scaffold stub file") + // scaffold a group.go to prevent erroneous gengo parse errors. + group := &scaffold.Group{Resource: r} + if err := scaffoldIfNoPkgFileExists(s, cfg, group); err != nil { + return errors.Wrap(err, "scaffold group file") } err = s.Execute(cfg, diff --git a/internal/pkg/scaffold/group.go b/internal/pkg/scaffold/group.go index 57e5f09b66b..900503d8b5c 100644 --- a/internal/pkg/scaffold/group.go +++ b/internal/pkg/scaffold/group.go @@ -34,10 +34,10 @@ func (s *Group) GetInput() (input.Input, error) { if s.Path == "" { s.Path = filepath.Join(ApisDir, s.Resource.GoImportGroup, GroupFile) } - s.TemplateBody = stubTmpl + s.TemplateBody = groupTmpl return s.Input, nil } -const stubTmpl = `// Package {{.Resource.GoImportGroup}} contains {{.Resource.GoImportGroup}} API versions +const groupTmpl = `// Package {{.Resource.GoImportGroup}} contains {{.Resource.GoImportGroup}} API versions package {{.Resource.GoImportGroup}} ` From 7f47faa8c08fd01ce7a7f1dfae8bede459057526 Mon Sep 17 00:00:00 2001 From: Eric Stroczynski Date: Fri, 14 Jun 2019 10:14:38 -0700 Subject: [PATCH 5/5] add explanation to group.go scaffold --- internal/pkg/scaffold/group.go | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/internal/pkg/scaffold/group.go b/internal/pkg/scaffold/group.go index 900503d8b5c..e406451f5d2 100644 --- a/internal/pkg/scaffold/group.go +++ b/internal/pkg/scaffold/group.go @@ -38,6 +38,10 @@ func (s *Group) GetInput() (input.Input, error) { return s.Input, nil } -const groupTmpl = `// Package {{.Resource.GoImportGroup}} contains {{.Resource.GoImportGroup}} API versions +const groupTmpl = `// Package {{.Resource.GoImportGroup}} contains {{.Resource.GoImportGroup}} API versions. +// +// This file ensures Go source parsers acknowledge the {{.Resource.GoImportGroup}} package +// and any child packages. It can be removed if any other Go source files are +// added to this package. package {{.Resource.GoImportGroup}} `