Skip to content

Commit

Permalink
Make examples runnable with kubectl 🐇
Browse files Browse the repository at this point in the history
This remove the use of `ko://` in the examples folder, using the
`:latest` release image for those. This allow users to apply those
with `kubectl` directly.

To keep testing the "currently" built image, this introduce a
`test/yamls` folder where we use `ko://`. This updates the tests to
run both (`examples` and `test/yamls`).

Signed-off-by: Vincent Demeester <vdemeest@redhat.com>
  • Loading branch information
vdemeester committed Nov 26, 2020
1 parent 4fdd018 commit c37f7dd
Show file tree
Hide file tree
Showing 5 changed files with 534 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ spec:
description: The precise commit SHA that was fetched by this Task
steps:
- name: clone
image: ko://github.com/tektoncd/pipeline/cmd/git-init
image: gcr.io/tekton-releases/github.com/tektoncd/pipeline/cmd/git-init:latest
script: |
CHECKOUT_DIR="$(workspaces.output.path)/$(params.subdirectory)"
Expand Down
4 changes: 2 additions & 2 deletions examples/v1beta1/pipelineruns/pipelinerun.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ spec:
description: The precise commit SHA that was fetched by this Task
steps:
- name: clone
image: ko://github.com/tektoncd/pipeline/cmd/git-init
image: gcr.io/tekton-releases/github.com/tektoncd/pipeline/cmd/git-init:latest
script: |
CHECKOUT_DIR="$(workspaces.output.path)/$(params.subdirectory)"
cleandir() {
Expand Down Expand Up @@ -167,7 +167,7 @@ spec:
runAsUser: 0
- name: write-digest
workingDir: $(workspaces.source.path)
image: ko://github.com/tektoncd/pipeline/cmd/imagedigestexporter
image: gcr.io/tekton-releases/github.com/tektoncd/pipeline/cmd/imagedigestexporter:latest
# output of imagedigestexport [{"name":"image","digest":"sha256:eed29..660"}]
command: ["/ko-app/imagedigestexporter"]
securityContext:
Expand Down
22 changes: 18 additions & 4 deletions test/examples_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,12 @@ func koCreate(input []byte, namespace string) ([]byte, error) {
return cmd.CombinedOutput()
}

func kubectlCreate(input []byte, namespace string) ([]byte, error) {
cmd := exec.Command("kubectl", "create", "-n", namespace, "-f", "-")
cmd.Stdin = bytes.NewReader(input)
return cmd.CombinedOutput()
}

// deleteClusterTask removes a single clustertask by name using provided
// clientset. Test state is used for logging. deleteClusterTask does not wait
// for the clustertask to be deleted, so it is still possible to have name
Expand All @@ -109,9 +115,10 @@ func deleteClusterTask(ctx context.Context, t *testing.T, c *clients, name strin
}
}

type createFunc func(input []byte, namespace string) ([]byte, error)
type waitFunc func(ctx context.Context, t *testing.T, c *clients, name string)

func exampleTest(path string, waitValidateFunc waitFunc, kind string) func(t *testing.T) {
func exampleTest(path string, waitValidateFunc waitFunc, createFunc createFunc, kind string) func(t *testing.T) {
return func(t *testing.T) {
t.Parallel()
ctx := context.Background()
Expand All @@ -135,7 +142,7 @@ func exampleTest(path string, waitValidateFunc waitFunc, kind string) func(t *te
t.Skipf("Couldn't substitute environment: %v", err)
}

out, err := koCreate(subbedInput, namespace)
out, err := createFunc(subbedInput, namespace)
if err != nil {
t.Fatalf("%s Output: %s", err, out)
}
Expand Down Expand Up @@ -214,7 +221,14 @@ func extractTestName(baseDir string, path string) string {
}

func TestExamples(t *testing.T) {
baseDir := "../examples"
testYamls(t, "../examples", kubectlCreate)
}

func TestYamls(t *testing.T) {
testYamls(t, "./yamls", koCreate)
}

func testYamls(t *testing.T, baseDir string, createFunc createFunc) {

t.Parallel()
for _, path := range getExamplePaths(t, baseDir) {
Expand All @@ -228,6 +242,6 @@ func TestExamples(t *testing.T) {
kind = "taskrun"
}

t.Run(testName, exampleTest(path, waitValidateFunc, kind))
t.Run(testName, exampleTest(path, waitValidateFunc, createFunc, kind))
}
}
187 changes: 187 additions & 0 deletions test/yamls/v1beta1/pipelineruns/pipelinerun-with-final-tasks.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,187 @@
# Copied from https://github.com/tektoncd/catalog/blob/v1beta1/git/git-clone.yaml :(
# This can be deleted after we add support to refer to the remote Task in a registry (Issue #1839) or
# add support for referencing task in git directly (issue #2298)
apiVersion: tekton.dev/v1beta1
kind: Task
metadata:
name: git-clone-from-catalog
spec:
workspaces:
- name: output
description: The git repo will be cloned onto the volume backing this workspace
params:
- name: url
description: git url to clone
type: string
- name: revision
description: git revision to checkout (branch, tag, sha, ref…)
type: string
default: master
- name: refspec
description: (optional) git refspec to fetch before checking out revision
default: ""
- name: submodules
description: defines if the resource should initialize and fetch the submodules
type: string
default: "true"
- name: depth
description: performs a shallow clone where only the most recent commit(s) will be fetched
type: string
default: "1"
- name: sslVerify
description: defines if http.sslVerify should be set to true or false in the global git config
type: string
default: "true"
- name: subdirectory
description: subdirectory inside the "output" workspace to clone the git repo into
type: string
default: ""
- name: deleteExisting
description: clean out the contents of the repo's destination directory (if it already exists) before trying to clone the repo there
type: string
default: "false"
- name: httpProxy
description: git HTTP proxy server for non-SSL requests
type: string
default: ""
- name: httpsProxy
description: git HTTPS proxy server for SSL requests
type: string
default: ""
- name: noProxy
description: git no proxy - opt out of proxying HTTP/HTTPS requests
type: string
default: ""
results:
- name: commit
description: The precise commit SHA that was fetched by this Task
steps:
- name: clone
image: ko://github.com/tektoncd/pipeline/cmd/git-init
script: |
CHECKOUT_DIR="$(workspaces.output.path)/$(params.subdirectory)"
cleandir() {
# Delete any existing contents of the repo directory if it exists.
#
# We don't just "rm -rf $CHECKOUT_DIR" because $CHECKOUT_DIR might be "/"
# or the root of a mounted volume.
if [[ -d "$CHECKOUT_DIR" ]] ; then
# Delete non-hidden files and directories
rm -rf "$CHECKOUT_DIR"/*
# Delete files and directories starting with . but excluding ..
rm -rf "$CHECKOUT_DIR"/.[!.]*
# Delete files and directories starting with .. plus any other character
rm -rf "$CHECKOUT_DIR"/..?*
fi
}
if [[ "$(params.deleteExisting)" == "true" ]] ; then
cleandir
fi
test -z "$(params.httpProxy)" || export HTTP_PROXY=$(params.httpProxy)
test -z "$(params.httpsProxy)" || export HTTPS_PROXY=$(params.httpsProxy)
test -z "$(params.noProxy)" || export NO_PROXY=$(params.noProxy)
/ko-app/git-init \
-url "$(params.url)" \
-revision "$(params.revision)" \
-refspec "$(params.refspec)" \
-path "$CHECKOUT_DIR" \
-sslVerify="$(params.sslVerify)" \
-submodules="$(params.submodules)" \
-depth "$(params.depth)"
cd "$CHECKOUT_DIR"
RESULT_SHA="$(git rev-parse HEAD | tr -d '\n')"
EXIT_CODE="$?"
if [ "$EXIT_CODE" != 0 ]
then
exit $EXIT_CODE
fi
# Make sure we don't add a trailing newline to the result!
echo -n "$RESULT_SHA" > $(results.commit.path)
---

# Task to cleanup shared workspace
apiVersion: tekton.dev/v1beta1
kind: Task
metadata:
name: cleanup-workspace
spec:
workspaces:
# Shared workspace where git repo is cloned
- name: source
steps:
- name: check-application-dir-has-source
image: ubuntu
script: |
if [ ! -d "$(workspaces.source.path)/application/" ]; then
echo "Something went wrong and could not find application source under $(workspaces.source.path)/application/"
exit 1
fi
- name: cleanup-workspace
image: ubuntu
script: |
rm -rf $(workspaces.source.path)/application/
- name: verify-application-dir-has-gone
image: ubuntu
script: |
if [ -d "$(workspaces.source.path)/application/" ]; then
echo "Something went wrong cleaning up and the application source still exists under $(workspaces.source.path)/application/"
exit 1
fi
---

# Pipeline to clone repo into shared workspace and cleanup the workspace after done
apiVersion: tekton.dev/v1beta1
kind: Pipeline
metadata:
name: clone-cleanup-workspace
spec:
workspaces:
# common workspace where git repo is cloned and needs to be cleanup after done
- name: git-source
tasks:
# Clone app repo to workspace
- name: clone-app-repo
taskRef:
name: git-clone-from-catalog
params:
- name: url
value: https://github.com/tektoncd/community.git
- name: subdirectory
value: application
workspaces:
- name: output
workspace: git-source
finally:
# Cleanup workspace
- name: cleanup
taskRef:
name: cleanup-workspace
workspaces:
- name: source
workspace: git-source
---

# PipelineRun to execute pipeline - clone-into-workspace-and-cleanup-workspace
apiVersion: tekton.dev/v1beta1
kind: PipelineRun
metadata:
name: write-and-cleanup-workspace
spec:
pipelineRef:
name: clone-cleanup-workspace
serviceAccountName: 'default'
workspaces:
- name: git-source
volumeClaimTemplate:
spec:
accessModes:
- ReadWriteOnce
resources:
requests:
storage: 1Gi
---
Loading

0 comments on commit c37f7dd

Please sign in to comment.