Skip to content

Commit

Permalink
Fix Logstash crashes with env (#7475)
Browse files Browse the repository at this point in the history
Fixed: #7450

Logstash crashes when env variable name is in the list of
[env2yaml](https://github.com/elastic/logstash/blob/main/docker/data/logstash/env2yaml/env2yaml.go#L50-L155)
This PR changes the config init container to copy the `logstash.yml` to
`config` Volume to allow updating the file.

The following resource should start without error
```yaml
apiVersion: logstash.k8s.elastic.co/v1alpha1
kind: Logstash
metadata:
  name: logstash-sample
spec:
  count: 1
  version: 8.11.1
  podTemplate:
    spec:
      containers:
        - name: logstash
          env:
            - name: "NODE_NAME"
              value: "No_Crash!"
```
  • Loading branch information
kaisecheng committed Jan 25, 2024
1 parent 447ea8a commit 7ab8da0
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 1 deletion.
4 changes: 3 additions & 1 deletion pkg/controller/logstash/initcontainer.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ const (
UseTLSEnv = "USE_TLS"

// InitConfigScript is a small bash script to prepare the logstash configuration directory
// Logstash rewrites the configuration file (logstash.yml) in the presence of ${VAR} environment variable,
// therefore the file is copied to the path instead of creating symbolic link.
InitConfigScript = `#!/usr/bin/env bash
set -eu
Expand Down Expand Up @@ -46,7 +48,7 @@ echo "Setup Logstash configuration"
cp -f /usr/share/logstash/config/*.* "$mount_path"
ln -sf ` + volume.InternalConfigVolumeMountPath + `/` + ConfigFileName + ` $mount_path
cp ` + volume.InternalConfigVolumeMountPath + `/` + ConfigFileName + ` $mount_path
ln -sf ` + volume.InternalPipelineVolumeMountPath + `/` + PipelineFileName + ` $mount_path
touch "${init_config_initialized_flag}"
Expand Down
22 changes: 22 additions & 0 deletions test/e2e/logstash/logstash_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,28 @@ func TestSingleLogstash(t *testing.T) {
test.Sequence(nil, test.EmptySteps, logstashBuilder).RunSequential(t)
}

func TestLogstashWithEnv(t *testing.T) {
name := "test-env-logstash"
logstashBuilder := logstash.NewBuilder(name).
WithNodeCount(1).
WithPodTemplate(corev1.PodTemplateSpec{
Spec: corev1.PodSpec{
Containers: []corev1.Container{
{
Name: "logstash",
Env: []corev1.EnvVar{
{
Name: "NODE_NAME",
Value: "node01",
},
},
},
},
},
})
test.Sequence(nil, test.EmptySteps, logstashBuilder).RunSequential(t)
}

func TestLogstashWithCustomService(t *testing.T) {
name := "test-multiple-custom-logstash"
service := logstashv1alpha1.LogstashService{
Expand Down

0 comments on commit 7ab8da0

Please sign in to comment.