-
Notifications
You must be signed in to change notification settings - Fork 2.1k
Check for auth version incompatibility (kube) #35025
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
Closed
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -22,6 +22,7 @@ import ( | |
|
|
||
| "github.com/distribution/reference" | ||
| "github.com/gravitational/trace" | ||
| "golang.org/x/mod/semver" | ||
| "sigs.k8s.io/controller-runtime/pkg/client" | ||
| ctrllog "sigs.k8s.io/controller-runtime/pkg/log" | ||
|
|
||
|
|
@@ -35,6 +36,7 @@ type VersionUpdater struct { | |
| imageValidators img.Validators | ||
| maintenanceTriggers maintenance.Triggers | ||
| baseImage reference.Named | ||
| authVersionGetter version.AuthVersionGetter | ||
| } | ||
|
|
||
| // GetVersion does all the version update logic: checking if a maintenance is allowed, | ||
|
|
@@ -64,6 +66,18 @@ func (r *VersionUpdater) GetVersion(ctx context.Context, obj client.Object, curr | |
| return nil, &NoNewVersionError{CurrentVersion: currentVersion, NextVersion: nextVersion} | ||
| } | ||
|
|
||
| log.Info("Getting auth server version") | ||
| authVersion, err := r.authVersionGetter.Get(ctx, obj) | ||
| if err != nil { | ||
| return nil, trace.Wrap(err) | ||
| } | ||
|
|
||
| log.Info("Verify new version candidate is compatible with the auth server version") | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same here: adding the versions to the log |
||
| // The auth server is incompatible with clients of a newer major version | ||
| if semver.Compare(semver.Major(nextVersion), semver.Major(authVersion)) > 0 { | ||
| return nil, &IncompatibleVersionError{AuthVersion: authVersion, NextVersion: nextVersion} | ||
| } | ||
|
|
||
| log.Info("Version change is valid, building img candidate") | ||
| // We tag our img candidate with the version | ||
| image, err := reference.WithTag(r.baseImage, strings.TrimPrefix(nextVersion, "v")) | ||
|
|
@@ -84,12 +98,13 @@ func (r *VersionUpdater) GetVersion(ctx context.Context, obj client.Object, curr | |
|
|
||
| // NewVersionUpdater returns a version updater using the given version.Getter, | ||
| // img.Validators, maintenance.Triggers and baseImage. | ||
| func NewVersionUpdater(v version.Getter, i img.Validators, t maintenance.Triggers, b reference.Named) VersionUpdater { | ||
| func NewVersionUpdater(v version.Getter, i img.Validators, t maintenance.Triggers, b reference.Named, a version.AuthVersionGetter) VersionUpdater { | ||
| // TODO: do checks to see if not nil/empty ? | ||
| return VersionUpdater{ | ||
| versionGetter: v, | ||
| imageValidators: i, | ||
| maintenanceTriggers: t, | ||
| baseImage: b, | ||
| authVersionGetter: a, | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,61 @@ | ||
| /* | ||
| Copyright 2023 Gravitational, Inc. | ||
|
|
||
| Licensed under the Apache License, Version 2.0 (the "License"); | ||
| you may not use this file except in compliance with the License. | ||
| You may obtain a copy of the License at | ||
|
|
||
| http://www.apache.org/licenses/LICENSE-2.0 | ||
|
|
||
| Unless required by applicable law or agreed to in writing, software | ||
| distributed under the License is distributed on an "AS IS" BASIS, | ||
| WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| See the License for the specific language governing permissions and | ||
| limitations under the License. | ||
| */ | ||
|
|
||
| package version | ||
|
|
||
| import ( | ||
| "context" | ||
| "fmt" | ||
|
|
||
| "github.com/gravitational/trace" | ||
| v1 "k8s.io/api/core/v1" | ||
| kclient "sigs.k8s.io/controller-runtime/pkg/client" | ||
| ) | ||
|
|
||
| const authVersionKeyName = "agent-auth-version" | ||
|
|
||
| // AuthVersionGetter gets the auth server version. | ||
| type AuthVersionGetter interface { | ||
| Get(context.Context, kclient.Object) (string, error) | ||
| } | ||
|
|
||
| type authVersionGetter struct { | ||
| kclient.Client | ||
| } | ||
|
|
||
| // NewAuthVersionGetter creates a new AuthVersionGetter | ||
| func NewAuthVersionGetter(client kclient.Client) AuthVersionGetter { | ||
| return &authVersionGetter{Client: client} | ||
| } | ||
|
|
||
| // Get returns the auth version stored in the shared state secret | ||
| func (a *authVersionGetter) Get(ctx context.Context, object kclient.Object) (string, error) { | ||
| secretName := fmt.Sprintf("%s-shared-state", object.GetName()) | ||
| var secret v1.Secret | ||
| err := a.Client.Get(ctx, kclient.ObjectKey{Namespace: object.GetNamespace(), Name: secretName}, &secret) | ||
| if err != nil { | ||
| return "", trace.Wrap(err) | ||
| } | ||
| rawData, ok := secret.Data[authVersionKeyName] | ||
| if !ok { | ||
| return "", trace.Errorf("secret %s does not have key %s", secretName, authVersionKeyName) | ||
| } | ||
| version, err := EnsureSemver(string(rawData)) | ||
| if err != nil { | ||
| return "", trace.Wrap(err) | ||
| } | ||
| return version, nil | ||
| } |
111 changes: 111 additions & 0 deletions
111
integrations/kube-agent-updater/pkg/version/auth_test.go
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change | ||
|---|---|---|---|---|
| @@ -0,0 +1,111 @@ | ||||
| /* | ||||
| Copyright 2023 Gravitational, Inc. | ||||
|
|
||||
| Licensed under the Apache License, Version 2.0 (the "License"); | ||||
| you may not use this file except in compliance with the License. | ||||
| You may obtain a copy of the License at | ||||
|
|
||||
| http://www.apache.org/licenses/LICENSE-2.0 | ||||
|
|
||||
| Unless required by applicable law or agreed to in writing, software | ||||
| distributed under the License is distributed on an "AS IS" BASIS, | ||||
| WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||||
| See the License for the specific language governing permissions and | ||||
| limitations under the License. | ||||
| */ | ||||
|
|
||||
| package version | ||||
|
|
||||
| import ( | ||||
| "context" | ||||
| "testing" | ||||
|
|
||||
| "github.com/stretchr/testify/require" | ||||
| appsv1 "k8s.io/api/apps/v1" | ||||
| v1 "k8s.io/api/core/v1" | ||||
| metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||||
| kclient "sigs.k8s.io/controller-runtime/pkg/client" | ||||
| "sigs.k8s.io/controller-runtime/pkg/client/fake" | ||||
| ) | ||||
|
|
||||
| func TestAuthVersionGetter_Get(t *testing.T) { | ||||
| // Test setup: generating and loading fixtures | ||||
| ctx := context.Background() | ||||
| namespace := "bar" | ||||
|
|
||||
| fixtures := &v1.SecretList{Items: []v1.Secret{ | ||||
| { | ||||
| ObjectMeta: metav1.ObjectMeta{Name: "no-key-shared-state", Namespace: namespace}, | ||||
| Data: map[string][]byte{"foo": []byte("bar")}, | ||||
| }, | ||||
| { | ||||
| ObjectMeta: metav1.ObjectMeta{Name: "invalid-json-shared-state", Namespace: namespace}, | ||||
| Data: map[string][]byte{authVersionKeyName: []byte(`{"foo": "bar"}`)}, | ||||
| }, | ||||
| { | ||||
| ObjectMeta: metav1.ObjectMeta{Name: "invalid-auth-version-shared-state", Namespace: namespace}, | ||||
| Data: map[string][]byte{authVersionKeyName: []byte(".13")}, | ||||
| }, | ||||
| { | ||||
| ObjectMeta: metav1.ObjectMeta{Name: "valid-auth-version-shared-state", Namespace: namespace}, | ||||
| Data: map[string][]byte{authVersionKeyName: []byte("13.4.5")}, | ||||
| }, | ||||
| { | ||||
| ObjectMeta: metav1.ObjectMeta{Name: "prefix-shared-state", Namespace: namespace}, | ||||
| Data: map[string][]byte{authVersionKeyName: []byte("v13.4.5")}, | ||||
| }, | ||||
| }} | ||||
|
|
||||
| clientBuilder := fake.NewClientBuilder() | ||||
| clientBuilder.WithLists(fixtures) | ||||
| client := clientBuilder.Build() | ||||
|
|
||||
| tests := []struct { | ||||
| name string | ||||
| object kclient.Object | ||||
| want string | ||||
| assertErr require.ErrorAssertionFunc | ||||
| }{ | ||||
| { | ||||
| name: "no secret", | ||||
| object: &appsv1.Deployment{ObjectMeta: metav1.ObjectMeta{Name: "not-found", Namespace: namespace}}, | ||||
| assertErr: require.Error, | ||||
| }, | ||||
| { | ||||
| name: "secret no key", | ||||
| object: &appsv1.Deployment{ObjectMeta: metav1.ObjectMeta{Name: "no-key", Namespace: namespace}}, | ||||
| assertErr: require.Error, | ||||
| }, | ||||
| { | ||||
| name: "secret invalid JSON", | ||||
| object: &appsv1.Deployment{ObjectMeta: metav1.ObjectMeta{Name: "invalid-json", Namespace: namespace}}, | ||||
| assertErr: require.Error, | ||||
| }, | ||||
| { | ||||
| name: "secret invalid auth version", | ||||
| object: &appsv1.Deployment{ObjectMeta: metav1.ObjectMeta{Name: "invalid-auth-version", Namespace: namespace}}, | ||||
| assertErr: require.Error, | ||||
| }, | ||||
| { | ||||
| name: "valid auth version", | ||||
| object: &appsv1.Deployment{ObjectMeta: metav1.ObjectMeta{Name: "valid-auth-version", Namespace: namespace}}, | ||||
| want: "v13.4.5", | ||||
| assertErr: require.NoError, | ||||
| }, | ||||
| { | ||||
| name: "prefix auth version", | ||||
| object: &appsv1.Deployment{ObjectMeta: metav1.ObjectMeta{Name: "prefix", Namespace: namespace}}, | ||||
| want: "v13.4.5", | ||||
| assertErr: require.NoError, | ||||
| }, | ||||
| } | ||||
| // Doing the real test | ||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||
| for _, tt := range tests { | ||||
| t.Run(tt.name, func(t *testing.T) { | ||||
| authVersionGetter := NewAuthVersionGetter(client) | ||||
| version, err := authVersionGetter.Get(ctx, tt.object) | ||||
| tt.assertErr(t, err) | ||||
| require.Equal(t, tt.want, version) | ||||
| }) | ||||
| } | ||||
| } | ||||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can we add the versions as fields to the log message?