|
| 1 | +/* |
| 2 | +Copyright (c) 2021 Nokia. All rights reserved. |
| 3 | +
|
| 4 | +
|
| 5 | +Redistribution and use in source and binary forms, with or without |
| 6 | +modification, are permitted provided that the following conditions are met: |
| 7 | +
|
| 8 | +1. Redistributions of source code must retain the above copyright notice, this |
| 9 | + list of conditions and the following disclaimer. |
| 10 | +
|
| 11 | +2. Redistributions in binary form must reproduce the above copyright notice, |
| 12 | + this list of conditions and the following disclaimer in the documentation |
| 13 | + and/or other materials provided with the distribution. |
| 14 | +
|
| 15 | +3. Neither the name of the copyright holder nor the names of its |
| 16 | + contributors may be used to endorse or promote products derived from |
| 17 | + this software without specific prior written permission. |
| 18 | +
|
| 19 | +THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
| 20 | +AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 21 | +IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
| 22 | +DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE |
| 23 | +FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
| 24 | +DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR |
| 25 | +SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER |
| 26 | +CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, |
| 27 | +OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 28 | +OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 29 | +*/ |
| 30 | + |
| 31 | +package v1 |
| 32 | + |
| 33 | +import ( |
| 34 | + "context" |
| 35 | + "fmt" |
| 36 | + "strings" |
| 37 | + |
| 38 | + corev1 "k8s.io/api/core/v1" |
| 39 | +) |
| 40 | + |
| 41 | +// key value for a combined license file stored in a secret. |
| 42 | +const allSecretKey = "all.key" |
| 43 | + |
| 44 | +// GetConfig gets config from srlinux spec. |
| 45 | +func (s *SrlinuxSpec) GetConfig() *NodeConfig { |
| 46 | + if s.Config != nil { |
| 47 | + return s.Config |
| 48 | + } |
| 49 | + |
| 50 | + return &NodeConfig{} |
| 51 | +} |
| 52 | + |
| 53 | +// GetConstraints gets constraints from srlinux spec, |
| 54 | +// default constraints are returned if none are present in the spec. |
| 55 | +func (s *SrlinuxSpec) GetConstraints() map[string]string { |
| 56 | + if s.Constraints != nil { |
| 57 | + return s.Constraints |
| 58 | + } |
| 59 | + |
| 60 | + return defaultConstraints |
| 61 | +} |
| 62 | + |
| 63 | +// GetModel gets srlinux model (aka variant) from srlinux spec, |
| 64 | +// default srlinux variant is returned if none present in the spec. |
| 65 | +func (s *SrlinuxSpec) GetModel() string { |
| 66 | + if s.Model != "" { |
| 67 | + return s.Model |
| 68 | + } |
| 69 | + |
| 70 | + return defaultSrlinuxVariant |
| 71 | +} |
| 72 | + |
| 73 | +// GetImage returns the srlinux container image name that is used in pod spec |
| 74 | +// if Config.Image is provided it takes precedence over all other option |
| 75 | +// if not, the Spec.Version is used as a tag for public container image ghcr.io/nokia/srlinux. |
| 76 | +func (s *SrlinuxSpec) GetImage() string { |
| 77 | + img := defaultSrLinuxImageName |
| 78 | + |
| 79 | + if s.GetConfig().Image != "" { |
| 80 | + img = s.GetConfig().Image |
| 81 | + } |
| 82 | + |
| 83 | + // when image is not defined, but version is |
| 84 | + // the version is used as a tag for a default image repo |
| 85 | + if s.GetConfig().Image == "" && s.Version != "" { |
| 86 | + img = img + ":" + s.Version |
| 87 | + } |
| 88 | + |
| 89 | + return img |
| 90 | +} |
| 91 | + |
| 92 | +// GetInitImage gets init container image from the srlinux spec, |
| 93 | +// default init container image is returned if none present in the spec. |
| 94 | +func (s *SrlinuxSpec) GetInitImage() string { |
| 95 | + if s.GetConfig().InitImage != "" { |
| 96 | + return s.GetConfig().InitImage |
| 97 | + } |
| 98 | + |
| 99 | + return defaultSrlinuxInitContainerImage |
| 100 | +} |
| 101 | + |
| 102 | +// GetImageVersion finds an srlinux image version by looking at the Image field of the spec |
| 103 | +// as well as at Version field. |
| 104 | +// When Version field is set it is returned. |
| 105 | +// In other cases, Image string is evaluated and it's tag substring is parsed. |
| 106 | +// If no tag is present, or tag is latest, the 0.0 version is assumed to be in use. |
| 107 | +func (s *SrlinuxSpec) GetImageVersion() *SrlVersion { |
| 108 | + if s.Version != "" { |
| 109 | + return parseVersionString(s.Version) |
| 110 | + } |
| 111 | + |
| 112 | + var tag string |
| 113 | + |
| 114 | + split := strings.Split(s.GetImage(), ":") |
| 115 | + if len(split) == 2 { //nolint:gomnd |
| 116 | + tag = split[1] |
| 117 | + } |
| 118 | + |
| 119 | + return parseVersionString(tag) |
| 120 | +} |
| 121 | + |
| 122 | +// InitLicenseKey sets the Srlinux.LicenseKey to a value of a key |
| 123 | +// that matches MAJOR-MINOR.key of a passed secret. |
| 124 | +// Where MAJOR-MINOR is retrieved from the image version. |
| 125 | +// If such key doesn't exist, it checks if a wildcard `all.key` is found in that secret, |
| 126 | +// if nothing found, LicenseKey stays empty, which denotes that no license was found for Srlinux. |
| 127 | +func (s *Srlinux) InitLicenseKey( |
| 128 | + _ context.Context, |
| 129 | + secret *corev1.Secret, |
| 130 | + version *SrlVersion, |
| 131 | +) { |
| 132 | + if secret == nil { |
| 133 | + return |
| 134 | + } |
| 135 | + |
| 136 | + versionSecretKey := fmt.Sprintf("%s-%s.key", version.Major, version.Minor) |
| 137 | + if _, ok := secret.Data[versionSecretKey]; ok { |
| 138 | + s.LicenseKey = versionSecretKey |
| 139 | + |
| 140 | + return |
| 141 | + } |
| 142 | + |
| 143 | + if _, ok := secret.Data[allSecretKey]; ok { |
| 144 | + s.LicenseKey = allSecretKey |
| 145 | + |
| 146 | + return |
| 147 | + } |
| 148 | +} |
0 commit comments