-
Notifications
You must be signed in to change notification settings - Fork 566
[WIP] Add cluster runtime constraint handling #2498
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
Changes from all commits
bb47c5f
6b876a0
73a56dc
de8afda
49ff255
ce65ef2
c9cd2a7
6e0465e
3576e84
ac67d37
6324417
13d3679
8bf42c5
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,91 @@ | ||
| package runtime_constraints | ||
|
|
||
| import ( | ||
| "encoding/json" | ||
| "io/ioutil" | ||
| "os" | ||
|
|
||
| "github.com/operator-framework/operator-lifecycle-manager/pkg/controller/registry/resolver/cache" | ||
| "github.com/operator-framework/operator-registry/pkg/registry" | ||
| "github.com/pkg/errors" | ||
| ) | ||
|
|
||
| const ( | ||
| MaxRuntimeConstraints = 10 | ||
| RuntimeConstraintEnvVarName = "RUNTIME_CONSTRAINTS" | ||
| ) | ||
|
|
||
| type RuntimeConstraintsProvider struct { | ||
| runtimeConstraints []cache.Predicate | ||
| } | ||
|
|
||
| func (p *RuntimeConstraintsProvider) Constraints() []cache.Predicate { | ||
| return p.runtimeConstraints | ||
| } | ||
|
|
||
| func New(runtimeConstraints []cache.Predicate) (*RuntimeConstraintsProvider, error) { | ||
| if len(runtimeConstraints) >= MaxRuntimeConstraints { | ||
| return nil, errors.Errorf("Too many runtime constraints defined (%d/%d)", len(runtimeConstraints), MaxRuntimeConstraints) | ||
| } | ||
|
|
||
| return &RuntimeConstraintsProvider{ | ||
| runtimeConstraints: runtimeConstraints, | ||
| }, nil | ||
| } | ||
|
|
||
| func NewFromEnv() (*RuntimeConstraintsProvider, error) { | ||
| runtimeConstraintsFilePath, isSet := os.LookupEnv(RuntimeConstraintEnvVarName) | ||
| if !isSet { | ||
| return nil, nil | ||
| } | ||
| return NewFromFile(runtimeConstraintsFilePath) | ||
| } | ||
|
|
||
| func NewFromFile(runtimeConstraintsFilePath string) (*RuntimeConstraintsProvider, error) { | ||
| propertiesFile, err := readRuntimeConstraintsYaml(runtimeConstraintsFilePath) | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
|
|
||
| // Using package type to test with | ||
| // We may only want to allow the generic constraint types once they are readym | ||
| var constraints = make([]cache.Predicate, 0) | ||
| for _, property := range propertiesFile.Properties { | ||
perdasilva marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| rawMessage := []byte(property.Value) | ||
| switch property.Type { | ||
| case registry.PackageType: | ||
| dep := registry.PackageDependency{} | ||
| err := json.Unmarshal(rawMessage, &dep) | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
| constraints = append(constraints, cache.PkgPredicate(dep.PackageName)) | ||
| case registry.LabelType: | ||
| dep := registry.LabelDependency{} | ||
| err := json.Unmarshal(rawMessage, &dep) | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
| constraints = append(constraints, cache.LabelPredicate(dep.Label)) | ||
| } | ||
| } | ||
|
|
||
| return New(constraints) | ||
| } | ||
|
|
||
| func readRuntimeConstraintsYaml(yamlPath string) (*registry.PropertiesFile, error) { | ||
| // Read file | ||
| yamlFile, err := ioutil.ReadFile(yamlPath) | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Did you test how this file-reading code will work in real life? How is this file gonna be "injected" into the process?
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I've tested it manually. I still need to add unit tests for this. I'm thinking the way to side-load it should be we have an environment variable that points to the path of the constraints file. Downstream, the file will be baked in to the olm operator image and we'll update the deployment to include the env var. This way, if people want to turn it off, it's doable. I'm not sure how this strategy fits with support, etc. E.g. if they turn it off would that mean the customer is in an unmanaged state?
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
it does |
||
| if err != nil { | ||
| return nil, err | ||
| } | ||
|
|
||
| // Parse yaml | ||
| var propertiesFile = ®istry.PropertiesFile{} | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We need to decide if this |
||
| err = json.Unmarshal(yamlFile, propertiesFile) | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
|
|
||
| return propertiesFile, nil | ||
| } | ||
Uh oh!
There was an error while loading. Please reload this page.