versionsapi: use new fetcher in upgrade-plan cmd

Signed-off-by: Paul Meyer <49727155+katexochen@users.noreply.github.com>
This commit is contained in:
Paul Meyer 2022-12-29 17:27:15 +01:00
parent 9dbe6033f2
commit 22f43d32dd
2 changed files with 31 additions and 23 deletions

View File

@ -20,7 +20,8 @@ import (
"github.com/edgelesssys/constellation/v2/internal/constants" "github.com/edgelesssys/constellation/v2/internal/constants"
"github.com/edgelesssys/constellation/v2/internal/file" "github.com/edgelesssys/constellation/v2/internal/file"
"github.com/edgelesssys/constellation/v2/internal/sigstore" "github.com/edgelesssys/constellation/v2/internal/sigstore"
"github.com/edgelesssys/constellation/v2/internal/versionsapi-old" "github.com/edgelesssys/constellation/v2/internal/versionsapi"
"github.com/edgelesssys/constellation/v2/internal/versionsapi/fetcher"
"github.com/manifoldco/promptui" "github.com/manifoldco/promptui"
"github.com/siderolabs/talos/pkg/machinery/config/encoder" "github.com/siderolabs/talos/pkg/machinery/config/encoder"
"github.com/spf13/afero" "github.com/spf13/afero"
@ -62,7 +63,7 @@ func runUpgradePlan(cmd *cobra.Command, args []string) error {
if err != nil { if err != nil {
return err return err
} }
patchLister := versionsapi.New() versionListFetcher := fetcher.NewFetcher()
rekor, err := sigstore.NewRekor() rekor, err := sigstore.NewRekor()
if err != nil { if err != nil {
return fmt.Errorf("constructing Rekor client: %w", err) return fmt.Errorf("constructing Rekor client: %w", err)
@ -70,11 +71,11 @@ func runUpgradePlan(cmd *cobra.Command, args []string) error {
cliVersion := getCurrentCLIVersion() cliVersion := getCurrentCLIVersion()
up := &upgradePlanCmd{log: log} up := &upgradePlanCmd{log: log}
return up.upgradePlan(cmd, planner, patchLister, fileHandler, http.DefaultClient, rekor, flags, cliVersion) return up.upgradePlan(cmd, planner, versionListFetcher, fileHandler, http.DefaultClient, rekor, flags, cliVersion)
} }
// upgradePlan plans an upgrade of a Constellation cluster. // upgradePlan plans an upgrade of a Constellation cluster.
func (up *upgradePlanCmd) upgradePlan(cmd *cobra.Command, planner upgradePlanner, patchLister patchLister, func (up *upgradePlanCmd) upgradePlan(cmd *cobra.Command, planner upgradePlanner, verListFetcher versionListFetcher,
fileHandler file.Handler, client *http.Client, rekor rekorVerifier, flags upgradePlanFlags, fileHandler file.Handler, client *http.Client, rekor rekorVerifier, flags upgradePlanFlags,
cliVersion string, cliVersion string,
) error { ) error {
@ -123,9 +124,16 @@ func (up *upgradePlanCmd) upgradePlan(cmd *cobra.Command, planner upgradePlanner
var updateCandidates []string var updateCandidates []string
for _, minorVer := range allowedMinorVersions { for _, minorVer := range allowedMinorVersions {
versionList, err := patchLister.PatchVersionsOf(cmd.Context(), "-", "stable", minorVer, "image") patchList := versionsapi.List{
Ref: versionsapi.ReleaseRef,
Stream: "stable",
Base: minorVer,
Granularity: versionsapi.GranularityMinor,
Kind: versionsapi.VersionKindImage,
}
patchList, err = verListFetcher.FetchVersionList(cmd.Context(), patchList)
if err == nil { if err == nil {
updateCandidates = append(updateCandidates, versionList.Versions...) updateCandidates = append(updateCandidates, patchList.Versions...)
} }
} }
up.log.Debugf("Update candidates are %v", updateCandidates) up.log.Debugf("Update candidates are %v", updateCandidates)
@ -353,6 +361,6 @@ type upgradePlanner interface {
GetCurrentImage(ctx context.Context) (*unstructured.Unstructured, string, error) GetCurrentImage(ctx context.Context) (*unstructured.Unstructured, string, error)
} }
type patchLister interface { type versionListFetcher interface {
PatchVersionsOf(ctx context.Context, ref, stream, minor, kind string) (*versionsapi.List, error) FetchVersionList(ctx context.Context, list versionsapi.List) (versionsapi.List, error)
} }

View File

@ -20,7 +20,7 @@ import (
"github.com/edgelesssys/constellation/v2/internal/constants" "github.com/edgelesssys/constellation/v2/internal/constants"
"github.com/edgelesssys/constellation/v2/internal/file" "github.com/edgelesssys/constellation/v2/internal/file"
"github.com/edgelesssys/constellation/v2/internal/logger" "github.com/edgelesssys/constellation/v2/internal/logger"
"github.com/edgelesssys/constellation/v2/internal/versionsapi-old" "github.com/edgelesssys/constellation/v2/internal/versionsapi"
"github.com/spf13/afero" "github.com/spf13/afero"
"github.com/spf13/cobra" "github.com/spf13/cobra"
"github.com/stretchr/testify/assert" "github.com/stretchr/testify/assert"
@ -186,7 +186,7 @@ func TestUpgradePlan(t *testing.T) {
pubK := "-----BEGIN PUBLIC KEY-----\nMFkwEwYHKoZIzj0CAQYIKoZIzj0DAQcDQgAEu78QgxOOcao6U91CSzEXxrKhvFTt\nJHNy+eX6EMePtDm8CnDF9HSwnTlD0itGJ/XHPQA5YX10fJAqI1y+ehlFMw==\n-----END PUBLIC KEY-----" pubK := "-----BEGIN PUBLIC KEY-----\nMFkwEwYHKoZIzj0CAQYIKoZIzj0DAQcDQgAEu78QgxOOcao6U91CSzEXxrKhvFTt\nJHNy+eX6EMePtDm8CnDF9HSwnTlD0itGJ/XHPQA5YX10fJAqI1y+ehlFMw==\n-----END PUBLIC KEY-----"
testCases := map[string]struct { testCases := map[string]struct {
patchLister stubPatchLister patchLister stubVersionListFetcher
planner stubUpgradePlanner planner stubUpgradePlanner
flags upgradePlanFlags flags upgradePlanFlags
cliVersion string cliVersion string
@ -197,7 +197,7 @@ func TestUpgradePlan(t *testing.T) {
wantErr bool wantErr bool
}{ }{
"upgrades gcp": { "upgrades gcp": {
patchLister: stubPatchLister{list: availablePatches}, patchLister: stubVersionListFetcher{list: availablePatches},
planner: stubUpgradePlanner{ planner: stubUpgradePlanner{
image: "v1.0.0", image: "v1.0.0",
}, },
@ -213,7 +213,7 @@ func TestUpgradePlan(t *testing.T) {
wantUpgrade: true, wantUpgrade: true,
}, },
"upgrades azure": { "upgrades azure": {
patchLister: stubPatchLister{list: availablePatches}, patchLister: stubVersionListFetcher{list: availablePatches},
planner: stubUpgradePlanner{ planner: stubUpgradePlanner{
image: "v1.0.0", image: "v1.0.0",
}, },
@ -229,7 +229,7 @@ func TestUpgradePlan(t *testing.T) {
wantUpgrade: true, wantUpgrade: true,
}, },
"current image newer than updates": { "current image newer than updates": {
patchLister: stubPatchLister{list: availablePatches}, patchLister: stubVersionListFetcher{list: availablePatches},
planner: stubUpgradePlanner{ planner: stubUpgradePlanner{
image: "v999.999.999", image: "v999.999.999",
}, },
@ -244,7 +244,7 @@ func TestUpgradePlan(t *testing.T) {
wantUpgrade: false, wantUpgrade: false,
}, },
"current image newer than cli": { "current image newer than cli": {
patchLister: stubPatchLister{list: availablePatches}, patchLister: stubVersionListFetcher{list: availablePatches},
planner: stubUpgradePlanner{ planner: stubUpgradePlanner{
image: "v999.999.999", image: "v999.999.999",
}, },
@ -260,7 +260,7 @@ func TestUpgradePlan(t *testing.T) {
wantUpgrade: false, wantUpgrade: false,
}, },
"upgrade to stdout": { "upgrade to stdout": {
patchLister: stubPatchLister{list: availablePatches}, patchLister: stubVersionListFetcher{list: availablePatches},
planner: stubUpgradePlanner{ planner: stubUpgradePlanner{
image: "v1.0.0", image: "v1.0.0",
}, },
@ -276,7 +276,7 @@ func TestUpgradePlan(t *testing.T) {
wantUpgrade: true, wantUpgrade: true,
}, },
"current image not valid": { "current image not valid": {
patchLister: stubPatchLister{list: availablePatches}, patchLister: stubVersionListFetcher{list: availablePatches},
planner: stubUpgradePlanner{ planner: stubUpgradePlanner{
image: "not-valid", image: "not-valid",
}, },
@ -292,7 +292,7 @@ func TestUpgradePlan(t *testing.T) {
wantErr: true, wantErr: true,
}, },
"image fetch error": { "image fetch error": {
patchLister: stubPatchLister{err: errors.New("error")}, patchLister: stubVersionListFetcher{err: errors.New("error")},
planner: stubUpgradePlanner{ planner: stubUpgradePlanner{
image: "v1.0.0", image: "v1.0.0",
}, },
@ -307,7 +307,7 @@ func TestUpgradePlan(t *testing.T) {
verifier: singleUUIDVerifier(), verifier: singleUUIDVerifier(),
}, },
"measurements fetch error": { "measurements fetch error": {
patchLister: stubPatchLister{list: availablePatches}, patchLister: stubVersionListFetcher{list: availablePatches},
planner: stubUpgradePlanner{ planner: stubUpgradePlanner{
image: "v1.0.0", image: "v1.0.0",
}, },
@ -322,7 +322,7 @@ func TestUpgradePlan(t *testing.T) {
verifier: singleUUIDVerifier(), verifier: singleUUIDVerifier(),
}, },
"failing search should not result in error": { "failing search should not result in error": {
patchLister: stubPatchLister{list: availablePatches}, patchLister: stubVersionListFetcher{list: availablePatches},
planner: stubUpgradePlanner{ planner: stubUpgradePlanner{
image: "v1.0.0", image: "v1.0.0",
}, },
@ -341,7 +341,7 @@ func TestUpgradePlan(t *testing.T) {
wantUpgrade: true, wantUpgrade: true,
}, },
"failing verify should not result in error": { "failing verify should not result in error": {
patchLister: stubPatchLister{list: availablePatches}, patchLister: stubVersionListFetcher{list: availablePatches},
planner: stubUpgradePlanner{ planner: stubUpgradePlanner{
image: "v1.0.0", image: "v1.0.0",
}, },
@ -488,11 +488,11 @@ func (u stubUpgradePlanner) GetCurrentImage(context.Context) (*unstructured.Unst
return nil, u.image, u.err return nil, u.image, u.err
} }
type stubPatchLister struct { type stubVersionListFetcher struct {
list versionsapi.List list versionsapi.List
err error err error
} }
func (s stubPatchLister) PatchVersionsOf(ctx context.Context, ref, stream, minor, kind string) (*versionsapi.List, error) { func (s stubVersionListFetcher) FetchVersionList(context.Context, versionsapi.List) (versionsapi.List, error) {
return &s.list, s.err return s.list, s.err
} }