Skip to content

Commit

Permalink
minor hashrelease updates
Browse files Browse the repository at this point in the history
  • Loading branch information
radTuti committed Jan 20, 2025
1 parent f3de937 commit 71acebb
Show file tree
Hide file tree
Showing 5 changed files with 98 additions and 58 deletions.
36 changes: 18 additions & 18 deletions release/cmd/hashrelease.go
Original file line number Diff line number Diff line change
Expand Up @@ -126,13 +126,9 @@ func hashreleaseSubCommands(cfg *Config) []*cli.Command {
// Define the hashrelease directory using the hash from the pinned file.
hashreleaseDir := filepath.Join(baseHashreleaseDir, data.Hash())

// Configure a release builder using the generated versions, and use it
// to build a Calico release.
pinnedOpts, err := pinned.ManagerOptions()
if err != nil {
return fmt.Errorf(("failed to retrieve pinned version options for manager: %v"), err)
}
opts := append(pinnedOpts,
opts := []calico.Option{
calico.WithVersion(data.ProductVersion()),
calico.WithOperator(c.String(operatorRegistryFlag.Name), c.String(operatorImageFlag.Name), data.OperatorVersion()),
calico.WithRepoRoot(cfg.RepoRootDir),
calico.WithReleaseBranchPrefix(c.String(releaseBranchPrefixFlag.Name)),
calico.IsHashRelease(),
Expand All @@ -144,7 +140,7 @@ func hashreleaseSubCommands(cfg *Config) []*cli.Command {
calico.WithRepoName(c.String(repoFlag.Name)),
calico.WithRepoRemote(c.String(repoRemoteFlag.Name)),
calico.WithArchitectures(c.StringSlice(archFlag.Name)),
)
}
if reg := c.StringSlice(registryFlag.Name); len(reg) > 0 {
opts = append(opts, calico.WithImageRegistries(reg))
}
Expand Down Expand Up @@ -264,16 +260,20 @@ func hashreleaseSubCommands(cfg *Config) []*cli.Command {
},
},

// The garbage-collect command is used to clean up older hashreleases from the hashrelease server.
{
Name: "garbage-collect",
Usage: "Clean up older hashreleases",
Aliases: []string{"gc"},
Flags: []cli.Flag{maxHashreleasesFlag},
Action: func(c *cli.Context) error {
configureLogging("hashrelease-garbage-collect.log")
return hashreleaseserver.CleanOldHashreleases(hashreleaseServerConfig(c), c.Int(maxHashreleasesFlag.Name))
},
hashreleaseGarbageCollectCommand(cfg),
}
}

// hashreleaseGarbageCollectCommand is used to clean up older hashreleases from the hashrelease server.
func hashreleaseGarbageCollectCommand(cfg *Config) *cli.Command {
return &cli.Command{
Name: "garbage-collect",
Usage: "Clean up older hashreleases",
Aliases: []string{"gc"},
Flags: []cli.Flag{maxHashreleasesFlag},
Action: func(c *cli.Context) error {
configureLogging("hashrelease-garbage-collect.log")
return hashreleaseserver.CleanOldHashreleases(hashreleaseServerConfig(c), c.Int(maxHashreleasesFlag.Name))
},
}
}
Expand Down
5 changes: 5 additions & 0 deletions release/internal/command/runner.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,8 @@ func runner() CommandRunner {
func Run(command string, args []string) (string, error) {
return runner().Run(command, args, nil)
}

// RunInDir runs a command with arguments in a specific directory.
func RunInDir(dir, command string, args []string) (string, error) {
return runner().RunInDir(dir, command, args, nil)
}
24 changes: 5 additions & 19 deletions release/internal/pinnedversion/pinnedversion.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,14 +31,13 @@ import (
"github.com/projectcalico/calico/release/internal/registry"
"github.com/projectcalico/calico/release/internal/utils"
"github.com/projectcalico/calico/release/internal/version"
"github.com/projectcalico/calico/release/pkg/manager/calico"
)

//go:embed templates/calico-versions.yaml.gotmpl
var calicoTemplate string

const (
pinnedVersionFileName = "pinned_version.yml"
pinnedVersionFileName = "pinned_versions.yml"
operatorComponentsFileName = "pinned_components.yml"
)

Expand All @@ -50,7 +49,6 @@ var noImageComponents = []string{

type PinnedVersions interface {
GenerateFile() (version.Versions, error)
ManagerOptions() ([]calico.Option, error)
}

type OperatorConfig struct {
Expand Down Expand Up @@ -100,7 +98,8 @@ func (d *calicoTemplateData) ReleaseURL() string {
return fmt.Sprintf("https://%s.%s", d.ReleaseName, d.BaseDomain)
}

func pinnedVersionFilePath(outputDir string) string {
// PinnedVersionFilePath returns the path of the pinned version file.
func PinnedVersionFilePath(outputDir string) string {
return filepath.Join(outputDir, pinnedVersionFileName)
}

Expand All @@ -126,7 +125,7 @@ type CalicoPinnedVersions struct {

// GenerateFile generates the pinned version file.
func (p *CalicoPinnedVersions) GenerateFile() (version.Versions, error) {
pinnedVersionPath := pinnedVersionFilePath(p.Dir)
pinnedVersionPath := PinnedVersionFilePath(p.Dir)

productBranch, err := utils.GitBranch(p.RootDir)
if err != nil {
Expand Down Expand Up @@ -189,19 +188,6 @@ func (p *CalicoPinnedVersions) GenerateFile() (version.Versions, error) {
return versionData, nil
}

// ManagerOptions returns the options for the manager.
func (p *CalicoPinnedVersions) ManagerOptions() ([]calico.Option, error) {
pinnedVersion, err := retrievePinnedVersion(p.Dir)
if err != nil {
return nil, err
}

return []calico.Option{
calico.WithVersion(pinnedVersion.Title),
calico.WithOperatorVersion(pinnedVersion.TigeraOperator.Version),
}, nil
}

// GenerateOperatorComponents generates the components-version.yaml for operator.
// It also copies the generated file to the output directory if provided.
func GenerateOperatorComponents(srcDir, outputDir string) (registry.OperatorComponent, string, error) {
Expand Down Expand Up @@ -230,7 +216,7 @@ func GenerateOperatorComponents(srcDir, outputDir string) (registry.OperatorComp

// retrievePinnedVersion retrieves the pinned version from the pinned version file.
func retrievePinnedVersion(outputDir string) (PinnedVersion, error) {
pinnedVersionPath := pinnedVersionFilePath(outputDir)
pinnedVersionPath := PinnedVersionFilePath(outputDir)
var pinnedVersionFile []PinnedVersion
if pinnedVersionData, err := os.ReadFile(pinnedVersionPath); err != nil {
return PinnedVersion{}, err
Expand Down
74 changes: 53 additions & 21 deletions release/pkg/manager/calico/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ import (
"github.com/projectcalico/calico/release/internal/registry"
"github.com/projectcalico/calico/release/internal/utils"
errr "github.com/projectcalico/calico/release/pkg/errors"
"github.com/projectcalico/calico/release/pkg/manager/operator"
)

// Global configuration for releases.
Expand Down Expand Up @@ -88,19 +89,23 @@ var (
"calico/cni-windows",
"calico/node-windows",
}

metadataFileName = "metadata.yaml"
)

func NewManager(opts ...Option) *CalicoManager {
// Configure defaults here.
b := &CalicoManager{
runner: &command.RealCommandRunner{},
productCode: utils.CalicoProductCode,
validate: true,
buildImages: true,
publishImages: true,
publishTag: true,
publishGithub: true,
imageRegistries: defaultRegistries,
runner: &command.RealCommandRunner{},
productCode: utils.CalicoProductCode,
validate: true,
buildImages: true,
publishImages: true,
publishTag: true,
publishGithub: true,
imageRegistries: defaultRegistries,
operatorRegistry: operator.DefaultRegistry,
operatorImage: operator.DefaultImage,
}

// Run through provided options.
Expand Down Expand Up @@ -165,8 +170,10 @@ type CalicoManager struct {
// calicoVersion is the version of calico to release.
calicoVersion string

// operatorVersion is the version of the operator to release.
operatorVersion string
// operator variables
operatorImage string
operatorRegistry string
operatorVersion string

// outputDir is the directory to which we should write release artifacts, and from
// which we should read them for publishing.
Expand Down Expand Up @@ -213,10 +220,10 @@ type CalicoManager struct {
githubToken string
}

func releaseImages(version, operatorVersion string) []string {
imgList := []string{fmt.Sprintf("quay.io/tigera/operator:%s", operatorVersion)}
for _, img := range append(images, windowsImages...) {
imgList = append(imgList, fmt.Sprintf("%s:%s", img, version))
func releaseImages(images []string, version, registry, operatorImage, operatorVersion, operatorRegistry string) []string {
imgList := []string{fmt.Sprintf("%s/%s:%s", operatorRegistry, operatorImage, operatorVersion)}
for _, img := range images {
imgList = append(imgList, fmt.Sprintf("%s/%s:%s", registry, img, version))
}
return imgList
}
Expand Down Expand Up @@ -307,18 +314,23 @@ func (r *CalicoManager) Build() error {
return nil
}

type metadata struct {
Version string `json:"version"`
OperatorVersion string `json:"operator_version" yaml:"operatorVersion"`
Images []string `json:"images"`
HelmChartVersion string `json:"helm_chart_version" yaml:"helmChartVersion"`
}

func (r *CalicoManager) BuildMetadata(dir string) error {
type metadata struct {
Version string `json:"version"`
OperatorVersion string `json:"operator_version" yaml:"operatorVersion"`
Images []string `json:"images"`
HelmChartVersion string `json:"helm_chart_version" yaml:"helmChartVersion"`
registry, err := r.getRegistryFromManifests()
if err != nil {
return err
}

m := metadata{
Version: r.calicoVersion,
OperatorVersion: r.operatorVersion,
Images: releaseImages(r.calicoVersion, r.operatorVersion),
Images: releaseImages(append(images, windowsImages...), r.calicoVersion, registry, r.operatorImage, r.operatorVersion, r.operatorRegistry),
HelmChartVersion: r.helmChartVersion(),
}

Expand All @@ -328,14 +340,34 @@ func (r *CalicoManager) BuildMetadata(dir string) error {
return err
}

err = os.WriteFile(fmt.Sprintf("%s/metadata.yaml", dir), []byte(bs), 0o644)
err = os.WriteFile(filepath.Join(dir, metadataFileName), []byte(bs), 0o644)
if err != nil {
return err
}

return nil
}

func (r *CalicoManager) getRegistryFromManifests() (string, error) {
args := []string{"-Po", `image:\K(.*)`, "calicoctl.yaml"}
out, err := r.runner.RunInDir(filepath.Join(r.repoRoot, "manifests"), "grep", args, nil)
if err != nil {
return "", err
}
imgs := strings.Split(out, "\n")
for _, i := range imgs {
if strings.Contains(i, "operator") {
continue
} else if strings.Contains(i, "tigera/") {
splits := strings.SplitAfter(i, "/tigera/")
registry := splits[0]
logrus.WithField("registry", registry).Debugf("Using registry from image %s", i)
return registry, nil
}
}
return "", fmt.Errorf("failed to find registry from manifests")
}

func (r *CalicoManager) PreHashreleaseValidate() error {
var errStack error
if r.validateBranch {
Expand Down
17 changes: 17 additions & 0 deletions release/pkg/manager/calico/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
package calico

import (
"fmt"

"github.com/projectcalico/calico/release/internal/hashreleaseserver"
"github.com/projectcalico/calico/release/internal/imagescanner"
"github.com/projectcalico/calico/release/internal/registry"
Expand Down Expand Up @@ -57,6 +59,21 @@ func WithVersion(version string) Option {
}
}

func WithOperator(registry, image, version string) Option {
return func(r *CalicoManager) error {
if image == "" {
return fmt.Errorf("operator image cannot be blank")
}
if registry == "" {
return fmt.Errorf("operator registry cannot be blank")
}
r.operatorImage = image
r.operatorVersion = version
r.operatorRegistry = registry
return nil
}
}

func WithOperatorVersion(version string) Option {
return func(r *CalicoManager) error {
r.operatorVersion = version
Expand Down

0 comments on commit 71acebb

Please sign in to comment.