-
Notifications
You must be signed in to change notification settings - Fork 213
Consume post install static spec capabilities #744
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,111 @@ | ||
| package capability | ||
|
|
||
| import ( | ||
| "fmt" | ||
| "strings" | ||
|
|
||
| configv1 "github.com/openshift/api/config/v1" | ||
| ) | ||
|
|
||
| const ( | ||
| CapabilityAnnotation = "capability.openshift.io/name" | ||
|
|
||
| DefaultCapabilitySet = configv1.ClusterVersionCapabilitySetCurrent | ||
| ) | ||
|
|
||
| type ClusterCapabilities struct { | ||
| KnownCapabilities map[configv1.ClusterVersionCapability]struct{} | ||
| EnabledCapabilities map[configv1.ClusterVersionCapability]struct{} | ||
| } | ||
|
|
||
| // SetCapabilities populates and returns cluster capabilities from ClusterVersion capabilities spec. | ||
| func SetCapabilities(config *configv1.ClusterVersion) ClusterCapabilities { | ||
| var capabilities ClusterCapabilities | ||
| capabilities.KnownCapabilities = setKnownCapabilities(config) | ||
| capabilities.EnabledCapabilities = setEnabledCapabilities(config) | ||
| return capabilities | ||
| } | ||
|
|
||
| // GetCapabilitiesStatus populates and returns ClusterVersion capabilities status from given capabilities. | ||
| func GetCapabilitiesStatus(capabilities ClusterCapabilities) configv1.ClusterVersionCapabilitiesStatus { | ||
| var status configv1.ClusterVersionCapabilitiesStatus | ||
| for k := range capabilities.EnabledCapabilities { | ||
| status.EnabledCapabilities = append(status.EnabledCapabilities, k) | ||
| } | ||
| for k := range capabilities.KnownCapabilities { | ||
| status.KnownCapabilities = append(status.KnownCapabilities, k) | ||
| } | ||
| return status | ||
| } | ||
|
|
||
| // CheckResourceEnablement, given resource annotations and defined cluster capabilities, checks if the capability | ||
| // annotation exists. If so, each capability name is validated against the known set of capabilities. Each valid | ||
| // capability is then checked if it is disabled. If any invalid capabilities are found an error is returned listing | ||
| // all invalid capabilities. Otherwise, if any disabled capabilities are found an error is returned listing all | ||
| // disabled capabilities. | ||
| func CheckResourceEnablement(annotations map[string]string, capabilities ClusterCapabilities) error { | ||
| val, ok := annotations[CapabilityAnnotation] | ||
| if !ok { | ||
| return nil | ||
| } | ||
| caps := strings.Split(val, "+") | ||
| numCaps := len(caps) | ||
| unknownCaps := make([]string, 0, numCaps) | ||
| disabledCaps := make([]string, 0, numCaps) | ||
|
|
||
| for _, c := range caps { | ||
| if _, ok = capabilities.KnownCapabilities[configv1.ClusterVersionCapability(c)]; !ok { | ||
| unknownCaps = append(unknownCaps, c) | ||
| } else if _, ok = capabilities.EnabledCapabilities[configv1.ClusterVersionCapability(c)]; !ok { | ||
| disabledCaps = append(disabledCaps, c) | ||
| } | ||
| } | ||
| if len(unknownCaps) > 0 { | ||
| return fmt.Errorf("unrecognized capability names: %s", strings.Join(unknownCaps, ", ")) | ||
| } | ||
| if len(disabledCaps) > 0 { | ||
| return fmt.Errorf("disabled capabilities: %s", strings.Join(disabledCaps, ", ")) | ||
| } | ||
| return nil | ||
| } | ||
|
|
||
| // setKnownCapabilities populates a map keyed by capability from all known capabilities as defined in ClusterVersion. | ||
| func setKnownCapabilities(config *configv1.ClusterVersion) map[configv1.ClusterVersionCapability]struct{} { | ||
| known := make(map[configv1.ClusterVersionCapability]struct{}) | ||
|
|
||
| for _, v := range configv1.ClusterVersionCapabilitySets { | ||
| for _, capability := range v { | ||
| if _, ok := known[capability]; ok { | ||
| continue | ||
| } | ||
| known[capability] = struct{}{} | ||
| } | ||
| } | ||
| return known | ||
| } | ||
|
|
||
| // setEnabledCapabilities populates a map keyed by capability from all enabled capabilities as defined in ClusterVersion. | ||
| // DefaultCapabilitySet is used if a baseline capability set is not defined by ClusterVersion. | ||
| func setEnabledCapabilities(config *configv1.ClusterVersion) map[configv1.ClusterVersionCapability]struct{} { | ||
| enabled := make(map[configv1.ClusterVersionCapability]struct{}) | ||
|
|
||
| capSet := DefaultCapabilitySet | ||
|
|
||
| if config.Spec.Capabilities != nil && len(config.Spec.Capabilities.BaselineCapabilitySet) > 0 { | ||
| capSet = config.Spec.Capabilities.BaselineCapabilitySet | ||
| } | ||
|
|
||
| for _, v := range configv1.ClusterVersionCapabilitySets[capSet] { | ||
| enabled[v] = struct{}{} | ||
| } | ||
|
|
||
| if config.Spec.Capabilities != nil { | ||
| for _, v := range config.Spec.Capabilities.AdditionalEnabledCapabilities { | ||
| if _, ok := enabled[v]; ok { | ||
| continue | ||
| } | ||
| enabled[v] = struct{}{} | ||
| } | ||
| } | ||
| return enabled | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.