Skip to content

Commit a3458ab

Browse files
camilamacedo86jmrodri
authored andcommitted
Allow some commands to generate k8s, without requiring build files. (#1966)
* Allow some commands to generate k8s, without requiring build files. * update changelog * adpt sub-commands * review changes * Update cmd/operator-sdk/main.go
1 parent fd528e8 commit a3458ab

File tree

4 files changed

+27
-16
lines changed

4 files changed

+27
-16
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
- Upgrade base image for Go, Helm, and scorecard proxy from `registry.access.redhat.com/ubi7/ubi-minimal:latest` to `registry.access.redhat.com/ubi8/ubi-minimal:latest`. ([#1952](https://github.com/operator-framework/operator-sdk/pull/1952))
1919
- Upgrade base image for Ansible from `registry.access.redhat.com/ubi7/ubi:latest` to `registry.access.redhat.com/ubi8/ubi:latest`. ([#1990](https://github.com/operator-framework/operator-sdk/pull/1990) and [#2004](https://github.com/operator-framework/operator-sdk/pull/2004))
2020
- Updated kube-state-metrics dependency from `v1.6.0` to `v1.7.2`. ([#1943](https://github.com/operator-framework/operator-sdk/pull/1943))
21+
- Change the structure used by sub-commands to check the type and the root dir of the projects for no longer relay in the `Dockerfile` and `main.go` files. ([#1966](https://github.com/operator-framework/operator-sdk/pull/1966))
2122

2223
### Breaking changes
2324

cmd/operator-sdk/main.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,8 @@ func main() {
5757
log.Debug("Debug logging is set")
5858
}
5959
if err := checkDepManagerForCmd(cmd); err != nil {
60-
log.Fatal(err)
60+
// Allow cases where subcommands do not require deps files
61+
log.Warnf("Operator type %s and the %s. Please, review the structure of your project.", projutil.GetOperatorType(), err)
6162
}
6263
},
6364
}

cmd/operator-sdk/printdeps/cmd.go

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,12 @@ package printdeps
1616

1717
import (
1818
"fmt"
19-
2019
"github.com/operator-framework/operator-sdk/internal/pkg/scaffold"
2120
"github.com/operator-framework/operator-sdk/internal/pkg/scaffold/ansible"
2221
"github.com/operator-framework/operator-sdk/internal/pkg/scaffold/helm"
2322
"github.com/operator-framework/operator-sdk/internal/util/projutil"
24-
2523
"github.com/spf13/cobra"
24+
log "github.com/sirupsen/logrus"
2625
)
2726

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

52+
// Allow the cases of some sub-commands might not require check the deps files.
53+
if projutil.GetOperatorType() == projutil.OperatorTypeUnknown {
54+
log.Fatal(fmt.Errorf("unknown project type, we were unable to print the deps"))
55+
}
56+
5357
if err := printDeps(depManager); err != nil {
5458
return fmt.Errorf("print deps failed: %v", err)
5559
}
5660
return nil
5761
}
5862

5963
func printDeps(depManager string) (err error) {
64+
6065
// Use depManager if set. Fall back to the project's current dep manager
6166
// type if unset.
6267
mt := projutil.DepManagerType(depManager)

internal/util/projutil/project_util.go

Lines changed: 17 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -35,13 +35,13 @@ const (
3535
GoModEnv = "GO111MODULE"
3636
SrcDir = "src"
3737

38-
fsep = string(filepath.Separator)
39-
mainFile = "cmd" + fsep + "manager" + fsep + "main.go"
40-
buildDockerfile = "build" + fsep + "Dockerfile"
41-
rolesDir = "roles"
42-
helmChartsDir = "helm-charts"
43-
goModFile = "go.mod"
44-
gopkgTOMLFile = "Gopkg.toml"
38+
fsep = string(filepath.Separator)
39+
buildDir = "build" + fsep
40+
rolesDir = "roles"
41+
pkgApiDir = "pkg" + fsep + "apis" + fsep
42+
helmChartsDir = "helm-charts"
43+
goModFile = "go.mod"
44+
gopkgTOMLFile = "Gopkg.toml"
4545
)
4646

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

135135
func MustGetwd() string {
@@ -215,8 +215,12 @@ func GetOperatorType() OperatorType {
215215
}
216216

217217
func IsOperatorGo() bool {
218-
_, err := os.Stat(mainFile)
219-
return err == nil
218+
_, errGoMod := os.Stat(goModFile)
219+
_, errGopkgTOMLFile := os.Stat(gopkgTOMLFile)
220+
_, errPkgApiDir := os.Stat(pkgApiDir)
221+
222+
// check if has go modules or dep files or pkg/api
223+
return errGoMod == nil || errGopkgTOMLFile == nil || errPkgApiDir == nil
220224
}
221225

222226
func IsOperatorAnsible() bool {

0 commit comments

Comments
 (0)