From 64a3aac8951ae96a1ea105361ba1c006323e6603 Mon Sep 17 00:00:00 2001 From: Ygal Blum Date: Wed, 27 Jul 2022 09:09:19 +0300 Subject: [PATCH] job/osbuild: move utility the `container` package Extract a helper method that parses the reference and applies defaults, i.e. registry and path, and move it to the contaier package. --- cmd/osbuild-worker/jobimpl-osbuild.go | 16 ++-------------- internal/container/client.go | 19 +++++++++++++++++++ 2 files changed, 21 insertions(+), 14 deletions(-) diff --git a/cmd/osbuild-worker/jobimpl-osbuild.go b/cmd/osbuild-worker/jobimpl-osbuild.go index 2ee6d8db0..da393ea32 100644 --- a/cmd/osbuild-worker/jobimpl-osbuild.go +++ b/cmd/osbuild-worker/jobimpl-osbuild.go @@ -208,25 +208,13 @@ func uploadToS3(a *awscloud.AWS, outputDirectory, exportPath, bucket, key, filen } func (impl *OSBuildJobImpl) getContainerClient(destination string, targetOptions *target.ContainerTargetOptions) (*container.Client, error) { - useImpl := false - i := strings.IndexRune(destination, '/') - if i == -1 || (!strings.ContainsAny(destination[:i], ".:") && destination[:i] != "localhost") { - if impl.ContainersConfig.Domain != "" { - base := impl.ContainersConfig.Domain - if impl.ContainersConfig.Account != "" { - base = fmt.Sprintf("%s/%s", base, impl.ContainersConfig.Account) - } - destination = fmt.Sprintf("%s/%s", base, destination) - useImpl = true - } - } - + destination, appliedDefaults := container.ApplyDefaultDomainPath(destination, impl.ContainersConfig.Domain, impl.ContainersConfig.Account) client, err := container.NewClient(destination) if err != nil { return nil, err } - if useImpl { + if appliedDefaults { if impl.ContainersConfig.CertPath != "" { client.SetDockerCertPath(impl.ContainersConfig.CertPath) } diff --git a/internal/container/client.go b/internal/container/client.go index cad254f8a..3e93662ef 100644 --- a/internal/container/client.go +++ b/internal/container/client.go @@ -71,6 +71,25 @@ func GetDefaultAuthFile() string { return filepath.FromSlash("/var/empty/containers-auth.json") } +// ApplyDefaultPath checks if the target includes a domain and if it doesn't adds the default ones +// to the returned string. If also returns a bool indicating whether the defaults were applied +func ApplyDefaultDomainPath(target, defaultDomain, defaultPath string) (string, bool) { + appliedDefaults := false + i := strings.IndexRune(target, '/') + if i == -1 || (!strings.ContainsAny(target[:i], ".:") && target[:i] != "localhost") { + if defaultDomain != "" { + base := defaultDomain + if defaultPath != "" { + base = fmt.Sprintf("%s/%s", base, defaultPath) + } + target = fmt.Sprintf("%s/%s", base, target) + appliedDefaults = true + } + } + + return target, appliedDefaults +} + // A Client to interact with the given Target object at a // container registry, like e.g. uploading an image to. // All mentioned defaults are only set when using the