Skip to content

Commit

Permalink
Only add Kibana config for Beats that support it
Browse files Browse the repository at this point in the history
  • Loading branch information
pebrc committed Jun 10, 2020
1 parent 6160a3b commit b526853
Show file tree
Hide file tree
Showing 10 changed files with 96 additions and 30 deletions.
4 changes: 2 additions & 2 deletions config/crds/all-crds.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -557,8 +557,8 @@ spec:
type: string
kibanaRef:
description: KibanaRef is a reference to a Kibana instance running in
the same Kubernetes cluster. It allows APM agent central configuration
management in Kibana.
the same Kubernetes cluster. It allows automatic setup of dashboards
and visualizations.
properties:
name:
description: Name of the Kubernetes object.
Expand Down
4 changes: 2 additions & 2 deletions config/crds/bases/beat.k8s.elastic.co_beats.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -12162,8 +12162,8 @@ spec:
type: string
kibanaRef:
description: KibanaRef is a reference to a Kibana instance running in
the same Kubernetes cluster. It allows APM agent central configuration
management in Kibana.
the same Kubernetes cluster. It allows automatic setup of dashboards
and visualizations.
properties:
name:
description: Name of the Kubernetes object.
Expand Down
22 changes: 14 additions & 8 deletions pkg/controller/beat/common/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,13 @@ import (
"github.com/elastic/cloud-on-k8s/pkg/utils/k8s"
)

type DefaultConfigs struct {
// Managed config is handled by ECK and will not be nullified by user provided config.
Managed *settings.CanonicalConfig
// Unmanaged config is default config that will be overridden by any user provided config.
Unmanaged *settings.CanonicalConfig
}

// setOutput will set the output section in Beat config according to the association configuration.
func buildOutputConfig(client k8s.Client, associated beatv1beta1.BeatESAssociation) (*settings.CanonicalConfig, error) {
if !associated.AssociationConf().IsConfigured() {
Expand All @@ -46,7 +53,8 @@ func buildOutputConfig(client k8s.Client, associated beatv1beta1.BeatESAssociati
return settings.MustCanonicalConfig(esOutput), nil
}

func buildKibanaConfig(client k8s.Client, associated beatv1beta1.BeatKibanaAssociation) (*settings.CanonicalConfig, error) {
// BuildKibanaConfig builds on optional Kibana configuration for dashboard setup and visualizations.
func BuildKibanaConfig(client k8s.Client, associated beatv1beta1.BeatKibanaAssociation) (*settings.CanonicalConfig, error) {
if !associated.AssociationConf().IsConfigured() {
return settings.NewCanonicalConfig(), nil
}
Expand Down Expand Up @@ -75,24 +83,22 @@ func buildBeatConfig(
log logr.Logger,
client k8s.Client,
beat beatv1beta1.Beat,
defaultConfig *settings.CanonicalConfig,
defaultConfigs DefaultConfigs,
) ([]byte, error) {
cfg := settings.NewCanonicalConfig()

outputCfg, err := buildOutputConfig(client, beatv1beta1.BeatESAssociation{Beat: &beat})
if err != nil {
return nil, err
}
kibanaCfg, err := buildKibanaConfig(client, beatv1beta1.BeatKibanaAssociation{Beat: &beat})

err = cfg.MergeWith(outputCfg, kibanaCfg)
err = cfg.MergeWith(outputCfg, defaultConfigs.Managed)
if err != nil {
return nil, err
}
// use only the default config or only the provided config - no overriding, no merging
userConfig := beat.Spec.Config
if userConfig == nil {
if err := cfg.MergeWith(defaultConfig); err != nil {
if err := cfg.MergeWith(defaultConfigs.Unmanaged); err != nil {
return nil, err
}
} else {
Expand All @@ -112,10 +118,10 @@ func buildBeatConfig(

func reconcileConfig(
params DriverParams,
defaultConfig *settings.CanonicalConfig,
defaultConfigs DefaultConfigs,
configHash hash.Hash,
) error {
cfgBytes, err := buildBeatConfig(params.Logger, params.Client, params.Beat, defaultConfig)
cfgBytes, err := buildBeatConfig(params.Logger, params.Client, params.Beat, defaultConfigs)
if err != nil {
return err
}
Expand Down
45 changes: 34 additions & 11 deletions pkg/controller/beat/common/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,8 @@ func Test_buildBeatConfig(t *testing.T) {
},
)

defaultConfig := settings.MustParseConfig([]byte("default: true"))
unmanagedDefaultCfg := settings.MustParseConfig([]byte("default: true"))
managedDefaultCfg := settings.MustParseConfig([]byte("setup.kibana: true"))
userConfig := &commonv1.Config{Data: map[string]interface{}{"user": "true"}}
userCanonicalConfig := settings.MustCanonicalConfig(userConfig.Data)
outputCAYaml := settings.MustParseConfig([]byte(`output.elasticsearch.ssl.certificate_authorities:
Expand Down Expand Up @@ -83,7 +84,7 @@ func Test_buildBeatConfig(t *testing.T) {
name string
client k8s.Client
beat beatv1beta1.Beat
defaultConfig *settings.CanonicalConfig
defaultConfig DefaultConfigs
want *settings.CanonicalConfig
wantErr bool
}{
Expand All @@ -94,8 +95,8 @@ func Test_buildBeatConfig(t *testing.T) {
{
name: "no association, only default config",
beat: beatv1beta1.Beat{},
defaultConfig: defaultConfig,
want: defaultConfig,
defaultConfig: DefaultConfigs{Unmanaged: unmanagedDefaultCfg},
want: unmanagedDefaultCfg,
},
{
name: "no association, only user config",
Expand All @@ -109,15 +110,27 @@ func Test_buildBeatConfig(t *testing.T) {
beat: beatv1beta1.Beat{Spec: beatv1beta1.BeatSpec{
Config: userConfig,
}},
defaultConfig: defaultConfig,
defaultConfig: DefaultConfigs{Unmanaged: unmanagedDefaultCfg},
want: userCanonicalConfig,
},
{
name: "no association, both default configs and user config",
beat: beatv1beta1.Beat{Spec: beatv1beta1.BeatSpec{
Config: userConfig,
}},
defaultConfig: DefaultConfigs{
Unmanaged: unmanagedDefaultCfg,
Managed: managedDefaultCfg,
},
want: merge(userCanonicalConfig, managedDefaultCfg),
},

{
name: "association without ca, only default config",
client: clientWithSecret,
beat: withAssociation,
defaultConfig: defaultConfig,
want: merge(defaultConfig, outputYaml),
defaultConfig: DefaultConfigs{Unmanaged: unmanagedDefaultCfg},
want: merge(unmanagedDefaultCfg, outputYaml),
},
{
name: "association without ca, only user config",
Expand All @@ -129,15 +142,15 @@ func Test_buildBeatConfig(t *testing.T) {
name: "association without ca, default and user config",
client: clientWithSecret,
beat: withAssociationWithConfig,
defaultConfig: defaultConfig,
defaultConfig: DefaultConfigs{Unmanaged: unmanagedDefaultCfg},
want: merge(userCanonicalConfig, outputYaml),
},
{
name: "association with ca, only default config",
client: clientWithSecret,
beat: withAssociationWithCA,
defaultConfig: defaultConfig,
want: merge(defaultConfig, outputYaml, outputCAYaml),
defaultConfig: DefaultConfigs{Unmanaged: unmanagedDefaultCfg},
want: merge(unmanagedDefaultCfg, outputYaml, outputCAYaml),
},
{
name: "association with ca, only user config",
Expand All @@ -149,9 +162,19 @@ func Test_buildBeatConfig(t *testing.T) {
name: "association with ca, default and user config",
client: clientWithSecret,
beat: withAssociationWithCAAndConfig,
defaultConfig: defaultConfig,
defaultConfig: DefaultConfigs{Unmanaged: unmanagedDefaultCfg},
want: merge(userCanonicalConfig, outputYaml, outputCAYaml),
},
{
name: "association with ca, both default configs and user config",
client: clientWithSecret,
beat: withAssociationWithCAAndConfig,
defaultConfig: DefaultConfigs{
Unmanaged: unmanagedDefaultCfg,
Managed: managedDefaultCfg,
},
want: merge(userCanonicalConfig, managedDefaultCfg, outputYaml, outputCAYaml),
},
} {
t.Run(tt.name, func(t *testing.T) {
gotYaml, gotErr := buildBeatConfig(logrtesting.NullLogger{}, tt.client, tt.beat, tt.defaultConfig)
Expand Down
5 changes: 2 additions & 3 deletions pkg/controller/beat/common/driver.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ import (
"github.com/elastic/cloud-on-k8s/pkg/controller/common/container"
"github.com/elastic/cloud-on-k8s/pkg/controller/common/defaults"
"github.com/elastic/cloud-on-k8s/pkg/controller/common/reconciler"
"github.com/elastic/cloud-on-k8s/pkg/controller/common/settings"
"github.com/elastic/cloud-on-k8s/pkg/utils/k8s"
)

Expand Down Expand Up @@ -56,7 +55,7 @@ func ValidateBeatSpec(spec beatv1beta1.BeatSpec) error {

func Reconcile(
params DriverParams,
defaultConfig *settings.CanonicalConfig,
defaultConfigs DefaultConfigs,
defaultImage container.Image,
modifyPodFunc func(builder *defaults.PodTemplateBuilder),
) *reconciler.Results {
Expand All @@ -71,7 +70,7 @@ func Reconcile(
}

configHash := sha256.New224()
if err := reconcileConfig(params, defaultConfig, configHash); err != nil {
if err := reconcileConfig(params, defaultConfigs, configHash); err != nil {
return results.WithError(err)
}

Expand Down
17 changes: 16 additions & 1 deletion pkg/controller/beat/filebeat/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,11 @@

package filebeat

import "github.com/elastic/cloud-on-k8s/pkg/controller/common/settings"
import (
"github.com/elastic/cloud-on-k8s/pkg/apis/beat/v1beta1"
"github.com/elastic/cloud-on-k8s/pkg/controller/beat/common"
"github.com/elastic/cloud-on-k8s/pkg/controller/common/settings"
)

var (
defaultConfig = settings.MustParseConfig([]byte(
Expand All @@ -24,3 +28,14 @@ processors:
- add_host_metadata: {}
`))
)

func (d *Driver) configParams() (common.DefaultConfigs, error) {
kibanaConfig, err := common.BuildKibanaConfig(d.Client, v1beta1.BeatKibanaAssociation{Beat: &d.Beat})
if err != nil {
return common.DefaultConfigs{}, err
}
return common.DefaultConfigs{
Managed: kibanaConfig,
Unmanaged: defaultConfig,
}, nil
}
7 changes: 6 additions & 1 deletion pkg/controller/beat/filebeat/filebeat.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,9 +59,14 @@ func (d *Driver) Reconcile() *reconciler.Results {
}
}

defaultConfigs, err := d.configParams()
if err != nil {
return reconciler.NewResult(d.DriverParams.Context).WithError(err)
}

return beatcommon.Reconcile(
d.DriverParams,
defaultConfig,
defaultConfigs,
container.FilebeatImage,
f)
}
13 changes: 13 additions & 0 deletions pkg/controller/beat/metricbeat/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
package metricbeat

import (
"github.com/elastic/cloud-on-k8s/pkg/apis/beat/v1beta1"
"github.com/elastic/cloud-on-k8s/pkg/controller/beat/common"
"github.com/elastic/cloud-on-k8s/pkg/controller/common/settings"
)

Expand Down Expand Up @@ -42,3 +44,14 @@ processors:
- add_cloud_metadata: {}
`))
)

func (d *Driver) configParams() (common.DefaultConfigs, error) {
kibanaConfig, err := common.BuildKibanaConfig(d.Client, v1beta1.BeatKibanaAssociation{Beat: &d.Beat})
if err != nil {
return common.DefaultConfigs{}, err
}
return common.DefaultConfigs{
Managed: kibanaConfig,
Unmanaged: defaultConfig,
}, nil
}
7 changes: 6 additions & 1 deletion pkg/controller/beat/metricbeat/metricbeat.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,9 +62,14 @@ func (d *Driver) Reconcile() *reconciler.Results {
builder.WithArgs("-e", "-c", beatcommon.ConfigMountPath, "-system.hostfs=/hostfs")
}

defaultConfigs, err := d.configParams()
if err != nil {
return reconciler.NewResult(d.DriverParams.Context).WithError(err)
}

return beatcommon.Reconcile(
d.DriverParams,
defaultConfig,
defaultConfigs,
container.MetricbeatImage,
f)
}
2 changes: 1 addition & 1 deletion pkg/controller/beat/otherbeat/otherbeat.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,5 +28,5 @@ func NewDriver(params beatcommon.DriverParams) beatcommon.Driver {
}

func (d *Driver) Reconcile() *reconciler.Results {
return beatcommon.Reconcile(d.DriverParams, nil, "", nil)
return beatcommon.Reconcile(d.DriverParams, beatcommon.DefaultConfigs{}, "", nil)
}

0 comments on commit b526853

Please sign in to comment.