|
| 1 | +package controller |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "encoding/json" |
| 6 | + "errors" |
| 7 | + "fmt" |
| 8 | + "sort" |
| 9 | + "strings" |
| 10 | + |
| 11 | + semver "github.com/blang/semver/v4" |
| 12 | + "github.com/go-logr/logr" |
| 13 | + operatorv1 "github.com/openshift/api/operator/v1" |
| 14 | + "github.com/openshift/cluster-olm-operator/internal/utils" |
| 15 | + "github.com/openshift/cluster-olm-operator/pkg/clients" |
| 16 | + "github.com/openshift/library-go/pkg/controller/factory" |
| 17 | + "github.com/openshift/library-go/pkg/operator/events" |
| 18 | + "github.com/openshift/library-go/pkg/operator/v1helpers" |
| 19 | + storage "github.com/operator-framework/helm-operator-plugins/pkg/storage" |
| 20 | + "github.com/operator-framework/operator-registry/alpha/property" |
| 21 | + helm "helm.sh/helm/v3/pkg/storage" |
| 22 | + "helm.sh/helm/v3/pkg/storage/driver" |
| 23 | + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" |
| 24 | + "k8s.io/apimachinery/pkg/labels" |
| 25 | + "k8s.io/client-go/kubernetes" |
| 26 | + v1 "k8s.io/client-go/kubernetes/typed/core/v1" |
| 27 | + "k8s.io/klog/v2" |
| 28 | +) |
| 29 | + |
| 30 | +const ( |
| 31 | + reasonIncompatibleOperatorsInstalled = "IncompatibleOperatorsInstalled" |
| 32 | + typeIncompatibelOperatorsUpgradeable = "InstalledOLMOperatorsUpgradeable" |
| 33 | + reasonInvalidBundleProperties = "InvalidBundleProperties" |
| 34 | + maxOpenShiftVersionProperty = "olm.maxOpenShiftVersion" |
| 35 | + ownerKindKey = "olm.operatorframework.io/owner-kind" |
| 36 | + ownerNameKey = "olm.operatorframework.io/owner-name" |
| 37 | + packageNameKey = "olm.operatorframework.io/package-name" |
| 38 | + bundleNameKey = "olm.operatorframework.io/bundle-name" |
| 39 | + bundleVersionKey = "olm.operatorframework.io/bundle-version" |
| 40 | +) |
| 41 | + |
| 42 | +type incompatibleOperatorController struct { |
| 43 | + name string |
| 44 | + nextOCPMinorVersion *semver.Version |
| 45 | + kubeclient kubernetes.Interface |
| 46 | + clusterExtensionClient *clients.ClusterExtensionClient |
| 47 | + operatorClient *clients.OperatorClient |
| 48 | + logger logr.Logger |
| 49 | +} |
| 50 | + |
| 51 | +func NewIncompatibleOperatorController(name string, nextOCPMinorVersion *semver.Version, kubeclient kubernetes.Interface, clusterExtensionClient *clients.ClusterExtensionClient, operatorClient *clients.OperatorClient, eventRecorder events.Recorder) factory.Controller { |
| 52 | + c := &incompatibleOperatorController{ |
| 53 | + name: name, |
| 54 | + nextOCPMinorVersion: nextOCPMinorVersion, |
| 55 | + kubeclient: kubeclient, |
| 56 | + clusterExtensionClient: clusterExtensionClient, |
| 57 | + operatorClient: operatorClient, |
| 58 | + logger: klog.NewKlogr().WithName(name), |
| 59 | + } |
| 60 | + |
| 61 | + return factory.New().WithSync(c.sync).WithSyncDegradedOnError(operatorClient).WithInformers(operatorClient.Informer(), clusterExtensionClient.Informer().Informer()).ToController(name, eventRecorder) |
| 62 | +} |
| 63 | + |
| 64 | +func (c *incompatibleOperatorController) sync(ctx context.Context, _ factory.SyncContext) error { |
| 65 | + c.logger.Info("sync started") |
| 66 | + defer c.logger.Info("sync finished") |
| 67 | + |
| 68 | + var updateStatusFn v1helpers.UpdateStatusFunc |
| 69 | + incompatibleOperators, err := c.getIncompatibleOperators() |
| 70 | + if len(incompatibleOperators) > 0 { |
| 71 | + updateStatusFn = v1helpers.UpdateConditionFn(operatorv1.OperatorCondition{ |
| 72 | + Type: typeIncompatibelOperatorsUpgradeable, |
| 73 | + Status: operatorv1.ConditionFalse, |
| 74 | + Reason: reasonIncompatibleOperatorsInstalled, |
| 75 | + Message: strings.Join(incompatibleOperators, ","), |
| 76 | + }) |
| 77 | + } else { |
| 78 | + if err != nil { |
| 79 | + updateStatusFn = v1helpers.UpdateConditionFn(operatorv1.OperatorCondition{ |
| 80 | + Type: typeIncompatibelOperatorsUpgradeable, |
| 81 | + Status: operatorv1.ConditionFalse, |
| 82 | + Reason: reasonInvalidBundleProperties, |
| 83 | + Message: err.Error(), |
| 84 | + }) |
| 85 | + } else { |
| 86 | + updateStatusFn = v1helpers.UpdateConditionFn(operatorv1.OperatorCondition{ |
| 87 | + Type: typeIncompatibelOperatorsUpgradeable, |
| 88 | + Status: operatorv1.ConditionTrue, |
| 89 | + }) |
| 90 | + } |
| 91 | + } |
| 92 | + |
| 93 | + if _, _, updateErr := v1helpers.UpdateStatus(ctx, c.operatorClient, updateStatusFn); updateErr != nil { |
| 94 | + c.logger.V(4).Info(fmt.Sprintf("Error updating operator condition status: %v", updateErr)) |
| 95 | + return updateErr |
| 96 | + } |
| 97 | + return err |
| 98 | +} |
| 99 | + |
| 100 | +func (c *incompatibleOperatorController) getIncompatibleOperators() ([]string, error) { |
| 101 | + var incompatibleOperators []string |
| 102 | + |
| 103 | + ceList, err := c.clusterExtensionClient.Informer().Lister().List(labels.NewSelector()) |
| 104 | + if err != nil { |
| 105 | + c.logger.V(4).Error(err, "Error listing cluster extensions") |
| 106 | + return nil, err |
| 107 | + } |
| 108 | + |
| 109 | + store := c.buildHelmStore(c.kubeclient.CoreV1().Secrets("openshift-operator-controller")) |
| 110 | + |
| 111 | + var errs []error |
| 112 | + // Get all ClusterExtensions incompatible with next Y-stream |
| 113 | + for _, obj := range ceList { |
| 114 | + metaObj, ok := obj.(metav1.Object) |
| 115 | + if !ok { |
| 116 | + errs = append(errs, fmt.Errorf("metav1.Object type assertion failed for object %v", obj)) |
| 117 | + continue |
| 118 | + } |
| 119 | + name := metaObj.GetName() |
| 120 | + rel, err := store.Deployed(name) |
| 121 | + if errors.Is(err, driver.ErrNoDeployedReleases) { |
| 122 | + c.logger.Info("Cluster Extension not yet deployed - will check again later") |
| 123 | + continue |
| 124 | + } |
| 125 | + if err != nil { |
| 126 | + c.logger.V(4).Error(err, "Error returning the last deployed release") |
| 127 | + return nil, err |
| 128 | + } |
| 129 | + |
| 130 | + if rel.Chart == nil || rel.Chart.Metadata == nil { |
| 131 | + continue |
| 132 | + } |
| 133 | + props, err := propertyListFromPropertiesAnnotation(rel.Chart.Metadata.Annotations["olm.properties"]) |
| 134 | + if err != nil { |
| 135 | + c.logger.V(4).Error(err, "Error converting olm.properties") |
| 136 | + return nil, err |
| 137 | + } |
| 138 | + numMaxOCPProps := 0 |
| 139 | + for _, p := range props { |
| 140 | + if p.Type == maxOpenShiftVersionProperty { |
| 141 | + numMaxOCPProps++ |
| 142 | + maxOCPVersion, err := utils.ToAllowedSemver(string(p.Value)) |
| 143 | + if err != nil { |
| 144 | + errs = append(errs, fmt.Errorf(fmt.Sprintf("Error converting to semver for version %s: %v", string(p.Value), err))) |
| 145 | + continue |
| 146 | + } |
| 147 | + if numMaxOCPProps > 1 { |
| 148 | + errs = append(errs, fmt.Errorf("%s has more than one %s", name, maxOpenShiftVersionProperty)) |
| 149 | + continue |
| 150 | + } |
| 151 | + if maxOCPVersion != nil && !maxOCPVersion.GTE(*c.nextOCPMinorVersion) { |
| 152 | + // Incompatible |
| 153 | + incompatibleOperators = append(incompatibleOperators, rel.Labels[bundleNameKey]) |
| 154 | + } |
| 155 | + } |
| 156 | + } |
| 157 | + } |
| 158 | + |
| 159 | + // deterministic ordering |
| 160 | + sort.Strings(incompatibleOperators) |
| 161 | + |
| 162 | + return incompatibleOperators, errors.Join(errs...) |
| 163 | +} |
| 164 | + |
| 165 | +func propertyListFromPropertiesAnnotation(raw string) ([]property.Property, error) { |
| 166 | + var props []property.Property |
| 167 | + if err := json.Unmarshal([]byte(raw), &props); err != nil { |
| 168 | + return nil, fmt.Errorf("failed to unmarshal properties annotation: %w", err) |
| 169 | + } |
| 170 | + return props, nil |
| 171 | +} |
| 172 | + |
| 173 | +func (c *incompatibleOperatorController) buildHelmStore(secretClient v1.SecretInterface) helm.Storage { |
| 174 | + log := func(s string, args ...interface{}) { c.logger.V(4).Info(fmt.Sprintf(s, args...)) } |
| 175 | + csConfig := storage.ChunkedSecretsConfig{Log: log} |
| 176 | + |
| 177 | + return helm.Storage{ |
| 178 | + Driver: storage.NewChunkedSecrets(secretClient, "operator-controller", csConfig), |
| 179 | + Log: log, |
| 180 | + } |
| 181 | +} |
0 commit comments