something is going on

This commit is contained in:
Nikolai Rodionov
2023-11-05 13:03:35 +01:00
parent 7b327b38e7
commit d469a758f5
17 changed files with 249 additions and 35 deletions

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

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

@ -1,14 +0,0 @@
package pkgdef
type Package struct {
Name string
Version string
AppVersion string
Image *Image
}
type Image struct {
Repository string
Tag string
}

View File

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

View File

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