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