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
14 changes: 14 additions & 0 deletions changelog/fragments/run-local.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
entries:
- description: >
Add 'run local' subcommand, which has the same functionality of the
deprecated 'run --local' mode.

kind: addition

breaking: false
- description: >
Deprecate 'run --local' mode. Use 'run local' instead.

kind: deprecation

breaking: false
2 changes: 1 addition & 1 deletion cmd/operator-sdk/execentrypoint/ansible.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ func newRunAnsibleCmd() *cobra.Command {
Short: "Runs as an ansible operator",
Long: `Runs as an ansible operator. This is intended to be used when running
in a Pod inside a cluster. Developers wanting to run their operator locally
should use "run --local" instead.`,
should use 'run local' instead.`,
RunE: func(cmd *cobra.Command, args []string) error {
logf.SetLogger(zap.Logger())
if err := setAnsibleEnvVars(flags); err != nil {
Expand Down
2 changes: 1 addition & 1 deletion cmd/operator-sdk/execentrypoint/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ func NewCmd() *cobra.Command {
Short: "Runs a generic operator",
Long: `Runs a generic operator. This is intended to be used when running
in a Pod inside a cluster. Developers wanting to run their operator locally
should use "run --local" instead.`,
should use 'run local' instead.`,
Hidden: true,
}

Expand Down
2 changes: 1 addition & 1 deletion cmd/operator-sdk/execentrypoint/helm.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ func newRunHelmCmd() *cobra.Command {
Short: "Runs as a helm operator",
Long: `Runs as a helm operator. This is intended to be used when running
in a Pod inside a cluster. Developers wanting to run their operator locally
should use "run --local" instead.`,
should use 'run local' instead.`,
RunE: func(cmd *cobra.Command, args []string) error {
logf.SetLogger(zap.Logger())

Expand Down
60 changes: 36 additions & 24 deletions cmd/operator-sdk/run/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ import (
"github.com/spf13/cobra"
"github.com/spf13/pflag"

"github.com/operator-framework/operator-sdk/cmd/operator-sdk/run/local"
"github.com/operator-framework/operator-sdk/cmd/operator-sdk/run/packagemanifests"
olmcatalog "github.com/operator-framework/operator-sdk/internal/generate/olm-catalog"
olmoperator "github.com/operator-framework/operator-sdk/internal/olm/operator"
k8sinternal "github.com/operator-framework/operator-sdk/internal/util/k8sutil"
Expand All @@ -44,7 +46,7 @@ type runCmd struct {

// Run type-specific options.
olmArgs olmoperator.PackageManifestsCmd
localArgs runLocalArgs
localArgs local.RunLocalCmd
}

// checkRunType ensures exactly one run type has been selected.
Expand All @@ -60,13 +62,10 @@ func NewCmd() *cobra.Command {
cmd := &cobra.Command{
Use: "run",
Short: "Run an Operator in a variety of environments",
Long: `This command will run or deploy your Operator in two different modes: locally
and using OLM. These modes are controlled by setting --local and --olm run mode
flags. Each run mode has a separate set of flags that configure 'run' for that
mode. Run 'operator-sdk run --help' for more information on these flags.

Read more about the --olm run mode and configuration options here:
https://sdk.operatorframework.io/docs/olm-integration/cli-overview
Long: `This command has subcommands that will run or deploy your Operator in two
different modes: locally and using OLM. These modes are controlled by using 'local'
or 'packagemanifests' subcommands. Run 'operator-sdk run --help' for more
information on these subcommands.
`,
RunE: func(cmd *cobra.Command, args []string) error {
if err := c.checkRunType(); err != nil {
Expand Down Expand Up @@ -114,25 +113,28 @@ https://sdk.operatorframework.io/docs/olm-integration/cli-overview
if err != nil {
return fmt.Errorf("error getting kubeconfig and default namespace: %v", err)
}
c.localArgs.watchNamespace = defaultNamespace
c.localArgs.WatchNamespace = defaultNamespace
}
}

c.localArgs.kubeconfig = c.kubeconfig
if err := c.localArgs.run(); err != nil {
c.localArgs.Kubeconfig = c.kubeconfig
if err := c.localArgs.Run(); err != nil {
log.Fatalf("Failed to run operator locally: %v", err)
}
}
return nil
},
}
// Avoid sorting flags so we can group them according to run type.
cmd.Flags().SortFlags = false

// Shared flags.
cmd.Flags().StringVar(&c.kubeconfig, "kubeconfig", "",
"The file path to kubernetes configuration file. Defaults to location "+
"specified by $KUBECONFIG, or to default file rules if not set")
err := cmd.Flags().MarkDeprecated("kubeconfig",
"use --kubeconfig with 'local' or 'packagemanifests' subcommands instead")
if err != nil {
panic(err)
}
// Deprecated: namespace exists for historical compatibility. Use watch-namespace instead.
//TODO: remove namespace flag before 1.0.0
if !kbutil.HasProjectFile() { // not show for the kb layout projects
Expand All @@ -149,38 +151,48 @@ https://sdk.operatorframework.io/docs/olm-integration/cli-overview
cmd.Flags().BoolVar(&c.olm, "olm", false,
"The operator to be run will be managed by OLM in a cluster. "+
"Cannot be set with another run-type flag")
err := cmd.Flags().MarkDeprecated("olm", "use 'run packagemanifests' instead")
err = cmd.Flags().MarkDeprecated("olm", "use 'run packagemanifests' instead")
if err != nil {
panic(err)
}
// Mark all flags used with '--olm' as deprecated and hidden separately so
// all other 'run' flags are still available.
fs := pflag.NewFlagSet("olm", pflag.ExitOnError)
fs.SortFlags = false
fs.StringVar(&c.olmArgs.ManifestsDir, "manifests", "",
olmFS := pflag.NewFlagSet("olm", pflag.ExitOnError)
olmFS.StringVar(&c.olmArgs.ManifestsDir, "manifests", "",
"Directory containing operator package directories and a package manifest file")
c.olmArgs.AddToFlagSet(fs)
fs.VisitAll(func(f *pflag.Flag) {
c.olmArgs.AddToFlagSet(olmFS)
olmFS.VisitAll(func(f *pflag.Flag) {
f.Deprecated = "use this flag with 'run packagemanifests' instead"
f.Hidden = true
})
cmd.Flags().AddFlagSet(fs)
cmd.Flags().AddFlagSet(olmFS)

// 'run --local' and related flags.
cmd.Flags().BoolVar(&c.local, "local", false,
"The operator will be run locally by building the operator binary with "+
"the ability to access a kubernetes cluster using a kubeconfig file. "+
"Cannot be set with another run-type flag.")
c.localArgs.addToFlags(cmd.Flags())
err = cmd.Flags().MarkDeprecated("local", "use 'run local' instead")
if err != nil {
panic(err)
}
localFS := pflag.NewFlagSet("local", pflag.ExitOnError)
c.localArgs.AddToFlags(localFS)
switch projutil.GetOperatorType() {
case projutil.OperatorTypeAnsible:
c.localArgs.ansibleOperatorFlags = aoflags.AddTo(cmd.Flags(), "(ansible operator)")
c.localArgs.AnsibleOperatorFlags = aoflags.AddTo(localFS, "(ansible operator)")
case projutil.OperatorTypeHelm:
c.localArgs.helmOperatorFlags = hoflags.AddTo(cmd.Flags(), "(helm operator)")
c.localArgs.HelmOperatorFlags = hoflags.AddTo(localFS, "(helm operator)")
}
localFS.VisitAll(func(f *pflag.Flag) {
f.Deprecated = "use this flag with 'run local' instead"
f.Hidden = true
})
cmd.Flags().AddFlagSet(localFS)

cmd.AddCommand(
newPackageManifestsCmd(),
packagemanifests.NewCmd(),
local.NewCmd(),
)

return cmd
Expand Down
71 changes: 71 additions & 0 deletions cmd/operator-sdk/run/local/cmd.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
// 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 local

import (
"fmt"

log "github.com/sirupsen/logrus"
"github.com/spf13/cobra"

k8sinternal "github.com/operator-framework/operator-sdk/internal/util/k8sutil"
kbutil "github.com/operator-framework/operator-sdk/internal/util/kubebuilder"
"github.com/operator-framework/operator-sdk/internal/util/projutil"
aoflags "github.com/operator-framework/operator-sdk/pkg/ansible/flags"
hoflags "github.com/operator-framework/operator-sdk/pkg/helm/flags"
)

func NewCmd() *cobra.Command {
c := &RunLocalCmd{}

cmd := &cobra.Command{
Use: "local",
Short: "Run an Operator locally",
Long: `This command will run your Operator locally by building the operator binary
with the ability to access a kubernetes cluster using a kubeconfig file`,
RunE: func(cmd *cobra.Command, args []string) error {
projutil.MustInProjectRoot()

// The main.go and manager.yaml scaffolds in the new layout do not support the WATCH_NAMESPACE
// env var to configure the namespace that the operator watches. The default is all namespaces.
// So this flag is unsupported for the new layout.
if !kbutil.HasProjectFile() {
// Get default namespace to watch if unset.
if !cmd.Flags().Changed("watch-namespace") {
_, defaultNamespace, err := k8sinternal.GetKubeconfigAndNamespace(c.Kubeconfig)
if err != nil {
return fmt.Errorf("error getting kubeconfig and default namespace: %v", err)
}
c.WatchNamespace = defaultNamespace
}
}

if err := c.Run(); err != nil {
log.Fatalf("Failed to run operator: %v", err)
}
return nil
},
}

c.AddToFlags(cmd.Flags())
switch projutil.GetOperatorType() {
case projutil.OperatorTypeAnsible:
c.AnsibleOperatorFlags = aoflags.AddTo(cmd.Flags(), "(ansible operator)")
case projutil.OperatorTypeHelm:
c.HelmOperatorFlags = hoflags.AddTo(cmd.Flags(), "(helm operator)")
}

return cmd
}
Loading