Skip to content

Be able to execute the commands operator-sdk generate k8s and openapi… #2054

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

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all 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
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

### Changed

- Change sub-commands for no longer relay in the `Dockerfile` and `main.go` files. ([#2054](https://github.com/operator-framework/operator-sdk/pull/2054))

### Deprecated

### Removed
Expand Down
3 changes: 2 additions & 1 deletion cmd/operator-sdk/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,8 @@ func main() {
log.Debug("Debug logging is set")
}
if err := checkDepManagerForCmd(cmd); err != nil {
log.Fatal(err)
// Allow cases where subcommands do not require deps files
log.Warnf("Operator type %s and the %s. Please, review the structure of your project.", projutil.GetOperatorType(), err)
}
},
}
Expand Down
9 changes: 7 additions & 2 deletions cmd/operator-sdk/printdeps/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,12 @@ package printdeps

import (
"fmt"

"github.com/operator-framework/operator-sdk/internal/pkg/scaffold"
"github.com/operator-framework/operator-sdk/internal/pkg/scaffold/ansible"
"github.com/operator-framework/operator-sdk/internal/pkg/scaffold/helm"
"github.com/operator-framework/operator-sdk/internal/util/projutil"

"github.com/spf13/cobra"
log "github.com/sirupsen/logrus"
)

var depManager string
Expand Down Expand Up @@ -50,13 +49,19 @@ func printDepsFunc(cmd *cobra.Command, args []string) error {
}
projutil.MustInProjectRoot()

// Allow the cases of some sub-commands might not require check the deps files.
if projutil.GetOperatorType() == projutil.OperatorTypeUnknown {
log.Fatal(fmt.Errorf("unknown project type, we were unable to print the deps"))
}

if err := printDeps(depManager); err != nil {
return fmt.Errorf("print deps failed: %v", err)
}
return nil
}

func printDeps(depManager string) (err error) {

// Use depManager if set. Fall back to the project's current dep manager
// type if unset.
mt := projutil.DepManagerType(depManager)
Expand Down
30 changes: 17 additions & 13 deletions internal/util/projutil/project_util.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,13 +35,13 @@ const (
GoModEnv = "GO111MODULE"
SrcDir = "src"

fsep = string(filepath.Separator)
mainFile = "cmd" + fsep + "manager" + fsep + "main.go"
buildDockerfile = "build" + fsep + "Dockerfile"
rolesDir = "roles"
helmChartsDir = "helm-charts"
goModFile = "go.mod"
gopkgTOMLFile = "Gopkg.toml"
fsep = string(filepath.Separator)
buildDir = "build" + fsep
rolesDir = "roles"
pkgApiDir = "pkg" + fsep + "apis" + fsep
helmChartsDir = "helm-charts"
goModFile = "go.mod"
gopkgTOMLFile = "Gopkg.toml"
)

// OperatorType - the type of operator
Expand Down Expand Up @@ -114,11 +114,11 @@ func MustInProjectRoot() {
// CheckProjectRoot checks if the current dir is the project root, and returns
// an error if not.
func CheckProjectRoot() error {
// If the current directory has a "build/Dockerfile", then it is safe to say
// If the current directory has a "build/", then it is safe to say
// we are at the project root.
if _, err := os.Stat(buildDockerfile); err != nil {
if _, err := os.Stat(buildDir); err != nil {
if os.IsNotExist(err) {
return fmt.Errorf("must run command in project root dir: project structure requires %s", buildDockerfile)
return fmt.Errorf("must run command in project root dir: project structure requires %s", buildDir)
}
return errors.Wrap(err, "error while checking if current directory is the project root")
}
Expand All @@ -129,7 +129,7 @@ func CheckGoProjectCmd(cmd *cobra.Command) error {
if IsOperatorGo() {
return nil
}
return fmt.Errorf("'%s' can only be run for Go operators; %s does not exist", cmd.CommandPath(), mainFile)
return fmt.Errorf("'%s' can only be run for Go operators; %s, %s or %s does not exist", cmd.CommandPath(), goModFile, gopkgTOMLFile, pkgApiDir)
}

func MustGetwd() string {
Expand Down Expand Up @@ -215,8 +215,12 @@ func GetOperatorType() OperatorType {
}

func IsOperatorGo() bool {
_, err := os.Stat(mainFile)
return err == nil
_, errGoMod := os.Stat(goModFile)
_, errGopkgTOMLFile := os.Stat(gopkgTOMLFile)
_, errPkgApiDir := os.Stat(pkgApiDir)

// check if has go modules or dep files or pkg/api
return errGoMod == nil || errGopkgTOMLFile == nil || errPkgApiDir == nil
}

func IsOperatorAnsible() bool {
Expand Down