Skip to content

Commit

Permalink
tests: adjust e2e test for custom configuration
Browse files Browse the repository at this point in the history
Signed-off-by: Dominik Rosiek <drosiek@sumologic.com>
  • Loading branch information
Dominik Rosiek committed Jul 3, 2023
1 parent 2128e75 commit cbde34e
Show file tree
Hide file tree
Showing 25 changed files with 177 additions and 19 deletions.
6 changes: 3 additions & 3 deletions helm/tests/values.withCustomConfiguration.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -105,9 +105,9 @@ sidecar:
DB.Sync Normal
[FILTER]
Name modify
Match *
Set message modified
Name modify
Match *
Set log modified
[OUTPUT]
name gstdout
Expand Down
4 changes: 2 additions & 2 deletions kuttl-test-helm-custom-configuration.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ apiVersion: kuttl.dev/v1beta1
kind: TestSuite
artifactsDir: ./tests/_build/artifacts/
testDirs:
- ./tests/sidecar/
- ./tests/operator/
- ./tests/modified/sidecar/
- ./tests/modified/operator/
timeout: 150
parallel: 4
startKIND: true
Expand Down
7 changes: 4 additions & 3 deletions operator/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,16 +19,17 @@ type SidecarConfig struct {
}

type SidecarConfigConfig struct {
ConfigMapName string `yaml:"name,omitempty"`
MountPath string `yaml:"mountPath,omitempty"`
NamespaceName string `yaml:"namespace,omitempty"`
Name string `yaml:"name,omitempty"`
MountPath string `yaml:"mountPath,omitempty"`
Namespace string `yaml:"namespace,omitempty"`
}

func ReadConfig(configPath string, config *Config) error {
content, err := os.ReadFile(configPath)
if err != nil {
return err
}

err = yaml.Unmarshal(content, config)
if err != nil {
return err
Expand Down
8 changes: 2 additions & 6 deletions operator/handler/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,7 @@ func (e PodExtender) extendPod(ctx context.Context, pod *corev1.Pod, tailingSide
},
}

if e.ConfigMapName != "" && e.ConfigMountPath != "" {
if e.ConfigMapName != "" && e.ConfigMountPath != "" && e.ConfigMapNamespace != "" {
volumeMounts = append(volumeMounts, corev1.VolumeMount{
Name: sidecarConfigurationName,
MountPath: e.ConfigMountPath,
Expand Down Expand Up @@ -505,7 +505,6 @@ func (e *PodExtender) handleDelete(ctx context.Context, req admission.Request) a
return admission.Allowed(fmt.Sprintf("Error while getting list of pods (%v); %s", err, deletionMessage))
}

configMapUsed := false
for _, p := range podList.Items {
// skip current pod as it is going to be removed anyway
if p.Name == pod.Name && p.Namespace == pod.Namespace {
Expand All @@ -515,13 +514,10 @@ func (e *PodExtender) handleDelete(ctx context.Context, req admission.Request) a
// check if sidecar configuration is attached to the p pod
for _, volume := range p.Spec.Volumes {
if volume.ConfigMap != nil && volume.ConfigMap.Name == e.ConfigMapName {
configMapUsed = true
// do not anything in case volume is used
return admission.Allowed(deletionMessage)
}
}
if configMapUsed {
return admission.Allowed(deletionMessage)
}
}

// delete configMap as it is not used by any pod
Expand Down
13 changes: 8 additions & 5 deletions operator/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,9 +65,12 @@ func main() {
ctrl.SetLogger(zap.New(zap.UseDevMode(true)))

config = GetDefaultConfig()
if configPath != "" && ReadConfig(configPath, &config) != nil {
setupLog.Error(err, "unable to read configuration", "configPath", configPath)
os.Exit(1)
if configPath != "" {
err = ReadConfig(configPath, &config)
if err != nil {
setupLog.Error(err, "unable to read configuration", "configPath", configPath)
os.Exit(1)
}
}

if err := config.Validate(); err != nil {
Expand Down Expand Up @@ -106,9 +109,9 @@ func main() {
Client: mgr.GetClient(),
TailingSidecarImage: config.Sidecar.Image,
TailingSidecarResources: config.Sidecar.Resources,
ConfigMapName: config.Sidecar.Config.ConfigMapName,
ConfigMapName: config.Sidecar.Config.Name,
ConfigMountPath: config.Sidecar.Config.MountPath,
ConfigMapNamespace: config.Sidecar.Config.NamespaceName,
ConfigMapNamespace: config.Sidecar.Config.Namespace,
},
})

Expand Down
14 changes: 14 additions & 0 deletions tests/modified/operator/daemonset-with-annotations/00-assert.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
apiVersion: apps/v1
kind: DaemonSet
metadata:
name: daemonset-with-annotations
status:
numberReady: 1
---
apiVersion: kuttl.dev/v1beta1
kind: TestAssert
collectors:
- selector: app=daemonset-with-annotations
commands:
- script: "[ $(kubectl logs -l app=daemonset-with-annotations -c tailing-sidecar-0 -n ${NAMESPACE} --tail 5 | grep modified | wc -l) -eq 5 ]"
- script: "[ $(kubectl logs -l app=daemonset-with-annotations -c tailing-sidecar-1 -n ${NAMESPACE} --tail 5 | grep modified | wc -l) -eq 5 ]"
15 changes: 15 additions & 0 deletions tests/modified/operator/deployment-with-annotations/00-assert.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
apiVersion: apps/v1
kind: Deployment
metadata:
name: deployment-with-annotations
status:
replicas: 1
readyReplicas: 1
---
apiVersion: kuttl.dev/v1beta1
kind: TestAssert
collectors:
- selector: app=deployment-with-annotations
commands:
- script: "[ $(kubectl logs -l app=deployment-with-annotations -c tailing-sidecar-0 -n ${NAMESPACE} --tail 5 | grep modified | wc -l) -eq 5 ]"
- script: "[ $(kubectl logs -l app=deployment-with-annotations -c tailing-sidecar-1 -n ${NAMESPACE} --tail 5 | grep modified | wc -l) -eq 5 ]"
22 changes: 22 additions & 0 deletions tests/modified/operator/deployment-with-annotations/01-assert.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
apiVersion: apps/v1
kind: Deployment
metadata:
name: deployment-with-annotations
status:
replicas: 1
readyReplicas: 1
---
apiVersion: v1
kind: Pod
status:
phase: Running
containerStatuses:
- name: count
- name: tailing-sidecar-0
---
apiVersion: kuttl.dev/v1beta1
kind: TestAssert
collectors:
- selector: app=deployment-with-annotations
commands:
- script: "[ $(kubectl logs -l app=deployment-with-annotations -c tailing-sidecar-0 -n ${NAMESPACE} --tail 5 | grep modified | wc -l) -eq 5 ]"
14 changes: 14 additions & 0 deletions tests/modified/operator/pod-with-annotations/00-assert.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
apiVersion: v1
kind: Pod
metadata:
name: pod-with-annotations
status:
phase: Running
---
apiVersion: kuttl.dev/v1beta1
kind: TestAssert
collectors:
- pod: pod-with-annotations
commands:
- script: "[ $(kubectl logs pod-with-annotations tailing-sidecar-0 -n ${NAMESPACE} --tail 5 | grep modified | wc -l) -eq 5 ]"
- script: "[ $(kubectl logs pod-with-annotations named-container -n ${NAMESPACE} --tail 5 | grep modified | wc -l) -eq 5 ]"
15 changes: 15 additions & 0 deletions tests/modified/operator/pod-with-annotations/01-assert.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
apiVersion: v1
kind: Pod
metadata:
name: pod-with-annotations-updated
status:
phase: Running
---
apiVersion: kuttl.dev/v1beta1
kind: TestAssert
collectors:
- pod: pod-with-annotations-updated
commands:
- script: "[ $(kubectl logs pod-with-annotations-updated tailing-sidecar-0 -n ${NAMESPACE} --tail 5 | grep modified | wc -l) -eq 5 ]"
- script: "[ $(kubectl logs pod-with-annotations-updated named-sidecar -n ${NAMESPACE} --tail 5 | grep modified | wc -l) -eq 5 ]"
- script: "[ $(kubectl logs pod-with-annotations-updated tailing-sidecar-1 -n ${NAMESPACE} --tail 5 | grep modified | wc -l) -eq 5 ]"
15 changes: 15 additions & 0 deletions tests/modified/operator/pod-with-cr/00-assert.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
apiVersion: v1
kind: Pod
metadata:
name: pod-with-tailing-sidecar-config
status:
phase: Running
---
apiVersion: kuttl.dev/v1beta1
kind: TestAssert
collectors:
- pod: pod-with-tailing-sidecar-config
commands:
- script: "[ $(kubectl logs pod-with-tailing-sidecar-config sidecar-0 -n ${NAMESPACE} --tail 5 | grep modified | wc -l) -eq 5 ]"
- script: "[ $(kubectl logs pod-with-tailing-sidecar-config sidecar-1 -n ${NAMESPACE} --tail 5 | grep modified | wc -l) -eq 5 ]"
- script: "[ $(kubectl logs pod-with-tailing-sidecar-config sidecar-2 -n ${NAMESPACE} --tail 5 | grep modified | wc -l) -eq 5 ]"
1 change: 1 addition & 0 deletions tests/modified/operator/pod-with-cr/00-install.yaml
16 changes: 16 additions & 0 deletions tests/modified/operator/pod-with-cr/01-assert.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
apiVersion: v1
kind: Pod
metadata:
name: pod-with-tailing-sidecar-config
status:
phase: Running
containerStatuses:
- name: count
- name: sidecar-2
---
apiVersion: kuttl.dev/v1beta1
kind: TestAssert
collectors:
- pod: pod-with-tailing-sidecar-config
commands:
- script: "[ $(kubectl logs pod-with-tailing-sidecar-config sidecar-2 -n ${NAMESPACE} --tail 5 | grep modified | wc -l) -eq 5 ]"
1 change: 1 addition & 0 deletions tests/modified/operator/pod-with-cr/01-update.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
apiVersion: v1
kind: Pod
metadata:
name: pod-without-annotations
status:
phase: Running
containerStatuses:
- name: count
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
apiVersion: apps/v1
kind: StatefulSet
metadata:
name: statefulset-with-annotations
status:
replicas: 1
readyReplicas: 1
---
apiVersion: kuttl.dev/v1beta1
kind: TestAssert
collectors:
- pod: statefulset-with-annotations-0
commands:
- script: "[ $(kubectl logs statefulset-with-annotations-0 my-named-sidecar -n ${NAMESPACE} --tail 5 | grep modified | wc -l) -eq 5 ]"
- script: "[ $(kubectl logs statefulset-with-annotations-0 tailing-sidecar-1 -n ${NAMESPACE} --tail 5 | grep modified | wc -l) -eq 5 ]"
14 changes: 14 additions & 0 deletions tests/modified/sidecar/pod-with-sidecar/00-assert.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
apiVersion: v1
kind: Pod
metadata:
name: example-with-tailling-sidecars
status:
phase: Running
---
apiVersion: kuttl.dev/v1beta1
kind: TestAssert
collectors:
- pod: example-with-tailling-sidecars
commands:
- script: "[ $(kubectl logs -n $NAMESPACE --tail 5 example-with-tailling-sidecars sidecar1 | grep modified | wc -l) -eq 5 ]"
- script: "[ $(kubectl logs -n $NAMESPACE --tail 5 example-with-tailling-sidecars sidecar2 | grep modified | wc -l) -eq 5 ]"
1 change: 1 addition & 0 deletions tests/modified/sidecar/pod-with-sidecar/00-install.yaml

0 comments on commit cbde34e

Please sign in to comment.