Skip to content

Commit

Permalink
Add support for volume and volume mounts for runners (#249)
Browse files Browse the repository at this point in the history
* Add support for volume and volume mounts for runners

* Fix formatting

* Revert makefile changes and add example k6 job for init container + volume scenario

* Update e2e/test-initcontainer-volumes.yaml

Co-authored-by: Olha Yevtushenko <[email protected]>

* Add maxDescLen=0 to crd options

---------

Co-authored-by: Olha Yevtushenko <[email protected]>
  • Loading branch information
andrei-trandafir and yorugac authored Aug 2, 2023
1 parent 435bb2a commit b5125f4
Show file tree
Hide file tree
Showing 10 changed files with 2,831 additions and 3,284 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ IMG ?= ghcr.io/grafana/k6-operator:latest
# Default dockerfile to build
DOCKERFILE ?= "Dockerfile.controller"
# Produce CRDs that work back to Kubernetes 1.11 (no version conversion)
CRD_OPTIONS ?= "crd:trivialVersions=true,crdVersions=v1"
CRD_OPTIONS ?= "crd:trivialVersions=true,crdVersions=v1,maxDescLen=0"

# Get the currently used golang install path (in GOPATH/bin, unless GOBIN is set)
ifeq (,$(shell go env GOBIN))
Expand Down
16 changes: 10 additions & 6 deletions api/v1alpha1/k6_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,15 +45,19 @@ type Pod struct {
ReadinessProbe *corev1.Probe `json:"readinessProbe,omitempty"`
LivenessProbe *corev1.Probe `json:"livenessProbe,omitempty"`
InitContainers []InitContainer `json:"initContainers,omitempty"`
Volumes []corev1.Volume `json:"volumes,omitempty"`
VolumeMounts []corev1.VolumeMount `json:"volumeMounts,omitempty"`
}

type InitContainer struct {
Image string `json:"image,omitempty"`
Env []corev1.EnvVar `json:"env,omitempty"`
EnvFrom []corev1.EnvFromSource `json:"envFrom,omitempty"`
Command []string `json:"command,omitempty"`
Args []string `json:"args,omitempty"`
WorkingDir string `json:"workingDir,omitempty"`
Name string `json:"name,omitempty"`
Image string `json:"image,omitempty"`
Env []corev1.EnvVar `json:"env,omitempty"`
EnvFrom []corev1.EnvFromSource `json:"envFrom,omitempty"`
Command []string `json:"command,omitempty"`
Args []string `json:"args,omitempty"`
WorkingDir string `json:"workingDir,omitempty"`
VolumeMounts []corev1.VolumeMount `json:"volumeMounts,omitempty"`
}

type K6Scuttle struct {
Expand Down
21 changes: 21 additions & 0 deletions api/v1alpha1/zz_generated.deepcopy.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5,774 changes: 2,548 additions & 3,226 deletions config/crd/bases/k6.io_k6s.yaml

Large diffs are not rendered by default.

44 changes: 0 additions & 44 deletions config/crd/bases/k6.io_privateloadzones.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -19,22 +19,14 @@ spec:
- name: v1alpha1
schema:
openAPIV3Schema:
description: PrivateLoadZone is the Schema for the privateloadzones API
properties:
apiVersion:
description: 'APIVersion defines the versioned schema of this representation
of an object. Servers should convert recognized schemas to the latest
internal value, and may reject unrecognized values. More info: https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md#resources'
type: string
kind:
description: 'Kind is a string value representing the REST resource this
object represents. Servers may infer this from the endpoint the client
submits requests to. Cannot be updated. In CamelCase. More info: https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md#types-kinds'
type: string
metadata:
type: object
spec:
description: PrivateLoadZoneSpec defines the desired state of PrivateLoadZone
properties:
nodeSelector:
additionalProperties:
Expand All @@ -47,7 +39,6 @@ spec:
- type: string
pattern: ^(\+|-)?(([0-9]+(\.[0-9]*)?)|(\.[0-9]+))(([KMGTPE]i)|[numkMGTPE]|([eE](\+|-)?(([0-9]+(\.[0-9]*)?)|(\.[0-9]+))))?$
x-kubernetes-int-or-string: true
description: ResourceList is a set of (resource name, quantity) pairs.
type: object
serviceAccountName:
type: string
Expand All @@ -58,67 +49,32 @@ spec:
- token
type: object
status:
description: PrivateLoadZoneStatus defines the observed state of PrivateLoadZone
properties:
conditions:
items:
description: "Condition contains details for one aspect of the current
state of this API Resource. --- This struct is intended for direct
use as an array at the field path .status.conditions. For example,
\n \ttype FooStatus struct{ \t // Represents the observations
of a foo's current state. \t // Known .status.conditions.type
are: \"Available\", \"Progressing\", and \"Degraded\" \t //
+patchMergeKey=type \t // +patchStrategy=merge \t // +listType=map
\t // +listMapKey=type \t Conditions []metav1.Condition
`json:\"conditions,omitempty\" patchStrategy:\"merge\" patchMergeKey:\"type\"
protobuf:\"bytes,1,rep,name=conditions\"` \n \t // other fields
\t}"
properties:
lastTransitionTime:
description: lastTransitionTime is the last time the condition
transitioned from one status to another. This should be when
the underlying condition changed. If that is not known, then
using the time when the API field changed is acceptable.
format: date-time
type: string
message:
description: message is a human readable message indicating
details about the transition. This may be an empty string.
maxLength: 32768
type: string
observedGeneration:
description: observedGeneration represents the .metadata.generation
that the condition was set based upon. For instance, if .metadata.generation
is currently 12, but the .status.conditions[x].observedGeneration
is 9, the condition is out of date with respect to the current
state of the instance.
format: int64
minimum: 0
type: integer
reason:
description: reason contains a programmatic identifier indicating
the reason for the condition's last transition. Producers
of specific condition types may define expected values and
meanings for this field, and whether the values are considered
a guaranteed API. The value should be a CamelCase string.
This field may not be empty.
maxLength: 1024
minLength: 1
pattern: ^[A-Za-z]([A-Za-z0-9_,:]*[A-Za-z0-9_])?$
type: string
status:
description: status of the condition, one of True, False, Unknown.
enum:
- "True"
- "False"
- Unknown
type: string
type:
description: type of condition in CamelCase or in foo.example.com/CamelCase.
--- Many .condition.type values are consistent across resources
like Available, but because arbitrary conditions can be useful
(see .node.status.conditions), the ability to deconflict is
important. The regex it matches is (dns1123SubdomainFmt/)?(qualifiedNameFmt)
maxLength: 316
pattern: ^([a-z0-9]([-a-z0-9]*[a-z0-9])?(\.[a-z0-9]([-a-z0-9]*[a-z0-9])?)*/)?(([A-Za-z0-9][-A-Za-z0-9_.]*)?[A-Za-z0-9])$
type: string
Expand Down
27 changes: 27 additions & 0 deletions e2e/test-initcontainer-volumes.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
---
apiVersion: k6.io/v1alpha1
kind: K6
metadata:
name: k6-sample-init-container-volume
spec:
parallelism: 1
script:
localFile: /test/test.js
runner:
volumes:
- emptyDir: {}
name: k6-provision-location
volumeMounts:
- mountPath: /test
name: k6-provision-location
initContainers:
- image: busybox:1.28
command:
[
"sh",
"-c",
"cd /test; wget https://raw.githubusercontent.com/grafana/k6-operator/main/e2e/test.js;",
]
volumeMounts:
- mountPath: /test
name: k6-provision-location
15 changes: 12 additions & 3 deletions pkg/resources/jobs/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@ package jobs

import (
"fmt"
"github.com/grafana/k6-operator/pkg/types"
"strconv"

"github.com/grafana/k6-operator/pkg/types"

"github.com/grafana/k6-operator/api/v1alpha1"
corev1 "k8s.io/api/core/v1"
)
Expand Down Expand Up @@ -121,15 +122,23 @@ func getInitContainers(k6Spec *v1alpha1.K6Spec, script *types.Script) []corev1.C
var initContainers []corev1.Container

for i, k6InitContainer := range k6Spec.Runner.InitContainers {

name := fmt.Sprintf("k6-init-%d", i)
if k6InitContainer.Name != "" {
name = k6InitContainer.Name
}

volumeMounts := append(script.VolumeMount(), k6InitContainer.VolumeMounts...)

initContainer := corev1.Container{
Name: fmt.Sprintf("k6-init-%d", i),
Name: name,
Image: k6InitContainer.Image,
Command: k6InitContainer.Command,
Args: k6InitContainer.Args,
WorkingDir: k6InitContainer.WorkingDir,
EnvFrom: k6InitContainer.EnvFrom,
Env: k6InitContainer.Env,
VolumeMounts: script.VolumeMount(),
VolumeMounts: volumeMounts,
ImagePullPolicy: k6Spec.Runner.ImagePullPolicy,
}
initContainers = append(initContainers, initContainer)
Expand Down
10 changes: 8 additions & 2 deletions pkg/resources/jobs/initializer.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,12 @@ func NewInitializerJob(k6 *v1alpha1.K6, argLine string) (*batchv1.Job, error) {

env := append(newIstioEnvVar(k6.Spec.Scuttle, istioEnabled), k6.Spec.Initializer.Env...)

volumes := script.Volume()
volumes = append(volumes, k6.Spec.Initializer.Volumes...)

volumeMounts := script.VolumeMount()
volumeMounts = append(volumeMounts, k6.Spec.Initializer.VolumeMounts...)

var zero32 int32
return &batchv1.Job{
ObjectMeta: metav1.ObjectMeta{
Expand Down Expand Up @@ -115,11 +121,11 @@ func NewInitializerJob(k6 *v1alpha1.K6, argLine string) (*batchv1.Job, error) {
Command: command,
Env: env,
Resources: k6.Spec.Initializer.Resources,
VolumeMounts: script.VolumeMount(),
VolumeMounts: volumeMounts,
Ports: ports,
},
},
Volumes: script.Volume(),
Volumes: volumes,
},
},
},
Expand Down
10 changes: 8 additions & 2 deletions pkg/resources/jobs/runner.go
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,12 @@ func NewRunnerJob(k6 *v1alpha1.K6, index int, token string) (*batchv1.Job, error

env = append(env, k6.Spec.Runner.Env...)

volumes := script.Volume()
volumes = append(volumes, k6.Spec.Runner.Volumes...)

volumeMounts := script.VolumeMount()
volumeMounts = append(volumeMounts, k6.Spec.Runner.VolumeMounts...)

job := &batchv1.Job{
ObjectMeta: metav1.ObjectMeta{
Name: name,
Expand Down Expand Up @@ -166,14 +172,14 @@ func NewRunnerJob(k6 *v1alpha1.K6, index int, token string) (*batchv1.Job, error
Command: command,
Env: env,
Resources: k6.Spec.Runner.Resources,
VolumeMounts: script.VolumeMount(),
VolumeMounts: volumeMounts,
Ports: ports,
EnvFrom: k6.Spec.Runner.EnvFrom,
LivenessProbe: generateProbe(k6.Spec.Runner.LivenessProbe),
ReadinessProbe: generateProbe(k6.Spec.Runner.ReadinessProbe),
}},
TerminationGracePeriodSeconds: &zero,
Volumes: script.Volume(),
Volumes: volumes,
},
},
},
Expand Down
Loading

0 comments on commit b5125f4

Please sign in to comment.