Skip to content

Commit

Permalink
Merge pull request #3079 from xmudrii/krel-obs-specs
Browse files Browse the repository at this point in the history
Refactor `krel obs specs` command
  • Loading branch information
k8s-ci-robot authored May 25, 2023
2 parents 60f655b + d86f4ce commit e4e2f48
Show file tree
Hide file tree
Showing 34 changed files with 656 additions and 1,527 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
}
File renamed without changes.
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,11 @@ License: Apache-2.0
URL: https://kubernetes.io
Source0: %{name}_%{version}.orig.tar.gz

Requires: kubelet >= {{ index .Dependencies "kubelet" }}
Requires: kubectl >= {{ index .Dependencies "kubectl" }}
Requires: kubernetes-cni >= {{ index .Dependencies "kubernetes-cni" }}
Requires: cri-tools >= {{ index .Dependencies "cri-tools" }}
Require: kubectl >= {{ .Version }}
Require: kubelet >= {{ .Version }}
{{ range $dep := .Metadata.Dependencies }}
Requires: {{ $dep.Name }} {{ $dep.VersionConstraint }}
{{- end }}

%if "%{_vendor}" == "debbuild"
BuildRequires: systemd-deb-macros
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,9 @@ Source0: %{name}_%{version}.orig.tar.gz

BuildRequires: systemd
Requires: iptables >= 1.4.21
Requires: kubernetes-cni >= {{ index .Dependencies "kubernetes-cni" }}
{{ range $dep := .Metadata.Dependencies }}
Requires: {{ $dep.Name }} {{ $dep.VersionConstraint }}
{{- end }}
%if "%{_vendor}" == "debbuild"
Requires: iproute2
Requires: mount
Expand Down
29 changes: 29 additions & 0 deletions cmd/krel/templates/latest/metadata.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
cri-tools:
- versionConstraint: ">= 1.0.0"
sourceURLTemplate: "gs://k8s-artifacts-cri-tools/release/v{{ .PackageVersion }}/crictl-v{{ .PackageVersion }}-linux-{{ .Architecture }}.tar.gz"
sourceTarGz: true
kubeadm:
- versionConstraint: ">= 1.0.0"
sourceURLTemplate: "{{ KubernetesURL }}"
dependencies:
- name: kubelet
versionConstraint: ">= 1.13.0"
- name: kubectl
versionConstraint: ">= 1.13.0"
- name: kubernetes-cni
versionConstraint: ">= 1.2.0"
- name: cri-tools
versionConstraint: ">= 1.26.0"
kubectl:
- versionConstraint: ">= 1.0.0"
sourceURLTemplate: "{{ KubernetesURL }}"
kubelet:
- versionConstraint: ">= 1.0.0"
sourceURLTemplate: "{{ KubernetesURL }}"
dependencies:
- name: kubernetes-cni
versionConstraint: ">= 1.2.0"
kubernetes-cni:
- versionConstraint: ">= 1.0.0"
sourceURLTemplate: "gs://k8s-artifacts-cni/release/v{{ .PackageVersion }}/cni-plugins-linux-{{ .Architecture }}-v{{ .PackageVersion }}.tgz"
sourceTarGz: true
76 changes: 76 additions & 0 deletions pkg/obs/metadata/metadata.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
/*
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 (
"errors"
"fmt"
"os"

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

// PackageMetadata is a struct that contains the following information about a package:
// - 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, errors.New("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
}
Loading

0 comments on commit e4e2f48

Please sign in to comment.