Skip to content
This repository has been archived by the owner on Jun 12, 2020. It is now read-only.

Commit

Permalink
fix(state): corrected state handling, so that only necessary internal…
Browse files Browse the repository at this point in the history
… states are used
  • Loading branch information
stebenz committed Mar 3, 2020
1 parent 5700980 commit 59185c1
Show file tree
Hide file tree
Showing 13 changed files with 117 additions and 178 deletions.
89 changes: 36 additions & 53 deletions internal/bundle/bundle.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ type Bundle struct {
HelmTemplator templator.Templator
YamlTemplator templator.Templator
monitor mntr.Monitor
status error
}

func New(conf *config.Config) *Bundle {
Expand All @@ -44,36 +43,25 @@ func New(conf *config.Config) *Bundle {
HelmTemplator: helmTemplator,
YamlTemplator: yamlTemplator,
Applications: apps,
status: nil,
}
return b
}

func (b *Bundle) GetStatus() error {
return b.status
}
func (b *Bundle) CleanUp() error {

func (b *Bundle) CleanUp() *Bundle {
if b.GetStatus() != nil {
return b
err := b.HelmTemplator.CleanUp()
if err != nil {
return err
}

b.status = b.HelmTemplator.CleanUp().GetStatus()
if b.GetStatus() != nil {
return b
}
b.status = b.YamlTemplator.CleanUp().GetStatus()
return b
return b.YamlTemplator.CleanUp()
}

func (b *Bundle) GetApplications() map[name.Application]application.Application {
return b.Applications
}

func (b *Bundle) AddApplicationsByBundleName(name name.Bundle) error {
if err := b.GetStatus(); err != nil {
return err
}

names := bundles.Get(name)
if names == nil {
Expand All @@ -82,72 +70,61 @@ func (b *Bundle) AddApplicationsByBundleName(name name.Bundle) error {

bnew := b
for _, name := range names {
bnew = bnew.AddApplicationByName(name)
if err := bnew.GetStatus(); err != nil {
if err := bnew.AddApplicationByName(name); err != nil {
return err
}
}
return nil
}

func (b *Bundle) AddApplicationByName(appName name.Application) *Bundle {
if b.GetStatus() != nil {
return b
}
func (b *Bundle) AddApplicationByName(appName name.Application) error {

app := application.New(b.monitor, appName)
return b.AddApplication(app)
}

func (b *Bundle) AddApplication(app application.Application) *Bundle {
if b.GetStatus() != nil {
return b
}
func (b *Bundle) AddApplication(app application.Application) error {

if _, found := b.Applications[app.GetName()]; found {
b.status = errors.New("Application already in bundle")
return b
return errors.New("Application already in bundle")
}

b.Applications[app.GetName()] = app
return b
return nil
}

func (b *Bundle) Reconcile(currentResourceList []*clientgo.Resource, spec *v1beta1.ToolsetSpec) *Bundle {
if b.status != nil {
return b
}
func (b *Bundle) Reconcile(currentResourceList []*clientgo.Resource, spec *v1beta1.ToolsetSpec) error {

applicationCount := 0
// go through list of application until every application is reconciled
// and this orderNumber by orderNumber (default is 1)
for orderNumber := 0; applicationCount < len(b.Applications); orderNumber++ {
var wg sync.WaitGroup
errList := make(map[name.Application]chan error, len(b.Applications))
for appName := range b.Applications {
//if application has the same orderNumber as currently iterating the reconcile the application
if application.GetOrderNumber(appName) == orderNumber {
wg.Add(1)
go b.ReconcileApplication(currentResourceList, appName, spec, &wg)
if err := b.GetStatus(); err != nil {
b.status = err
return b
}
errChan := make(chan error)
go b.ReconcileApplication(currentResourceList, appName, spec, &wg, errChan)
applicationCount++
errList[appName] = errChan
}
}
for appName, errChan := range errList {
if err := <-errChan; err != nil {
return errors.Wrapf(err, "Error while reconciling application %s", appName.String())
}
}
wg.Wait()
}

return b
return nil
}

func (b *Bundle) ReconcileApplication(currentResourceList []*clientgo.Resource, appName name.Application, spec *v1beta1.ToolsetSpec, wg *sync.WaitGroup) *Bundle {
func (b *Bundle) ReconcileApplication(currentResourceList []*clientgo.Resource, appName name.Application, spec *v1beta1.ToolsetSpec, wg *sync.WaitGroup, errChan chan error) {
defer wg.Done()

if b.status != nil {
return b
}

logFields := map[string]interface{}{
"application": appName,
"action": "reconciling",
Expand All @@ -156,9 +133,10 @@ func (b *Bundle) ReconcileApplication(currentResourceList []*clientgo.Resource,

app, found := b.Applications[appName]
if !found {
b.status = errors.New("Application not found")
monitor.Error(b.status)
return b
err := errors.New("Application not found")
monitor.Error(err)
errChan <- err
return
}
monitor.Info("Start")

Expand All @@ -180,16 +158,21 @@ func (b *Bundle) ReconcileApplication(currentResourceList []*clientgo.Resource,

_, usedHelm := app.(application.HelmApplication)
if usedHelm {
b.status = b.HelmTemplator.Template(app, spec, resultFunc).GetStatus()
if b.status != nil {
return b
err := b.HelmTemplator.Template(app, spec, resultFunc)
if err != nil {
errChan <- err
return
}
}
_, usedYaml := app.(application.YAMLApplication)
if usedYaml {
b.status = b.YamlTemplator.Template(app, spec, resultFunc).GetStatus()
err := b.YamlTemplator.Template(app, spec, resultFunc)
if err != nil {
errChan <- err
return
}
}

monitor.Info("Done")
return b
errChan <- nil
}
20 changes: 10 additions & 10 deletions internal/bundle/bundle_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -127,13 +127,13 @@ func TestBundle_AddApplication_AlreadyAdded(t *testing.T) {
out, _ := yamlv3.Marshal(testHelperResource)
app.SetDeploy(spec, true).SetGetYaml(spec, string(out))

err := b.AddApplication(app.Application()).GetStatus()
err := b.AddApplication(app.Application())
assert.NoError(t, err)

apps := b.GetApplications()
assert.Equal(t, 1, len(apps))

err2 := b.AddApplication(app.Application()).GetStatus()
err2 := b.AddApplication(app.Application())
assert.Error(t, err2)

}
Expand All @@ -153,8 +153,9 @@ func TestBundle_ReconcileApplication(t *testing.T) {

var wg sync.WaitGroup
wg.Add(1)
err := b.ReconcileApplication(resources, app.Application().GetName(), spec, &wg).GetStatus()
assert.NoError(t, err)
errChan := make(chan error)
go b.ReconcileApplication(resources, app.Application().GetName(), spec, &wg, errChan)
assert.NoError(t, <-errChan)
}

func TestBundle_ReconcileApplication_nonexistent(t *testing.T) {
Expand All @@ -170,8 +171,9 @@ func TestBundle_ReconcileApplication_nonexistent(t *testing.T) {

var wg sync.WaitGroup
wg.Add(1)
err := b.ReconcileApplication(resources, app.Application().GetName(), nil, &wg).GetStatus()
assert.Error(t, err)
errChan := make(chan error)
go b.ReconcileApplication(resources, app.Application().GetName(), nil, &wg, errChan)
assert.Error(t, <-errChan)
}

func TestBundle_Reconcile(t *testing.T) {
Expand All @@ -186,8 +188,7 @@ func TestBundle_Reconcile(t *testing.T) {

resources := []*clientgo.Resource{}

b.Reconcile(resources, spec)
err := b.GetStatus()
err := b.Reconcile(resources, spec)
assert.NoError(t, err)
}

Expand All @@ -196,7 +197,6 @@ func TestBundle_Reconcile_NoApplications(t *testing.T) {

spec := &v1beta1.ToolsetSpec{}
resources := []*clientgo.Resource{}
b.Reconcile(resources, spec)
err := b.GetStatus()
err := b.Reconcile(resources, spec)
assert.NoError(t, err)
}
1 change: 1 addition & 0 deletions internal/crd/crd.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ type Crd interface {
Reconcile([]*clientgo.Resource, *toolsetsv1beta1.Toolset)
CleanUp()
GetStatus() error
SetBackStatus()
}

func New(conf *config.Config) (Crd, error) {
Expand Down
9 changes: 7 additions & 2 deletions internal/crd/v1beta1/crd.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,16 @@ func (c *Crd) GetStatus() error {
return c.status
}

func (c *Crd) SetBackStatus() {
c.status = nil
}

func (c *Crd) CleanUp() {
if c.GetStatus() != nil {
return
}

c.status = c.bundle.CleanUp().GetStatus()
c.status = c.bundle.CleanUp()
}

func GetVersion() name.Version {
Expand Down Expand Up @@ -94,6 +98,7 @@ func (c *Crd) Reconcile(currentResourceList []*clientgo.Resource, toolsetCRD *to
if c.GetStatus() != nil {
return
}

logFields := map[string]interface{}{
"CRD": toolsetCRD.Name,
"action": "reconciling",
Expand All @@ -112,5 +117,5 @@ func (c *Crd) Reconcile(currentResourceList []*clientgo.Resource, toolsetCRD *to
return
}

c.status = c.bundle.Reconcile(currentResourceList, toolsetCRD.Spec).GetStatus()
c.status = c.bundle.Reconcile(currentResourceList, toolsetCRD.Spec)
}
3 changes: 3 additions & 0 deletions internal/gitcrd/v1beta1/gitcrd.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,9 @@ func New(conf *config.Config) (*GitCrd, error) {
func (c *GitCrd) GetStatus() error {
return c.status
}

func (c *GitCrd) SetBackStatus() {
c.crd.SetBackStatus()
c.status = nil
}

Expand Down Expand Up @@ -160,6 +162,7 @@ func (c *GitCrd) Reconcile(currentResourceList []*clientgo.Resource) {
}
}
}

func (c *GitCrd) getCrdContent() (*toolsetsv1beta1.Toolset, error) {
c.gitMutex.Lock()
defer c.gitMutex.Unlock()
Expand Down
14 changes: 2 additions & 12 deletions internal/templator/helm/helm.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ func GetPrio() name.Templator {

type Helm struct {
overlay string
status error
monitor mntr.Monitor
templatorDirectoryPath string
}
Expand All @@ -37,13 +36,8 @@ func New(monitor mntr.Monitor, overlay, templatorDirectoryPath string) templator
}
}

func (h *Helm) CleanUp() templator.Templator {
if h.status != nil {
return h
}

h.status = os.RemoveAll(h.templatorDirectoryPath)
return h
func (h *Helm) CleanUp() error {
return os.RemoveAll(h.templatorDirectoryPath)
}

func (h *Helm) getResultsFileDirectory(appName name.Application, overlay, basePath string) string {
Expand All @@ -54,10 +48,6 @@ func (h *Helm) GetResultsFilePath(appName name.Application, overlay, basePath st
return filepath.Join(h.getResultsFileDirectory(appName, overlay, basePath), "results.yaml")
}

func (h *Helm) GetStatus() error {
return h.status
}

func checkTemplatorInterface(templatorInterface interface{}) (templator.HelmApplication, error) {
app, isTemplator := templatorInterface.(templator.HelmApplication)
if !isTemplator {
Expand Down
11 changes: 4 additions & 7 deletions internal/templator/helm/mutate.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,7 @@ type TemplatorMutate interface {
HelmMutate(mntr.Monitor, *v1beta1.ToolsetSpec, string) error
}

func (h *Helm) mutate(app interface{}, spec *v1beta1.ToolsetSpec) templator.Templator {
if h.status != nil {
return h
}
func (h *Helm) mutate(app interface{}, spec *v1beta1.ToolsetSpec) error {

mutate, ok := app.(TemplatorMutate)
if ok {
Expand All @@ -30,9 +27,9 @@ func (h *Helm) mutate(app interface{}, spec *v1beta1.ToolsetSpec) templator.Temp
resultfilepath := h.GetResultsFilePath(mutate.GetName(), h.overlay, h.templatorDirectoryPath)

if err := mutate.HelmMutate(mutateMonitor, spec, resultfilepath); err != nil {
h.status = err
return h
return err
}
}
return h

return nil
}
11 changes: 3 additions & 8 deletions internal/templator/helm/mutatevalues.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,7 @@ type TemplatorMutateValues interface {
HelmMutateValues(mntr.Monitor, *v1beta1.ToolsetSpec, string) error
}

func (h *Helm) mutateValue(app interface{}, spec *v1beta1.ToolsetSpec, valuesAbsFilePath string) templator.Templator {
if h.status != nil {
return h
}

func (h *Helm) mutateValue(app interface{}, spec *v1beta1.ToolsetSpec, valuesAbsFilePath string) error {
mutate, ok := app.(TemplatorMutateValues)
if ok {

Expand All @@ -28,10 +24,9 @@ func (h *Helm) mutateValue(app interface{}, spec *v1beta1.ToolsetSpec, valuesAbs
mutateMonitor.Debug("Mutate values")

if err := mutate.HelmMutateValues(mutateMonitor, spec, valuesAbsFilePath); err != nil {
h.status = err
return h
return err
}
}

return h
return nil
}
Loading

0 comments on commit 59185c1

Please sign in to comment.