Skip to content

Commit

Permalink
Expose two Read methods in gktest (#1651)
Browse files Browse the repository at this point in the history
I have an opportunity to re-use code made written in gktest, as long as
the necessary functions are public.  This PR makes the following two
functions in the gktest package public.  They are (with their newly
capitalized names):

- ReadTemplate()
- ReadObject()

Signed-off-by: juliankatz <[email protected]>
  • Loading branch information
julianKatz authored Nov 16, 2021
1 parent 3ba8e93 commit 5a2b027
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 11 deletions.
17 changes: 7 additions & 10 deletions pkg/gktest/read_constraints.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"fmt"
"io/fs"
"strings"
"sync"

templatesv1 "github.com/open-policy-agent/frameworks/constraint/pkg/apis/templates/v1"
"github.com/open-policy-agent/frameworks/constraint/pkg/core/templates"
Expand Down Expand Up @@ -72,13 +71,10 @@ func readUnstructured(bytes []byte) (*unstructured.Unstructured, error) {
return u, nil
}

// TODO(willbeason): Remove once ToVersionless() is threadsafe.
var versionlessMtx sync.Mutex

// readTemplate reads the contents of the path and returns the
// ReadTemplate reads the contents of the path and returns the
// ConstraintTemplate it defines. Returns an error if the file does not define
// a ConstraintTemplate.
func readTemplate(scheme *runtime.Scheme, f fs.FS, path string) (*templates.ConstraintTemplate, error) {
func ReadTemplate(scheme *runtime.Scheme, f fs.FS, path string) (*templates.ConstraintTemplate, error) {
bytes, err := fs.ReadFile(f, path)
if err != nil {
return nil, fmt.Errorf("reading ConstraintTemplate from %q: %w", path, err)
Expand Down Expand Up @@ -118,9 +114,7 @@ func readTemplate(scheme *runtime.Scheme, f fs.FS, path string) (*templates.Cons
return nil, fmt.Errorf("%w: %T", ErrConvertingTemplate, t)
}

versionlessMtx.Lock()
template, err := v.ToVersionless()
versionlessMtx.Unlock()
if err != nil {
// This shouldn't happen unless there's a bug in the conversion functions.
// Most likely it means the conversion functions weren't generated.
Expand All @@ -130,7 +124,10 @@ func readTemplate(scheme *runtime.Scheme, f fs.FS, path string) (*templates.Cons
return template, nil
}

func readObject(f fs.FS, path string) (*unstructured.Unstructured, error) {
// ReadObject reads a file from the filesystem abstraction at the specified
// path, and returns an unstructured.Unstructured object if the file can be
// successfully unmarshalled.
func ReadObject(f fs.FS, path string) (*unstructured.Unstructured, error) {
bytes, err := fs.ReadFile(f, path)
if err != nil {
return nil, fmt.Errorf("reading Constraint from %q: %w", path, err)
Expand All @@ -145,7 +142,7 @@ func readObject(f fs.FS, path string) (*unstructured.Unstructured, error) {
}

func readConstraint(f fs.FS, path string) (*unstructured.Unstructured, error) {
u, err := readObject(f, path)
u, err := ReadObject(f, path)
if err != nil {
return nil, err
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/gktest/runner.go
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,7 @@ func (r *Runner) addTemplate(ctx context.Context, suiteDir, templatePath string,
return fmt.Errorf("%w: missing template", ErrInvalidSuite)
}

template, err := readTemplate(r.scheme, r.filesystem, path.Join(suiteDir, templatePath))
template, err := ReadTemplate(r.scheme, r.filesystem, path.Join(suiteDir, templatePath))
if err != nil {
return err
}
Expand Down

0 comments on commit 5a2b027

Please sign in to comment.