From 094ef3b89555f847c13af0edb9e481d8e1ec132d Mon Sep 17 00:00:00 2001 From: Nader Ziada Date: Tue, 7 Apr 2020 11:53:31 -0400 Subject: [PATCH] simplify code by switching from docker lib to util pkg - use func in util pkg instead of exteral docker reference library to simplify the code --- .../client/config/imagemeta_client.go | 46 +++---------------- .../internal/workload_cluster_coredns.go | 15 +++--- 2 files changed, 13 insertions(+), 48 deletions(-) diff --git a/cmd/clusterctl/client/config/imagemeta_client.go b/cmd/clusterctl/client/config/imagemeta_client.go index 334bc282bda6..146005b2c122 100644 --- a/cmd/clusterctl/client/config/imagemeta_client.go +++ b/cmd/clusterctl/client/config/imagemeta_client.go @@ -17,11 +17,10 @@ limitations under the License. package config import ( - "fmt" "strings" - "github.com/docker/distribution/reference" "github.com/pkg/errors" + "sigs.k8s.io/cluster-api/util/container" ) const ( @@ -119,53 +118,20 @@ func (i *imageMeta) Union(other *imageMeta) { // ApplyToImage changes an image name applying the transformations defined in the current imageMeta. func (i *imageMeta) ApplyToImage(image string) (string, error) { - // Splits the image name into its own composing parts - ref, err := reference.ParseNormalizedNamed(image) + + newImage, err := container.ImageFromString(image) if err != nil { return "", err } // apply transformations if i.Repository != "" { - // store tag & digest for rebuilding the image name - tagged, hasTag := ref.(reference.Tagged) - digested, hasDigest := ref.(reference.Digested) - - // detect the image name, dropping host and path if any - name := ref.Name() - imageNameIndex := strings.LastIndex(name, "/") - if imageNameIndex != -1 { - name = strings.TrimPrefix(name[imageNameIndex+1:], "/") - } - - // parse the new image resulting by concatenating the new repository and the image name - ref, err = reference.ParseNormalizedNamed(fmt.Sprintf("%s/%s", strings.TrimSuffix(i.Repository, "/"), name)) - if err != nil { - return "", err - } - - // applies back tag & digest - if hasTag { - ref, err = reference.WithTag(ref, tagged.Tag()) - if err != nil { - return "", err - } - } - - if hasDigest { - ref, err = reference.WithDigest(ref, digested.Digest()) - if err != nil { - return "", err - } - } + newImage.Repository = strings.TrimSuffix(i.Repository, "/") } if i.Tag != "" { - ref, err = reference.WithTag(reference.TrimNamed(ref), i.Tag) - if err != nil { - return "", err - } + newImage.Tag = i.Tag } // returns the resulting image name - return ref.String(), nil + return newImage.String(), nil } diff --git a/controlplane/kubeadm/internal/workload_cluster_coredns.go b/controlplane/kubeadm/internal/workload_cluster_coredns.go index 74386d76f686..ad795f6fbc12 100644 --- a/controlplane/kubeadm/internal/workload_cluster_coredns.go +++ b/controlplane/kubeadm/internal/workload_cluster_coredns.go @@ -21,7 +21,6 @@ import ( "fmt" "github.com/coredns/corefile-migration/migration" - "github.com/docker/distribution/reference" "github.com/pkg/errors" appsv1 "k8s.io/api/apps/v1" corev1 "k8s.io/api/core/v1" @@ -30,6 +29,7 @@ import ( kubeadmv1 "sigs.k8s.io/cluster-api/bootstrap/kubeadm/types/v1beta1" controlplanev1 "sigs.k8s.io/cluster-api/controlplane/kubeadm/api/v1alpha3" "sigs.k8s.io/cluster-api/util" + containerutil "sigs.k8s.io/cluster-api/util/container" "sigs.k8s.io/cluster-api/util/patch" ctrlclient "sigs.k8s.io/controller-runtime/pkg/client" ) @@ -143,13 +143,13 @@ func (w *Workload) getCoreDNSInfo(ctx context.Context, clusterConfig *kubeadmv1. } // Parse container image. - parsedImage, err := reference.ParseNormalizedNamed(container.Image) + parsedImage, err := containerutil.ImageFromString(container.Image) if err != nil { return nil, errors.Wrapf(err, "unable to parse %q deployment image", container.Image) } // Handle imageRepository. - toImageRepository := fmt.Sprintf("%s/%s", reference.Domain(parsedImage), reference.Path(parsedImage)) + toImageRepository := fmt.Sprintf("%s/%s", parsedImage.Repository, parsedImage.Name) if clusterConfig.ImageRepository != "" { toImageRepository = fmt.Sprintf("%s/%s", clusterConfig.ImageRepository, coreDNSKey) } @@ -158,15 +158,14 @@ func (w *Workload) getCoreDNSInfo(ctx context.Context, clusterConfig *kubeadmv1. } // Handle imageTag. - imageRefTag, ok := parsedImage.(reference.Tagged) - if !ok { + if parsedImage.Tag == "" { return nil, errors.Errorf("failed to update coredns deployment: does not have a valid image tag: %q", container.Image) } - currentMajorMinorPatch, err := extractImageVersion(imageRefTag.Tag()) + currentMajorMinorPatch, err := extractImageVersion(parsedImage.Tag) if err != nil { return nil, err } - toImageTag := imageRefTag.Tag() + toImageTag := parsedImage.Tag if clusterConfig.DNS.ImageTag != "" { toImageTag = clusterConfig.DNS.ImageTag } @@ -180,7 +179,7 @@ func (w *Workload) getCoreDNSInfo(ctx context.Context, clusterConfig *kubeadmv1. Deployment: deployment, CurrentMajorMinorPatch: currentMajorMinorPatch, TargetMajorMinorPatch: targetMajorMinorPatch, - FromImageTag: imageRefTag.Tag(), + FromImageTag: parsedImage.Tag, ToImageTag: toImageTag, FromImage: container.Image, ToImage: fmt.Sprintf("%s:%s", toImageRepository, toImageTag),