-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinternal_convert.go
178 lines (145 loc) · 4.77 KB
/
internal_convert.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
// This file is part of kueueleuleu (https://github.com/norbjd/kueueleuleu).
//
// Copyright (C) 2023 norbjd
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, version 3 of the License.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
package kueueleuleu
import (
"errors"
"fmt"
"strings"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)
const (
prepareInitContainerName = "kueueleuleu-prepare"
tektonEntrypointImage = "gcr.io/tekton-releases/github.com/tektoncd/pipeline/cmd/entrypoint" +
"@sha256:40abc3a78b558f251e890085972ed25fe7ad428f47998bc9c9c18f564dc03c32"
kueueleuleuAnnotationKey = "norbjd.github.io/kueueleuleu"
kueueleuleuAnnotationValue = "true"
)
var ErrContainerDoesNotHaveACommand = errors.New("container does not have a command, but we expect one")
func convertObjectMeta(objectMeta metav1.ObjectMeta) metav1.ObjectMeta {
if objectMeta.Annotations == nil {
objectMeta.Annotations = make(map[string]string)
}
objectMeta.Annotations[kueueleuleuAnnotationKey] = kueueleuleuAnnotationValue
return objectMeta
}
func checkPodSpecIsValid(podSpec corev1.PodSpec) error {
var err error
for _, container := range podSpec.Containers {
if len(container.Command) == 0 {
err = errors.Join(err,
fmt.Errorf("%w (container %s)", ErrContainerDoesNotHaveACommand, container.Name),
)
}
}
return err
}
//nolint:funlen
func convertPodSpec(podSpec corev1.PodSpec) (corev1.PodSpec, error) {
errInvalidPodSpec := checkPodSpecIsValid(podSpec)
if errInvalidPodSpec != nil {
return corev1.PodSpec{}, fmt.Errorf("pod spec is invalid: %w", errInvalidPodSpec)
}
kueueleuleuPodSpec := podSpec
initContainer := corev1.Container{
Name: prepareInitContainerName,
Image: tektonEntrypointImage,
Command: strings.Split("/ko-app/entrypoint init /ko-app/entrypoint /tekton/bin/entrypoint", " "),
VolumeMounts: []corev1.VolumeMount{
{
Name: "tekton-internal-bin",
MountPath: "/tekton/bin",
},
{
Name: "tekton-internal-steps",
MountPath: "/tekton/steps",
},
},
}
// prepend our own init container
kueueleuleuPodSpec.InitContainers = append([]corev1.Container{initContainer}, podSpec.InitContainers...)
newVolumes := kueueleuleuPodSpec.Volumes
newVolumes = append(newVolumes,
[]corev1.Volume{
{
// this is only used in the init container because /tekton/steps must exist, and is not useful otherwise
Name: "tekton-internal-steps",
VolumeSource: corev1.VolumeSource{
EmptyDir: &corev1.EmptyDirVolumeSource{},
},
},
{
Name: "tekton-internal-bin",
VolumeSource: corev1.VolumeSource{
EmptyDir: &corev1.EmptyDirVolumeSource{},
},
},
}...,
)
for i := range podSpec.Containers {
volume := corev1.Volume{
Name: fmt.Sprintf("tekton-internal-run-%d", i),
VolumeSource: corev1.VolumeSource{},
}
newVolumes = append(newVolumes, volume)
}
kueueleuleuPodSpec.Volumes = newVolumes
for index, container := range podSpec.Containers {
newVolumeMounts := container.VolumeMounts
newVolumeMounts = append(newVolumeMounts, []corev1.VolumeMount{
{
Name: "tekton-internal-bin",
MountPath: "/tekton/bin",
ReadOnly: true,
},
}...)
for otherContainerIndex := range podSpec.Containers {
volumeMount := corev1.VolumeMount{
Name: fmt.Sprintf("tekton-internal-run-%d", otherContainerIndex),
MountPath: fmt.Sprintf("/tekton/run/%d", otherContainerIndex),
}
if index != otherContainerIndex {
volumeMount.ReadOnly = true
}
newVolumeMounts = append(newVolumeMounts, volumeMount)
}
container.VolumeMounts = newVolumeMounts
newArgs := make([]string, 0)
if index > 0 {
newArgs = []string{
"-wait_file",
fmt.Sprintf("/tekton/run/%d/out", index-1),
}
}
newArgs = append(newArgs, []string{
"-post_file",
fmt.Sprintf("/tekton/run/%d/out", index),
"-step_metadata_dir",
fmt.Sprintf("/tekton/run/%d/status", index),
"-entrypoint",
}...)
newArgs = append(newArgs, container.Command[0])
newArgs = append(newArgs, "--")
if len(container.Command) > 1 {
newArgs = append(newArgs, container.Command[1:]...)
}
newArgs = append(newArgs, container.Args...)
container.Args = newArgs
container.Command = []string{"/tekton/bin/entrypoint"}
kueueleuleuPodSpec.Containers[index] = container
}
return kueueleuleuPodSpec, nil
}