Skip to content

Commit

Permalink
Add a custom config parameter to the Kibana spec.
Browse files Browse the repository at this point in the history
Any YAML here ends up in `kibana.yml`.

Example usage:

```yaml
apiVersion: kibana.k8s.elastic.co/v1alpha1
kind: Kibana
metadata:
  name: kibana-sample
spec:
  version: "7.1.0"
  nodeCount: 1
  elasticsearchRef:
    name: "elasticsearch-sample"
  config:
    xpack.security.sessionTimeout: 600000
```

Closes: elastic#740
  • Loading branch information
nkvoll committed Jun 8, 2019
1 parent 26052a4 commit 5b97d34
Show file tree
Hide file tree
Showing 5 changed files with 111 additions and 1 deletion.
3 changes: 3 additions & 0 deletions operators/config/crds/kibana_v1alpha1_kibana.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,9 @@ spec:
type: object
spec:
properties:
config:
description: Config represents Kibana configuration.
type: object
elasticsearch:
description: Elasticsearch configures how Kibana connects to Elasticsearch
properties:
Expand Down
3 changes: 3 additions & 0 deletions operators/pkg/apis/kibana/v1alpha1/kibana_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,9 @@ type KibanaSpec struct {
// +optional
Elasticsearch BackendElasticsearch `json:"elasticsearch,omitempty"`

// Config represents Kibana configuration.
Config *commonv1alpha1.Config `json:"config,omitempty"`

// HTTP contains settings for HTTP.
HTTP commonv1alpha1.HTTPConfig `json:"http,omitempty"`

Expand Down
4 changes: 4 additions & 0 deletions operators/pkg/apis/kibana/v1alpha1/zz_generated.deepcopy.go

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

11 changes: 10 additions & 1 deletion operators/pkg/controller/kibana/config/settings.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ package config
import (
"path"

commonv1alpha1 "github.com/elastic/cloud-on-k8s/operators/pkg/apis/common/v1alpha1"
"github.com/elastic/cloud-on-k8s/operators/pkg/apis/kibana/v1alpha1"
"github.com/elastic/cloud-on-k8s/operators/pkg/controller/common/certificates"
"github.com/elastic/cloud-on-k8s/operators/pkg/controller/common/settings"
Expand All @@ -25,7 +26,15 @@ type CanonicalConfig struct {

// NewConfigSettings returns the Kibana configuration settings for the given Kibana resource.
func NewConfigSettings(client k8s.Client, kb v1alpha1.Kibana) (CanonicalConfig, error) {
cfg := settings.NewCanonicalConfig()
userConfig := kb.Spec.Config
if userConfig == nil {
userConfig = &commonv1alpha1.Config{}
}

cfg, err := settings.NewCanonicalConfigFrom(userConfig.Data)
if err != nil {
return CanonicalConfig{}, err
}

esAuthSettings, err := elasticsearchAuthSettings(client, kb)
if err != nil {
Expand Down
91 changes: 91 additions & 0 deletions operators/pkg/controller/kibana/config/settings_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
// Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
// or more contributor license agreements. Licensed under the Elastic License;
// you may not use this file except in compliance with the Elastic License.

package config

import (
"testing"

commonv1alpha1 "github.com/elastic/cloud-on-k8s/operators/pkg/apis/common/v1alpha1"
"github.com/elastic/cloud-on-k8s/operators/pkg/apis/kibana/v1alpha1"
"github.com/elastic/cloud-on-k8s/operators/pkg/utils/k8s"
uyaml "github.com/elastic/go-ucfg/yaml"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

var defaultConfig = []byte(`
elasticsearch:
hosts:
- ""
ssl:
certificateAuthorities: /usr/share/kibana/config/elasticsearch-certs/ca.crt
verificationMode: certificate
server:
host: "0"
name: ""
xpack:
monitoring:
ui:
container:
elasticsearch:
enabled: true
`)

func TestNewConfigSettings(t *testing.T) {
type args struct {
client k8s.Client
kb v1alpha1.Kibana
}
tests := []struct {
name string
args args
want []byte
wantErr bool
}{
{
name: "default config",
args: args{
kb: v1alpha1.Kibana{},
},
want: defaultConfig,
},
{
name: "with user config",
args: args{
kb: v1alpha1.Kibana{
Spec: v1alpha1.KibanaSpec{
Config: &commonv1alpha1.Config{
Data: map[string]interface{}{
"foo": "bar",
},
},
},
},
},
want: append(defaultConfig, []byte(`foo: bar`)...),
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, err := NewConfigSettings(tt.args.client, tt.args.kb)
if tt.wantErr {
require.NotNil(t, err)
}
require.NoError(t, err)

// convert "got" into something comparable
var gotCfg map[string]interface{}
require.NoError(t, got.Unpack(&gotCfg))

// convert "want" into something comparable
cfg, err := uyaml.NewConfig(tt.want, commonv1alpha1.CfgOptions...)
require.NoError(t, err)
var wantCfg map[string]interface{}
require.NoError(t, cfg.Unpack(&wantCfg))

assert.Equal(t, wantCfg, gotCfg)
})
}
}

0 comments on commit 5b97d34

Please sign in to comment.