Skip to content

Commit

Permalink
Pass the SCC credentials in pullImage() to factorize code
Browse files Browse the repository at this point in the history
pullImage() needs the SCC credentials, but we don't want the
InspectHost() function to be called multiple times in one command. We
thus need to pass it down.
  • Loading branch information
cbosdo committed Jul 12, 2024
1 parent a9fc1b3 commit 4692c1d
Show file tree
Hide file tree
Showing 7 changed files with 47 additions and 54 deletions.
6 changes: 1 addition & 5 deletions mgradm/cmd/install/podman/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,12 +87,8 @@ func installForPodman(
if err != nil {
return utils.Errorf(err, L("failed to compute image URL"))
}
pullArgs := []string{}
if inspectedHostValues.SccUsername != "" && inspectedHostValues.SccPassword != "" {
pullArgs = append(pullArgs, "--creds", inspectedHostValues.SccUsername+":"+inspectedHostValues.SccPassword)
}

preparedImage, err := shared_podman.PrepareImage(image, flags.Image.PullPolicy, pullArgs...)
preparedImage, err := shared_podman.PrepareImage(inspectedHostValues, image, flags.Image.PullPolicy)
if err != nil {
return err
}
Expand Down
8 changes: 1 addition & 7 deletions mgradm/cmd/migrate/podman/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,18 +32,12 @@ func migrateToPodman(globalFlags *types.GlobalFlags, flags *podmanMigrateFlags,
return utils.Errorf(err, L("cannot compute image"))
}

// FIXME all this code should be centralized. Now it being called in several different places.
inspectedHostValues, err := utils.InspectHost(false)
if err != nil {
return utils.Errorf(err, L("cannot inspect host values"))
}

pullArgs := []string{}
if inspectedHostValues.SccUsername != "" && inspectedHostValues.SccPassword != "" {
pullArgs = append(pullArgs, "--creds", inspectedHostValues.SccUsername+":"+inspectedHostValues.SccPassword)
}

preparedImage, err := podman_utils.PrepareImage(serverImage, flags.Image.PullPolicy, pullArgs...)
preparedImage, err := podman_utils.PrepareImage(inspectedHostValues, serverImage, flags.Image.PullPolicy)
if err != nil {
return err
}
Expand Down
14 changes: 2 additions & 12 deletions mgradm/shared/podman/podman.go
Original file line number Diff line number Diff line change
Expand Up @@ -247,12 +247,7 @@ func RunPgsqlVersionUpgrade(image types.ImageFlags, upgradeImage types.ImageFlag
return utils.Errorf(err, L("cannot inspect host values"))
}

pullArgs := []string{}
if inspectedHostValues.SccUsername != "" && inspectedHostValues.SccPassword != "" {
pullArgs = append(pullArgs, "--creds", inspectedHostValues.SccUsername+":"+inspectedHostValues.SccPassword)
}

preparedImage, err := podman.PrepareImage(upgradeImageUrl, image.PullPolicy, pullArgs...)
preparedImage, err := podman.PrepareImage(inspectedHostValues, upgradeImageUrl, image.PullPolicy)
if err != nil {
return err
}
Expand Down Expand Up @@ -340,12 +335,7 @@ func Upgrade(image types.ImageFlags, upgradeImage types.ImageFlags, cocoImage ty
return utils.Errorf(err, L("cannot inspect host values"))
}

pullArgs := []string{}
if inspectedHostValues.SccUsername != "" && inspectedHostValues.SccPassword != "" {
pullArgs = append(pullArgs, "--creds", inspectedHostValues.SccUsername+":"+inspectedHostValues.SccPassword)
}

preparedImage, err := podman.PrepareImage(serverImage, image.PullPolicy, pullArgs...)
preparedImage, err := podman.PrepareImage(inspectedHostValues, serverImage, image.PullPolicy)
if err != nil {
return err
}
Expand Down
15 changes: 10 additions & 5 deletions mgrpxy/cmd/install/podman/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,23 +37,28 @@ func installForPodman(globalFlags *types.GlobalFlags, flags *podman.PodmanProxyF
return shared_utils.Errorf(err, L("failed to extract proxy config from %s file"), configPath)
}

httpdImage, err := podman.GetContainerImage(&flags.ProxyImageFlags, "httpd")
inspectedHostValues, err := shared_utils.InspectHost(true)
if err != nil {
return shared_utils.Errorf(err, L("cannot inspect host values"))
}

httpdImage, err := podman.GetContainerImage(inspectedHostValues, &flags.ProxyImageFlags, "httpd")
if err != nil {
return err
}
saltBrokerImage, err := podman.GetContainerImage(&flags.ProxyImageFlags, "salt-broker")
saltBrokerImage, err := podman.GetContainerImage(inspectedHostValues, &flags.ProxyImageFlags, "salt-broker")
if err != nil {
return err
}
squidImage, err := podman.GetContainerImage(&flags.ProxyImageFlags, "squid")
squidImage, err := podman.GetContainerImage(inspectedHostValues, &flags.ProxyImageFlags, "squid")
if err != nil {
return err
}
sshImage, err := podman.GetContainerImage(&flags.ProxyImageFlags, "ssh")
sshImage, err := podman.GetContainerImage(inspectedHostValues, &flags.ProxyImageFlags, "ssh")
if err != nil {
return err
}
tftpdImage, err := podman.GetContainerImage(&flags.ProxyImageFlags, "tftpd")
tftpdImage, err := podman.GetContainerImage(inspectedHostValues, &flags.ProxyImageFlags, "tftpd")
if err != nil {
return err
}
Expand Down
33 changes: 17 additions & 16 deletions mgrpxy/shared/podman/podman.go
Original file line number Diff line number Diff line change
Expand Up @@ -151,19 +151,14 @@ func getHttpProxyConfig() string {
}

// GetContainerImage returns a proxy image URL.
func GetContainerImage(flags *utils.ProxyImageFlags, name string) (string, error) {
func GetContainerImage(
inspectedHostValues *shared_utils.HostInspectData,
flags *utils.ProxyImageFlags,
name string,
) (string, error) {
image := flags.GetContainerImage(name)
inspectedHostValues, err := shared_utils.InspectHost(true)
if err != nil {
return "", shared_utils.Errorf(err, L("cannot inspect host values"))
}

pullArgs := []string{}
if inspectedHostValues.SccUsername != "" && inspectedHostValues.SccPassword != "" {
pullArgs = append(pullArgs, "--creds", inspectedHostValues.SccUsername+":"+inspectedHostValues.SccPassword)
}

preparedImage, err := podman.PrepareImage(image, flags.PullPolicy, pullArgs...)
preparedImage, err := podman.PrepareImage(inspectedHostValues, image, flags.PullPolicy)
if err != nil {
return "", err
}
Expand Down Expand Up @@ -220,23 +215,29 @@ func Upgrade(globalFlags *types.GlobalFlags, flags *PodmanProxyFlags, cmd *cobra
if err := podman.StopService(podman.ProxyService); err != nil {
return err
}
httpdImage, err := GetContainerImage(&flags.ProxyImageFlags, "httpd")

inspectedHostValues, err := shared_utils.InspectHost(true)
if err != nil {
return shared_utils.Errorf(err, L("cannot inspect host values"))
}

httpdImage, err := GetContainerImage(inspectedHostValues, &flags.ProxyImageFlags, "httpd")
if err != nil {
log.Warn().Msgf(L("cannot find httpd image: it will no be upgraded"))
}
saltBrokerImage, err := GetContainerImage(&flags.ProxyImageFlags, "salt-broker")
saltBrokerImage, err := GetContainerImage(inspectedHostValues, &flags.ProxyImageFlags, "salt-broker")
if err != nil {
log.Warn().Msgf(L("cannot find salt-broker image: it will no be upgraded"))
}
squidImage, err := GetContainerImage(&flags.ProxyImageFlags, "squid")
squidImage, err := GetContainerImage(inspectedHostValues, &flags.ProxyImageFlags, "squid")
if err != nil {
log.Warn().Msgf(L("cannot find squid image: it will no be upgraded"))
}
sshImage, err := GetContainerImage(&flags.ProxyImageFlags, "ssh")
sshImage, err := GetContainerImage(inspectedHostValues, &flags.ProxyImageFlags, "ssh")
if err != nil {
log.Warn().Msgf(L("cannot find ssh image: it will no be upgraded"))
}
tftpdImage, err := GetContainerImage(&flags.ProxyImageFlags, "tftpd")
tftpdImage, err := GetContainerImage(inspectedHostValues, &flags.ProxyImageFlags, "tftpd")
if err != nil {
log.Warn().Msgf(L("cannot find tftpd image: it will no be upgraded"))
}
Expand Down
18 changes: 15 additions & 3 deletions shared/podman/images.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,12 @@ const rpmImageDir = "/usr/share/suse-docker-images/native/"
// Ensure the container image is pulled or pull it if the pull policy allows it.
//
// Returns the image name to use. Note that it may be changed if the image has been loaded from a local RPM package.
func PrepareImage(image string, pullPolicy string, args ...string) (string, error) {
func PrepareImage(
inspectedHostValues *utils.HostInspectData,
image string,
pullPolicy string,
args ...string,
) (string, error) {
if strings.ToLower(pullPolicy) != "always" {
log.Info().Msgf(L("Ensure image %s is available"), image)

Expand Down Expand Up @@ -62,7 +67,7 @@ func PrepareImage(image string, pullPolicy string, args ...string) (string, erro

if strings.ToLower(pullPolicy) != "never" {
log.Debug().Msgf("Pulling image %s because it is missing and pull policy is not 'never'", image)
return image, pullImage(image, args...)
return image, pullImage(inspectedHostValues, image, args...)
}

return image, fmt.Errorf(L("image %s is missing and cannot be fetched"), image)
Expand Down Expand Up @@ -203,14 +208,21 @@ func GetPulledImageName(image string) (string, error) {
return string(bytes.TrimSpace(out)), nil
}

func pullImage(image string, args ...string) error {
func pullImage(inspectedHostValues *utils.HostInspectData, image string, args ...string) error {
if utils.ContainsUpperCase(image) {
return fmt.Errorf(L("%s should contains just lower case character, otherwise podman pull would fails"), image)
}

// Do we need to authenticate?

log.Info().Msgf(L("Running podman pull %s"), image)
podmanImageArgs := []string{"pull", image}
podmanArgs := append(podmanImageArgs, args...)

if inspectedHostValues.SccUsername != "" && inspectedHostValues.SccPassword != "" {
podmanArgs = append(podmanArgs, "--creds", inspectedHostValues.SccUsername+":"+inspectedHostValues.SccPassword)
}

loglevel := zerolog.DebugLevel
if len(args) > 0 {
loglevel = zerolog.Disabled
Expand Down
7 changes: 1 addition & 6 deletions shared/podman/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -185,12 +185,7 @@ func Inspect(serverImage string, pullPolicy string, proxyHost bool) (*utils.Serv
return nil, utils.Errorf(err, L("cannot inspect host values"))
}

pullArgs := []string{}
if inspectedHostValues.SccUsername != "" && inspectedHostValues.SccPassword != "" {
pullArgs = append(pullArgs, "--creds", inspectedHostValues.SccUsername+":"+inspectedHostValues.SccPassword)
}

preparedImage, err := PrepareImage(serverImage, pullPolicy, pullArgs...)
preparedImage, err := PrepareImage(inspectedHostValues, serverImage, pullPolicy)
if err != nil {
return nil, err
}
Expand Down

0 comments on commit 4692c1d

Please sign in to comment.