Skip to content
Merged
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: 1 addition & 1 deletion cmd/operator-sdk/generate/bundle/bundle.go
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,7 @@ func (c bundleCmd) runManifests(cfg *config.Config) (err error) {

csvGen := gencsv.Generator{
OperatorName: c.operatorName,
OperatorType: genutil.PluginKeyToOperatorType(cfg.Layout),
OperatorType: projutil.PluginKeyToOperatorType(cfg.Layout),
Version: c.version,
Collector: col,
}
Expand Down
15 changes: 0 additions & 15 deletions cmd/operator-sdk/generate/internal/genutil.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,11 @@ import (
"io"
"os"
"path/filepath"
"strings"

"github.com/blang/semver"
apiextv1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1"
apiextv1beta1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1beta1"
"sigs.k8s.io/yaml"

"github.com/operator-framework/operator-sdk/internal/util/projutil"
)

// ValidateVersion returns an error if version is not a strict semantic version.
Expand All @@ -55,18 +52,6 @@ func IsPipeReader() bool {
return info.Mode()&os.ModeNamedPipe != 0
}

// PluginKeyToOperatorType converts a plugin key string to an operator project
// type.
// TODO(estroz): this can probably be made more robust by checking known
// plugin keys directly.
func PluginKeyToOperatorType(pluginKey string) projutil.OperatorType {
switch {
case strings.HasPrefix(pluginKey, "go"):
return projutil.OperatorTypeGo
}
return ""
}

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

move the implementation to the project util since is required/useful to check the project type.

// WriteObjects writes each object in objs to w.
func WriteObjects(w io.Writer, objs ...interface{}) error {
for _, obj := range objs {
Expand Down
3 changes: 1 addition & 2 deletions cmd/operator-sdk/generate/kustomize/manifests.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ import (
"github.com/spf13/pflag"
"sigs.k8s.io/kubebuilder/pkg/model/config"

genutil "github.com/operator-framework/operator-sdk/cmd/operator-sdk/generate/internal"
gencsv "github.com/operator-framework/operator-sdk/internal/generate/clusterserviceversion"
"github.com/operator-framework/operator-sdk/internal/scaffold/kustomize"
kbutil "github.com/operator-framework/operator-sdk/internal/util/kubebuilder"
Expand Down Expand Up @@ -165,7 +164,7 @@ func (c manifestsCmd) run(cfg *config.Config) error {

csvGen := gencsv.Generator{
OperatorName: c.operatorName,
OperatorType: genutil.PluginKeyToOperatorType(cfg.Layout),
OperatorType: projutil.PluginKeyToOperatorType(cfg.Layout),
}
opts := []gencsv.Option{
gencsv.WithBase(c.inputDir, c.apisDir, c.interactiveLevel),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@ func (c packagemanifestsCmd) run(cfg *config.Config) error {

csvGen := gencsv.Generator{
OperatorName: c.operatorName,
OperatorType: genutil.PluginKeyToOperatorType(cfg.Layout),
OperatorType: projutil.PluginKeyToOperatorType(cfg.Layout),
Version: c.version,
Collector: col,
}
Expand Down
49 changes: 46 additions & 3 deletions internal/util/projutil/project_util.go
Original file line number Diff line number Diff line change
Expand Up @@ -207,11 +207,32 @@ func GetOperatorType() OperatorType {
return OperatorTypeUnknown
}

// PluginKeyToOperatorType converts a plugin key string to an operator project
// type.
// TODO(estroz): this can probably be made more robust by checking known
Comment thread
asmacdo marked this conversation as resolved.
// plugin keys directly.
func PluginKeyToOperatorType(pluginKey string) OperatorType {
switch {
case strings.HasPrefix(pluginKey, "go"):
return OperatorTypeGo
case strings.HasPrefix(pluginKey, "helm"):
return OperatorTypeHelm
case strings.HasPrefix(pluginKey, "ansible"):
return OperatorTypeAnsible
}
return OperatorTypeUnknown
}

// IsOperatorGo returns true when the layout field in PROJECT file has the Go prefix key.
// NOTE: For the legacy, returns true when the project contains the cmd/manager directory and main.go file.
func IsOperatorGo() bool {
// todo: in the future we should check the plugin prefix to ensure the operator type
// for now, we can assume that any project with the kubebuilder layout is Go Type
// If the project has the new layout we will check the type in the config file
if kbutil.HasProjectFile() {
return true
cfg, err := kbutil.ReadConfig()
if err != nil {
log.Fatalf("Error reading config: %v", err)
}
return cfg.IsV2() || PluginKeyToOperatorType(cfg.Layout) == OperatorTypeGo
}

// todo: remove the following code when the legacy layout is no longer supported
Expand All @@ -225,7 +246,18 @@ func IsOperatorGo() bool {
return err == nil || os.IsExist(err)
}

// IsOperatorAnsible returns true when the layout field in PROJECT file has the Ansible prefix key.
// NOTE: For the legacy, returns true when the project contains the roles and the molecule directory.
func IsOperatorAnsible() bool {
Comment thread
camilamacedo86 marked this conversation as resolved.
// If the project is in the new layout, check the config file's plugin type.
if kbutil.HasProjectFile() {
Comment thread
camilamacedo86 marked this conversation as resolved.
cfg, err := kbutil.ReadConfig()
if err != nil {
log.Fatalf("Error reading config: %v", err)
}
return PluginKeyToOperatorType(cfg.Layout) == OperatorTypeAnsible
}
// todo(camilamacedo86): remove when the legacy layout is no longer supported
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Will the roles dir no longer be in the new layout?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The roles dir will be in the new layout, however, see:

  • Follow up the same standard for all types shows very beneficial and increase the manutence ability
  • We can init an Ansible/Helm project without scaffold an API which means that the roles dir would NOT exist.

stat, err := os.Stat(rolesDir)
if (err == nil && stat.IsDir()) || os.IsExist(err) {
return true
Expand All @@ -238,7 +270,18 @@ func IsOperatorAnsible() bool {
return err == nil || os.IsExist(err)
}

// IsOperatorHelm returns true when the layout field in PROJECT file has the Helm prefix key.
// NOTE: For the legacy, returns true when the project contains the helm-charts directory.
func IsOperatorHelm() bool {
// If the project has the new layout we will check the type in the config file
if kbutil.HasProjectFile() {
cfg, err := kbutil.ReadConfig()
if err != nil {
log.Fatalf("Error reading config: %v", err)
}
return PluginKeyToOperatorType(cfg.Layout) == OperatorTypeHelm
}
// todo(camilamacedo86): remove when the legacy layout is no longer supported
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Will the charts dir no longer be in the new layout?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ditto above. We can scaffold a project without any api.

stat, err := os.Stat(helmChartsDir)
return (err == nil && stat.IsDir()) || os.IsExist(err)
}
Expand Down