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

--dry-run Option for tkn pipeline start #643

Merged
merged 1 commit into from
Jan 29, 2020
Merged

--dry-run Option for tkn pipeline start #643

merged 1 commit into from
Jan 29, 2020

Conversation

danielhelfand
Copy link
Member

@danielhelfand danielhelfand commented Jan 27, 2020

Closes #372

Changes

This pull request adds a --dry-run option for tkn pipeline start. The idea would be to print the pipelinerun to stdout so a user can look over the pipelinerun without actually running the pipeline.

An --output option will also be made available to output the dry run in yaml or json:

Output the pipeline named pipelinename in namespace ns in yaml:

tkn pipeline start pipelinename -n ns --dry-run --output yaml

Output the pipeline named pipelinename in namespace ns in json:
tkn pipeline start pipelinename -n ns --dry-run -o json

Not using output flag with --dry-run uses yaml output default:

tkn pipeline start pipelinename -n ns --dry-run

Example out:

output = json

{
	"kind": "PipelineRun",
	"apiVersion": "tekton.dev/v1alpha1",
	"metadata": {
		"generateName": "deploy-pipeline-run-",
		"namespace": "default",
		"creationTimestamp": null
	},
	"spec": {
		"pipelineRef": {
			"name": "deploy-pipeline"
		},
		"resources": [
			{
				"name": "app-git",
				"resourceRef": {
					"name": "nodejs-ex-git"
				}
			},
			{
				"name": "app-image",
				"resourceRef": {
					"name": "nodejs-ex-image"
				}
			}
		],
		"serviceAccountName": "default",
		"podTemplate": {}
	},
	"status": {}
}

output = yaml

kind: PipelineRun
metadata:
  creationTimestamp: null
  generateName: deploy-pipeline-run-
  namespace: default
spec:
  pipelineRef:
    name: deploy-pipeline
  podTemplate: {}
  resources:
  - name: app-git
    resourceRef:
      name: nodejs-ex-git
  - name: app-image
    resourceRef:
      name: nodejs-ex-image
  serviceAccountName: default
status: {}

Submitter Checklist

These are the criteria that every PR should meet, please check them off as you
review them:

  • Includes tests (if functionality changed/added)
  • Includes docs (if user facing)
  • Regenerate the manpages and docs with make docs and make man if needed.
  • Run the code checkers with make check
  • Commit messages follow commit message best practices

Release Notes

Add --dry-run option to tkn pipeline start to preview a pipelinerun

@tekton-robot tekton-robot added the size/S Denotes a PR that changes 10-29 lines, ignoring generated files. label Jan 27, 2020
@tekton-robot
Copy link
Contributor

The following is the coverage report on pkg/.
Say /test pull-tekton-cli-go-coverage to re-run this coverage report

File Old Coverage New Coverage Delta
pkg/cmd/pipeline/start.go 70.1% 68.7% -1.4

@@ -376,6 +381,10 @@ func getOptionsByType(resources resourceOptionsFilter, restype string) []string

func (opt *startOptions) startPipeline(pName string) error {
pr := &v1alpha1.PipelineRun{
TypeMeta: metav1.TypeMeta{
APIVersion: "tekton.dev/v1alpha1",
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How to specify the APIVersion seems difficult. This could maybe be pulled from the pipeline?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we would want at least to have it in a global since that string is scattered everywhere, but that's a refactoring that can be done for later....

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🤔 I think it's fine for now, but indeed, good question. I would guess we default to a hard coded version, and we could have some smart code that checks what api version are available in the cluster (and adjust from it or error out from there)

return err
}

if err := ioutil.WriteFile(opt.DryRun, prBytes, os.ModePerm); err != nil {
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I do not have unit tests for this since it writes a local file. Would it make sense to write the file and then delete it as part of the unit test?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

not sure if that's the proper way but that's how i would do it 😅

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure, I'll try it.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wondering if this approach is the right way here, but there are at least unit tests for errors/marshaling the pipelinerun.

@chmouel
Copy link
Member

chmouel commented Jan 27, 2020

I like the idea! thinking about it, what do you think about -o yaml -f file.yaml for this? as -o json may be useful too...

@danielhelfand
Copy link
Member Author

I like the idea! thinking about it, what do you think about -o yaml -f file.yaml for this? as -o json may be useful too...

🤔 Yeah, I could see this. So potentially convert --dry-run to a boolean and specify the outputs with -o and -f. Maybe default to yaml if nothing is specified?

@tekton-robot tekton-robot added size/L Denotes a PR that changes 100-499 lines, ignoring generated files. and removed size/S Denotes a PR that changes 10-29 lines, ignoring generated files. labels Jan 27, 2020
@tekton-robot
Copy link
Contributor

The following is the coverage report on pkg/.
Say /test pull-tekton-cli-go-coverage to re-run this coverage report

File Old Coverage New Coverage Delta
pkg/cmd/pipeline/start.go 70.1% 70.5% 0.3

@tekton-robot
Copy link
Contributor

The following is the coverage report on pkg/.
Say /test pull-tekton-cli-go-coverage to re-run this coverage report

File Old Coverage New Coverage Delta
pkg/cmd/pipeline/start.go 70.1% 70.5% 0.3

@tekton-robot
Copy link
Contributor

The following is the coverage report on pkg/.
Say /test pull-tekton-cli-go-coverage to re-run this coverage report

File Old Coverage New Coverage Delta
pkg/cmd/pipeline/start.go 70.1% 70.6% 0.4

@tekton-robot
Copy link
Contributor

The following is the coverage report on pkg/.
Say /test pull-tekton-cli-go-coverage to re-run this coverage report

File Old Coverage New Coverage Delta
pkg/cmd/pipeline/start.go 70.1% 70.6% 0.4

Copy link
Member

@vdemeester vdemeester left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Any reason to have the -f ? Doesn't tkn pipeline start --dry-run -o yaml > foo.yaml work ? (and its equivalent in Powershell).
We are using -f as input elsewhere in tkn, and I also wonder what happens we want some input someday ? I think, the initial bet is to use stdout and let other tools that can manage stdout do there thing.

@vdemeester
Copy link
Member

To add more to my previous comment, I wanna be able to do something like tkn pipeline start --dry-run --output=json | sed … | kubectl apply -f - or simial things. The PR, as-is, force me to use a -f and write a file, which might not be needed.

@chmouel
Copy link
Member

chmouel commented Jan 28, 2020

humm yeah make sense, sorry @danielhelfand that i made. you change it 🙇

@danielhelfand
Copy link
Member Author

To add more to my previous comment, I wanna be able to do something like tkn pipeline start --dry-run --output=json | sed … | kubectl apply -f - or simial things. The PR, as-is, force me to use a -f and write a file, which might not be needed.

Yeah, that makes sense. I can see scenarios where you don't actually want to output a file so makes sense. Also makes it easier to write tests for.

Would tkn pipeline start --dry-run --output=json then print the pipelinerun in json format in the case when not using >?

@vdemeester
Copy link
Member

Would tkn pipeline start --dry-run --output=json then print the pipelinerun in json format in the case when not using >?

Yes 👼

namespace: "",
input: cs2,
wantError: false,
want: `apiVersion: tekton.dev/v1alpha1
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think these should be golden files, but I think it would be better to refactor these to golden files in a separate pr.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes they should be golden files 👼 Could be done now or in a follow-up, as you want 😉

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Will refactor them all in a separate pr.

@tekton-robot
Copy link
Contributor

The following is the coverage report on pkg/.
Say /test pull-tekton-cli-go-coverage to re-run this coverage report

File Old Coverage New Coverage Delta
pkg/cmd/pipeline/start.go 70.1% 71.4% 1.3

}

if format == "json" {
prBytes, err := json.MarshalIndent(pr, "", "\t")
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can do something besides tab for indent if there are thoughts on that.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

no idea 😅
I think kubectl prints it without indent and let the user pass it through a tool for indent ?

/cc @chmouel

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess I would like the indent here if the behavior is to print for the end user to see.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

they are spaces on kubectl output or at least according to whitespace-mode in emacs

as long it's yamllint compatible i think it's fine for me!

I guess I would like the indent here if the behavior is to print for the end user to see.

It's fine either way? printed to end user or not we could do indent?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Without the indent, it's all crammed to the left like below:

{
"kind": "PipelineRun",
"apiVersion": "tekton.dev/v1alpha1",
"metadata": {
"generateName": "deploy-pipeline-run-",
"namespace": "default",
"creationTimestamp": null
},
"spec": {
"pipelineRef": {
"name": "deploy-pipeline"
},
"resources": [
{
"name": "app-git",
"resourceRef": {
"name": "nodejs-ex-git"
}
},
{
"name": "app-image",
"resourceRef": {
"name": "nodejs-ex-image"
}
}
],
"serviceAccountName": "default",
"podTemplate": {}
},
"status": {}
}

With the indent, it's as follows:

{
	"kind": "PipelineRun",
	"apiVersion": "tekton.dev/v1alpha1",
	"metadata": {
		"generateName": "deploy-pipeline-run-",
		"namespace": "default",
		"creationTimestamp": null
	},
	"spec": {
		"pipelineRef": {
			"name": "deploy-pipeline"
		},
		"resources": [
			{
				"name": "app-git",
				"resourceRef": {
					"name": "nodejs-ex-git"
				}
			},
			{
				"name": "app-image",
				"resourceRef": {
					"name": "nodejs-ex-image"
				}
			}
		],
		"serviceAccountName": "default",
		"podTemplate": {}
	},
	"status": {}
}

I think I'll keep as is.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes please big +1 on indents! any chanced to check if yamllint can validate it ? (i mean not a test but manually)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, it works with yamllint.

@@ -376,6 +381,10 @@ func getOptionsByType(resources resourceOptionsFilter, restype string) []string

func (opt *startOptions) startPipeline(pName string) error {
pr := &v1alpha1.PipelineRun{
TypeMeta: metav1.TypeMeta{
APIVersion: "tekton.dev/v1alpha1",
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🤔 I think it's fine for now, but indeed, good question. I would guess we default to a hard coded version, and we could have some smart code that checks what api version are available in the cluster (and adjust from it or error out from there)

}

if format == "json" {
prBytes, err := json.MarshalIndent(pr, "", "\t")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

no idea 😅
I think kubectl prints it without indent and let the user pass it through a tool for indent ?

/cc @chmouel

namespace: "",
input: cs2,
wantError: false,
want: `apiVersion: tekton.dev/v1alpha1
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes they should be golden files 👼 Could be done now or in a follow-up, as you want 😉

@tekton-robot
Copy link
Contributor

[APPROVALNOTIFIER] This PR is APPROVED

This pull-request has been approved by: vdemeester

The full list of commands accepted by this bot can be found here.

The pull request process is described here

Needs approval from an approver in each of these files:

Approvers can indicate their approval by writing /approve in a comment
Approvers can cancel approval by writing /approve cancel in a comment

@tekton-robot tekton-robot added the approved Indicates a PR has been approved by an approver from all required OWNERS files. label Jan 28, 2020
@tekton-robot
Copy link
Contributor

The following is the coverage report on pkg/.
Say /test pull-tekton-cli-go-coverage to re-run this coverage report

File Old Coverage New Coverage Delta
pkg/cmd/pipeline/start.go 70.1% 71.4% 1.3

@tekton-robot
Copy link
Contributor

The following is the coverage report on pkg/.
Say /test pull-tekton-cli-go-coverage to re-run this coverage report

File Old Coverage New Coverage Delta
pkg/cmd/pipeline/start.go 70.1% 71.4% 1.3

@chmouel
Copy link
Member

chmouel commented Jan 29, 2020

/lgtm

Looking Good! 🤙🏽

@tekton-robot tekton-robot added the lgtm Indicates that a PR is ready to be merged. label Jan 29, 2020
@tekton-robot tekton-robot merged commit f4f32ac into tektoncd:master Jan 29, 2020
piyush-garg added a commit that referenced this pull request Feb 3, 2020
#513 | [Daniel Helfand] update versions in README for 0.6.0 | 2019/12/12-02:36
#514 | [Chmouel Boudjnah] Add info in README about debian installs | 2019/12/12-04:27
#515 | [Chmouel Boudjnah] Change NFPM package name | 2019/12/12-04:44
#516 | [Chmouel Boudjnah] Fix install bash and zsh completion | 2019/12/13-04:55
#518 | [Chmouel Boudjnah] Tektoncd Release 0.9.2 | 2019/12/13-05:37
#508 | [16yuki0702] Making task logs interactive. | 2019/12/16-02:57
#517 | [Daniel Helfand] test for tkn task start --filename | 2019/12/16-03:11
#525 | [Jason Hall] Attempt to fix formatting of examples | 2019/12/18-09:07
#528 | [Chmouel Boudjnah] Set default to False for Show Logs on TaskRun and PipelineRun | 2019/12/18-10:11
#533 | [Daniel Helfand] show cancelled instead of failed for pr and tr desc | 2019/12/19-10:10
null | [Vincent Demeester] Switch to gotest.tools/v3 instead of testify 🐺 | 2019/12/20-04:44
null | [Vincent Demeester] Use ./hack/tools.go instead of dummy_test.go ⚓ | 2019/12/20-05:14
null | [Vincent Demeester] Bump github.com/imdario/mergo to 0.3.8 | 2019/12/20-05:33
null | [Chmouel Boudjnah] Add piyush as an OWNER 🕺 | 2019/12/20-08:04
null | [Piyush Garg] Add triggertemplate list | 2019/12/23-00:08
null | [Piyush Garg] Fix CI | 2019/12/23-10:09
null | [Daniel Helfand] remove namespace check for clustertask | 2019/12/23-10:38
null | [Piyush Garg] Add triggerbinding list | 2019/12/23-14:36
null | [Piyush Garg] Add eventlistener list | 2019/12/26-06:12
null | [Pradeep Kumar] Adds command to delete triggertemplate | 2019/12/26-21:38
null | [Daniel Helfand] converting taskrun desc tests to use step state test builder | 2019/12/27-00:24
null | [Pradeep Kumar] change test name | 2019/12/27-09:20
null | [Pradeep Kumar] Add command to delete triggerbinding | 2019/12/27-11:11
null | [Piyush Garg] Bump hashicorp/golang-lru | 2019/12/28-04:01
null | [Khurram Baig] Add Command to Remove EventListener | 2020/01/02-08:53
null | [Daniel Helfand] error message for using --last with tkn task start -f | 2020/01/02-09:39
null | [16yuki0702] Refactoring pipeline log tests. | 2020/01/02-09:51
null | [Daniel Helfand] updating release notes for CLI | 2020/01/06-07:22
null | [Daniel Helfand] add choco package to README and minor edits | 2020/01/06-08:17
null | [Piyush Garg] Refactor triggertemplate list test | 2020/01/07-00:29
null | [Chmouel Boudjnah] Syncronize cobra to latest | 2020/01/07-09:01
null | [Scott] Update all delete commands to support multiple arguments | 2020/01/08-13:02
null | [16yuki0702] Refactoring pipeline start tests. | 2020/01/09-07:56
null | [Daniel Helfand] add tkn task start -f with remote files | 2020/01/09-10:01
null | [Scott] Refactor delete command code to make a bit more DRY | 2020/01/10-02:49
null | [Daniel Helfand] add nil check for pr desc taskrun sorting | 2020/01/10-07:12
null | [Chmouel Boudjnah] Add coloured status condition | 2020/01/10-15:20
null | [Chmouel Boudjnah] Add colouring to steps status too | 2020/01/13-10:29
null | [Daniel Helfand] correct tkn task start typo | 2020/01/13-14:18
null | [Daniel Helfand] add tkn clustertask start | 2020/01/14-08:29
null | [Daniel Helfand] remove second taskrun from pr desc test | 2020/01/15-00:01
null | [Chmouel Boudjnah] Subtle cosmetics changes to describe command | 2020/01/15-09:58
null | [Chmouel Boudjnah] Show fluffy decorations only when we can or when the user wants it | 2020/01/15-09:58
null | [Chmouel Boudjnah] Fix release tarball version to remove the v at first | 2020/01/15-11:38
null | [Chmouel Boudjnah] Add Namespace to tkn pipeline desc | 2020/01/15-14:57
null | [Chmouel Boudjnah] Fix to auto select the first pipeline when only one is available | 2020/01/16-02:45
null | [Chmouel Boudjnah] Disable TestPipelineLog_Interactive | 2020/01/16-02:45
null | [Daniel Helfand] tkn create command | 2020/01/16-03:32
null | [Chmouel Boudjnah] Do not show bracket when showing the RunAfter.Tasks in Pipeline desc | 2020/01/16-04:07
null | [Chmouel Boudjnah] Set no colours automatically when not running with a tty or a pipe | 2020/01/16-16:33
null | [Chmouel Boudjnah] Skipping flakey interactive tests | 2020/01/17-07:42
null | [Vincent Demeester] Use golden package for unit test for Task 🐐 | 2020/01/19-15:20
null | [Vincent Demeester] Use golden package for unit test in TaskRun 🐐 | 2020/01/19-15:20
null | [Vincent Demeester] Use golden package for unit test for Pipeline 🐐 | 2020/01/19-15:20
null | [Vincent Demeester] Use golden package for unit test for PipelineRun 🐐 | 2020/01/19-15:20
null | [Vincent Demeester] Use golden package for unit test for Condition 🐐 | 2020/01/19-15:20
null | [Vincent Demeester] Use golden package for unit test for ClusterTask 🐐 | 2020/01/19-15:20
null | [Vincent Demeester] Use golden package for unit test for TriggerBinding 🐐 | 2020/01/19-15:20
null | [Vincent Demeester] Use golden package for unit test for TriggerTemplate 🐐 | 2020/01/19-15:20
null | [Vincent Demeester] Use golden package for unit test for EventListener 🐐 | 2020/01/19-15:20
null | [Vincent Demeester] Use golden package for unit test for PipelineResource 🐐 | 2020/01/19-15:20
null | [Vincent Demeester] Add `test-unit-update-golden` target to update golden files 🐟 | 2020/01/19-15:20
null | [Chmouel Boudjnah] generate steps name when none has been provided | 2020/01/20-00:02
null | [Chmouel Boudjnah] Add tests for k8.Condition | 2020/01/20-04:27
null | [Daniel Helfand] add tkn clustertask desc | 2020/01/20-11:11
null | [Chmouel Boudjnah] Add emojis to pipeline/pipelinerun/task/taskrun describe | 2020/01/22-08:40
null | [Chmouel Boudjnah] tkn clustertask describe zsh completion is broken | 2020/01/22-12:21
null | [Chmouel Boudjnah] Add emojis support for tkn clustertask describe command | 2020/01/22-12:44
null | [Piyush Garg] Return error inspite of nil | 2020/01/23-08:28
null | [Chmouel Boudjnah] Implement output for pipeline describe | 2020/01/27-03:07
null | [Daniel Helfand] sort steps for tr desc by StartedAt | 2020/01/28-03:57
null | [Scott] Add flag to task and pipeline to only delete related resources | 2020/01/28-08:47
null | [Vincent Demeester] Remove 🏻 from the step emoji 🍲 | 2020/01/28-10:00
null | [Chmouel Boudjnah] Refactor a bit custom output function | 2020/01/28-10:57
null | [Chmouel Boudjnah] Fix PipelineResource custom output | 2020/01/28-10:57
null | [Chmouel Boudjnah] Fix PipelineRuns custom output | 2020/01/28-10:57
#648 | [Chmouel Boudjnah] Fix Task custom output | 2020/01/28-10:57
#648 | [Chmouel Boudjnah] Fix TaskRuns custom output | 2020/01/28-10:57
#648 | [Chmouel Boudjnah] Fix ClusterTask custom output | 2020/01/28-10:57
#643 | [Daniel Helfand] --dry-run option for tkn pipeline start | 2020/01/29-02:50
#653 | [Chmouel Boudjnah] Minor wording fixes on the RELEASE_PROCESS document | 2020/01/29-04:26
#656 | [Vincent Demeester] Use gotest.tools assert package instead of testify 🍵 | 2020/01/29-05:16
#657 | [Daniel Helfand] refactor --dry-run tests for tkn pipeline start | 2020/01/29-10:55
#661 | [Chmouel Boudjnah] Fix output=name for pipeline list | 2020/01/30-10:08
#658 | [Piyush Garg] Bump tektoncd/pipeline and tektoncd/triggers | 2020/01/31-03:37
null | [Piyush Garg] Fix workspace not getting copied with --last | 2020/01/31-04:34
null | [16yuki0702] This fixes #639 | 2020/01/31-06:46
null | [ansky] Remove extra empty lines | 2020/01/31-10:30
null | [Daniel Helfand] remove tkn create | 2020/01/31-10:52
null | [Daniel Helfand] add --dry-run option for tkn task start | 2020/02/03-00:59
null | [Daniel Helfand] add --dry-run option for tkn ct start | 2020/02/03-01:17
null | [16yuki0702] Fix overriding GenerateName on taskrun. | 2020/02/03-06:23

Signed-off-by: Piyush Garg <piyushgarg001@gmail.com>
@danielhelfand danielhelfand deleted the print_pipelinerun branch February 10, 2020 16:21
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
approved Indicates a PR has been approved by an approver from all required OWNERS files. cla: yes lgtm Indicates that a PR is ready to be merged. size/L Denotes a PR that changes 100-499 lines, ignoring generated files.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Flag to print PipelineRun when calling tkn pipeline start
5 participants