Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: dont error out on version parse if we fail, just skip dealing w/ license secret #40

Merged
merged 2 commits into from
Feb 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@
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

Check warning on line 49 in controllers/secret.go

View check run for this annotation

Codecov / codecov/patch

controllers/secret.go#L49

Added line #L49 was not covered by tests
}

func (r *SrlinuxReconciler) addOrUpdateLicenseSecret(
Expand Down
Loading