|
| 1 | +package pod |
| 2 | + |
| 3 | +import ( |
| 4 | + "errors" |
| 5 | + "fmt" |
| 6 | + "path/filepath" |
| 7 | + "strings" |
| 8 | + |
| 9 | + corev1 "k8s.io/api/core/v1" |
| 10 | + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" |
| 11 | + "k8s.io/client-go/kubernetes" |
| 12 | +) |
| 13 | + |
| 14 | +const ( |
| 15 | + toolsVolumeName = "tools" |
| 16 | + mountPoint = "/builder/tools" |
| 17 | + entrypointBinary = mountPoint + "/entrypoint" |
| 18 | + |
| 19 | + downwardVolumeName = "downward" |
| 20 | + downwardMountPoint = "/builder/downward" |
| 21 | + downwardMountReadyFile = "ready" |
| 22 | + ReadyAnnotation = "tekton.dev/ready" |
| 23 | + ReadyAnnotationValue = "READY" |
| 24 | + |
| 25 | + StepPrefix = "step-" |
| 26 | + SidecarPrefix = "sidecar-" |
| 27 | +) |
| 28 | + |
| 29 | +var ( |
| 30 | + // TODO(#1605): Generate volumeMount names, to avoid collisions. |
| 31 | + // TODO(#1605): Unexport these vars when Pod conversion is entirely within |
| 32 | + // this package. |
| 33 | + ToolsMount = corev1.VolumeMount{ |
| 34 | + Name: toolsVolumeName, |
| 35 | + MountPath: mountPoint, |
| 36 | + } |
| 37 | + ToolsVolume = corev1.Volume{ |
| 38 | + Name: toolsVolumeName, |
| 39 | + VolumeSource: corev1.VolumeSource{EmptyDir: &corev1.EmptyDirVolumeSource{}}, |
| 40 | + } |
| 41 | + |
| 42 | + // TODO(#1605): Signal sidecar readiness by injecting entrypoint, |
| 43 | + // remove dependency on Downward API. |
| 44 | + DownwardVolume = corev1.Volume{ |
| 45 | + Name: downwardVolumeName, |
| 46 | + VolumeSource: corev1.VolumeSource{ |
| 47 | + DownwardAPI: &corev1.DownwardAPIVolumeSource{ |
| 48 | + Items: []corev1.DownwardAPIVolumeFile{{ |
| 49 | + Path: downwardMountReadyFile, |
| 50 | + FieldRef: &corev1.ObjectFieldSelector{ |
| 51 | + FieldPath: fmt.Sprintf("metadata.annotations['%s']", ReadyAnnotation), |
| 52 | + }, |
| 53 | + }}, |
| 54 | + }, |
| 55 | + }, |
| 56 | + } |
| 57 | + DownwardMount = corev1.VolumeMount{ |
| 58 | + Name: downwardVolumeName, |
| 59 | + MountPath: downwardMountPoint, |
| 60 | + } |
| 61 | +) |
| 62 | + |
| 63 | +// OrderContainers returns the specified steps, modified so that they are |
| 64 | +// executed in order by overriding the entrypoint binary. It also returns the |
| 65 | +// init container that places the entrypoint binary pulled from the |
| 66 | +// entrypointImage. |
| 67 | +// |
| 68 | +// Containers must have Command specified; if the user didn't specify a |
| 69 | +// command, we must have fetched the image's ENTRYPOINT before calling this |
| 70 | +// method, using entrypoint_lookup.go. |
| 71 | +// |
| 72 | +// TODO(#1605): Also use entrypoint injection to order sidecar start/stop. |
| 73 | +func OrderContainers(entrypointImage string, steps []corev1.Container) (corev1.Container, []corev1.Container, error) { |
| 74 | + toolsInit := corev1.Container{ |
| 75 | + Name: "place-tools", |
| 76 | + Image: entrypointImage, |
| 77 | + Command: []string{"cp", "/ko-app/entrypoint", entrypointBinary}, |
| 78 | + VolumeMounts: []corev1.VolumeMount{ToolsMount}, |
| 79 | + } |
| 80 | + |
| 81 | + if len(steps) == 0 { |
| 82 | + return corev1.Container{}, nil, errors.New("No steps specified") |
| 83 | + } |
| 84 | + |
| 85 | + for i, s := range steps { |
| 86 | + var argsForEntrypoint []string |
| 87 | + switch i { |
| 88 | + case 0: |
| 89 | + argsForEntrypoint = []string{ |
| 90 | + // First step waits for the Downward volume file. |
| 91 | + "-wait_file", filepath.Join(downwardMountPoint, downwardMountReadyFile), |
| 92 | + "-wait_file_content", // Wait for file contents, not just an empty file. |
| 93 | + // Start next step. |
| 94 | + "-post_file", filepath.Join(mountPoint, fmt.Sprintf("%d", i)), |
| 95 | + } |
| 96 | + default: |
| 97 | + // All other steps wait for previous file, write next file. |
| 98 | + argsForEntrypoint = []string{ |
| 99 | + "-wait_file", filepath.Join(mountPoint, fmt.Sprintf("%d", i-1)), |
| 100 | + "-post_file", filepath.Join(mountPoint, fmt.Sprintf("%d", i)), |
| 101 | + } |
| 102 | + } |
| 103 | + |
| 104 | + cmd, args := s.Command, s.Args |
| 105 | + if len(cmd) == 0 { |
| 106 | + return corev1.Container{}, nil, fmt.Errorf("Step %d did not specify command", i) |
| 107 | + } |
| 108 | + if len(cmd) > 1 { |
| 109 | + args = append(cmd[1:], args...) |
| 110 | + cmd = []string{cmd[0]} |
| 111 | + } |
| 112 | + argsForEntrypoint = append(argsForEntrypoint, "-entrypoint", cmd[0], "--") |
| 113 | + argsForEntrypoint = append(argsForEntrypoint, args...) |
| 114 | + |
| 115 | + steps[i].Command = []string{entrypointBinary} |
| 116 | + steps[i].Args = argsForEntrypoint |
| 117 | + steps[i].VolumeMounts = append(steps[i].VolumeMounts, ToolsMount) |
| 118 | + } |
| 119 | + // Mount the Downward volume into the first step container. |
| 120 | + steps[0].VolumeMounts = append(steps[0].VolumeMounts, DownwardMount) |
| 121 | + |
| 122 | + return toolsInit, steps, nil |
| 123 | +} |
| 124 | + |
| 125 | +// UpdateReady updates the Pod's annotations to signal the first step to start |
| 126 | +// by projecting the ready annotation via the Downward API. |
| 127 | +func UpdateReady(kubeclient kubernetes.Interface, pod corev1.Pod) error { |
| 128 | + newPod, err := kubeclient.CoreV1().Pods(pod.Namespace).Get(pod.Name, metav1.GetOptions{}) |
| 129 | + if err != nil { |
| 130 | + return fmt.Errorf("Error getting Pod %q when updating ready annotation: %w", pod.Name, err) |
| 131 | + } |
| 132 | + |
| 133 | + // Update the Pod's "READY" annotation to signal the first step to |
| 134 | + // start. |
| 135 | + if newPod.ObjectMeta.Annotations == nil { |
| 136 | + newPod.ObjectMeta.Annotations = map[string]string{} |
| 137 | + } |
| 138 | + if newPod.ObjectMeta.Annotations[ReadyAnnotation] != ReadyAnnotationValue { |
| 139 | + newPod.ObjectMeta.Annotations[ReadyAnnotation] = ReadyAnnotationValue |
| 140 | + if _, err := kubeclient.CoreV1().Pods(newPod.Namespace).Update(newPod); err != nil { |
| 141 | + return fmt.Errorf("Error adding ready annotation to Pod %q: %w", pod.Name, err) |
| 142 | + } |
| 143 | + } |
| 144 | + return nil |
| 145 | +} |
| 146 | + |
| 147 | +// StopSidecars updates sidecar containers in the Pod to a nop image, which |
| 148 | +// exits successfully immediately. |
| 149 | +func StopSidecars(nopImage string, kubeclient kubernetes.Interface, pod corev1.Pod) error { |
| 150 | + newPod, err := kubeclient.CoreV1().Pods(pod.Namespace).Get(pod.Name, metav1.GetOptions{}) |
| 151 | + if err != nil { |
| 152 | + return fmt.Errorf("Error getting Pod %q when stopping sidecars: %w", pod.Name, err) |
| 153 | + } |
| 154 | + |
| 155 | + updated := false |
| 156 | + if newPod.Status.Phase == corev1.PodRunning { |
| 157 | + for _, s := range newPod.Status.ContainerStatuses { |
| 158 | + if IsContainerSidecar(s.Name) && s.State.Running != nil { |
| 159 | + for j, c := range newPod.Spec.Containers { |
| 160 | + if c.Name == s.Name && c.Image != nopImage { |
| 161 | + updated = true |
| 162 | + newPod.Spec.Containers[j].Image = nopImage |
| 163 | + } |
| 164 | + } |
| 165 | + } |
| 166 | + } |
| 167 | + } |
| 168 | + if updated { |
| 169 | + if _, err := kubeclient.CoreV1().Pods(newPod.Namespace).Update(newPod); err != nil { |
| 170 | + return fmt.Errorf("Error adding ready annotation to Pod %q: %w", pod.Name, err) |
| 171 | + } |
| 172 | + } |
| 173 | + return nil |
| 174 | +} |
| 175 | + |
| 176 | +func IsContainerStep(name string) bool { return strings.HasPrefix(name, StepPrefix) } |
| 177 | +func IsContainerSidecar(name string) bool { return strings.HasPrefix(name, SidecarPrefix) } |
| 178 | + |
| 179 | +func TrimStepPrefix(name string) string { return strings.TrimPrefix(name, StepPrefix) } |
| 180 | +func TrimSidecarPrefix(name string) string { return strings.TrimPrefix(name, SidecarPrefix) } |
0 commit comments