Skip to content

Commit

Permalink
Fixed kaniko job naming (#1520)
Browse files Browse the repository at this point in the history
  • Loading branch information
FelGel committed Feb 20, 2020
1 parent 75b0cad commit 926a715
Showing 1 changed file with 36 additions and 12 deletions.
48 changes: 36 additions & 12 deletions pkg/containerimagebuilderpusher/kaniko.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"os"
"os/exec"
"path"
"regexp"
"strings"
"time"

Expand All @@ -25,6 +26,7 @@ type Kaniko struct {
kubeClientSet kubernetes.Interface
logger logger.Logger
builderConfiguration *ContainerBuilderConfiguration
jobNameRegex *regexp.Regexp
}

func NewKaniko(logger logger.Logger, kubeClientSet kubernetes.Interface,
Expand All @@ -34,10 +36,18 @@ func NewKaniko(logger logger.Logger, kubeClientSet kubernetes.Interface,
return nil, errors.New("Missing kaniko builder configuration")
}

// Valid job name is composed from a DNS-1123 subdomains which in turn must contain only lower case
// alphanumeric characters, '-' or '.', and must start and end with an alphanumeric character (e.g. 'example.com')
jobNameRegex, err := regexp.Compile(`^[a-z0-9]([-a-z0-9]*[a-z0-9])?(\.[a-z0-9]([-a-z0-9]*[a-z0-9])?)*$`)
if err != nil {
return nil, errors.New("Failed to compile job name regex")
}

kanikoBuilder := &Kaniko{
logger: logger,
kubeClientSet: kubeClientSet,
builderConfiguration: builderConfiguration,
jobNameRegex: jobNameRegex,
}

return kanikoBuilder, nil
Expand Down Expand Up @@ -196,17 +206,7 @@ func (k *Kaniko) getKanikoJobSpec(namespace string, buildOptions *BuildOptions,
MountPath: "/tmp",
}

functionName := strings.Replace(buildOptions.Image, "/", "-", -1)
functionName = strings.Replace(functionName, ":", "-", -1)
timestamp := fmt.Sprintf("%d", time.Now().Unix())

// Truncate function name so the job name won't exceed k8s limit of 63
functionNameLimit := 63 - (len(k.builderConfiguration.JobPrefix) + len(timestamp) + 2)
if len(functionName) > functionNameLimit {
functionName = functionName[0:functionNameLimit]
}

jobName := fmt.Sprintf("%s.%s.%s", k.builderConfiguration.JobPrefix, functionName, timestamp)
jobName := k.compileJobName(buildOptions.Image)

kanikoJobSpec := &batch_v1.Job{
ObjectMeta: meta_v1.ObjectMeta{
Expand Down Expand Up @@ -300,6 +300,30 @@ func (k *Kaniko) getKanikoJobSpec(namespace string, buildOptions *BuildOptions,
return kanikoJobSpec
}

func (k *Kaniko) compileJobName(image string) string {

functionName := strings.Replace(image, "/", "", -1)
functionName = strings.Replace(functionName, ":", "", -1)
functionName = strings.Replace(functionName, "-", "", -1)
timestamp := fmt.Sprintf("%d", time.Now().Unix())

// Truncate function name so the job name won't exceed k8s limit of 63
functionNameLimit := 63 - (len(k.builderConfiguration.JobPrefix) + len(timestamp) + 2)
if len(functionName) > functionNameLimit {
functionName = functionName[0:functionNameLimit]
}

jobName := fmt.Sprintf("%s.%s.%s", k.builderConfiguration.JobPrefix, functionName, timestamp)

// Fallback
if !k.jobNameRegex.MatchString(jobName) {
k.logger.DebugWith("Kaniko job name does not match k8s regex. Won't use function name", "jobName", jobName)
jobName = fmt.Sprintf("%s.%s", k.builderConfiguration.JobPrefix, timestamp)
}

return jobName
}

func (k *Kaniko) waitForKanikoJobCompletion(namespace string, jobName string, BuildTimeoutSeconds int64) error {
k.logger.DebugWith("Waiting for kaniko to finish", "BuildTimeoutSeconds", BuildTimeoutSeconds)
timeout := time.Now().Add(time.Duration(BuildTimeoutSeconds) * time.Second)
Expand All @@ -318,7 +342,7 @@ func (k *Kaniko) waitForKanikoJobCompletion(namespace string, jobName string, Bu
return nil
}

k.logger.Debug("Kaniko job was completed successfully", "jobLogs", jobLogs)
k.logger.DebugWith("Kaniko job was completed successfully", "jobLogs", jobLogs)
return nil
}
if runningJob.Status.Failed > 0 {
Expand Down

0 comments on commit 926a715

Please sign in to comment.