Skip to content

Commit f692635

Browse files
Add stdin and multidoc support to preflight. (#1114)
* add - url keyword for stdin * add basic multidoc support * filter on preflight kind * add e2e test for stdin
1 parent 39606f6 commit f692635

File tree

2 files changed

+52
-17
lines changed

2 files changed

+52
-17
lines changed

pkg/preflight/run.go

Lines changed: 45 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ import (
2727
spin "github.com/tj/go-spin"
2828
"go.opentelemetry.io/otel"
2929
"golang.org/x/sync/errgroup"
30+
"gopkg.in/yaml.v2"
3031
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
3132
"k8s.io/apimachinery/pkg/labels"
3233
"k8s.io/client-go/kubernetes/scheme"
@@ -75,6 +76,12 @@ func RunPreflights(interactive bool, output string, format string, args []string
7576
return err
7677
}
7778

79+
preflightContent = b
80+
} else if v == "-" {
81+
b, err := io.ReadAll(os.Stdin)
82+
if err != nil {
83+
return err
84+
}
7885
preflightContent = b
7986
} else {
8087
u, err := url.Parse(v)
@@ -118,27 +125,48 @@ func RunPreflights(interactive bool, output string, format string, args []string
118125
}
119126
}
120127

121-
preflightContent, err = docrewrite.ConvertToV1Beta2(preflightContent)
122-
if err != nil {
123-
return errors.Wrap(err, "failed to convert to v1beta2")
124-
}
128+
multidocs := strings.Split(string(preflightContent), "\n---\n")
125129

126-
troubleshootclientsetscheme.AddToScheme(scheme.Scheme)
127-
decode := scheme.Codecs.UniversalDeserializer().Decode
128-
obj, _, err := decode([]byte(preflightContent), nil, nil)
129-
if err != nil {
130-
return errors.Wrapf(err, "failed to parse %s", v)
131-
}
130+
for _, doc := range multidocs {
132131

133-
if spec, ok := obj.(*troubleshootv1beta2.Preflight); ok {
134-
if spec.Spec.UploadResultsTo == "" {
135-
preflightSpec = ConcatPreflightSpec(preflightSpec, spec)
136-
} else {
137-
uploadResultSpecs = append(uploadResultSpecs, spec)
132+
type documentHead struct {
133+
Kind string `yaml:"kind"`
134+
}
135+
136+
var parsedDocHead documentHead
137+
138+
err := yaml.Unmarshal([]byte(doc), &parsedDocHead)
139+
if err != nil {
140+
return errors.Wrap(err, "failed to parse yaml")
141+
}
142+
143+
if parsedDocHead.Kind != "Preflight" {
144+
continue
145+
}
146+
147+
preflightContent, err = docrewrite.ConvertToV1Beta2([]byte(doc))
148+
if err != nil {
149+
return errors.Wrap(err, "failed to convert to v1beta2")
150+
}
151+
152+
troubleshootclientsetscheme.AddToScheme(scheme.Scheme)
153+
decode := scheme.Codecs.UniversalDeserializer().Decode
154+
obj, _, err := decode([]byte(preflightContent), nil, nil)
155+
if err != nil {
156+
return errors.Wrapf(err, "failed to parse %s", v)
157+
}
158+
159+
if spec, ok := obj.(*troubleshootv1beta2.Preflight); ok {
160+
if spec.Spec.UploadResultsTo == "" {
161+
preflightSpec = ConcatPreflightSpec(preflightSpec, spec)
162+
} else {
163+
uploadResultSpecs = append(uploadResultSpecs, spec)
164+
}
165+
} else if spec, ok := obj.(*troubleshootv1beta2.HostPreflight); ok {
166+
hostPreflightSpec = ConcatHostPreflightSpec(hostPreflightSpec, spec)
138167
}
139-
} else if spec, ok := obj.(*troubleshootv1beta2.HostPreflight); ok {
140-
hostPreflightSpec = ConcatHostPreflightSpec(hostPreflightSpec, spec)
141168
}
169+
142170
}
143171

144172
var collectResults []CollectResult

test/validate-preflight-e2e.sh

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,13 @@ echo "Failed preflights found"
3333
EXIT_STATUS=1
3434
fi
3535

36+
# test stdin
37+
cat examples/preflight/e2e.yaml | ./bin/preflight --debug --interactive=false --format=json - > "$tmpdir/result.json"
38+
if [ $? -ne 0 ]; then
39+
echo "preflight command failed"
40+
exit $EXIT_STATUS
41+
fi
42+
3643
rm -rf "$tmpdir"
3744

3845
exit $EXIT_STATUS

0 commit comments

Comments
 (0)