Skip to content

Commit

Permalink
Embed config.Object struct into object (kube-burner#326)
Browse files Browse the repository at this point in the history
Signed-off-by: Raul Sevilla <[email protected]>
  • Loading branch information
rsevilla87 authored May 29, 2023
1 parent 22c4705 commit d415f86
Show file tree
Hide file tree
Showing 6 changed files with 29 additions and 40 deletions.
23 changes: 9 additions & 14 deletions pkg/burner/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,21 +75,16 @@ func setupCreateJob(jobConfig config.Job) Executor {
_, gvk := yamlToUnstructured(cleanTemplate, uns)
gvr, _ := meta.UnsafeGuessKindToResource(*gvk)
obj := object{
gvr: gvr,
objectSpec: t,
objectTemplate: o.ObjectTemplate,
replicas: o.Replicas,
kind: gvk.Kind,
inputVars: o.InputVars,
namespaced: o.Namespaced,
wait: o.Wait,
waitOptions: o.WaitOptions,
gvr: gvr,
objectSpec: t,
kind: gvk.Kind,
Object: o,
}
// If any of the objects is namespaced, we configure the job to create namepaces
if o.Namespaced {
ex.Config.NamespacedIterations = true
}
log.Infof("Job %s: %d iterations with %d %s replicas", jobConfig.Name, jobConfig.JobIterations, obj.replicas, gvk.Kind)
log.Infof("Job %s: %d iterations with %d %s replicas", jobConfig.Name, jobConfig.JobIterations, obj.Replicas, gvk.Kind)
ex.objects = append(ex.objects, obj)
}
return ex
Expand Down Expand Up @@ -192,7 +187,7 @@ func (ex *Executor) generateNamespace(iteration int) string {

func (ex *Executor) replicaHandler(objectIndex int, obj object, ns string, iteration int, replicaWg *sync.WaitGroup) {
var wg sync.WaitGroup
for r := 1; r <= obj.replicas; r++ {
for r := 1; r <= obj.Replicas; r++ {
wg.Add(1)
go func(r int) {
defer wg.Done()
Expand All @@ -208,13 +203,13 @@ func (ex *Executor) replicaHandler(objectIndex int, obj object, ns string, itera
jobUUID: ex.uuid,
replica: r,
}
for k, v := range obj.inputVars {
for k, v := range obj.InputVars {
templateData[k] = v
}
ex.limiter.Wait(context.TODO())
renderedObj, err := util.RenderTemplate(obj.objectSpec, templateData, util.MissingKeyError)
if err != nil {
log.Fatalf("Template error in %s: %s", obj.objectTemplate, err)
log.Fatalf("Template error in %s: %s", obj.ObjectTemplate, err)
}
// Re-decode rendered object
yamlToUnstructured(renderedObj, newObject)
Expand All @@ -230,7 +225,7 @@ func (ex *Executor) replicaHandler(objectIndex int, obj object, ns string, itera
// hasn't been created yet
replicaWg.Add(1)
go func() {
createRequest(obj.gvr, ns, newObject, obj.namespaced)
createRequest(obj.gvr, ns, newObject, obj.Namespaced)
replicaWg.Done()
}()
}(r)
Expand Down
17 changes: 6 additions & 11 deletions pkg/burner/job.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,17 +38,12 @@ import (
)

type object struct {
gvr schema.GroupVersionResource
objectTemplate string
objectSpec []byte
replicas int
inputVars map[string]interface{}
labelSelector map[string]string
patchType string
namespaced bool
kind string
wait bool
waitOptions config.WaitOptions
gvr schema.GroupVersionResource
objectSpec []byte
labelSelector map[string]string
patchType string
kind string
config.Object
}

// Executor contains the information required to execute a job
Expand Down
17 changes: 8 additions & 9 deletions pkg/burner/patch.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,12 +64,11 @@ func setupPatchJob(jobConfig config.Job) Executor {
log.Fatalln("Empty Patch Type not allowed")
}
obj := object{
gvr: gvr,
objectSpec: t,
objectTemplate: o.ObjectTemplate,
inputVars: o.InputVars,
labelSelector: o.LabelSelector,
patchType: o.PatchType,
gvr: gvr,
objectSpec: t,
Object: o,
labelSelector: o.LabelSelector,
patchType: o.PatchType,
}
log.Infof("Job %s: Patch %s with selector %s", jobConfig.Name, gvk.Kind, labels.Set(obj.labelSelector))
ex.objects = append(ex.objects, obj)
Expand Down Expand Up @@ -122,7 +121,7 @@ func (ex *Executor) patchHandler(obj object, originalItem unstructured.Unstructu
var data []byte
patchOptions := metav1.PatchOptions{}

if strings.HasSuffix(obj.objectTemplate, "json") {
if strings.HasSuffix(obj.ObjectTemplate, "json") {
if obj.patchType == string(types.ApplyPatchType) {
log.Fatalf("Apply patch type requires YAML")
}
Expand All @@ -134,12 +133,12 @@ func (ex *Executor) patchHandler(obj object, originalItem unstructured.Unstructu
jobIteration: iteration,
jobUUID: ex.uuid,
}
for k, v := range obj.inputVars {
for k, v := range obj.InputVars {
templateData[k] = v
}
renderedObj, err := util.RenderTemplate(obj.objectSpec, templateData, util.MissingKeyError)
if err != nil {
log.Fatalf("Template error in %s: %s", obj.objectTemplate, err)
log.Fatalf("Template error in %s: %s", obj.ObjectTemplate, err)
}

// Converting to JSON if patch type is not Apply
Expand Down
2 changes: 1 addition & 1 deletion pkg/burner/pre_load.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ func getJobImages(job Executor) ([]string, error) {
var imageList []string
var unstructuredObject unstructured.Unstructured
for _, object := range job.objects {
renderedObj, err := util.RenderTemplate(object.objectSpec, object.inputVars, util.MissingKeyZero)
renderedObj, err := util.RenderTemplate(object.objectSpec, object.InputVars, util.MissingKeyZero)
if err != nil {
return imageList, err
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/burner/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ func (ex *Executor) Verify() bool {
success = false
continue
}
objectsExpected := ex.Config.JobIterations * obj.replicas
objectsExpected := ex.Config.JobIterations * obj.Replicas
if replicas != objectsExpected {
log.Errorf("%s found: %d Expected: %d", obj.gvr.Resource, replicas, objectsExpected)
success = false
Expand Down
8 changes: 4 additions & 4 deletions pkg/burner/waiters.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,10 @@ import (
func (ex *Executor) waitForObjects(ns string) {
var wg sync.WaitGroup
for _, obj := range ex.objects {
if obj.wait {
if obj.Wait {
wg.Add(1)
if obj.waitOptions.ForCondition != "" {
go waitForCondition(obj.gvr, ns, obj.waitOptions.ForCondition, ex.Config.MaxWaitTimeout, &wg)
if obj.WaitOptions.ForCondition != "" {
go waitForCondition(obj.gvr, ns, obj.WaitOptions.ForCondition, ex.Config.MaxWaitTimeout, &wg)
} else {
switch obj.kind {
case "Deployment":
Expand All @@ -51,7 +51,7 @@ func (ex *Executor) waitForObjects(ns string) {
case "Pod":
go waitForPod(ns, ex.Config.MaxWaitTimeout, &wg)
case "Build", "BuildConfig":
go waitForBuild(ns, ex.Config.MaxWaitTimeout, obj.replicas, &wg)
go waitForBuild(ns, ex.Config.MaxWaitTimeout, obj.Replicas, &wg)
case "VirtualMachine":
go waitForVM(ns, ex.Config.MaxWaitTimeout, &wg)
case "VirtualMachineInstance":
Expand Down

0 comments on commit d415f86

Please sign in to comment.