Compare commits

...

2 Commits

Author SHA1 Message Date
Nikolai Rodionov
d469a758f5
something is going on 2023-11-05 13:03:35 +01:00
Nikolai Rodionov
7b327b38e7
WIP: something 2023-11-03 14:22:22 +01:00
19 changed files with 358 additions and 1 deletions

View File

@ -1,3 +1,3 @@
# giops # giops
A templater for the gitops setup A templater for the gitops setup

View File

@ -0,0 +1,7 @@
metadata:
name: db-operator
version: 1.0.0
image:
tag: 1.15.3
repository: db-operator

View File

@ -0,0 +1,7 @@
workload:
kind: Deployment
replicas: 1
containers:
- name: controller
image: {{ .Image }}
imagePullPolicy: Always

View File

@ -0,0 +1,8 @@
metadata:
name: vaultwarden
version: 1.0.0
spec:
image:
repository: registry.hub.docker.com/vaultwarden/server
tag: 1.29.2

16
go.mod Normal file
View File

@ -0,0 +1,16 @@
module git.badhouseplants.net/allanger/shoebill
go 1.21.3
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
)

20
go.sum Normal file
View File

@ -0,0 +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=

View File

@ -0,0 +1,3 @@
package bundler
func Build()

View File

@ -0,0 +1,13 @@
package controller
type Client struct {}
func (cli *Client) Install () error {
// Generate the package from templates
return nil
}
func (cli *Client) Tempalte() error {
return nil
}

View File

@ -0,0 +1,9 @@
package templater
// Datasource for template rendering
type TemplateDS struct {
}
func (tds *TemplateDS) Render() {
}

View File

@ -0,0 +1 @@
package templater_test

View File

@ -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
}

View File

@ -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)
}

6
pkg/types/image/image.go Normal file
View File

@ -0,0 +1,6 @@
package image
type Image struct {
Repository string
Tag string
}

View File

@ -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
}

View File

@ -0,0 +1,35 @@
package workload
import (
"fmt"
"os"
"github.com/sirupsen/logrus"
"gopkg.in/yaml.v2"
)
type Workload struct {
Kind string
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
}
err := fmt.Errorf("kind is not valid, expect Deployment|StatefulSet|DaemonSet, got %s", wd.Kind)
return err
}

View File

@ -0,0 +1,67 @@
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",
}
err := wd.ValidateKind()
assert.NoError(t, err)
}
func TestUnitKindValidatorStatefulSet(t *testing.T) {
wd := &workload.Workload{
Kind: "StatefulSet",
}
err := wd.ValidateKind()
assert.NoError(t, err)
}
func TestUnitKindValidatorDeamonSet(t *testing.T) {
wd := &workload.Workload{
Kind: "DaemonSet",
}
err := wd.ValidateKind()
assert.NoError(t, err)
}
func TestUnitKindValidatorInvalid(t *testing.T) {
wd := &workload.Workload{
Kind: "Invalid",
}
err := wd.ValidateKind()
assert.ErrorContains(t, err, "got Invalid")
}

View File

@ -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

View File

@ -0,0 +1,6 @@
kind: Deployment
replicas: 1
containers:
- name: controller
image: {{ .Image }}
imagePullPolicy: Always

22
utils/helpers/test.go Normal file
View File

@ -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)
}
}