-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Expand file tree
/
Copy pathdynamographdeployment_conversion.go
More file actions
839 lines (769 loc) · 29.6 KB
/
Copy pathdynamographdeployment_conversion.go
File metadata and controls
839 lines (769 loc) · 29.6 KB
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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
/*
* SPDX-FileCopyrightText: Copyright (c) 2025-2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
* SPDX-License-Identifier: Apache-2.0
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
// Conversion between v1alpha1 and v1beta1 DynamoGraphDeployment.
//
// v1beta1 is the hub (see api/v1beta1/dynamographdeployment_conversion.go).
// This file implements v1alpha1 as a spoke in the hub-and-spoke model used by
// controller-runtime's conversion webhook.
//
// Round-trip fidelity
//
// For every v1beta1 input V, ConvertTo(ConvertFrom(V)) must equal V bitwise.
// Lossy-direction fields (v1alpha1 shapes with no v1beta1 equivalent, and
// v1beta1 ordering that is not representable in v1alpha1's unordered map) are
// preserved via reserved "nvidia.com/dgd-*" annotations. The annotation
// namespace is operator-owned; user-set annotations with the same prefix are
// parsed best-effort and consumed on ConvertFrom.
package v1alpha1
import (
"fmt"
"slices"
"strings"
apiequality "k8s.io/apimachinery/pkg/api/equality"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/util/sets"
"k8s.io/utils/ptr"
"sigs.k8s.io/controller-runtime/pkg/conversion"
v1beta1 "github.com/ai-dynamo/dynamo/deploy/operator/api/v1beta1"
)
const (
dgdConversionAnnotationPrefix = "nvidia.com/dgd-"
annDGDSpec = dgdConversionAnnotationPrefix + "spec"
annDGDStatus = dgdConversionAnnotationPrefix + "status"
)
// IsDynamoGraphDeploymentConversionAnnotation reports whether key is reserved
// for DGD conversion bookkeeping.
func IsDynamoGraphDeploymentConversionAnnotation(key string) bool {
return strings.HasPrefix(key, dgdConversionAnnotationPrefix)
}
// DynamoGraphDeploymentConversionContext carries DGD-level conversion context
// that component converters cannot derive from their local inputs.
// +kubebuilder:object:generate=false
type DynamoGraphDeploymentConversionContext struct {
IncludeOriginSplits bool
SaveHubOrigin bool
}
// ConvertTo converts this DynamoGraphDeployment (v1alpha1) into the hub
// version (v1beta1).
func (src *DynamoGraphDeployment) ConvertTo(dstRaw conversion.Hub) error {
dst, ok := dstRaw.(*v1beta1.DynamoGraphDeployment)
if !ok {
return fmt.Errorf("expected *v1beta1.DynamoGraphDeployment but got %T", dstRaw)
}
dst.ObjectMeta = *src.ObjectMeta.DeepCopy()
var restoredHubSpec *v1beta1.DynamoGraphDeploymentSpec
if raw, ok := getAnnFromObj(&dst.ObjectMeta, annDGDSpec); ok && raw != "" {
if spec, ok := restoreDGDHubSpec(raw); ok {
restoredHubSpec = &spec
}
}
hubOrigin := restoredHubSpec != nil
scrubDGDInternalAnnotations(&dst.ObjectMeta)
ctx := DynamoGraphDeploymentConversionContext{
IncludeOriginSplits: !hubOrigin,
}
var spokeSave DynamoGraphDeploymentSpec
if err := ConvertFromDynamoGraphDeploymentSpec(&src.Spec, &dst.Spec, restoredHubSpec, &spokeSave, ctx); err != nil {
return err
}
var statusSave DynamoGraphDeploymentStatus
saveDGDAlphaOnlyStatus(&src.Status, &statusSave)
ConvertFromDynamoGraphDeploymentStatus(&src.Status, &dst.Status)
if !dgdAlphaSpecSaveIsZero(&spokeSave) || !dgdAlphaStatusSaveIsZero(&statusSave) {
if err := saveDGDSpokeAnnotations(&spokeSave, &statusSave, dst); err != nil {
return err
}
}
return nil
}
// ConvertFromDynamoGraphDeploymentSpec converts the DGD spec from v1alpha1 to
// v1beta1.
func ConvertFromDynamoGraphDeploymentSpec(src *DynamoGraphDeploymentSpec, dst *v1beta1.DynamoGraphDeploymentSpec, restored *v1beta1.DynamoGraphDeploymentSpec, save *DynamoGraphDeploymentSpec, ctx DynamoGraphDeploymentConversionContext) error {
// Convert fields represented by both versions from the live source.
dst.Annotations = src.Annotations
dst.Labels = src.Labels
dst.PriorityClassName = src.PriorityClassName
dst.BackendFramework = src.BackendFramework
if src.Restart != nil {
dst.Restart = &v1beta1.Restart{}
ConvertFromRestart(src.Restart, dst.Restart)
}
if src.TopologyConstraint != nil {
dst.TopologyConstraint = &v1beta1.SpecTopologyConstraint{}
ConvertFromSpecTopologyConstraint(src.TopologyConstraint, dst.TopologyConstraint)
}
if src.Experimental != nil {
dst.Experimental = &v1beta1.DynamoGraphDeploymentExperimentalSpec{}
ConvertFromDynamoGraphDeploymentExperimentalSpec(src.Experimental, dst.Experimental)
}
dst.Env = src.Envs
if save != nil && len(src.PVCs) > 0 {
save.PVCs = slices.Clone(src.PVCs)
}
// Restore target-only component leaves from the preserved hub payload.
restoredHubComponents := restoredDGDHubComponentsByName(restored)
// Components: v1alpha1 map -> v1beta1 list. Prefer the preserved hub list
// order when it carried non-sorted order information; otherwise sort by
// name for deterministic alpha-first output.
if len(src.Services) > 0 {
names := dgdServiceNamesInEmissionOrder(src.Services, restored)
dst.Components = make([]v1beta1.DynamoComponentDeploymentSharedSpec, 0, len(names))
for _, name := range names {
compSrc := src.Services[name]
if compSrc == nil {
if save != nil {
if save.Services == nil {
save.Services = map[string]*DynamoComponentDeploymentSharedSpec{}
}
save.Services[name] = nil
}
continue
}
restoredShared := restoredHubComponents[name]
var compDst v1beta1.DynamoComponentDeploymentSharedSpec
var compSave *DynamoComponentDeploymentSharedSpec
if save != nil {
compSave = &DynamoComponentDeploymentSharedSpec{}
}
sharedCtx := DynamoComponentDeploymentSharedSpecConversionContext{
IncludeOriginSplits: ctx.IncludeOriginSplits,
PodTemplateOrigin: restoredShared != nil && restoredShared.PodTemplate != nil,
}
if err := ConvertFromDynamoComponentDeploymentSharedSpec(compSrc, &compDst, restoredShared, compSave, sharedCtx); err != nil {
return fmt.Errorf("component %q: %w", name, err)
}
// In v1alpha1 DGD, the services-map key is the canonical name and
// any value in compSrc.ServiceName is treated as legacy/dead by
// the reconciler (graph.go materialises DCDs with ServiceName =
// map key). Force the v1beta1 ComponentName to the map key so the
// +listMapKey=name invariant (and the round-trip identity) hold, and
// save the (now redundant) v1alpha1 ServiceName so a mismatched
// value still round-trips.
compDst.ComponentName = name
if save != nil && compSrc.ServiceName != "" && compSrc.ServiceName != name {
compSave.ServiceName = compSrc.ServiceName
}
if save != nil && !sharedAlphaSpecSaveIsZero(compSave) {
if save.Services == nil {
save.Services = map[string]*DynamoComponentDeploymentSharedSpec{}
}
save.Services[name] = compSave
}
dst.Components = append(dst.Components, compDst)
}
}
return nil
}
func saveDGDSpokeAnnotations(specSave *DynamoGraphDeploymentSpec, statusSave *DynamoGraphDeploymentStatus, dst *v1beta1.DynamoGraphDeployment) error {
if !dgdAlphaSpecSaveIsZero(specSave) {
data, err := marshalDGDSpokeSpec(specSave)
if err != nil {
return fmt.Errorf("preserve DGD spoke spec: %w", err)
}
setAnnOnObj(&dst.ObjectMeta, annDGDSpec, string(data))
}
if !dgdAlphaStatusSaveIsZero(statusSave) {
if err := setJSONAnnOnObj(&dst.ObjectMeta, annDGDStatus, statusSave); err != nil {
return err
}
}
return nil
}
func restoredDGDHubComponentsByName(restored *v1beta1.DynamoGraphDeploymentSpec) map[string]*v1beta1.DynamoComponentDeploymentSharedSpec {
if restored == nil || len(restored.Components) == 0 {
return nil
}
out := make(map[string]*v1beta1.DynamoComponentDeploymentSharedSpec, len(restored.Components))
for i := range restored.Components {
out[restored.Components[i].ComponentName] = &restored.Components[i]
}
return out
}
func dgdServiceNamesInEmissionOrder(services map[string]*DynamoComponentDeploymentSharedSpec, restored *v1beta1.DynamoGraphDeploymentSpec) []string {
if len(services) == 0 {
return nil
}
if restored == nil || !dgdComponentOrderNeedsPreservation(restored.Components) {
return sets.List(sets.KeySet(services))
}
remaining := sets.KeySet(services)
out := make([]string, 0, len(services))
for _, comp := range restored.Components {
name := comp.ComponentName
if !remaining.Has(name) {
continue
}
remaining.Delete(name)
out = append(out, name)
}
return append(out, sets.List(remaining)...)
}
func dgdComponentOrderNeedsPreservation(components []v1beta1.DynamoComponentDeploymentSharedSpec) bool {
for i := 1; i < len(components); i++ {
if components[i-1].ComponentName > components[i].ComponentName {
return true
}
}
return false
}
func restoreDGDAlphaOnlySpecFromSaved(dstSpec *DynamoGraphDeploymentSpec, savedSpec *DynamoGraphDeploymentSpec) {
if savedSpec != nil {
if len(dstSpec.PVCs) == 0 {
dstSpec.PVCs = slices.Clone(savedSpec.PVCs)
}
for name, savedComp := range savedSpec.Services {
if savedComp != nil {
continue
}
if dstSpec.Services == nil {
dstSpec.Services = map[string]*DynamoComponentDeploymentSharedSpec{}
}
if _, ok := dstSpec.Services[name]; !ok {
dstSpec.Services[name] = nil
}
}
for name, dstComp := range dstSpec.Services {
if dstComp == nil || savedSpec.Services == nil {
continue
}
savedComp := savedSpec.Services[name]
if savedComp == nil {
continue
}
if dstComp.ServiceName == "" && savedComp.ServiceName != "" && savedComp.ServiceName != name {
dstComp.ServiceName = savedComp.ServiceName
}
}
}
}
func restoreDGDAlphaOnlyStatusFromSaved(dstStatus *DynamoGraphDeploymentStatus, savedStatus *DynamoGraphDeploymentStatus) {
if savedStatus == nil {
return
}
for name, dstSvc := range dstStatus.Services {
savedSvc, ok := savedStatus.Services[name]
if !ok {
continue
}
if shouldRestoreSavedServiceReplicaStatus(&dstSvc, &savedSvc) {
dstSvc.ComponentName = savedSvc.ComponentName
dstSvc.ComponentNames = slices.Clone(savedSvc.ComponentNames)
}
dstStatus.Services[name] = dstSvc
}
}
func dgdAlphaSpecSaveIsZero(save *DynamoGraphDeploymentSpec) bool {
return save == nil || apiequality.Semantic.DeepEqual(*save, DynamoGraphDeploymentSpec{})
}
func saveDGDAlphaOnlyStatus(src *DynamoGraphDeploymentStatus, save *DynamoGraphDeploymentStatus) {
if src == nil || save == nil {
return
}
for name, svc := range src.Services {
if !serviceStatusComponentNameNeedsPreservation(&svc) {
continue
}
if save.Services == nil {
save.Services = map[string]ServiceReplicaStatus{}
}
save.Services[name] = ServiceReplicaStatus{
ComponentName: svc.ComponentName,
ComponentNames: slices.Clone(svc.ComponentNames),
}
}
}
func dgdAlphaStatusSaveIsZero(save *DynamoGraphDeploymentStatus) bool {
return save == nil || apiequality.Semantic.DeepEqual(*save, DynamoGraphDeploymentStatus{})
}
func dgdHubSpecSaveIsZero(save *v1beta1.DynamoGraphDeploymentSpec) bool {
return save == nil || apiequality.Semantic.DeepEqual(*save, v1beta1.DynamoGraphDeploymentSpec{})
}
func dgdHubComponentSaveIsZero(save *v1beta1.DynamoComponentDeploymentSharedSpec) bool {
return sharedHubSpecSaveIsZero(save)
}
func serviceStatusComponentNameNeedsPreservation(src *ServiceReplicaStatus) bool {
if src == nil {
return false
}
if len(src.ComponentNames) == 0 {
return src.ComponentName != ""
}
return src.ComponentNames[len(src.ComponentNames)-1] != src.ComponentName
}
func shouldRestoreSavedServiceReplicaStatus(dst, saved *ServiceReplicaStatus) bool {
if !serviceStatusComponentNameNeedsPreservation(saved) || dst == nil {
return false
}
if slices.Equal(dst.ComponentNames, componentNamesToHub(saved)) {
return true
}
return saved.ComponentName != "" &&
len(saved.ComponentNames) == 0 &&
len(dst.ComponentNames) == 0
}
func componentNamesToHub(src *ServiceReplicaStatus) []string {
if src == nil {
return nil
}
componentNames := slices.Clone(src.ComponentNames)
if len(componentNames) == 0 && src.ComponentName != "" {
componentNames = []string{src.ComponentName}
}
return componentNames
}
func restoreDGDSpokeAnnotations(obj metav1.Object) (*DynamoGraphDeploymentSpec, *DynamoGraphDeploymentStatus, error) {
var restoredSpokeSpec *DynamoGraphDeploymentSpec
var restoredSpokeStatus *DynamoGraphDeploymentStatus
if raw, ok := getAnnFromObj(obj, annDGDSpec); ok && raw != "" {
if spec, ok := restoreDGDSpokeSpec(raw); ok {
restoredSpokeSpec = &spec
}
}
if status, ok, err := getJSONAnnFromObj[DynamoGraphDeploymentStatus](obj, annDGDStatus); err != nil {
return nil, nil, err
} else if ok {
restoredSpokeStatus = &status
}
return restoredSpokeSpec, restoredSpokeStatus, nil
}
// ConvertFrom converts from the hub (v1beta1) DynamoGraphDeployment into this
// v1alpha1 instance.
func (dst *DynamoGraphDeployment) ConvertFrom(srcRaw conversion.Hub) error {
src, ok := srcRaw.(*v1beta1.DynamoGraphDeployment)
if !ok {
return fmt.Errorf("expected *v1beta1.DynamoGraphDeployment but got %T", srcRaw)
}
dst.ObjectMeta = *src.ObjectMeta.DeepCopy()
spokeOrigin := hasDGDSpokeAnnotations(&dst.ObjectMeta)
restoredSpokeSpec, restoredSpokeStatus, err := restoreDGDSpokeAnnotations(&dst.ObjectMeta)
if err != nil {
return err
}
scrubDGDInternalAnnotations(&dst.ObjectMeta)
ctx := DynamoGraphDeploymentConversionContext{SaveHubOrigin: !spokeOrigin}
var hubSave v1beta1.DynamoGraphDeploymentSpec
if err := ConvertToDynamoGraphDeploymentSpec(&src.Spec, &dst.Spec, restoredSpokeSpec, &hubSave, ctx); err != nil {
return err
}
ConvertToDynamoGraphDeploymentStatus(&src.Status, &dst.Status)
restoreDGDAlphaOnlySpecFromSaved(&dst.Spec, restoredSpokeSpec)
restoreDGDAlphaOnlyStatusFromSaved(&dst.Status, restoredSpokeStatus)
if !dgdHubSpecSaveIsZero(&hubSave) {
data, err := marshalDGDHubSpec(&hubSave)
if err != nil {
return fmt.Errorf("preserve DGD hub spec: %w", err)
}
setAnnOnObj(&dst.ObjectMeta, annDGDSpec, string(data))
}
return nil
}
// ConvertToDynamoGraphDeploymentSpec converts the DGD spec from v1beta1 to
// v1alpha1.
func ConvertToDynamoGraphDeploymentSpec(src *v1beta1.DynamoGraphDeploymentSpec, dst *DynamoGraphDeploymentSpec, restored *DynamoGraphDeploymentSpec, save *v1beta1.DynamoGraphDeploymentSpec, ctx DynamoGraphDeploymentConversionContext) error {
// Convert fields represented by both versions from the live source.
dst.Annotations = src.Annotations
dst.Labels = src.Labels
dst.PriorityClassName = src.PriorityClassName
dst.BackendFramework = src.BackendFramework
if src.Restart != nil {
dst.Restart = &Restart{}
ConvertToRestart(src.Restart, dst.Restart)
}
if src.TopologyConstraint != nil {
dst.TopologyConstraint = &SpecTopologyConstraint{}
ConvertToSpecTopologyConstraint(src.TopologyConstraint, dst.TopologyConstraint)
}
if src.Experimental != nil {
dst.Experimental = &DynamoGraphDeploymentExperimentalSpec{}
ConvertToDynamoGraphDeploymentExperimentalSpec(src.Experimental, dst.Experimental)
}
dst.Envs = src.Env
if len(src.Components) > 0 {
preserveComponentOrder := dgdComponentOrderNeedsPreservation(src.Components)
dst.Services = make(map[string]*DynamoComponentDeploymentSharedSpec, len(src.Components))
for i := range src.Components {
compSrc := &src.Components[i]
// v1beta1 declares +listType=map +listMapKey=name so
// the API server normally rejects duplicates, but the
// conversion path is also reached from in-memory unit-test
// fixtures and other code paths that bypass CRD validation.
// Surface duplicates here as a hard error rather than
// silently overwriting the earlier entry on map insertion.
if _, dup := dst.Services[compSrc.ComponentName]; dup {
return fmt.Errorf("duplicate component name %q in spec.components", compSrc.ComponentName)
}
compDst := &DynamoComponentDeploymentSharedSpec{}
var preservedShared *DynamoComponentDeploymentSharedSpec
if restored != nil && restored.Services != nil {
preservedShared = restored.Services[compSrc.ComponentName]
}
var compSave *v1beta1.DynamoComponentDeploymentSharedSpec
if save != nil {
compSave = &v1beta1.DynamoComponentDeploymentSharedSpec{
ComponentName: compSrc.ComponentName,
}
}
if err := ConvertToDynamoComponentDeploymentSharedSpec(compSrc, compDst, preservedShared, compSave); err != nil {
return fmt.Errorf("component %q: %w", compSrc.ComponentName, err)
}
// In v1alpha1 the services-map key is the canonical name; the
// per-entry ServiceName field is redundant. Keep it empty for
// v1beta1-first inputs; restore saved mismatches after the full
// spec conversion.
compDst.ServiceName = ""
dst.Services[compSrc.ComponentName] = compDst
if save != nil && (preserveComponentOrder || !dgdHubComponentSaveIsZero(compSave) || ctx.SaveHubOrigin && dgdHubComponentOriginSaveNeeded(compSrc)) {
save.Components = append(save.Components, *compSave)
}
}
}
return nil
}
func dgdHubComponentOriginSaveNeeded(src *v1beta1.DynamoComponentDeploymentSharedSpec) bool {
return src != nil && src.PodTemplate != nil
}
func marshalDGDHubSpec(src *v1beta1.DynamoGraphDeploymentSpec) ([]byte, error) {
return marshalPreservedSpec(*src.DeepCopy(), func(spec *v1beta1.DynamoGraphDeploymentSpec, records *[]preservedRawJSON) {
for i := range spec.Components {
if spec.Components[i].EPPConfig != nil {
preserveEPPPluginParameters(spec.Components[i].EPPConfig.Config, fmt.Sprintf("components/%d/eppConfig/config", i), records)
}
}
})
}
func restoreDGDHubSpec(raw string) (v1beta1.DynamoGraphDeploymentSpec, bool) {
return restorePreservedSpec(raw, func(spec *v1beta1.DynamoGraphDeploymentSpec, records []preservedRawJSON) {
for i := range spec.Components {
if spec.Components[i].EPPConfig != nil {
restoreEPPPluginParameters(spec.Components[i].EPPConfig.Config, fmt.Sprintf("components/%d/eppConfig/config", i), records)
}
}
})
}
func marshalDGDSpokeSpec(src *DynamoGraphDeploymentSpec) ([]byte, error) {
return marshalPreservedSpec(*src.DeepCopy(), func(spec *DynamoGraphDeploymentSpec, records *[]preservedRawJSON) {
for name, svc := range spec.Services {
if svc != nil && svc.EPPConfig != nil {
preserveEPPPluginParameters(svc.EPPConfig.Config, fmt.Sprintf("services/%s/eppConfig/config", name), records)
}
}
})
}
func restoreDGDSpokeSpec(raw string) (DynamoGraphDeploymentSpec, bool) {
return restorePreservedSpec(raw, func(spec *DynamoGraphDeploymentSpec, records []preservedRawJSON) {
for name, svc := range spec.Services {
if svc != nil && svc.EPPConfig != nil {
restoreEPPPluginParameters(svc.EPPConfig.Config, fmt.Sprintf("services/%s/eppConfig/config", name), records)
}
}
})
}
func hasDGDSpokeAnnotations(obj metav1.Object) bool {
_, hasSpec := getAnnFromObj(obj, annDGDSpec)
_, hasStatus := getAnnFromObj(obj, annDGDStatus)
return hasSpec || hasStatus
}
func scrubDGDInternalAnnotations(obj metav1.Object) {
for _, key := range []string{
annDGDSpec,
annDGDStatus,
} {
delAnnFromObj(obj, key)
}
}
// ConvertFromRestart converts the restart spec from v1alpha1 to v1beta1.
func ConvertFromRestart(src *Restart, dst *v1beta1.Restart) {
*dst = v1beta1.Restart{ID: src.ID}
if src.Strategy != nil {
dst.Strategy = &v1beta1.RestartStrategy{}
ConvertFromRestartStrategy(src.Strategy, dst.Strategy)
}
}
// ConvertToRestart converts the restart spec from v1beta1 to v1alpha1.
func ConvertToRestart(src *v1beta1.Restart, dst *Restart) {
*dst = Restart{ID: src.ID}
if src.Strategy != nil {
dst.Strategy = &RestartStrategy{}
ConvertToRestartStrategy(src.Strategy, dst.Strategy)
}
}
// ConvertFromRestartStrategy converts the restart strategy from v1alpha1 to
// v1beta1.
func ConvertFromRestartStrategy(src *RestartStrategy, dst *v1beta1.RestartStrategy) {
*dst = v1beta1.RestartStrategy{
Type: v1beta1.RestartStrategyType(src.Type),
Order: slices.Clone(src.Order),
}
}
// ConvertToRestartStrategy converts the restart strategy from v1beta1 to
// v1alpha1.
func ConvertToRestartStrategy(src *v1beta1.RestartStrategy, dst *RestartStrategy) {
*dst = RestartStrategy{
Type: RestartStrategyType(src.Type),
Order: slices.Clone(src.Order),
}
}
// ConvertFromDynamoGraphDeploymentExperimentalSpec converts graph-level
// experimental config from v1alpha1 to v1beta1.
func ConvertFromDynamoGraphDeploymentExperimentalSpec(src *DynamoGraphDeploymentExperimentalSpec, dst *v1beta1.DynamoGraphDeploymentExperimentalSpec) {
if src.KvTransferPolicy != nil {
dst.KvTransferPolicy = &v1beta1.KvTransferPolicy{}
ConvertFromKvTransferPolicy(src.KvTransferPolicy, dst.KvTransferPolicy)
}
}
// ConvertToDynamoGraphDeploymentExperimentalSpec converts graph-level
// experimental config from v1beta1 to v1alpha1.
func ConvertToDynamoGraphDeploymentExperimentalSpec(src *v1beta1.DynamoGraphDeploymentExperimentalSpec, dst *DynamoGraphDeploymentExperimentalSpec) {
if src.KvTransferPolicy != nil {
dst.KvTransferPolicy = &KvTransferPolicy{}
ConvertToKvTransferPolicy(src.KvTransferPolicy, dst.KvTransferPolicy)
}
}
// ConvertFromKvTransferPolicy converts KV transfer policy from v1alpha1 to
// v1beta1.
func ConvertFromKvTransferPolicy(src *KvTransferPolicy, dst *v1beta1.KvTransferPolicy) {
*dst = v1beta1.KvTransferPolicy{
ClusterTopologyName: src.ClusterTopologyName,
LabelKey: src.LabelKey,
Domain: v1beta1.TopologyDomain(src.Domain),
Enforcement: v1beta1.KvTransferEnforcement(src.Enforcement),
}
if src.PreferredWeight != nil {
dst.PreferredWeight = ptr.To(*src.PreferredWeight)
}
}
// ConvertToKvTransferPolicy converts KV transfer policy from v1beta1 to
// v1alpha1.
func ConvertToKvTransferPolicy(src *v1beta1.KvTransferPolicy, dst *KvTransferPolicy) {
*dst = KvTransferPolicy{
ClusterTopologyName: src.ClusterTopologyName,
LabelKey: src.LabelKey,
Domain: TopologyDomain(src.Domain),
Enforcement: KvTransferEnforcement(src.Enforcement),
}
if src.PreferredWeight != nil {
dst.PreferredWeight = ptr.To(*src.PreferredWeight)
}
}
// ConvertFromSpecTopologyConstraint converts deployment topology constraints
// from v1alpha1 to v1beta1.
func ConvertFromSpecTopologyConstraint(src *SpecTopologyConstraint, dst *v1beta1.SpecTopologyConstraint) {
*dst = v1beta1.SpecTopologyConstraint{
ClusterTopologyName: src.TopologyProfile,
PackDomain: v1beta1.TopologyDomain(src.PackDomain),
}
}
// ConvertToSpecTopologyConstraint converts deployment topology constraints
// from v1beta1 to v1alpha1.
func ConvertToSpecTopologyConstraint(src *v1beta1.SpecTopologyConstraint, dst *SpecTopologyConstraint) {
*dst = SpecTopologyConstraint{
TopologyProfile: src.ClusterTopologyName,
PackDomain: TopologyDomain(src.PackDomain),
}
}
// ConvertFromDynamoGraphDeploymentStatus converts the DGD status from
// v1alpha1 to v1beta1.
func ConvertFromDynamoGraphDeploymentStatus(src *DynamoGraphDeploymentStatus, dst *v1beta1.DynamoGraphDeploymentStatus) {
dst.ObservedGeneration = src.ObservedGeneration
dst.State = v1beta1.DGDState(src.State)
if src.Placement != nil {
dst.Placement = &v1beta1.PlacementStatus{
State: v1beta1.PlacementScoreState(src.Placement.State),
}
if src.Placement.Score != nil {
dst.Placement.Score = ptr.To(*src.Placement.Score)
}
} else {
dst.Placement = nil
}
if len(src.Conditions) > 0 {
dst.Conditions = make([]metav1.Condition, 0, len(src.Conditions))
for _, c := range src.Conditions {
dst.Conditions = append(dst.Conditions, *c.DeepCopy())
}
}
if len(src.Services) > 0 {
dst.Components = make(map[string]v1beta1.ComponentReplicaStatus, len(src.Services))
for k, v := range src.Services {
var component v1beta1.ComponentReplicaStatus
ConvertFromServiceReplicaStatus(&v, &component)
dst.Components[k] = component
}
}
if src.Restart != nil {
dst.Restart = &v1beta1.RestartStatus{}
ConvertFromRestartStatus(src.Restart, dst.Restart)
}
if len(src.Checkpoints) > 0 {
dst.Checkpoints = make(map[string]v1beta1.ComponentCheckpointStatus, len(src.Checkpoints))
for k, v := range src.Checkpoints {
var checkpoint v1beta1.ComponentCheckpointStatus
ConvertFromServiceCheckpointStatus(&v, &checkpoint)
dst.Checkpoints[k] = checkpoint
}
}
if src.RollingUpdate != nil {
dst.RollingUpdate = &v1beta1.RollingUpdateStatus{}
ConvertFromRollingUpdateStatus(src.RollingUpdate, dst.RollingUpdate)
}
}
// ConvertToDynamoGraphDeploymentStatus converts the DGD status from v1beta1 to
// v1alpha1.
func ConvertToDynamoGraphDeploymentStatus(src *v1beta1.DynamoGraphDeploymentStatus, dst *DynamoGraphDeploymentStatus) {
dst.ObservedGeneration = src.ObservedGeneration
dst.State = DGDState(src.State)
if src.Placement != nil {
dst.Placement = &PlacementStatus{
State: PlacementScoreState(src.Placement.State),
}
if src.Placement.Score != nil {
dst.Placement.Score = ptr.To(*src.Placement.Score)
}
} else {
dst.Placement = nil
}
if len(src.Conditions) > 0 {
dst.Conditions = make([]metav1.Condition, 0, len(src.Conditions))
for _, c := range src.Conditions {
dst.Conditions = append(dst.Conditions, *c.DeepCopy())
}
}
if len(src.Components) > 0 {
dst.Services = make(map[string]ServiceReplicaStatus, len(src.Components))
for k, v := range src.Components {
var service ServiceReplicaStatus
ConvertToServiceReplicaStatus(&v, &service)
dst.Services[k] = service
}
}
if src.Restart != nil {
dst.Restart = &RestartStatus{}
ConvertToRestartStatus(src.Restart, dst.Restart)
}
if len(src.Checkpoints) > 0 {
dst.Checkpoints = make(map[string]ServiceCheckpointStatus, len(src.Checkpoints))
for k, v := range src.Checkpoints {
var checkpoint ServiceCheckpointStatus
ConvertToServiceCheckpointStatus(&v, &checkpoint)
dst.Checkpoints[k] = checkpoint
}
}
if src.RollingUpdate != nil {
dst.RollingUpdate = &RollingUpdateStatus{}
ConvertToRollingUpdateStatus(src.RollingUpdate, dst.RollingUpdate)
}
}
// ConvertFromRestartStatus converts restart status from v1alpha1 to v1beta1.
func ConvertFromRestartStatus(src *RestartStatus, dst *v1beta1.RestartStatus) {
*dst = v1beta1.RestartStatus{
ObservedID: src.ObservedID,
Phase: v1beta1.RestartPhase(src.Phase),
InProgress: slices.Clone(src.InProgress),
}
}
// ConvertToRestartStatus converts restart status from v1beta1 to v1alpha1.
func ConvertToRestartStatus(src *v1beta1.RestartStatus, dst *RestartStatus) {
*dst = RestartStatus{
ObservedID: src.ObservedID,
Phase: RestartPhase(src.Phase),
InProgress: slices.Clone(src.InProgress),
}
}
// ConvertFromServiceCheckpointStatus converts checkpoint status from v1alpha1
// to v1beta1.
func ConvertFromServiceCheckpointStatus(src *ServiceCheckpointStatus, dst *v1beta1.ComponentCheckpointStatus) {
*dst = v1beta1.ComponentCheckpointStatus{
CheckpointName: src.CheckpointName,
CheckpointID: src.CheckpointID,
IdentityHash: src.IdentityHash,
Ready: src.Ready,
}
}
// ConvertToServiceCheckpointStatus converts checkpoint status from v1beta1 to
// v1alpha1.
func ConvertToServiceCheckpointStatus(src *v1beta1.ComponentCheckpointStatus, dst *ServiceCheckpointStatus) {
*dst = ServiceCheckpointStatus{
CheckpointName: src.CheckpointName,
CheckpointID: src.CheckpointID,
IdentityHash: src.IdentityHash,
Ready: src.Ready,
}
}
// ConvertFromRollingUpdateStatus converts rolling-update status from v1alpha1
// to v1beta1.
func ConvertFromRollingUpdateStatus(src *RollingUpdateStatus, dst *v1beta1.RollingUpdateStatus) {
*dst = v1beta1.RollingUpdateStatus{
Phase: v1beta1.RollingUpdatePhase(src.Phase),
StartTime: src.StartTime.DeepCopy(),
EndTime: src.EndTime.DeepCopy(),
UpdatedComponents: slices.Clone(src.UpdatedServices),
}
}
// ConvertToRollingUpdateStatus converts rolling-update status from v1beta1 to
// v1alpha1.
func ConvertToRollingUpdateStatus(src *v1beta1.RollingUpdateStatus, dst *RollingUpdateStatus) {
*dst = RollingUpdateStatus{
Phase: RollingUpdatePhase(src.Phase),
StartTime: src.StartTime.DeepCopy(),
EndTime: src.EndTime.DeepCopy(),
UpdatedServices: slices.Clone(src.UpdatedComponents),
}
}
// ConvertFromServiceReplicaStatus converts replica status from v1alpha1 to
// v1beta1.
func ConvertFromServiceReplicaStatus(src *ServiceReplicaStatus, dst *v1beta1.ComponentReplicaStatus) {
*dst = v1beta1.ComponentReplicaStatus{
ComponentKind: v1beta1.ComponentKind(src.ComponentKind),
ComponentNames: componentNamesToHub(src),
RuntimeNamespace: src.RuntimeNamespace,
Replicas: src.Replicas,
UpdatedReplicas: src.UpdatedReplicas,
}
if src.ReadyReplicas != nil {
dst.ReadyReplicas = ptr.To(*src.ReadyReplicas)
}
if src.AvailableReplicas != nil {
dst.AvailableReplicas = ptr.To(*src.AvailableReplicas)
}
if src.ScheduledReplicas != nil {
dst.ScheduledReplicas = ptr.To(*src.ScheduledReplicas)
}
}
// ConvertToServiceReplicaStatus converts replica status from v1beta1 to
// v1alpha1.
func ConvertToServiceReplicaStatus(src *v1beta1.ComponentReplicaStatus, dst *ServiceReplicaStatus) {
componentNames := slices.Clone(src.ComponentNames)
*dst = ServiceReplicaStatus{
ComponentKind: ComponentKind(src.ComponentKind),
ComponentNames: componentNames,
RuntimeNamespace: src.RuntimeNamespace,
Replicas: src.Replicas,
UpdatedReplicas: src.UpdatedReplicas,
}
if len(componentNames) > 0 {
dst.ComponentName = componentNames[len(componentNames)-1]
}
if src.ReadyReplicas != nil {
dst.ReadyReplicas = ptr.To(*src.ReadyReplicas)
}
if src.AvailableReplicas != nil {
dst.AvailableReplicas = ptr.To(*src.AvailableReplicas)
}
if src.ScheduledReplicas != nil {
dst.ScheduledReplicas = ptr.To(*src.ScheduledReplicas)
}
}