Skip to content

Commit

Permalink
fix(tekton): Add a random suffix to Tekton CRD name base to avoid dup…
Browse files Browse the repository at this point in the history
…lication

Signed-off-by: Andrew Bayer <andrew.bayer@gmail.com>
  • Loading branch information
abayer committed Jul 2, 2019
1 parent 16a6128 commit 231515b
Show file tree
Hide file tree
Showing 11 changed files with 55 additions and 29 deletions.
2 changes: 1 addition & 1 deletion pkg/cmd/step/create/step_create_meta_pipeline.go
Expand Up @@ -141,7 +141,7 @@ func (o *StepCreatePipelineOptions) Run() error {
return errors.Wrap(err, "unable to retrieve pod templates")
}

pipelineName := tekton.PipelineResourceNameFromGitInfo(gitInfo, o.Branch, o.Context, tekton.MetaPipeline)
pipelineName := tekton.PipelineResourceNameFromGitInfo(gitInfo, o.Branch, o.Context, tekton.MetaPipeline, tektonClient, ns)
buildNumber, err := tekton.GenerateNextBuildNumber(tektonClient, jxClient, ns, gitInfo, o.Branch, retryDuration, o.Context)
if err != nil {
return errors.Wrap(err, "unable to determine next build number")
Expand Down
2 changes: 1 addition & 1 deletion pkg/cmd/step/create/step_create_task.go
Expand Up @@ -255,7 +255,7 @@ func (o *StepCreateTaskOptions) Run() error {
return errors.Wrap(err, "Unable to load pod templates")
}

pipelineName := tekton.PipelineResourceNameFromGitInfo(o.GitInfo, o.Branch, o.Context, tekton.BuildPipeline)
pipelineName := tekton.PipelineResourceNameFromGitInfo(o.GitInfo, o.Branch, o.Context, tekton.BuildPipeline, tektonClient, ns)

exists, err = o.effectiveProjectConfigExists()
if err != nil {
Expand Down
17 changes: 12 additions & 5 deletions pkg/cmd/step/create/step_create_task_test.go
Expand Up @@ -2,10 +2,6 @@ package create

import (
"fmt"
"github.com/jenkins-x/jx/pkg/cmd/testhelpers"
"github.com/jenkins-x/jx/pkg/log"
"github.com/jenkins-x/jx/pkg/prow"
"github.com/jenkins-x/jx/pkg/tekton"
"io/ioutil"
"os"
"path"
Expand All @@ -14,12 +10,17 @@ import (
"testing"

"github.com/google/go-cmp/cmp/cmpopts"
"github.com/jenkins-x/jx/pkg/cmd/testhelpers"
"github.com/jenkins-x/jx/pkg/gits/mocks"
"github.com/jenkins-x/jx/pkg/helm/mocks"
"github.com/jenkins-x/jx/pkg/kube"
"github.com/jenkins-x/jx/pkg/log"
"github.com/jenkins-x/jx/pkg/prow"
"github.com/jenkins-x/jx/pkg/tekton"
"github.com/knative/pkg/kmp"
"github.com/satori/go.uuid"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/util/rand"

"github.com/ghodss/yaml"
"github.com/google/go-cmp/cmp"
Expand All @@ -31,6 +32,7 @@ import (
"github.com/jenkins-x/jx/pkg/tests"
"github.com/stretchr/testify/assert"
pipelineapi "github.com/tektoncd/pipeline/pkg/apis/pipeline/v1alpha1"
tektonfake "github.com/tektoncd/pipeline/pkg/client/clientset/versioned/fake"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)
Expand All @@ -50,6 +52,8 @@ func TestGenerateTektonCRDs(t *testing.T) {
_, err = os.Stat(packsDir)
assert.NoError(t, err)

rand.Seed(12345)

resolver := func(importFile *jenkinsfile.ImportFile) (string, error) {
dirPath := []string{packsDir, "import_dir", importFile.Import}
// lets handle cross platform paths in `importFile.File`
Expand Down Expand Up @@ -376,11 +380,14 @@ func TestGenerateTektonCRDs(t *testing.T) {
testhelpers.ConfigureTestOptionsWithResources(createTask.CommonOptions, k8sObjects, jxObjects, gits_test.NewMockGitter(), fakeGitProvider, helm_test.NewMockHelmer(), nil)

ns := "jx"
// Create a single duplicate PipelineResource for the name used by the 'kaniko_entrypoint' test case to verify that the deduplication logic works correctly.
tektonClient := tektonfake.NewSimpleClientset(tekton_helpers_test.AssertLoadPipelineResources(t, path.Join(testData, "prepopulated")))

effectiveProjectConfig, _ := createTask.createEffectiveProjectConfig(packsDir, projectConfig, projectConfigFile, resolver, ns)
if effectiveProjectConfig != nil {
createTask.setBuildVersion(effectiveProjectConfig.PipelineConfig)
}
pipelineName := tekton.PipelineResourceNameFromGitInfo(createTask.GitInfo, createTask.Branch, createTask.Context, tekton.BuildPipeline)
pipelineName := tekton.PipelineResourceNameFromGitInfo(createTask.GitInfo, createTask.Branch, createTask.Context, tekton.BuildPipeline, tektonClient, ns)
crds, err := createTask.generateTektonCRDs(effectiveProjectConfig, ns, pipelineName)
if tt.expectingError {
if err == nil {
Expand Down
Expand Up @@ -2,7 +2,7 @@ apiVersion: tekton.dev/v1alpha1
kind: Pipeline
metadata:
creationTimestamp: null
name: jenkins-x-jx-fix-kaniko-special-1
name: jenkins-x-jx-fix-kaniko-special-9l9zj-1
namespace: jx
labels:
branch: fix-kaniko-special-casing
Expand All @@ -16,7 +16,7 @@ spec:
images and helm charts
name: version
resources:
- name: jenkins-x-jx-fix-kaniko-special
- name: jenkins-x-jx-fix-kaniko-special-9l9zj
type: git
tasks:
- name: ci
Expand All @@ -26,7 +26,7 @@ spec:
resources:
inputs:
- name: workspace
resource: jenkins-x-jx-fix-kaniko-special
resource: jenkins-x-jx-fix-kaniko-special-9l9zj
taskRef:
name: jenkins-x-jx-fix-kaniko-special-ci-1
name: jenkins-x-jx-fix-kaniko-special-9l9zj-ci-1
status: {}
Expand Up @@ -3,7 +3,7 @@ items:
kind: PipelineResource
metadata:
creationTimestamp: null
name: jenkins-x-jx-fix-kaniko-special
name: jenkins-x-jx-fix-kaniko-special-9l9zj
spec:
params:
- name: revision
Expand All @@ -12,4 +12,4 @@ items:
value: https://github.com/jenkins-x/jx
type: git
status: {}
metadata: {}
metadata: {}
Expand Up @@ -7,19 +7,19 @@ metadata:
build: "1"
owner: jenkins-x
repo: jx
name: jenkins-x-jx-fix-kaniko-special-1
name: jenkins-x-jx-fix-kaniko-special-9l9zj-1
spec:
params:
- name: version
value: 0.0.1
pipelineRef:
apiVersion: tekton.dev/v1alpha1
name: jenkins-x-jx-fix-kaniko-special-1
name: jenkins-x-jx-fix-kaniko-special-9l9zj-1
resources:
- name: jenkins-x-jx-fix-kaniko-special
- name: jenkins-x-jx-fix-kaniko-special-9l9zj
resourceRef:
apiVersion: tekton.dev/v1alpha1
name: jenkins-x-jx-fix-kaniko-special
name: jenkins-x-jx-fix-kaniko-special-9l9zj
serviceAccount: tekton-bot
timeout: 240h0m0s
trigger:
Expand Down
@@ -1,6 +1,6 @@
metadata:
creationTimestamp: null
name: jenkins-x-jx-fix-kaniko-special-1
name: jenkins-x-jx-fix-kaniko-special-9l9zj-1
labels:
branch: fix-kaniko-special-casing
build: "1"
Expand All @@ -11,4 +11,4 @@ pipelineRunRef: null
stages:
- depth: 0
name: ci
taskRef: jenkins-x-jx-fix-kaniko-special-ci-1
taskRef: jenkins-x-jx-fix-kaniko-special-9l9zj-ci-1
Expand Up @@ -9,7 +9,7 @@ items:
build: "1"
owner: jenkins-x
repo: jx
name: jenkins-x-jx-fix-kaniko-special-ci-1
name: jenkins-x-jx-fix-kaniko-special-9l9zj-ci-1
namespace: jx
spec:
inputs:
Expand Down
@@ -0,0 +1,16 @@
items:
- apiVersion: tekton.dev/v1alpha1
kind: PipelineResource
metadata:
creationTimestamp: null
name: jenkins-x-jx-fix-kaniko-special
namespace: jx
spec:
params:
- name: revision
value: v0.0.1
- name: url
value: https://github.com/jenkins-x/jx
type: git
status: {}
metadata: {}
2 changes: 1 addition & 1 deletion pkg/tekton/pipeline_info.go
Expand Up @@ -141,7 +141,7 @@ func CreatePipelineRunInfo(prName string, podList *corev1.PodList, ps *v1.Pipeli
}

pri := &PipelineRunInfo{
Name: PipelineResourceName(pr.Labels[LabelOwner], pr.Labels[LabelRepo], pr.Labels[LabelBranch], pr.Labels[LabelContext], BuildPipeline) + "-" + pr.Labels[LabelBuild],
Name: PipelineResourceName(pr.Labels[LabelOwner], pr.Labels[LabelRepo], pr.Labels[LabelBranch], pr.Labels[LabelContext], BuildPipeline, nil, "") + "-" + pr.Labels[LabelBuild],
PipelineRun: pr.Name,
Pipeline: pr.Spec.PipelineRef.Name,
}
Expand Down
19 changes: 11 additions & 8 deletions pkg/tekton/pipelines.go
Expand Up @@ -9,6 +9,7 @@ import (
jenkinsio "github.com/jenkins-x/jx/pkg/apis/jenkins.io"
"github.com/jenkins-x/jx/pkg/kube/naming"
"github.com/jenkins-x/jx/pkg/prow"
"k8s.io/apimachinery/pkg/util/rand"

"github.com/jenkins-x/jx/pkg/client/clientset/versioned"
jxClient "github.com/jenkins-x/jx/pkg/client/clientset/versioned"
Expand Down Expand Up @@ -302,12 +303,12 @@ func CreateOrUpdatePipeline(tektonClient tektonclient.Interface, ns string, crea
}

// PipelineResourceNameFromGitInfo returns the pipeline resource name for the given git repository, branch and context
func PipelineResourceNameFromGitInfo(gitInfo *gits.GitRepository, branch string, context string, pipelineType PipelineType) string {
return PipelineResourceName(gitInfo.Organisation, gitInfo.Name, branch, context, pipelineType)
func PipelineResourceNameFromGitInfo(gitInfo *gits.GitRepository, branch string, context string, pipelineType PipelineType, tektonClient tektonclient.Interface, ns string) string {
return PipelineResourceName(gitInfo.Organisation, gitInfo.Name, branch, context, pipelineType, tektonClient, ns)
}

// PipelineResourceName returns the pipeline resource name for the given git org, repo name, branch and context
func PipelineResourceName(organisation string, name string, branch string, context string, pipelineType PipelineType) string {
func PipelineResourceName(organisation string, name string, branch string, context string, pipelineType PipelineType, tektonClient tektonclient.Interface, ns string) string {
dirtyName := organisation + "-" + name + "-" + branch
if context != "" {
dirtyName += "-" + context
Expand All @@ -316,12 +317,14 @@ func PipelineResourceName(organisation string, name string, branch string, conte
if pipelineType == MetaPipeline {
dirtyName = pipelineType.String() + "-" + dirtyName
}
// TODO: https://github.com/tektoncd/pipeline/issues/481 causes
// problems since autogenerated container names can end up surpassing 63
// characters, which is not allowed. Longest known prefix for now is 28
// chars (build-step-artifact-copy-to-), so we truncate to 35 so the
// generated container names are no more than 63 chars.
resourceName := naming.ToValidNameTruncated(dirtyName, 31)

if tektonClient != nil {
_, err := tektonClient.TektonV1alpha1().PipelineResources(ns).Get(resourceName, metav1.GetOptions{})
if err == nil {
return resourceName + "-" + rand.String(5)
}
}
return resourceName
}

Expand Down

0 comments on commit 231515b

Please sign in to comment.