Skip to content

Commit 0f5ffa0

Browse files
committed
Add specific DecodeURL function in decoder package
1 parent 65f31fb commit 0f5ffa0

File tree

2 files changed

+48
-2
lines changed

2 files changed

+48
-2
lines changed

klient/decoder/decoder.go

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ import (
2727
"os"
2828
"strings"
2929

30+
"github.com/vladimirvivien/gexe/http"
3031
apierrors "k8s.io/apimachinery/pkg/api/errors"
3132
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
3233
"k8s.io/apimachinery/pkg/runtime"
@@ -83,7 +84,7 @@ func DecodeEachFile(ctx context.Context, fsys fs.FS, pattern string, handlerFn H
8384
return nil
8485
}
8586

86-
// DecodeAllFiles resolves files at the filesystem matching the pattern, decoding JSON or YAML files. Supports multi-document files.
87+
// DecodeAllFiles resolves files at the filesystem matching the pattern, decoding JSON or YAML files. Supports multi-document files.
8788
// Falls back to the unstructured.Unstructured type if a matching type cannot be found for the Kind.
8889
// Options may be provided to configure the behavior of the decoder.
8990
func DecodeAllFiles(ctx context.Context, fsys fs.FS, pattern string, options ...DecodeOption) ([]k8s.Object, error) {
@@ -140,7 +141,7 @@ func DecodeEach(ctx context.Context, manifest io.Reader, handlerFn HandlerFunc,
140141
return nil
141142
}
142143

143-
// DecodeAll is a stream of documents of any Kind using either the innate typing of the scheme.
144+
// DecodeAll is a stream of documents of any Kind using either the innate typing of the scheme.
144145
// Falls back to the unstructured.Unstructured type if a matching type cannot be found for the Kind.
145146
// Options may be provided to configure the behavior of the decoder.
146147
func DecodeAll(ctx context.Context, manifest io.Reader, options ...DecodeOption) ([]k8s.Object, error) {
@@ -217,6 +218,18 @@ func DecodeFile(fsys fs.FS, manifestPath string, obj k8s.Object, options ...Deco
217218
return Decode(f, obj, options...)
218219
}
219220

221+
// DecodeURL decodes a document from the URL of any Kind using either the innate typing of the scheme.
222+
// Falls back to the unstructured.Unstructured type if a matching type cannot be found for the Kind.
223+
func DecodeURL(ctx context.Context, url string, handlerFn HandlerFunc, options ...DecodeOption) error {
224+
resp := http.Get(url).Do()
225+
if resp.Err() != nil {
226+
return resp.Err()
227+
}
228+
defer resp.Body().Close()
229+
230+
return DecodeEach(ctx, resp.Body(), handlerFn, options...)
231+
}
232+
220233
// DecodeString decodes a single-document YAML or JSON string into the provided object. Patches are applied
221234
// after decoding to the object to update the loaded resource.
222235
func DecodeString(rawManifest string, obj k8s.Object, options ...DecodeOption) error {

klient/decoder/decoder_test.go

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,10 @@ package decoder_test
1818

1919
import (
2020
"context"
21+
_ "embed"
2122
"fmt"
23+
"net/http"
24+
"net/http/httptest"
2225
"os"
2326
"path/filepath"
2427
"testing"
@@ -38,6 +41,9 @@ const (
3841
serviceAccountPrefix = "example-sa*"
3942
)
4043

44+
//go:embed testdata/example-multidoc-1.yaml
45+
var testDataExampleMultiDoc string
46+
4147
func TestDecode(t *testing.T) {
4248
testYAML := filepath.Join("testdata", "example-configmap-1.yaml")
4349
f, err := os.Open(testYAML)
@@ -217,6 +223,33 @@ func TestDecodeEach(t *testing.T) {
217223
}
218224
}
219225

226+
func TestDecodeURL(t *testing.T) {
227+
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
228+
fmt.Fprint(w, testDataExampleMultiDoc)
229+
}))
230+
defer ts.Close()
231+
t.Run("Testing decode with URL", func(t *testing.T) {
232+
count := 0
233+
err := decoder.DecodeURL(context.TODO(), ts.URL, func(ctx context.Context, obj k8s.Object) error {
234+
count++
235+
switch cfg := obj.(type) {
236+
case *v1.ConfigMap:
237+
if _, ok := cfg.Data["foo"]; !ok {
238+
t.Fatalf("expected key 'foo' in ConfigMap.Data, got: %v", cfg.Data)
239+
}
240+
default:
241+
t.Fatalf("unexpected type returned not ConfigMap: %T", cfg)
242+
}
243+
return nil
244+
})
245+
if err != nil {
246+
t.Fatal(err)
247+
} else if count != 2 {
248+
t.Fatalf("expected 2 documents, got: %d", count)
249+
}
250+
})
251+
}
252+
220253
func TestDecodeAll(t *testing.T) {
221254
for _, file := range []string{"example-multidoc-1.yaml", "example-multidoc-emptyitemcomment.yaml"} {
222255
t.Run(fmt.Sprintf("Testing multi doc with %s", file), func(t *testing.T) {

0 commit comments

Comments
 (0)