|
| 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 Adam Janikowski |
| 21 | +// |
| 22 | + |
| 23 | +package v1 |
| 24 | + |
| 25 | +import ( |
| 26 | + "fmt" |
| 27 | + |
| 28 | + "github.com/arangodb/kube-arangodb/pkg/apis/shared" |
| 29 | + sharedv1 "github.com/arangodb/kube-arangodb/pkg/apis/shared/v1" |
| 30 | + |
| 31 | + "github.com/arangodb/kube-arangodb/pkg/util/k8sutil" |
| 32 | + "github.com/pkg/errors" |
| 33 | + core "k8s.io/api/core/v1" |
| 34 | +) |
| 35 | + |
| 36 | +var ( |
| 37 | + restrictedVolumeNames = []string{ |
| 38 | + k8sutil.ArangodVolumeName, |
| 39 | + k8sutil.TlsKeyfileVolumeName, |
| 40 | + k8sutil.RocksdbEncryptionVolumeName, |
| 41 | + k8sutil.ExporterJWTVolumeName, |
| 42 | + k8sutil.ClusterJWTSecretVolumeName, |
| 43 | + "lifecycle", |
| 44 | + } |
| 45 | +) |
| 46 | + |
| 47 | +// IsRestrictedVolumeName check of volume name is restricted, for example for originally mounted volumes |
| 48 | +func IsRestrictedVolumeName(name string) bool { |
| 49 | + for _, restrictedVolumeName := range restrictedVolumeNames { |
| 50 | + if restrictedVolumeName == name { |
| 51 | + return true |
| 52 | + } |
| 53 | + } |
| 54 | + |
| 55 | + return false |
| 56 | +} |
| 57 | + |
| 58 | +// ServerGroupSpecVolumes definition of volume list which need to be mounted to Pod |
| 59 | +type ServerGroupSpecVolumes []ServerGroupSpecVolume |
| 60 | + |
| 61 | +// Validate if ServerGroupSpec volumes are valid and does not collide |
| 62 | +func (s ServerGroupSpecVolumes) Validate() error { |
| 63 | + var validationErrors []error |
| 64 | + |
| 65 | + mappedVolumes := map[string]int{} |
| 66 | + |
| 67 | + for id, volume := range s { |
| 68 | + if i, ok := mappedVolumes[volume.Name]; ok { |
| 69 | + mappedVolumes[volume.Name] = i + 1 |
| 70 | + } else { |
| 71 | + mappedVolumes[volume.Name] = 1 |
| 72 | + } |
| 73 | + |
| 74 | + if err := volume.Validate(); err != nil { |
| 75 | + validationErrors = append(validationErrors, shared.PrefixResourceErrors(fmt.Sprintf("%d", id), err)) |
| 76 | + } |
| 77 | + } |
| 78 | + |
| 79 | + for volumeName, count := range mappedVolumes { |
| 80 | + if IsRestrictedVolumeName(volumeName) { |
| 81 | + validationErrors = append(validationErrors, errors.Errorf("volume with name %s is restricted", volumeName)) |
| 82 | + } |
| 83 | + |
| 84 | + if count == 1 { |
| 85 | + continue |
| 86 | + } |
| 87 | + |
| 88 | + validationErrors = append(validationErrors, errors.Errorf("volume with name %s defined more than once: %d", volumeName, count)) |
| 89 | + } |
| 90 | + |
| 91 | + return shared.WithErrors(validationErrors...) |
| 92 | +} |
| 93 | + |
| 94 | +// Volumes create volumes |
| 95 | +func (s ServerGroupSpecVolumes) Volumes() []core.Volume { |
| 96 | + volumes := make([]core.Volume, len(s)) |
| 97 | + |
| 98 | + for id, volume := range s { |
| 99 | + volumes[id] = volume.Volume() |
| 100 | + } |
| 101 | + |
| 102 | + return volumes |
| 103 | +} |
| 104 | + |
| 105 | +// ServerGroupSpecVolume definition of volume which need to be mounted to Pod |
| 106 | +type ServerGroupSpecVolume struct { |
| 107 | + // Name of volume |
| 108 | + Name string `json:"name"` |
| 109 | + |
| 110 | + // Secret which should be mounted into pod |
| 111 | + Secret *ServerGroupSpecVolumeSecret `json:"secret,omitempty"` |
| 112 | + |
| 113 | + // ConfigMap which should be mounted into pod |
| 114 | + ConfigMap *ServerGroupSpecVolumeConfigMap `json:"configMap,omitempty"` |
| 115 | +} |
| 116 | + |
| 117 | +// Validate if ServerGroupSpec volume is valid |
| 118 | +func (s *ServerGroupSpecVolume) Validate() error { |
| 119 | + if s == nil { |
| 120 | + return nil |
| 121 | + } |
| 122 | + |
| 123 | + return shared.WithErrors( |
| 124 | + shared.PrefixResourceErrors("name", sharedv1.AsKubernetesResourceName(&s.Name).Validate()), |
| 125 | + shared.PrefixResourceErrors("secret", s.Secret.Validate()), |
| 126 | + shared.PrefixResourceErrors("configMap", s.ConfigMap.Validate()), |
| 127 | + s.validate(), |
| 128 | + ) |
| 129 | +} |
| 130 | + |
| 131 | +// Volume create Pod Volume object |
| 132 | +func (s ServerGroupSpecVolume) Volume() core.Volume { |
| 133 | + return core.Volume{ |
| 134 | + Name: s.Name, |
| 135 | + VolumeSource: core.VolumeSource{ |
| 136 | + ConfigMap: (*core.ConfigMapVolumeSource)(s.ConfigMap), |
| 137 | + Secret: (*core.SecretVolumeSource)(s.Secret), |
| 138 | + }, |
| 139 | + } |
| 140 | +} |
| 141 | + |
| 142 | +func (s *ServerGroupSpecVolume) validate() error { |
| 143 | + if s.ConfigMap == nil && s.Secret == nil { |
| 144 | + return errors.Errorf("at least one option need to be defined: secret or configMap") |
| 145 | + } |
| 146 | + |
| 147 | + if s.ConfigMap != nil && s.Secret != nil { |
| 148 | + return errors.Errorf("only one option can be defined: secret or configMap") |
| 149 | + } |
| 150 | + |
| 151 | + return nil |
| 152 | +} |
| 153 | + |
| 154 | +type ServerGroupSpecVolumeSecret core.SecretVolumeSource |
| 155 | + |
| 156 | +func (s *ServerGroupSpecVolumeSecret) Validate() error { |
| 157 | + if s == nil { |
| 158 | + return nil |
| 159 | + } |
| 160 | + |
| 161 | + return shared.WithErrors( |
| 162 | + shared.PrefixResourceError("secretName", sharedv1.AsKubernetesResourceName(&s.SecretName).Validate()), |
| 163 | + ) |
| 164 | +} |
| 165 | + |
| 166 | +type ServerGroupSpecVolumeConfigMap core.ConfigMapVolumeSource |
| 167 | + |
| 168 | +func (s *ServerGroupSpecVolumeConfigMap) Validate() error { |
| 169 | + if s == nil { |
| 170 | + return nil |
| 171 | + } |
| 172 | + |
| 173 | + return shared.WithErrors( |
| 174 | + shared.PrefixResourceError("name", sharedv1.AsKubernetesResourceName(&s.Name).Validate()), |
| 175 | + ) |
| 176 | +} |
0 commit comments