-
Notifications
You must be signed in to change notification settings - Fork 80
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
Completion of a restore triggers secrets refresh #1134
Merged
adejanovski
merged 26 commits into
k8ssandra:main
from
Miles-Garnsey:feature/refresh-secrets
Jan 9, 2024
Merged
Changes from all commits
Commits
Show all changes
26 commits
Select commit
Hold shift + click to select a range
e8831f3
Completion of a restore triggers secrets refresh.
Miles-Garnsey 1ce6ce1
Fix error check.
Miles-Garnsey 8b29f68
Add a test to the envtests too.
Miles-Garnsey 281b556
Fix envtest.
Miles-Garnsey 85d9aed
Delete envtest test.
Miles-Garnsey 111c5ff
Add e2e test.
Miles-Garnsey a0e6f02
more logging for medusa tests.
Miles-Garnsey 227a42e
Add some logging to allow debugging.
Miles-Garnsey 8277e18
Ensure right default username is used.
Miles-Garnsey 66c45af
Deal with multiple different cluster names leading to different super…
Miles-Garnsey c4e1eee
Alex's suggested test change.
Miles-Garnsey 5ec13af
Fix error introduced by last change.
Miles-Garnsey 2166919
Fix tests so that they always retrieve the right secret.
Miles-Garnsey 0cee309
Changelog.
Miles-Garnsey 5d63dfb
More debugging.
Miles-Garnsey e922327
More debugging.
Miles-Garnsey 9a218fb
Error handling, restore old logging style.
Miles-Garnsey f7019cb
Print version and commit information at startup.
Miles-Garnsey d4c5188
Reinstate other tests.
Miles-Garnsey d823aa4
Fix infinite requeue problem.
Miles-Garnsey 824732d
Longer timeout for restore e2e test.
Miles-Garnsey c98f5f2
Ensure we always get most recent version of secret even after failure.
Miles-Garnsey a9e707f
Back out changes to e2e test timeout so we can get logs again.
Miles-Garnsey b28e8d1
Fix up build variables in makefile.
Miles-Garnsey 2ba393e
Fix requeue handling and switch to making timestamp based on finishTi…
Miles-Garnsey 905a912
Maybe let's use StartTime instead of finishTime to ensure that we don…
Miles-Garnsey 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 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 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 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 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 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 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 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,63 @@ | ||
package medusa | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"time" | ||
|
||
"github.com/go-logr/logr" | ||
cassdcapi "github.com/k8ssandra/cass-operator/apis/cassandra/v1beta1" | ||
k8ssandraapi "github.com/k8ssandra/k8ssandra-operator/apis/k8ssandra/v1alpha1" | ||
"github.com/k8ssandra/k8ssandra-operator/pkg/reconciliation" | ||
"github.com/k8ssandra/k8ssandra-operator/pkg/result" | ||
corev1 "k8s.io/api/core/v1" | ||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
"k8s.io/apimachinery/pkg/types" | ||
"sigs.k8s.io/controller-runtime/pkg/client" | ||
) | ||
|
||
func RefreshSecrets(dc *cassdcapi.CassandraDatacenter, ctx context.Context, client client.Client, logger logr.Logger, requeueDelay time.Duration, restoreTimestamp metav1.Time) result.ReconcileResult { | ||
logger.Info(fmt.Sprintf("Restore complete for DC %#v, Refreshing secrets", dc.ObjectMeta)) | ||
userSecrets := []string{} | ||
for _, user := range dc.Spec.Users { | ||
userSecrets = append(userSecrets, user.SecretName) | ||
} | ||
if dc.Spec.SuperuserSecretName == "" { | ||
userSecrets = append(userSecrets, cassdcapi.CleanupForKubernetes(dc.Spec.ClusterName)+"-superuser") //default SU secret | ||
} else { | ||
userSecrets = append(userSecrets, dc.Spec.SuperuserSecretName) | ||
} | ||
logger.Info(fmt.Sprintf("refreshing user secrets for %v", userSecrets)) | ||
// Both Reaper and medusa secrets go into the userSecrets, so they don't need special handling. | ||
requeues := 0 | ||
for _, i := range userSecrets { | ||
secret := &corev1.Secret{} | ||
// We need to do our own retries here instead of delegating it back up to the reconciler, because of | ||
// the nature (time based) of the annotation we're adding. Otherwise we never complete because the | ||
// object on the server never matches the desired object with the new time. | ||
err := client.Get(ctx, types.NamespacedName{Name: i, Namespace: dc.Namespace}, secret) | ||
|
||
if err != nil { | ||
logger.Error(err, fmt.Sprintf("Failed to get secret %s", i)) | ||
return result.Error(err) | ||
} | ||
if secret.ObjectMeta.Annotations == nil { | ||
secret.ObjectMeta.Annotations = make(map[string]string) | ||
} | ||
secret.ObjectMeta.Annotations[k8ssandraapi.RefreshAnnotation] = restoreTimestamp.String() | ||
recRes := reconciliation.ReconcileObject(ctx, client, requeueDelay, *secret) | ||
switch { | ||
case recRes.IsError(): | ||
return recRes | ||
case recRes.IsRequeue(): | ||
requeues++ | ||
continue | ||
case recRes.IsDone(): | ||
continue | ||
} | ||
if requeues > 0 { | ||
return result.RequeueSoon(requeueDelay) | ||
} | ||
} | ||
return result.Done() | ||
} |
This file contains 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,72 @@ | ||
package medusa | ||
|
||
import ( | ||
"context" | ||
"testing" | ||
|
||
"github.com/go-logr/logr" | ||
cassdcapi "github.com/k8ssandra/cass-operator/apis/cassandra/v1beta1" | ||
"github.com/k8ssandra/k8ssandra-operator/pkg/test" | ||
"github.com/stretchr/testify/assert" | ||
corev1 "k8s.io/api/core/v1" | ||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
"k8s.io/apimachinery/pkg/types" | ||
) | ||
|
||
func TestRefreshSecrets_defaultSUSecret(t *testing.T) { | ||
fakeClient := test.NewFakeClientWRestMapper() | ||
cassDC := test.NewCassandraDatacenter("dc1", "test") | ||
cassDC.Spec.Users = []cassdcapi.CassandraUser{ | ||
{SecretName: "custom-user"}, | ||
} | ||
assert.NoError(t, fakeClient.Create(context.Background(), &corev1.Namespace{ObjectMeta: metav1.ObjectMeta{Name: "test"}})) | ||
assert.NoError(t, fakeClient.Create(context.Background(), &cassDC)) | ||
secrets := []corev1.Secret{ | ||
{ObjectMeta: metav1.ObjectMeta{Name: "custom-user", Namespace: "test"}, Data: map[string][]byte{"username": []byte("test")}}, | ||
{ObjectMeta: metav1.ObjectMeta{Name: "test-cluster-superuser", Namespace: "test"}, Data: map[string][]byte{"username": []byte("test")}}, | ||
} | ||
for _, i := range secrets { | ||
assert.NoError(t, fakeClient.Create(context.Background(), &i)) | ||
} | ||
|
||
recRes := RefreshSecrets(&cassDC, context.Background(), fakeClient, logr.Logger{}, 0, metav1.Now()) | ||
assert.True(t, recRes.IsDone()) | ||
suSecret := &corev1.Secret{} | ||
assert.NoError(t, fakeClient.Get(context.Background(), types.NamespacedName{Name: "test-cluster-superuser", Namespace: "test"}, suSecret)) | ||
_, exists := suSecret.ObjectMeta.Annotations["k8ssandra.io/refresh"] | ||
assert.True(t, exists) | ||
userSecret := &corev1.Secret{} | ||
assert.NoError(t, fakeClient.Get(context.Background(), types.NamespacedName{Name: "custom-user", Namespace: "test"}, userSecret)) | ||
_, exists = userSecret.ObjectMeta.Annotations["k8ssandra.io/refresh"] | ||
assert.True(t, exists) | ||
} | ||
|
||
func TestRefreshSecrets_customSecrets(t *testing.T) { | ||
fakeClient := test.NewFakeClientWRestMapper() | ||
cassDC := test.NewCassandraDatacenter("dc1", "test") | ||
cassDC.Spec.Users = []cassdcapi.CassandraUser{ | ||
{SecretName: "custom-user"}, | ||
} | ||
cassDC.Spec.SuperuserSecretName = "cass-custom-superuser" | ||
assert.NoError(t, fakeClient.Create(context.Background(), &corev1.Namespace{ObjectMeta: metav1.ObjectMeta{Name: "test"}})) | ||
assert.NoError(t, fakeClient.Create(context.Background(), &cassDC)) | ||
secrets := []corev1.Secret{ | ||
{ObjectMeta: metav1.ObjectMeta{Name: "custom-user", Namespace: "test"}}, | ||
{ObjectMeta: metav1.ObjectMeta{Name: "cass-custom-superuser", Namespace: "test"}}, | ||
} | ||
for _, i := range secrets { | ||
assert.NoError(t, fakeClient.Create(context.Background(), &i)) | ||
} | ||
|
||
recRes := RefreshSecrets(&cassDC, context.Background(), fakeClient, logr.Logger{}, 0, metav1.Now()) | ||
assert.True(t, recRes.IsDone()) | ||
suSecret := &corev1.Secret{} | ||
assert.NoError(t, fakeClient.Get(context.Background(), types.NamespacedName{Name: "cass-custom-superuser", Namespace: "test"}, suSecret)) | ||
_, exists := suSecret.ObjectMeta.Annotations["k8ssandra.io/refresh"] | ||
assert.True(t, exists) | ||
userSecret := &corev1.Secret{} | ||
assert.NoError(t, fakeClient.Get(context.Background(), types.NamespacedName{Name: "custom-user", Namespace: "test"}, userSecret)) | ||
_, exists = userSecret.ObjectMeta.Annotations["k8ssandra.io/refresh"] | ||
assert.True(t, exists) | ||
|
||
} |
This file contains 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
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.
nit: this shouldn't be part of this PR I guess.