-
Notifications
You must be signed in to change notification settings - Fork 171
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
Entrypoint: Support for multiple pods and multiple stages #1824
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
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 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 |
---|---|---|
@@ -1,2 +1,4 @@ | ||
bin/* | ||
srv/* | ||
.vscode/* | ||
**/.minio.sys/* |
This file contains 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 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 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 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,195 @@ | ||
// Copyright 2020 Red Hat, Inc. | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package cosa | ||
|
||
import ( | ||
"encoding/json" | ||
"fmt" | ||
"io" | ||
"io/ioutil" | ||
"os" | ||
"path/filepath" | ||
"reflect" | ||
"runtime" | ||
"strings" | ||
|
||
"github.com/pkg/errors" | ||
) | ||
|
||
var ( | ||
// ErrMetaFailsValidation is thrown on reading and invalid meta.json | ||
ErrMetaFailsValidation = errors.New("meta.json failed schema validation") | ||
|
||
// ErrMetaNotFound is thrown when a meta.json cannot be found | ||
ErrMetaNotFound = errors.New("meta.json was not found") | ||
) | ||
|
||
const ( | ||
// CosaMetaJSON is the meta.json file | ||
CosaMetaJSON = "meta.json" | ||
) | ||
|
||
// BuilderArch converts the GOARCH to the build arch. | ||
// In other words, it translates amd64 to x86_64. | ||
func BuilderArch() string { | ||
arch := runtime.GOARCH | ||
if arch == "amd64" { | ||
arch = "x86_64" | ||
} | ||
return arch | ||
} | ||
|
||
// ReadBuild returns a build upon finding a meta.json. | ||
// If build is "", use latest | ||
func ReadBuild(dir, buildID, arch string) (*Build, string, error) { | ||
if arch == "" { | ||
arch = BuilderArch() | ||
} | ||
|
||
if buildID == "" { | ||
b, err := getBuilds(dir) | ||
if err == nil { | ||
latest, ok := b.getLatest(arch) | ||
if !ok { | ||
return nil, "", ErrNoBuildsFound | ||
} | ||
buildID = latest | ||
} | ||
} | ||
|
||
if buildID == "" { | ||
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. Can this code be moved to the block above? |
||
return nil, "", fmt.Errorf("build is undefined") | ||
} | ||
|
||
p := filepath.Join(dir, "builds", buildID, arch) | ||
f, err := os.Open(filepath.Join(p, CosaMetaJSON)) | ||
if err != nil { | ||
return nil, "", fmt.Errorf("failed to open %s to read meta.json: %w", p, err) | ||
} | ||
|
||
b, err := buildParser(f) | ||
return b, p, err | ||
} | ||
|
||
func buildParser(r io.Reader) (*Build, error) { | ||
dec := json.NewDecoder(r) | ||
dec.DisallowUnknownFields() | ||
var cosaBuild *Build | ||
if err := dec.Decode(&cosaBuild); err != nil { | ||
return nil, errors.Wrapf(err, "failed to parse build") | ||
} | ||
if errs := cosaBuild.Validate(); len(errs) > 0 { | ||
return nil, errors.Wrapf(ErrMetaFailsValidation, "%v", errs) | ||
} | ||
return cosaBuild, nil | ||
} | ||
|
||
// ParseBuild parses the meta.json and reutrns a build | ||
func ParseBuild(path string) (*Build, error) { | ||
f, err := os.Open(path) | ||
if err != nil { | ||
return nil, errors.Wrapf(err, "failed to open %s", path) | ||
} | ||
defer f.Close() | ||
b, err := buildParser(f) | ||
if err != nil { | ||
return nil, errors.Wrapf(err, "failed parsing of %s", path) | ||
} | ||
return b, err | ||
} | ||
|
||
// WriteMeta records the meta-data | ||
func (build *Build) WriteMeta(path string, validate bool) error { | ||
if validate { | ||
if err := build.Validate(); len(err) != 0 { | ||
return errors.New("data is not compliant with schema") | ||
} | ||
} | ||
out, err := json.MarshalIndent(build, "", " ") | ||
if err != nil { | ||
return err | ||
} | ||
return ioutil.WriteFile(path, out, 0644) | ||
} | ||
|
||
// GetArtifact returns an artifact by JSON tag | ||
func (build *Build) GetArtifact(artifact string) (*Artifact, error) { | ||
r, ok := build.artifacts()[artifact] | ||
if ok { | ||
return r, nil | ||
} | ||
return nil, errors.New("artifact not defined") | ||
} | ||
|
||
// IsArtifact takes a path and returns the artifact type and a bool if | ||
// the artifact is described in the build. | ||
func (build *Build) IsArtifact(path string) (string, bool) { | ||
path = filepath.Base(path) | ||
for k, v := range build.artifacts() { | ||
if v.Path == path { | ||
return k, true | ||
} | ||
} | ||
return "", false | ||
} | ||
|
||
// CanArtifact reports whether an artifact name is buildable by COSA based | ||
// on the meta.json name. CanArtifact is used to signal if the artifact is a known | ||
// artifact type. | ||
func CanArtifact(artifact string) bool { | ||
b := new(Build) | ||
b.BuildArtifacts = new(BuildArtifacts) | ||
_, ok := b.artifacts()[artifact] | ||
return ok | ||
} | ||
|
||
// artifact returns a string map of artifacts, where the key | ||
// is the JSON tag. Reflection was over a case statement to make meta.json | ||
// and the schema authoritative for adding and removing artifacts. | ||
func (build *Build) artifacts() map[string]*Artifact { | ||
ret := make(map[string]*Artifact) | ||
var ba BuildArtifacts = *build.BuildArtifacts | ||
rv := reflect.TypeOf(ba) | ||
for i := 0; i < rv.NumField(); i++ { | ||
tag := rv.Field(i).Tag.Get("json") | ||
tag = strings.Split(tag, ",")[0] | ||
field := reflect.ValueOf(&ba).Elem().Field(i) | ||
|
||
// If the field is zero, then we create a stub artifact. | ||
if field.IsZero() { | ||
ret[strings.ToLower(tag)] = &Artifact{} | ||
continue | ||
} | ||
|
||
// When the json struct tag does not have "omitempty" | ||
// then we get an actual struct not the pointer. | ||
if field.Kind() == reflect.Struct { | ||
r, ok := field.Interface().(Artifact) | ||
if ok { | ||
ret[strings.ToLower(tag)] = &r | ||
} | ||
continue | ||
} | ||
|
||
// Optional structs (i.e. "omitempty") are pointers a struct. | ||
if field.Addr().Elem().CanInterface() { | ||
r, ok := reflect.ValueOf(&ba).Elem().Field(i).Elem().Interface().(Artifact) | ||
if ok { | ||
ret[strings.ToLower(tag)] = &r | ||
} | ||
} | ||
} | ||
return ret | ||
} |
This file contains 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,55 @@ | ||
package cosa | ||
|
||
import ( | ||
"encoding/json" | ||
"io/ioutil" | ||
"path/filepath" | ||
|
||
"github.com/pkg/errors" | ||
) | ||
|
||
const ( | ||
// CosaBuildsJSON is the COSA build.json file name | ||
CosaBuildsJSON = "builds.json" | ||
) | ||
|
||
var ( | ||
// ErrNoBuildsFound is thrown when a build is missing | ||
ErrNoBuildsFound = errors.New("no COSA builds found") | ||
) | ||
|
||
// buildsJSON represents the JSON that records the builds | ||
// TODO: this should be generated by a schema | ||
type buildsJSON struct { | ||
SchemaVersion string `json:"schema-version"` | ||
Builds []struct { | ||
ID string `json:"id"` | ||
Arches []string `json:"arches"` | ||
} `json:"builds"` | ||
TimeStamp string `json:"timestamp"` | ||
} | ||
|
||
func getBuilds(dir string) (*buildsJSON, error) { | ||
path := filepath.Join(dir, "builds", CosaBuildsJSON) | ||
d, err := ioutil.ReadFile(path) | ||
if err != nil { | ||
return nil, ErrNoBuildsFound | ||
} | ||
b := &buildsJSON{} | ||
if err := json.Unmarshal(d, b); err != nil { | ||
return nil, err | ||
} | ||
return b, nil | ||
} | ||
|
||
// getLatest returns the latest build for the arch. | ||
func (b *buildsJSON) getLatest(arch string) (string, bool) { | ||
for _, b := range b.Builds { | ||
for _, a := range b.Arches { | ||
if a == arch { | ||
return b.ID, true | ||
} | ||
} | ||
} | ||
return "", false | ||
} |
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.
.PHONY: