|
| 1 | +// |
| 2 | +// DISCLAIMER |
| 3 | +// |
| 4 | +// Copyright 2020 ArangoDB GmbH, Cologne, Germany |
| 5 | +// |
| 6 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 7 | +// you may not use this file except in compliance with the License. |
| 8 | +// You may obtain a copy of the License at |
| 9 | +// |
| 10 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | +// |
| 12 | +// Unless required by applicable law or agreed to in writing, software |
| 13 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 14 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 | +// See the License for the specific language governing permissions and |
| 16 | +// limitations under the License. |
| 17 | +// |
| 18 | +// Copyright holder is ArangoDB GmbH, Cologne, Germany |
| 19 | +// |
| 20 | +// Author Ewout Prangsma |
| 21 | +// |
| 22 | + |
| 23 | +package reconcile |
| 24 | + |
| 25 | +import ( |
| 26 | + "context" |
| 27 | + |
| 28 | + api "github.com/arangodb/kube-arangodb/pkg/apis/deployment/v1" |
| 29 | + "github.com/rs/zerolog" |
| 30 | +) |
| 31 | + |
| 32 | +func init() { |
| 33 | + registerAction(api.ActionTypeSetMemberCurrentImage, newSetCurrentMemberImageAction) |
| 34 | +} |
| 35 | + |
| 36 | +// newSetCurrentImageAction creates a new Action that implements the given |
| 37 | +// planned SetCurrentImage action. |
| 38 | +func newSetCurrentMemberImageAction(log zerolog.Logger, action api.Action, actionCtx ActionContext) Action { |
| 39 | + a := &setCurrentMemberImageAction{} |
| 40 | + |
| 41 | + a.actionImpl = newActionImplDefRef(log, action, actionCtx, upgradeMemberTimeout) |
| 42 | + |
| 43 | + return a |
| 44 | +} |
| 45 | + |
| 46 | +// setCurrentImageAction implements an SetCurrentImage. |
| 47 | +type setCurrentMemberImageAction struct { |
| 48 | + // actionImpl implement timeout and member id functions |
| 49 | + actionImpl |
| 50 | +} |
| 51 | + |
| 52 | +// Start performs the start of the action. |
| 53 | +// Returns true if the action is completely finished, false in case |
| 54 | +// the start time needs to be recorded and a ready condition needs to be checked. |
| 55 | +func (a *setCurrentMemberImageAction) Start(ctx context.Context) (bool, error) { |
| 56 | + ready, _, err := a.CheckProgress(ctx) |
| 57 | + if err != nil { |
| 58 | + return false, maskAny(err) |
| 59 | + } |
| 60 | + return ready, nil |
| 61 | +} |
| 62 | + |
| 63 | +// CheckProgress checks the progress of the action. |
| 64 | +// Returns true if the action is completely finished, false otherwise. |
| 65 | +func (a *setCurrentMemberImageAction) CheckProgress(ctx context.Context) (bool, bool, error) { |
| 66 | + log := a.log |
| 67 | + |
| 68 | + imageInfo, found := a.actionCtx.GetImageInfo(a.action.Image) |
| 69 | + if !found { |
| 70 | + log.Info().Msgf("Image not found") |
| 71 | + return true, false, nil |
| 72 | + } |
| 73 | + |
| 74 | + if err := a.actionCtx.WithStatusUpdate(func(s *api.DeploymentStatus) bool { |
| 75 | + m, g, found := s.Members.ElementByID(a.action.MemberID) |
| 76 | + if !found { |
| 77 | + log.Error().Msg("No such member") |
| 78 | + return false |
| 79 | + } |
| 80 | + |
| 81 | + m.Image = &imageInfo |
| 82 | + |
| 83 | + if err := s.Members.Update(m, g); err != nil { |
| 84 | + log.Error().Msg("Member update failed") |
| 85 | + return false |
| 86 | + } |
| 87 | + |
| 88 | + return true |
| 89 | + }); err != nil { |
| 90 | + log.Error().Msg("Member failed") |
| 91 | + return true, false, nil |
| 92 | + } |
| 93 | + |
| 94 | + return true, false, nil |
| 95 | +} |
0 commit comments