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 GetMaxAPIVersion to get max supported API #337

Merged
merged 1 commit into from
Aug 9, 2024
Merged
Show file tree
Hide file tree
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
17 changes: 17 additions & 0 deletions client/nginx.go
Original file line number Diff line number Diff line change
Expand Up @@ -610,6 +610,23 @@ func versionSupported(n int) bool {
return false
}

// GetMaxAPIVersion returns the maximum API version supported by the server and the client.
func (client *NginxClient) GetMaxAPIVersion() (int, error) {
serverVersions, err := client.getAPIVersions(client.httpClient, client.apiEndpoint)
if err != nil {
return 0, fmt.Errorf("failed to get max API version: %w", err)
}

maxServerVersion := slices.Max(*serverVersions)
maxClientVersion := slices.Max(supportedAPIVersions)

if maxServerVersion > maxClientVersion {
return maxClientVersion, nil
}

return maxServerVersion, nil
}

func (client *NginxClient) getAPIVersions(httpClient *http.Client, endpoint string) (*versions, error) {
ctx, cancel := context.WithTimeout(context.Background(), client.ctxTimeout)
defer cancel()
Expand Down
64 changes: 64 additions & 0 deletions client/nginx_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -752,3 +752,67 @@ func TestGetStats_SSL(t *testing.T) {
t.Fatalf("SSL stats: expected %v, actual %v", testStats, stats.SSL)
}
}

func TestGetMaxAPIVersionServer(t *testing.T) {
t.Parallel()
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
switch {
case r.RequestURI == "/":
_, err := w.Write([]byte(`[4, 5, 6, 7]`))
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
default:
_, err := w.Write([]byte(`{}`))
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
}
}))
defer ts.Close()

c, err := NewNginxClient(ts.URL)
if err != nil {
t.Fatalf("unexpected error: %v", err)
}

maxVer, err := c.GetMaxAPIVersion()
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
if maxVer != 7 {
t.Fatalf("expected 7, got %v", maxVer)
}
}

func TestGetMaxAPIVersionClient(t *testing.T) {
t.Parallel()
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
switch {
case r.RequestURI == "/":
_, err := w.Write([]byte(`[4, 5, 6, 7, 8, 9, 25]`))
sjberman marked this conversation as resolved.
Show resolved Hide resolved
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
default:
_, err := w.Write([]byte(`{}`))
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
}
}))
defer ts.Close()

c, err := NewNginxClient(ts.URL)
if err != nil {
t.Fatalf("unexpected error: %v", err)
}

maxVer, err := c.GetMaxAPIVersion()
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
if maxVer != c.apiVersion {
t.Fatalf("expected %v, got %v", c.apiVersion, maxVer)
}
}
2 changes: 1 addition & 1 deletion docker/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# syntax=docker/dockerfile:1.7
# syntax=docker/dockerfile:1.8
FROM debian:12-slim

LABEL maintainer="NGINX Docker Maintainers <integrations@nginx.com>"
Expand Down