|
| 1 | +// |
| 2 | +// DISCLAIMER |
| 3 | +// |
| 4 | +// Copyright 2016-2022 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 | + api "github.com/arangodb/kube-arangodb/pkg/apis/deployment/v1" |
| 27 | + "github.com/arangodb/kube-arangodb/pkg/util/k8sutil" |
| 28 | + inspectorInterface "github.com/arangodb/kube-arangodb/pkg/util/k8sutil/inspector" |
| 29 | + "github.com/rs/zerolog" |
| 30 | +) |
| 31 | + |
| 32 | +func createBackupInProgressConditionPlan(ctx context.Context, |
| 33 | + log zerolog.Logger, apiObject k8sutil.APIObject, |
| 34 | + spec api.DeploymentSpec, status api.DeploymentStatus, |
| 35 | + cachedStatus inspectorInterface.Inspector, context PlanBuilderContext) api.Plan { |
| 36 | + |
| 37 | + if spec.Mode.Get() != api.DeploymentModeCluster { |
| 38 | + return nil |
| 39 | + } |
| 40 | + |
| 41 | + cache, ok := context.GetAgencyCache() |
| 42 | + if !ok { |
| 43 | + return nil |
| 44 | + } |
| 45 | + |
| 46 | + currentCondition, currentConditionExists := status.Conditions.Get(api.ConditionTypeBackupInProgress) |
| 47 | + |
| 48 | + backupInProgress := cache.Target.HotBackup.Create |
| 49 | + |
| 50 | + if currentConditionExists { |
| 51 | + // Condition exists |
| 52 | + if !backupInProgress.Exists() { |
| 53 | + // Condition needs to be removed |
| 54 | + return api.Plan{ |
| 55 | + removeConditionActionV2("Backup not in progress", api.ConditionTypeBackupInProgress), |
| 56 | + } |
| 57 | + } |
| 58 | + |
| 59 | + // Backup is in progress |
| 60 | + |
| 61 | + hash := backupInProgress.Hash() |
| 62 | + |
| 63 | + if !currentCondition.IsTrue() || currentCondition.Hash != hash { |
| 64 | + return api.Plan{ |
| 65 | + updateConditionActionV2("Backup in progress", api.ConditionTypeBackupInProgress, true, "Backup In Progress", "", hash), |
| 66 | + } |
| 67 | + } |
| 68 | + |
| 69 | + return nil |
| 70 | + } else { |
| 71 | + if backupInProgress.Exists() { |
| 72 | + return api.Plan{ |
| 73 | + updateConditionActionV2("Backup in progress", api.ConditionTypeBackupInProgress, true, "Backup In Progress", "", backupInProgress.Hash()), |
| 74 | + } |
| 75 | + } |
| 76 | + |
| 77 | + return nil |
| 78 | + } |
| 79 | +} |
| 80 | + |
| 81 | +func createMaintenanceConditionPlan(ctx context.Context, |
| 82 | + log zerolog.Logger, apiObject k8sutil.APIObject, |
| 83 | + spec api.DeploymentSpec, status api.DeploymentStatus, |
| 84 | + cachedStatus inspectorInterface.Inspector, context PlanBuilderContext) api.Plan { |
| 85 | + |
| 86 | + if spec.Mode.Get() != api.DeploymentModeCluster { |
| 87 | + return nil |
| 88 | + } |
| 89 | + |
| 90 | + cache, ok := context.GetAgencyCache() |
| 91 | + if !ok { |
| 92 | + return nil |
| 93 | + } |
| 94 | + |
| 95 | + currentCondition, currentConditionExists := status.Conditions.Get(api.ConditionTypeMaintenance) |
| 96 | + |
| 97 | + backupInProgress := cache.Target.HotBackup.Create |
| 98 | + |
| 99 | + if currentConditionExists { |
| 100 | + // Condition exists |
| 101 | + if !backupInProgress.Exists() { |
| 102 | + // Condition needs to be removed |
| 103 | + return api.Plan{ |
| 104 | + removeConditionActionV2("Backup not in progress", api.ConditionTypeMaintenance), |
| 105 | + } |
| 106 | + } |
| 107 | + |
| 108 | + // Backup is in progress |
| 109 | + |
| 110 | + hash := backupInProgress.Hash() |
| 111 | + |
| 112 | + if !currentCondition.IsTrue() || currentCondition.Hash != hash { |
| 113 | + return api.Plan{ |
| 114 | + updateConditionActionV2("Backup in progress", api.ConditionTypeMaintenance, true, "Backup In Progress", "", hash), |
| 115 | + } |
| 116 | + } |
| 117 | + |
| 118 | + return nil |
| 119 | + } else { |
| 120 | + if backupInProgress.Exists() { |
| 121 | + return api.Plan{ |
| 122 | + updateConditionActionV2("Backup in progress", api.ConditionTypeMaintenance, true, "Backup In Progress", "", backupInProgress.Hash()), |
| 123 | + } |
| 124 | + } |
| 125 | + |
| 126 | + return nil |
| 127 | + } |
| 128 | +} |
0 commit comments