|
| 1 | +/* |
| 2 | +Copyright 2019 the Velero contributors. |
| 3 | +
|
| 4 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +you may not use this file except in compliance with the License. |
| 6 | +You may obtain a copy of the License at |
| 7 | +
|
| 8 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +
|
| 10 | +Unless required by applicable law or agreed to in writing, software |
| 11 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +See the License for the specific language governing permissions and |
| 14 | +limitations under the License. |
| 15 | +*/ |
| 16 | + |
| 17 | +package cloudprovider |
| 18 | + |
| 19 | +import ( |
| 20 | + "github.com/pkg/errors" |
| 21 | + "k8s.io/apimachinery/pkg/util/sets" |
| 22 | +) |
| 23 | + |
| 24 | +// ValidateObjectStoreConfigKeys ensures that an object store's config |
| 25 | +// is valid by making sure each `config` key is in the `validKeys` list. |
| 26 | +// The special key "bucket" is always considered valid. |
| 27 | +func ValidateObjectStoreConfigKeys(config map[string]string, validKeys ...string) error { |
| 28 | + // `bucket` is automatically added to all object store config by |
| 29 | + // velero, so add it as a valid key. |
| 30 | + return validateConfigKeys(config, append(validKeys, "bucket")...) |
| 31 | +} |
| 32 | + |
| 33 | +// ValidateVolumeSnapshotterConfigKeys ensures that a volume snapshotter's |
| 34 | +// config is valid by making sure each `config` key is in the `validKeys` list. |
| 35 | +func ValidateVolumeSnapshotterConfigKeys(config map[string]string, validKeys ...string) error { |
| 36 | + return validateConfigKeys(config, validKeys...) |
| 37 | +} |
| 38 | + |
| 39 | +func validateConfigKeys(config map[string]string, validKeys ...string) error { |
| 40 | + validKeysSet := sets.NewString(validKeys...) |
| 41 | + |
| 42 | + var invalidKeys []string |
| 43 | + for k := range config { |
| 44 | + if !validKeysSet.Has(k) { |
| 45 | + invalidKeys = append(invalidKeys, k) |
| 46 | + } |
| 47 | + } |
| 48 | + |
| 49 | + if len(invalidKeys) > 0 { |
| 50 | + return errors.Errorf("config has invalid keys %v; valid keys are %v", invalidKeys, validKeys) |
| 51 | + } |
| 52 | + |
| 53 | + return nil |
| 54 | +} |
0 commit comments