-
Notifications
You must be signed in to change notification settings - Fork 215
Add CVO sync from payload #10
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
openshift-merge-robot
merged 6 commits into
openshift:master
from
abhinavdahiya:builder
Aug 27, 2018
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
6045c23
vendor: update
abhinavdahiya 697cbf6
lib: update resource{read,merge,apply} to add new objects
abhinavdahiya 04ebff0
lib: add funcs to load manifests from disk
abhinavdahiya 2d334c2
lib: add resource builder that allows Do on any lib.Manifest
abhinavdahiya 4b485ca
pkg: update sync to fetchupdatepayload and apply payload
abhinavdahiya 9527a3b
cmd: update start to add nodename
abhinavdahiya 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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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,198 @@ | ||
| package lib | ||
|
|
||
| import ( | ||
| "bytes" | ||
| "errors" | ||
| "fmt" | ||
| "io" | ||
| "io/ioutil" | ||
| "os" | ||
| "path/filepath" | ||
| "sort" | ||
|
|
||
| metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
| "k8s.io/apimachinery/pkg/apis/meta/v1/unstructured" | ||
| "k8s.io/apimachinery/pkg/runtime/schema" | ||
| utilerrors "k8s.io/apimachinery/pkg/util/errors" | ||
| "k8s.io/apimachinery/pkg/util/yaml" | ||
| "k8s.io/client-go/kubernetes/scheme" | ||
| ) | ||
|
|
||
| // Manifest stores Kubernetes object in Raw from a file. | ||
| // It stores the GroupVersionKind for the manifest. | ||
| type Manifest struct { | ||
| Raw []byte | ||
| GVK schema.GroupVersionKind | ||
|
|
||
| obj *unstructured.Unstructured | ||
| } | ||
|
|
||
| // UnmarshalJSON unmarshals bytes of single kubernetes object to Manifest. | ||
| func (m *Manifest) UnmarshalJSON(in []byte) error { | ||
| if m == nil { | ||
| return errors.New("Manifest: UnmarshalJSON on nil pointer") | ||
| } | ||
|
|
||
| // This happens when marshalling | ||
| // <yaml> | ||
| // --- (this between two `---`) | ||
| // --- | ||
| // <yaml> | ||
| if bytes.Equal(in, []byte("null")) { | ||
| m.Raw = nil | ||
| return nil | ||
| } | ||
|
|
||
| m.Raw = append(m.Raw[0:0], in...) | ||
| udi, _, err := scheme.Codecs.UniversalDecoder().Decode(in, nil, &unstructured.Unstructured{}) | ||
| if err != nil { | ||
| return fmt.Errorf("unable to decode manifest: %v", err) | ||
| } | ||
| ud, ok := udi.(*unstructured.Unstructured) | ||
| if !ok { | ||
| return fmt.Errorf("expected manifest to decode into *unstructured.Unstructured, got %T", ud) | ||
| } | ||
|
|
||
| m.GVK = ud.GroupVersionKind() | ||
| m.obj = ud.DeepCopy() | ||
| return nil | ||
| } | ||
|
|
||
| // Object returns underlying metav1.Object | ||
| func (m *Manifest) Object() metav1.Object { return m.obj } | ||
|
|
||
| const ( | ||
| // rootDirKey is used as key for the manifest files in root dir | ||
| // passed to LoadManifests | ||
| // It is set to `000` to give it more priority if the actor sorts | ||
| // based on keys. | ||
| rootDirKey = "000" | ||
| ) | ||
|
|
||
| // LoadManifests loads manifest from disk. | ||
| // | ||
| // root/ | ||
| // manifest0 | ||
| // manifest1 | ||
| // 00_subdir0/ | ||
| // manifest0 | ||
| // manifest1 | ||
| // 01_subdir1/ | ||
| // manifest0 | ||
| // manifest1 | ||
| // LoadManifests(<abs path to>/root): | ||
| // returns map | ||
| // 000: [manifest0, manifest1] | ||
| // 00_subdir0: [manifest0, manifest1] | ||
| // 01_subdir1: [manifest0, manifest1] | ||
| // | ||
| // It skips dirs that have not files. | ||
| // It only reads dir `p` and its direct subdirs. | ||
| func LoadManifests(p string) (map[string][]Manifest, error) { | ||
| var out = make(map[string][]Manifest) | ||
|
|
||
| fs, err := ioutil.ReadDir(p) | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
|
|
||
| // We want to accumulate all the errors, not returning at the | ||
| // first error encountered when reading subdirs. | ||
| var errs []error | ||
|
|
||
| // load manifest files in p to rootDirKey | ||
| ms, err := loadManifestsFromDir(p) | ||
| if err != nil { | ||
| errs = append(errs, fmt.Errorf("error loading from dir %s: %v", p, err)) | ||
| } | ||
| if len(ms) > 0 { | ||
| out[rootDirKey] = ms | ||
| } | ||
|
|
||
| // load manifests from subdirs to subdir-name | ||
| for _, f := range fs { | ||
| if !f.IsDir() { | ||
| continue | ||
| } | ||
| path := filepath.Join(p, f.Name()) | ||
| ms, err := loadManifestsFromDir(path) | ||
| if err != nil { | ||
| errs = append(errs, fmt.Errorf("error loading from dir %s: %v", path, err)) | ||
| continue | ||
| } | ||
| if len(ms) > 0 { | ||
| out[f.Name()] = ms | ||
| } | ||
| } | ||
|
|
||
| agg := utilerrors.NewAggregate(errs) | ||
| if agg != nil { | ||
| return nil, errors.New(agg.Error()) | ||
| } | ||
| return out, nil | ||
| } | ||
|
|
||
| // loadManifestsFromDir only returns files. not subdirs are traversed. | ||
| // returns manifests in increasing order of their filename. | ||
| func loadManifestsFromDir(dir string) ([]Manifest, error) { | ||
| var manifests []Manifest | ||
| fs, err := ioutil.ReadDir(dir) | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
|
|
||
| // ensure sorted. | ||
| sort.Slice(fs, func(i, j int) bool { | ||
| return fs[i].Name() < fs[j].Name() | ||
| }) | ||
|
|
||
| var errs []error | ||
| for _, f := range fs { | ||
| if f.IsDir() { | ||
| continue | ||
| } | ||
|
|
||
| path := filepath.Join(dir, f.Name()) | ||
| file, err := os.Open(path) | ||
| if err != nil { | ||
| errs = append(errs, fmt.Errorf("error opening %s: %v", path, err)) | ||
| continue | ||
| } | ||
| defer file.Close() | ||
|
|
||
| ms, err := parseManifests(file) | ||
| if err != nil { | ||
| errs = append(errs, fmt.Errorf("error parsing %s: %v", path, err)) | ||
| continue | ||
| } | ||
| manifests = append(manifests, ms...) | ||
| } | ||
|
|
||
| agg := utilerrors.NewAggregate(errs) | ||
| if agg != nil { | ||
| return nil, fmt.Errorf("error loading manifests from %q: %v", dir, agg.Error()) | ||
| } | ||
|
|
||
| return manifests, nil | ||
| } | ||
|
|
||
| // parseManifests parses a YAML or JSON document that may contain one or more | ||
| // kubernetes resources. | ||
| func parseManifests(r io.Reader) ([]Manifest, error) { | ||
| d := yaml.NewYAMLOrJSONDecoder(r, 1024) | ||
| var manifests []Manifest | ||
| for { | ||
| m := Manifest{} | ||
| if err := d.Decode(&m); err != nil { | ||
| if err == io.EOF { | ||
| return manifests, nil | ||
| } | ||
| return manifests, fmt.Errorf("error parsing: %v", err) | ||
| } | ||
| m.Raw = bytes.TrimSpace(m.Raw) | ||
| if len(m.Raw) == 0 || bytes.Equal(m.Raw, []byte("null")) { | ||
| continue | ||
| } | ||
| manifests = append(manifests, m) | ||
| } | ||
| } | ||
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Alternatively we could just force the root dir to be a real dir and ignore files in the root (treat as not manifests). The only content we had in there in the payload right now was the image mapping and the Cincinnati file.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If the update payload is
The
roothere would meanmanifestsdir.making manifests in
/manifests (or root in code)gives us a way to add things like job-migrations with highest priority above any operators.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
+1 on keeping the manifests dir, seems pretty clean to me and not much overhead.