Skip to content

Commit 5f376fd

Browse files
committed
update
Signed-off-by: David VIEJO <[email protected]>
1 parent c4210a9 commit 5f376fd

File tree

3 files changed

+119
-34
lines changed

3 files changed

+119
-34
lines changed

controllers/ca/ca_controller.go

+57-28
Original file line numberDiff line numberDiff line change
@@ -933,6 +933,32 @@ func Reconcile(
933933
// it doesn't exist
934934
return ctrl.Result{}, err
935935
}
936+
} else if exists && helmStatus.Info.Status == release.StatusPendingRollback {
937+
historyAction := action.NewHistory(cfg)
938+
history, err := historyAction.Run(releaseName)
939+
if err != nil {
940+
return ctrl.Result{}, err
941+
}
942+
if len(history) > 0 {
943+
// find the last deployed revision
944+
// and rollback to it
945+
// sort history by revision number descending using raw go
946+
sort.Slice(history, func(i, j int) bool {
947+
return history[i].Version > history[j].Version
948+
})
949+
for _, historyItem := range history {
950+
if historyItem.Info.Status == release.StatusDeployed {
951+
rollbackStatus := action.NewRollback(cfg)
952+
rollbackStatus.Version = historyItem.Version
953+
err = rollbackStatus.Run(releaseName)
954+
if err != nil {
955+
// it doesn't exist
956+
return ctrl.Result{}, err
957+
}
958+
break
959+
}
960+
}
961+
}
936962
}
937963
log.Debugf("Release %s exists=%v", releaseName, exists)
938964
clientSet, err := utils.GetClientKubeWithConf(r.Config)
@@ -969,35 +995,38 @@ func Reconcile(
969995
Status: "True",
970996
LastTransitionTime: v1.Time{},
971997
})
972-
c, err := GetConfig(hlf, clientSet, releaseName, req.Namespace)
973-
if err != nil {
974-
return ctrl.Result{}, err
975-
}
976-
inrec, err := json.Marshal(c)
977-
if err != nil {
978-
return ctrl.Result{}, err
979-
}
980-
var inInterface map[string]interface{}
981-
err = json.Unmarshal(inrec, &inInterface)
982-
if err != nil {
983-
return ctrl.Result{}, err
984-
}
985-
cmd := action.NewUpgrade(cfg)
986-
cmd.Timeout = r.Timeout
987-
cmd.Wait = r.Wait
988-
cmd.MaxHistory = r.MaxHistory
989-
settings := cli.New()
990-
chartPath, err := cmd.LocateChart(r.ChartPath, settings)
991-
ch, err := loader.Load(chartPath)
992-
if err != nil {
993-
return ctrl.Result{}, err
994-
}
995-
release, err := cmd.Run(releaseName, ch, inInterface)
996-
if err != nil {
997-
setConditionStatus(hlf, hlfv1alpha1.FailedStatus, false, err, false)
998-
return r.updateCRStatusOrFailReconcile(ctx, r.Log, hlf)
998+
if helmStatus.Info.Status != release.StatusPendingUpgrade {
999+
c, err := GetConfig(hlf, clientSet, releaseName, req.Namespace)
1000+
if err != nil {
1001+
return ctrl.Result{}, err
1002+
}
1003+
inrec, err := json.Marshal(c)
1004+
if err != nil {
1005+
return ctrl.Result{}, err
1006+
}
1007+
var inInterface map[string]interface{}
1008+
err = json.Unmarshal(inrec, &inInterface)
1009+
if err != nil {
1010+
return ctrl.Result{}, err
1011+
}
1012+
cmd := action.NewUpgrade(cfg)
1013+
cmd.Timeout = r.Timeout
1014+
cmd.Wait = r.Wait
1015+
cmd.MaxHistory = r.MaxHistory
1016+
1017+
settings := cli.New()
1018+
chartPath, err := cmd.LocateChart(r.ChartPath, settings)
1019+
ch, err := loader.Load(chartPath)
1020+
if err != nil {
1021+
return ctrl.Result{}, err
1022+
}
1023+
release, err := cmd.Run(releaseName, ch, inInterface)
1024+
if err != nil {
1025+
setConditionStatus(hlf, hlfv1alpha1.FailedStatus, false, err, false)
1026+
return r.updateCRStatusOrFailReconcile(ctx, r.Log, hlf)
1027+
}
1028+
log.Debugf("Chart upgraded %s", release.Name)
9991029
}
1000-
log.Debugf("Chart upgraded %s", release.Name)
10011030
if !reflect.DeepEqual(fca.Status, hlf.Status) {
10021031
if err := r.Status().Update(ctx, fca); err != nil {
10031032
log.Debugf("Error updating the status: %v", err)

controllers/ordnode/ordnode_controller.go

+29-1
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import (
1111
"helm.sh/helm/v3/pkg/release"
1212
"os"
1313
"reflect"
14+
"sort"
1415
"strings"
1516
"time"
1617

@@ -162,6 +163,32 @@ func (r *FabricOrdererNodeReconciler) Reconcile(ctx context.Context, req ctrl.Re
162163
// it doesn't exist
163164
return ctrl.Result{}, err
164165
}
166+
} else if exists && helmStatus.Info.Status == release.StatusPendingRollback {
167+
historyAction := action.NewHistory(cfg)
168+
history, err := historyAction.Run(releaseName)
169+
if err != nil {
170+
return ctrl.Result{}, err
171+
}
172+
if len(history) > 0 {
173+
// find the last deployed revision
174+
// and rollback to it
175+
// sort history by revision number descending using raw go
176+
sort.Slice(history, func(i, j int) bool {
177+
return history[i].Version > history[j].Version
178+
})
179+
for _, historyItem := range history {
180+
if historyItem.Info.Status == release.StatusDeployed {
181+
rollbackStatus := action.NewRollback(cfg)
182+
rollbackStatus.Version = historyItem.Version
183+
err = rollbackStatus.Run(releaseName)
184+
if err != nil {
185+
// it doesn't exist
186+
return ctrl.Result{}, err
187+
}
188+
break
189+
}
190+
}
191+
}
165192
}
166193
log.Printf("Release %s exists=%v", releaseName, exists)
167194
clientSet, err := utils.GetClientKubeWithConf(r.Config)
@@ -216,7 +243,7 @@ func (r *FabricOrdererNodeReconciler) Reconcile(ctx context.Context, req ctrl.Re
216243
lastTimeCertsRenewed = &newTime
217244
log.Infof("Certs updated, last time updated: %v", lastTimeCertsRenewed)
218245
requeueAfter = time.Minute * 1
219-
} else {
246+
} else if helmStatus.Info.Status != release.StatusPendingUpgrade {
220247
c, err := getConfig(fabricOrdererNode, clientSet, releaseName, req.Namespace, false)
221248
if err != nil {
222249
return ctrl.Result{}, err
@@ -478,6 +505,7 @@ func (r *FabricOrdererNodeReconciler) upgradeChart(
478505
cmd.Wait = r.Wait
479506
cmd.Timeout = r.Timeout
480507
cmd.MaxHistory = r.MaxHistory
508+
481509
release, err := cmd.Run(releaseName, ch, inInterface)
482510
if err != nil {
483511
return err

controllers/peer/peer_controller.go

+33-5
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import (
1111
"helm.sh/helm/v3/pkg/release"
1212
"os"
1313
"reflect"
14+
"sort"
1415
"strings"
1516
"time"
1617

@@ -378,6 +379,32 @@ func (r *FabricPeerReconciler) Reconcile(ctx context.Context, req ctrl.Request)
378379
// it doesn't exist
379380
return ctrl.Result{}, err
380381
}
382+
} else if exists && helmStatus.Info.Status == release.StatusPendingRollback {
383+
historyAction := action.NewHistory(cfg)
384+
history, err := historyAction.Run(releaseName)
385+
if err != nil {
386+
return ctrl.Result{}, err
387+
}
388+
if len(history) > 0 {
389+
// find the last deployed revision
390+
// and rollback to it
391+
// sort history by revision number descending using raw go
392+
sort.Slice(history, func(i, j int) bool {
393+
return history[i].Version > history[j].Version
394+
})
395+
for _, historyItem := range history {
396+
if historyItem.Info.Status == release.StatusDeployed {
397+
rollbackStatus := action.NewRollback(cfg)
398+
rollbackStatus.Version = historyItem.Version
399+
err = rollbackStatus.Run(releaseName)
400+
if err != nil {
401+
// it doesn't exist
402+
return ctrl.Result{}, err
403+
}
404+
break
405+
}
406+
}
407+
}
381408
}
382409
log.Debugf("Release %s exists=%v", releaseName, exists)
383410
clientSet, err := utils.GetClientKubeWithConf(r.Config)
@@ -449,11 +476,12 @@ func (r *FabricPeerReconciler) Reconcile(ctx context.Context, req ctrl.Request)
449476
r.setConditionStatus(ctx, fabricPeer, hlfv1alpha1.FailedStatus, false, err, false)
450477
return r.updateCRStatusOrFailReconcile(ctx, r.Log, fabricPeer)
451478
}
452-
453-
err = r.upgradeChart(cfg, err, ns, releaseName, c)
454-
if err != nil {
455-
r.setConditionStatus(ctx, fabricPeer, hlfv1alpha1.FailedStatus, false, err, false)
456-
return r.updateCRStatusOrFailReconcile(ctx, r.Log, fabricPeer)
479+
if helmStatus.Info.Status != release.StatusPendingUpgrade {
480+
err = r.upgradeChart(cfg, err, ns, releaseName, c)
481+
if err != nil {
482+
r.setConditionStatus(ctx, fabricPeer, hlfv1alpha1.FailedStatus, false, err, false)
483+
return r.updateCRStatusOrFailReconcile(ctx, r.Log, fabricPeer)
484+
}
457485
}
458486
requeueAfter = time.Minute * 10
459487
}

0 commit comments

Comments
 (0)