Skip to content

Commit

Permalink
fix: OLM commands for Helm/Ansible in the new layout
Browse files Browse the repository at this point in the history
  • Loading branch information
camilamacedo86 committed Jul 3, 2020
1 parent 4c409fb commit d210134
Show file tree
Hide file tree
Showing 7 changed files with 89 additions and 48 deletions.
11 changes: 9 additions & 2 deletions cmd/operator-sdk/generate/bundle/bundle.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ import (
"path/filepath"
"strings"

kbutil "github.com/operator-framework/operator-sdk/internal/util/kubebuilder"

"github.com/operator-framework/operator-registry/pkg/lib/bundle"
"sigs.k8s.io/kubebuilder/pkg/model/config"

Expand Down Expand Up @@ -99,15 +101,20 @@ https://github.com/operator-framework/operator-registry/#manifest-format
const defaultRootDir = "bundle"

// setCommonDefaults sets defaults useful to all modes of this subcommand.
func (c *bundleCmd) setCommonDefaults(cfg *config.Config) {
func (c *bundleCmd) setCommonDefaults(cfg *config.Config) error {
if c.operatorName == "" {
c.operatorName = filepath.Base(cfg.Repo)
name, err := kbutil.GetOperatorName(cfg)
if err != nil {
return err
}
c.operatorName = name
}
// A default channel can be inferred if there is only one channel. Don't infer
// default otherwise; the user must set this value.
if c.defaultChannel == "" && strings.Count(c.channels, ",") == 0 {
c.defaultChannel = c.channels
}
return nil
}

// validateManifests validates c for bundle manifests generation.
Expand Down
5 changes: 4 additions & 1 deletion cmd/operator-sdk/generate/bundle/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,10 @@ func NewCmd() *cobra.Command {
if err != nil {
return fmt.Errorf("error reading configuration: %v", err)
}
c.setCommonDefaults(cfg)

if err := c.setCommonDefaults(cfg); err != nil {
return err
}

// Validate command args before running so a preceding mode doesn't run
// before a following validation fails.
Expand Down
14 changes: 10 additions & 4 deletions cmd/operator-sdk/generate/kustomize/manifests.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,8 +99,9 @@ func newManifestsCmd() *cobra.Command {
if err != nil {
return fmt.Errorf("error reading configuration: %v", err)
}
c.setDefaults(cfg)

if err := c.setDefaults(cfg); err != nil {
return err
}
// Run command logic.
if err = c.run(cfg); err != nil {
log.Fatalf("Error generating kustomize files: %v", err)
Expand Down Expand Up @@ -129,9 +130,13 @@ func (c *manifestsCmd) addFlagsTo(fs *pflag.FlagSet) {
var defaultDir = filepath.Join("config", "manifests")

// setDefaults sets command defaults.
func (c *manifestsCmd) setDefaults(cfg *config.Config) {
func (c *manifestsCmd) setDefaults(cfg *config.Config) error {
if c.operatorName == "" {
c.operatorName = filepath.Base(cfg.Repo)
name, err := kbutil.GetOperatorName(cfg)
if err != nil {
return err
}
c.operatorName = name
}

if c.inputDir == "" {
Expand All @@ -147,6 +152,7 @@ func (c *manifestsCmd) setDefaults(cfg *config.Config) {
c.apisDir = "api"
}
}
return nil
}

// kustomization.yaml file contents for manifests. this should always be written to
Expand Down
5 changes: 3 additions & 2 deletions cmd/operator-sdk/generate/packagemanifests/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,8 +73,9 @@ func NewCmd() *cobra.Command {
if err != nil {
log.Fatal(fmt.Errorf("error reading configuration: %v", err))
}
c.setDefaults(cfg)

if err := c.setDefaults(cfg); err != nil {
return err
}
if err = c.validate(); err != nil {
return fmt.Errorf("invalid command options: %v", err)
}
Expand Down
11 changes: 9 additions & 2 deletions cmd/operator-sdk/generate/packagemanifests/packagemanifests.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ import (
"os"
"path/filepath"

kbutil "github.com/operator-framework/operator-sdk/internal/util/kubebuilder"

"sigs.k8s.io/kubebuilder/pkg/model/config"

genutil "github.com/operator-framework/operator-sdk/cmd/operator-sdk/generate/internal"
Expand Down Expand Up @@ -80,9 +82,13 @@ https://github.com/operator-framework/operator-registry/#manifest-format
const defaultRootDir = "packagemanifests"

// setDefaults sets command defaults.
func (c *packagemanifestsCmd) setDefaults(cfg *config.Config) {
func (c *packagemanifestsCmd) setDefaults(cfg *config.Config) error {
if c.operatorName == "" {
c.operatorName = filepath.Base(cfg.Repo)
name, err := kbutil.GetOperatorName(cfg)
if err != nil {
return err
}
c.operatorName = name
}

if c.inputDir == "" {
Expand All @@ -93,6 +99,7 @@ func (c *packagemanifestsCmd) setDefaults(cfg *config.Config) {
c.outputDir = defaultRootDir
}
}
return nil
}

// validate validates c for package manifests generation.
Expand Down
17 changes: 17 additions & 0 deletions internal/util/kubebuilder/project.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,11 @@
package kbutil

import (
"fmt"
"io/ioutil"
"os"
"path/filepath"
"strings"

log "github.com/sirupsen/logrus"
"sigs.k8s.io/kubebuilder/pkg/model/config"
Expand Down Expand Up @@ -50,3 +53,17 @@ func ReadConfig() (*config.Config, error) {
}
return c, nil
}

// GetOperatorName will looking for the value in the config.repo and return the project dir if it do not exist
// Note that Ansible/Helm operators in the new layout do not have the repo information.
func GetOperatorName(cfg *config.Config) (string, error) {
if len(strings.TrimSpace(cfg.Repo)) < 1 {
// Check if the project name is a valid namespace according to k8s
dir, err := os.Getwd()
if err != nil {
return "", fmt.Errorf("error to get the current path: %v", err)
}
return filepath.Base(dir), nil
}
return filepath.Base(cfg.Repo), nil
}
74 changes: 37 additions & 37 deletions test/e2e-helm-new/e2e_suite.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,10 @@
package e2e

import (
"bytes"
"fmt"
"os/exec"
"path"
"path/filepath"
"strings"
"time"
Expand All @@ -33,8 +36,8 @@ import (
var _ = Describe("operator-sdk", func() {
Context("with the new project layout", func() {
var (
tc testutils.TestContext
//operatorVersion = "0.0.1"
tc testutils.TestContext
operatorVersion = "0.0.1"
)

BeforeEach(func() {
Expand Down Expand Up @@ -155,41 +158,38 @@ var _ = Describe("operator-sdk", func() {
replace := "operator-sdk generate kustomize manifests"
testutils.ReplaceInFile(filepath.Join(tc.Dir, "Makefile"), replace, replace+" --interactive=false")

// todo: we need to fix the bundle gen for Ansible/Helm in a follow up
// err = tc.Make("bundle")
// Expect(err).NotTo(HaveOccurred())
//By("building the operator bundle image")
//// Use the existing image tag but with a "-bundle" suffix.
//imageSplit := strings.SplitN(tc.ImageName, ":", 2)
//bundleImage := path.Join("quay.io", imageSplit[0]+"-bundle")
//if len(imageSplit) == 2 {
// bundleImage += ":" + imageSplit[1]
//}
//err = tc.Make("bundle-build", "BUNDLE_IMG="+bundleImage)
//Expect(err).NotTo(HaveOccurred())
//
//By("generating the operator package manifests")
//Expect(tc.Make("manifests")).Should(Succeed())
//genKustomizeCmd := exec.Command(tc.BinaryName, "generate", "kustomize", "manifests")
//_, err = tc.Run(genKustomizeCmd)
//Expect(err).NotTo(HaveOccurred())
//manifestsOutput, err := tc.KustomizeBuild(filepath.Join("config", "manifests"))
//Expect(err).NotTo(HaveOccurred())
//genPkgManCmd := exec.Command(tc.BinaryName, "generate", "packagemanifests", "--version", "0.0.1")
//tc.Stdin = bytes.NewBuffer(manifestsOutput)
//_, err = tc.Run(genPkgManCmd)
//Expect(err).NotTo(HaveOccurred())
//
//By("running the package manifests-formatted operator")
//_, err = tc.Kubectl.Command("create", "namespace", tc.Kubectl.Namespace)
//Expect(err).NotTo(HaveOccurred())
//runPkgManCmd := exec.Command(tc.BinaryName, "run", "packagemanifests",
// "--install-mode", "AllNamespaces",
// "--operator-namespace", tc.Kubectl.Namespace,
// "--operator-version", operatorVersion,
// "--timeout", "4m")
//_, err = tc.Run(runPkgManCmd)
//Expect(err).NotTo(HaveOccurred())
err = tc.Make("bundle")
Expect(err).NotTo(HaveOccurred())
By("building the operator bundle image")
// Use the existing image tag but with a "-bundle" suffix.
imageSplit := strings.SplitN(tc.ImageName, ":", 2)
bundleImage := path.Join("quay.io", imageSplit[0]+"-bundle")
if len(imageSplit) == 2 {
bundleImage += ":" + imageSplit[1]
}
err = tc.Make("bundle-build", "BUNDLE_IMG="+bundleImage)
Expect(err).NotTo(HaveOccurred())

By("generating the operator package manifests")
genKustomizeCmd := exec.Command(tc.BinaryName, "generate", "kustomize", "manifests")
_, err = tc.Run(genKustomizeCmd)
Expect(err).NotTo(HaveOccurred())
manifestsOutput, err := tc.KustomizeBuild(filepath.Join("config", "manifests"))
Expect(err).NotTo(HaveOccurred())
genPkgManCmd := exec.Command(tc.BinaryName, "generate", "packagemanifests",
"--version", operatorVersion)
tc.Stdin = bytes.NewBuffer(manifestsOutput)
_, err = tc.Run(genPkgManCmd)
Expect(err).NotTo(HaveOccurred())

By("running the package manifests-formatted operator")
runPkgManCmd := exec.Command(tc.BinaryName, "run", "packagemanifests",
"--install-mode", "AllNamespaces",
"--operator-namespace", tc.Kubectl.Namespace,
"--operator-version", operatorVersion,
"--timeout", "4m")
_, err = tc.Run(runPkgManCmd)
Expect(err).NotTo(HaveOccurred())

// TODO: run 'cleanup packagemanifests' when added to the new CLI.
})
Expand Down

0 comments on commit d210134

Please sign in to comment.