Skip to content

Commit

Permalink
Move enhancer to renderer package (#993)
Browse files Browse the repository at this point in the history
What this PR does / why we need it:
This is a follow-up of #992 where we wanted to move enhancer into the rendering package but just did not manage to do that. This PR moves it there plus the metadata from tasker, that is actually rendering metadata so it belongs there.

This also moves template package and health package into the engine and does a bit of renaming.
  • Loading branch information
alenkacz authored Oct 24, 2019
1 parent 3ce18a5 commit e6ca394
Show file tree
Hide file tree
Showing 13 changed files with 50 additions and 42 deletions.
5 changes: 3 additions & 2 deletions pkg/controller/instance/instance_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,9 @@ import (
"strings"
"time"

"github.com/kudobuilder/kudo/pkg/engine/renderer"

"github.com/kudobuilder/kudo/pkg/engine"
"github.com/kudobuilder/kudo/pkg/engine/task"
"github.com/kudobuilder/kudo/pkg/engine/workflow"
"sigs.k8s.io/controller-runtime/pkg/handler"
"sigs.k8s.io/controller-runtime/pkg/source"
Expand Down Expand Up @@ -168,7 +169,7 @@ func (r *Reconciler) Reconcile(request ctrl.Request) (ctrl.Result, error) {
return reconcile.Result{}, err
}
log.Printf("InstanceController: Going to proceed in execution of active plan %s on instance %s/%s", activePlan.Name, instance.Namespace, instance.Name)
newStatus, err := workflow.Execute(activePlan, metadata, r.Client, &task.KustomizeEnhancer{Scheme: r.Scheme}, time.Now())
newStatus, err := workflow.Execute(activePlan, metadata, r.Client, &renderer.KustomizeEnhancer{Scheme: r.Scheme}, time.Now())

// ---------- 4. Update status of instance after the execution proceeded ----------
if newStatus != nil {
Expand Down
File renamed without changes.
13 changes: 13 additions & 0 deletions pkg/engine/renderer/engine.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,22 @@ import (
"fmt"
"text/template"

"github.com/kudobuilder/kudo/pkg/engine"

"github.com/masterminds/sprig"
)

// Metadata contains Metadata along with specific fields associated with current plan
// being executed like current plan, phase, step or task names.
type Metadata struct {
engine.Metadata

PlanName string
PhaseName string
StepName string
TaskName string
}

// Engine is the control struct for parsing and templating Kubernetes resources in an ordered fashion
type Engine struct {
FuncMap template.FuncMap
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package task
package renderer

import (
"fmt"
Expand All @@ -9,7 +9,6 @@ import (

"github.com/pkg/errors"

"github.com/kudobuilder/kudo/pkg/util/template"
"gopkg.in/yaml.v2"
"k8s.io/apimachinery/pkg/runtime"
"sigs.k8s.io/kustomize/k8sdeps/kunstruct"
Expand Down Expand Up @@ -109,7 +108,7 @@ func (k *KustomizeEnhancer) Apply(templates map[string]string, metadata Metadata
return nil, errors.Wrapf(err, "error encoding kustomized files into yaml")
}

objsToAdd, err = template.ParseKubernetesObjects(string(res))
objsToAdd, err = YamlToObject(string(res))
if err != nil {
return nil, errors.Wrapf(err, "error parsing kubernetes objects after applying kustomize")
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package template
package renderer

import (
"bytes"
Expand All @@ -12,10 +12,10 @@ import (
"k8s.io/client-go/kubernetes/scheme"
)

// ParseKubernetesObjects parses a list of runtime.Objects from the provided yaml
// YamlToObject parses a list of runtime.Objects from the provided yaml
// If the type is not known in the scheme, it tries to parse it as Unstructured
// TODO(av) could we use something else than a global scheme here? Should we somehow inject it?
func ParseKubernetesObjects(yaml string) (objs []runtime.Object, err error) {
func YamlToObject(yaml string) (objs []runtime.Object, err error) {
sepYamlfiles := strings.Split(yaml, "---")
for _, f := range sepYamlfiles {
if f == "\n" || f == "" {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
package template
package renderer

import "testing"

func TestParseKubernetesObjects_UnknownType(t *testing.T) {
_, err := ParseKubernetesObjects(`apiVersion: monitoring.coreos.com/v1
_, err := YamlToObject(`apiVersion: monitoring.coreos.com/v1
kind: ServiceMonitor
metadata:
labels:
Expand Down
4 changes: 2 additions & 2 deletions pkg/engine/task/render.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (
)

// render method takes resource names and Instance parameters and then renders passed templates using kudo engine.
func render(resourceNames []string, templates map[string]string, params map[string]string, meta Metadata) (map[string]string, error) {
func render(resourceNames []string, templates map[string]string, params map[string]string, meta renderer.Metadata) (map[string]string, error) {
configs := make(map[string]interface{})
configs["OperatorName"] = meta.OperatorName
configs["Name"] = meta.InstanceName
Expand Down Expand Up @@ -40,7 +40,7 @@ func render(resourceNames []string, templates map[string]string, params map[stri

// kustomize method takes a slice of rendered templates, applies conventions using Enhancer and
// returns a slice of k8s objects.
func kustomize(rendered map[string]string, meta Metadata, enhancer Enhancer) ([]runtime.Object, error) {
func kustomize(rendered map[string]string, meta renderer.Metadata, enhancer renderer.Enhancer) ([]runtime.Object, error) {
enhanced, err := enhancer.Apply(rendered, meta)
return enhanced, err
}
18 changes: 4 additions & 14 deletions pkg/engine/task/task.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,27 +4,17 @@ import (
"errors"
"fmt"

"github.com/kudobuilder/kudo/pkg/engine/renderer"

"github.com/kudobuilder/kudo/pkg/apis/kudo/v1alpha1"
"github.com/kudobuilder/kudo/pkg/engine"
"sigs.k8s.io/controller-runtime/pkg/client"
)

// Metadata contains Metadata along with specific fields associated with current plan
// being executed like current plan, phase, step or task names.
type Metadata struct {
engine.Metadata

PlanName string
PhaseName string
StepName string
TaskName string
}

// Context is a engine.task execution context containing k8s client, templates parameters etc.
type Context struct {
Client client.Client
Enhancer Enhancer
Meta Metadata
Enhancer renderer.Enhancer
Meta renderer.Metadata
Templates map[string]string // Raw templates
Parameters map[string]string // Instance and OperatorVersion parameters merged
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/engine/task/task_apply.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (
"log"

"github.com/kudobuilder/kudo/pkg/apis/kudo/v1alpha1"
"github.com/kudobuilder/kudo/pkg/util/health"
"github.com/kudobuilder/kudo/pkg/engine/health"
apiextv1beta1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1beta1"
apierrors "k8s.io/apimachinery/pkg/api/errors"
"k8s.io/apimachinery/pkg/runtime"
Expand Down
13 changes: 7 additions & 6 deletions pkg/engine/task/task_apply_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,9 @@ import (
"fmt"
"testing"

"github.com/kudobuilder/kudo/pkg/engine/renderer"

"github.com/kudobuilder/kudo/pkg/engine"
"github.com/kudobuilder/kudo/pkg/util/template"
"github.com/stretchr/testify/assert"
batchv1 "k8s.io/api/batch/v1"
corev1 "k8s.io/api/core/v1"
Expand All @@ -18,7 +19,7 @@ import (
)

func TestApplyTask_Run(t *testing.T) {
meta := Metadata{
meta := renderer.Metadata{
Metadata: engine.Metadata{
InstanceName: "test",
InstanceNamespace: "default",
Expand Down Expand Up @@ -51,7 +52,7 @@ func TestApplyTask_Run(t *testing.T) {
ctx: Context{
Client: fake.NewFakeClientWithScheme(scheme.Scheme),
Enhancer: &testEnhancer{},
Meta: Metadata{},
Meta: renderer.Metadata{},
},
},
{
Expand Down Expand Up @@ -168,10 +169,10 @@ func resourceAsString(resource metav1.Object) string {

type testEnhancer struct{}

func (k *testEnhancer) Apply(templates map[string]string, metadata Metadata) ([]runtime.Object, error) {
func (k *testEnhancer) Apply(templates map[string]string, metadata renderer.Metadata) ([]runtime.Object, error) {
result := make([]runtime.Object, 0)
for _, t := range templates {
objsToAdd, err := template.ParseKubernetesObjects(t)
objsToAdd, err := renderer.YamlToObject(t)
if err != nil {
return nil, fmt.Errorf("error parsing kubernetes objects after applying kustomize: %w", err)
}
Expand All @@ -182,6 +183,6 @@ func (k *testEnhancer) Apply(templates map[string]string, metadata Metadata) ([]

type errorEnhancer struct{}

func (k *errorEnhancer) Apply(templates map[string]string, metadata Metadata) ([]runtime.Object, error) {
func (k *errorEnhancer) Apply(templates map[string]string, metadata renderer.Metadata) ([]runtime.Object, error) {
return nil, errors.New("always error")
}
6 changes: 4 additions & 2 deletions pkg/engine/task/task_delete_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,16 @@ import (
"fmt"
"testing"

"github.com/kudobuilder/kudo/pkg/engine/renderer"

"github.com/kudobuilder/kudo/pkg/engine"
"github.com/stretchr/testify/assert"
"k8s.io/client-go/kubernetes/scheme"
"sigs.k8s.io/controller-runtime/pkg/client/fake"
)

func TestDeleteTask_Run(t *testing.T) {
meta := Metadata{
meta := renderer.Metadata{
Metadata: engine.Metadata{
InstanceName: "test",
InstanceNamespace: "default",
Expand Down Expand Up @@ -45,7 +47,7 @@ func TestDeleteTask_Run(t *testing.T) {
ctx: Context{
Client: fake.NewFakeClientWithScheme(scheme.Scheme),
Enhancer: &testEnhancer{},
Meta: Metadata{},
Meta: renderer.Metadata{},
},
},
{
Expand Down
6 changes: 4 additions & 2 deletions pkg/engine/workflow/engine.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import (
"log"
"time"

"github.com/kudobuilder/kudo/pkg/engine/renderer"

"github.com/kudobuilder/kudo/pkg/apis/kudo/v1alpha1"
"github.com/kudobuilder/kudo/pkg/engine"
"github.com/kudobuilder/kudo/pkg/engine/task"
Expand Down Expand Up @@ -58,7 +60,7 @@ func (ap *ActivePlan) taskByName(name string) (*v1alpha1.Task, bool) {
//
// Furthermore, a transient ERROR during a step execution, means that the next step may be executed if the step strategy
// is "parallel". In case of a fatal error, it is returned alongside with the new plan status and published on the event bus.
func Execute(pl *ActivePlan, em *engine.Metadata, c client.Client, enh task.Enhancer, currentTime time.Time) (*v1alpha1.PlanStatus, error) {
func Execute(pl *ActivePlan, em *engine.Metadata, c client.Client, enh renderer.Enhancer, currentTime time.Time) (*v1alpha1.PlanStatus, error) {
if pl.Status.IsTerminal() {
log.Printf("PlanExecution: Plan %s for instance %s is terminal, nothing to do", pl.Name, em.InstanceName)
return pl.PlanStatus, nil
Expand Down Expand Up @@ -130,7 +132,7 @@ func Execute(pl *ActivePlan, em *engine.Metadata, c client.Client, enh task.Enha
}
}
// - 3.a build execution metadata -
exm := task.Metadata{
exm := renderer.Metadata{
Metadata: *em,
PlanName: pl.Name,
PhaseName: ph.Name,
Expand Down
10 changes: 5 additions & 5 deletions pkg/engine/workflow/engine_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@ import (
"testing"
"time"

"github.com/kudobuilder/kudo/pkg/engine/renderer"

"github.com/kudobuilder/kudo/pkg/apis/kudo/v1alpha1"
"github.com/kudobuilder/kudo/pkg/engine"
"github.com/kudobuilder/kudo/pkg/engine/task"
"github.com/kudobuilder/kudo/pkg/util/template"
"github.com/pkg/errors"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
Expand Down Expand Up @@ -37,7 +37,7 @@ func TestExecutePlan(t *testing.T) {
metadata *engine.Metadata
expectedStatus *v1alpha1.PlanStatus
wantErr bool
enhancer task.Enhancer
enhancer renderer.Enhancer
}{
{name: "plan already finished will not change its status", activePlan: &ActivePlan{
Name: "test",
Expand Down Expand Up @@ -636,10 +636,10 @@ func instance() *v1alpha1.Instance {

type testEnhancer struct{}

func (k *testEnhancer) Apply(templates map[string]string, metadata task.Metadata) ([]runtime.Object, error) {
func (k *testEnhancer) Apply(templates map[string]string, metadata renderer.Metadata) ([]runtime.Object, error) {
result := make([]runtime.Object, 0)
for _, t := range templates {
objsToAdd, err := template.ParseKubernetesObjects(t)
objsToAdd, err := renderer.YamlToObject(t)
if err != nil {
return nil, errors.Wrapf(err, "error parsing kubernetes objects after applying kustomize")
}
Expand Down

0 comments on commit e6ca394

Please sign in to comment.