Skip to content

Commit 7df0dd0

Browse files
committed
fix: refactor to proces resources while streaming stdin
1 parent 0c12419 commit 7df0dd0

File tree

2 files changed

+20
-18
lines changed

2 files changed

+20
-18
lines changed

README.md

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -27,13 +27,13 @@ docker pull ghcr.io/doodlescheduling/xunpack:v0
2727

2828
## Arguments
2929

30-
| Flag | Env | Default | Description |
30+
| Flag | Short | Env | Default | Description |
3131
| ------------- | ------------- | ------------- | ------------- |
32-
| `--file` | `IFILE` | `/dev/stdin` | Path to input |
33-
| `--workers` | `WORKERS` | `Number of CPU cores` | Number of workers to process the manifest |
34-
| `--fail-fast` | `FAIL_FAST` | `false` | Exit early if an error occured |
35-
| `--allow-failure` | `ALLOW_FAILURE` | `false` | Do not exit > 0 if an error occured |
36-
| `--output` | `OUTPUT` | `/dev/stdout` | Path to output file |
32+
| `--file` | `-f` | `IFILE` | `/dev/stdin` | Path to input |
33+
| `--workers` | `` | `WORKERS` | `Number of CPU cores` | Number of workers to process the manifest |
34+
| `--fail-fast` | `` | `FAIL_FAST` | `false` | Exit early if an error occured |
35+
| `--allow-failure` | `` | `ALLOW_FAILURE` | `false` | Do not exit > 0 if an error occured |
36+
| `--output` | `-o` | `OUTPUT` | `/dev/stdout` | Path to output file |
3737

3838

3939
## Github Action
@@ -59,7 +59,7 @@ jobs:
5959
- uses: docker://ghcr.io/doodlescheduling/xunpack:v0
6060
env:
6161
PATHS: ./${{ matrix.cluster }}
62-
OUTPUT: /dev/null
62+
OUTPUT: build.yaml
6363
```
6464
6565
### Advanced example

internal/parser/parser.go

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package parser
22

33
import (
44
"archive/tar"
5+
"bufio"
56
"context"
67
"io"
78
"os"
@@ -18,6 +19,7 @@ import (
1819
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
1920
"k8s.io/apimachinery/pkg/runtime"
2021
"k8s.io/apimachinery/pkg/runtime/schema"
22+
utilyaml "k8s.io/apimachinery/pkg/util/yaml"
2123
"k8s.io/cli-runtime/pkg/printers"
2224
)
2325

@@ -77,20 +79,21 @@ func (p *Parser) Run(ctx context.Context, in io.Reader) error {
7779
}
7880
}))
7981

80-
manifest, err := io.ReadAll(in)
81-
if err != nil {
82-
return err
83-
}
82+
multidocReader := utilyaml.NewYAMLReader(bufio.NewReader(in))
8483

85-
for _, resourceYAML := range strings.Split(string(manifest), "---") {
86-
r := resourceYAML
87-
pool.Push(worker.Task(func(ctx context.Context) error {
88-
if len(resourceYAML) == 0 {
89-
return nil
84+
for {
85+
resourceYAML, err := multidocReader.Read()
86+
if err != nil {
87+
if err == io.EOF {
88+
break
9089
}
9190

91+
return err
92+
}
93+
94+
pool.Push(worker.Task(func(ctx context.Context) error {
9295
obj, gvk, err := p.Decoder.Decode(
93-
[]byte(r),
96+
resourceYAML,
9497
nil,
9598
nil)
9699
if err != nil {
@@ -99,7 +102,6 @@ func (p *Parser) Run(ctx context.Context, in io.Reader) error {
99102

100103
return p.handleResource(obj, gvk, objects)
101104
}))
102-
103105
}
104106

105107
p.exit(pool)

0 commit comments

Comments
 (0)