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

Get rid of statik and fix failing tests #4887

Merged
merged 1 commit into from
Nov 24, 2022
Merged
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
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 0 additions & 3 deletions Makefile-tools.mk
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,6 @@ $(MYGOBIN)/mdtogo:
$(MYGOBIN)/addlicense:
go install github.com/google/addlicense@latest

$(MYGOBIN)/statik:
go install github.com/rakyll/statik@latest

$(MYGOBIN)/goreleaser:
go install github.com/goreleaser/[email protected] # https://github.com/kubernetes-sigs/kustomize/issues/4542

Expand Down
16 changes: 5 additions & 11 deletions cmd/pluginator/Makefile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
.PHONY: statik test all clean generate run
.PHONY: test all clean generate run

FUNC_WRAPPER_SRC_DIR = funcwrappersrc
FUNC_WRAPPER_DST_DIR = funcwrapper
Expand All @@ -7,22 +7,16 @@ include ../../Makefile-modules.mk

all: test build

test: generate
test:
go test -v ./...

generate: $(MYGOBIN)/statik
( \
cd internal/krmfunction; \
$(MYGOBIN)/statik -src=$(FUNC_WRAPPER_SRC_DIR) -f -p $(FUNC_WRAPPER_DST_DIR) -include=main.go,go.mod.src \
)

build: generate
build:
go build -o $(MYGOBIN) main.go

install: generate
install:
go install .

run: generate
run:
go run . $(ARGS)

clean:
Expand Down
1 change: 0 additions & 1 deletion cmd/pluginator/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ module sigs.k8s.io/kustomize/cmd/pluginator/v2
go 1.19

require (
github.com/rakyll/statik v0.1.7
github.com/spf13/cobra v1.4.0
github.com/stretchr/testify v1.8.0
sigs.k8s.io/kustomize/api v0.12.1
Expand Down
2 changes: 0 additions & 2 deletions cmd/pluginator/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -93,8 +93,6 @@ github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINE
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/prometheus/client_model v0.0.0-20190812154241-14fe0d1b01d4/go.mod h1:xMI15A0UPsDsEKsMN9yxemIoYk6Tm2C1GtYGdfGttqA=
github.com/rakyll/statik v0.1.7 h1:OF3QCZUuyPxuGEP7B4ypUa7sB/iHtqOTDYZXGM8KOdQ=
github.com/rakyll/statik v0.1.7/go.mod h1:AlZONWzMtEnMs7W4e/1LURLiI49pIMmp6V9Unghqrcc=
github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=
github.com/sergi/go-diff v1.1.0 h1:we8PVUC3FE2uYfodKH/nBHMSetSfHDR6scGdBi+erh0=
github.com/spf13/afero v1.2.2/go.mod h1:9ZxEEn6pIJ8Rxe320qSDBk6AsU0r9pR7Q4OcevTdifk=
Expand Down
15 changes: 6 additions & 9 deletions cmd/pluginator/internal/krmfunction/converter.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,18 @@ package krmfunction
import (
"bufio"
"bytes"
"embed"
"fmt"
"io"
"os"
"path/filepath"
"strings"

"github.com/rakyll/statik/fs"
// load embedded func wrapper
_ "sigs.k8s.io/kustomize/cmd/pluginator/v2/internal/krmfunction/funcwrapper"
)

//go:embed funcwrappersrc/go.mod.src
//go:embed funcwrappersrc/main.go
var fs embed.FS

// Converter is a converter to convert the
// plugin file to KRM function
type Converter struct {
Expand Down Expand Up @@ -122,11 +123,7 @@ func (c *Converter) prepareWrapper(content string) string {
// readEmbeddedFile read the file from embedded files with filename
// name. Return the file content if it's successful.
func (c *Converter) readEmbeddedFile(name string) (string, error) {
statikFS, err := fs.New()
if err != nil {
return "", err
}
r, err := statikFS.Open("/" + name)
r, err := fs.Open("funcwrappersrc/" + name)
if err != nil {
return "", err
}
Expand Down
33 changes: 19 additions & 14 deletions cmd/pluginator/internal/krmfunction/converter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ func (p *plugin) Transform(rm resmap.ResMap) error {
return nil
}
for _, r := range rm.Resources() {
if r.IsEmpty() {
if r.IsNilOrEmpty() {
// Don't mutate empty objects?
continue
}
Expand Down Expand Up @@ -97,23 +97,26 @@ items:

func runKrmFunction(t *testing.T, input []byte, dir string) []byte {
t.Helper()
cmd := exec.Command("go", "run", ".")
ib := bytes.NewReader(input)
cmd.Stdin = ib
ob := bytes.NewBuffer([]byte{})
cmd.Stdout = ob
eb := bytes.NewBuffer([]byte{})
cmd.Stderr = eb
cmd.Dir = dir
err := cmd.Run()
if !assert.NoErrorf(t, err, "Stdout:\n%s\nStderr:\n%s\n", ob.String(), eb.String()) {
t.FailNow()
prepareCmd := func(name string, arg ...string) (*exec.Cmd, *bytes.Buffer, *bytes.Buffer) {
ob := bytes.NewBuffer([]byte{})
eb := bytes.NewBuffer([]byte{})
cmd := exec.Command(name, arg...)
cmd.Stdout = ob
cmd.Stderr = eb
cmd.Dir = dir
return cmd, ob, eb
}
cmd, ob, eb := prepareCmd("go", "mod", "tidy")
require.NoErrorf(t, cmd.Run(), "Stdout:\n%s\nStderr:\n%s\n", ob.String(), eb.String())

cmd, ob, eb = prepareCmd("go", "run", ".")
cmd.Stdin = bytes.NewReader(input)
require.NoErrorf(t, cmd.Run(), "Stdout:\n%s\nStderr:\n%s\n", ob.String(), eb.String())

return ob.Bytes()
}

func TestTransformerConverter(t *testing.T) {
t.Skip("TODO: fix this test, which was not running in CI and does not pass")
dir := t.TempDir()

err := os.WriteFile(filepath.Join(dir, "Plugin.go"),
Expand Down Expand Up @@ -211,7 +214,6 @@ items: []
}

func TestGeneratorConverter(t *testing.T) {
t.Skip("TODO: fix this test, which was not running in CI and does not pass")
dir := t.TempDir()

err := os.WriteFile(filepath.Join(dir, "Plugin.go"),
Expand All @@ -231,6 +233,9 @@ items:
kind: ConfigMap
metadata:
name: staging
annotations:
internal.config.kubernetes.io/generatorBehavior: unspecified
internal.config.kubernetes.io/needsHashSuffix: enabled
functionConfig:
apiVersion: foo-corp.com/v1
kind: FulfillmentCenter
Expand Down
14 changes: 0 additions & 14 deletions cmd/pluginator/internal/krmfunction/funcwrapper/statik.go

This file was deleted.

8 changes: 4 additions & 4 deletions cmd/pluginator/internal/krmfunction/funcwrappersrc/go.mod.src
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
module main

go 1.15
go 1.19

require (
github.com/spf13/cobra v1.0.0
sigs.k8s.io/kustomize/api v0.6.4
sigs.k8s.io/kustomize/kyaml v0.9.3
github.com/spf13/cobra v1.4.0
sigs.k8s.io/kustomize/api v0.12.1
sigs.k8s.io/kustomize/kyaml v0.13.9
sigs.k8s.io/yaml v1.2.0
)