|
| 1 | +package action |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "fmt" |
| 6 | + "slices" |
| 7 | + "time" |
| 8 | + |
| 9 | + "github.com/blang/semver/v4" |
| 10 | + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" |
| 11 | + "k8s.io/apimachinery/pkg/types" |
| 12 | + |
| 13 | + olmv1 "github.com/operator-framework/operator-controller/api/v1" |
| 14 | + |
| 15 | + "github.com/operator-framework/kubectl-operator/pkg/action" |
| 16 | +) |
| 17 | + |
| 18 | +type OperatorUpdate struct { |
| 19 | + config *action.Configuration |
| 20 | + |
| 21 | + Package string |
| 22 | + |
| 23 | + Version string |
| 24 | + Channels []string |
| 25 | + UpgradeConstraintPolicy string |
| 26 | + OverrideUnset bool |
| 27 | + |
| 28 | + CleanupTimeout time.Duration |
| 29 | + |
| 30 | + Logf func(string, ...interface{}) |
| 31 | +} |
| 32 | + |
| 33 | +func NewOperatorUpdate(cfg *action.Configuration) *OperatorUpdate { |
| 34 | + return &OperatorUpdate{ |
| 35 | + config: cfg, |
| 36 | + Logf: func(string, ...interface{}) {}, |
| 37 | + } |
| 38 | +} |
| 39 | + |
| 40 | +func (ou *OperatorUpdate) Run(ctx context.Context) (*olmv1.ClusterExtension, error) { |
| 41 | + var ext olmv1.ClusterExtension |
| 42 | + |
| 43 | + opKey := types.NamespacedName{Name: ou.Package} |
| 44 | + if err := ou.config.Client.Get(ctx, opKey, &ext); err != nil { |
| 45 | + return nil, err |
| 46 | + } |
| 47 | + |
| 48 | + if ext.Spec.Source.SourceType != olmv1.SourceTypeCatalog { |
| 49 | + return nil, fmt.Errorf("unrecognized source type: %q", ext.Spec.Source.SourceType) |
| 50 | + } |
| 51 | + |
| 52 | + ou.setDefaults(ext.Spec.Source.Catalog) |
| 53 | + constraintPolicy := olmv1.UpgradeConstraintPolicy(ou.UpgradeConstraintPolicy) |
| 54 | + if !ou.needsUpdate(ext.Spec.Source.Catalog, constraintPolicy) { |
| 55 | + return nil, errNoChange |
| 56 | + } |
| 57 | + |
| 58 | + if ou.Version != "" { |
| 59 | + if _, err := semver.ParseRange(ou.Version); err != nil { |
| 60 | + return nil, fmt.Errorf("failed parsing version: %w", err) |
| 61 | + } |
| 62 | + } |
| 63 | + |
| 64 | + ext.Spec.Source.Catalog.Version = ou.Version |
| 65 | + ext.Spec.Source.Catalog.Channels = ou.Channels |
| 66 | + ext.Spec.Source.Catalog.UpgradeConstraintPolicy = constraintPolicy |
| 67 | + if err := ou.config.Client.Update(ctx, &ext); err != nil { |
| 68 | + return nil, err |
| 69 | + } |
| 70 | + |
| 71 | + if err := waitUntilOperatorStatusCondition(ctx, ou.config.Client, &ext, olmv1.TypeInstalled, metav1.ConditionTrue); err != nil { |
| 72 | + return nil, fmt.Errorf("timed out waiting for operator: %w", err) |
| 73 | + } |
| 74 | + |
| 75 | + return &ext, nil |
| 76 | +} |
| 77 | + |
| 78 | +func (ou *OperatorUpdate) setDefaults(catalogSrc *olmv1.CatalogSource) { |
| 79 | + if ou.OverrideUnset { |
| 80 | + if ou.Version == "" { |
| 81 | + ou.Version = catalogSrc.Version |
| 82 | + } |
| 83 | + if len(ou.Channels) == 0 { |
| 84 | + ou.Channels = catalogSrc.Channels |
| 85 | + } |
| 86 | + if ou.UpgradeConstraintPolicy == "" { |
| 87 | + ou.UpgradeConstraintPolicy = string(catalogSrc.UpgradeConstraintPolicy) |
| 88 | + } |
| 89 | + |
| 90 | + return |
| 91 | + } |
| 92 | + |
| 93 | + if ou.UpgradeConstraintPolicy == "" { |
| 94 | + ou.UpgradeConstraintPolicy = string(olmv1.UpgradeConstraintPolicyCatalogProvided) |
| 95 | + } |
| 96 | +} |
| 97 | + |
| 98 | +func (ou *OperatorUpdate) needsUpdate(catalogSrc *olmv1.CatalogSource, constraintPolicy olmv1.UpgradeConstraintPolicy) bool { |
| 99 | + if catalogSrc.Version == ou.Version && |
| 100 | + slices.Equal(catalogSrc.Channels, ou.Channels) && |
| 101 | + catalogSrc.UpgradeConstraintPolicy == constraintPolicy { |
| 102 | + return false |
| 103 | + } |
| 104 | + |
| 105 | + return true |
| 106 | +} |
0 commit comments