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

refactored version detection for license and lic update procedure #14

Merged
merged 2 commits into from
Aug 2, 2022
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
38 changes: 21 additions & 17 deletions api/clientset/v1alpha1/srlinux.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,21 +25,25 @@ var ErrUpdateFailed = errors.New("operation update failed")

// SrlinuxInterface provides access to the Srlinux CRD.
type SrlinuxInterface interface {
List(ctx context.Context, opts metav1.ListOptions) (*typesv1alpha1.SrlinuxList, error)
Get(ctx context.Context, name string, options metav1.GetOptions) (*typesv1alpha1.Srlinux, error)
List(ctx context.Context, opts *metav1.ListOptions) (*typesv1alpha1.SrlinuxList, error)
Get(
ctx context.Context,
name string,
options *metav1.GetOptions,
) (*typesv1alpha1.Srlinux, error)
Create(ctx context.Context, srlinux *typesv1alpha1.Srlinux) (*typesv1alpha1.Srlinux, error)
Delete(ctx context.Context, name string, opts metav1.DeleteOptions) error
Watch(ctx context.Context, opts metav1.ListOptions) (watch.Interface, error)
Delete(ctx context.Context, name string, opts *metav1.DeleteOptions) error
Watch(ctx context.Context, opts *metav1.ListOptions) (watch.Interface, error)
Unstructured(
ctx context.Context,
name string,
opts metav1.GetOptions,
opts *metav1.GetOptions,
subresources ...string,
) (*unstructured.Unstructured, error)
Update(
ctx context.Context,
obj *unstructured.Unstructured,
opts metav1.UpdateOptions,
opts *metav1.UpdateOptions,
) (*typesv1alpha1.Srlinux, error)
}

Expand Down Expand Up @@ -104,14 +108,14 @@ type srlinuxClient struct {
// List gets a list of SRLinux resources.
func (s *srlinuxClient) List(
ctx context.Context,
opts metav1.ListOptions,
opts *metav1.ListOptions,
) (*typesv1alpha1.SrlinuxList, error) {
result := typesv1alpha1.SrlinuxList{}
err := s.restClient.
Get().
Namespace(s.ns).
Resource(gvr.Resource).
VersionedParams(&opts, scheme.ParameterCodec).
VersionedParams(opts, scheme.ParameterCodec).
Do(ctx).
Into(&result)

Expand All @@ -122,15 +126,15 @@ func (s *srlinuxClient) List(
func (s *srlinuxClient) Get(
ctx context.Context,
name string,
opts metav1.GetOptions,
opts *metav1.GetOptions,
) (*typesv1alpha1.Srlinux, error) {
result := typesv1alpha1.Srlinux{}
err := s.restClient.
Get().
Namespace(s.ns).
Resource(gvr.Resource).
Name(name).
VersionedParams(&opts, scheme.ParameterCodec).
VersionedParams(opts, scheme.ParameterCodec).
Do(ctx).
Into(&result)

Expand All @@ -156,24 +160,24 @@ func (s *srlinuxClient) Create(

func (s *srlinuxClient) Watch(
ctx context.Context,
opts metav1.ListOptions,
opts *metav1.ListOptions,
) (watch.Interface, error) {
opts.Watch = true

return s.restClient.
Get().
Namespace(s.ns).
Resource(gvr.Resource).
VersionedParams(&opts, scheme.ParameterCodec).
VersionedParams(opts, scheme.ParameterCodec).
Watch(ctx)
}

func (s *srlinuxClient) Delete(ctx context.Context, name string, opts metav1.DeleteOptions) error {
func (s *srlinuxClient) Delete(ctx context.Context, name string, opts *metav1.DeleteOptions) error {
return s.restClient.
Delete().
Namespace(s.ns).
Resource(gvr.Resource).
VersionedParams(&opts, scheme.ParameterCodec).
VersionedParams(opts, scheme.ParameterCodec).
Name(name).
Do(ctx).
Error()
Expand All @@ -182,7 +186,7 @@ func (s *srlinuxClient) Delete(ctx context.Context, name string, opts metav1.Del
func (s *srlinuxClient) Update(
ctx context.Context,
obj *unstructured.Unstructured,
opts metav1.UpdateOptions,
_ *metav1.UpdateOptions,
) (*typesv1alpha1.Srlinux, error) {
result := typesv1alpha1.Srlinux{}

Expand All @@ -202,10 +206,10 @@ func (s *srlinuxClient) Update(
func (s *srlinuxClient) Unstructured(
ctx context.Context,
name string,
opts metav1.GetOptions,
opts *metav1.GetOptions,
subresources ...string,
) (*unstructured.Unstructured, error) {
return s.dInterface.Namespace(s.ns).Get(ctx, name, opts, subresources...)
return s.dInterface.Namespace(s.ns).Get(ctx, name, *opts, subresources...)
}

func init() {
Expand Down
5 changes: 2 additions & 3 deletions api/types/v1alpha1/srlinux_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,6 @@ type Srlinux struct {
Spec SrlinuxSpec `json:"spec,omitempty"`
Status SrlinuxStatus `json:"status,omitempty"`

// parsed NOS version
NOSVersion *SrlVersion `json:"nos-version,omitempty"`
// license key from license secret that contains a license file for this Srlinux
LicenseKey string `json:"license_key,omitempty"`
}
Expand Down Expand Up @@ -154,12 +152,13 @@ func (s *SrlinuxSpec) GetImageVersion() (*SrlVersion, error) {
func (s *Srlinux) InitLicenseKey(
_ context.Context,
secret *corev1.Secret,
version *SrlVersion,
) {
if secret == nil {
return
}

versionSecretKey := fmt.Sprintf("%s-%s.key", s.NOSVersion.Major, s.NOSVersion.Minor)
versionSecretKey := fmt.Sprintf("%s-%s.key", version.Major, version.Minor)
if _, ok := secret.Data[versionSecretKey]; ok {
s.LicenseKey = versionSecretKey

Expand Down
37 changes: 16 additions & 21 deletions api/types/v1alpha1/srlinux_types_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -135,16 +135,14 @@ func TestGetImageVersion(t *testing.T) {

func TestInitVersion(t *testing.T) {
tests := []struct {
desc string
srl *Srlinux
secret *corev1.Secret
want string
desc string
version *SrlVersion
secret *corev1.Secret
want string
}{
{
desc: "secret key matches srl version",
srl: &Srlinux{
NOSVersion: &SrlVersion{"22", "3", "", "", ""},
},
desc: "secret key matches srl version",
version: &SrlVersion{"22", "3", "", "", ""},
secret: &corev1.Secret{
Data: map[string][]byte{
"22-3.key": nil,
Expand All @@ -154,10 +152,8 @@ func TestInitVersion(t *testing.T) {
want: "22-3.key",
},
{
desc: "wildcard secret key matches srl version",
srl: &Srlinux{
NOSVersion: &SrlVersion{"22", "3", "", "", ""},
},
desc: "wildcard secret key matches srl version",
version: &SrlVersion{"22", "3", "", "", ""},
secret: &corev1.Secret{
Data: map[string][]byte{
"22-6.key": nil,
Expand All @@ -167,24 +163,23 @@ func TestInitVersion(t *testing.T) {
want: "all.key",
},
{
desc: "secret does not exist",
srl: &Srlinux{
NOSVersion: &SrlVersion{"22", "3", "", "", ""},
},
secret: nil,
want: "",
desc: "secret does not exist",
version: &SrlVersion{"22", "3", "", "", ""},
secret: nil,
want: "",
},
}

for _, tt := range tests {
t.Run(tt.desc, func(t *testing.T) {
tt.srl.InitLicenseKey(context.TODO(), tt.secret)
srl := &Srlinux{}
srl.InitLicenseKey(context.TODO(), tt.secret, tt.version)

if !cmp.Equal(tt.srl.LicenseKey, tt.want) {
if !cmp.Equal(srl.LicenseKey, tt.want) {
t.Fatalf(
"%s: actual and expected inputs do not match\nactual: %+v\nexpected:%+v",
tt.desc,
tt.srl.LicenseKey,
srl.LicenseKey,
tt.want,
)
}
Expand Down
5 changes: 0 additions & 5 deletions api/types/v1alpha1/zz_generated.deepcopy.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

14 changes: 0 additions & 14 deletions config/crd/bases/kne.srlinux.dev_srlinuxes.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -44,20 +44,6 @@ spec:
type: string
metadata:
type: object
nos-version:
description: parsed NOS version
properties:
build:
type: string
commit:
type: string
major:
type: string
minor:
type: string
patch:
type: string
type: object
spec:
description: SrlinuxSpec defines the desired state of Srlinux.
properties:
Expand Down
2 changes: 1 addition & 1 deletion config/manager/kustomization.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,4 @@ kind: Kustomization
images:
- name: controller
newName: ghcr.io/srl-labs/srl-controller
newTag: 0.4.0
newTag: 0.4.2
Loading