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

Create Spacelift client #5

Merged
merged 13 commits into from
Mar 4, 2024
Merged

Conversation

michalg9
Copy link
Contributor

@michalg9 michalg9 commented Feb 29, 2024

The most simple solution that is basically re-using code we have in spacectl.

The main idea for dropping it is because we'd need to re-implement Spacelift token fetching. There are also other challenges (e.g. you need to have whole graphql schema on a file locally to generate safe-type structs) and all this seems like a big overkill for a simple POC we need to deliver in 2 weeks.

Happy to take the idea of writing an SDK later, as indeed it would be useful if all our tooling (e.g. tf provider, spacectl, this) used something common.

image

@michalg9 michalg9 requested a review from eliecharra February 29, 2024 12:46
Copy link
Member

@eliecharra eliecharra left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I like the idea to reuse the client from spacectl 👍🏻
I just dropped a few comments but the overall approach looks great 👍🏻

go.mod Show resolved Hide resolved
client/client.go Outdated Show resolved Hide resolved
client/client_test.go Outdated Show resolved Hide resolved
go.mod Outdated Show resolved Hide resolved
@michalg9 michalg9 force-pushed the CU-8693y867z-create-spacelift-client branch from a515328 to 55ac561 Compare March 1, 2024 11:18
@michalg9 michalg9 requested a review from a team as a code owner March 1, 2024 11:18
@adamconnelly
Copy link

@michalg9 just to mention, the Prometheus exporter has a Spacelift GraphQL client in it that's basically a stripped-down version of the one from spacectl that only support API keys: https://github.com/spacelift-io/prometheus-exporter.

Feel free to ignore it since you've already done this, but just mentioning it because it might be worth taking a look at. It has some fixes for things like refetching the token when it expires and retrying the request.

@adamconnelly
Copy link

RE the token refresh issue - here's what I'm talking about: spacelift-io/prometheus-exporter@380423d

The problem is that spacectl isn't designed for long-running operations, so if you cache the session it stops working after a while.

@eliecharra
Copy link
Member

That's actually a very good concern @adamconnelly. We talked about that with @michalg9 and we thought that the spacectl client was handling the token refresh for us. But if not, we should probably use the prom version one 👍🏻

@michalg9
Copy link
Contributor Author

michalg9 commented Mar 1, 2024

After discussing with @adamconnelly he suggested that even though the refresh token logic is in spacectl client, that.. I'll just quote the Man 😆 :

So the spacectl code assumes the token is valid until its expiry, but that doesn't take into account things like login policy changes, or a session being deleted in the backend.

I'll swap the spacectl client with prometheus client, and then, after kubecon we can work on extracting that to a separate repo.

@eliecharra
Copy link
Member

After discussing with @adamconnelly he suggested that even though the refresh token logic is in spacectl client, that.. I'll just quote the Man 😆

Let's do that, good catch @adamconnelly

I'll swap the spacectl client with prometheus client, and then, after kubecon we can work on extracting that to a separate repo.

100% I created an entry for that in our sheet here to be sure we'll not forget

@michalg9
Copy link
Contributor Author

michalg9 commented Mar 1, 2024

Okay, I've refactored a bunch of things and added secret fetching, so that it should unblock @eliecharra and me with continuing with CRDs.

I will handle the edge case of removing session in UI or due to login policy change in a separate PR to unblock our work (CC @adamconnelly )

Comment on lines +59 to +73
var query struct {
Stack struct {
ID string `graphql:"id"`
Administrative bool `graphql:"administrative"`
} `graphql:"stack(id: $id)"`
}

variables := map[string]interface{}{
"id": graphql.ID("end-to-end-autoconfirm"),
}

err = spaceliftClient.Query(context.Background(), &query, variables)
if err != nil {
return ctrl.Result{}, err
}
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I believe we can enclose queries we will use in some nice functions in client package, for now that's just an usage example, wanted to make sure it works e2e.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah I was gonna drop a comment about that, we can still refactor later on but I would like to have a internal/spacelift/repository/stack.go and internal/spacelift/repository/run.go to abstract that from the reconcilier.

Copy link

@adamconnelly adamconnelly left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I know you guys are gonna tweak this, but +1 from me to unblock you for now. Overall approach is fine.

client/client.go Outdated Show resolved Hide resolved
client/client.go Outdated Show resolved Hide resolved
client/client.go Outdated Show resolved Hide resolved
client/client.go Outdated Show resolved Hide resolved
client/client.go Outdated Show resolved Hide resolved
client/client_test.go Outdated Show resolved Hide resolved
client/interface.go Outdated Show resolved Hide resolved
Comment on lines +59 to +73
var query struct {
Stack struct {
ID string `graphql:"id"`
Administrative bool `graphql:"administrative"`
} `graphql:"stack(id: $id)"`
}

variables := map[string]interface{}{
"id": graphql.ID("end-to-end-autoconfirm"),
}

err = spaceliftClient.Query(context.Background(), &query, variables)
if err != nil {
return ctrl.Result{}, err
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah I was gonna drop a comment about that, we can still refactor later on but I would like to have a internal/spacelift/repository/stack.go and internal/spacelift/repository/run.go to abstract that from the reconcilier.

@michalg9 michalg9 requested a review from eliecharra March 1, 2024 18:02
Copy link
Member

@eliecharra eliecharra left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Amazing @michalg9, thanks a lot for that 🙏🏻
Let's merge it asap so we can unblock run and stack reconcilier work, and refactor it a bit later.
I actually got an idea to make the client a bit easier to use, I'll push a PR soon

@eliecharra eliecharra merged commit 6cbbe6d into main Mar 4, 2024
5 checks passed
@eliecharra eliecharra deleted the CU-8693y867z-create-spacelift-client branch March 4, 2024 08:50
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants