|
| 1 | +// Package capability_v2 collects functions about capabilities that are going to be lifted to library-go |
| 2 | +// https://github.com/openshift/library-go |
| 3 | +// Thus it should not depend on any packages from CVO: github.com/openshift/cluster-version-operator/ |
| 4 | +package capability_v2 |
| 5 | + |
| 6 | +import ( |
| 7 | + configv1 "github.com/openshift/api/config/v1" |
| 8 | + "github.com/openshift/library-go/pkg/manifest" |
| 9 | + |
| 10 | + "k8s.io/apimachinery/pkg/util/sets" |
| 11 | + "k8s.io/klog/v2" |
| 12 | +) |
| 13 | + |
| 14 | +// ManifestInclusionConfiguration configures manifest inclusion, so |
| 15 | +// callers can opt in to new filtering options instead of having to |
| 16 | +// update existing call-sites, even if they do not need a new |
| 17 | +// filtering option. |
| 18 | +type ManifestInclusionConfiguration struct { |
| 19 | + // ExcludeIdentifier, if non-nil, excludes manifests that match the exclusion identifier. |
| 20 | + ExcludeIdentifier *string |
| 21 | + |
| 22 | + // RequiredFeatureSet, if non-nil, excludes manifests unless they match the desired feature set. |
| 23 | + RequiredFeatureSet *string |
| 24 | + |
| 25 | + // Profile, if non-nil, excludes manifests unless they match the cluster profile. |
| 26 | + Profile *string |
| 27 | + |
| 28 | + // Capabilities, if non-nil, excludes manifests unless they match the enabled cluster capabilities. |
| 29 | + Capabilities *configv1.ClusterVersionCapabilitiesStatus |
| 30 | + |
| 31 | + // Overrides excludes manifests for overridden resources. |
| 32 | + Overrides []configv1.ComponentOverride |
| 33 | + |
| 34 | + // Platform, if non-nil, excludes CredentialsRequests manifests unless they match the infrastructure platform. |
| 35 | + Platform *string |
| 36 | +} |
| 37 | + |
| 38 | +// GetImplicitlyEnabledCapabilities returns a set of capabilities that are implicitly enabled after a cluster update. |
| 39 | +// The arguments are two sets of manifests, manifest inclusion configuration, and |
| 40 | +// a set of capabilities that are implicitly enabled on the cluster, i.e., the capabilities |
| 41 | +// that are NOT specified in the cluster version but has to considered enabled on the cluster. |
| 42 | +// The manifest inclusion configuration is used to determine if a manifest should be included. |
| 43 | +// In other words, whether, or not the cluster version operator reconcile that manifest on the cluster. |
| 44 | +// The two sets of manifests are respectively from the release that is currently running on the cluster and |
| 45 | +// from the release that the cluster is updated to. |
| 46 | +func GetImplicitlyEnabledCapabilities( |
| 47 | + updatePayloadManifests []manifest.Manifest, |
| 48 | + currentPayloadManifests []manifest.Manifest, |
| 49 | + manifestInclusionConfiguration ManifestInclusionConfiguration, |
| 50 | + currentImplicitlyEnabled sets.Set[configv1.ClusterVersionCapability], |
| 51 | +) sets.Set[configv1.ClusterVersionCapability] { |
| 52 | + |
| 53 | + ret := currentImplicitlyEnabled.Clone() |
| 54 | + for _, updateManifest := range updatePayloadManifests { |
| 55 | + updateManErr := updateManifest.IncludeAllowUnknownCapabilities( |
| 56 | + manifestInclusionConfiguration.ExcludeIdentifier, |
| 57 | + manifestInclusionConfiguration.RequiredFeatureSet, |
| 58 | + manifestInclusionConfiguration.Profile, |
| 59 | + manifestInclusionConfiguration.Capabilities, |
| 60 | + manifestInclusionConfiguration.Overrides, |
| 61 | + true, |
| 62 | + ) |
| 63 | + // update manifest is enabled, no need to check |
| 64 | + if updateManErr == nil { |
| 65 | + continue |
| 66 | + } |
| 67 | + for _, currentManifest := range currentPayloadManifests { |
| 68 | + if !updateManifest.SameResourceID(currentManifest) { |
| 69 | + continue |
| 70 | + } |
| 71 | + // current manifest is disabled, no need to check |
| 72 | + if err := currentManifest.IncludeAllowUnknownCapabilities( |
| 73 | + manifestInclusionConfiguration.ExcludeIdentifier, |
| 74 | + manifestInclusionConfiguration.RequiredFeatureSet, |
| 75 | + manifestInclusionConfiguration.Profile, |
| 76 | + manifestInclusionConfiguration.Capabilities, |
| 77 | + manifestInclusionConfiguration.Overrides, |
| 78 | + true, |
| 79 | + ); err != nil { |
| 80 | + continue |
| 81 | + } |
| 82 | + newImplicitlyEnabled := sets.New[configv1.ClusterVersionCapability](updateManifest.GetManifestCapabilities()...). |
| 83 | + Difference(sets.New[configv1.ClusterVersionCapability](currentManifest.GetManifestCapabilities()...)). |
| 84 | + Difference(currentImplicitlyEnabled). |
| 85 | + Difference(sets.New[configv1.ClusterVersionCapability](manifestInclusionConfiguration.Capabilities.EnabledCapabilities...)) |
| 86 | + ret = ret.Union(newImplicitlyEnabled) |
| 87 | + klog.V(2).Infof("%s has changed and is now part of one or more disabled capabilities. The following capabilities will be implicitly enabled: %s", |
| 88 | + updateManifest.String(), newImplicitlyEnabled.UnsortedList()) |
| 89 | + } |
| 90 | + } |
| 91 | + return ret |
| 92 | +} |
0 commit comments