generated from kubernetes/kubernetes-template-project
-
Notifications
You must be signed in to change notification settings - Fork 22
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #68 from evacchi/wasm-uris
config: add support to file:// and http(s):// URIs
- Loading branch information
Showing
9 changed files
with
311 additions
and
101 deletions.
There are no files selected for viewing
This file contains 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 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 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 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 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,66 @@ | ||
/* | ||
Copyright 2023 The Kubernetes Authors. | ||
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 wasm | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"io" | ||
"net/http" | ||
"net/url" | ||
) | ||
|
||
// httpClient decorates an http.Client with convenience methods. | ||
type httpClient struct { | ||
c http.Client | ||
} | ||
|
||
// newHTTPClient is a constructor for httpFetcher. | ||
// | ||
// It is possible to plug a custom http.RoundTripper to handle other concerns (e.g. retries) | ||
// Compression is handled transparently and automatically by http.Client. | ||
func newHTTPClient(transport http.RoundTripper) *httpClient { | ||
return &httpClient{ | ||
c: http.Client{Transport: transport}, | ||
} | ||
} | ||
|
||
// get returns a byte slice of the wasm module found at the given URL, or an error otherwise. | ||
func (f *httpClient) get(ctx context.Context, u *url.URL) ([]byte, error) { | ||
req, err := http.NewRequestWithContext(ctx, http.MethodGet, u.String(), nil) | ||
if err != nil { | ||
return nil, err | ||
} | ||
resp, err := f.c.Do(req.WithContext(ctx)) | ||
defer func() { | ||
io.Copy(io.Discard, resp.Body) //nolint | ||
resp.Body.Close() | ||
}() | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
if resp.StatusCode != http.StatusOK { | ||
return nil, fmt.Errorf("received %v status code from %q", resp.StatusCode, u) | ||
} | ||
|
||
bytes, err := io.ReadAll(resp.Body) | ||
if err != nil { | ||
return nil, err | ||
} | ||
return bytes, nil | ||
} |
This file contains 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,89 @@ | ||
/* | ||
Copyright 2023 The Kubernetes Authors. | ||
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 wasm | ||
|
||
import ( | ||
"compress/gzip" | ||
"context" | ||
"net/http" | ||
"net/http/httptest" | ||
"net/url" | ||
"testing" | ||
"time" | ||
|
||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
var wasmMagicNumber = []byte{0x00, 0x61, 0x73, 0x6d} | ||
|
||
func TestWasmHTTPFetch(t *testing.T) { | ||
wasmBinary := wasmMagicNumber | ||
wasmBinary = append(wasmBinary, 0x00, 0x00, 0x00, 0x00) | ||
cases := []struct { | ||
name string | ||
handler http.HandlerFunc | ||
expectedError string | ||
}{ | ||
{ | ||
name: "plain wasm binary", | ||
handler: func(w http.ResponseWriter, r *http.Request) { | ||
_, _ = w.Write(wasmBinary) | ||
}, | ||
}, | ||
// Compressed payloads are handled automatically by http.Client. | ||
{ | ||
name: "compressed payload", | ||
handler: func(w http.ResponseWriter, r *http.Request) { | ||
w.Header().Set("Content-Type", "application/json") | ||
w.Header().Set("Content-Encoding", "gzip") | ||
|
||
gw := gzip.NewWriter(w) | ||
defer gw.Close() | ||
_, _ = gw.Write(wasmBinary) | ||
}, | ||
}, | ||
{ | ||
name: "http error", | ||
handler: func(w http.ResponseWriter, r *http.Request) { | ||
w.WriteHeader(http.StatusInternalServerError) | ||
}, | ||
expectedError: "received 500 status code", | ||
}, | ||
} | ||
|
||
for _, proto := range []string{"http", "https"} { | ||
t.Run(proto, func(t *testing.T) { | ||
for _, tc := range cases { | ||
t.Run(tc.name, func(t *testing.T) { | ||
ts := httptest.NewServer(tc.handler) | ||
defer ts.Close() | ||
c := newHTTPClient(http.DefaultTransport) | ||
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second) | ||
defer cancel() | ||
parse, err := url.Parse(ts.URL) | ||
require.NoError(t, err) | ||
_, err = c.get(ctx, parse) | ||
if tc.expectedError != "" { | ||
require.ErrorContains(t, err, tc.expectedError) | ||
return | ||
} | ||
require.NoError(t, err, "Wasm download got an unexpected error: %v", err) | ||
}) | ||
} | ||
}) | ||
} | ||
} |
This file contains 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
Oops, something went wrong.