Skip to content

Commit

Permalink
Add '--olm-namespace' flag to OLM related subcommands
Browse files Browse the repository at this point in the history
- Add '--olm-namespace' flag to to 'operator-sdk run --olm' command
- Add '--olm-namespace' flag to 'operator-sdk olm status' command
- Add '--olm-namespace' flag to 'operator-sdk cleanup' command

This fixes the bug where running these commands against a openshift 4.x
cluster would fail because in those clusters the OLM is installed in
'namesapce: openshift-operator-lifecycle-manager' and the code always
defaulted to default 'namespace: olm'. Its now fixed by adding a new
flag '--olm-namespace' that allows to override the default namespace
(olm).

Fixes: #2595
  • Loading branch information
jeyaramashok committed Mar 14, 2020
1 parent 373ebf3 commit af4be55
Show file tree
Hide file tree
Showing 11 changed files with 63 additions and 35 deletions.
10 changes: 5 additions & 5 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,10 @@
- Add a new flag option (`--skip-cleanup-error`) to the test framework to allow skip the function which will remove all artefacts when an error be faced to perform this operation. ([#2512](https://github.com/operator-framework/operator-sdk/pull/2512))
- Add event stats output to the operator logs for Ansible based-operators. ([2580](https://github.com/operator-framework/operator-sdk/pull/2580))
- Improve Ansible logs by allowing output the full Ansible result for Ansible based-operators configurable by environment variable. ([2589](https://github.com/operator-framework/operator-sdk/pull/2589))
- Add the --max-workers flag to the commands operator-sdk exec-entrypoint and operator-sdk run --local for Helm based-operators with the purpose of controling the number of concurrent reconcile workers. ([2607](https://github.com/operator-framework/operator-sdk/pull/2607))
- Add the --proxy-port flag to the operator-sdk scorecard command allowing users to override the default proxy port value (8889). ([2634](https://github.com/operator-framework/operator-sdk/pull/2634))
- Add support for Metrics with MultiNamespace scenario. ([#2603](https://github.com/operator-framework/operator-sdk/pull/2603))
- Add the --max-workers flag to the commands operator-sdk exec-entrypoint and operator-sdk run --local for Helm based-operators with the purpose of controling the number of concurrent reconcile workers. ([2607](https://github.com/operator-framework/operator-sdk/pull/2607))
- Add support for Metrics with MultiNamespace scenario. ([#2603](https://github.com/operator-framework/operator-sdk/pull/2603))
- Add Prometheus metrics support to Helm-based operators. ([#2603](https://github.com/operator-framework/operator-sdk/pull/2603))
- Add a new flag option `--olm-namespace` to `operator-sdk run --olm`, `operator-sdk cleanup --olm` and `operator-sdk olm status` command, which allows specifying the namespace in which OLM is installed. ([#2613](https://github.com/operator-framework/operator-sdk/pull/2613))

### Changed
- Ansible scaffolding has been rewritten to be simpler and make use of newer features of Ansible and Molecule.
Expand All @@ -28,8 +28,8 @@
- Operator user setup and entrypoint scripts no longer insert dynamic runtime user entries into `/etc/passwd`. To use dynamic runtime users, use a container runtime that supports it (e.g. CRI-O). ([#2469](https://github.com/operator-framework/operator-sdk/pull/2469))
- Changed the scorecard basic test, `Writing into CRs has an effect`, to include the http.MethodPatch as part of its test criteria alongside http.MethodPut and http.MethodPost. ([#2509](https://github.com/operator-framework/operator-sdk/pull/2509))
- Changed the scorecard to use the init-timeout configuration setting as a wait time when performing cleanup instead of a hard-coded time. ([#2597](https://github.com/operator-framework/operator-sdk/pull/2597))
- Upgrade the Helm dependency version from `v3.0.1` to `v3.0.2`. ([#2621](https://github.com/operator-framework/operator-sdk/pull/2621))
- Changed the scaffolded `serveCRMetrics` to use the namespaces informed in the environment variable `WATCH_NAMESPACE` in the MultiNamespace scenario. ([#2603](https://github.com/operator-framework/operator-sdk/pull/2603))
- Upgrade the Helm dependency version from `v3.0.1` to `v3.0.2`. ([#2621](https://github.com/operator-framework/operator-sdk/pull/2621))
- Changed the scaffolded `serveCRMetrics` to use the namespaces informed in the environment variable `WATCH_NAMESPACE` in the MultiNamespace scenario. ([#2603](https://github.com/operator-framework/operator-sdk/pull/2603))
- Improve skip metrics logs when running the operator locally in order to make clear the information for Helm based operators. ([#2603](https://github.com/operator-framework/operator-sdk/pull/2603))

### Deprecated
Expand Down
1 change: 1 addition & 0 deletions cmd/operator-sdk/olm/status.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ func newStatusCmd() *cobra.Command {
},
}

cmd.Flags().StringVar(&mgr.OLMNamespace, "olm-namespace", olm.DefaultOLMNamespace, "Namespace where OLM is installed")
mgr.AddToFlagSet(cmd.Flags())
return cmd
}
1 change: 1 addition & 0 deletions doc/cli/operator-sdk_cleanup.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ operator-sdk cleanup [flags]
--install-mode string [olm only] InstallMode to create OperatorGroup with. Format: InstallModeType=[ns1,ns2[, ...]]
--include strings [olm only] Path to Kubernetes resource manifests, ex. Role, Subscription. These supplement or override defaults generated by run/cleanup
--timeout duration [olm only] Time to wait for the command to complete before failing (default 2m0s)
--olm-namespace string [olm only] Namespace where OLM is installed (default "olm")
-h, --help help for cleanup
```

Expand Down
5 changes: 3 additions & 2 deletions doc/cli/operator-sdk_olm_status.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,9 @@ operator-sdk olm status [flags]
### Options

```
-h, --help help for status
--timeout duration time to wait for the command to complete before failing (default 2m0s)
-h, --help help for status
--olm-namespace string Namespace where OLM is installed (default "olm")
--timeout duration time to wait for the command to complete before failing (default 2m0s)
```

### SEE ALSO
Expand Down
1 change: 1 addition & 0 deletions doc/cli/operator-sdk_run.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ operator-sdk run [flags]
--install-mode string [olm only] InstallMode to create OperatorGroup with. Format: InstallModeType=[ns1,ns2[, ...]]
--include strings [olm only] Path to Kubernetes resource manifests, ex. Role, Subscription. These supplement or override defaults generated by run/cleanup
--timeout duration [olm only] Time to wait for the command to complete before failing (default 2m0s)
--olm-namespace string [olm only] Namespace where OLM is installed (default "olm")
--local 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.
--operator-flags string [local only] The flags that the operator needs. Example: "--flag1 value1 --flag2=value2"
--go-ldflags string [local only] Set Go linker options
Expand Down
18 changes: 11 additions & 7 deletions internal/olm/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,10 @@ import (
"k8s.io/client-go/rest"
)

var (
olmOperatorKey = types.NamespacedName{Namespace: olmresourceclient.OLMNamespace, Name: "olm-operator"}
catalogOperatorKey = types.NamespacedName{Namespace: olmresourceclient.OLMNamespace, Name: "catalog-operator"}
packageServerKey = types.NamespacedName{Namespace: olmresourceclient.OLMNamespace, Name: "packageserver"}
const (
olmOperatorName = "olm-operator"
catalogOperatorName = "catalog-operator"
packageServerName = "packageserver"
)

type Client struct {
Expand All @@ -64,7 +64,8 @@ func ClientForConfig(cfg *rest.Config) (*Client, error) {
return c, nil
}

func (c Client) InstallVersion(ctx context.Context, version string) (*olmresourceclient.Status, error) {
func (c Client) InstallVersion(ctx context.Context, namespace, version string) (*olmresourceclient.Status, error) {

resources, err := c.getResources(ctx, version)
if err != nil {
return nil, fmt.Errorf("failed to get resources: %v", err)
Expand All @@ -86,11 +87,13 @@ func (c Client) InstallVersion(ctx context.Context, version string) (*olmresourc
}

log.Print("Waiting for deployment/olm-operator rollout to complete")
olmOperatorKey := types.NamespacedName{Namespace: namespace, Name: olmOperatorName}
if err := c.DoRolloutWait(ctx, olmOperatorKey); err != nil {
return nil, fmt.Errorf("deployment/%s failed to rollout: %v", olmOperatorKey.Name, err)
}

log.Print("Waiting for deployment/catalog-operator rollout to complete")
catalogOperatorKey := types.NamespacedName{Namespace: namespace, Name: catalogOperatorName}
if err := c.DoRolloutWait(ctx, catalogOperatorKey); err != nil {
return nil, fmt.Errorf("deployment/%s failed to rollout: %v", catalogOperatorKey.Name, err)
}
Expand All @@ -117,6 +120,7 @@ func (c Client) InstallVersion(ctx context.Context, version string) (*olmresourc
}
}

packageServerKey := types.NamespacedName{Namespace: namespace, Name: packageServerName}
log.Printf("Waiting for deployment/%s rollout to complete", packageServerKey.Name)
if err := c.DoRolloutWait(ctx, packageServerKey); err != nil {
return nil, fmt.Errorf("deployment/%s failed to rollout: %v", packageServerKey.Name, err)
Expand All @@ -126,7 +130,7 @@ func (c Client) InstallVersion(ctx context.Context, version string) (*olmresourc
return &status, nil
}

func (c Client) UninstallVersion(ctx context.Context, version string) error {
func (c Client) UninstallVersion(ctx context.Context, namespace, version string) error {
resources, err := c.getResources(ctx, version)
if err != nil {
return fmt.Errorf("failed to get resources: %v", err)
Expand All @@ -146,7 +150,7 @@ func (c Client) UninstallVersion(ctx context.Context, version string) error {
return nil
}

func (c Client) GetStatus(ctx context.Context, version string) (*olmresourceclient.Status, error) {
func (c Client) GetStatus(ctx context.Context, namespace, version string) (*olmresourceclient.Status, error) {
resources, err := c.getResources(ctx, version)
if err != nil {
return nil, fmt.Errorf("failed to get resources: %v", err)
Expand Down
9 changes: 4 additions & 5 deletions internal/olm/client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,6 @@ import (
"sigs.k8s.io/controller-runtime/pkg/client/apiutil"
)

const OLMNamespace = "olm"

var ErrOLMNotInstalled = errors.New("no existing installation found")

var Scheme = scheme.Scheme
Expand Down Expand Up @@ -211,14 +209,15 @@ func (c Client) DoCSVWait(ctx context.Context, key types.NamespacedName) error {
return wait.PollImmediateUntil(time.Second, csvPhaseSucceeded, ctx.Done())
}

func (c Client) GetInstalledVersion(ctx context.Context) (string, error) {
opts := client.InNamespace(OLMNamespace)
// GetInstalledVersion returns the OLM version installed in the namespace informed.
func (c Client) GetInstalledVersion(ctx context.Context, namespace string) (string, error) {
opts := client.InNamespace(namespace)
csvs := &olmapiv1alpha1.ClusterServiceVersionList{}
if err := c.KubeClient.List(ctx, csvs, opts); err != nil {
if apierrors.IsNotFound(err) || meta.IsNoMatchError(err) {
return "", ErrOLMNotInstalled
}
return "", fmt.Errorf("failed to list CSVs in namespace %q: %v", OLMNamespace, err)
return "", fmt.Errorf("failed to list CSVs in namespace %q: %v", namespace, err)
}
var pkgServerCSV *olmapiv1alpha1.ClusterServiceVersion
for _, csv := range csvs.Items {
Expand Down
25 changes: 15 additions & 10 deletions internal/olm/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,14 +28,16 @@ import (
const (
DefaultVersion = "latest"
DefaultTimeout = time.Minute * 2
// DefaultOLMNamespace is the namespace where OLM is installed
DefaultOLMNamespace = "olm"
)

type Manager struct {
Client *Client
Version string
Timeout time.Duration

once sync.Once
Client *Client
Version string
Timeout time.Duration
OLMNamespace string
once sync.Once
}

func (m *Manager) initialize() (err error) {
Expand All @@ -60,6 +62,9 @@ func (m *Manager) initialize() (err error) {
if m.Timeout <= 0 {
m.Timeout = DefaultTimeout
}
if m.OLMNamespace == "" {
m.OLMNamespace = DefaultOLMNamespace
}
})
return err
}
Expand All @@ -72,7 +77,7 @@ func (m *Manager) Install() error {
ctx, cancel := context.WithTimeout(context.Background(), m.Timeout)
defer cancel()

status, err := m.Client.InstallVersion(ctx, m.Version)
status, err := m.Client.InstallVersion(ctx, m.OLMNamespace, m.Version)
if err != nil {
return err
}
Expand All @@ -91,12 +96,12 @@ func (m *Manager) Uninstall() error {
ctx, cancel := context.WithTimeout(context.Background(), m.Timeout)
defer cancel()

version, err := m.Client.GetInstalledVersion(ctx)
version, err := m.Client.GetInstalledVersion(ctx, m.OLMNamespace)
if err != nil {
return err
}

if err := m.Client.UninstallVersion(ctx, version); err != nil {
if err := m.Client.UninstallVersion(ctx, m.OLMNamespace, version); err != nil {
return err
}

Expand All @@ -112,12 +117,12 @@ func (m *Manager) Status() error {
ctx, cancel := context.WithTimeout(context.Background(), m.Timeout)
defer cancel()

version, err := m.Client.GetInstalledVersion(ctx)
version, err := m.Client.GetInstalledVersion(ctx, m.OLMNamespace)
if err != nil {
return err
}

status, err := m.Client.GetStatus(ctx, version)
status, err := m.Client.GetStatus(ctx, m.OLMNamespace, version)
if err != nil {
return err
}
Expand Down
20 changes: 14 additions & 6 deletions internal/olm/operator/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"fmt"
"io/ioutil"

"github.com/operator-framework/operator-sdk/internal/olm"
olmresourceclient "github.com/operator-framework/operator-sdk/internal/olm/client"
opinternal "github.com/operator-framework/operator-sdk/internal/olm/operator/internal"
"github.com/operator-framework/operator-sdk/internal/util/k8sutil"
Expand Down Expand Up @@ -51,7 +52,10 @@ func init() {
}

type operatorManager struct {
client *olmresourceclient.Client
client *olmresourceclient.Client
// olmNamespace is the namespace where olm is installed
// and operator registry server resources are created
olmNamespace string
version string
namespace string
forceRegistry bool
Expand All @@ -72,6 +76,10 @@ func (c *OLMCmd) newManager() (*operatorManager, error) {
if err != nil {
return nil, fmt.Errorf("failed to get namespace from kubeconfig %s: %w", c.KubeconfigPath, err)
}
if m.olmNamespace = c.OLMNamespace; m.olmNamespace == "" {
m.olmNamespace = olm.DefaultOLMNamespace
}

if ns == "" {
ns = defaultNamespace
}
Expand Down Expand Up @@ -132,7 +140,7 @@ func (c *OLMCmd) newManager() (*operatorManager, error) {

func (m *operatorManager) run(ctx context.Context) (err error) {
// Ensure OLM is installed.
olmVer, err := m.client.GetInstalledVersion(ctx)
olmVer, err := m.client.GetInstalledVersion(ctx, m.olmNamespace)
if err != nil {
return fmt.Errorf("error getting installed OLM version: %w", err)
}
Expand All @@ -159,12 +167,12 @@ func (m *operatorManager) run(ctx context.Context) (err error) {
return fmt.Errorf("an operator with name %q is present and has resource errors\n%s", pkgName, status)
}

if err = m.registryUp(ctx, olmresourceclient.OLMNamespace); err != nil {
if err = m.registryUp(ctx, m.olmNamespace); err != nil {
return fmt.Errorf("error creating registry resources: %w", err)
}
log.Info("Creating resources")
if !m.hasCatalogSource() {
registryGRPCAddr := opinternal.GetRegistryServiceAddr(pkgName, olmresourceclient.OLMNamespace)
registryGRPCAddr := opinternal.GetRegistryServiceAddr(pkgName, m.olmNamespace)
catsrc := newCatalogSource(pkgName, m.namespace, withGRPC(registryGRPCAddr))
m.olmObjects = append(m.olmObjects, catsrc)
}
Expand Down Expand Up @@ -223,7 +231,7 @@ func (m *operatorManager) run(ctx context.Context) (err error) {

func (m *operatorManager) cleanup(ctx context.Context) (err error) {
// Ensure OLM is installed.
olmVer, err := m.client.GetInstalledVersion(ctx)
olmVer, err := m.client.GetInstalledVersion(ctx, m.olmNamespace)
if err != nil {
return fmt.Errorf("error getting installed OLM version: %w", err)
}
Expand All @@ -237,7 +245,7 @@ func (m *operatorManager) cleanup(ctx context.Context) (err error) {
return fmt.Errorf("error getting CSV from bundle: %w", err)
}

if err = m.registryDown(ctx, olmresourceclient.OLMNamespace); err != nil {
if err = m.registryDown(ctx, m.olmNamespace); err != nil {
return fmt.Errorf("error removing registry resources: %w", err)
}
log.Info("Deleting resources")
Expand Down
6 changes: 6 additions & 0 deletions internal/olm/operator/operator.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ import (
"sync"
"time"

"github.com/operator-framework/operator-sdk/internal/olm"

"github.com/spf13/pflag"
)

Expand Down Expand Up @@ -73,6 +75,8 @@ type OLMCmd struct { // nolint:golint
// OperatorNamespace must already exist in the cluster or be defined in
// a manifest passed to IncludePaths.
OperatorNamespace string
// OLMNamespace is the namespace in which OLM is installed.
OLMNamespace string
// Timeout dictates how long to wait for a REST call to complete. A call
// exceeding Timeout will generate an error.
Timeout time.Duration
Expand All @@ -97,6 +101,8 @@ func (c *OLMCmd) AddToFlagSet(fs *pflag.FlagSet) {
"These supplement or override defaults generated by run/cleanup")
fs.DurationVar(&c.Timeout, "timeout", defaultTimeout,
prefix+"Time to wait for the command to complete before failing")
fs.StringVar(&c.OLMNamespace, "olm-namespace", olm.DefaultOLMNamespace,
prefix+"Namespace where OLM is installed")
}

func (c *OLMCmd) validate() error {
Expand Down
2 changes: 2 additions & 0 deletions test/integration/operator_olm_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"testing"
"time"

"github.com/operator-framework/operator-sdk/internal/olm"
operator "github.com/operator-framework/operator-sdk/internal/olm/operator"
"github.com/operator-framework/operator-sdk/pkg/k8sutil"

Expand Down Expand Up @@ -91,6 +92,7 @@ func SingleOperator(t *testing.T) {
OperatorVersion: operatorVersion,
KubeconfigPath: kubeconfigPath,
Timeout: defaultTimeout,
OLMNamespace: olm.DefaultOLMNamespace,
}
// Cleanup.
defer func() {
Expand Down

0 comments on commit af4be55

Please sign in to comment.