Skip to content

Commit

Permalink
Code optimizations
Browse files Browse the repository at this point in the history
Signed-off-by: Raul Sevilla <rsevilla@redhat.com>
  • Loading branch information
rsevilla87 committed Nov 4, 2022
1 parent 0705206 commit 39d79f8
Show file tree
Hide file tree
Showing 5 changed files with 16 additions and 12 deletions.
15 changes: 10 additions & 5 deletions pkg/burner/create.go
Expand Up @@ -29,7 +29,7 @@ import (
"github.com/cloud-bulldozer/kube-burner/log"
"github.com/cloud-bulldozer/kube-burner/pkg/config"
"github.com/cloud-bulldozer/kube-burner/pkg/util"
"k8s.io/apimachinery/pkg/api/errors"
kerrors "k8s.io/apimachinery/pkg/api/errors"
"k8s.io/apimachinery/pkg/api/meta"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
Expand Down Expand Up @@ -75,10 +75,11 @@ func setupCreateJob(jobConfig config.Job) Executor {
objectSpec: t,
objectTemplate: o.ObjectTemplate,
replicas: o.Replicas,
unstructured: uns,
kind: gvk.Kind,
inputVars: o.InputVars,
namespaced: o.Namespaced,
}
// If any of the objects is namespaced, we configure the job to create namepaces
if o.Namespaced {
ex.nsObjects = true
}
Expand Down Expand Up @@ -225,10 +226,10 @@ func createRequest(gvr schema.GroupVersionResource, ns string, obj *unstructured
uns, err = dynamicClient.Resource(gvr).Create(context.TODO(), obj, metav1.CreateOptions{})
}
if err != nil {
if errors.IsForbidden(err) {
if kerrors.IsForbidden(err) {
log.Fatalf("Authorization error creating %s/%s: %s", obj.GetKind(), obj.GetName(), err)
return true, err
} else if errors.IsAlreadyExists(err) {
} else if kerrors.IsAlreadyExists(err) {
log.Errorf("%s/%s in namespace %s already exists", obj.GetKind(), obj.GetName(), ns)
return true, nil
} else if err != nil {
Expand All @@ -237,7 +238,11 @@ func createRequest(gvr schema.GroupVersionResource, ns string, obj *unstructured
log.Error("Retrying object creation")
return false, nil
}
log.Debugf("Created %s/%s in namespace %s", uns.GetKind(), uns.GetName(), ns)
if namespaced {
log.Debugf("Created %s/%s in namespace %s", uns.GetKind(), uns.GetName(), ns)
} else {
log.Debugf("Created %s/%s", uns.GetKind(), uns.GetName(), ns)
}
return true, err
}, 1*time.Second, 3, 0, 3)
}
Expand Down
3 changes: 1 addition & 2 deletions pkg/burner/job.go
Expand Up @@ -21,7 +21,6 @@ import (
"github.com/cloud-bulldozer/kube-burner/pkg/config"
"github.com/cloud-bulldozer/kube-burner/pkg/util"
"golang.org/x/time/rate"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"k8s.io/apimachinery/pkg/runtime/schema"

"k8s.io/client-go/dynamic"
Expand All @@ -34,11 +33,11 @@ type object struct {
objectTemplate string
objectSpec []byte
replicas int
unstructured *unstructured.Unstructured
inputVars map[string]interface{}
labelSelector map[string]string
patchType string
namespaced bool
kind string
}

// Executor contains the information required to execute a job
Expand Down
4 changes: 2 additions & 2 deletions pkg/burner/utils.go
Expand Up @@ -49,11 +49,11 @@ func prepareTemplate(original []byte) ([]byte, error) {
}

func yamlToUnstructured(y []byte, uns *unstructured.Unstructured) (runtime.Object, *schema.GroupVersionKind) {
o, gvr, err := scheme.Codecs.UniversalDeserializer().Decode(y, nil, uns)
o, gvk, err := scheme.Codecs.UniversalDeserializer().Decode(y, nil, uns)
if err != nil {
log.Fatalf("Error decoding YAML: %s", err)
}
return o, gvr
return o, gvk
}

// Cleanup deletes old namespaces from a given job
Expand Down
4 changes: 2 additions & 2 deletions pkg/burner/waiters.go
Expand Up @@ -36,15 +36,15 @@ func (ex *Executor) waitForObjects(ns string) {
if len(ex.Config.WaitFor) > 0 {
waitFor = false
for _, kind := range ex.Config.WaitFor {
if obj.unstructured.GetKind() == kind {
if obj.kind == kind {
waitFor = true
break
}
}
}
if waitFor {
wg.Add(1)
switch obj.unstructured.GetKind() {
switch obj.kind {
case "Deployment":
go waitForDeployments(ns, ex.Config.MaxWaitTimeout, &wg)
case "ReplicaSet":
Expand Down
2 changes: 1 addition & 1 deletion pkg/config/types.go
Expand Up @@ -90,7 +90,7 @@ type Object struct {
APIVersion string `yaml:"apiVersion" json:"apiVersion,omitempty"`
// LabelSelector objects with this labels will be removed
LabelSelector map[string]string `yaml:"labelSelector" json:"labelSelector,omitempty"`
// Namespaced
// Namespaced this object is namespaced
Namespaced bool `yaml:"namespaced" json:"namespaced"`
}

Expand Down

0 comments on commit 39d79f8

Please sign in to comment.