Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

internal/pkg/scaffold: add group.go scaffold to prevent deepcopy parse errors #1401

Merged
merged 6 commits into from
Jun 14, 2019
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 35 additions & 1 deletion cmd/operator-sdk/add/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
Expand Down Expand Up @@ -102,8 +106,15 @@ 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.
estroz marked this conversation as resolved.
Show resolved Hide resolved
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.Types{Resource: r},
&scaffold.AddToScheme{Resource: r},
Expand Down Expand Up @@ -134,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)
}
42 changes: 42 additions & 0 deletions internal/pkg/scaffold/stub.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
// 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
}

var _ input.File = &Stub{}

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}}
`