Skip to content

Commit

Permalink
Refactor krel obs specs command
Browse files Browse the repository at this point in the history
Signed-off-by: Marko Mudrinić <mudrinic.mare@gmail.com>
  • Loading branch information
xmudrii committed May 23, 2023
1 parent 3c407a3 commit 2e93033
Show file tree
Hide file tree
Showing 15 changed files with 629 additions and 1,525 deletions.
64 changes: 25 additions & 39 deletions cmd/krel/cmd/obs_specs.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,11 +40,11 @@ var obsSpecsCmd = &cobra.Command{
}

func init() {
obsSpecsCmd.PersistentFlags().StringSliceVar(
&obsOpts.Packages,
"packages",
obsOpts.Packages,
"packages to create specs and archives for",
obsSpecsCmd.PersistentFlags().StringVar(
&obsOpts.Package,
"package",
obsOpts.Package,
"package to create specs and archives for",
)

obsSpecsCmd.PersistentFlags().StringVar(
Expand All @@ -62,10 +62,10 @@ func init() {
)

obsSpecsCmd.PersistentFlags().StringVar(
&obsOpts.KubernetesVersion,
"kubernetes-version",
obsOpts.KubernetesVersion,
"kubernetes version to use in specs and for downloading binaries",
&obsOpts.Version,
"version",
obsOpts.Version,
"package version",
)

obsSpecsCmd.PersistentFlags().StringVar(
Expand All @@ -76,37 +76,23 @@ func init() {
)

obsSpecsCmd.PersistentFlags().StringVar(
&obsOpts.CNIVersion,
"cni-version",
obsOpts.CNIVersion,
"cni version to download binaries for when creating the archive",
)

obsSpecsCmd.PersistentFlags().StringVar(
&obsOpts.CRIToolsVersion,
"cri-tools-version",
obsOpts.CRIToolsVersion,
"cri-tools version to download binaries for when creating the archive",
)

obsSpecsCmd.PersistentFlags().StringVar(
&obsOpts.ReleaseDownloadLinkBase,
"release-download-link-base",
obsOpts.ReleaseDownloadLinkBase,
"release download link base",
&obsOpts.PackageSourceBase,
"package-source",
obsOpts.PackageSourceBase,
"source to download artifacts for package",
)

obsSpecsCmd.PersistentFlags().StringVar(
&obsOpts.TemplateDir,
&obsOpts.SpecTemplatePath,
"template-dir",
obsOpts.TemplateDir,
obsOpts.SpecTemplatePath,
"template directory containing spec files",
)

obsSpecsCmd.PersistentFlags().StringVar(
&obsOpts.OutputDir,
&obsOpts.SpecOutputPath,
"output",
obsOpts.OutputDir,
obsOpts.SpecOutputPath,
"output directory to store specs and archives",
)

Expand All @@ -123,28 +109,28 @@ func init() {
func runGenerateOBSSpecs(opts *options.Options) (err error) {
logrus.Debugf("Using options: %+v", opts)

client := specs.New(opts)

builder, err := client.ConstructPackageBuilder()
if err != nil {
if err := opts.Validate(); err != nil {
return fmt.Errorf("running krel obs: %w", err)
}

if err = client.ConstructPackageDefinitions(builder); err != nil {
client := specs.New(opts)

pkgDef, err := client.ConstructPackageDefinition()
if err != nil {
return fmt.Errorf("running krel obs: %w", err)
}

if err = client.BuildSpecs(builder); err != nil {
if err = client.BuildSpecs(pkgDef, opts.SpecOnly); err != nil {
return fmt.Errorf("running krel obs: %w", err)
}

if !opts.SpecOnly {
if err = client.DownloadAndArchiveBinaries(builder); err != nil {
if err = client.BuildArtifactsArchive(pkgDef); err != nil {
return fmt.Errorf("running krel obs: %w", err)
}
}

logrus.Infof("krel obs done, files available in %s", opts.OutputDir)
logrus.Infof("krel obs done, files available in %s", opts.SpecOutputPath)

return nil
}
75 changes: 75 additions & 0 deletions pkg/obs/metadata/metadata.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
/*
Copyright 2023 The Kubernetes Authors.
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 metadata

import (
"fmt"
"os"

"github.com/sirupsen/logrus"
"sigs.k8s.io/yaml"
)

// PackageMetadata is a struct that containts the following information about a package:

Check failure on line 27 in pkg/obs/metadata/metadata.go

View workflow job for this annotation

GitHub Actions / lint

`containts` is a misspelling of `contains` (misspell)
// - URL from which to download artifacts needed to build the package
// - Indicator if artifacts are packed in a .tar.gz archive
// - Dependencies needed to install the package
// Package's metadata is versioned based on the given version constraint.
type PackageMetadata struct {
// VersionConstraint is a semver range that defines the version of the package for which the metadata is valid.
VersionConstraint string `json:"versionConstraint"`
// SourceURLTemplate is a template for the URL from which to download artifacts needed to build the package.
SourceURLTemplate string `json:"sourceURLTemplate"`
// SourceTarGz is an indicator if artifacts are packed in a .tar.gz archive.
SourceTarGz bool `json:"sourceTarGz"`
// Dependencies is a list of dependencies needed to install the package.
Dependencies []PackageDependency `json:"dependencies,omitempty"`
}

// PackageDependency is a struct that defines a single runtime dependency.
type PackageDependency struct {
// Name is the name of the package.
Name string `json:"name"`
// VersionConstraint is a version constraint that's embedded in the spec file.
VersionConstraint string `json:"versionConstraint"`
}

// PackageMetadataList is a map that represents metadata.yaml file.
type PackageMetadataList map[string][]PackageMetadata

// LoadPackageMetadata loads metadata.yaml file from the given path.
func LoadPackageMetadata(path string) (PackageMetadataList, error) {
if path == "" {
return nil, fmt.Errorf("path cannot be empty")
}

logrus.Infof("Loading metadata from %s...", path)

b, err := os.ReadFile(path)
if err != nil {
return nil, fmt.Errorf("reading metadata file %q: %w", path, err)
}

var pkgDeps PackageMetadataList
if err := yaml.Unmarshal(b, &pkgDeps); err != nil {
return nil, fmt.Errorf("unmarshalling metadata file %q: %w", path, err)
}

logrus.Infof("Found metadata for %d packages.", len(pkgDeps))

return pkgDeps, nil
}

0 comments on commit 2e93033

Please sign in to comment.