Skip to content

Commit e41b06c

Browse files
committed
move api helper functions to _helpers
1 parent c0a59db commit e41b06c

File tree

4 files changed

+142
-109
lines changed

4 files changed

+142
-109
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ Available statuses:
8484

8585
- `STATUS`: `Running` when the underlying pod is running. The status is copied from the pod status.
8686
- `READY`: `true` when the SR Linux node is ready to accept configuration. The status is `true` when SR Linux management servers is ready to accept connections and configurations.
87-
- `CONFIG`: `loaded` when the startup-configuration is succesfully applied. The status is `failed` when errors occured during startup-configuration load.
87+
- `CONFIG`: `loaded` when the startup-configuration is successfully applied. The status is `failed` when errors occurred during startup-configuration load.
8888

8989
The services will be exposed via MetalLB and can be queried as:
9090

api/v1/srl_version.go

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ package v1
66

77
import (
88
"regexp"
9+
"slices"
910
"strings"
1011
)
1112

@@ -21,10 +22,8 @@ type SrlVersion struct {
2122
func parseVersionString(s string) *SrlVersion {
2223
// Check if the version string is an engineering build with major = 0
2324
engineeringVersions := []string{"", "latest", "ga"}
24-
for _, ver := range engineeringVersions {
25-
if ver == strings.ToLower(s) {
26-
return &SrlVersion{"0", "", "", "", ""}
27-
}
25+
if slices.Contains(engineeringVersions, strings.ToLower(s)) {
26+
return &SrlVersion{"0", "", "", "", ""}
2827
}
2928

3029
// https://regex101.com/r/eWS6Ms/3

api/v1/srlinux_helpers.go

Lines changed: 138 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,138 @@
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+
// GetImageVersion finds an srlinux image version by looking at the Image field of the spec
93+
// as well as at Version field.
94+
// When Version field is set it is returned.
95+
// In other cases, Image string is evaluated and it's tag substring is parsed.
96+
// If no tag is present, or tag is latest, the 0.0 version is assumed to be in use.
97+
func (s *SrlinuxSpec) GetImageVersion() *SrlVersion {
98+
if s.Version != "" {
99+
return parseVersionString(s.Version)
100+
}
101+
102+
var tag string
103+
104+
split := strings.Split(s.GetImage(), ":")
105+
if len(split) == 2 { //nolint:gomnd
106+
tag = split[1]
107+
}
108+
109+
return parseVersionString(tag)
110+
}
111+
112+
// InitLicenseKey sets the Srlinux.LicenseKey to a value of a key
113+
// that matches MAJOR-MINOR.key of a passed secret.
114+
// Where MAJOR-MINOR is retrieved from the image version.
115+
// If such key doesn't exist, it checks if a wildcard `all.key` is found in that secret,
116+
// if nothing found, LicenseKey stays empty, which denotes that no license was found for Srlinux.
117+
func (s *Srlinux) InitLicenseKey(
118+
_ context.Context,
119+
secret *corev1.Secret,
120+
version *SrlVersion,
121+
) {
122+
if secret == nil {
123+
return
124+
}
125+
126+
versionSecretKey := fmt.Sprintf("%s-%s.key", version.Major, version.Minor)
127+
if _, ok := secret.Data[versionSecretKey]; ok {
128+
s.LicenseKey = versionSecretKey
129+
130+
return
131+
}
132+
133+
if _, ok := secret.Data[allSecretKey]; ok {
134+
s.LicenseKey = allSecretKey
135+
136+
return
137+
}
138+
}

api/v1/srlinux_types.go

Lines changed: 0 additions & 104 deletions
Original file line numberDiff line numberDiff line change
@@ -31,17 +31,9 @@ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
3131
package v1
3232

3333
import (
34-
"context"
35-
"fmt"
36-
"strings"
37-
38-
corev1 "k8s.io/api/core/v1"
3934
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
4035
)
4136

42-
// key value for a combined license file stored in a secret.
43-
const allSecretKey = "all.key"
44-
4537
// SrlinuxSpec defines the desired state of Srlinux.
4638
type SrlinuxSpec struct {
4739
Config *NodeConfig `json:"config,omitempty"`
@@ -105,99 +97,3 @@ type SrlinuxList struct {
10597
func init() {
10698
SchemeBuilder.Register(&Srlinux{}, &SrlinuxList{})
10799
}
108-
109-
// GetConfig gets config from srlinux spec.
110-
func (s *SrlinuxSpec) GetConfig() *NodeConfig {
111-
if s.Config != nil {
112-
return s.Config
113-
}
114-
115-
return &NodeConfig{}
116-
}
117-
118-
// GetConstraints gets constraints from srlinux spec,
119-
// default constraints are returned if none are present in the spec.
120-
func (s *SrlinuxSpec) GetConstraints() map[string]string {
121-
if s.Constraints != nil {
122-
return s.Constraints
123-
}
124-
125-
return defaultConstraints
126-
}
127-
128-
// GetModel gets srlinux model (aka variant) from srlinux spec,
129-
// default srlinux variant is returned if none present in the spec.
130-
func (s *SrlinuxSpec) GetModel() string {
131-
if s.Model != "" {
132-
return s.Model
133-
}
134-
135-
return defaultSrlinuxVariant
136-
}
137-
138-
// GetImage returns the srlinux container image name that is used in pod spec
139-
// if Config.Image is provided it takes precedence over all other option
140-
// if not, the Spec.Version is used as a tag for public container image ghcr.io/nokia/srlinux.
141-
func (s *SrlinuxSpec) GetImage() string {
142-
img := defaultSRLinuxImageName
143-
144-
if s.GetConfig().Image != "" {
145-
img = s.GetConfig().Image
146-
}
147-
148-
// when image is not defined, but version is
149-
// the version is used as a tag for a default image repo
150-
if s.GetConfig().Image == "" && s.Version != "" {
151-
img = img + ":" + s.Version
152-
}
153-
154-
return img
155-
}
156-
157-
// GetImageVersion finds an srlinux image version by looking at the Image field of the spec
158-
// as well as at Version field.
159-
// When Version field is set it is returned.
160-
// In other cases, Image string is evaluated and it's tag substring is parsed.
161-
// If no tag is present, or tag is latest, the 0.0 version is assumed to be in use.
162-
func (s *SrlinuxSpec) GetImageVersion() *SrlVersion {
163-
if s.Version != "" {
164-
return parseVersionString(s.Version)
165-
}
166-
167-
var tag string
168-
169-
split := strings.Split(s.GetImage(), ":")
170-
if len(split) == 2 { //nolint:gomnd
171-
tag = split[1]
172-
}
173-
174-
return parseVersionString(tag)
175-
}
176-
177-
// InitLicenseKey sets the Srlinux.LicenseKey to a value of a key
178-
// that matches MAJOR-MINOR.key of a passed secret.
179-
// Where MAJOR-MINOR is retrieved from the image version.
180-
// If such key doesn't exist, it checks if a wildcard `all.key` is found in that secret,
181-
// if nothing found, LicenseKey stays empty, which denotes that no license was found for Srlinux.
182-
func (s *Srlinux) InitLicenseKey(
183-
_ context.Context,
184-
secret *corev1.Secret,
185-
version *SrlVersion,
186-
) {
187-
if secret == nil {
188-
return
189-
}
190-
191-
versionSecretKey := fmt.Sprintf("%s-%s.key", version.Major, version.Minor)
192-
if _, ok := secret.Data[versionSecretKey]; ok {
193-
s.LicenseKey = versionSecretKey
194-
195-
return
196-
}
197-
198-
if _, ok := secret.Data[allSecretKey]; ok {
199-
s.LicenseKey = allSecretKey
200-
201-
return
202-
}
203-
}

0 commit comments

Comments
 (0)