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: allow to dynamically reload spacelift credentials from secret #49

Merged
merged 1 commit into from
Jun 20, 2024
Merged
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
15 changes: 10 additions & 5 deletions internal/spacelift/client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,10 @@ import (
// spaceliftClient is the authenticated client that can be used to interact with Spacelift
var spaceliftClient Client

// secretVersion stores the version of the secret currently in use by the client
// This allows to recreate the spaceliftClient if the secret has been changed in the cluster
var secretVersion string

const (
SecretName = "spacelift-credentials" //nolint:gosec
SpaceliftApiKeyEndpointKey = "SPACELIFT_API_KEY_ENDPOINT" //nolint:gosec
Expand All @@ -31,12 +35,7 @@ const (
var DefaultClient = GetSpaceliftClient

func GetSpaceliftClient(ctx context.Context, client k8sclient.Client, namespace string) (Client, error) {
if spaceliftClient != nil {
return spaceliftClient, nil
}

var secret v1.Secret

if err := client.Get(
ctx,
k8sclient.ObjectKey{
Expand All @@ -52,6 +51,11 @@ func GetSpaceliftClient(ctx context.Context, client k8sclient.Client, namespace
apiKeyID := string(secret.Data[SpaceliftApiKeyIDKey])
apiKeySecret := string(secret.Data[SpaceliftApiKeySecretKey])

currentSecretVersion := secret.GetResourceVersion()
if spaceliftClient != nil && currentSecretVersion == secretVersion {
return spaceliftClient, nil
}

session, err := func() (session.Session, error) {
sessionCtx, cancel := context.WithTimeout(ctx, time.Second*5)
defer cancel()
Expand All @@ -62,6 +66,7 @@ func GetSpaceliftClient(ctx context.Context, client k8sclient.Client, namespace
}

spaceliftClient = New(http.DefaultClient, session)
secretVersion = currentSecretVersion

return spaceliftClient, nil
}
Expand Down