|
4 | 4 | "context" |
5 | 5 | "fmt" |
6 | 6 | "net/http" |
| 7 | + "strconv" |
7 | 8 |
|
8 | 9 | "k8s.io/apimachinery/pkg/api/errors" |
9 | 10 | metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" |
@@ -87,7 +88,13 @@ func (c *ManifestWorkSourceClient) Create(ctx context.Context, manifestWork *wor |
87 | 88 | newWork := manifestWork.DeepCopy() |
88 | 89 | newWork.UID = kubetypes.UID(utils.UID(c.sourceID, common.ManifestWorkGR.String(), c.namespace, newWork.Name)) |
89 | 90 | newWork.Namespace = c.namespace |
90 | | - newWork.ResourceVersion = getWorkResourceVersion(manifestWork) |
| 91 | + |
| 92 | + rv, generation, err := getWorkResourceVersion(manifestWork) |
| 93 | + if err != nil { |
| 94 | + return nil, errors.NewInternalError(err) |
| 95 | + } |
| 96 | + newWork.Generation = generation |
| 97 | + newWork.ResourceVersion = rv |
91 | 98 |
|
92 | 99 | if err := utils.EncodeManifests(newWork); err != nil { |
93 | 100 | returnErr := errors.NewInternalError(err) |
@@ -273,7 +280,12 @@ func (c *ManifestWorkSourceClient) Patch(ctx context.Context, name string, pt ku |
273 | 280 | } |
274 | 281 |
|
275 | 282 | newWork := patchedWork.DeepCopy() |
276 | | - newWork.ResourceVersion = getWorkResourceVersion(patchedWork) |
| 283 | + rv, generation, err := getWorkResourceVersion(patchedWork) |
| 284 | + if err != nil { |
| 285 | + return nil, errors.NewInternalError(err) |
| 286 | + } |
| 287 | + newWork.Generation = generation |
| 288 | + newWork.ResourceVersion = rv |
277 | 289 |
|
278 | 290 | if errs := utils.ValidateWork(newWork); len(errs) != 0 { |
279 | 291 | returnErr := errors.NewInvalid(common.ManifestWorkGK, name, errs) |
@@ -303,15 +315,27 @@ func (c *ManifestWorkSourceClient) Patch(ctx context.Context, name string, pt ku |
303 | 315 | // firstly, if no annotation is set, we will get the the resource version from work itself, |
304 | 316 | // if the wok does not have it, "0" will be returned, which means the version of the work |
305 | 317 | // will not be maintained on source, the message broker guarantees the work update order. |
306 | | -func getWorkResourceVersion(work *workv1.ManifestWork) string { |
| 318 | +func getWorkResourceVersion(work *workv1.ManifestWork) (string, int64, error) { |
| 319 | + var generation int64 |
| 320 | + var err error |
| 321 | + |
307 | 322 | resourceVersion, ok := work.Annotations[common.CloudEventsResourceVersionAnnotationKey] |
308 | 323 | if ok { |
309 | | - return resourceVersion |
| 324 | + generation, err = strconv.ParseInt(resourceVersion, 10, 16) |
| 325 | + if err != nil { |
| 326 | + return "", 0, errors.NewInternalError(err) |
| 327 | + } |
| 328 | + } |
| 329 | + |
| 330 | + if generation == 0 { |
| 331 | + generation = work.Generation |
310 | 332 | } |
311 | 333 |
|
312 | | - if work.ResourceVersion != "" { |
313 | | - return work.ResourceVersion |
| 334 | + if len(resourceVersion) == 0 && len(work.ResourceVersion) != 0 { |
| 335 | + resourceVersion = work.ResourceVersion |
| 336 | + } else { |
| 337 | + resourceVersion = "0" |
314 | 338 | } |
315 | 339 |
|
316 | | - return "0" |
| 340 | + return resourceVersion, generation, nil |
317 | 341 | } |
0 commit comments