Skip to content

Commit bf48013

Browse files
committed
operator install: honor operatorframework.io/suggested-namespace by default
Signed-off-by: Joe Lanford <[email protected]>
1 parent 720400e commit bf48013

File tree

4 files changed

+48
-12
lines changed

4 files changed

+48
-12
lines changed

go.sum

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -581,7 +581,6 @@ github.com/onsi/gomega v1.7.1/go.mod h1:XdKZgCCFLUoM/7CFJVPcG8C1xQ1AJ0vpAezJrB7J
581581
github.com/onsi/gomega v1.8.1/go.mod h1:Ho0h+IUsWyvy1OpqCwxlQ/21gkhVunqlU8fDGcoTdcA=
582582
github.com/onsi/gomega v1.9.0/go.mod h1:Ho0h+IUsWyvy1OpqCwxlQ/21gkhVunqlU8fDGcoTdcA=
583583
github.com/onsi/gomega v1.10.1/go.mod h1:iN09h71vgCQne3DLsj+A5owkum+a2tYe+TOCB1ybHNo=
584-
github.com/onsi/gomega v1.10.2 h1:aY/nuoWlKJud2J6U0E3NWsjlg+0GtwXxgEqthRdzlcs=
585584
github.com/onsi/gomega v1.10.2/go.mod h1:iN09h71vgCQne3DLsj+A5owkum+a2tYe+TOCB1ybHNo=
586585
github.com/onsi/gomega v1.11.0 h1:+CqWgvj0OZycCaqclBD1pxKHAU+tOkHmQIWvDHq2aug=
587586
github.com/onsi/gomega v1.11.0/go.mod h1:azGKhqFUon9Vuj0YmTfLSmx0FUwqXYSTl5re8lQLTUg=
@@ -898,7 +897,6 @@ golang.org/x/net v0.0.0-20200226121028-0de0cce0169b/go.mod h1:z5CRVTTTmAJ677TzLL
898897
golang.org/x/net v0.0.0-20200301022130-244492dfa37a/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
899898
golang.org/x/net v0.0.0-20200324143707-d3edc9973b7e/go.mod h1:qpuaurCH72eLCgpAm/N6yyVIVM9cpaDIP3A8BGJEC5A=
900899
golang.org/x/net v0.0.0-20200520004742-59133d7f0dd7/go.mod h1:qpuaurCH72eLCgpAm/N6yyVIVM9cpaDIP3A8BGJEC5A=
901-
golang.org/x/net v0.0.0-20201110031124-69a78807bb2b h1:uwuIcX0g4Yl1NC5XAz37xsr2lTtcqevgzYNVt49waME=
902900
golang.org/x/net v0.0.0-20201110031124-69a78807bb2b/go.mod h1:sp8m0HH+o8qH0wwXwYZr8TS3Oi6o0r6Gce1SSxlDquU=
903901
golang.org/x/net v0.0.0-20201202161906-c7110b5ffcbb h1:eBmm0M9fYhWpKZLjQUUKka/LtIxf46G4fxeEz5KJr9U=
904902
golang.org/x/net v0.0.0-20201202161906-c7110b5ffcbb/go.mod h1:sp8m0HH+o8qH0wwXwYZr8TS3Oi6o0r6Gce1SSxlDquU=
@@ -1147,7 +1145,6 @@ gopkg.in/yaml.v2 v2.2.4/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
11471145
gopkg.in/yaml.v2 v2.2.5/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
11481146
gopkg.in/yaml.v2 v2.2.7/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
11491147
gopkg.in/yaml.v2 v2.2.8/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
1150-
gopkg.in/yaml.v2 v2.3.0 h1:clyUAQHOM3G0M3f5vQj7LuJrETvjVot3Z5el9nffUtU=
11511148
gopkg.in/yaml.v2 v2.3.0/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
11521149
gopkg.in/yaml.v2 v2.4.0 h1:D8xgwECY7CYvx+Y2n4sBz93Jn9JRvxdiyyo8CTfuKaY=
11531150
gopkg.in/yaml.v2 v2.4.0/go.mod h1:RDklbk79AGWmwhnvt/jBztapEOGDOx6ZbXqjP6csGnQ=

internal/cmd/operator_install.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package cmd
22

33
import (
4+
"errors"
45
"fmt"
56
"time"
67

@@ -25,6 +26,11 @@ func newOperatorInstallCmd(cfg *action.Configuration) *cobra.Command {
2526
i.Package = args[0]
2627
csv, err := i.Run(cmd.Context())
2728
if err != nil {
29+
if errors.Is(err, internalaction.ErrNoOperatorGroup) {
30+
log.Fatalf("operator group not found in namespace %q, use --create-operator-group to create one automatically", cfg.Namespace)
31+
} else if altNsErr := (&internalaction.ErrIncorrectNamespace{}); errors.As(err, altNsErr) {
32+
log.Fatalf("invalid installation namespace: use --namespace=%q to install into operator's suggested namespace or --permit-alternate-namespace to force installation in %q", altNsErr.Suggested, altNsErr.Requested)
33+
}
2834
log.Fatalf("failed to install operator: %v", err)
2935
}
3036
log.Printf("operator %q installed; installed csv is %q", i.Package, csv.Name)
@@ -42,6 +48,7 @@ func bindOperatorInstallFlags(fs *pflag.FlagSet, i *internalaction.OperatorInsta
4248
fs.StringSliceVarP(&i.WatchNamespaces, "watch", "w", []string{}, "namespaces to watch")
4349
fs.DurationVar(&i.CleanupTimeout, "cleanup-timeout", time.Minute, "the amount of time to wait before cancelling cleanup")
4450
fs.BoolVarP(&i.CreateOperatorGroup, "create-operator-group", "C", false, "create operator group if necessary")
51+
fs.BoolVar(&i.PermitAlternateNamespace, "permit-alternate-namespace", false, "permit an alternate namespace to be used when the operator defines operatorframework.io/suggested-namespace")
4552

4653
fs.VarP(&i.InstallMode, "install-mode", "i", "install mode")
4754
err := fs.MarkHidden("install-mode")

internal/pkg/action/operator_install.go

Lines changed: 39 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package action
22

33
import (
44
"context"
5+
"errors"
56
"fmt"
67
"regexp"
78
"strings"
@@ -10,6 +11,7 @@ import (
1011
v1 "github.com/operator-framework/api/pkg/operators/v1"
1112
"github.com/operator-framework/api/pkg/operators/v1alpha1"
1213
operatorsv1 "github.com/operator-framework/operator-lifecycle-manager/pkg/package-server/apis/operators/v1"
14+
corev1 "k8s.io/api/core/v1"
1315
"k8s.io/apimachinery/pkg/types"
1416
"k8s.io/apimachinery/pkg/util/sets"
1517
"k8s.io/apimachinery/pkg/util/wait"
@@ -23,14 +25,15 @@ import (
2325
type OperatorInstall struct {
2426
config *action.Configuration
2527

26-
Package string
27-
Channel string
28-
Version string
29-
Approval subscription.ApprovalValue
30-
WatchNamespaces []string
31-
InstallMode operator.InstallMode
32-
CleanupTimeout time.Duration
33-
CreateOperatorGroup bool
28+
Package string
29+
Channel string
30+
Version string
31+
Approval subscription.ApprovalValue
32+
WatchNamespaces []string
33+
InstallMode operator.InstallMode
34+
CleanupTimeout time.Duration
35+
CreateOperatorGroup bool
36+
PermitAlternateNamespace bool
3437

3538
Logf func(string, ...interface{})
3639
}
@@ -42,6 +45,17 @@ func NewOperatorInstall(cfg *action.Configuration) *OperatorInstall {
4245
}
4346
}
4447

48+
var ErrNoOperatorGroup = errors.New("operator group not found")
49+
50+
type ErrIncorrectNamespace struct {
51+
Requested string
52+
Suggested string
53+
}
54+
55+
func (e ErrIncorrectNamespace) Error() string {
56+
return fmt.Sprintf("requested install namespace is %q, but operator's suggested namespace is %q", e.Requested, e.Suggested)
57+
}
58+
4559
func (i *OperatorInstall) Run(ctx context.Context) (*v1alpha1.ClusterServiceVersion, error) {
4660
if len(i.WatchNamespaces) > 0 && !i.InstallMode.IsEmpty() {
4761
return nil, fmt.Errorf("WatchNamespaces and InstallMode options are mutually exclusive")
@@ -60,6 +74,10 @@ func (i *OperatorInstall) Run(ctx context.Context) (*v1alpha1.ClusterServiceVers
6074
return nil, fmt.Errorf("get package channel: %v", err)
6175
}
6276

77+
if err := i.ensureNamespace(ctx, pc); err != nil {
78+
return nil, err
79+
}
80+
6381
if _, err := i.ensureOperatorGroup(ctx, pm, pc); err != nil {
6482
return nil, err
6583
}
@@ -117,6 +135,18 @@ func (i *OperatorInstall) getPackageManifest(ctx context.Context) (*operator.Pac
117135
return &operator.PackageManifest{PackageManifest: *pm}, nil
118136
}
119137

138+
func (i *OperatorInstall) ensureNamespace(ctx context.Context, pc *operator.PackageChannel) error {
139+
suggestedNamespace := pc.CurrentCSVDesc.Annotations["operatorframework.io/suggested-namespace"]
140+
if !i.PermitAlternateNamespace && suggestedNamespace != "" && i.config.Namespace != suggestedNamespace {
141+
return ErrIncorrectNamespace{Suggested: suggestedNamespace, Requested: i.config.Namespace}
142+
}
143+
ns := corev1.Namespace{}
144+
if err := i.config.Client.Get(ctx, types.NamespacedName{Name: i.config.Namespace}, &ns); err != nil {
145+
return err
146+
}
147+
return nil
148+
}
149+
120150
func (i *OperatorInstall) ensureOperatorGroup(ctx context.Context, pm *operator.PackageManifest, pc *operator.PackageChannel) (*v1.OperatorGroup, error) {
121151
og, err := i.getOperatorGroup(ctx)
122152
if err != nil {
@@ -156,7 +186,7 @@ func (i *OperatorInstall) ensureOperatorGroup(ctx context.Context, pm *operator.
156186
}
157187
i.Logf("operatorgroup %q created", og.Name)
158188
} else {
159-
return nil, fmt.Errorf("namespace %q has no existing operator group; use --create-operator-group to create one automatically", i.config.Namespace)
189+
return nil, ErrNoOperatorGroup
160190
}
161191
} else if err := i.validateOperatorGroup(*og, supported); err != nil {
162192
return nil, err

pkg/action/config.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77
"github.com/operator-framework/api/pkg/operators/v1alpha1"
88
operatorsv1 "github.com/operator-framework/operator-lifecycle-manager/pkg/package-server/apis/operators/v1"
99
"github.com/spf13/pflag"
10+
corev1 "k8s.io/api/core/v1"
1011
apiextv1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1"
1112
"k8s.io/apimachinery/pkg/runtime"
1213
"k8s.io/client-go/tools/clientcmd"
@@ -16,6 +17,7 @@ import (
1617
func NewScheme() (*runtime.Scheme, error) {
1718
sch := runtime.NewScheme()
1819
for _, f := range []func(*runtime.Scheme) error{
20+
corev1.AddToScheme,
1921
v1alpha1.AddToScheme,
2022
operatorsv1.AddToScheme,
2123
v1.AddToScheme,

0 commit comments

Comments
 (0)