-
Notifications
You must be signed in to change notification settings - Fork 352
feat: add support for health-check flag #1271
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
Merged
Merged
Changes from all commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
044cfc5
feat: add support for health-check flag
enocom 5c47cc0
Fix headers
enocom 0bbc034
Add nil check
enocom 6abe3b1
Fix header again
enocom a1b7781
Add log line for missing flag
enocom f6481f5
Remove old test
enocom 5be5c9c
Increase wait time for proxy startup
enocom 4e21223
Update internal/proxy/proxy.go
enocom 82e1b24
Update cmd/root.go
enocom b98d6c5
Update cmd/root.go
enocom 055a579
PR comments
enocom 899474d
test cleanup
enocom abffead
Use needsHTTPServer to gate HTTP server startup
enocom ace86db
Comment cleanup
enocom 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -516,10 +516,6 @@ func TestNewCommandWithErrors(t *testing.T) { | |
| desc: "when the iam authn login query param is bogus", | ||
| args: []string{"proj:region:inst?auto-iam-authn=nope"}, | ||
| }, | ||
| { | ||
enocom marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| desc: "enabling a Prometheus port without a namespace", | ||
| args: []string{"--http-port", "1111", "proj:region:inst"}, | ||
| }, | ||
| { | ||
| desc: "using an invalid url for sqladmin-api-endpoint", | ||
| args: []string{"--sqladmin-api-endpoint", "https://user:abc{[email protected]:5432/db?sslmode=require", "proj:region:inst"}, | ||
|
|
||
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,109 @@ | ||
| // Copyright 2022 Google LLC | ||
| // | ||
| // 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 healthcheck tests and communicates the health of the Cloud SQL Auth proxy. | ||
| package healthcheck | ||
|
|
||
| import ( | ||
| "context" | ||
| "errors" | ||
| "fmt" | ||
| "net/http" | ||
| "sync" | ||
|
|
||
| "github.com/GoogleCloudPlatform/cloudsql-proxy/v2/cloudsql" | ||
| "github.com/GoogleCloudPlatform/cloudsql-proxy/v2/internal/proxy" | ||
| ) | ||
|
|
||
| // Check provides HTTP handlers for use as healthchecks typically in a | ||
| // Kubernetes context. | ||
| type Check struct { | ||
| once *sync.Once | ||
| started chan struct{} | ||
| proxy *proxy.Client | ||
| logger cloudsql.Logger | ||
| } | ||
|
|
||
| // NewCheck is the initializer for Check. | ||
| func NewCheck(p *proxy.Client, l cloudsql.Logger) *Check { | ||
| return &Check{ | ||
| once: &sync.Once{}, | ||
| started: make(chan struct{}), | ||
| proxy: p, | ||
| logger: l, | ||
| } | ||
| } | ||
|
|
||
| // NotifyStarted notifies the check that the proxy has started up successfully. | ||
| func (c *Check) NotifyStarted() { | ||
| c.once.Do(func() { close(c.started) }) | ||
| } | ||
|
|
||
| // HandleStartup reports whether the Check has been notified of startup. | ||
| func (c *Check) HandleStartup(w http.ResponseWriter, _ *http.Request) { | ||
| select { | ||
| case <-c.started: | ||
| w.WriteHeader(http.StatusOK) | ||
| w.Write([]byte("ok")) | ||
| default: | ||
| w.WriteHeader(http.StatusServiceUnavailable) | ||
| w.Write([]byte("error")) | ||
| } | ||
| } | ||
|
|
||
| var errNotStarted = errors.New("proxy is not started") | ||
|
|
||
| // HandleReadiness ensures the Check has been notified of successful startup, | ||
| // that the proxy has not reached maximum connections, and that all connections | ||
| // are healthy. | ||
| func (c *Check) HandleReadiness(w http.ResponseWriter, _ *http.Request) { | ||
| ctx, cancel := context.WithCancel(context.Background()) | ||
| defer cancel() | ||
|
|
||
| select { | ||
| case <-c.started: | ||
hessjcg marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| default: | ||
| c.logger.Errorf("[Health Check] Readiness failed: %v", errNotStarted) | ||
| w.WriteHeader(http.StatusServiceUnavailable) | ||
| w.Write([]byte(errNotStarted.Error())) | ||
| return | ||
| } | ||
|
|
||
| if open, max := c.proxy.ConnCount(); max > 0 && open == max { | ||
| err := fmt.Errorf("max connections reached (open = %v, max = %v)", open, max) | ||
| c.logger.Errorf("[Health Check] Readiness failed: %v", err) | ||
| w.WriteHeader(http.StatusServiceUnavailable) | ||
| w.Write([]byte(err.Error())) | ||
| return | ||
| } | ||
|
|
||
| err := c.proxy.CheckConnections(ctx) | ||
enocom marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| if err != nil { | ||
| c.logger.Errorf("[Health Check] Readiness failed: %v", err) | ||
| w.WriteHeader(http.StatusServiceUnavailable) | ||
| w.Write([]byte(err.Error())) | ||
| return | ||
| } | ||
|
|
||
| w.WriteHeader(http.StatusOK) | ||
| w.Write([]byte("ok")) | ||
| } | ||
|
|
||
| // HandleLiveness indicates the process is up and responding to HTTP requests. | ||
| // If this check fails (because it's not reachable), the process is in a bad | ||
| // state and should be restarted. | ||
| func (c *Check) HandleLiveness(w http.ResponseWriter, _ *http.Request) { | ||
| w.WriteHeader(http.StatusOK) | ||
| w.Write([]byte("ok")) | ||
| } | ||
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.
Uh oh!
There was an error while loading. Please reload this page.