Skip to content
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

Basic validation of index files #1281

Merged
merged 2 commits into from
Jan 16, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 31 additions & 14 deletions pkg/kudoctl/util/repo/index.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,28 +76,17 @@ func (b PackageVersions) Less(x, y int) bool {
return appVersionX.LessThan(appVersionY)
}

// sortPackages sorts the entries by version in descending order.
//
// In canonical form, the individual version records should be sorted so that
// the most recent release for every version is in the 0th slot in the
// Entries.PackageVersions array. That way, tooling can predict the newest
// version without needing to parse SemVers.
func (i IndexFile) sortPackages() {
for _, versions := range i.Entries {
sort.Sort(sort.Reverse(versions))
}
}

// ParseIndexFile loads an index file and sorts the included packages by version.
// The function will fail if `APIVersion` is not specified.
func ParseIndexFile(data []byte) (*IndexFile, error) {
i := &IndexFile{}
if err := yaml.Unmarshal(data, i); err != nil {
return nil, fmt.Errorf("unmarshalling index file: %w", err)
}
if i.APIVersion == "" {
return nil, errors.New("no API version specified")
if err := i.validate(); err != nil {
return nil, fmt.Errorf("validating index file: %w", err)
}

i.sortPackages()
return i, nil
}
Expand Down Expand Up @@ -162,6 +151,34 @@ func (i *IndexFile) WriteFile(fs afero.Fs, file string) (err error) {
return i.Write(f)
}

func (i *IndexFile) validate() error {
if i.APIVersion == "" {
return errors.New("no API version defined")
}

for _, packageVersions := range i.Entries {
for _, packageVersion := range packageVersions {
if packageVersion.OperatorVersion == "" {
return fmt.Errorf("package %s is missing an operator version", packageVersion.Name)
}
}
}

return nil
}

// sortPackages sorts the entries by version in descending order.
//
// In canonical form, the individual version records should be sorted so that
// the most recent release for every version is in the 0th slot in the
// Entries.PackageVersions array. That way, tooling can predict the newest
// version without needing to parse SemVers.
func (i IndexFile) sortPackages() {
for _, versions := range i.Entries {
sort.Sort(sort.Reverse(versions))
}
}

// Map transforms a slice of packagefiles with file digests into a slice of PackageVersions
func Map(pkgs []*PackageFilesDigest, url string) PackageVersions {
return mapPackages(pkgs, url, ToPackageVersion)
Expand Down