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(helm): don't panic if reference parsing fails #8796

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
28 changes: 16 additions & 12 deletions pkg/skaffold/helm/args.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,11 @@ func ConstructOverrideArgs(r *latest.HelmRelease, builds []graph.Artifact, args
idxSuffix = strconv.Itoa(idx + 1)
}

for k, v := range envVarForImage(b.ImageName, b.Tag) {
vars, err := envVarForImage(b.ImageName, b.Tag)
if err != nil {
return nil, err
}
for k, v := range vars {
envMap[k+idxSuffix] = v
envMap[k+"_"+nameSuffix] = v
}
Expand Down Expand Up @@ -116,25 +120,25 @@ func GetArgs(releaseName string, namespace string) []string {
}

// envVarForImage creates an environment map for an image and digest tag (fqn)
func envVarForImage(imageName string, digest string) map[string]string {
func envVarForImage(imageName string, digest string) (map[string]string, error) {
customMap := map[string]string{
"IMAGE_NAME": imageName,
"DIGEST": digest, // The `DIGEST` name is kept for compatibility reasons
}

if digest == "" {
return customMap, nil
}

// Standardize access to Image reference fields in templates
ref, err := docker.ParseReference(digest)
if err == nil {
customMap[constants.ImageRef.Repo] = ref.BaseName
customMap[constants.ImageRef.Tag] = ref.Tag
customMap[constants.ImageRef.Digest] = ref.Digest
} else {
log.Entry(context.TODO()).Warnf("unable to extract values for %v, %v and %v from image %v due to error:\n%v", constants.ImageRef.Repo, constants.ImageRef.Tag, constants.ImageRef.Digest, digest, err)
if err != nil {
return nil, fmt.Errorf("unable to extract values for %v, %v and %v from image %v due to error: %w", constants.ImageRef.Repo, constants.ImageRef.Tag, constants.ImageRef.Digest, digest, err)
}

if digest == "" {
return customMap
}
customMap[constants.ImageRef.Repo] = ref.BaseName
customMap[constants.ImageRef.Tag] = ref.Tag
customMap[constants.ImageRef.Digest] = ref.Digest

// DIGEST_ALGO and DIGEST_HEX are deprecated and will contain nonsense values
names := strings.SplitN(digest, ":", 2)
Expand All @@ -149,5 +153,5 @@ func envVarForImage(imageName string, digest string) map[string]string {
customMap["IMAGE_DOMAIN"] = ref.Domain
customMap["IMAGE_REPO_NO_DOMAIN"] = strings.TrimPrefix(ref.BaseName, ref.Domain+"/")
customMap["IMAGE_FULLY_QUALIFIED"] = digest
return customMap
return customMap, nil
}