From ef5e0fd45a66e4f4c546be605ec39f2026e17c42 Mon Sep 17 00:00:00 2001 From: Gerrit Date: Fri, 6 Aug 2021 09:33:12 +0200 Subject: [PATCH 1/2] Improve stability of finalize allocation function. --- .../internal/service/machine-service.go | 39 ++++++++++--------- 1 file changed, 20 insertions(+), 19 deletions(-) diff --git a/cmd/metal-api/internal/service/machine-service.go b/cmd/metal-api/internal/service/machine-service.go index e46713827..48fdce014 100644 --- a/cmd/metal-api/internal/service/machine-service.go +++ b/cmd/metal-api/internal/service/machine-service.go @@ -1593,25 +1593,6 @@ func (r machineResource) finalizeAllocation(request *restful.Request, response * } } - old := *m - - m.Allocation.ConsolePassword = requestPayload.ConsolePassword - m.Allocation.MachineSetup = &metal.MachineSetup{ - ImageID: m.Allocation.ImageID, - PrimaryDisk: requestPayload.PrimaryDisk, - OSPartition: requestPayload.OSPartition, - Initrd: requestPayload.Initrd, - Cmdline: requestPayload.Cmdline, - Kernel: requestPayload.Kernel, - BootloaderID: requestPayload.BootloaderID, - } - m.Allocation.Reinstall = false - - err = r.ds.UpdateMachine(&old, m) - if checkError(request, response, utils.CurrentFuncName(), err) { - return - } - vrf := "" imgs, err := r.ds.ListImages() if checkError(request, response, utils.CurrentFuncName(), err) { @@ -1637,6 +1618,26 @@ func (r machineResource) finalizeAllocation(request *restful.Request, response * } } + old := *m + + m.Allocation.ConsolePassword = requestPayload.ConsolePassword + m.Allocation.MachineSetup = &metal.MachineSetup{ + ImageID: m.Allocation.ImageID, + PrimaryDisk: requestPayload.PrimaryDisk, + OSPartition: requestPayload.OSPartition, + Initrd: requestPayload.Initrd, + Cmdline: requestPayload.Cmdline, + Kernel: requestPayload.Kernel, + BootloaderID: requestPayload.BootloaderID, + } + m.Allocation.Reinstall = false + m.Allocation.Succeeded = true + + err = r.ds.UpdateMachine(&old, m) + if checkError(request, response, utils.CurrentFuncName(), err) { + return + } + err = response.WriteHeaderAndEntity(http.StatusOK, makeMachineResponse(m, r.ds, utils.Logger(request).Sugar())) if err != nil { zapup.MustRootLogger().Error("Failed to send response", zap.Error(err)) From fd344e208e9c04c803b9168687c4543064331100 Mon Sep 17 00:00:00 2001 From: Gerrit Date: Mon, 9 Aug 2021 12:50:23 +0200 Subject: [PATCH 2/2] Migrate allocation succeeded flag for existing machines. --- .../migrations/03_allocation_succeeded.go | 40 +++++++++++++++++++ 1 file changed, 40 insertions(+) create mode 100644 cmd/metal-api/internal/datastore/migrations/03_allocation_succeeded.go diff --git a/cmd/metal-api/internal/datastore/migrations/03_allocation_succeeded.go b/cmd/metal-api/internal/datastore/migrations/03_allocation_succeeded.go new file mode 100644 index 000000000..395f9e58b --- /dev/null +++ b/cmd/metal-api/internal/datastore/migrations/03_allocation_succeeded.go @@ -0,0 +1,40 @@ +package migrations + +import ( + r "gopkg.in/rethinkdb/rethinkdb-go.v6" + + "github.com/metal-stack/metal-api/cmd/metal-api/internal/datastore" +) + +func init() { + datastore.MustRegisterMigration(datastore.Migration{ + Name: "set allocation succeeded to true for allocated machines (#210)", + Version: 3, + Up: func(db *r.Term, session r.QueryExecutor, rs *datastore.RethinkStore) error { + ms, err := rs.ListMachines() + if err != nil { + return err + } + + for i := range ms { + old := ms[i] + if old.Allocation == nil || old.Allocation.ConsolePassword == "" { + // these machines have never succeeded finalize-allocation + continue + } + + if old.Allocation.Succeeded { + continue + } + + n := old + n.Allocation.Succeeded = true + err = rs.UpdateMachine(&old, &n) + if err != nil { + return err + } + } + return nil + }, + }) +}