|
| 1 | +// |
| 2 | +// DISCLAIMER |
| 3 | +// |
| 4 | +// Copyright 2023 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 | + |
| 21 | +package reconcile |
| 22 | + |
| 23 | +import ( |
| 24 | + "context" |
| 25 | + |
| 26 | + core "k8s.io/api/core/v1" |
| 27 | + |
| 28 | + api "github.com/arangodb/kube-arangodb/pkg/apis/deployment/v1" |
| 29 | + "github.com/arangodb/kube-arangodb/pkg/deployment/reconcile/shared" |
| 30 | + "github.com/arangodb/kube-arangodb/pkg/util/k8sutil" |
| 31 | + inspectorInterface "github.com/arangodb/kube-arangodb/pkg/util/k8sutil/inspector" |
| 32 | +) |
| 33 | + |
| 34 | +// updateMemberPhasePlan creates plan to update member phase |
| 35 | +func (r *Reconciler) updateMemberConditionTypeMemberVolumeUnschedulableCondition(ctx context.Context, apiObject k8sutil.APIObject, |
| 36 | + spec api.DeploymentSpec, status api.DeploymentStatus, |
| 37 | + context PlanBuilderContext) api.Plan { |
| 38 | + var plan api.Plan |
| 39 | + |
| 40 | + cache := context.ACS().CurrentClusterCache() |
| 41 | + |
| 42 | + volumeClient, err := cache.PersistentVolume().V1() |
| 43 | + if err != nil { |
| 44 | + // We cant fetch volumes, continue |
| 45 | + return nil |
| 46 | + } |
| 47 | + |
| 48 | + for _, e := range status.Members.AsList() { |
| 49 | + if pvcStatus := e.Member.PersistentVolumeClaim; pvcStatus != nil { |
| 50 | + if pvc, ok := context.ACS().CurrentClusterCache().PersistentVolumeClaim().V1().GetSimple(pvcStatus.GetName()); ok { |
| 51 | + if volumeName := pvc.Spec.VolumeName; volumeName != "" { |
| 52 | + if pv, ok := volumeClient.GetSimple(volumeName); ok { |
| 53 | + // We have volume and volumeclaim, lets calculate condition |
| 54 | + unschedulable := memberConditionTypeMemberVolumeUnschedulableCalculate(cache, pv, pvc) |
| 55 | + |
| 56 | + if unschedulable == e.Member.Conditions.IsTrue(api.ConditionTypeMemberVolumeUnschedulable) { |
| 57 | + continue |
| 58 | + } else if unschedulable && !e.Member.Conditions.IsTrue(api.ConditionTypeMemberVolumeUnschedulable) { |
| 59 | + plan = append(plan, shared.UpdateMemberConditionActionV2("PV Unschedulable", api.ConditionTypeMemberVolumeUnschedulable, e.Group, e.Member.ID, true, |
| 60 | + "PV Unschedulable", "PV Unschedulable", "")) |
| 61 | + } else if !unschedulable && e.Member.Conditions.IsTrue(api.ConditionTypeMemberVolumeUnschedulable) { |
| 62 | + plan = append(plan, shared.RemoveMemberConditionActionV2("PV Schedulable", api.ConditionTypeMemberVolumeUnschedulable, e.Group, e.Member.ID)) |
| 63 | + } |
| 64 | + |
| 65 | + } |
| 66 | + } |
| 67 | + } |
| 68 | + } |
| 69 | + } |
| 70 | + |
| 71 | + return plan |
| 72 | +} |
| 73 | + |
| 74 | +type memberConditionTypeMemberVolumeUnschedulableCalculateFunc func(cache inspectorInterface.Inspector, pv *core.PersistentVolume, pvc *core.PersistentVolumeClaim) bool |
| 75 | + |
| 76 | +func memberConditionTypeMemberVolumeUnschedulableCalculate(cache inspectorInterface.Inspector, pv *core.PersistentVolume, pvc *core.PersistentVolumeClaim, funcs ...memberConditionTypeMemberVolumeUnschedulableCalculateFunc) bool { |
| 77 | + for _, f := range funcs { |
| 78 | + if f(cache, pv, pvc) { |
| 79 | + return true |
| 80 | + } |
| 81 | + } |
| 82 | + |
| 83 | + return false |
| 84 | +} |
0 commit comments