Skip to content

Commit 8261b24

Browse files
committed
add selector
1 parent df4c62c commit 8261b24

File tree

4 files changed

+73
-48
lines changed

4 files changed

+73
-48
lines changed

internal/cmd/internal/olmv1/operator_update.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import (
1111

1212
// NewOperatorUpdateCmd allows updating a selected operator
1313
func NewOperatorUpdateCmd(cfg *action.Configuration) *cobra.Command {
14-
i := v1action.NewOperatorUpdate(cfg.Client)
14+
i := v1action.NewOperatorUpdate(cfg)
1515
i.Logf = log.Printf
1616

1717
cmd := &cobra.Command{
@@ -33,8 +33,9 @@ func NewOperatorUpdateCmd(cfg *action.Configuration) *cobra.Command {
3333
}
3434

3535
func bindOperatorUpdateFlags(fs *pflag.FlagSet, i *v1action.OperatorUpdate) {
36-
fs.StringVar(&i.Version, "version", "", "desired operator version (single or range) in semver format. AND operation with channels")
37-
fs.StringArrayVar(&i.Channels, "channels", []string{}, "desired channels for operator versions. AND operation with version. Empty list means all available channels will be taken into consideration.")
36+
fs.StringVar(&i.Version, "version", "", "desired operator version (single or range) in semVer format. AND operation with channels")
37+
fs.StringVar(&i.Selector, "selector", "", "filters the set of catalogs used in the bundle selection process. Empty means that all catalogs will be used in the bundle selection process")
38+
fs.StringArrayVar(&i.Channels, "channels", []string{}, "desired channels for operator versions. AND operation with version. Empty list means all available channels will be taken into consideration")
3839
fs.StringVar(&i.UpgradeConstraintPolicy, "upgrade-constraint-policy", "", "controls whether the upgrade path(s) defined in the catalog are enforced. One of CatalogProvided|SelfCertified), Default: CatalogProvided")
3940
fs.StringToStringVar(&i.Labels, "labels", map[string]string{}, "labels that will be set on the operator")
4041
fs.BoolVar(&i.OverrideUnset, "override-unset-with-current", false, "when enabled, any unset flag value will be overridden with value already set in current operator")

internal/pkg/v1/action/interfaces.go

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,6 @@ type creator interface {
1010
Create(ctx context.Context, obj client.Object, opts ...client.CreateOption) error
1111
}
1212

13-
type updater interface {
14-
Update(ctx context.Context, obj client.Object, opts ...client.UpdateOption) error
15-
}
16-
1713
type deleter interface {
1814
Delete(ctx context.Context, obj client.Object, opts ...client.DeleteOption) error
1915
}

internal/pkg/v1/action/operator_update.go

Lines changed: 62 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -12,20 +12,18 @@ import (
1212
"k8s.io/apimachinery/pkg/types"
1313

1414
olmv1 "github.com/operator-framework/operator-controller/api/v1"
15-
)
1615

17-
type updateClient interface {
18-
updater
19-
getter
20-
}
16+
"github.com/operator-framework/kubectl-operator/pkg/action"
17+
)
2118

2219
type OperatorUpdate struct {
23-
client updateClient
20+
cfg *action.Configuration
2421

2522
Package string
2623

2724
Version string
2825
Channels []string
26+
Selector string
2927
UpgradeConstraintPolicy string
3028
Labels map[string]string
3129
OverrideUnset bool
@@ -35,18 +33,18 @@ type OperatorUpdate struct {
3533
Logf func(string, ...interface{})
3634
}
3735

38-
func NewOperatorUpdate(client updateClient) *OperatorUpdate {
36+
func NewOperatorUpdate(cfg *action.Configuration) *OperatorUpdate {
3937
return &OperatorUpdate{
40-
client: client,
41-
Logf: func(string, ...interface{}) {},
38+
cfg: cfg,
39+
Logf: func(string, ...interface{}) {},
4240
}
4341
}
4442

4543
func (ou *OperatorUpdate) Run(ctx context.Context) (*olmv1.ClusterExtension, error) {
4644
var ext olmv1.ClusterExtension
4745

4846
opKey := types.NamespacedName{Name: ou.Package}
49-
if err := ou.client.Get(ctx, opKey, &ext); err != nil {
47+
if err := ou.cfg.Client.Get(ctx, opKey, &ext); err != nil {
5048
return nil, err
5149
}
5250

@@ -60,60 +58,90 @@ func (ou *OperatorUpdate) Run(ctx context.Context) (*olmv1.ClusterExtension, err
6058
return nil, ErrNoChange
6159
}
6260

63-
if ou.Version != "" {
64-
if _, err := semver.ParseRange(ou.Version); err != nil {
65-
return nil, fmt.Errorf("failed parsing version: %w", err)
66-
}
61+
if err := ou.prepareUpdatedExtension(&ext, constraintPolicy); err != nil {
62+
return nil, err
6763
}
6864

69-
ext.SetLabels(ou.Labels)
70-
ext.Spec.Source.Catalog.Version = ou.Version
71-
ext.Spec.Source.Catalog.Channels = ou.Channels
72-
ext.Spec.Source.Catalog.UpgradeConstraintPolicy = constraintPolicy
73-
if err := ou.client.Update(ctx, &ext); err != nil {
65+
if err := ou.cfg.Client.Update(ctx, &ext); err != nil {
7466
return nil, err
7567
}
7668

77-
if err := waitUntilOperatorStatusCondition(ctx, ou.client, &ext, olmv1.TypeInstalled, metav1.ConditionTrue); err != nil {
69+
if err := waitUntilOperatorStatusCondition(ctx, ou.cfg.Client, &ext, olmv1.TypeInstalled, metav1.ConditionTrue); err != nil {
7870
return nil, fmt.Errorf("timed out waiting for operator: %w", err)
7971
}
8072

8173
return &ext, nil
8274
}
8375

8476
func (ou *OperatorUpdate) setDefaults(ext olmv1.ClusterExtension) {
85-
catalogSrc := ext.Spec.Source.Catalog
86-
if ou.OverrideUnset {
87-
if ou.Version == "" {
88-
ou.Version = catalogSrc.Version
89-
}
90-
if len(ou.Channels) == 0 {
91-
ou.Channels = catalogSrc.Channels
92-
}
77+
if !ou.OverrideUnset {
9378
if ou.UpgradeConstraintPolicy == "" {
94-
ou.UpgradeConstraintPolicy = string(catalogSrc.UpgradeConstraintPolicy)
95-
}
96-
if len(ou.Labels) == 0 {
97-
ou.Labels = ext.Labels
79+
ou.UpgradeConstraintPolicy = string(olmv1.UpgradeConstraintPolicyCatalogProvided)
9880
}
9981

10082
return
10183
}
10284

85+
// OverrideUnset is enabled
86+
// set all unset values to what they are on the current object
87+
catalogSrc := ext.Spec.Source.Catalog
88+
if ou.Version == "" {
89+
ou.Version = catalogSrc.Version
90+
}
91+
if len(ou.Channels) == 0 {
92+
ou.Channels = catalogSrc.Channels
93+
}
10394
if ou.UpgradeConstraintPolicy == "" {
104-
ou.UpgradeConstraintPolicy = string(olmv1.UpgradeConstraintPolicyCatalogProvided)
95+
ou.UpgradeConstraintPolicy = string(catalogSrc.UpgradeConstraintPolicy)
96+
}
97+
if len(ou.Labels) == 0 {
98+
ou.Labels = ext.Labels
99+
}
100+
if ou.Selector == "" && catalogSrc.Selector != nil {
101+
ou.Selector = catalogSrc.Selector.String()
105102
}
106103
}
107104

108105
func (ou *OperatorUpdate) needsUpdate(ext olmv1.ClusterExtension, constraintPolicy olmv1.UpgradeConstraintPolicy) bool {
109106
catalogSrc := ext.Spec.Source.Catalog
110107

108+
var currentSelector string
109+
if catalogSrc.Selector != nil {
110+
currentSelector = catalogSrc.Selector.String()
111+
}
112+
111113
if catalogSrc.Version == ou.Version &&
112114
slices.Equal(catalogSrc.Channels, ou.Channels) &&
113115
catalogSrc.UpgradeConstraintPolicy == constraintPolicy &&
114-
maps.Equal(ext.Labels, ou.Labels) {
116+
maps.Equal(ext.Labels, ou.Labels) &&
117+
currentSelector == ou.Selector {
115118
return false
116119
}
117120

118121
return true
119122
}
123+
124+
func (ou *OperatorUpdate) prepareUpdatedExtension(ext *olmv1.ClusterExtension, constraintPolicy olmv1.UpgradeConstraintPolicy) error {
125+
var err error
126+
var selector *metav1.LabelSelector
127+
128+
if ou.Version != "" {
129+
if _, err = semver.ParseRange(ou.Version); err != nil {
130+
return fmt.Errorf("failed parsing version: %w", err)
131+
}
132+
}
133+
if ou.Selector != "" {
134+
selector, err = metav1.ParseToLabelSelector(ou.Selector)
135+
if err != nil {
136+
return fmt.Errorf("failed parsing selector: %w", err)
137+
}
138+
}
139+
140+
ext.SetLabels(ou.Labels)
141+
ext.Spec.Source.Catalog.Version = ou.Version
142+
ext.Spec.Source.Catalog.Selector = selector
143+
ext.Spec.Source.Catalog.Channels = ou.Channels
144+
ext.Spec.Source.Catalog.UpgradeConstraintPolicy = constraintPolicy
145+
146+
return nil
147+
}

internal/pkg/v1/action/operator_update_test.go

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ var _ = Describe("OperatorUpdate", func() {
3838
It("fails finding existing operator", func() {
3939
cfg := setupEnv()
4040

41-
updater := internalaction.NewOperatorUpdate(cfg.Client)
41+
updater := internalaction.NewOperatorUpdate(&cfg)
4242
updater.Package = "does-not-exist"
4343
ext, err := updater.Run(context.TODO())
4444

@@ -50,7 +50,7 @@ var _ = Describe("OperatorUpdate", func() {
5050
It("fails to handle operator with non-catalog source type", func() {
5151
cfg := setupEnv(buildExtension("test", withSourceType("unknown")))
5252

53-
updater := internalaction.NewOperatorUpdate(cfg.Client)
53+
updater := internalaction.NewOperatorUpdate(&cfg)
5454
updater.Package = "test"
5555
ext, err := updater.Run(context.TODO())
5656

@@ -66,7 +66,7 @@ var _ = Describe("OperatorUpdate", func() {
6666
withConstraintPolicy(string(olmv1.UpgradeConstraintPolicyCatalogProvided))),
6767
)
6868

69-
updater := internalaction.NewOperatorUpdate(cfg.Client)
69+
updater := internalaction.NewOperatorUpdate(&cfg)
7070
updater.Package = "test"
7171
ext, err := updater.Run(context.TODO())
7272

@@ -85,7 +85,7 @@ var _ = Describe("OperatorUpdate", func() {
8585
withVersion("10.0.4"),
8686
))
8787

88-
updater := internalaction.NewOperatorUpdate(cfg.Client)
88+
updater := internalaction.NewOperatorUpdate(&cfg)
8989
updater.Package = "test"
9090
updater.OverrideUnset = true
9191
ext, err := updater.Run(context.TODO())
@@ -102,7 +102,7 @@ var _ = Describe("OperatorUpdate", func() {
102102
withConstraintPolicy(string(olmv1.UpgradeConstraintPolicyCatalogProvided))),
103103
)
104104

105-
updater := internalaction.NewOperatorUpdate(cfg.Client)
105+
updater := internalaction.NewOperatorUpdate(&cfg)
106106
updater.Package = "test"
107107
updater.Version = "10-4"
108108
ext, err := updater.Run(context.TODO())
@@ -123,7 +123,7 @@ var _ = Describe("OperatorUpdate", func() {
123123
ctx, cancel := context.WithCancel(context.TODO())
124124
cancel()
125125

126-
updater := internalaction.NewOperatorUpdate(cfg.Client)
126+
updater := internalaction.NewOperatorUpdate(&cfg)
127127
updater.Package = "test"
128128
updater.Version = "10.0.4"
129129
updater.Channels = []string{"a", "b"}
@@ -150,7 +150,7 @@ var _ = Describe("OperatorUpdate", func() {
150150
Should(Succeed())
151151
}()
152152

153-
updater := internalaction.NewOperatorUpdate(cfg.Client)
153+
updater := internalaction.NewOperatorUpdate(&cfg)
154154
updater.Package = "test"
155155
updater.Version = "10.0.4"
156156
updater.Channels = []string{"a", "b"}

0 commit comments

Comments
 (0)