Skip to content

Commit b345abb

Browse files
committed
operator install: honor operatorframework.io/suggested-namespace by default
1 parent f12f147 commit b345abb

File tree

4 files changed

+53
-12
lines changed

4 files changed

+53
-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: 9 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,13 @@ func newOperatorInstallCmd(cfg *action.Configuration) *cobra.Command {
2526
i.Package = args[0]
2627
csv, err := i.Run(cmd.Context())
2728
if err != nil {
29+
ogErr := internalaction.ErrNoOperatorGroup{}
30+
altNsErr := internalaction.ErrIncorrectNamespace{}
31+
if errors.As(err, &ogErr) {
32+
log.Fatalf("operator group not found in namespace %q, use --create-operator-group to create one automatically", cfg.Namespace)
33+
} else if errors.As(err, &altNsErr) {
34+
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)
35+
}
2836
log.Fatalf("failed to install operator: %v", err)
2937
}
3038
log.Printf("operator %q installed; installed csv is %q", i.Package, csv.Name)
@@ -42,6 +50,7 @@ func bindOperatorInstallFlags(fs *pflag.FlagSet, i *internalaction.OperatorInsta
4250
fs.StringSliceVarP(&i.WatchNamespaces, "watch", "w", []string{}, "namespaces to watch")
4351
fs.DurationVar(&i.CleanupTimeout, "cleanup-timeout", time.Minute, "the amount of time to wait before cancelling cleanup")
4452
fs.BoolVarP(&i.CreateOperatorGroup, "create-operator-group", "C", false, "create operator group if necessary")
53+
fs.BoolVar(&i.PermitAlternateNamespace, "permit-alternate-namespace", false, "permit an alternate namespace to be used when the operator defines operatorframework.io/suggested-namespace")
4554

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

internal/pkg/action/operator_install.go

Lines changed: 42 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import (
1010
v1 "github.com/operator-framework/api/pkg/operators/v1"
1111
"github.com/operator-framework/api/pkg/operators/v1alpha1"
1212
operatorsv1 "github.com/operator-framework/operator-lifecycle-manager/pkg/package-server/apis/operators/v1"
13+
corev1 "k8s.io/api/core/v1"
1314
"k8s.io/apimachinery/pkg/types"
1415
"k8s.io/apimachinery/pkg/util/sets"
1516
"k8s.io/apimachinery/pkg/util/wait"
@@ -23,14 +24,15 @@ import (
2324
type OperatorInstall struct {
2425
config *action.Configuration
2526

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
27+
Package string
28+
Channel string
29+
Version string
30+
Approval subscription.ApprovalValue
31+
WatchNamespaces []string
32+
InstallMode operator.InstallMode
33+
CleanupTimeout time.Duration
34+
CreateOperatorGroup bool
35+
PermitAlternateNamespace bool
3436

3537
Logf func(string, ...interface{})
3638
}
@@ -42,6 +44,21 @@ func NewOperatorInstall(cfg *action.Configuration) *OperatorInstall {
4244
}
4345
}
4446

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

80+
if err := i.ensureNamespace(ctx, pc); err != nil {
81+
return nil, err
82+
}
83+
6384
if _, err := i.ensureOperatorGroup(ctx, pm, pc); err != nil {
6485
return nil, err
6586
}
@@ -117,6 +138,18 @@ func (i *OperatorInstall) getPackageManifest(ctx context.Context) (*operator.Pac
117138
return &operator.PackageManifest{PackageManifest: *pm}, nil
118139
}
119140

141+
func (i *OperatorInstall) ensureNamespace(ctx context.Context, pc *operator.PackageChannel) error {
142+
suggestedNamespace := pc.CurrentCSVDesc.Annotations["operatorframework.io/suggested-namespace"]
143+
if !i.PermitAlternateNamespace && suggestedNamespace != "" && i.config.Namespace != suggestedNamespace {
144+
return ErrIncorrectNamespace{Suggested: suggestedNamespace, Requested: i.config.Namespace}
145+
}
146+
ns := corev1.Namespace{}
147+
if err := i.config.Client.Get(ctx, types.NamespacedName{Name: i.config.Namespace}, &ns); err != nil {
148+
return err
149+
}
150+
return nil
151+
}
152+
120153
func (i *OperatorInstall) ensureOperatorGroup(ctx context.Context, pm *operator.PackageManifest, pc *operator.PackageChannel) (*v1.OperatorGroup, error) {
121154
og, err := i.getOperatorGroup(ctx)
122155
if err != nil {
@@ -156,7 +189,7 @@ func (i *OperatorInstall) ensureOperatorGroup(ctx context.Context, pm *operator.
156189
}
157190
i.Logf("operatorgroup %q created", og.Name)
158191
} else {
159-
return nil, fmt.Errorf("namespace %q has no existing operator group; use --create-operator-group to create one automatically", i.config.Namespace)
192+
return nil, ErrNoOperatorGroup{}
160193
}
161194
} else if err := i.validateOperatorGroup(*og, supported); err != nil {
162195
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)