Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix registry urls on generate missing images list and adds registry info to digests #467

Merged
merged 7 commits into from
Aug 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions cmd/release/cmd/generate.go
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ var rancherGenerateMissingImagesListSubCmd = &cobra.Command{
if len(checkImages) == 0 && imagesListURL == "" {
return errors.New("either --images-list-url or --check-images must be provided")
}
missingImages, err := rancher.GenerateMissingImagesList(imagesListURL, registry, concurrencyLimit, checkImages, ignoreImages)
missingImages, err := rancher.GenerateMissingImagesList(imagesListURL, registry, concurrencyLimit, checkImages, ignoreImages, verbose)
if err != nil {
return err
}
Expand All @@ -147,7 +147,7 @@ var rancherGenerateDockerImagesDigestsSubCmd = &cobra.Command{
Use: "docker-images-digests",
Short: "Generate a file with images digests from an images list",
RunE: func(cmd *cobra.Command, args []string) error {
return rancher.GenerateDockerImageDigests(rancherImagesDigestsOutputFile, rancherImagesDigestsImagesURL, rancherImagesDigestsRegistry)
return rancher.GenerateDockerImageDigests(rancherImagesDigestsOutputFile, rancherImagesDigestsImagesURL, rancherImagesDigestsRegistry, verbose)
},
}

Expand Down
5 changes: 3 additions & 2 deletions cmd/release/cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package cmd

import (
"fmt"
"log"
"os"
"strings"

Expand All @@ -15,6 +14,7 @@ var (
dryRun bool
ignoreValidate bool
rootConfig *config.Config
verbose bool
configFile string
stringConfig string
)
Expand Down Expand Up @@ -44,6 +44,7 @@ func SetVersion(version string) {
func init() {
rootCmd.PersistentFlags().BoolVarP(&debug, "debug", "D", false, "Debug")
rootCmd.PersistentFlags().BoolVarP(&dryRun, "dry-run", "R", false, "Dry Run")
rootCmd.PersistentFlags().BoolVarP(&verbose, "verbose", "V", false, "Verbose output")
rootCmd.PersistentFlags().BoolVarP(&ignoreValidate, "ignore-validate", "I", false, "Ignore the validate config step")
rootCmd.PersistentFlags().StringVarP(&configFile, "config-file", "c", "$HOME/.ecm-distro-tools/config.json", "Path for the config.json file")
rootCmd.PersistentFlags().StringVarP(&stringConfig, "config", "C", "", "JSON config string")
Expand All @@ -67,7 +68,7 @@ func initConfig() {
configFile = os.ExpandEnv(configFile)
conf, err = config.Load(configFile)
if err != nil {
log.Println("failed to load config, use 'release config gen' to create a new one at: " + configFile)
fmt.Println("failed to load config, use 'release config gen' to create a new one at: " + configFile)
fmt.Println(err)
os.Exit(1)
}
Expand Down
32 changes: 32 additions & 0 deletions log/log.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package log

import "fmt"

type Logger struct {
verbose bool
}

func NewLogger(verbose bool) Logger {
return Logger{verbose: verbose}
}

func (l Logger) Print(a ...any) (int, error) {
if l.verbose {
return fmt.Print(a...)
}
return 0, nil
}

func (l Logger) Printf(format string, a ...any) (int, error) {
if l.verbose {
return fmt.Printf(format, a...)
}
return 0, nil
}

func (l Logger) Println(a ...any) (int, error) {
if l.verbose {
return fmt.Println(a...)
}
return 0, nil
}
23 changes: 14 additions & 9 deletions release/rancher/rancher.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ import (
"io"
"io/ioutil"
"log"
"log/slog"
"net/http"
"os"
"path"
Expand All @@ -28,6 +27,7 @@ import (
"github.com/google/go-github/v39/github"
ecmConfig "github.com/rancher/ecm-distro-tools/cmd/release/config"
ecmHTTP "github.com/rancher/ecm-distro-tools/http"
ecmLog "github.com/rancher/ecm-distro-tools/log"
"github.com/rancher/ecm-distro-tools/release"
"github.com/rancher/ecm-distro-tools/repository"
"golang.org/x/mod/semver"
Expand Down Expand Up @@ -386,7 +386,8 @@ func formatContentLine(line string) string {
return strings.TrimSpace(line)
}

func GenerateMissingImagesList(imagesListURL, registry string, concurrencyLimit int, checkImages, ignoreImages []string) ([]string, error) {
func GenerateMissingImagesList(imagesListURL, registry string, concurrencyLimit int, checkImages, ignoreImages []string, verbose bool) ([]string, error) {
log := ecmLog.NewLogger(verbose)
if len(checkImages) == 0 {
if imagesListURL == "" {
return nil, errors.New("if no images are provided, an images list URL must be provided")
Expand All @@ -413,6 +414,11 @@ func GenerateMissingImagesList(imagesListURL, registry string, concurrencyLimit
repositoryAuths := make(map[string]string)
mu := sync.RWMutex{}

rgInfo, ok := registriesInfo[registry]
if !ok {
return nil, errors.New("registry must be one of the following: 'docker.io', 'registry.rancher.com' or 'stgregistry.suse.com'")
}

for _, imageAndVersion := range checkImages {
image, imageVersion, err := splitImageAndVersion(imageAndVersion)
if err != nil {
Expand Down Expand Up @@ -441,7 +447,7 @@ func GenerateMissingImagesList(imagesListURL, registry string, concurrencyLimit

auth, ok = repositoryAuths[image]
if !ok {
auth, err = registryAuth(sccSUSEURL, sccSUSEService, image)
auth, err = registryAuth(rgInfo.AuthURL, rgInfo.Service, image)
if err != nil {
cancel()
return err
Expand All @@ -450,7 +456,7 @@ func GenerateMissingImagesList(imagesListURL, registry string, concurrencyLimit
}
mu.Unlock()

exists, err := checkIfImageExists(rancherRegistryBaseURL, image, imageVersion, auth)
exists, err := checkIfImageExists(rgInfo.BaseURL, image, imageVersion, auth)
if err != nil {
cancel()
return err
Expand Down Expand Up @@ -521,7 +527,7 @@ func validateRepoImage(repoImage string) error {
return nil
}

func GenerateDockerImageDigests(outputFile, imagesFileURL, registry string) error {
func GenerateDockerImageDigests(outputFile, imagesFileURL, registry string, verbose bool) error {
imagesDigests, err := dockerImagesDigests(imagesFileURL, registry)
if err != nil {
return err
Expand All @@ -547,7 +553,6 @@ func dockerImagesDigests(imagesFileURL, registry string) (imageDigest, error) {
if imageAndVersion == "" || imageAndVersion == " " {
continue
}
slog.Info("image: " + imageAndVersion)
if !strings.Contains(imageAndVersion, ":") {
return nil, errors.New("malformed image name: , missing ':'")
}
Expand All @@ -562,12 +567,12 @@ func dockerImagesDigests(imagesFileURL, registry string) (imageDigest, error) {
}
repositoryAuths[image] = auth
}
digest, statusCode, err := dockerImageDigest(rgInfo.BaseURL, image, imageVersion, repositoryAuths[image])
slog.Info("status code: " + strconv.Itoa(statusCode))
digest, _, err := dockerImageDigest(rgInfo.BaseURL, image, imageVersion, repositoryAuths[image])
if err != nil {
return nil, err
}
imagesDigests[imageAndVersion] = digest
// e.g: registry.rancher.com/rancher/rancher:v2.9.0 = sha256:1234567890
imagesDigests[registry+"/"+imageAndVersion] = digest
}
return imagesDigests, nil
}
Expand Down
Loading