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

pipeline: add render trigger func #547

Merged
merged 1 commit into from
Jan 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
70 changes: 70 additions & 0 deletions pkg/fleet-manager/pipeline/render/testdata/trigger/default.yaml
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
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
163 changes: 163 additions & 0 deletions pkg/fleet-manager/pipeline/render/trigger.go
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)
Copy link
Member

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

Copy link
Contributor Author

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

t := template.New("fleet plugin template")

IMHO, It is only required as a parameter for creating Go templates and more like a key for tpls.

Copy link
Contributor Author

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

Copy link
Member

Choose a reason for hiding this comment

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

IC, it is good

}

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
`
73 changes: 73 additions & 0 deletions pkg/fleet-manager/pipeline/render/trigger_test.go
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))
}
})
}
}
Loading