-
Notifications
You must be signed in to change notification settings - Fork 4.9k
/
Copy pathconfig.go
110 lines (93 loc) · 3.65 KB
/
config.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
// Licensed to Elasticsearch B.V. under one or more contributor
// license agreements. See the NOTICE file distributed with
// this work for additional information regarding copyright
// ownership. Elasticsearch B.V. licenses this file to you under
// the Apache License, Version 2.0 (the "License"); you may
// not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. See the License for the
// specific language governing permissions and limitations
// under the License.
// +build linux darwin windows
package kubernetes
import (
"fmt"
"time"
"github.com/elastic/beats/v7/libbeat/common/kubernetes/metadata"
"github.com/elastic/beats/v7/libbeat/autodiscover/template"
"github.com/elastic/beats/v7/libbeat/common"
"github.com/elastic/beats/v7/libbeat/common/cfgwarn"
"github.com/elastic/beats/v7/libbeat/logp"
)
// Config for kubernetes autodiscover provider
type Config struct {
KubeConfig string `config:"kube_config"`
Namespace string `config:"namespace"`
SyncPeriod time.Duration `config:"sync_period"`
CleanupTimeout time.Duration `config:"cleanup_timeout" validate:"positive"`
// Needed when resource is a pod
HostDeprecated string `config:"host"`
Node string `config:"node"`
// Scope can be either node or cluster.
Scope string `config:"scope"`
Resource string `config:"resource"`
// Unique identifies if this provider enables its templates only when it is elected as leader in a k8s cluster
Unique bool `config:"unique"`
LeaderLease string `config:"leader_lease"`
Prefix string `config:"prefix"`
Hints *common.Config `config:"hints"`
Builders []*common.Config `config:"builders"`
Appenders []*common.Config `config:"appenders"`
Templates template.MapperSettings `config:"templates"`
AddResourceMetadata *metadata.AddResourceMetadataConfig `config:"add_resource_metadata"`
}
func defaultConfig() *Config {
return &Config{
SyncPeriod: 10 * time.Minute,
Resource: "pod",
CleanupTimeout: 60 * time.Second,
Prefix: "co.elastic",
Unique: false,
}
}
// Validate ensures correctness of config
func (c *Config) Validate() error {
// Make sure that prefix doesn't ends with a '.'
if c.Prefix[len(c.Prefix)-1] == '.' && c.Prefix != "." {
c.Prefix = c.Prefix[:len(c.Prefix)-2]
}
if len(c.Templates) == 0 && !c.Hints.Enabled() && len(c.Builders) == 0 {
return fmt.Errorf("no configs or hints defined for autodiscover provider")
}
// Check if host is being defined and change it to node instead.
if c.Node == "" && c.HostDeprecated != "" {
c.Node = c.HostDeprecated
cfgwarn.Deprecate("8.0", "`host` will be deprecated, use `node` instead")
}
// Check if resource is either node or pod. If yes then default the scope to "node" if not provided.
// Default the scope to "cluster" for everything else.
switch c.Resource {
case "node", "pod":
if c.Scope == "" {
c.Scope = "node"
}
default:
if c.Scope == "node" {
logp.L().Warnf("can not set scope to `node` when using resource %s. resetting scope to `cluster`", c.Resource)
}
c.Scope = "cluster"
}
if c.Scope != "node" && c.Scope != "cluster" {
return fmt.Errorf("invalid `scope` configured. supported values are `node` and `cluster`")
}
if c.Unique && c.Scope != "cluster" {
logp.L().Warnf("can only set `unique` when scope is `cluster`")
}
return nil
}