-
Notifications
You must be signed in to change notification settings - Fork 459
/
main.go
186 lines (157 loc) · 5.96 KB
/
main.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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
// 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 config contains the operator's runtime configuration.
package config
import (
"fmt"
"time"
"github.com/go-logr/logr"
"github.com/spf13/pflag"
logf "sigs.k8s.io/controller-runtime/pkg/log"
"github.com/open-telemetry/opentelemetry-operator/internal/version"
"github.com/open-telemetry/opentelemetry-operator/pkg/autodetect"
"github.com/open-telemetry/opentelemetry-operator/pkg/platform"
)
const (
defaultAutoDetectFrequency = 5 * time.Second
defaultCollectorConfigMapEntry = "collector.yaml"
defaultTargetAllocatorConfigMapEntry = "targetallocator.yaml"
)
// Config holds the static configuration for this operator.
type Config struct {
// Registers a callback, to be called once a configuration change happens
OnChange func() error
logger logr.Logger
autoDetect autodetect.AutoDetect
autoDetectFrequency time.Duration
onChange []func() error
// config state
collectorImage string
collectorConfigMapEntry string
targetAllocatorImage string
targetAllocatorConfigMapEntry string
platform platform.Platform
version version.Version
}
// New constructs a new configuration based on the given options.
func New(opts ...Option) Config {
// initialize with the default values
o := options{
autoDetectFrequency: defaultAutoDetectFrequency,
collectorConfigMapEntry: defaultCollectorConfigMapEntry,
targetAllocatorConfigMapEntry: defaultTargetAllocatorConfigMapEntry,
logger: logf.Log.WithName("config"),
platform: platform.Unknown,
version: version.Get(),
}
for _, opt := range opts {
opt(&o)
}
// this is derived from another option, so, we need to first parse the options, then set a default
// if there's no explicit value being set
if len(o.collectorImage) == 0 {
o.collectorImage = fmt.Sprintf("otel/opentelemetry-collector:%s", o.version.OpenTelemetryCollector)
}
if len(o.targetAllocatorImage) == 0 {
o.targetAllocatorImage = fmt.Sprintf("quay.io/opentelemetry/target-allocator:%s", o.version.TargetAllocator)
}
return Config{
autoDetect: o.autoDetect,
autoDetectFrequency: o.autoDetectFrequency,
collectorImage: o.collectorImage,
collectorConfigMapEntry: o.collectorConfigMapEntry,
targetAllocatorImage: o.targetAllocatorImage,
targetAllocatorConfigMapEntry: o.targetAllocatorConfigMapEntry,
logger: o.logger,
onChange: o.onChange,
platform: o.platform,
version: o.version,
}
}
// FlagSet binds the flags to the user-modifiable values of the operator's configuration.
func (c *Config) FlagSet() *pflag.FlagSet {
fs := pflag.NewFlagSet("opentelemetry-operator", pflag.ExitOnError)
pflag.StringVar(&c.collectorImage,
"otelcol-image",
c.collectorImage,
"The default image to use for OpenTelemetry Collector when not specified in the individual custom resource (CR)",
)
return fs
}
// StartAutoDetect attempts to automatically detect relevant information for this operator. This will block until the first
// run is executed and will schedule periodic updates.
func (c *Config) StartAutoDetect() error {
err := c.AutoDetect()
go c.periodicAutoDetect()
return err
}
func (c *Config) periodicAutoDetect() {
ticker := time.NewTicker(c.autoDetectFrequency)
for range ticker.C {
if err := c.AutoDetect(); err != nil {
c.logger.Info("auto-detection failed", "error", err)
}
}
}
// AutoDetect attempts to automatically detect relevant information for this operator.
func (c *Config) AutoDetect() error {
changed := false
c.logger.V(2).Info("auto-detecting the configuration based on the environment")
// TODO: once new things need to be detected, extract this into individual detection routines
if c.platform == platform.Unknown {
plt, err := c.autoDetect.Platform()
if err != nil {
return err
}
if c.platform != plt {
c.logger.V(1).Info("platform detected", "platform", plt)
c.platform = plt
changed = true
}
}
if changed {
for _, callback := range c.onChange {
if err := callback(); err != nil {
// we don't fail if the callback failed, as the auto-detection itself
// did work
c.logger.Error(err, "configuration change notification failed for callback")
}
}
}
return nil
}
// CollectorImage represents the flag to override the OpenTelemetry Collector container image.
func (c *Config) CollectorImage() string {
return c.collectorImage
}
// CollectorConfigMapEntry represents the configuration file name for the collector. Immutable.
func (c *Config) CollectorConfigMapEntry() string {
return c.collectorConfigMapEntry
}
// TargetAllocatorImage represents the flag to override the OpenTelemetry TargetAllocator container image.
func (c *Config) TargetAllocatorImage() string {
return c.targetAllocatorImage
}
// TargetAllocatorConfigMapEntry represents the configuration file name for the TargetAllocator. Immutable.
func (c *Config) TargetAllocatorConfigMapEntry() string {
return c.targetAllocatorConfigMapEntry
}
// Platform represents the type of the platform this operator is running.
func (c *Config) Platform() platform.Platform {
return c.platform
}
// Version holds the versions used by this operator.
func (c *Config) Version() version.Version {
return c.version
}