Skip to content

Commit b2ccb7e

Browse files
committed
Add command to update olmv1 operator
Signed-off-by: Artur Zych <[email protected]>
1 parent 6be5255 commit b2ccb7e

File tree

8 files changed

+498
-15
lines changed

8 files changed

+498
-15
lines changed
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
package olmv1
2+
3+
import (
4+
"github.com/spf13/cobra"
5+
"github.com/spf13/pflag"
6+
7+
"github.com/operator-framework/kubectl-operator/internal/cmd/internal/log"
8+
v1action "github.com/operator-framework/kubectl-operator/internal/pkg/v1/action"
9+
"github.com/operator-framework/kubectl-operator/pkg/action"
10+
)
11+
12+
// NewOperatorUpdateCmd allows updating a selected operator
13+
func NewOperatorUpdateCmd(cfg *action.Configuration) *cobra.Command {
14+
i := v1action.NewOperatorUpdate(cfg)
15+
i.Logf = log.Printf
16+
17+
cmd := &cobra.Command{
18+
Use: "operator <operator>",
19+
Short: "Update an operator",
20+
Args: cobra.ExactArgs(1),
21+
Run: func(cmd *cobra.Command, args []string) {
22+
i.Package = args[0]
23+
_, err := i.Run(cmd.Context())
24+
if err != nil {
25+
log.Fatalf("failed to update operator: %v", err)
26+
}
27+
log.Printf("operator %q updated", i.Package)
28+
},
29+
}
30+
bindOperatorUpdateFlags(cmd.Flags(), i)
31+
32+
return cmd
33+
}
34+
35+
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.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")
39+
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")
40+
fs.StringToStringVar(&i.Labels, "labels", map[string]string{}, "labels that will be set on the operator")
41+
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")
42+
}

internal/cmd/olmv1.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,12 +38,22 @@ func newOlmV1Cmd(cfg *action.Configuration) *cobra.Command {
3838
}
3939
deleteCmd.AddCommand(olmv1.NewCatalogDeleteCmd(cfg))
4040

41+
updateCmd := &cobra.Command{
42+
Use: "update",
43+
Short: "Update a resource",
44+
Long: "Update a resource",
45+
}
46+
updateCmd.AddCommand(
47+
olmv1.NewOperatorUpdateCmd(cfg),
48+
)
49+
4150
cmd.AddCommand(
4251
olmv1.NewOperatorInstallCmd(cfg),
4352
olmv1.NewOperatorUninstallCmd(cfg),
4453
getCmd,
4554
createCmd,
4655
deleteCmd,
56+
updateCmd,
4757
)
4858

4959
return cmd

internal/pkg/v1/action/action_suite_test.go

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,9 @@ import (
88
. "github.com/onsi/ginkgo"
99
. "github.com/onsi/gomega"
1010

11+
apimeta "k8s.io/apimachinery/pkg/api/meta"
1112
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
13+
"k8s.io/apimachinery/pkg/types"
1214
"sigs.k8s.io/controller-runtime/pkg/client"
1315

1416
olmv1 "github.com/operator-framework/operator-controller/api/v1"
@@ -63,3 +65,69 @@ func newClusterCatalog(name string) *olmv1.ClusterCatalog {
6365
ObjectMeta: metav1.ObjectMeta{Name: name},
6466
}
6567
}
68+
69+
type extensionOpt func(*olmv1.ClusterExtension)
70+
71+
func withVersion(version string) extensionOpt {
72+
return func(ext *olmv1.ClusterExtension) {
73+
ext.Spec.Source.Catalog.Version = version
74+
}
75+
}
76+
77+
func withSourceType(sourceType string) extensionOpt {
78+
return func(ext *olmv1.ClusterExtension) {
79+
ext.Spec.Source.SourceType = sourceType
80+
}
81+
}
82+
83+
// nolint: unparam
84+
func withConstraintPolicy(policy string) extensionOpt {
85+
return func(ext *olmv1.ClusterExtension) {
86+
ext.Spec.Source.Catalog.UpgradeConstraintPolicy = olmv1.UpgradeConstraintPolicy(policy)
87+
}
88+
}
89+
90+
func withChannels(channels ...string) extensionOpt {
91+
return func(ext *olmv1.ClusterExtension) {
92+
ext.Spec.Source.Catalog.Channels = channels
93+
}
94+
}
95+
96+
func withLabels(labels map[string]string) extensionOpt {
97+
return func(ext *olmv1.ClusterExtension) {
98+
ext.SetLabels(labels)
99+
}
100+
}
101+
102+
func buildExtension(packageName string, opts ...extensionOpt) *olmv1.ClusterExtension {
103+
ext := &olmv1.ClusterExtension{
104+
Spec: olmv1.ClusterExtensionSpec{
105+
Source: olmv1.SourceConfig{
106+
Catalog: &olmv1.CatalogFilter{PackageName: packageName},
107+
},
108+
},
109+
}
110+
ext.SetName(packageName)
111+
for _, opt := range opts {
112+
opt(ext)
113+
}
114+
115+
return ext
116+
}
117+
118+
func updateOperatorConditionStatus(name string, cl client.Client, typ string, status metav1.ConditionStatus) error {
119+
var ext olmv1.ClusterExtension
120+
key := types.NamespacedName{Name: name}
121+
122+
if err := cl.Get(context.TODO(), key, &ext); err != nil {
123+
return err
124+
}
125+
126+
apimeta.SetStatusCondition(&ext.Status.Conditions, metav1.Condition{
127+
Type: typ,
128+
Status: status,
129+
ObservedGeneration: ext.GetGeneration(),
130+
})
131+
132+
return cl.Update(context.TODO(), &ext)
133+
}

internal/pkg/v1/action/catalog_delete.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ func NewCatalogDelete(cfg *action.Configuration) *CatalogDelete {
2828
func (cd *CatalogDelete) Run(ctx context.Context) ([]string, error) {
2929
// validate
3030
if cd.DeleteAll && cd.CatalogName != "" {
31-
return nil, errNameAndSelector
31+
return nil, ErrNameAndSelector
3232
}
3333

3434
// delete single, specified catalog
@@ -42,7 +42,7 @@ func (cd *CatalogDelete) Run(ctx context.Context) ([]string, error) {
4242
return nil, err
4343
}
4444
if len(catatalogList.Items) == 0 {
45-
return nil, errNoResourcesFound
45+
return nil, ErrNoResourcesFound
4646
}
4747

4848
errs := make([]error, 0, len(catatalogList.Items))

internal/pkg/v1/action/errors.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package action
33
import "errors"
44

55
var (
6-
errNoResourcesFound = errors.New("no resources found")
7-
errNameAndSelector = errors.New("name cannot be provided when a selector is specified")
6+
ErrNoResourcesFound = errors.New("no resources found")
7+
ErrNameAndSelector = errors.New("name cannot be provided when a selector is specified")
8+
ErrNoChange = errors.New("no changes detected - operator already in desired state")
89
)

internal/pkg/v1/action/helpers.go

Lines changed: 37 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"context"
55
"fmt"
66
"slices"
7+
"strings"
78
"time"
89

910
apierrors "k8s.io/apimachinery/pkg/api/errors"
@@ -46,20 +47,26 @@ func waitUntilCatalogStatusCondition(
4647
})
4748
}
4849

49-
func waitForDeletion(ctx context.Context, cl client.Client, obj client.Object) error {
50-
key := objectKeyForObject(obj)
51-
if err := wait.PollUntilContextCancel(ctx, pollInterval, true, func(conditionCtx context.Context) (bool, error) {
52-
if err := cl.Get(conditionCtx, key, obj); apierrors.IsNotFound(err) {
53-
return true, nil
54-
} else if err != nil {
50+
func waitUntilOperatorStatusCondition(
51+
ctx context.Context,
52+
cl getter,
53+
operator *olmv1.ClusterExtension,
54+
conditionType string,
55+
conditionStatus metav1.ConditionStatus,
56+
) error {
57+
opKey := objectKeyForObject(operator)
58+
return wait.PollUntilContextCancel(ctx, pollInterval, true, func(conditionCtx context.Context) (bool, error) {
59+
if err := cl.Get(conditionCtx, opKey, operator); err != nil {
5560
return false, err
5661
}
57-
return false, nil
58-
}); err != nil {
59-
return fmt.Errorf("waiting for deletion: %w", err)
60-
}
6162

62-
return nil
63+
if slices.ContainsFunc(operator.Status.Conditions, func(cond metav1.Condition) bool {
64+
return cond.Type == conditionType && cond.Status == conditionStatus
65+
}) {
66+
return true, nil
67+
}
68+
return false, nil
69+
})
6370
}
6471

6572
func deleteWithTimeout(cl deleter, obj client.Object, timeout time.Duration) error {
@@ -72,3 +79,22 @@ func deleteWithTimeout(cl deleter, obj client.Object, timeout time.Duration) err
7279

7380
return nil
7481
}
82+
83+
func waitForDeletion(ctx context.Context, cl client.Client, objs ...client.Object) error {
84+
for _, obj := range objs {
85+
obj := obj
86+
lowerKind := strings.ToLower(obj.GetObjectKind().GroupVersionKind().Kind)
87+
key := objectKeyForObject(obj)
88+
if err := wait.PollUntilContextCancel(ctx, pollInterval, true, func(conditionCtx context.Context) (bool, error) {
89+
if err := cl.Get(conditionCtx, key, obj); apierrors.IsNotFound(err) {
90+
return true, nil
91+
} else if err != nil {
92+
return false, err
93+
}
94+
return false, nil
95+
}); err != nil {
96+
return fmt.Errorf("wait for %s %q deleted: %v", lowerKind, key.Name, err)
97+
}
98+
}
99+
return nil
100+
}
Lines changed: 147 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,147 @@
1+
package action
2+
3+
import (
4+
"context"
5+
"fmt"
6+
"maps"
7+
"slices"
8+
"time"
9+
10+
"github.com/blang/semver/v4"
11+
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
12+
"k8s.io/apimachinery/pkg/types"
13+
14+
olmv1 "github.com/operator-framework/operator-controller/api/v1"
15+
16+
"github.com/operator-framework/kubectl-operator/pkg/action"
17+
)
18+
19+
type OperatorUpdate struct {
20+
cfg *action.Configuration
21+
22+
Package string
23+
24+
Version string
25+
Channels []string
26+
Selector string
27+
UpgradeConstraintPolicy string
28+
Labels map[string]string
29+
OverrideUnset bool
30+
31+
CleanupTimeout time.Duration
32+
33+
Logf func(string, ...interface{})
34+
}
35+
36+
func NewOperatorUpdate(cfg *action.Configuration) *OperatorUpdate {
37+
return &OperatorUpdate{
38+
cfg: cfg,
39+
Logf: func(string, ...interface{}) {},
40+
}
41+
}
42+
43+
func (ou *OperatorUpdate) Run(ctx context.Context) (*olmv1.ClusterExtension, error) {
44+
var ext olmv1.ClusterExtension
45+
46+
opKey := types.NamespacedName{Name: ou.Package}
47+
if err := ou.cfg.Client.Get(ctx, opKey, &ext); err != nil {
48+
return nil, err
49+
}
50+
51+
if ext.Spec.Source.SourceType != olmv1.SourceTypeCatalog {
52+
return nil, fmt.Errorf("unrecognized source type: %q", ext.Spec.Source.SourceType)
53+
}
54+
55+
ou.setDefaults(ext)
56+
constraintPolicy := olmv1.UpgradeConstraintPolicy(ou.UpgradeConstraintPolicy)
57+
if !ou.needsUpdate(ext, constraintPolicy) {
58+
return nil, ErrNoChange
59+
}
60+
61+
if err := ou.prepareUpdatedExtension(&ext, constraintPolicy); err != nil {
62+
return nil, err
63+
}
64+
65+
if err := ou.cfg.Client.Update(ctx, &ext); err != nil {
66+
return nil, err
67+
}
68+
69+
if err := waitUntilOperatorStatusCondition(ctx, ou.cfg.Client, &ext, olmv1.TypeInstalled, metav1.ConditionTrue); err != nil {
70+
return nil, fmt.Errorf("timed out waiting for operator: %w", err)
71+
}
72+
73+
return &ext, nil
74+
}
75+
76+
func (ou *OperatorUpdate) setDefaults(ext olmv1.ClusterExtension) {
77+
if !ou.OverrideUnset {
78+
if ou.UpgradeConstraintPolicy == "" {
79+
ou.UpgradeConstraintPolicy = string(olmv1.UpgradeConstraintPolicyCatalogProvided)
80+
}
81+
82+
return
83+
}
84+
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+
}
94+
if ou.UpgradeConstraintPolicy == "" {
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()
102+
}
103+
}
104+
105+
func (ou *OperatorUpdate) needsUpdate(ext olmv1.ClusterExtension, constraintPolicy olmv1.UpgradeConstraintPolicy) bool {
106+
catalogSrc := ext.Spec.Source.Catalog
107+
108+
var currentSelector string
109+
if catalogSrc.Selector != nil {
110+
currentSelector = catalogSrc.Selector.String()
111+
}
112+
113+
if catalogSrc.Version == ou.Version &&
114+
slices.Equal(catalogSrc.Channels, ou.Channels) &&
115+
catalogSrc.UpgradeConstraintPolicy == constraintPolicy &&
116+
maps.Equal(ext.Labels, ou.Labels) &&
117+
currentSelector == ou.Selector {
118+
return false
119+
}
120+
121+
return true
122+
}
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+
}

0 commit comments

Comments
 (0)