forked from kubernetes-sigs/kube-scheduler-wasm-extension
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
config: add support to file:// and http(s):// URIs
Extends the current plugin config to use instead a URI. In the case of `file://` the behavior is the same as it is currently. In the case of `http(s)://` it will fetch the URI and try to evaluate it as a wasm payload. This PR is based on earlier work on `dapr/component-contrib`. See: dapr/components-contrib#3005 Signed-off-by: Edoardo Vacchi <[email protected]>
- Loading branch information
Showing
9 changed files
with
310 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,65 @@ | ||
/* | ||
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 | ||
} | ||
|
||
// newHTTPFetcher 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}, | ||
} | ||
} | ||
|
||
// fetch 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)) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
if resp.StatusCode != http.StatusOK { | ||
io.Copy(io.Discard, resp.Body) //nolint | ||
resp.Body.Close() | ||
return nil, fmt.Errorf("received %v status code from %q", resp.StatusCode, u) | ||
} | ||
|
||
bytes, err := io.ReadAll(resp.Body) | ||
resp.Body.Close() | ||
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.