Skip to content

Commit

Permalink
fix: use default resources for the init container (#5684)
Browse files Browse the repository at this point in the history
  • Loading branch information
rangoo94 committed Jul 24, 2024
1 parent e1f3d83 commit 91b2e7f
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 24 deletions.
27 changes: 3 additions & 24 deletions pkg/testworkflows/testworkflowprocessor/container.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,7 @@ import (
"slices"
"strings"

"github.com/pkg/errors"
corev1 "k8s.io/api/core/v1"
quantity "k8s.io/apimachinery/pkg/api/resource"

testworkflowsv1 "github.com/kubeshop/testkube-operator/api/testworkflows/v1"
"github.com/kubeshop/testkube/internal/common"
Expand Down Expand Up @@ -348,28 +346,9 @@ func (c *container) ToKubernetesTemplate() (corev1.Container, error) {
if cr.WorkingDir != nil {
workingDir = *cr.WorkingDir
}
resources := corev1.ResourceRequirements{}
if cr.Resources != nil {
if len(cr.Resources.Requests) > 0 {
resources.Requests = make(corev1.ResourceList)
}
if len(cr.Resources.Limits) > 0 {
resources.Limits = make(corev1.ResourceList)
}
for k, v := range cr.Resources.Requests {
var err error
resources.Requests[k], err = quantity.ParseQuantity(v.String())
if err != nil {
return corev1.Container{}, errors.Wrap(err, "parsing resources")
}
}
for k, v := range cr.Resources.Limits {
var err error
resources.Limits[k], err = quantity.ParseQuantity(v.String())
if err != nil {
return corev1.Container{}, errors.Wrap(err, "parsing resources")
}
}
resources, resourcesErr := MapResourcesToKubernetesResources(cr.Resources)
if resourcesErr != nil {
return corev1.Container{}, resourcesErr
}
return corev1.Container{
Image: cr.Image,
Expand Down
6 changes: 6 additions & 0 deletions pkg/testworkflows/testworkflowprocessor/processor.go
Original file line number Diff line number Diff line change
Expand Up @@ -312,13 +312,19 @@ func (p *processor) Bundle(ctx context.Context, workflow *testworkflowsv1.TestWo
},
}
AnnotateControlledBy(&podSpec, resourceRoot.Template(), resourceId.Template())

defaultResources, defaultResourcesErr := MapResourcesToKubernetesResources(common.Ptr(layer.ContainerDefaults().Resources()))
if defaultResourcesErr != nil {
return nil, defaultResourcesErr
}
initContainer := corev1.Container{
Name: "tktw-init",
Image: constants.DefaultInitImage,
ImagePullPolicy: corev1.PullIfNotPresent,
Command: []string{"/bin/sh", "-c"},
Args: []string{constants.InitScript},
VolumeMounts: layer.ContainerDefaults().VolumeMounts(),
Resources: defaultResources,
Env: []corev1.EnvVar{
{Name: "TK_DEBUG_NODE", ValueFrom: &corev1.EnvVarSource{
FieldRef: &corev1.ObjectFieldSelector{FieldPath: "spec.nodeName"},
Expand Down
30 changes: 30 additions & 0 deletions pkg/testworkflows/testworkflowprocessor/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,13 @@ import (
"fmt"
"strings"

"github.com/pkg/errors"
batchv1 "k8s.io/api/batch/v1"
corev1 "k8s.io/api/core/v1"
quantity "k8s.io/apimachinery/pkg/api/resource"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"

testworkflowsv1 "github.com/kubeshop/testkube-operator/api/testworkflows/v1"
"github.com/kubeshop/testkube/internal/common"
"github.com/kubeshop/testkube/pkg/expressions"
"github.com/kubeshop/testkube/pkg/rand"
Expand All @@ -19,6 +22,33 @@ var BypassToolkitCheck = corev1.EnvVar{
Value: rand.String(20),
}

func MapResourcesToKubernetesResources(resources *testworkflowsv1.Resources) (corev1.ResourceRequirements, error) {
result := corev1.ResourceRequirements{}
if resources != nil {
if len(resources.Requests) > 0 {
result.Requests = make(corev1.ResourceList)
}
if len(resources.Limits) > 0 {
result.Limits = make(corev1.ResourceList)
}
for k, v := range resources.Requests {
var err error
result.Requests[k], err = quantity.ParseQuantity(v.String())
if err != nil {
return corev1.ResourceRequirements{}, errors.Wrap(err, "parsing resources")
}
}
for k, v := range resources.Limits {
var err error
result.Limits[k], err = quantity.ParseQuantity(v.String())
if err != nil {
return corev1.ResourceRequirements{}, errors.Wrap(err, "parsing resources")
}
}
}
return result, nil
}

func AnnotateControlledBy(obj metav1.Object, rootId, id string) {
labels := obj.GetLabels()
if labels == nil {
Expand Down

0 comments on commit 91b2e7f

Please sign in to comment.