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

feature(kumactl) add install postgres-schema command #236

Merged
merged 4 commits into from
Sep 20, 2019
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
1 change: 1 addition & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -324,6 +324,7 @@ protoc/pkg/config/app/kumactl/v1alpha1:
# Notice that this command is not include into `make generate` by intention (since generated code differes between dev host and ci server)
generate/kumactl/install/control-plane:
go generate ./app/kumactl/pkg/install/k8s/control-plane/...
go generate ./app/kumactl/pkg/install/universal/control-plane/postgres/...

fmt: fmt/go fmt/proto ## Dev: Run various format tools

Expand Down
1 change: 1 addition & 0 deletions app/kumactl/cmd/install/install.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,6 @@ func NewInstallCmd(pctx *kumactl_cmd.RootContext) *cobra.Command {
}
// sub-commands
cmd.AddCommand(newInstallControlPlaneCmd(pctx))
cmd.AddCommand(newInstallDatabaseSchemaCmd())
return cmd
}
40 changes: 40 additions & 0 deletions app/kumactl/cmd/install/install_database_schema.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package install

import (
"github.com/Kong/kuma/app/kumactl/pkg/install/data"
postgres_schema "github.com/Kong/kuma/app/kumactl/pkg/install/universal/control-plane/postgres"
kuma_cmd "github.com/Kong/kuma/pkg/cmd"
"github.com/pkg/errors"
"github.com/spf13/cobra"
)

func newInstallDatabaseSchemaCmd() *cobra.Command {
args := struct {
target string
}{}
cmd := &cobra.Command{
Use: "database-schema",
Short: "Install Kuma schema on DB",
Long: `Install Kuma schema on DB.`,
Example: `1. kumactl install database-schema --target=postgres | PGPASSWORD=mysecretpassword psql -h localhost -U postgres
2. sql_file=$(mktemp) ; \
kumactl install database-schema --target=postgres >$sql_file ; \
psql --host=localhost --username=postgres --password --file=$sql_file ; \
rm $sql_file`,
RunE: func(cmd *cobra.Command, _ []string) error {
switch args.target {
case "postgres":
file, err := data.ReadFile(postgres_schema.Schema, "resource.sql")
if err != nil {
return errors.Wrap(err, "could not read schema file")
}
_, err = cmd.OutOrStdout().Write(file)
return err
default:
return errors.Errorf("unknown target type: %s", args.target)
}
},
}
cmd.Flags().StringVar(&args.target, "target", "postgres", kuma_cmd.UsageOptions("Database type", "postgres"))
return cmd
}
30 changes: 30 additions & 0 deletions app/kumactl/cmd/install/install_database_schema_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package install_test

import (
"bytes"
"github.com/Kong/kuma/app/kumactl/cmd"
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
"io/ioutil"
"path/filepath"
)

var _ = Describe("kumactl install database-schema", func() {
It("should give the schema postgres", func() {
// given
stdout := bytes.Buffer{}
rootCmd := cmd.DefaultRootCmd()
rootCmd.SetOut(&stdout)
rootCmd.SetArgs([]string{"install", "database-schema", "--target=postgres"})

expected, err := ioutil.ReadFile(filepath.Join("testdata", "postgres_schema.golden.sql"))
Expect(err).ToNot(HaveOccurred())

// when
err = rootCmd.Execute()

// then
Expect(err).ToNot(HaveOccurred())
Expect(string(stdout.Bytes())).To(Equal(string(expected)))
})
})
9 changes: 9 additions & 0 deletions app/kumactl/cmd/install/testdata/postgres_schema.golden.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
CREATE TABLE IF NOT EXISTS resources (
name varchar(100) NOT NULL,
namespace varchar(100) NOT NULL,
mesh varchar(100) NOT NULL,
type varchar(100) NOT NULL,
version integer NOT NULL,
spec text,
PRIMARY KEY (name, namespace, mesh, type)
);
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
CREATE TABLE IF NOT EXISTS resources (
name varchar(100) NOT NULL,
namespace varchar(100) NOT NULL,
mesh varchar(100) NOT NULL,
type varchar(100) NOT NULL,
version integer NOT NULL,
spec text,
PRIMARY KEY (name, namespace, mesh, type)
);
12 changes: 12 additions & 0 deletions app/kumactl/pkg/install/data/files.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,3 +39,15 @@ func ReadFiles(fs http.FileSystem) ([]File, error) {
}
return files, nil
}

func ReadFile(fs http.FileSystem, file string) (File, error) {
f, err := fs.Open(file)
if err != nil {
return nil, err
}
b, err := ioutil.ReadAll(f)
if err != nil {
return nil, err
}
return b, nil
}
28 changes: 14 additions & 14 deletions app/kumactl/pkg/install/k8s/control-plane/templates_vfsdata.go

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package postgres_test

import (
"testing"

. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
)

func TestControlPlane(t *testing.T) {
RegisterFailHandler(Fail)
RunSpecs(t, "Postgres Suite")
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package postgres

//go:generate go run github.com/shurcooL/vfsgen/cmd/vfsgendev -source="github.com/Kong/kuma/app/kumactl/pkg/install/universal/control-plane/postgres".Schema

import (
"path/filepath"
)

func SchemaDir(kumactlSrcDir string) string {
return filepath.Join(kumactlSrcDir, "data", "install", "universal", "control-plane", "postgres")
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
// +build dev

package postgres

import (
"net/http"
"path/filepath"
"runtime"
)

var Schema http.FileSystem = http.Dir(SchemaDir(kumactlSrcDir()))

func kumactlSrcDir() string {
_, thisFile, _, _ := runtime.Caller(1)

thisDir := filepath.Dir(thisFile)

return filepath.Join(thisDir, "..", "..", "..", "..", "..")
}

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

Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
// +build !dev

package postgres_test

import (
"github.com/Kong/kuma/app/kumactl/pkg/install/universal/control-plane/postgres"
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
"io/ioutil"
"path/filepath"
)

var _ = Describe("Schema file", func() {

It("generated Go code must be in sync with the original schema file", func() {
// given compiled file
file, err := postgres.Schema.Open("resource.sql")
Expect(err).ToNot(HaveOccurred())
expectedContents, err := ioutil.ReadAll(file)
Expect(err).ToNot(HaveOccurred())

// and actual file
kumactlSrcDir := filepath.Join("..", "..", "..", "..", "..")
schemaDir := postgres.SchemaDir(kumactlSrcDir)
actualContents, err := ioutil.ReadFile(filepath.Join(schemaDir, "resource.sql"))

// then both files are identical
Expect(err).ToNot(HaveOccurred())
Expect(string(actualContents)).To(Equal(string(expectedContents)), "generated Go code is no longer in sync with the original schema file. To re-generate it, run `make generate/kumactl/install/control-plane`")
})

})
Loading