Skip to content

Commit 3ee82df

Browse files
authored
Init container updates for persisting kibana plugins. (#8389) (#8411)
* Init container for persisting kibana plugins. --------- Signed-off-by: Michael Montgomery <[email protected]> (cherry picked from commit 0efeb94)
1 parent 33caea2 commit 3ee82df

24 files changed

+840
-246
lines changed

.golangci.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
run:
2-
timeout: 360s
2+
timeout: 420s
33

44
linters-settings:
55
exhaustive:

docs/release-notes/highlights-2.16.0.asciidoc

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,8 @@ securityContext:
3232
readOnlyRootFilesystem: true
3333
----
3434

35+
Also note that this change will cause the Kibana pod(s) to be restarted as this change is applied during the upgrade.
36+
3537
[float]
3638
[id="{p}-2160-breaking-changes"]
3739
=== eck-fleet-server Helm chart breaking changes

pkg/apis/kibana/v1/name.go

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,11 @@ import (
88
common_name "github.com/elastic/cloud-on-k8s/v2/pkg/controller/common/name"
99
)
1010

11-
const httpServiceSuffix = "http"
11+
const (
12+
httpServiceSuffix = "http"
13+
scriptsConfigMapSuffix = "scripts"
14+
configSecretSuffix = "config"
15+
)
1216

1317
// KBNamer is a KBNamer that is configured with the defaults for resources related to a Kibana resource.
1418
var KBNamer = common_name.NewNamer("kb")
@@ -20,3 +24,13 @@ func HTTPService(kbName string) string {
2024
func Deployment(kbName string) string {
2125
return KBNamer.Suffix(kbName)
2226
}
27+
28+
// ScriptsConfigMap returns the name of the ConfigMap containing scripts for the given Kibana resource.
29+
func ScriptsConfigMap(kbName string) string {
30+
return KBNamer.Suffix(kbName, scriptsConfigMapSuffix)
31+
}
32+
33+
// ConfigSecret returns the name of the Secret containing the Kibana configuration for the given Kibana resource.
34+
func ConfigSecret(kbName string) string {
35+
return KBNamer.Suffix(kbName, configSecretSuffix)
36+
}

pkg/apis/kibana/v1/name_test.go

Lines changed: 29 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -8,25 +8,42 @@ import (
88
"testing"
99
)
1010

11-
func TestHTTPService(t *testing.T) {
12-
type args struct {
13-
kbName string
14-
}
11+
func TestNamers(t *testing.T) {
1512
tests := []struct {
16-
name string
17-
args args
18-
want string
13+
name string
14+
namer func(string) string
15+
arg string
16+
want string
1917
}{
2018
{
21-
name: "sample",
22-
args: args{kbName: "sample"},
23-
want: "sample-kb-http",
19+
name: "test httpService namer",
20+
namer: HTTPService,
21+
arg: "sample",
22+
want: "sample-kb-http",
23+
},
24+
{
25+
name: "test deployment namer",
26+
namer: Deployment,
27+
arg: "sample",
28+
want: "sample-kb",
29+
},
30+
{
31+
name: "test scripts configmap namer",
32+
namer: ScriptsConfigMap,
33+
arg: "sample",
34+
want: "sample-kb-scripts",
35+
},
36+
{
37+
name: "test ConfigSecret namer",
38+
namer: ConfigSecret,
39+
arg: "sample",
40+
want: "sample-kb-config",
2441
},
2542
}
2643
for _, tt := range tests {
2744
t.Run(tt.name, func(t *testing.T) {
28-
if got := HTTPService(tt.args.kbName); got != tt.want {
29-
t.Errorf("HTTPService() = %v, want %v", got, tt.want)
45+
if got := tt.namer(tt.arg); got != tt.want {
46+
t.Errorf("%s = %v, want %v", tt.name, got, tt.want)
3047
}
3148
})
3249
}

pkg/controller/kibana/config_reconcile.go

Lines changed: 2 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -18,51 +18,15 @@ import (
1818
"github.com/elastic/cloud-on-k8s/v2/pkg/controller/common/labels"
1919
"github.com/elastic/cloud-on-k8s/v2/pkg/controller/common/reconciler"
2020
"github.com/elastic/cloud-on-k8s/v2/pkg/controller/common/tracing"
21-
"github.com/elastic/cloud-on-k8s/v2/pkg/controller/common/volume"
2221
kblabel "github.com/elastic/cloud-on-k8s/v2/pkg/controller/kibana/label"
2322
"github.com/elastic/cloud-on-k8s/v2/pkg/utils/k8s"
2423
)
2524

2625
// Constants to use for the config files in a Kibana pod.
2726
const (
28-
ConfigVolumeName = "elastic-internal-kibana-config-local"
29-
ConfigVolumeMountPath = "/usr/share/kibana/config"
30-
InitContainerConfigVolumeMountPath = "/mnt/elastic-internal/kibana-config-local"
31-
32-
// InternalConfigVolumeName is a volume which contains the generated configuration.
33-
InternalConfigVolumeName = "elastic-internal-kibana-config"
34-
InternalConfigVolumeMountPath = "/mnt/elastic-internal/kibana-config"
35-
3627
TelemetryFilename = "telemetry.yml"
3728
)
3829

39-
var (
40-
// ConfigSharedVolume contains the Kibana config/ directory, it's an empty volume where the required configuration
41-
// is initialized by the elastic-internal-init-config init container. Its content is then shared by the init container
42-
// that creates the keystore and the main Kibana container.
43-
// This is needed in order to have in a same directory both the generated configuration and the keystore file which
44-
// is created in /usr/share/kibana/config since Kibana 7.9
45-
ConfigSharedVolume = volume.SharedVolume{
46-
VolumeName: ConfigVolumeName,
47-
InitContainerMountPath: InitContainerConfigVolumeMountPath,
48-
ContainerMountPath: ConfigVolumeMountPath,
49-
}
50-
)
51-
52-
// ConfigVolume returns a SecretVolume to hold the Kibana config of the given Kibana resource.
53-
func ConfigVolume(kb kbv1.Kibana) volume.SecretVolume {
54-
return volume.NewSecretVolumeWithMountPath(
55-
SecretName(kb),
56-
InternalConfigVolumeName,
57-
InternalConfigVolumeMountPath,
58-
)
59-
}
60-
61-
// SecretName is the name of the secret that holds the Kibana config for the given Kibana resource.
62-
func SecretName(kb kbv1.Kibana) string {
63-
return kb.Name + "-kb-config"
64-
}
65-
6630
// ReconcileConfigSecret reconciles the expected Kibana config secret for the given Kibana resource.
6731
// This managed secret is mounted into each pod of the Kibana deployment.
6832
func ReconcileConfigSecret(
@@ -95,7 +59,7 @@ func ReconcileConfigSecret(
9559
expected := corev1.Secret{
9660
ObjectMeta: metav1.ObjectMeta{
9761
Namespace: kb.Namespace,
98-
Name: SecretName(kb),
62+
Name: kbv1.ConfigSecret(kb.Name),
9963
Labels: labels.AddCredentialsLabel(map[string]string{
10064
kblabel.KibanaNameLabelName: kb.Name,
10165
}),
@@ -111,7 +75,7 @@ func ReconcileConfigSecret(
11175
// if the Secret or usage key doesn't exist yet.
11276
func getTelemetryYamlBytes(client k8s.Client, kb kbv1.Kibana) ([]byte, error) {
11377
var secret corev1.Secret
114-
if err := client.Get(context.Background(), types.NamespacedName{Namespace: kb.Namespace, Name: SecretName(kb)}, &secret); err != nil {
78+
if err := client.Get(context.Background(), types.NamespacedName{Namespace: kb.Namespace, Name: kbv1.ConfigSecret(kb.Name)}, &secret); err != nil {
11579
if apierrors.IsNotFound(err) {
11680
// this secret is just about to be created, we don't know usage yet
11781
return nil, nil

pkg/controller/kibana/config_settings.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -199,7 +199,7 @@ type reusableSettings struct {
199199
func getExistingConfig(ctx context.Context, client k8s.Client, kb kbv1.Kibana) (*settings.CanonicalConfig, error) {
200200
log := ulog.FromContext(ctx)
201201
var secret corev1.Secret
202-
err := client.Get(context.Background(), types.NamespacedName{Name: SecretName(kb), Namespace: kb.Namespace}, &secret)
202+
err := client.Get(context.Background(), types.NamespacedName{Name: kbv1.ConfigSecret(kb.Name), Namespace: kb.Namespace}, &secret)
203203
if err != nil && apierrors.IsNotFound(err) {
204204
log.V(1).Info("Kibana config secret does not exist", "namespace", kb.Namespace, "kibana_name", kb.Name)
205205
return nil, nil

pkg/controller/kibana/config_settings_test.go

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ func Test_reuseOrGenerateSecrets(t *testing.T) {
104104
args: args{
105105
c: k8s.NewFakeClient(
106106
&corev1.Secret{
107-
ObjectMeta: metav1.ObjectMeta{Namespace: defaultKb.Namespace, Name: SecretName(defaultKb)},
107+
ObjectMeta: metav1.ObjectMeta{Namespace: defaultKb.Namespace, Name: kbv1.ConfigSecret(defaultKb.Name)},
108108
Data: map[string][]byte{
109109
SettingsFilename: defaultConfig,
110110
},
@@ -127,7 +127,7 @@ func Test_reuseOrGenerateSecrets(t *testing.T) {
127127
args: args{
128128
c: k8s.NewFakeClient(
129129
&corev1.Secret{
130-
ObjectMeta: metav1.ObjectMeta{Namespace: defaultKb.Namespace, Name: SecretName(defaultKb)},
130+
ObjectMeta: metav1.ObjectMeta{Namespace: defaultKb.Namespace, Name: kbv1.ConfigSecret(defaultKb.Name)},
131131
Data: map[string][]byte{
132132
SettingsFilename: esAssociationConfig,
133133
},
@@ -151,7 +151,7 @@ func Test_reuseOrGenerateSecrets(t *testing.T) {
151151
args: args{
152152
c: k8s.NewFakeClient(
153153
&corev1.Secret{
154-
ObjectMeta: metav1.ObjectMeta{Namespace: defaultKb.Namespace, Name: SecretName(defaultKb)},
154+
ObjectMeta: metav1.ObjectMeta{Namespace: defaultKb.Namespace, Name: kbv1.ConfigSecret(defaultKb.Name)},
155155
Data: map[string][]byte{
156156
SettingsFilename: esAssociationConfig,
157157
},
@@ -186,7 +186,7 @@ func TestNewConfigSettings(t *testing.T) {
186186
defaultKb := mkKibana()
187187
existingSecret := &corev1.Secret{
188188
ObjectMeta: metav1.ObjectMeta{
189-
Name: SecretName(defaultKb),
189+
Name: kbv1.ConfigSecret(defaultKb.Name),
190190
Namespace: defaultKb.Namespace,
191191
},
192192
Data: map[string][]byte{
@@ -519,7 +519,7 @@ func TestNewConfigSettings(t *testing.T) {
519519
args: args{
520520
client: k8s.NewFakeClient(&corev1.Secret{
521521
ObjectMeta: metav1.ObjectMeta{
522-
Name: SecretName(defaultKb),
522+
Name: kbv1.ConfigSecret(defaultKb.Name),
523523
Namespace: defaultKb.Namespace,
524524
},
525525
Data: map[string][]byte{
@@ -544,7 +544,7 @@ func TestNewConfigSettings(t *testing.T) {
544544
args: args{
545545
client: k8s.NewFakeClient(&corev1.Secret{
546546
ObjectMeta: metav1.ObjectMeta{
547-
Name: SecretName(defaultKb),
547+
Name: kbv1.ConfigSecret(defaultKb.Name),
548548
Namespace: defaultKb.Namespace,
549549
},
550550
Data: map[string][]byte{
@@ -607,7 +607,7 @@ func TestNewConfigSettingsExistingEncryptionKey(t *testing.T) {
607607
savedObjsKey := "savedObjsKey"
608608
existingSecret := &corev1.Secret{
609609
ObjectMeta: metav1.ObjectMeta{
610-
Name: SecretName(kb),
610+
Name: kbv1.ConfigSecret(kb.Name),
611611
Namespace: kb.Namespace,
612612
},
613613
Data: map[string][]byte{
@@ -686,7 +686,7 @@ func Test_getExistingConfig(t *testing.T) {
686686
}
687687
testValidSecret := corev1.Secret{
688688
ObjectMeta: metav1.ObjectMeta{
689-
Name: SecretName(testKb),
689+
Name: kbv1.ConfigSecret(testKb.Name),
690690
Namespace: testKb.Namespace,
691691
},
692692
Data: map[string][]byte{
@@ -695,7 +695,7 @@ func Test_getExistingConfig(t *testing.T) {
695695
}
696696
testNoYaml := corev1.Secret{
697697
ObjectMeta: metav1.ObjectMeta{
698-
Name: SecretName(testKb),
698+
Name: kbv1.ConfigSecret(testKb.Name),
699699
Namespace: testKb.Namespace,
700700
},
701701
Data: map[string][]byte{
@@ -704,7 +704,7 @@ func Test_getExistingConfig(t *testing.T) {
704704
}
705705
testInvalidYaml := corev1.Secret{
706706
ObjectMeta: metav1.ObjectMeta{
707-
Name: SecretName(testKb),
707+
Name: kbv1.ConfigSecret(testKb.Name),
708708
Namespace: testKb.Namespace,
709709
},
710710
Data: map[string][]byte{

pkg/controller/kibana/controller.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,14 @@ func addWatches(mgr manager.Manager, c controller.Controller, r *ReconcileKibana
101101
return err
102102
}
103103

104+
// Watch configmaps
105+
if err := c.Watch(source.Kind(mgr.GetCache(), &corev1.ConfigMap{}, handler.TypedEnqueueRequestForOwner[*corev1.ConfigMap](
106+
mgr.GetScheme(), mgr.GetRESTMapper(),
107+
&kbv1.Kibana{}, handler.OnlyControllerOwner(),
108+
))); err != nil {
109+
return err
110+
}
111+
104112
// dynamically watch referenced secrets to connect to Elasticsearch
105113
return c.Watch(source.Kind(mgr.GetCache(), &corev1.Secret{}, r.dynamicWatches.Secrets))
106114
}

pkg/controller/kibana/driver.go

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ import (
3333
"github.com/elastic/cloud-on-k8s/v2/pkg/controller/common/version"
3434
commonvolume "github.com/elastic/cloud-on-k8s/v2/pkg/controller/common/volume"
3535
"github.com/elastic/cloud-on-k8s/v2/pkg/controller/common/watches"
36+
"github.com/elastic/cloud-on-k8s/v2/pkg/controller/kibana/initcontainer"
3637
kblabel "github.com/elastic/cloud-on-k8s/v2/pkg/controller/kibana/label"
3738
"github.com/elastic/cloud-on-k8s/v2/pkg/controller/kibana/network"
3839
"github.com/elastic/cloud-on-k8s/v2/pkg/controller/kibana/stackmon"
@@ -160,8 +161,7 @@ func (d *driver) Reconcile(
160161
return results.WithError(err)
161162
}
162163

163-
err = ReconcileConfigSecret(ctx, d.client, *kb, kbSettings)
164-
if err != nil {
164+
if err = ReconcileConfigSecret(ctx, d.client, *kb, kbSettings); err != nil {
165165
return results.WithError(err)
166166
}
167167

@@ -170,8 +170,11 @@ func (d *driver) Reconcile(
170170
return results.WithError(err)
171171
}
172172

173-
err = stackmon.ReconcileConfigSecrets(ctx, d.client, *kb, basePath)
174-
if err != nil {
173+
if err = stackmon.ReconcileConfigSecrets(ctx, d.client, *kb, basePath); err != nil {
174+
return results.WithError(err)
175+
}
176+
177+
if err = initcontainer.ReconcileScriptsConfigMap(ctx, d.client, *kb); err != nil {
175178
return results.WithError(err)
176179
}
177180

@@ -226,7 +229,7 @@ func (d *driver) getStrategyType(kb *kbv1.Kibana) (appsv1.DeploymentStrategyType
226229
}
227230

228231
func (d *driver) deploymentParams(ctx context.Context, kb *kbv1.Kibana, policyAnnotations map[string]string, basePath string, setDefaultSecurityContext bool) (deployment.Params, error) {
229-
initContainersParameters, err := newInitContainersParameters(kb)
232+
initContainersParameters, err := initcontainer.NewInitContainersParameters(kb)
230233
if err != nil {
231234
return deployment.Params{}, err
232235
}
@@ -282,7 +285,7 @@ func (d *driver) deploymentParams(ctx context.Context, kb *kbv1.Kibana, policyAn
282285

283286
// get config secret to add its content to the config checksum
284287
configSecret := corev1.Secret{}
285-
err = d.client.Get(ctx, types.NamespacedName{Name: SecretName(*kb), Namespace: kb.Namespace}, &configSecret)
288+
err = d.client.Get(ctx, types.NamespacedName{Name: kbv1.ConfigSecret(kb.Name), Namespace: kb.Namespace}, &configSecret)
286289
if err != nil {
287290
return deployment.Params{}, err
288291
}
@@ -314,7 +317,7 @@ func (d *driver) deploymentParams(ctx context.Context, kb *kbv1.Kibana, policyAn
314317
}
315318

316319
func (d *driver) buildVolumes(kb *kbv1.Kibana) ([]commonvolume.VolumeLike, error) {
317-
volumes := []commonvolume.VolumeLike{DataVolume, ConfigSharedVolume, ConfigVolume(*kb)}
320+
volumes := []commonvolume.VolumeLike{DataVolume, initcontainer.ConfigSharedVolume, initcontainer.ConfigVolume(*kb)}
318321

319322
esAssocConf, err := kb.EsAssociation().AssociationConf()
320323
if err != nil {

0 commit comments

Comments
 (0)