Skip to content

Commit

Permalink
simplify code by switching from docker lib to util pkg
Browse files Browse the repository at this point in the history
- use func in util pkg instead of exteral docker reference library to simplify the code
  • Loading branch information
nader-ziada committed Apr 7, 2020
1 parent dfdc1d2 commit 6bb8d92
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 48 deletions.
46 changes: 6 additions & 40 deletions cmd/clusterctl/client/config/imagemeta_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
Expand Down Expand Up @@ -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
}
15 changes: 7 additions & 8 deletions controlplane/kubeadm/internal/workload_cluster_coredns.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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"
)
Expand Down Expand Up @@ -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)
}
Expand All @@ -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
}
Expand All @@ -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),
Expand Down

0 comments on commit 6bb8d92

Please sign in to comment.