Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat: Support KRM input #170

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
25 changes: 24 additions & 1 deletion cmd/kubeconform/main.go
Original file line number Diff line number Diff line change
@@ -1,14 +1,17 @@
package main

import (
"bufio"
"context"
"fmt"
"log"
"os"
"runtime"
"runtime/pprof"
"strings"
"sync"

"github.com/GoogleContainerTools/kpt-functions-sdk/go/fn"
"github.com/yannh/kubeconform/pkg/config"
"github.com/yannh/kubeconform/pkg/output"
"github.com/yannh/kubeconform/pkg/resource"
Expand Down Expand Up @@ -46,6 +49,26 @@ func processResults(cancel context.CancelFunc, o output.Output, validationResult
return result
}

func parseStdin(stdin *os.File) *strings.Reader {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

would be awesome if this could take a reader and return a reader

var content string

// Read all stdin lines.
scanner := bufio.NewScanner(stdin)
for scanner.Scan() {
content += fmt.Sprintln(scanner.Text())
}

// Check if the stdin input is ResourceList.
rl, err := fn.ParseResourceList([]byte(content))
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This adds support for ResourceLists (I need to read about this) only if passed via stdin? This would not work if I ran kubeconform ./fixtures/krm.yaml? I think we would need some consistency there

// If it's ResourceList, use the items as the new content.
if err == nil {
content = rl.Items.String()
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's one part I'm worried about... there are some ridiculous optimisations to ensure we read stdin as a stream, and from previous tickets it looks like some people do indeed pass very large amounts of data via stdin... if we were to do that, this function would have to operate on a stream without buffering the whole thing

}

// Convert the content which is type "string" to a "Reader" again.
return strings.NewReader(content)
}

func realMain() int {
cfg, out, err := config.FromFlags(os.Args[0], os.Args[1:])
if out != "" {
Expand Down Expand Up @@ -121,7 +144,7 @@ func realMain() int {
var resourcesChan <-chan resource.Resource
var errors <-chan error
if useStdin {
resourcesChan, errors = resource.FromStream(ctx, "stdin", os.Stdin)
resourcesChan, errors = resource.FromStream(ctx, "stdin", parseStdin(os.Stdin))
} else {
resourcesChan, errors = resource.FromFiles(ctx, cfg.Files, cfg.IgnoreFilenamePatterns)
}
Expand Down
17 changes: 17 additions & 0 deletions fixtures/krm.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
apiVersion: config.kubernetes.io/v1
kind: ResourceList
metadata:
name: krm-function-input
items:
- apiVersion: v1
kind: ConfigMap
metadata:
name: foo
data:
val: foo
- apiVersion: v1
kind: ConfigMap
metadata:
name: bar
data:
val: bar
36 changes: 33 additions & 3 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,40 @@ module github.com/yannh/kubeconform
go 1.17

require (
github.com/beevik/etree v1.1.0
github.com/GoogleContainerTools/kpt-functions-sdk/go/fn v0.0.0-20221109010843-1f7d0c07a381
github.com/xeipuuv/gojsonschema v1.2.0
sigs.k8s.io/yaml v1.3.0
)

require (
github.com/GoogleContainerTools/kpt-functions-sdk/go/api v0.0.0-20220720212527-133180134b93 // indirect
github.com/PuerkitoBio/purell v1.1.1 // indirect
github.com/PuerkitoBio/urlesc v0.0.0-20170810143723-de5bf2ad4578 // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/go-errors/errors v1.0.1 // indirect
github.com/go-logr/logr v1.2.0 // indirect
github.com/go-openapi/jsonpointer v0.19.5 // indirect
github.com/go-openapi/jsonreference v0.19.6 // indirect
github.com/go-openapi/swag v0.21.1 // indirect
github.com/gogo/protobuf v1.3.2 // indirect
github.com/golang/protobuf v1.5.2 // indirect
github.com/google/gnostic v0.5.7-v3refs // indirect
github.com/josharian/intern v1.0.0 // indirect
github.com/mailru/easyjson v0.7.7 // indirect
github.com/monochromegane/go-gitignore v0.0.0-20200626010858-205db1a8cc00 // indirect
github.com/pkg/errors v0.9.1 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/stretchr/testify v1.8.0 // indirect
github.com/xeipuuv/gojsonpointer v0.0.0-20190905194746-02993c407bfb // indirect
github.com/xeipuuv/gojsonreference v0.0.0-20180127040603-bd5ef7bd5415 // indirect
github.com/xeipuuv/gojsonschema v1.2.0
github.com/xlab/treeprint v1.1.0 // indirect
golang.org/x/net v0.0.0-20220722155237-a158d28d115b // indirect
golang.org/x/text v0.3.7 // indirect
google.golang.org/protobuf v1.28.0 // indirect
gopkg.in/yaml.v2 v2.4.0 // indirect
sigs.k8s.io/yaml v1.2.0
gopkg.in/yaml.v3 v3.0.1 // indirect
k8s.io/apimachinery v0.24.0 // indirect
k8s.io/klog/v2 v2.60.1 // indirect
k8s.io/kube-openapi v0.0.0-20220401212409-b28bf2818661 // indirect
sigs.k8s.io/kustomize/kyaml v0.13.7-0.20220418212550-9d5491c2e20c // indirect
)
714 changes: 709 additions & 5 deletions go.sum

Large diffs are not rendered by default.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading