Skip to content

Commit

Permalink
Merge pull request #40 from srl-labs/feat/ignore-license-if-version-p…
Browse files Browse the repository at this point in the history
…arse-fails

feat: dont error out on version parse if we fail, just skip dealing w/ license secret
  • Loading branch information
carlmontanari authored Feb 2, 2024
2 parents 1844b46 + 1cd1e85 commit fad8b54
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 29 deletions.
12 changes: 4 additions & 8 deletions api/v1/srl_version.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,10 @@
package v1

import (
"errors"
"regexp"
"strings"
)

// ErrVersionParse is an error which is raised when srlinux version is failed to parse.
var ErrVersionParse = errors.New("version parsing failed")

// SrlVersion represents an sr linux version as a set of fields.
type SrlVersion struct {
Major string `json:"major,omitempty"`
Expand All @@ -22,12 +18,12 @@ type SrlVersion struct {
Commit string `json:"commit,omitempty"`
}

func parseVersionString(s string) (*SrlVersion, error) {
func parseVersionString(s string) *SrlVersion {
// Check if the version string is an engineering build with major = 0
engineeringVersions := []string{"", "latest", "ga"}
for _, ver := range engineeringVersions {
if ver == strings.ToLower(s) {
return &SrlVersion{"0", "", "", "", ""}, nil
return &SrlVersion{"0", "", "", "", ""}
}
}

Expand All @@ -38,8 +34,8 @@ func parseVersionString(s string) (*SrlVersion, error) {

v := re.FindStringSubmatch(s)
if v == nil {
return nil, ErrVersionParse
return &SrlVersion{"0", "", "", "", ""}
}

return &SrlVersion{v[1], v[2], v[3], v[4], v[5]}, nil
return &SrlVersion{v[1], v[2], v[3], v[4], v[5]}
}
10 changes: 2 additions & 8 deletions api/v1/srl_version_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
package v1

import (
"errors"
"testing"

"github.com/google/go-cmp/cmp"
Expand All @@ -16,7 +15,6 @@ func TestParseVersionString(t *testing.T) {
desc string
got string
want *SrlVersion
err error
}{
{
desc: "maj, minor, patch",
Expand Down Expand Up @@ -86,17 +84,13 @@ func TestParseVersionString(t *testing.T) {
{
desc: "invalid1",
got: "abcd",
want: nil,
err: ErrVersionParse,
want: &SrlVersion{"0", "", "", "", ""},
},
}

for _, tt := range tests {
t.Run(tt.desc, func(t *testing.T) {
ver, err := parseVersionString(tt.got)
if !errors.Is(err, tt.err) {
t.Fatalf("got error '%v' but expected '%v'", err, tt.err)
}
ver := parseVersionString(tt.got)

if !cmp.Equal(ver, tt.want) {
t.Fatalf(
Expand Down
2 changes: 1 addition & 1 deletion api/v1/srlinux_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ func (s *SrlinuxSpec) GetImage() string {
// When Version field is set it is returned.
// In other cases, Image string is evaluated and it's tag substring is parsed.
// If no tag is present, or tag is latest, the 0.0 version is assumed to be in use.
func (s *SrlinuxSpec) GetImageVersion() (*SrlVersion, error) {
func (s *SrlinuxSpec) GetImageVersion() *SrlVersion {
if s.Version != "" {
return parseVersionString(s.Version)
}
Expand Down
11 changes: 3 additions & 8 deletions api/v1/srlinux_types_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ package v1

import (
"context"
"errors"
"testing"

"github.com/google/go-cmp/cmp"
Expand Down Expand Up @@ -98,7 +97,7 @@ func TestGetImageVersion(t *testing.T) {
Version: "abc",
Config: &NodeConfig{Image: "ghcr.io/nokia/srlinux:somever"},
},
err: ErrVersionParse,
want: &SrlVersion{"0", "", "", "", ""},
},
{
desc: "version is not present, valid image tag is given",
Expand All @@ -112,17 +111,13 @@ func TestGetImageVersion(t *testing.T) {
spec: &SrlinuxSpec{
Config: &NodeConfig{Image: "ghcr.io/nokia/srlinux:somesrl"},
},
err: ErrVersionParse,
want: &SrlVersion{"0", "", "", "", ""},
},
}

for _, tt := range tests {
t.Run(tt.desc, func(t *testing.T) {
v, err := tt.spec.GetImageVersion()

if !errors.Is(err, tt.err) {
t.Fatalf("got error '%v' but expected '%v'", err, tt.err)
}
v := tt.spec.GetImageVersion()

if !cmp.Equal(v, tt.want) {
t.Fatalf(
Expand Down
13 changes: 9 additions & 4 deletions controllers/secret.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,17 +31,22 @@ func (r *SrlinuxReconciler) createSecrets(
return err
}

v, err := s.Spec.GetImageVersion()
if err != nil {
return err
v := s.Spec.GetImageVersion()

if v.Major == "0" {
log.Info(
"SR Linux image version could not be parsed, will continue without handling license",
)

return nil
}

log.Info("SR Linux image version parsed", "version", v)

// set license key matching image version
s.InitLicenseKey(ctx, secret, v)

return err
return nil
}

func (r *SrlinuxReconciler) addOrUpdateLicenseSecret(
Expand Down

0 comments on commit fad8b54

Please sign in to comment.