From f8dee2d0c37282704e74b44bfc55a4740dd7aa5f Mon Sep 17 00:00:00 2001 From: Camila Macedo Date: Wed, 1 Jul 2020 21:53:19 +0100 Subject: [PATCH 1/2] feat: allow know the project type by the config file --- cmd/operator-sdk/generate/bundle/bundle.go | 2 +- cmd/operator-sdk/generate/internal/genutil.go | 15 ------ .../generate/kustomize/manifests.go | 3 +- .../packagemanifests/packagemanifests.go | 2 +- internal/util/projutil/project_util.go | 49 +++++++++++++++++-- 5 files changed, 49 insertions(+), 22 deletions(-) diff --git a/cmd/operator-sdk/generate/bundle/bundle.go b/cmd/operator-sdk/generate/bundle/bundle.go index 2227b44f1a..4c3da416dc 100644 --- a/cmd/operator-sdk/generate/bundle/bundle.go +++ b/cmd/operator-sdk/generate/bundle/bundle.go @@ -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, } diff --git a/cmd/operator-sdk/generate/internal/genutil.go b/cmd/operator-sdk/generate/internal/genutil.go index 9657addf8a..a1131806b0 100644 --- a/cmd/operator-sdk/generate/internal/genutil.go +++ b/cmd/operator-sdk/generate/internal/genutil.go @@ -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. @@ -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 "" -} - // WriteObjects writes each object in objs to w. func WriteObjects(w io.Writer, objs ...interface{}) error { for _, obj := range objs { diff --git a/cmd/operator-sdk/generate/kustomize/manifests.go b/cmd/operator-sdk/generate/kustomize/manifests.go index 5fdd94fc82..dd60d01b0e 100644 --- a/cmd/operator-sdk/generate/kustomize/manifests.go +++ b/cmd/operator-sdk/generate/kustomize/manifests.go @@ -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" @@ -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), diff --git a/cmd/operator-sdk/generate/packagemanifests/packagemanifests.go b/cmd/operator-sdk/generate/packagemanifests/packagemanifests.go index af9032841b..76802b4c7f 100644 --- a/cmd/operator-sdk/generate/packagemanifests/packagemanifests.go +++ b/cmd/operator-sdk/generate/packagemanifests/packagemanifests.go @@ -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, } diff --git a/internal/util/projutil/project_util.go b/internal/util/projutil/project_util.go index 418320e3ae..dead5cc383 100644 --- a/internal/util/projutil/project_util.go +++ b/internal/util/projutil/project_util.go @@ -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 +// 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 @@ -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 { + // + if kbutil.HasProjectFile() { + 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 stat, err := os.Stat(rolesDir) if (err == nil && stat.IsDir()) || os.IsExist(err) { return true @@ -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 stat, err := os.Stat(helmChartsDir) return (err == nil && stat.IsDir()) || os.IsExist(err) } From 93321f8e6e765ef2265458a3bc48c06a6c924ed3 Mon Sep 17 00:00:00 2001 From: Camila Macedo Date: Fri, 10 Jul 2020 18:09:07 +0100 Subject: [PATCH 2/2] Update internal/util/projutil/project_util.go Co-authored-by: Eric Stroczynski --- internal/util/projutil/project_util.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/internal/util/projutil/project_util.go b/internal/util/projutil/project_util.go index dead5cc383..7fae5a3831 100644 --- a/internal/util/projutil/project_util.go +++ b/internal/util/projutil/project_util.go @@ -249,7 +249,7 @@ func IsOperatorGo() bool { // 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 { - // + // If the project is in the new layout, check the config file's plugin type. if kbutil.HasProjectFile() { cfg, err := kbutil.ReadConfig() if err != nil {