@@ -18,14 +18,18 @@ package pod
18
18
19
19
import (
20
20
"context"
21
+ "encoding/json"
21
22
"errors"
22
23
"fmt"
24
+ "log"
23
25
"path/filepath"
24
26
"strings"
25
27
26
28
"github.com/tektoncd/pipeline/pkg/apis/pipeline/v1beta1"
29
+ "gomodules.xyz/jsonpatch/v2"
27
30
corev1 "k8s.io/api/core/v1"
28
31
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
32
+ "k8s.io/apimachinery/pkg/types"
29
33
"k8s.io/client-go/kubernetes"
30
34
)
31
35
@@ -165,26 +169,29 @@ func collectResultsName(results []v1beta1.TaskResult) string {
165
169
return strings .Join (resultNames , "," )
166
170
}
167
171
168
- // UpdateReady updates the Pod's annotations to signal the first step to start
169
- // by projecting the ready annotation via the Downward API.
170
- func UpdateReady (ctx context.Context , kubeclient kubernetes.Interface , pod corev1.Pod ) error {
171
- newPod , err := kubeclient .CoreV1 ().Pods (pod .Namespace ).Get (ctx , pod .Name , metav1.GetOptions {})
172
+ var replaceReadyPatchBytes []byte
173
+
174
+ func init () {
175
+ // https://stackoverflow.com/questions/55573724/create-a-patch-to-add-a-kubernetes-annotation
176
+ readyAnnotationPath := "/metadata/annotations/" + strings .Replace (readyAnnotation , "/" , "~1" , 1 )
177
+ var err error
178
+ replaceReadyPatchBytes , err = json .Marshal ([]jsonpatch.JsonPatchOperation {{
179
+ Operation : "replace" ,
180
+ Path : readyAnnotationPath ,
181
+ Value : readyAnnotationValue ,
182
+ }})
172
183
if err != nil {
173
- return fmt . Errorf ( "error getting Pod %q when updating ready annotation : %w" , pod . Name , err )
184
+ log . Fatalf ( "failed to marshal replace ready patch bytes : %v" , err )
174
185
}
186
+ }
175
187
176
- // Update the Pod's "READY" annotation to signal the first step to
177
- // start.
178
- if newPod .ObjectMeta .Annotations == nil {
179
- newPod .ObjectMeta .Annotations = map [string ]string {}
180
- }
181
- if newPod .ObjectMeta .Annotations [readyAnnotation ] != readyAnnotationValue {
182
- newPod .ObjectMeta .Annotations [readyAnnotation ] = readyAnnotationValue
183
- if _ , err := kubeclient .CoreV1 ().Pods (newPod .Namespace ).Update (ctx , newPod , metav1.UpdateOptions {}); err != nil {
184
- return fmt .Errorf ("error adding ready annotation to Pod %q: %w" , pod .Name , err )
185
- }
186
- }
187
- return nil
188
+ // UpdateReady updates the Pod's annotations to signal the first step to start
189
+ // by projecting the ready annotation via the Downward API.
190
+ func UpdateReady (ctx context.Context , kubeclient kubernetes.Interface , pod corev1.Pod ) error {
191
+ // PATCH the Pod's annotations to replace the ready annotation with the
192
+ // "READY" value, to signal the first step to start.
193
+ _ , err := kubeclient .CoreV1 ().Pods (pod .Namespace ).Patch (ctx , pod .Name , types .JSONPatchType , replaceReadyPatchBytes , metav1.PatchOptions {})
194
+ return err
188
195
}
189
196
190
197
// StopSidecars updates sidecar containers in the Pod to a nop image, which
0 commit comments