2022-12-29 11:25:54 -05:00
|
|
|
/*
|
|
|
|
Copyright (c) Edgeless Systems GmbH
|
|
|
|
|
|
|
|
SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
*/
|
|
|
|
|
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
|
2023-06-05 04:51:05 -04:00
|
|
|
apiclient "github.com/edgelesssys/constellation/v2/internal/api/client"
|
2023-06-07 10:16:32 -04:00
|
|
|
"github.com/edgelesssys/constellation/v2/internal/api/versionsapi"
|
2022-12-29 11:25:54 -05:00
|
|
|
"github.com/edgelesssys/constellation/v2/internal/logger"
|
|
|
|
"github.com/spf13/cobra"
|
|
|
|
"go.uber.org/zap/zapcore"
|
|
|
|
"golang.org/x/mod/semver"
|
|
|
|
)
|
|
|
|
|
|
|
|
func newAddCmd() *cobra.Command {
|
|
|
|
cmd := &cobra.Command{
|
|
|
|
Use: "add",
|
|
|
|
Short: "Add a new version",
|
|
|
|
Long: `Add a new version to the versions API.
|
|
|
|
|
|
|
|
Developers should not use this command directly. It is invoked by the CI/CD pipeline.
|
2023-03-10 04:21:58 -05:00
|
|
|
If you've build a local image, use a local override instead of adding a new version.
|
2022-12-29 11:25:54 -05:00
|
|
|
|
|
|
|
❗ If you use the command nevertheless, you better know what you do.
|
|
|
|
`,
|
|
|
|
RunE: runAdd,
|
|
|
|
}
|
|
|
|
|
|
|
|
cmd.Flags().String("ref", "", "Ref of the version to add")
|
|
|
|
cmd.Flags().String("stream", "", "Stream of the version to add")
|
|
|
|
cmd.Flags().String("version", "", "Version to add (format: \"v1.2.3\")")
|
2023-03-10 04:21:58 -05:00
|
|
|
cmd.Flags().String("kind", "", "Version kind to add (e.g. image, cli)")
|
2022-12-29 11:25:54 -05:00
|
|
|
cmd.Flags().Bool("latest", false, "Whether the version is the latest version of the ref/stream")
|
|
|
|
cmd.Flags().Bool("release", false, "Whether the version is a release version")
|
|
|
|
cmd.Flags().Bool("dryrun", false, "Whether to run in dry-run mode (no changes are made)")
|
|
|
|
|
|
|
|
cmd.MarkFlagsMutuallyExclusive("ref", "release")
|
|
|
|
must(cmd.MarkFlagRequired("version"))
|
|
|
|
|
|
|
|
return cmd
|
|
|
|
}
|
|
|
|
|
2023-03-20 06:03:36 -04:00
|
|
|
func runAdd(cmd *cobra.Command, _ []string) (retErr error) {
|
2022-12-29 11:25:54 -05:00
|
|
|
flags, err := parseAddFlags(cmd)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
log := logger.New(logger.PlainLog, flags.logLevel)
|
|
|
|
log.Debugf("Parsed flags: %+v", flags)
|
|
|
|
|
2023-01-31 06:12:19 -05:00
|
|
|
log.Debugf("Validating flags")
|
2022-12-29 11:25:54 -05:00
|
|
|
if err := flags.validate(log); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2023-01-31 06:12:19 -05:00
|
|
|
log.Debugf("Creating version struct")
|
2023-08-01 10:48:13 -04:00
|
|
|
ver, err := versionsapi.NewVersion(flags.ref, flags.stream, flags.version, flags.kind)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("creating version: %w", err)
|
2022-12-29 11:25:54 -05:00
|
|
|
}
|
|
|
|
|
2023-01-31 06:12:19 -05:00
|
|
|
log.Debugf("Creating versions API client")
|
2023-06-07 10:16:32 -04:00
|
|
|
client, clientClose, err := versionsapi.NewClient(cmd.Context(), flags.region, flags.bucket, flags.distributionID, flags.dryRun, log)
|
2022-12-29 11:25:54 -05:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("creating client: %w", err)
|
|
|
|
}
|
2023-08-31 04:31:45 -04:00
|
|
|
defer func() {
|
|
|
|
err := clientClose(cmd.Context())
|
|
|
|
if err != nil {
|
|
|
|
retErr = errors.Join(retErr, fmt.Errorf("failed to invalidate cache: %w", err))
|
2022-12-29 11:25:54 -05:00
|
|
|
}
|
2023-08-31 04:31:45 -04:00
|
|
|
}()
|
2022-12-29 11:25:54 -05:00
|
|
|
|
2023-01-31 06:12:19 -05:00
|
|
|
log.Infof("Adding version")
|
2023-03-10 04:21:58 -05:00
|
|
|
if err := ensureVersion(cmd.Context(), client, flags.kind, ver, versionsapi.GranularityMajor, log); err != nil {
|
2022-12-29 11:25:54 -05:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2023-03-10 04:21:58 -05:00
|
|
|
if err := ensureVersion(cmd.Context(), client, flags.kind, ver, versionsapi.GranularityMinor, log); err != nil {
|
2022-12-29 11:25:54 -05:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if flags.latest {
|
2023-03-10 04:21:58 -05:00
|
|
|
if err := updateLatest(cmd.Context(), client, flags.kind, ver, log); err != nil {
|
2022-12-29 11:25:54 -05:00
|
|
|
return fmt.Errorf("setting latest version: %w", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
log.Infof("List major->minor URL: %s", ver.ListURL(versionsapi.GranularityMajor))
|
|
|
|
log.Infof("List minor->patch URL: %s", ver.ListURL(versionsapi.GranularityMinor))
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-06-07 10:16:32 -04:00
|
|
|
func ensureVersion(ctx context.Context, client *versionsapi.Client, kind versionsapi.VersionKind, ver versionsapi.Version, gran versionsapi.Granularity,
|
2022-12-29 11:25:54 -05:00
|
|
|
log *logger.Logger,
|
|
|
|
) error {
|
|
|
|
verListReq := versionsapi.List{
|
2023-08-01 10:48:13 -04:00
|
|
|
Ref: ver.Ref(),
|
|
|
|
Stream: ver.Stream(),
|
2022-12-29 11:25:54 -05:00
|
|
|
Granularity: gran,
|
|
|
|
Base: ver.WithGranularity(gran),
|
2023-03-10 04:21:58 -05:00
|
|
|
Kind: kind,
|
2022-12-29 11:25:54 -05:00
|
|
|
}
|
|
|
|
verList, err := client.FetchVersionList(ctx, verListReq)
|
2023-06-05 04:51:05 -04:00
|
|
|
var notFoundErr *apiclient.NotFoundError
|
2022-12-29 11:25:54 -05:00
|
|
|
if errors.As(err, ¬FoundErr) {
|
|
|
|
log.Infof("Version list for %s versions under %q does not exist. Creating new list", gran.String(), ver.Major())
|
|
|
|
verList = verListReq
|
|
|
|
} else if err != nil {
|
|
|
|
return fmt.Errorf("failed to list minor versions: %w", err)
|
|
|
|
}
|
|
|
|
log.Debugf("%s version list: %v", gran.String(), verList)
|
|
|
|
|
|
|
|
insertGran := gran + 1
|
|
|
|
insertVersion := ver.WithGranularity(insertGran)
|
|
|
|
|
|
|
|
if verList.Contains(insertVersion) {
|
2023-01-31 06:12:19 -05:00
|
|
|
log.Infof("Version %q already exists in list %v", insertVersion, verList.Versions)
|
2022-12-29 11:25:54 -05:00
|
|
|
return nil
|
|
|
|
}
|
2023-01-31 06:12:19 -05:00
|
|
|
log.Infof("Inserting %s version %q into list", insertGran.String(), insertVersion)
|
2022-12-29 11:25:54 -05:00
|
|
|
|
|
|
|
verList.Versions = append(verList.Versions, insertVersion)
|
2023-01-31 06:12:19 -05:00
|
|
|
log.Debugf("New %s version list: %v", gran.String(), verList)
|
2022-12-29 11:25:54 -05:00
|
|
|
|
|
|
|
if err := client.UpdateVersionList(ctx, verList); err != nil {
|
|
|
|
return fmt.Errorf("failed to add %s version: %w", gran.String(), err)
|
|
|
|
}
|
|
|
|
|
2023-01-31 06:12:19 -05:00
|
|
|
log.Infof("Added %q to list", insertVersion)
|
2022-12-29 11:25:54 -05:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-06-07 10:16:32 -04:00
|
|
|
func updateLatest(ctx context.Context, client *versionsapi.Client, kind versionsapi.VersionKind, ver versionsapi.Version, log *logger.Logger) error {
|
2022-12-29 11:25:54 -05:00
|
|
|
latest := versionsapi.Latest{
|
2023-08-01 10:48:13 -04:00
|
|
|
Ref: ver.Ref(),
|
|
|
|
Stream: ver.Stream(),
|
2023-03-10 04:21:58 -05:00
|
|
|
Kind: kind,
|
2022-12-29 11:25:54 -05:00
|
|
|
}
|
|
|
|
latest, err := client.FetchVersionLatest(ctx, latest)
|
2023-06-05 04:51:05 -04:00
|
|
|
var notFoundErr *apiclient.NotFoundError
|
2022-12-29 11:25:54 -05:00
|
|
|
if errors.As(err, ¬FoundErr) {
|
2023-08-01 10:48:13 -04:00
|
|
|
log.Debugf("Latest version for ref %q and stream %q not found", ver.Ref(), ver.Stream())
|
2022-12-29 11:25:54 -05:00
|
|
|
} else if err != nil {
|
|
|
|
return fmt.Errorf("fetching latest version: %w", err)
|
|
|
|
}
|
|
|
|
|
2023-08-01 10:48:13 -04:00
|
|
|
if latest.Version == ver.Version() {
|
2022-12-29 11:25:54 -05:00
|
|
|
log.Infof("Version %q is already latest version", ver)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-01-31 06:12:19 -05:00
|
|
|
log.Infof("Setting %q as latest version", ver)
|
2022-12-29 11:25:54 -05:00
|
|
|
latest = versionsapi.Latest{
|
2023-08-01 10:48:13 -04:00
|
|
|
Ref: ver.Ref(),
|
|
|
|
Stream: ver.Stream(),
|
|
|
|
Version: ver.Version(),
|
2023-03-10 04:21:58 -05:00
|
|
|
Kind: kind,
|
2022-12-29 11:25:54 -05:00
|
|
|
}
|
|
|
|
if err := client.UpdateVersionLatest(ctx, latest); err != nil {
|
|
|
|
return fmt.Errorf("updating latest version: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
type addFlags struct {
|
|
|
|
version string
|
|
|
|
stream string
|
|
|
|
ref string
|
|
|
|
release bool
|
|
|
|
latest bool
|
|
|
|
dryRun bool
|
|
|
|
region string
|
|
|
|
bucket string
|
|
|
|
distributionID string
|
2023-03-10 04:21:58 -05:00
|
|
|
kind versionsapi.VersionKind
|
2022-12-29 11:25:54 -05:00
|
|
|
logLevel zapcore.Level
|
|
|
|
}
|
|
|
|
|
|
|
|
func (f *addFlags) validate(log *logger.Logger) error {
|
|
|
|
if !semver.IsValid(f.version) {
|
|
|
|
return fmt.Errorf("version %q is not a valid semantic version", f.version)
|
|
|
|
}
|
|
|
|
if semver.Canonical(f.version) != f.version {
|
|
|
|
return fmt.Errorf("version %q is not a canonical semantic version", f.version)
|
|
|
|
}
|
|
|
|
|
|
|
|
if f.ref == "" && !f.release {
|
|
|
|
return fmt.Errorf("either --ref or --release must be set")
|
|
|
|
}
|
|
|
|
|
2023-03-10 04:21:58 -05:00
|
|
|
if f.kind == versionsapi.VersionKindUnknown {
|
|
|
|
return fmt.Errorf("unknown version kind %q", f.kind)
|
|
|
|
}
|
|
|
|
|
2022-12-29 11:25:54 -05:00
|
|
|
if f.release {
|
|
|
|
log.Debugf("Setting ref to %q, as release flag is set", versionsapi.ReleaseRef)
|
|
|
|
f.ref = versionsapi.ReleaseRef
|
|
|
|
} else {
|
|
|
|
log.Debugf("Setting latest to true, as release flag is not set")
|
|
|
|
f.latest = true // always set latest for non-release versions
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := versionsapi.ValidateRef(f.ref); err != nil {
|
|
|
|
return fmt.Errorf("invalid ref %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := versionsapi.ValidateStream(f.ref, f.stream); err != nil {
|
|
|
|
return fmt.Errorf("invalid stream %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func parseAddFlags(cmd *cobra.Command) (addFlags, error) {
|
|
|
|
ref, err := cmd.Flags().GetString("ref")
|
|
|
|
if err != nil {
|
|
|
|
return addFlags{}, err
|
|
|
|
}
|
|
|
|
ref = versionsapi.CanonicalizeRef(ref)
|
|
|
|
stream, err := cmd.Flags().GetString("stream")
|
|
|
|
if err != nil {
|
|
|
|
return addFlags{}, err
|
|
|
|
}
|
2023-03-10 04:21:58 -05:00
|
|
|
kindFlag, err := cmd.Flags().GetString("kind")
|
|
|
|
if err != nil {
|
|
|
|
return addFlags{}, err
|
|
|
|
}
|
|
|
|
kind := versionsapi.VersionKindFromString(kindFlag)
|
2022-12-29 11:25:54 -05:00
|
|
|
version, err := cmd.Flags().GetString("version")
|
|
|
|
if err != nil {
|
|
|
|
return addFlags{}, err
|
|
|
|
}
|
|
|
|
release, err := cmd.Flags().GetBool("release")
|
|
|
|
if err != nil {
|
|
|
|
return addFlags{}, err
|
|
|
|
}
|
|
|
|
latest, err := cmd.Flags().GetBool("latest")
|
|
|
|
if err != nil {
|
|
|
|
return addFlags{}, err
|
|
|
|
}
|
|
|
|
dryRun, err := cmd.Flags().GetBool("dryrun")
|
|
|
|
if err != nil {
|
|
|
|
return addFlags{}, err
|
|
|
|
}
|
|
|
|
verbose, err := cmd.Flags().GetBool("verbose")
|
|
|
|
if err != nil {
|
|
|
|
return addFlags{}, err
|
|
|
|
}
|
|
|
|
logLevel := zapcore.InfoLevel
|
|
|
|
if verbose {
|
|
|
|
logLevel = zapcore.DebugLevel
|
|
|
|
}
|
|
|
|
region, err := cmd.Flags().GetString("region")
|
|
|
|
if err != nil {
|
|
|
|
return addFlags{}, err
|
|
|
|
}
|
|
|
|
bucket, err := cmd.Flags().GetString("bucket")
|
|
|
|
if err != nil {
|
|
|
|
return addFlags{}, err
|
|
|
|
}
|
|
|
|
distributionID, err := cmd.Flags().GetString("distribution-id")
|
|
|
|
if err != nil {
|
|
|
|
return addFlags{}, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return addFlags{
|
|
|
|
version: version,
|
|
|
|
stream: stream,
|
|
|
|
ref: versionsapi.CanonicalizeRef(ref),
|
|
|
|
release: release,
|
|
|
|
latest: latest,
|
|
|
|
dryRun: dryRun,
|
|
|
|
region: region,
|
|
|
|
bucket: bucket,
|
|
|
|
distributionID: distributionID,
|
|
|
|
logLevel: logLevel,
|
2023-03-10 04:21:58 -05:00
|
|
|
kind: kind,
|
2022-12-29 11:25:54 -05:00
|
|
|
}, nil
|
|
|
|
}
|