-
Notifications
You must be signed in to change notification settings - Fork 38
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
pipeline: add render trigger func #547
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
70 changes: 70 additions & 0 deletions
70
pkg/fleet-manager/pipeline/render/testdata/trigger/default.yaml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
apiVersion: triggers.tekton.dev/v1alpha1 | ||
kind: TriggerTemplate | ||
metadata: | ||
name: test-pipeline-triggertemplate | ||
namespace: kurator-pipeline | ||
spec: | ||
params: | ||
- name: gitrevision | ||
description: The git revision | ||
- name: gitrepositoryurl | ||
description: The git repository url | ||
- name: namespace | ||
description: The namespace to create the resources | ||
resourceTemplates: | ||
- apiVersion: tekton.dev/v1beta1 | ||
kind: PipelineRun | ||
metadata: | ||
generateName: test-pipeline-run- | ||
namespace: $(tt.params.namespace) | ||
spec: | ||
serviceAccountName: test-pipeline | ||
pipelineRef: | ||
name: test-pipeline | ||
params: | ||
- name: revision | ||
value: $(tt.params.gitrevision) | ||
- name: repo-url | ||
value: $(tt.params.gitrepositoryurl) | ||
workspaces: | ||
- name: kurator-pipeline-shared-data # there only one pvc workspace in each pipeline, and the name is kurator-pipeline-shared-data | ||
volumeClaimTemplate: | ||
spec: | ||
accessModes: | ||
- ReadWriteOnce | ||
resources: | ||
requests: | ||
storage: 1Gi | ||
- name: git-credentials | ||
secret: | ||
secretName: git-credentials | ||
- name: docker-credentials | ||
secret: | ||
secretName: docker-credentials # auth for task | ||
--- | ||
apiVersion: triggers.tekton.dev/v1alpha1 | ||
kind: TriggerBinding | ||
metadata: | ||
name: test-pipeline-triggerbinding | ||
namespace: kurator-pipeline | ||
spec: | ||
params: | ||
- name: gitrevision | ||
value: $(body.head_commit.id) | ||
- name: namespace | ||
value: kurator-pipeline | ||
- name: gitrepositoryurl | ||
value: "https://github.com/$(body.repository.full_name)" | ||
--- | ||
apiVersion: triggers.tekton.dev/v1alpha1 | ||
kind: EventListener | ||
metadata: | ||
name: test-pipeline-listener | ||
namespace: kurator-pipeline | ||
spec: | ||
serviceAccountName: test-pipeline | ||
triggers: | ||
- bindings: | ||
- ref: test-pipeline-triggerbinding | ||
template: | ||
ref: test-pipeline-triggertemplate |
72 changes: 72 additions & 0 deletions
72
pkg/fleet-manager/pipeline/render/testdata/trigger/with-volume-claim.yaml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
apiVersion: triggers.tekton.dev/v1alpha1 | ||
kind: TriggerTemplate | ||
metadata: | ||
name: test-pipeline-triggertemplate | ||
namespace: kurator-pipeline | ||
spec: | ||
params: | ||
- name: gitrevision | ||
description: The git revision | ||
- name: gitrepositoryurl | ||
description: The git repository url | ||
- name: namespace | ||
description: The namespace to create the resources | ||
resourceTemplates: | ||
- apiVersion: tekton.dev/v1beta1 | ||
kind: PipelineRun | ||
metadata: | ||
generateName: test-pipeline-run- | ||
namespace: $(tt.params.namespace) | ||
spec: | ||
serviceAccountName: test-pipeline | ||
pipelineRef: | ||
name: test-pipeline | ||
params: | ||
- name: revision | ||
value: $(tt.params.gitrevision) | ||
- name: repo-url | ||
value: $(tt.params.gitrepositoryurl) | ||
workspaces: | ||
- name: kurator-pipeline-shared-data # there only one pvc workspace in each pipeline, and the name is kurator-pipeline-shared-data | ||
volumeClaimTemplate: | ||
spec: | ||
accessModes: | ||
- ReadWriteOnce | ||
resources: | ||
requests: | ||
storage: 500Mi | ||
volumeMode: Filesystem | ||
storageClassName: manual | ||
- name: git-credentials | ||
secret: | ||
secretName: git-credentials | ||
- name: docker-credentials | ||
secret: | ||
secretName: docker-credentials # auth for task | ||
--- | ||
apiVersion: triggers.tekton.dev/v1alpha1 | ||
kind: TriggerBinding | ||
metadata: | ||
name: test-pipeline-triggerbinding | ||
namespace: kurator-pipeline | ||
spec: | ||
params: | ||
- name: gitrevision | ||
value: $(body.head_commit.id) | ||
- name: namespace | ||
value: kurator-pipeline | ||
- name: gitrepositoryurl | ||
value: "https://github.com/$(body.repository.full_name)" | ||
--- | ||
apiVersion: triggers.tekton.dev/v1alpha1 | ||
kind: EventListener | ||
metadata: | ||
name: test-pipeline-listener | ||
namespace: kurator-pipeline | ||
spec: | ||
serviceAccountName: test-pipeline | ||
triggers: | ||
- bindings: | ||
- ref: test-pipeline-triggerbinding | ||
template: | ||
ref: test-pipeline-triggertemplate |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,163 @@ | ||
/* | ||
Copyright Kurator Authors. | ||
|
||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
|
||
http://www.apache.org/licenses/LICENSE-2.0 | ||
|
||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
package render | ||
|
||
import ( | ||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
|
||
pipelineapi "kurator.dev/kurator/pkg/apis/pipeline/v1alpha1" | ||
) | ||
|
||
const ( | ||
TriggerTemplateName = "pipeline trigger template" | ||
) | ||
|
||
type TriggerConfig struct { | ||
PipelineName string | ||
PipelineNamespace string | ||
OwnerReference *metav1.OwnerReference | ||
AccessMode string | ||
StorageRequest string | ||
StorageClassName string | ||
VolumeMode string | ||
} | ||
|
||
// ServiceAccountName is the service account used by trigger | ||
func (cfg TriggerConfig) ServiceAccountName() string { | ||
return cfg.PipelineName | ||
} | ||
|
||
// RenderTriggerWithPipeline takes a pipeline object and generates YAML byte array configuration representing the trigger configuration. | ||
func RenderTriggerWithPipeline(pipeline *pipelineapi.Pipeline) ([]byte, error) { | ||
config := TriggerConfig{ | ||
PipelineName: pipeline.Name, | ||
PipelineNamespace: pipeline.Namespace, | ||
OwnerReference: GeneratePipelineOwnerRef(pipeline), | ||
} | ||
if pipeline.Spec.SharedWorkspace != nil { | ||
config.AccessMode = string(pipeline.Spec.SharedWorkspace.AccessMode) | ||
config.StorageRequest = pipeline.Spec.SharedWorkspace.StorageRequest | ||
config.StorageClassName = pipeline.Spec.SharedWorkspace.StorageClassName | ||
config.VolumeMode = string(pipeline.Spec.SharedWorkspace.VolumeMode) | ||
} | ||
|
||
return RenderTrigger(config) | ||
} | ||
|
||
// RenderTrigger takes a TriggerConfig object and generates YAML byte array configuration representing the trigger configuration. | ||
func RenderTrigger(cfg TriggerConfig) ([]byte, error) { | ||
return renderTemplate(TriggerTemplateContent, TriggerTemplateName, cfg) | ||
} | ||
|
||
const TriggerTemplateContent = `apiVersion: triggers.tekton.dev/v1alpha1 | ||
kind: TriggerTemplate | ||
metadata: | ||
name: {{ .PipelineName }}-triggertemplate | ||
namespace: {{ .PipelineNamespace }} | ||
{{- if .OwnerReference }} | ||
ownerReferences: | ||
- apiVersion: "{{ .OwnerReference.APIVersion }}" | ||
kind: "{{ .OwnerReference.Kind }}" | ||
name: "{{ .OwnerReference.Name }}" | ||
uid: "{{ .OwnerReference.UID }}" | ||
{{- end }} | ||
spec: | ||
params: | ||
- name: gitrevision | ||
description: The git revision | ||
- name: gitrepositoryurl | ||
description: The git repository url | ||
- name: namespace | ||
description: The namespace to create the resources | ||
resourceTemplates: | ||
- apiVersion: tekton.dev/v1beta1 | ||
kind: PipelineRun | ||
metadata: | ||
generateName: {{ .PipelineName }}-run- | ||
namespace: $(tt.params.namespace) | ||
spec: | ||
serviceAccountName: {{ .ServiceAccountName }} | ||
pipelineRef: | ||
name: {{ .PipelineName }} | ||
params: | ||
- name: revision | ||
value: $(tt.params.gitrevision) | ||
- name: repo-url | ||
value: $(tt.params.gitrepositoryurl) | ||
workspaces: | ||
- name: kurator-pipeline-shared-data # there only one pvc workspace in each pipeline, and the name is kurator-pipeline-shared-data | ||
volumeClaimTemplate: | ||
spec: | ||
accessModes: | ||
- {{ default "ReadWriteOnce" .AccessMode }} | ||
resources: | ||
requests: | ||
storage: {{ default "1Gi" .StorageRequest }} | ||
{{- if .VolumeMode }} | ||
volumeMode: {{ .VolumeMode }} | ||
{{- end }} | ||
{{- if .StorageClassName }} | ||
storageClassName: {{ .StorageClassName }} | ||
{{- end }} | ||
- name: git-credentials | ||
secret: | ||
secretName: git-credentials | ||
- name: docker-credentials | ||
secret: | ||
secretName: docker-credentials # auth for task | ||
--- | ||
apiVersion: triggers.tekton.dev/v1alpha1 | ||
kind: TriggerBinding | ||
metadata: | ||
name: {{ .PipelineName }}-triggerbinding | ||
namespace: {{ .PipelineNamespace}} | ||
{{- if .OwnerReference }} | ||
ownerReferences: | ||
- apiVersion: "{{ .OwnerReference.APIVersion }}" | ||
kind: "{{ .OwnerReference.Kind }}" | ||
name: "{{ .OwnerReference.Name }}" | ||
uid: "{{ .OwnerReference.UID }}" | ||
{{- end }} | ||
spec: | ||
params: | ||
- name: gitrevision | ||
value: $(body.head_commit.id) | ||
- name: namespace | ||
value: {{ .PipelineNamespace}} | ||
- name: gitrepositoryurl | ||
value: "https://github.com/$(body.repository.full_name)" | ||
--- | ||
apiVersion: triggers.tekton.dev/v1alpha1 | ||
kind: EventListener | ||
metadata: | ||
name: {{ .PipelineName }}-listener | ||
namespace: {{ .PipelineNamespace}} | ||
{{- if .OwnerReference }} | ||
ownerReferences: | ||
- apiVersion: "{{ .OwnerReference.APIVersion }}" | ||
kind: "{{ .OwnerReference.Kind }}" | ||
name: "{{ .OwnerReference.Name }}" | ||
uid: "{{ .OwnerReference.UID }}" | ||
{{- end }} | ||
spec: | ||
serviceAccountName: {{ .ServiceAccountName }} | ||
triggers: | ||
- bindings: | ||
- ref: {{ .PipelineName }}-triggerbinding | ||
template: | ||
ref: {{ .PipelineName }}-triggertemplate | ||
` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
/* | ||
Copyright Kurator Authors. | ||
|
||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
|
||
http://www.apache.org/licenses/LICENSE-2.0 | ||
|
||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
package render | ||
|
||
import ( | ||
"os" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestRenderTrigger(t *testing.T) { | ||
expectedRBACFilePath := "testdata/trigger/" | ||
cases := []struct { | ||
name string | ||
cfg TriggerConfig | ||
expectError bool | ||
expectedFile string | ||
}{ | ||
{ | ||
name: "default trigger configuration", | ||
cfg: TriggerConfig{ | ||
PipelineName: "test-pipeline", | ||
PipelineNamespace: "kurator-pipeline", | ||
}, | ||
expectError: false, | ||
expectedFile: "default.yaml", | ||
}, | ||
{ | ||
name: "trigger configuration with custom volume claim", | ||
cfg: TriggerConfig{ | ||
PipelineName: "test-pipeline", | ||
PipelineNamespace: "kurator-pipeline", | ||
AccessMode: "ReadWriteOnce", | ||
StorageRequest: "500Mi", | ||
StorageClassName: "manual", | ||
VolumeMode: "Filesystem", | ||
}, | ||
expectError: false, | ||
expectedFile: "with-volume-claim.yaml", | ||
}, | ||
} | ||
|
||
for _, tc := range cases { | ||
t.Run(tc.name, func(t *testing.T) { | ||
result, err := RenderTrigger(tc.cfg) | ||
|
||
if tc.expectError { | ||
assert.Error(t, err) | ||
} else { | ||
assert.NoError(t, err) | ||
|
||
expected, err := os.ReadFile(expectedRBACFilePath + tc.expectedFile) | ||
assert.NoError(t, err) | ||
assert.Equal(t, string(expected), string(result)) | ||
} | ||
}) | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
same as other comments. do we need white space for TriggerTemplateName, as i cannot see the details
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actually, 'TemplateName' is not a functioning variable, and it can contain spaces in the middle. see
kurator/pkg/fleet-manager/plugin/render.go
Line 93 in 09a35eb
IMHO, It is only required as a parameter for creating Go templates and more like a key for tpls.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
of cause, we can use "-" instead of space
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
IC, it is good