From d469a758f5e0c34df65a9bc1534a73d1011fa750 Mon Sep 17 00:00:00 2001 From: Nikolai Rodionov Date: Sun, 5 Nov 2023 13:03:35 +0100 Subject: [PATCH] something is going on --- README.md | 2 +- example/db-operator/package.yaml | 6 ++- example/db-operator/workload/workload.yaml | 19 ++----- example/vaultwarden/bundle.yaml | 8 +++ go.mod | 7 ++- go.sum | 10 ++++ internal/bundler/bundler.go | 3 ++ internal/controller/controller.go | 6 ++- pkg/types/bundle/bundle.go | 50 +++++++++++++++++ pkg/types/bundle/bundle_test.go | 62 ++++++++++++++++++++++ pkg/types/metadata/metadata.go | 13 +++++ pkg/types/pkgdef/pkfdef.go | 14 ----- pkg/types/workload/workload.go | 17 ++++++ pkg/types/workload/workload_test.go | 27 ++++++++++ test/test-bundle/bundle.yaml | 12 +++++ test/test-bundle/bundle/workload.yaml | 6 +++ utils/helpers/test.go | 22 ++++++++ 17 files changed, 249 insertions(+), 35 deletions(-) create mode 100644 example/vaultwarden/bundle.yaml create mode 100644 internal/bundler/bundler.go create mode 100644 pkg/types/bundle/bundle.go create mode 100644 pkg/types/bundle/bundle_test.go create mode 100644 pkg/types/metadata/metadata.go delete mode 100644 pkg/types/pkgdef/pkfdef.go create mode 100644 test/test-bundle/bundle.yaml create mode 100644 test/test-bundle/bundle/workload.yaml create mode 100644 utils/helpers/test.go diff --git a/README.md b/README.md index 04d5ca2..3d7b900 100644 --- a/README.md +++ b/README.md @@ -1,3 +1,3 @@ # giops -A templater for the gitops setup \ No newline at end of file +A templater for the gitops setup diff --git a/example/db-operator/package.yaml b/example/db-operator/package.yaml index 293acb5..2677255 100644 --- a/example/db-operator/package.yaml +++ b/example/db-operator/package.yaml @@ -1,5 +1,7 @@ -name: db-operator -version: 1.0.0 +metadata: + name: db-operator + version: 1.0.0 + image: tag: 1.15.3 repository: db-operator diff --git a/example/db-operator/workload/workload.yaml b/example/db-operator/workload/workload.yaml index 21ccf47..ff45381 100644 --- a/example/db-operator/workload/workload.yaml +++ b/example/db-operator/workload/workload.yaml @@ -2,19 +2,6 @@ workload: kind: Deployment replicas: 1 containers: - - name: controller - image: {{ .Image }} - imagePullPolicy: Always - commands: - - sh - args: - - -c - - 'sleep 1000' - - name: ubuntu - image: ubuntu:latest - imagePullPolicy: Always - commands: - - sh - args: - - -c - - 'sleep 1000' + - name: controller + image: {{ .Image }} + imagePullPolicy: Always \ No newline at end of file diff --git a/example/vaultwarden/bundle.yaml b/example/vaultwarden/bundle.yaml new file mode 100644 index 0000000..f2473d0 --- /dev/null +++ b/example/vaultwarden/bundle.yaml @@ -0,0 +1,8 @@ +metadata: + name: vaultwarden + version: 1.0.0 + +spec: + image: + repository: registry.hub.docker.com/vaultwarden/server + tag: 1.29.2 diff --git a/go.mod b/go.mod index 6179ffb..33ecf54 100644 --- a/go.mod +++ b/go.mod @@ -2,10 +2,15 @@ module git.badhouseplants.net/allanger/shoebill go 1.21.3 -require github.com/stretchr/testify v1.8.4 +require ( + github.com/sirupsen/logrus v1.9.3 + github.com/stretchr/testify v1.8.4 + gopkg.in/yaml.v2 v2.4.0 +) require ( github.com/davecgh/go-spew v1.1.1 // indirect github.com/pmezard/go-difflib v1.0.0 // indirect + golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8 // indirect gopkg.in/yaml.v3 v3.0.1 // indirect ) diff --git a/go.sum b/go.sum index fa4b6e6..9c3ca1e 100644 --- a/go.sum +++ b/go.sum @@ -1,10 +1,20 @@ +github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= 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/sirupsen/logrus v1.9.3 h1:dueUQJ1C2q9oE3F7wvmSGAaVtTmUizReu6fjN8uqzbQ= +github.com/sirupsen/logrus v1.9.3/go.mod h1:naHLuLoDiP4jHNo9R0sCBMtWGeIprob74mVsIT4qYEQ= +github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= +github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcUk= github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo= +golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8 h1:0A+M6Uqn+Eje4kHMK80dtF3JCXC4ykBgQG4Fe06QRhQ= +golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= +gopkg.in/yaml.v2 v2.4.0 h1:D8xgwECY7CYvx+Y2n4sBz93Jn9JRvxdiyyo8CTfuKaY= +gopkg.in/yaml.v2 v2.4.0/go.mod h1:RDklbk79AGWmwhnvt/jBztapEOGDOx6ZbXqjP6csGnQ= +gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= diff --git a/internal/bundler/bundler.go b/internal/bundler/bundler.go new file mode 100644 index 0000000..8e97258 --- /dev/null +++ b/internal/bundler/bundler.go @@ -0,0 +1,3 @@ +package bundler + +func Build() \ No newline at end of file diff --git a/internal/controller/controller.go b/internal/controller/controller.go index c1d925d..6f3ef8d 100644 --- a/internal/controller/controller.go +++ b/internal/controller/controller.go @@ -4,6 +4,10 @@ type Client struct {} func (cli *Client) Install () error { // Generate the package from templates - + return nil } + +func (cli *Client) Tempalte() error { + return nil +} \ No newline at end of file diff --git a/pkg/types/bundle/bundle.go b/pkg/types/bundle/bundle.go new file mode 100644 index 0000000..fd317b5 --- /dev/null +++ b/pkg/types/bundle/bundle.go @@ -0,0 +1,50 @@ +// It's called bundle to avoid a conflict with the GO package +package bundle + +import ( + "fmt" + "os" + "path/filepath" + + "git.badhouseplants.net/allanger/shoebill/pkg/types/metadata" + "git.badhouseplants.net/allanger/shoebill/pkg/types/workload" + "github.com/sirupsen/logrus" + "gopkg.in/yaml.v2" +) + +type Bundle struct { + // A bundle's metadata + Metadata *metadata.Metadata `yaml:"metadata"` + BundlePath string `yaml:"-"` + Workload *workload.Workload `yaml:"-"` +} + +type Image struct { + Repository string + Tag string +} + +// Init a new bundle struct from the file +func ReadFromFile(filePath string) (*Bundle, error) { + var bundle Bundle + logrus.Infof("readig the bundle file: %s", filePath) + bundleFile, err := os.ReadFile(filePath) + if err != nil { + return nil, err + } + if err := yaml.Unmarshal(bundleFile, &bundle); err != nil { + return nil, err + } + bundle.BundlePath = filepath.Dir(filePath) + return &bundle, nil +} + +func (bundle *Bundle) Bootstrap() (err error) { + // TODO: It's ugly as hell + workloadFilePath := fmt.Sprintf("%s/bundle/workload.yaml", bundle.BundlePath) + bundle.Workload, err = workload.ReadFromFile(workloadFilePath) + if err != nil { + return + } + return nil +} diff --git a/pkg/types/bundle/bundle_test.go b/pkg/types/bundle/bundle_test.go new file mode 100644 index 0000000..9e953b0 --- /dev/null +++ b/pkg/types/bundle/bundle_test.go @@ -0,0 +1,62 @@ +package bundle_test + +import ( + "os" + "testing" + + "git.badhouseplants.net/allanger/shoebill/pkg/types/bundle" + "git.badhouseplants.net/allanger/shoebill/utils/helpers" + "github.com/stretchr/testify/assert" +) + +// Test that the file can be read +func TestUnitReadValidFile(t *testing.T) { + f := helpers.CreateFile(t) + defer os.Remove(f.Name()) + + const validFile = `--- +metadata: + name: db-operator + version: 1.0.0 + maintainer: + name: allanger + email: allanger@badhouseplants.net + website: https://badhouseplants.net +` + helpers.FillFile(t, f, validFile) + + bundle, err := bundle.ReadFromFile(f.Name()) + + // Check that there is no unexpected error + assert.NoError(t, err) + + // Check the bundle metadata + assert.Equal(t, "1.0.0", bundle.Metadata.Version) + assert.Equal(t, "db-operator", bundle.Metadata.Name) + + assert.Equal(t, "allanger", bundle.Metadata.Maintainer.Name) + assert.Equal(t, "allanger@badhouseplants.net", bundle.Metadata.Maintainer.Email) + assert.Equal(t, "https://badhouseplants.net", bundle.Metadata.Maintainer.Website) +} + +func TestIntegrationBootstrap(t *testing.T) { + bundlePath := "../../../test/test-bundle/bundle.yaml" + bundle, err := bundle.ReadFromFile(bundlePath) + + // Check that there is no unexpected error + assert.NoError(t, err) + + // Check the bundle file in the test folder to get the actual data + assert.Equal(t, "0.0.1", bundle.Metadata.Version) + assert.Equal(t, "test-bundle", bundle.Metadata.Name) + + assert.Equal(t, "allanger", bundle.Metadata.Maintainer.Name) + assert.Equal(t, "allanger@badhouseplants.net", bundle.Metadata.Maintainer.Email) + assert.Equal(t, "https://badhouseplants.net", bundle.Metadata.Maintainer.Website) + + err = bundle.Bootstrap() + assert.NoError(t, err) + + assert.Equal(t, "Deployment", bundle.Workload.Kind) + assert.Equal(t, 1, bundle.Workload.Replicas) +} diff --git a/pkg/types/metadata/metadata.go b/pkg/types/metadata/metadata.go new file mode 100644 index 0000000..05162c6 --- /dev/null +++ b/pkg/types/metadata/metadata.go @@ -0,0 +1,13 @@ +package metadata + +type Metadata struct { + Name string `yaml:"name"` // -- A name of the bundle + Version string `yaml:"version"` // -- The bundle's version + Maintainer Maintainer `yaml:"maintainer"` // -- A main maintainer of the package +} + +type Maintainer struct { + Name string `yaml:"name"` // -- Maintainer's name + Email string `yaml:"email"` // -- Maintainer's email + Website string `yaml:"website"` // -- Maintainer's website +} diff --git a/pkg/types/pkgdef/pkfdef.go b/pkg/types/pkgdef/pkfdef.go deleted file mode 100644 index 42a8e79..0000000 --- a/pkg/types/pkgdef/pkfdef.go +++ /dev/null @@ -1,14 +0,0 @@ -package pkgdef - - -type Package struct { - Name string - Version string - AppVersion string - Image *Image -} - -type Image struct { - Repository string - Tag string -} diff --git a/pkg/types/workload/workload.go b/pkg/types/workload/workload.go index 461d573..00bb7e0 100644 --- a/pkg/types/workload/workload.go +++ b/pkg/types/workload/workload.go @@ -2,6 +2,10 @@ package workload import ( "fmt" + "os" + + "github.com/sirupsen/logrus" + "gopkg.in/yaml.v2" ) type Workload struct { @@ -9,6 +13,19 @@ type Workload struct { Replicas int } +func ReadFromFile(bundlePath string) (*Workload, error) { + var workload Workload + logrus.Infof("readig the workload file: %s", bundlePath) + bundleFile, err := os.ReadFile(bundlePath) + if err != nil { + return nil, err + } + if err := yaml.Unmarshal(bundleFile, &workload); err != nil { + return nil, err + } + return &workload, nil +} + func (wd *Workload) ValidateKind() error { if wd.Kind == "Deployment" || wd.Kind == "StatefulSet" || wd.Kind == "DaemonSet" { return nil diff --git a/pkg/types/workload/workload_test.go b/pkg/types/workload/workload_test.go index 3be7b6f..7337094 100644 --- a/pkg/types/workload/workload_test.go +++ b/pkg/types/workload/workload_test.go @@ -1,12 +1,39 @@ package workload_test import ( + "os" "testing" "git.badhouseplants.net/allanger/shoebill/pkg/types/workload" + "git.badhouseplants.net/allanger/shoebill/utils/helpers" "github.com/stretchr/testify/assert" ) +func TestUnitReadValidFile(t *testing.T) { + f := helpers.CreateFile(t) + defer os.Remove(f.Name()) + + const validFile = `--- +kind: Deployment +replicas: 1 +containers: + - name: controller + image: {{ .Image }} + imagePullPolicy: Always +` + helpers.FillFile(t, f, validFile) + + workload, err := workload.ReadFromFile(f.Name()) + + // Check that there is no unexpected error + assert.NoError(t, err) + + // Check the bundle metadata + assert.Equal(t, "Deployment", workload.Kind) + assert.Equal(t, 1, workload.Replicas) + +} + func TestUnitKindValidatorDeployment(t *testing.T) { wd := &workload.Workload{ Kind: "Deployment", diff --git a/test/test-bundle/bundle.yaml b/test/test-bundle/bundle.yaml new file mode 100644 index 0000000..e2c8f99 --- /dev/null +++ b/test/test-bundle/bundle.yaml @@ -0,0 +1,12 @@ +metadata: + name: test-bundle + version: 0.0.1 + maintainer: + name: allanger + email: allanger@badhouseplants.net + website: https://badhouseplants.net + +spec: + image: + repository: registry.hub.docker.com/vaultwarden/server + tag: 1.29.2 \ No newline at end of file diff --git a/test/test-bundle/bundle/workload.yaml b/test/test-bundle/bundle/workload.yaml new file mode 100644 index 0000000..bd28c1b --- /dev/null +++ b/test/test-bundle/bundle/workload.yaml @@ -0,0 +1,6 @@ +kind: Deployment +replicas: 1 +containers: + - name: controller + image: {{ .Image }} + imagePullPolicy: Always \ No newline at end of file diff --git a/utils/helpers/test.go b/utils/helpers/test.go new file mode 100644 index 0000000..e194b1d --- /dev/null +++ b/utils/helpers/test.go @@ -0,0 +1,22 @@ +package helpers + +import ( + "os" + "testing" +) + +func CreateFile(t *testing.T) *os.File { + f, err := os.CreateTemp("", "sample") + if err != nil { + t.Error(err) + } + t.Logf("file is created: %s", f.Name()) + return f +} + +func FillFile(t *testing.T, f *os.File, content string) { + _, err := f.WriteString(content) + if err != nil { + t.Error(err) + } +}