@@ -12,7 +12,8 @@ import (
12
12
13
13
clusterlister "open-cluster-management.io/api/client/cluster/listers/cluster/v1beta1"
14
14
worklisterv1 "open-cluster-management.io/api/client/work/listers/work/v1"
15
- clusterv1beta1 "open-cluster-management.io/api/cluster/v1beta1"
15
+ //clusterv1beta1 "open-cluster-management.io/api/cluster/v1beta1"
16
+ clusterv1alpha1 "open-cluster-management.io/api/cluster/v1alpha1"
16
17
"open-cluster-management.io/api/utils/work/v1/workapplier"
17
18
workv1 "open-cluster-management.io/api/work/v1"
18
19
workapiv1alpha1 "open-cluster-management.io/api/work/v1alpha1"
@@ -31,8 +32,17 @@ type deployReconciler struct {
31
32
func (d * deployReconciler ) reconcile (ctx context.Context , mwrSet * workapiv1alpha1.ManifestWorkReplicaSet ,
32
33
) (* workapiv1alpha1.ManifestWorkReplicaSet , reconcileState , error ) {
33
34
// Manifestwork create/update/delete logic.
34
- var placements []* clusterv1beta1.Placement
35
+
36
+ //var placements []*clusterv1beta1.Placement
37
+ //existingClusters := make(map[string]sets.String)
38
+ // placementName as key and RolloutResult is the value
39
+ //placementsRolloutResults := make(map[string]clusterv1alpha1.RolloutResult)
40
+ var errs []error
41
+ count := 0
42
+
43
+ // Getting the placements and the created ManifestWorks related to each placement
35
44
for _ , placementRef := range mwrSet .Spec .PlacementRefs {
45
+ existingClusters , addedClusters , deletedClusters := sets .New [string ](), sets .New [string ](), sets .New [string ]()
36
46
placement , err := d .placementLister .Placements (mwrSet .Namespace ).Get (placementRef .Name )
37
47
if errors .IsNotFound (err ) {
38
48
apimeta .SetStatusCondition (& mwrSet .Status .Conditions , GetPlacementDecisionVerified (workapiv1alpha1 .ReasonPlacementDecisionNotFound , "" ))
@@ -41,74 +51,101 @@ func (d *deployReconciler) reconcile(ctx context.Context, mwrSet *workapiv1alpha
41
51
if err != nil {
42
52
return mwrSet , reconcileContinue , fmt .Errorf ("failed get placement %w" , err )
43
53
}
44
- placements = append (placements , placement )
45
- }
46
54
47
- manifestWorks , err := listManifestWorksByManifestWorkReplicaSet (mwrSet , d .manifestWorkLister )
48
- if err != nil {
49
- return mwrSet , reconcileContinue , err
50
- }
55
+ //placements = append(placements, placement)
56
+ manifestWorks , err := listManifestWorksByMWRSetPlacementRef (mwrSet , placementRef .Name , d .manifestWorkLister )
57
+ if err != nil {
58
+ return mwrSet , reconcileContinue , err
59
+ }
51
60
52
- var errs []error
53
- addedClusters , deletedClusters , existingClusters := sets .New [string ](), sets .New [string ](), sets .New [string ]()
54
- for _ , mw := range manifestWorks {
55
- existingClusters .Insert (mw .Namespace )
56
- }
61
+ for _ , mw := range manifestWorks {
62
+ existingClusters .Insert (mw .Namespace )
63
+ }
57
64
58
- for _ , placement := range placements {
59
- added , deleted , err := helper .GetClusters (d .placeDecisionLister , placement , existingClusters )
65
+ rolloutHandler , err := helper .GetRollOutHandler (d .placeDecisionLister , placement , existingClusters )
60
66
if err != nil {
61
67
apimeta .SetStatusCondition (& mwrSet .Status .Conditions , GetPlacementDecisionVerified (workapiv1alpha1 .ReasonNotAsExpected , "" ))
62
68
63
69
return mwrSet , reconcileContinue , utilerrors .NewAggregate (errs )
64
70
}
65
71
66
- addedClusters = addedClusters .Union (added )
67
- deletedClusters = deletedClusters .Union (deleted )
68
- }
69
-
70
- // Create manifestWork for added clusters
71
- for cls := range addedClusters {
72
- mw , err := CreateManifestWork (mwrSet , cls )
73
- if err != nil {
74
- errs = append (errs , err )
75
- continue
72
+ // The clusterRolloutStatusFunc need different signature only clusterName as input param not helping
73
+ _ , rolloutResult , err := rolloutHandler .GetRolloutCluster (placementRef .RolloutStrategy , d .clusterRolloutStatusFunc )
74
+ //placementsRolloutResults[placementRef.Name] = rolloutResult
75
+
76
+ // Creating ManifestWork that has the ToApply status
77
+ for cls , rolloutStatue := range rolloutResult .ClustersToRollout {
78
+ if rolloutStatue .Status == clusterv1alpha1 .ToApply {
79
+ mw , err := CreateManifestWork (mwrSet , cls , placementRef .Name )
80
+ if err != nil {
81
+ errs = append (errs , err )
82
+ continue
83
+ }
84
+
85
+ _ , err = d .workApplier .Apply (ctx , mw )
86
+ if err != nil {
87
+ errs = append (errs , err )
88
+ }
89
+ }
76
90
}
77
91
78
- _ , err = d .workApplier .Apply (ctx , mw )
92
+ // checking the Added and deleted clusters.
93
+ // How to check if the added cluster is part of the rolloutResult ?
94
+ // Does the RolloutResult already consider the added clusters ?
95
+ // Better to have the deleted/added clusters within the rolloutResult
96
+ addedClusters , deletedClusters , err = helper .GetClusters (d .placeDecisionLister , placement , existingClusters )
79
97
if err != nil {
80
- errs = append (errs , err )
98
+ apimeta .SetStatusCondition (& mwrSet .Status .Conditions , GetPlacementDecisionVerified (workapiv1alpha1 .ReasonNotAsExpected , "" ))
99
+
100
+ return mwrSet , reconcileContinue , utilerrors .NewAggregate (errs )
81
101
}
82
- }
83
102
84
- // Update manifestWorks in case there are changes at ManifestWork or ManifestWorkReplicaSet
85
- for cls := range existingClusters {
86
- // Delete manifestWork for deleted clusters
87
- if deletedClusters .Has (cls ) {
88
- err = d .workApplier .Delete (ctx , cls , mwrSet .Name )
103
+ // Create manifestWork for added clusters
104
+ for cls := range addedClusters {
105
+ mw , err := CreateManifestWork (mwrSet , cls , placementRef .Name )
89
106
if err != nil {
90
107
errs = append (errs , err )
108
+ continue
91
109
}
92
- continue
93
- }
94
110
95
- mw , err := CreateManifestWork ( mwrSet , cls )
96
- if err != nil {
97
- errs = append (errs , err )
98
- continue
111
+ _ , err = d . workApplier . Apply ( ctx , mw )
112
+ if err != nil {
113
+ errs = append (errs , err )
114
+ }
99
115
}
100
116
101
- _ , err = d .workApplier .Apply (ctx , mw )
102
- if err != nil {
103
- errs = append (errs , err )
117
+ // How to rollOut updated manifestWork ? that can be handled in the clusterRolloutStatusFunc
118
+ // Update manifestWorks in case there are changes at ManifestWork or ManifestWorkReplicaSet
119
+ for cls := range existingClusters {
120
+ // Delete manifestWork for deleted clusters
121
+ if deletedClusters .Has (cls ) {
122
+ err = d .workApplier .Delete (ctx , cls , mwrSet .Name )
123
+ if err != nil {
124
+ errs = append (errs , err )
125
+ }
126
+ continue
127
+ }
128
+
129
+ mw , err := CreateManifestWork (mwrSet , cls , placementRef .Name )
130
+ if err != nil {
131
+ errs = append (errs , err )
132
+ continue
133
+ }
134
+
135
+ _ , err = d .workApplier .Apply (ctx , mw )
136
+ if err != nil {
137
+ errs = append (errs , err )
138
+ }
104
139
}
140
+ count = count + len (rolloutResult .ClustersToRollout )
105
141
}
106
142
107
143
// Set the Summary
108
144
if mwrSet .Status .Summary == (workapiv1alpha1.ManifestWorkReplicaSetSummary {}) {
109
145
mwrSet .Status .Summary = workapiv1alpha1.ManifestWorkReplicaSetSummary {}
110
146
}
111
- total := len (existingClusters ) - len (deletedClusters ) + len (addedClusters )
147
+ //total := len(existingClusters) - len(deletedClusters) + len(addedClusters)
148
+ total := count
112
149
if total < 0 {
113
150
total = 0
114
151
}
@@ -127,6 +164,13 @@ func (d *deployReconciler) reconcile(ctx context.Context, mwrSet *workapiv1alpha
127
164
return mwrSet , reconcileContinue , utilerrors .NewAggregate (errs )
128
165
}
129
166
167
+ // TODO: Implement clusterRolloutStatusFunc logic.
168
+ func (d * deployReconciler ) clusterRolloutStatusFunc (clusterName string ) clusterv1alpha1.ClusterRolloutStatus {
169
+ clsRolloutStatus := clusterv1alpha1.ClusterRolloutStatus {}
170
+
171
+ return clsRolloutStatus
172
+ }
173
+
130
174
// GetManifestworkApplied return only True status if there all clusters have manifests applied as expected
131
175
func GetManifestworkApplied (reason string , message string ) metav1.Condition {
132
176
if reason == workapiv1alpha1 .ReasonAsExpected {
@@ -156,7 +200,7 @@ func getCondition(conditionType string, reason string, message string, status me
156
200
}
157
201
}
158
202
159
- func CreateManifestWork (mwrSet * workapiv1alpha1.ManifestWorkReplicaSet , clusterNS string ) (* workv1.ManifestWork , error ) {
203
+ func CreateManifestWork (mwrSet * workapiv1alpha1.ManifestWorkReplicaSet , clusterNS string , placementRefName string ) (* workv1.ManifestWork , error ) {
160
204
if clusterNS == "" {
161
205
return nil , fmt .Errorf ("invalid cluster namespace" )
162
206
}
@@ -165,7 +209,8 @@ func CreateManifestWork(mwrSet *workapiv1alpha1.ManifestWorkReplicaSet, clusterN
165
209
ObjectMeta : metav1.ObjectMeta {
166
210
Name : mwrSet .Name ,
167
211
Namespace : clusterNS ,
168
- Labels : map [string ]string {ManifestWorkReplicaSetControllerNameLabelKey : manifestWorkReplicaSetKey (mwrSet )},
212
+ Labels : map [string ]string {ManifestWorkReplicaSetControllerNameLabelKey : manifestWorkReplicaSetKey (mwrSet ),
213
+ ManifestWorkReplicaSetPlacementNameLabelKey : placementRefName },
169
214
},
170
215
Spec : mwrSet .Spec .ManifestWorkTemplate }, nil
171
216
}
0 commit comments