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

Add current-job api #1943

Merged
merged 15 commits into from
Mar 15, 2023
Merged

Add current-job api #1943

merged 15 commits into from
Mar 15, 2023

Conversation

moskyb
Copy link
Contributor

@moskyb moskyb commented Feb 7, 2023

This PR: Exposes an API through the agent's job executor (aka "the bootstrap") that allows running jobs to mutate or introspect their own state.

This API currently exposes three endpoints:

  • GET /api/current-job/v0/env to get the environment of the currently running job
  • PATCH /api/current-job/v0/env to update the job's environment
  • DELETE /api/current-job/v0/env to remove certain keys from the job's environment

On unix machines, this API is exposed via a unix domain socket, and on windows, TBD.

Still to do:

  • Figure out how to serve the jobapi.Server on windows (probably a normal TCP port or something? UDSes are available on recent builds of windows, but we don't want to have to replay on that) we're going to do this in a separate PR
  • Write tests for jobapi.Server
  • Actually run the jobapi.Server from the job executor + do some manual testing that it looks all good
  • Create a jobapi.Client thingy to allow other bits of the agent (and potentially other golang projects 👀) to use this API We're also going to do this in a separate PR (and potentially later on)

@moskyb moskyb force-pushed the jobs-api branch 3 times, most recently from d948e81 to 0ea0a67 Compare February 7, 2023 03:28
jobapi/middleware.go Outdated Show resolved Hide resolved
jobapi/doc.go Outdated Show resolved Hide resolved
jobapi/tokens.go Outdated Show resolved Hide resolved
jobapi/server.go Outdated Show resolved Hide resolved
jobapi/server.go Outdated Show resolved Hide resolved
jobapi/routes.go Outdated Show resolved Hide resolved
jobapi/routes.go Outdated Show resolved Hide resolved
jobapi/middleware.go Outdated Show resolved Hide resolved
@moskyb moskyb requested a review from DrJosh9000 February 7, 2023 21:08
Copy link
Contributor

@DrJosh9000 DrJosh9000 left a comment

Choose a reason for hiding this comment

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

Some comments for you!

jobapi/tokens.go Outdated Show resolved Hide resolved
jobapi/server.go Outdated Show resolved Hide resolved
jobapi/middleware.go Outdated Show resolved Hide resolved
jobapi/routes.go Outdated Show resolved Hide resolved
jobapi/routes.go Outdated Show resolved Hide resolved
jobapi/routes.go Outdated Show resolved Hide resolved
@moskyb moskyb force-pushed the jobs-api branch 2 times, most recently from 37a923c to 52a6c01 Compare February 10, 2023 00:40
@moskyb moskyb changed the title WIP: Add current-job api Add current-job api Feb 10, 2023
@moskyb moskyb marked this pull request as ready for review February 10, 2023 00:41
@moskyb moskyb requested a review from a team February 10, 2023 00:43
Copy link
Contributor

@DrJosh9000 DrJosh9000 left a comment

Choose a reason for hiding this comment

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

Hey! I wrote some more comments for you...

bootstrap/bootstrap.go Outdated Show resolved Hide resolved
bootstrap/bootstrap.go Outdated Show resolved Hide resolved
jobapi/server.go Outdated Show resolved Hide resolved
bootstrap/bootstrap.go Outdated Show resolved Hide resolved
jobapi/server_test.go Show resolved Hide resolved
@moskyb moskyb force-pushed the jobs-api branch 2 times, most recently from 4d63043 to 47be827 Compare February 28, 2023 01:13
@DrJosh9000 DrJosh9000 added this to the v3.45 milestone Feb 28, 2023
bootstrap/api.go Outdated Show resolved Hide resolved
@moskyb moskyb force-pushed the jobs-api branch 2 times, most recently from d6a4098 to 2484b4d Compare March 2, 2023 06:31
EXPERIMENTS.md Show resolved Hide resolved
@@ -44,7 +44,7 @@ type BootstrapTester struct {
}

func NewBootstrapTester() (*BootstrapTester, error) {
homeDir, err := os.MkdirTemp("", "home")
homeDir, err := os.MkdirTemp("/tmp", "home")
Copy link
Contributor

Choose a reason for hiding this comment

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

Maybe worth a comment mentioning the Linux-path-length-for-Unix-sockets problem?

Copy link
Contributor

@DrJosh9000 DrJosh9000 left a comment

Choose a reason for hiding this comment

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

🎉 🎉 🎉 🥳 🎊 🪩 🍕

A bunch of remaining comments are minor nits, I leave it up to you whether to apply, raise a follow-up, or ignore 🥺

jobapi/middleware_test.go Outdated Show resolved Hide resolved
jobapi/middleware_test.go Outdated Show resolved Hide resolved
jobapi/middleware_test.go Show resolved Hide resolved

resp := EnvGetResponse{Env: normalizedEnv}
w.WriteHeader(http.StatusOK)
json.NewEncoder(w).Encode(resp)
Copy link
Contributor

Choose a reason for hiding this comment

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

[Something to think about] If this did return an error, is that worth logging?

Comment on lines +78 to +80
go func() {
_ = s.httpSvr.Serve(l)
}()
Copy link
Contributor

Choose a reason for hiding this comment

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

[Suggestion] If the error is being dropped here (which is fine) then why not go s.httpSvr.Serve(l) ?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

if a func returns an err that i'm not tracking, i much prefer to have _ = mightFail(), as it makes my intentions clear - it shows that i know that this function might return an error, but that i explicitly don't care. happy to change either way, but it's a readability thing that i like

Copy link
Contributor

Choose a reason for hiding this comment

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

That's fair, and tend to agree that _ = to denote deliberately ignored errors is nice. I'm less used to it.

Though I think http.Server.Serve is a bit unusual in that it always returns a non-nil error.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

oh that's true, i hadn't realised that... i think we're probably still fine to ignore it though, unless you disagree?

Copy link
Contributor

Choose a reason for hiding this comment

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

Ignoring it is fine, with either syntax.

jobapi/server_test.go Outdated Show resolved Hide resolved
jobapi/server_test.go Outdated Show resolved Hide resolved
jobapi/server_test.go Outdated Show resolved Hide resolved
@@ -1,3 +1,5 @@
*.DS_STORE
Copy link
Contributor

Choose a reason for hiding this comment

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

There should be a .gitignorefmt. I want to argue with this being at this spot in the file and in all caps, but it also isn't wrong?

jobapi/server_test.go Outdated Show resolved Hide resolved
@moskyb moskyb force-pushed the jobs-api branch 2 times, most recently from dc3ce8a to 8dacf98 Compare March 3, 2023 02:27
Copy link
Contributor

@triarius triarius left a comment

Choose a reason for hiding this comment

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

Nice work! I have some early comments I want to get in.

bootstrap/bootstrap.go Outdated Show resolved Hide resolved
jobapi/socket.go Outdated Show resolved Hide resolved
jobapi/socket.go Outdated Show resolved Hide resolved
t.Fatalf("os.Stat(%q) error = %v", srv.SocketPath, err)
}

isSocket := info.Mode()&os.ModeSocket != 0
Copy link
Contributor

Choose a reason for hiding this comment

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

I just discovered golang/go#33357, so this check might need skipping on Windows

Copy link
Contributor Author

Choose a reason for hiding this comment

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

😭

Copy link
Contributor

Choose a reason for hiding this comment

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

I'm having trouble even calling os.Stat on the socket on Windows. But connecting to it is fine.

Copy link
Contributor

Choose a reason for hiding this comment

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

Here's what I ended up with in the client:

agent/jobapi/client.go

Lines 51 to 69 in 3f9c89d

// Check the socket path exists and is a socket.
// Note that os.ModeSocket might not be set on Windows.
// (https://github.com/golang/go/issues/33357)
if runtime.GOOS != "windows" {
fi, err := os.Stat(sock)
if err != nil {
return nil, fmt.Errorf("stat socket: %w", err)
}
if fi.Mode()&os.ModeSocket == 0 {
return nil, fmt.Errorf("%q is not a socket", sock)
}
}
// Try to connect to the socket.
test, err := net.Dial("unix", sock)
if err != nil {
return nil, fmt.Errorf("socket test connection: %w", err)
}
test.Close()

@moskyb moskyb force-pushed the jobs-api branch 4 times, most recently from 38d241b to 2ca3dc9 Compare March 13, 2023 02:16
moskyb and others added 13 commits March 15, 2023 14:05
This is an API that allows the currently-running job to observe and mutate its own state in the form of environment variables. It's exposed via a Unix Domain Socket on supported OSes, and unsupported ones (Windows prior to build 17063) may not use it
Co-authored-by: Josh Deprez <[email protected]>
silly me, filepath.Join is OS-aware, but path.Join just puts in a slash! how could i have been so boneheaded!
@moskyb moskyb merged commit f549ecf into main Mar 15, 2023
@moskyb moskyb deleted the jobs-api branch March 15, 2023 01:46
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