|
| 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 | + |
| 21 | +package v1alpha |
| 22 | + |
| 23 | +import ( |
| 24 | + "fmt" |
| 25 | + |
| 26 | + "github.com/arangodb/kube-arangodb/pkg/util/k8sutil" |
| 27 | +) |
| 28 | + |
| 29 | +const ( |
| 30 | + // UserNameRoot root user name |
| 31 | + UserNameRoot = "root" |
| 32 | +) |
| 33 | + |
| 34 | +// PasswordSecretName contains user password secret name |
| 35 | +type PasswordSecretName string |
| 36 | + |
| 37 | +const ( |
| 38 | + // PasswordSecretNameNone is magic value for no action |
| 39 | + PasswordSecretNameNone PasswordSecretName = "None" |
| 40 | + // PasswordSecretNameAuto is magic value for autogenerate name |
| 41 | + PasswordSecretNameAuto PasswordSecretName = "Auto" |
| 42 | +) |
| 43 | + |
| 44 | +// PasswordSecretNameList is a map from username to secretnames |
| 45 | +type PasswordSecretNameList map[string]PasswordSecretName |
| 46 | + |
| 47 | +// BootstrapSpec contains information for cluster bootstrapping |
| 48 | +type BootstrapSpec struct { |
| 49 | + // PasswordSecretNames contains a map of username to password-secret-name |
| 50 | + PasswordSecretNames PasswordSecretNameList `json:"passwordSecretNames,omitempty"` |
| 51 | +} |
| 52 | + |
| 53 | +// IsNone returns true if p is None or p is empty |
| 54 | +func (p PasswordSecretName) IsNone() bool { |
| 55 | + return p == PasswordSecretNameNone || p == "" |
| 56 | +} |
| 57 | + |
| 58 | +// IsAuto returns true if p is Auto |
| 59 | +func (p PasswordSecretName) IsAuto() bool { |
| 60 | + return p == PasswordSecretNameAuto |
| 61 | +} |
| 62 | + |
| 63 | +// GetSecretName returns the secret name given by the specs. Or None if not set. |
| 64 | +func (s PasswordSecretNameList) GetSecretName(user string) PasswordSecretName { |
| 65 | + if s != nil { |
| 66 | + if secretname, ok := s[user]; ok { |
| 67 | + return secretname |
| 68 | + } |
| 69 | + } |
| 70 | + return PasswordSecretNameNone |
| 71 | +} |
| 72 | + |
| 73 | +// getSecretNameForUserPassword returns the default secret name for the given user |
| 74 | +func getSecretNameForUserPassword(deploymentname, username string) PasswordSecretName { |
| 75 | + return PasswordSecretName(k8sutil.FixupResourceName(deploymentname + "-" + username + "-password")) |
| 76 | +} |
| 77 | + |
| 78 | +// Validate the specification. |
| 79 | +func (b *BootstrapSpec) Validate() error { |
| 80 | + for username, secretname := range b.PasswordSecretNames { |
| 81 | + // Remove this restriction as soon as we can bootstrap databases |
| 82 | + if username != UserNameRoot { |
| 83 | + return fmt.Errorf("only username `root` allowed in passwordSecretNames") |
| 84 | + } |
| 85 | + |
| 86 | + if secretname.IsNone() { |
| 87 | + if username != UserNameRoot { |
| 88 | + return fmt.Errorf("magic value None not allowed for %s", username) |
| 89 | + } |
| 90 | + } else { |
| 91 | + if err := k8sutil.ValidateResourceName(string(secretname)); err != nil { |
| 92 | + return maskAny(err) |
| 93 | + } |
| 94 | + } |
| 95 | + } |
| 96 | + |
| 97 | + return nil |
| 98 | +} |
| 99 | + |
| 100 | +// SetDefaults fills in default values when a field is not specified. |
| 101 | +func (b *BootstrapSpec) SetDefaults(deploymentname string) { |
| 102 | + if b.PasswordSecretNames == nil { |
| 103 | + b.PasswordSecretNames = make(map[string]PasswordSecretName) |
| 104 | + } |
| 105 | + |
| 106 | + // If root is not set init with Auto |
| 107 | + if _, ok := b.PasswordSecretNames[UserNameRoot]; !ok { |
| 108 | + b.PasswordSecretNames[UserNameRoot] = PasswordSecretNameAuto |
| 109 | + } |
| 110 | + |
| 111 | + // Replace Auto with generated secret name |
| 112 | + for user, secretname := range b.PasswordSecretNames { |
| 113 | + if secretname.IsAuto() { |
| 114 | + b.PasswordSecretNames[user] = getSecretNameForUserPassword(deploymentname, user) |
| 115 | + } |
| 116 | + } |
| 117 | +} |
| 118 | + |
| 119 | +// NewPasswordSecretNameListOrNil returns nil if input is nil, otherwise returns a clone of the given value. |
| 120 | +func NewPasswordSecretNameListOrNil(list PasswordSecretNameList) PasswordSecretNameList { |
| 121 | + if list == nil { |
| 122 | + return nil |
| 123 | + } |
| 124 | + var newList = make(PasswordSecretNameList) |
| 125 | + for k, v := range list { |
| 126 | + newList[k] = v |
| 127 | + } |
| 128 | + return newList |
| 129 | +} |
| 130 | + |
| 131 | +// SetDefaultsFrom fills unspecified fields with a value from given source spec. |
| 132 | +func (b *BootstrapSpec) SetDefaultsFrom(source BootstrapSpec) { |
| 133 | + if b.PasswordSecretNames == nil { |
| 134 | + b.PasswordSecretNames = NewPasswordSecretNameListOrNil(source.PasswordSecretNames) |
| 135 | + } |
| 136 | +} |
0 commit comments