|
| 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 | + // parsedSelector is used internally to avoid potentially costly transformations |
| 28 | + // between string and metav1.LabelSelector formats |
| 29 | + parsedSelector *metav1.LabelSelector |
| 30 | + UpgradeConstraintPolicy string |
| 31 | + Labels map[string]string |
| 32 | + IgnoreUnset bool |
| 33 | + |
| 34 | + CleanupTimeout time.Duration |
| 35 | + |
| 36 | + Logf func(string, ...interface{}) |
| 37 | +} |
| 38 | + |
| 39 | +func NewOperatorUpdate(cfg *action.Configuration) *OperatorUpdate { |
| 40 | + return &OperatorUpdate{ |
| 41 | + cfg: cfg, |
| 42 | + Logf: func(string, ...interface{}) {}, |
| 43 | + } |
| 44 | +} |
| 45 | + |
| 46 | +func (ou *OperatorUpdate) Run(ctx context.Context) (*olmv1.ClusterExtension, error) { |
| 47 | + var ext olmv1.ClusterExtension |
| 48 | + var err error |
| 49 | + |
| 50 | + opKey := types.NamespacedName{Name: ou.Package} |
| 51 | + if err = ou.cfg.Client.Get(ctx, opKey, &ext); err != nil { |
| 52 | + return nil, err |
| 53 | + } |
| 54 | + |
| 55 | + if ext.Spec.Source.SourceType != olmv1.SourceTypeCatalog { |
| 56 | + return nil, fmt.Errorf("unrecognized source type: %q", ext.Spec.Source.SourceType) |
| 57 | + } |
| 58 | + |
| 59 | + ou.setDefaults(ext) |
| 60 | + |
| 61 | + if ou.Version != "" { |
| 62 | + if _, err = semver.ParseRange(ou.Version); err != nil { |
| 63 | + return nil, fmt.Errorf("failed parsing version: %w", err) |
| 64 | + } |
| 65 | + } |
| 66 | + if ou.Selector != "" && ou.parsedSelector == nil { |
| 67 | + ou.parsedSelector, err = metav1.ParseToLabelSelector(ou.Selector) |
| 68 | + if err != nil { |
| 69 | + return nil, fmt.Errorf("failed parsing selector: %w", err) |
| 70 | + } |
| 71 | + } |
| 72 | + |
| 73 | + constraintPolicy := olmv1.UpgradeConstraintPolicy(ou.UpgradeConstraintPolicy) |
| 74 | + if !ou.needsUpdate(ext, constraintPolicy) { |
| 75 | + return nil, ErrNoChange |
| 76 | + } |
| 77 | + |
| 78 | + ou.prepareUpdatedExtension(&ext, constraintPolicy) |
| 79 | + if err := ou.cfg.Client.Update(ctx, &ext); err != nil { |
| 80 | + return nil, err |
| 81 | + } |
| 82 | + |
| 83 | + if err := waitUntilOperatorStatusCondition(ctx, ou.cfg.Client, &ext, olmv1.TypeInstalled, metav1.ConditionTrue); err != nil { |
| 84 | + return nil, fmt.Errorf("timed out waiting for operator: %w", err) |
| 85 | + } |
| 86 | + |
| 87 | + return &ext, nil |
| 88 | +} |
| 89 | + |
| 90 | +func (ou *OperatorUpdate) setDefaults(ext olmv1.ClusterExtension) { |
| 91 | + if !ou.IgnoreUnset { |
| 92 | + if ou.UpgradeConstraintPolicy == "" { |
| 93 | + ou.UpgradeConstraintPolicy = string(olmv1.UpgradeConstraintPolicyCatalogProvided) |
| 94 | + } |
| 95 | + |
| 96 | + return |
| 97 | + } |
| 98 | + |
| 99 | + // IgnoreUnset is enabled |
| 100 | + // set all unset values to what they are on the current object |
| 101 | + catalogSrc := ext.Spec.Source.Catalog |
| 102 | + if ou.Version == "" { |
| 103 | + ou.Version = catalogSrc.Version |
| 104 | + } |
| 105 | + if len(ou.Channels) == 0 { |
| 106 | + ou.Channels = catalogSrc.Channels |
| 107 | + } |
| 108 | + if ou.UpgradeConstraintPolicy == "" { |
| 109 | + ou.UpgradeConstraintPolicy = string(catalogSrc.UpgradeConstraintPolicy) |
| 110 | + } |
| 111 | + if len(ou.Labels) == 0 { |
| 112 | + ou.Labels = ext.Labels |
| 113 | + } |
| 114 | + if ou.Selector == "" && catalogSrc.Selector != nil { |
| 115 | + ou.parsedSelector = catalogSrc.Selector |
| 116 | + } |
| 117 | +} |
| 118 | + |
| 119 | +func (ou *OperatorUpdate) needsUpdate(ext olmv1.ClusterExtension, constraintPolicy olmv1.UpgradeConstraintPolicy) bool { |
| 120 | + catalogSrc := ext.Spec.Source.Catalog |
| 121 | + |
| 122 | + // object string form is used for comparison to: |
| 123 | + // - remove the need for potentially costly metav1.FormatLabelSelector calls |
| 124 | + // - avoid having to handle potential reordering of items from on cluster state |
| 125 | + sameSelectors := (catalogSrc.Selector == nil && ou.parsedSelector == nil) || |
| 126 | + (catalogSrc.Selector != nil && ou.parsedSelector != nil && |
| 127 | + catalogSrc.Selector.String() == ou.parsedSelector.String()) |
| 128 | + |
| 129 | + if catalogSrc.Version == ou.Version && |
| 130 | + slices.Equal(catalogSrc.Channels, ou.Channels) && |
| 131 | + catalogSrc.UpgradeConstraintPolicy == constraintPolicy && |
| 132 | + maps.Equal(ext.Labels, ou.Labels) && |
| 133 | + sameSelectors { |
| 134 | + return false |
| 135 | + } |
| 136 | + |
| 137 | + return true |
| 138 | +} |
| 139 | + |
| 140 | +func (ou *OperatorUpdate) prepareUpdatedExtension(ext *olmv1.ClusterExtension, constraintPolicy olmv1.UpgradeConstraintPolicy) { |
| 141 | + ext.SetLabels(ou.Labels) |
| 142 | + ext.Spec.Source.Catalog.Version = ou.Version |
| 143 | + ext.Spec.Source.Catalog.Selector = ou.parsedSelector |
| 144 | + ext.Spec.Source.Catalog.Channels = ou.Channels |
| 145 | + ext.Spec.Source.Catalog.UpgradeConstraintPolicy = constraintPolicy |
| 146 | +} |
0 commit comments