forked from open-telemetry/opentelemetry-operator
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconfig.go
165 lines (150 loc) · 4.83 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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
// Copyright The OpenTelemetry Authors
//
// Licensed 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.
package v1beta1
import (
"bytes"
"encoding/json"
"fmt"
"reflect"
"sort"
"gopkg.in/yaml.v3"
)
// AnyConfig represent parts of the config.
type AnyConfig struct {
Object map[string]interface{} `json:"-" yaml:",inline"`
}
// DeepCopyInto is an autogenerated deepcopy function, copying the receiver, writing into out. in must be non-nil.
func (in *AnyConfig) DeepCopyInto(out *AnyConfig) {
*out = *in
if in.Object != nil {
in, out := &in.Object, &out.Object
*out = make(map[string]interface{}, len(*in))
for key, val := range *in {
(*out)[key] = val
}
}
}
// DeepCopy is an autogenerated deepcopy function, copying the receiver, creating a new AnyConfig.
func (in *AnyConfig) DeepCopy() *AnyConfig {
if in == nil {
return nil
}
out := new(AnyConfig)
in.DeepCopyInto(out)
return out
}
var _ json.Marshaler = &AnyConfig{}
var _ json.Unmarshaler = &AnyConfig{}
// UnmarshalJSON implements an alternative parser for this field.
func (c *AnyConfig) UnmarshalJSON(b []byte) error {
vals := map[string]interface{}{}
if err := json.Unmarshal(b, &vals); err != nil {
return err
}
c.Object = vals
return nil
}
// MarshalJSON specifies how to convert this object into JSON.
func (c *AnyConfig) MarshalJSON() ([]byte, error) {
if c == nil {
return []byte("{}"), nil
}
return json.Marshal(c.Object)
}
// Config encapsulates collector config.
type Config struct {
// +kubebuilder:pruning:PreserveUnknownFields
Receivers AnyConfig `json:"receivers" yaml:"receivers"`
// +kubebuilder:pruning:PreserveUnknownFields
Exporters AnyConfig `json:"exporters" yaml:"exporters"`
// +kubebuilder:pruning:PreserveUnknownFields
Processors *AnyConfig `json:"processors,omitempty" yaml:"processors,omitempty"`
// +kubebuilder:pruning:PreserveUnknownFields
Connectors *AnyConfig `json:"connectors,omitempty" yaml:"connectors,omitempty"`
// +kubebuilder:pruning:PreserveUnknownFields
Extensions *AnyConfig `json:"extensions,omitempty" yaml:"extensions,omitempty"`
Service Service `json:"service" yaml:"service"`
}
// Yaml encodes the current object and returns it as a string.
func (c Config) Yaml() (string, error) {
var buf bytes.Buffer
yamlEncoder := yaml.NewEncoder(&buf)
yamlEncoder.SetIndent(2)
if err := yamlEncoder.Encode(&c); err != nil {
return "", err
}
return buf.String(), nil
}
type Service struct {
Extensions *[]string `json:"extensions,omitempty" yaml:"extensions,omitempty"`
// +kubebuilder:pruning:PreserveUnknownFields
Telemetry *AnyConfig `json:"telemetry,omitempty" yaml:"telemetry,omitempty"`
// +kubebuilder:pruning:PreserveUnknownFields
Pipelines AnyConfig `json:"pipelines" yaml:"pipelines"`
}
// Returns null objects in the config.
func (c Config) nullObjects() []string {
var nullKeys []string
if nulls := hasNullValue(c.Receivers.Object); len(nulls) > 0 {
nullKeys = append(nullKeys, addPrefix("receivers.", nulls)...)
}
if nulls := hasNullValue(c.Exporters.Object); len(nulls) > 0 {
nullKeys = append(nullKeys, addPrefix("exporters.", nulls)...)
}
if c.Processors != nil {
if nulls := hasNullValue(c.Processors.Object); len(nulls) > 0 {
nullKeys = append(nullKeys, addPrefix("processors.", nulls)...)
}
}
if c.Extensions != nil {
if nulls := hasNullValue(c.Extensions.Object); len(nulls) > 0 {
nullKeys = append(nullKeys, addPrefix("extensions.", nulls)...)
}
}
if c.Connectors != nil {
if nulls := hasNullValue(c.Connectors.Object); len(nulls) > 0 {
nullKeys = append(nullKeys, addPrefix("connectors.", nulls)...)
}
}
// Make the return deterministic. The config uses maps therefore processing order is non-deterministic.
sort.Strings(nullKeys)
return nullKeys
}
func hasNullValue(cfg map[string]interface{}) []string {
var nullKeys []string
for k, v := range cfg {
if v == nil {
nullKeys = append(nullKeys, fmt.Sprintf("%s:", k))
}
if reflect.ValueOf(v).Kind() == reflect.Map {
var nulls []string
val, ok := v.(map[string]interface{})
if ok {
nulls = hasNullValue(val)
}
if len(nulls) > 0 {
prefixed := addPrefix(k+".", nulls)
nullKeys = append(nullKeys, prefixed...)
}
}
}
return nullKeys
}
func addPrefix(prefix string, arr []string) []string {
var prefixed []string
for _, v := range arr {
prefixed = append(prefixed, fmt.Sprintf("%s%s", prefix, v))
}
return prefixed
}