Skip to content

Commit

Permalink
fix: refactor to proces resources while streaming stdin
Browse files Browse the repository at this point in the history
  • Loading branch information
raffis committed Jan 23, 2024
1 parent 0c12419 commit 7df0dd0
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 18 deletions.
14 changes: 7 additions & 7 deletions README.md
Expand Up @@ -27,13 +27,13 @@ docker pull ghcr.io/doodlescheduling/xunpack:v0

## Arguments

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


## Github Action
Expand All @@ -59,7 +59,7 @@ jobs:
- uses: docker://ghcr.io/doodlescheduling/xunpack:v0
env:
PATHS: ./${{ matrix.cluster }}
OUTPUT: /dev/null
OUTPUT: build.yaml
```

### Advanced example
Expand Down
24 changes: 13 additions & 11 deletions internal/parser/parser.go
Expand Up @@ -2,6 +2,7 @@ package parser

import (
"archive/tar"
"bufio"
"context"
"io"
"os"
Expand All @@ -18,6 +19,7 @@ import (
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/runtime/schema"
utilyaml "k8s.io/apimachinery/pkg/util/yaml"
"k8s.io/cli-runtime/pkg/printers"
)

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

manifest, err := io.ReadAll(in)
if err != nil {
return err
}
multidocReader := utilyaml.NewYAMLReader(bufio.NewReader(in))

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

return err
}

pool.Push(worker.Task(func(ctx context.Context) error {
obj, gvk, err := p.Decoder.Decode(
[]byte(r),
resourceYAML,
nil,
nil)
if err != nil {
Expand All @@ -99,7 +102,6 @@ func (p *Parser) Run(ctx context.Context, in io.Reader) error {

return p.handleResource(obj, gvk, objects)
}))

}

p.exit(pool)
Expand Down

0 comments on commit 7df0dd0

Please sign in to comment.