Skip to content

Commit

Permalink
Add V1 TaskRun Golang structs
Browse files Browse the repository at this point in the history
  • Loading branch information
JeromeJu committed Aug 3, 2022
1 parent b754e71 commit 28a6ce2
Show file tree
Hide file tree
Showing 8 changed files with 2,176 additions and 0 deletions.
112 changes: 112 additions & 0 deletions pkg/apis/pipeline/v1/pod.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
/*
Copyright 2022 The Tekton 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 v1

import "github.com/tektoncd/pipeline/pkg/apis/pipeline/pod"

// PodTemplate holds pod specific configuration
type PodTemplate = pod.Template

// MergePodTemplateWithDefault merges 2 PodTemplates together. If the same
// field is set on both templates, the value from tpl will overwrite the value
// from defaultTpl.
func MergePodTemplateWithDefault(tpl, defaultTpl *PodTemplate) *PodTemplate {
switch {
case defaultTpl == nil:
// No configured default, just return the template
return tpl
case tpl == nil:
// No template, just return the default template
return defaultTpl
default:
// Otherwise, merge fields
if tpl.NodeSelector == nil {
tpl.NodeSelector = defaultTpl.NodeSelector
}
if tpl.Tolerations == nil {
tpl.Tolerations = defaultTpl.Tolerations
}
if tpl.Affinity == nil {
tpl.Affinity = defaultTpl.Affinity
}
if tpl.SecurityContext == nil {
tpl.SecurityContext = defaultTpl.SecurityContext
}
if tpl.Volumes == nil {
tpl.Volumes = defaultTpl.Volumes
}
if tpl.RuntimeClassName == nil {
tpl.RuntimeClassName = defaultTpl.RuntimeClassName
}
if tpl.AutomountServiceAccountToken == nil {
tpl.AutomountServiceAccountToken = defaultTpl.AutomountServiceAccountToken
}
if tpl.DNSPolicy == nil {
tpl.DNSPolicy = defaultTpl.DNSPolicy
}
if tpl.DNSConfig == nil {
tpl.DNSConfig = defaultTpl.DNSConfig
}
if tpl.EnableServiceLinks == nil {
tpl.EnableServiceLinks = defaultTpl.EnableServiceLinks
}
if tpl.PriorityClassName == nil {
tpl.PriorityClassName = defaultTpl.PriorityClassName
}
if tpl.SchedulerName == "" {
tpl.SchedulerName = defaultTpl.SchedulerName
}
if tpl.ImagePullSecrets == nil {
tpl.ImagePullSecrets = defaultTpl.ImagePullSecrets
}
if tpl.HostAliases == nil {
tpl.HostAliases = defaultTpl.HostAliases
}
if tpl.HostNetwork == false && defaultTpl.HostNetwork == true {
tpl.HostNetwork = true
}
return tpl
}
}

// AAPodTemplate holds pod specific configuration for the affinity-assistant
type AAPodTemplate = pod.AffinityAssistantTemplate

// MergeAAPodTemplateWithDefault is the same as MergePodTemplateWithDefault but
// for AffinityAssistantPodTemplates.
func MergeAAPodTemplateWithDefault(tpl, defaultTpl *AAPodTemplate) *AAPodTemplate {
switch {
case defaultTpl == nil:
// No configured default, just return the template
return tpl
case tpl == nil:
// No template, just return the default template
return defaultTpl
default:
// Otherwise, merge fields
if tpl.NodeSelector == nil {
tpl.NodeSelector = defaultTpl.NodeSelector
}
if tpl.Tolerations == nil {
tpl.Tolerations = defaultTpl.Tolerations
}
if tpl.ImagePullSecrets == nil {
tpl.ImagePullSecrets = defaultTpl.ImagePullSecrets
}
return tpl
}
}
49 changes: 49 additions & 0 deletions pkg/apis/pipeline/v1/taskref_types.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
/*
Copyright 2022 The Tekton 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 v1

// TaskRef can be used to refer to a specific instance of a task.
type TaskRef struct {
// Name of the referent; More info: http://kubernetes.io/docs/user-guide/identifiers#names
Name string `json:"name,omitempty"`
// TaskKind indicates the kind of the task, namespaced or cluster scoped.
Kind TaskKind `json:"kind,omitempty"`
// API version of the referent
// +optional
APIVersion string `json:"apiVersion,omitempty"`
// Bundle url reference to a Tekton Bundle.
// +optional
Bundle string `json:"bundle,omitempty"`

// ResolverRef allows referencing a Task in a remote location
// like a git repo. This field is only supported when the alpha
// feature gate is enabled.
// +optional
ResolverRef `json:",omitempty"`
}

// Check that Pipeline may be validated and defaulted.

// TaskKind defines the type of Task used by the pipeline.
type TaskKind string

const (
// NamespacedTaskKind indicates that the task type has a namespaced scope.
NamespacedTaskKind TaskKind = "Task"
// ClusterTaskKind indicates that task type has a cluster scope.
ClusterTaskKind TaskKind = "ClusterTask"
)
72 changes: 72 additions & 0 deletions pkg/apis/pipeline/v1/taskrun_defaults.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
/*
Copyright 2022 The Tekton 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 v1

import (
"context"
"time"

"github.com/tektoncd/pipeline/pkg/apis/config"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"knative.dev/pkg/apis"
)

var _ apis.Defaultable = (*TaskRun)(nil)

// ManagedByLabelKey is the label key used to mark what is managing this resource
const ManagedByLabelKey = "app.kubernetes.io/managed-by"

// SetDefaults implements apis.Defaultable
func (tr *TaskRun) SetDefaults(ctx context.Context) {
ctx = apis.WithinParent(ctx, tr.ObjectMeta)
tr.Spec.SetDefaults(ctx)

// If the TaskRun doesn't have a managed-by label, apply the default
// specified in the config.
cfg := config.FromContextOrDefaults(ctx)
if tr.ObjectMeta.Labels == nil {
tr.ObjectMeta.Labels = map[string]string{}
}
if _, found := tr.ObjectMeta.Labels[ManagedByLabelKey]; !found {
tr.ObjectMeta.Labels[ManagedByLabelKey] = cfg.Defaults.DefaultManagedByLabelValue
}
}

// SetDefaults implements apis.Defaultable
func (trs *TaskRunSpec) SetDefaults(ctx context.Context) {
cfg := config.FromContextOrDefaults(ctx)
if trs.TaskRef != nil && trs.TaskRef.Kind == "" {
trs.TaskRef.Kind = NamespacedTaskKind
}

if trs.Timeout == nil {
trs.Timeout = &metav1.Duration{Duration: time.Duration(cfg.Defaults.DefaultTimeoutMinutes) * time.Minute}
}

defaultSA := cfg.Defaults.DefaultServiceAccount
if trs.ServiceAccountName == "" && defaultSA != "" {
trs.ServiceAccountName = defaultSA
}

defaultPodTemplate := cfg.Defaults.DefaultPodTemplate
trs.PodTemplate = MergePodTemplateWithDefault(trs.PodTemplate, defaultPodTemplate)

// If this taskrun has an embedded task, apply the usual task defaults
if trs.TaskSpec != nil {
trs.TaskSpec.SetDefaults(ctx)
}
}
Loading

0 comments on commit 28a6ce2

Please sign in to comment.