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
4 changes: 2 additions & 2 deletions cmd/operator-sdk/cli/cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (
"github.com/operator-framework/operator-sdk/cmd/operator-sdk/generate"
"github.com/operator-framework/operator-sdk/cmd/operator-sdk/new"
"github.com/operator-framework/operator-sdk/cmd/operator-sdk/olm"
"github.com/operator-framework/operator-sdk/cmd/operator-sdk/run"
"github.com/operator-framework/operator-sdk/cmd/operator-sdk/version"
"github.com/operator-framework/operator-sdk/internal/flags"
golangv2 "github.com/operator-framework/operator-sdk/internal/plugins/golang/v2"
Expand All @@ -46,8 +47,7 @@ var commands = []*cobra.Command{
completion.NewCmd(),
generate.NewCmd(),
olm.NewCmd(),
// Add back when implemented for new project layouts.
// run.NewCmd(),
run.NewCmd(),
version.NewCmd(),
}

Expand Down
2 changes: 1 addition & 1 deletion cmd/operator-sdk/cli/legacy.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ func GetCLIRoot() *cobra.Command {
new.NewCmd(),
olm.NewCmd(),
printdeps.NewCmd(),
run.NewCmd(),
run.NewCmdLegacy(),
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

helm/Ansible still requiring run operator-sdk run --local with the new layout.

scorecard.NewCmd(),
test.NewCmd(),
version.NewCmd(),
Expand Down
19 changes: 18 additions & 1 deletion cmd/operator-sdk/run/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,23 @@ import (
hoflags "github.com/operator-framework/operator-sdk/pkg/helm/flags"
)

func NewCmd() *cobra.Command {
cmd := &cobra.Command{
Use: "run",
Short: "Run an Operator in a variety of environments",
Long: `This command has subcommands that will deploy your Operator with OLM.
Currently only the package manifests format is supported via the 'packagemanifests' subcommand.
Run 'operator-sdk run --help' for more information.
`,
}

cmd.AddCommand(
packagemanifests.NewCmd(),
)

return cmd
}

type runCmd struct {
// Common options.
kubeconfig string
Expand All @@ -57,7 +74,7 @@ func (c *runCmd) checkRunType() error {
return nil
}

func NewCmd() *cobra.Command {
func NewCmdLegacy() *cobra.Command {
c := &runCmd{}
cmd := &cobra.Command{
Use: "run",
Expand Down
14 changes: 11 additions & 3 deletions cmd/operator-sdk/run/packagemanifests/packagemanifests.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (

olmcatalog "github.com/operator-framework/operator-sdk/internal/generate/olm-catalog"
olmoperator "github.com/operator-framework/operator-sdk/internal/olm/operator"
kbutil "github.com/operator-framework/operator-sdk/internal/util/kubebuilder"
"github.com/operator-framework/operator-sdk/internal/util/projutil"
)

Expand All @@ -35,16 +36,23 @@ func NewCmd() *cobra.Command {

cmd := &cobra.Command{
Use: "packagemanifests",
Short: "Run an Operator organized in the package manifests format with OLM",
Short: "Deploy an Operator in the package manifests format with OLM",
Long: `'run packagemanifests' deploys an Operator's package manifests with OLM. The command's argument
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

if we have a choice between run packagemanifests and run olm, I'd vote for the latter one.

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.

@tengqm the reason we're thinking of using packagemanifests is to distinguish it from an upcoming run bundle subcommand. The idea is that we want to support various OLM packaging formats for the run subcommand, and if we tried to encapsulate all of that under a single run olm command, the UX would get unwieldy since there are some differences in how they work.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Thanks for the info, @joelanford. I am just not a big fan of long commands, :).

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.

@estroz Perhaps we could add a cobra alias for this (e.g. operator-sdk run pm)?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

SGTM, I'll make a follow-up.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

must be set to a valid package manifests root directory, ex. '<project-root>/packagemanifests'.`,
RunE: func(cmd *cobra.Command, args []string) error {
if len(args) > 0 {
if len(args) > 1 {
return fmt.Errorf("exactly one argument is required")
}
c.ManifestsDir = args[0]
} else {
operatorName := filepath.Base(projutil.MustGetwd())
c.ManifestsDir = filepath.Join(olmcatalog.OLMCatalogDir, operatorName)
// Choose the default path depending on project configuration.
if kbutil.HasProjectFile() {
c.ManifestsDir = "packagemanifests"
} else {
operatorName := filepath.Base(projutil.MustGetwd())
c.ManifestsDir = filepath.Join(olmcatalog.OLMCatalogDir, operatorName)
}
}

log.Infof("Running operator from directory %s", c.ManifestsDir)
Expand Down
10 changes: 10 additions & 0 deletions internal/olm/operator/packagemanifests.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import (
"context"
"errors"
"fmt"
"os"

"github.com/spf13/pflag"
)
Expand Down Expand Up @@ -61,9 +62,18 @@ func (c *PackageManifestsCmd) validate() error {
if c.ManifestsDir == "" {
return errors.New("manifests dir must be set")
}
manDirInfo, err := os.Stat(c.ManifestsDir)
if err != nil {
return err
}
if !manDirInfo.IsDir() {
return fmt.Errorf("%s must be a directory", c.ManifestsDir)
}

if c.OperatorVersion == "" {
return errors.New("operator version must be set")
}

return c.OperatorCmd.validate()
}

Expand Down
45 changes: 35 additions & 10 deletions test/e2e-new/e2e_suite.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,26 +29,29 @@ import (

. "github.com/onsi/ginkgo" //nolint:golint
. "github.com/onsi/gomega" //nolint:golint
kbtestutils "sigs.k8s.io/kubebuilder/test/e2e/utils"

"sigs.k8s.io/kubebuilder/test/e2e/utils"
testutils "github.com/operator-framework/operator-sdk/test/internal"
)

var _ = Describe("operator-sdk", func() {
Context("with the new project layout", func() {
var (
tc *utils.TestContext
projectName string
tc testutils.TestContext
projectName string
operatorVersion = "0.0.1"
)

BeforeEach(func() {

By("creating a new test context")
var err error
tc, err = utils.NewTestContext("operator-sdk", "GO111MODULE=on")
tc, err = testutils.NewTestContext("GO111MODULE=on")
Expect(err).NotTo(HaveOccurred())
Expect(tc.Prepare()).To(Succeed())

projectName = filepath.Base(tc.Dir)

By("installing OLM")
Expect(tc.InstallOLM()).To(Succeed())
})

AfterEach(func() {
Expand All @@ -57,6 +60,9 @@ var _ = Describe("operator-sdk", func() {

By("removing container image and work dir")
tc.Destroy()

By("uninstalling OLM")
tc.UninstallOLM()
})

It("should generate a runnable project", func() {
Expand All @@ -81,7 +87,7 @@ var _ = Describe("operator-sdk", func() {
Expect(err).Should(Succeed())

By("implementing the API")
Expect(utils.InsertCode(
Expect(kbtestutils.InsertCode(
filepath.Join(tc.Dir, "api", tc.Version, fmt.Sprintf("%s_types.go", strings.ToLower(tc.Kind))),
fmt.Sprintf(`type %sSpec struct {
`, tc.Kind),
Expand Down Expand Up @@ -110,7 +116,7 @@ var _ = Describe("operator-sdk", func() {
"-o", "go-template={{ range .items }}{{ if not .metadata.deletionTimestamp }}{{ .metadata.name }}"+
"{{ \"\\n\" }}{{ end }}{{ end }}")
Expect(err).NotTo(HaveOccurred())
podNames := utils.GetNonEmptyLines(podOutput)
podNames := kbtestutils.GetNonEmptyLines(podOutput)
if len(podNames) != 1 {
return fmt.Errorf("expect 1 controller pods running, but got %d", len(podNames))
}
Expand Down Expand Up @@ -147,6 +153,12 @@ var _ = Describe("operator-sdk", func() {
}
Eventually(managerContainerLogs, time.Minute, time.Second).Should(ContainSubstring("Successfully Reconciled"))

By("cleaning up the operator and resources")
defaultOutput, err := tc.KustomizeBuild(filepath.Join("config", "default"))
Expect(err).NotTo(HaveOccurred())
_, err = tc.Kubectl.WithInput(string(defaultOutput)).Command("delete", "-f", "-")
Expect(err).NotTo(HaveOccurred())

By("generating the operator bundle")
// Turn off interactive prompts for all generation tasks.
replace := "operator-sdk generate kustomize manifests"
Expand All @@ -169,12 +181,25 @@ var _ = Describe("operator-sdk", func() {
genKustomizeCmd := exec.Command(tc.BinaryName, "generate", "kustomize", "manifests")
_, err = tc.Run(genKustomizeCmd)
Expect(err).NotTo(HaveOccurred())
kustomizeOutput, err := tc.Run(exec.Command("kustomize", "build", filepath.Join("config", "manifests")))
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(kustomizeOutput)
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())

// TODO: run 'cleanup packagemanifests' when added to the new CLI.
})
})
})
Expand Down
55 changes: 55 additions & 0 deletions test/internal/utils.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
// Copyright 2020 The Operator-SDK Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package internal

import (
"fmt"
"os/exec"

. "github.com/onsi/ginkgo" //nolint:golint

kbtestutils "sigs.k8s.io/kubebuilder/test/e2e/utils"
)

// TestContext wraps kubebuilder's e2e TestContext.
type TestContext struct {
*kbtestutils.TestContext
}

// NewTestContext returns a TestContext containing a new kubebuilder TestContext.
func NewTestContext(env ...string) (tc TestContext, err error) {
tc.TestContext, err = kbtestutils.NewTestContext("operator-sdk", env...)
return tc, err
}

// InstallOLM runs 'operator-sdk olm install' and returns any errors emitted by that command.
func (tc TestContext) InstallOLM() error {
cmd := exec.Command(tc.BinaryName, "olm", "install", "--timeout", "4m")
_, err := tc.Run(cmd)
return err
}

// InstallOLM runs 'operator-sdk olm uninstall' and logs any errors emitted by that command.
func (tc TestContext) UninstallOLM() {
cmd := exec.Command(tc.BinaryName, "olm", "uninstall")
if _, err := tc.Run(cmd); err != nil {
fmt.Fprintln(GinkgoWriter, "warning: error when uninstalling OLM:", err)
}
}

// KustomizeBuild runs 'kustomize build <dir>' and returns its output and an error if any.
func (tc TestContext) KustomizeBuild(dir string) ([]byte, error) {
return tc.Run(exec.Command("kustomize", "build", dir))
}
2 changes: 1 addition & 1 deletion website/content/en/docs/cli/operator-sdk_run.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,5 +27,5 @@ operator-sdk run [flags]

* [operator-sdk](../operator-sdk) - An SDK for building operators with ease
* [operator-sdk run local](../operator-sdk_run_local) - Run an Operator locally
* [operator-sdk run packagemanifests](../operator-sdk_run_packagemanifests) - Run an Operator organized in the package manifests format with OLM
* [operator-sdk run packagemanifests](../operator-sdk_run_packagemanifests) - Deploy an Operator in the package manifests format with OLM

Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,12 @@ title: "operator-sdk run packagemanifests"
---
## operator-sdk run packagemanifests

Run an Operator organized in the package manifests format with OLM
Deploy an Operator in the package manifests format with OLM

### Synopsis

Run an Operator organized in the package manifests format with OLM
'run packagemanifests' deploys an Operator's package manifests with OLM. The command's argument
must be set to a valid package manifests root directory, ex. '<project-root>/packagemanifests'.

```
operator-sdk run packagemanifests [flags]
Expand Down
1 change: 1 addition & 0 deletions website/content/en/docs/new-cli/operator-sdk.md
Original file line number Diff line number Diff line change
Expand Up @@ -75,5 +75,6 @@ operator-sdk [flags]
* [operator-sdk init](../operator-sdk_init) - Initialize a new project
* [operator-sdk new](../operator-sdk_new) - Creates a new operator application
* [operator-sdk olm](../operator-sdk_olm) - Manage the Operator Lifecycle Manager installation in your cluster
* [operator-sdk run](../operator-sdk_run) - Run an Operator in a variety of environments
* [operator-sdk version](../operator-sdk_version) - Prints the version of operator-sdk

31 changes: 31 additions & 0 deletions website/content/en/docs/new-cli/operator-sdk_run.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
---
title: "operator-sdk run"
---
## operator-sdk run

Run an Operator in a variety of environments

### Synopsis

This command has subcommands that will deploy your Operator with OLM.
Currently only the package manifests format is supported via the 'packagemanifests' subcommand.
Run 'operator-sdk run --help' for more information.


### Options

```
-h, --help help for run
```

### Options inherited from parent commands

```
--verbose Enable verbose logging
```

### SEE ALSO

* [operator-sdk](../operator-sdk) - Development kit for building Kubernetes extensions and tools.
* [operator-sdk run packagemanifests](../operator-sdk_run_packagemanifests) - Deploy an Operator in the package manifests format with OLM

Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
---
title: "operator-sdk run packagemanifests"
---
## operator-sdk run packagemanifests

Deploy an Operator in the package manifests format with OLM

### Synopsis

'run packagemanifests' deploys an Operator's package manifests with OLM. The command's argument
must be set to a valid package manifests root directory, ex. '<project-root>/packagemanifests'.

```
operator-sdk run packagemanifests [flags]
```

### Options

```
-h, --help help for packagemanifests
--include strings Path to Kubernetes resource manifests, ex. Role, Subscription. These supplement or override defaults generated by run/cleanup
--install-mode string InstallMode to create OperatorGroup with. Format: InstallModeType[=ns1,ns2[, ...]]
--kubeconfig string The file path to kubernetes configuration file. Defaults to location specified by $KUBECONFIG, or to default file rules if not set
--olm-namespace string The namespace where OLM is installed (default "olm")
--operator-namespace string The namespace where operator resources are created. It must already exist in the cluster or be defined in a manifest passed to --include
--operator-version string Version of operator to deploy
--timeout duration Time to wait for the command to complete before failing (default 2m0s)
```

### Options inherited from parent commands

```
--verbose Enable verbose logging
```

### SEE ALSO

* [operator-sdk run](../operator-sdk_run) - Run an Operator in a variety of environments