|
| 1 | +// |
| 2 | +// DISCLAIMER |
| 3 | +// |
| 4 | +// Copyright 2018 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 | + "time" |
| 28 | + |
| 29 | + "github.com/arangodb/kube-arangodb/pkg/util/k8sutil" |
| 30 | + core "k8s.io/api/core/v1" |
| 31 | + "k8s.io/apimachinery/pkg/api/errors" |
| 32 | + |
| 33 | + api "github.com/arangodb/kube-arangodb/pkg/apis/deployment/v1" |
| 34 | + "github.com/rs/zerolog" |
| 35 | +) |
| 36 | + |
| 37 | +// NewRotateMemberAction creates a new Action that implements the given |
| 38 | +// planned RotateMember action. |
| 39 | +func NewPVCResizeAction(log zerolog.Logger, action api.Action, actionCtx ActionContext) Action { |
| 40 | + return &actionPVCResize{ |
| 41 | + log: log, |
| 42 | + action: action, |
| 43 | + actionCtx: actionCtx, |
| 44 | + } |
| 45 | +} |
| 46 | + |
| 47 | +// actionRotateMember implements an RotateMember. |
| 48 | +type actionPVCResize struct { |
| 49 | + log zerolog.Logger |
| 50 | + action api.Action |
| 51 | + actionCtx ActionContext |
| 52 | +} |
| 53 | + |
| 54 | +// Start performs the start of the action. |
| 55 | +// Returns true if the action is completely finished, false in case |
| 56 | +// the start time needs to be recorded and a ready condition needs to be checked. |
| 57 | +func (a *actionPVCResize) Start(ctx context.Context) (bool, error) { |
| 58 | + log := a.log |
| 59 | + group := a.action.Group |
| 60 | + groupSpec := a.actionCtx.GetSpec().GetServerGroupSpec(group) |
| 61 | + m, ok := a.actionCtx.GetMemberStatusByID(a.action.MemberID) |
| 62 | + if !ok { |
| 63 | + log.Error().Msg("No such member") |
| 64 | + return true, nil |
| 65 | + } |
| 66 | + |
| 67 | + if m.PersistentVolumeClaimName == "" { |
| 68 | + // Nothing to do, PVC is empty |
| 69 | + return true, nil |
| 70 | + } |
| 71 | + |
| 72 | + pvc, err := a.actionCtx.GetPvc(m.PersistentVolumeClaimName) |
| 73 | + if err != nil { |
| 74 | + if errors.IsNotFound(err) { |
| 75 | + return true, nil |
| 76 | + } |
| 77 | + |
| 78 | + return false, err |
| 79 | + } |
| 80 | + |
| 81 | + var res core.ResourceList |
| 82 | + if groupSpec.HasVolumeClaimTemplate() { |
| 83 | + res = groupSpec.GetVolumeClaimTemplate().Spec.Resources.Requests |
| 84 | + } else { |
| 85 | + res = groupSpec.Resources.Requests |
| 86 | + } |
| 87 | + |
| 88 | + if requestedSize, ok := res[core.ResourceStorage]; ok { |
| 89 | + if volumeSize, ok := pvc.Spec.Resources.Requests[core.ResourceStorage]; ok { |
| 90 | + cmp := volumeSize.Cmp(requestedSize) |
| 91 | + if cmp < 0 { |
| 92 | + pvc.Spec.Resources.Requests[core.ResourceStorage] = requestedSize |
| 93 | + if err := a.actionCtx.UpdatePvc(pvc); err != nil { |
| 94 | + return false, err |
| 95 | + } |
| 96 | + |
| 97 | + return false, nil |
| 98 | + } else if cmp > 0 { |
| 99 | + log.Error().Str("server-group", group.AsRole()).Str("pvc-storage-size", volumeSize.String()).Str("requested-size", requestedSize.String()). |
| 100 | + Msg("Volume size should not shrink") |
| 101 | + a.actionCtx.CreateEvent(k8sutil.NewCannotShrinkVolumeEvent(a.actionCtx.GetAPIObject(), pvc.Name)) |
| 102 | + return false, nil |
| 103 | + } |
| 104 | + } |
| 105 | + } |
| 106 | + |
| 107 | + return true, nil |
| 108 | +} |
| 109 | + |
| 110 | +// CheckProgress checks the progress of the action. |
| 111 | +// Returns: ready, abort, error. |
| 112 | +func (a *actionPVCResize) CheckProgress(ctx context.Context) (bool, bool, error) { |
| 113 | + // Check that pod is removed |
| 114 | + log := a.log |
| 115 | + m, found := a.actionCtx.GetMemberStatusByID(a.action.MemberID) |
| 116 | + if !found { |
| 117 | + log.Error().Msg("No such member") |
| 118 | + return true, false, nil |
| 119 | + } |
| 120 | + |
| 121 | + pvc, err := a.actionCtx.GetPvc(m.PersistentVolumeClaimName) |
| 122 | + if err != nil { |
| 123 | + if errors.IsNotFound(err) { |
| 124 | + return true, false, nil |
| 125 | + } |
| 126 | + |
| 127 | + return false, true, err |
| 128 | + } |
| 129 | + |
| 130 | + pv, err := a.actionCtx.GetPv(pvc.Spec.VolumeName) |
| 131 | + if err != nil { |
| 132 | + if errors.IsNotFound(err) { |
| 133 | + return true, false, nil |
| 134 | + } |
| 135 | + |
| 136 | + return false, true, err |
| 137 | + } |
| 138 | + |
| 139 | + if requestedSize, ok := pvc.Spec.Resources.Requests[core.ResourceStorage]; ok { |
| 140 | + if volumeSize, ok := pv.Spec.Capacity[core.ResourceStorage]; ok { |
| 141 | + cmp := volumeSize.Cmp(requestedSize) |
| 142 | + if cmp >= 0 { |
| 143 | + return true, false, nil |
| 144 | + } |
| 145 | + } |
| 146 | + } |
| 147 | + |
| 148 | + return false, false, nil |
| 149 | +} |
| 150 | + |
| 151 | +// Timeout returns the amount of time after which this action will timeout. |
| 152 | +func (a *actionPVCResize) Timeout() time.Duration { |
| 153 | + return pvcResizeTimeout |
| 154 | +} |
| 155 | + |
| 156 | +// Return the MemberID used / created in this action |
| 157 | +func (a *actionPVCResize) MemberID() string { |
| 158 | + return a.action.MemberID |
| 159 | +} |
0 commit comments