Skip to content

Commit ad8d07b

Browse files
naemonopebrc
andauthored
Fix beats stack monitoring cert reload (#8833)
Fix beats stack monitoring certificates not reloading automatically. --------- Signed-off-by: Michael Montgomery <[email protected]> Co-authored-by: Peter Brachwitz <[email protected]>
1 parent face37a commit ad8d07b

File tree

5 files changed

+73
-236
lines changed

5 files changed

+73
-236
lines changed

pkg/controller/beat/common/stackmon/stackmon_test.go

Lines changed: 46 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,34 @@ output:
117117
ssl:
118118
verification_mode: certificate
119119
username: es-user
120+
`
121+
beatYmlGT88 := `http:
122+
enabled: false
123+
metricbeat:
124+
modules:
125+
- hosts:
126+
- http+unix:///var/shared/metricbeat-test-beat.sock
127+
metricsets:
128+
- stats
129+
- state
130+
module: beat
131+
period: 10s
132+
xpack:
133+
enabled: true
134+
monitoring:
135+
cluster_uuid: %s
136+
enabled: false
137+
output:
138+
elasticsearch:
139+
hosts:
140+
- %s
141+
password: es-password
142+
ssl:
143+
restart_on_cert_change:
144+
enabled: true
145+
period: 1m
146+
verification_mode: certificate
147+
username: es-user
120148
`
121149
standaloneBeatYML := `http:
122150
enabled: false
@@ -299,6 +327,22 @@ output:
299327
want: beatSidecarFixture(standaloneBeatYML),
300328
wantErr: false,
301329
},
330+
{
331+
name: "beat > 8.8 with stack monitoring enabled and valid elasticsearchRef returns properly configured sidecar",
332+
args: args{
333+
client: k8s.NewFakeClient(&beatFixture, &esFixture, &monitoringEsFixture, &corev1.Secret{
334+
ObjectMeta: metav1.ObjectMeta{Name: "es-secret-name", Namespace: "test"},
335+
Data: map[string][]byte{"es-user": []byte("es-password")},
336+
}),
337+
beat: func() *v1beta1.Beat {
338+
beat := beatFixture.DeepCopy()
339+
beat.Spec.Version = "8.8.0"
340+
return beat
341+
},
342+
},
343+
want: beatSidecarFixture(fmt.Sprintf(beatYmlGT88, "abcd1234", "es-metrics-monitoring-url")),
344+
wantErr: false,
345+
},
302346
{
303347
name: "Beat with stack monitoring enabled and remote elasticsearchRef",
304348
args: args{
@@ -349,8 +393,8 @@ output:
349393
t.Errorf("MetricBeat() error = %v, wantErr %v", err, tt.wantErr)
350394
return
351395
}
352-
if !cmp.Equal(got, tt.want, cmpopts.IgnoreFields(stackmon.BeatSidecar{}, "ConfigHash")) {
353-
t.Errorf("MetricBeat() = diff: %s", cmp.Diff(got, tt.want, cmpopts.IgnoreFields(stackmon.BeatSidecar{}, "ConfigHash")))
396+
if !cmp.Equal(got, tt.want, cmpopts.IgnoreFields(stackmon.BeatSidecar{}, "ConfigHash"), cmpopts.IgnoreFields(corev1.Container{}, "Image")) {
397+
t.Errorf("MetricBeat() = diff: %s", cmp.Diff(got, tt.want, cmpopts.IgnoreFields(stackmon.BeatSidecar{}, "ConfigHash"), cmpopts.IgnoreFields(corev1.Container{}, "Image")))
354398
}
355399
})
356400
}

pkg/controller/common/stackmon/config.go

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ import (
2424
"github.com/elastic/cloud-on-k8s/v3/pkg/controller/common/metadata"
2525
"github.com/elastic/cloud-on-k8s/v3/pkg/controller/common/settings"
2626
"github.com/elastic/cloud-on-k8s/v3/pkg/controller/common/stackmon/monitoring"
27+
"github.com/elastic/cloud-on-k8s/v3/pkg/controller/common/version"
2728
"github.com/elastic/cloud-on-k8s/v3/pkg/controller/common/volume"
2829
"github.com/elastic/cloud-on-k8s/v3/pkg/utils/k8s"
2930
)
@@ -40,6 +41,7 @@ func newBeatConfig(
4041
ctx context.Context,
4142
client k8s.Client,
4243
beatName string,
44+
imageVersion string,
4345
resource monitoring.HasMonitoring,
4446
associations []commonv1.Association,
4547
baseConfig string,
@@ -52,7 +54,7 @@ func newBeatConfig(
5254
assoc := associations[0]
5355

5456
// build the output section of the beat configuration file
55-
outputCfg, caVolume, err := buildOutputConfig(ctx, client, assoc)
57+
outputCfg, caVolume, err := buildOutputConfig(ctx, client, assoc, imageVersion)
5658
if err != nil {
5759
return beatConfig{}, err
5860
}
@@ -112,7 +114,7 @@ func newBeatConfig(
112114
}, err
113115
}
114116

115-
func buildOutputConfig(ctx context.Context, client k8s.Client, assoc commonv1.Association) (map[string]interface{}, volume.VolumeLike, error) {
117+
func buildOutputConfig(ctx context.Context, client k8s.Client, assoc commonv1.Association, imageVersion string) (map[string]interface{}, volume.VolumeLike, error) {
116118
credentials, err := association.ElasticsearchAuthSettings(ctx, client, assoc)
117119
if err != nil {
118120
return nil, volume.SecretVolume{}, err
@@ -132,6 +134,19 @@ func buildOutputConfig(ctx context.Context, client k8s.Client, assoc commonv1.As
132134
// and therefore not being valid for the internal URL.
133135
outputConfig["ssl.verification_mode"] = "certificate"
134136

137+
v, err := version.Parse(imageVersion)
138+
if err != nil {
139+
return nil, nil, err
140+
}
141+
// Reloading of certificates is only supported for Beats >= 8.8.0.
142+
if v.GE(version.MinFor(8, 8, 0)) {
143+
// Allow beats to reload when the ssl certificate changes (renewals)
144+
outputConfig["ssl.restart_on_cert_change"] = map[string]interface{}{
145+
"enabled": true,
146+
"period": "1m",
147+
}
148+
}
149+
135150
caDirPath := fmt.Sprintf(
136151
"/mnt/elastic-internal/%s-association/%s/%s/certs",
137152
assoc.AssociationType(), assoc.AssociationRef().Namespace, assoc.AssociationRef().NameOrSecretName(),

pkg/controller/common/stackmon/config_test.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ func Test_newBeatConfig(t *testing.T) {
2525
initObjects []client.Object
2626
beatName string
2727
baseConfig string
28+
image string
2829
associated commonv1.Associated
2930
}
3031
tests := []struct {
@@ -52,6 +53,7 @@ param2: value2
5253
},
5354
},
5455
beatName: "metricbeat",
56+
image: "8.8.0",
5557
associated: &esv1.Elasticsearch{
5658
ObjectMeta: metav1.ObjectMeta{
5759
Name: "monitored",
@@ -91,6 +93,9 @@ param2: value2
9193
ssl:
9294
certificate_authorities:
9395
- /mnt/elastic-internal/es-monitoring-association/default/monitoring/certs/ca.crt
96+
restart_on_cert_change:
97+
enabled: true
98+
period: 1m
9499
verification_mode: certificate
95100
username: default-monitored-default-monitoring-beat-es-mon-user
96101
param1: value1
@@ -112,6 +117,7 @@ param2: value2
112117
context.Background(),
113118
fakeClient,
114119
tt.args.beatName,
120+
tt.args.image,
115121
hasMonitoring,
116122
tt.args.associated.GetAssociations(),
117123
tt.args.baseConfig,

pkg/controller/common/stackmon/sidecar.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ func NewMetricBeatSidecar(
3838
image := container.ImageRepository(container.MetricbeatImage, imageVersion)
3939
// EmptyDir volume so that MetricBeat does not write in the container image, which allows ReadOnlyRootFilesystem: true
4040
emptyDir := volume.NewEmptyDirVolume("metricbeat-data", "/usr/share/metricbeat/data")
41-
return NewBeatSidecar(ctx, client, "metricbeat", image, resource, monitoring.GetMetricsAssociation(resource), baseConfig, meta, caVolume, emptyDir)
41+
return NewBeatSidecar(ctx, client, "metricbeat", image, imageVersion.String(), resource, monitoring.GetMetricsAssociation(resource), baseConfig, meta, caVolume, emptyDir)
4242
}
4343

4444
func NewFileBeatSidecar(
@@ -57,7 +57,7 @@ func NewFileBeatSidecar(
5757
image := container.ImageRepository(container.FilebeatImage, v)
5858
// EmptyDir volume so that FileBeat does not write in the container image, which allows ReadOnlyRootFilesystem: true
5959
emptyDir := volume.NewEmptyDirVolume("filebeat-data", "/usr/share/filebeat/data")
60-
return NewBeatSidecar(ctx, client, "filebeat", image, resource, monitoring.GetLogsAssociation(resource), baseConfig, meta, additionalVolume, emptyDir)
60+
return NewBeatSidecar(ctx, client, "filebeat", image, imageVersion, resource, monitoring.GetLogsAssociation(resource), baseConfig, meta, additionalVolume, emptyDir)
6161
}
6262

6363
// BeatSidecar helps with building a beat sidecar container to monitor an Elastic Stack application. It focuses on
@@ -69,11 +69,11 @@ type BeatSidecar struct {
6969
Volumes []corev1.Volume
7070
}
7171

72-
func NewBeatSidecar(ctx context.Context, client k8s.Client, beatName string, image string, resource monitoring.HasMonitoring,
72+
func NewBeatSidecar(ctx context.Context, client k8s.Client, beatName string, image string, imageVersion string, resource monitoring.HasMonitoring,
7373
associations []commonv1.Association, baseConfig string, meta metadata.Metadata, additionalVolumes ...volume.VolumeLike,
7474
) (BeatSidecar, error) {
7575
// build the beat config
76-
config, err := newBeatConfig(ctx, client, beatName, resource, associations, baseConfig, meta)
76+
config, err := newBeatConfig(ctx, client, beatName, imageVersion, resource, associations, baseConfig, meta)
7777
if err != nil {
7878
return BeatSidecar{}, err
7979
}

0 commit comments

Comments
 (0)