Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Target Allocator implementation (Part 1 - OTEL Operator Enhancements) #351

Merged
merged 33 commits into from
Aug 13, 2021
Merged
Show file tree
Hide file tree
Changes from 28 commits
Commits
Show all changes
33 commits
Select commit Hold shift + click to select a range
130e69d
load balancer deployment implementation & controller & e2e tests
alexperez52 Jul 8, 2021
2835c16
Load balancer crd update, reconcile logic and e2e tests
Jul 8, 2021
6f81007
Reset last commit and only uploading changes to main.go
alexperez52 Jul 8, 2021
31f97fe
Updated bundle and api with new autogenerated resources
rsvarma95 Jul 8, 2021
47c5fe8
Updated code with lint fixes
Jul 9, 2021
8ef4cde
Updated code to include minor fixes
rsvarma95 Jul 12, 2021
c73470e
Update helper.go to add header
Jul 12, 2021
7482e13
Updated comment and bundles
rsvarma95 Jul 13, 2021
729d4ea
Updated lb reconcile helper function and its invocations
rsvarma95 Jul 13, 2021
2dcf8a1
Updated naming scheme and changed CR config
rsvarma95 Jul 16, 2021
a0d3f34
Updated bundle files
rsvarma95 Jul 16, 2021
492749e
Minor changes
rsvarma95 Jul 20, 2021
6a72424
Lint and default file fixes
rsvarma95 Jul 21, 2021
8657668
Added rolebinding to automate manual setting
rsvarma95 Jul 30, 2021
0528856
Update opentelemetry-operator.clusterserviceversion.yaml
Jul 30, 2021
1a921cb
Merge branch 'main' into load-balancer-implementation
Jul 30, 2021
bf1010b
Removed role/rolebinding files & Minor changes
Aug 3, 2021
38ca88a
Minor changes
Aug 3, 2021
be56aa7
Merge branch 'main' into load-balancer-implementation
Aug 3, 2021
e339eba
Added error check in configmap reconcile & spelling correction
Aug 3, 2021
6f39c47
Updated target allocator KUTTL tests & renamed folder
rsvarma95 Aug 3, 2021
367c623
Updated folder structure to reduce code duplication between collector…
rsvarma95 Aug 4, 2021
7dd75b3
Updated kuttl tests to use namespace-scope resources
rsvarma95 Aug 4, 2021
0ef5424
Merge branch 'main' into load-balancer-implementation
rsvarma95 Aug 5, 2021
db40373
Revert part of "Updated folder structure to reduce code duplication b…
Aug 5, 2021
67af909
Removed separate controller for target allocation
Aug 6, 2021
888ae31
Added additional label for collector pod selection and removed test-step
Aug 9, 2021
11d3c87
Minor changes
Aug 9, 2021
a7764b9
Added version management & Minor fixes
Aug 11, 2021
33cb042
Updated docker image to quay.io image
Aug 11, 2021
3d37148
Added validation in webhook to validate prometheus config
Aug 11, 2021
a496825
Update api/v1alpha1/opentelemetrycollector_webhook.go
Aug 11, 2021
98acb2f
Updated struct name & minor changes
Aug 12, 2021
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions api/v1alpha1/opentelemetrycollector_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,11 @@ type OpenTelemetryCollectorSpec struct {
// +operator-sdk:gen-csv:customresourcedefinitions.specDescriptors=true
Image string `json:"image,omitempty"`

// TargetAllocator indicates a value which determines whether to spawn a target allocation resource or not.
// +optional
// +operator-sdk:gen-csv:customresourcedefinitions.specDescriptors=true
TargetAllocator OpenTelemetryTargetAllocator `json:"targetAllocator,omitempty"`

// Mode represents how the collector should be deployed (deployment, daemonset, statefulset or sidecar)
// +optional
// +operator-sdk:gen-csv:customresourcedefinitions.specDescriptors=true
Expand Down Expand Up @@ -116,6 +121,17 @@ type OpenTelemetryCollectorStatus struct {
Messages []string `json:"messages,omitempty"`
}

// OpenTelemetryTargetAllocator defines the configurations for the Prometheus target allocator.
type OpenTelemetryTargetAllocator struct {
jpkrohling marked this conversation as resolved.
Show resolved Hide resolved
// Enabled indicates whether to use a target allocation mechanism for Prometheus targets or not.
// +optional
Enabled bool `json:"enabled,omitempty"`

// Image indicates the container image to use for the OpenTelemetry TargetAllocator.
// +optional
Image string `json:"image,omitempty"`
}

// +kubebuilder:object:root=true
// +kubebuilder:resource:shortName=otelcol;otelcols
// +kubebuilder:subresource:status
Expand Down
5 changes: 5 additions & 0 deletions api/v1alpha1/opentelemetrycollector_webhook.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,5 +91,10 @@ func (r *OpenTelemetryCollector) validateCRDSpec() error {
return fmt.Errorf("the OpenTelemetry Collector mode is set to %s, which does not support the attribute 'tolerations'", r.Spec.Mode)
}

// validate target allocation
if r.Spec.TargetAllocator.Enabled && r.Spec.Mode != ModeStatefulSet {
return fmt.Errorf("the OpenTelemetry Collector mode is set to %s, which does not support the target allocation deployment", r.Spec.Mode)
}

return nil
}
16 changes: 16 additions & 0 deletions api/v1alpha1/zz_generated.deepcopy.go

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

13 changes: 13 additions & 0 deletions bundle/manifests/opentelemetry.io_opentelemetrycollectors.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -442,6 +442,19 @@ spec:
description: ServiceAccount indicates the name of an existing service
account to use with this instance.
type: string
targetAllocator:
description: TargetAllocator indicates a value which determines whether
to spawn a target allocation resource or not.
properties:
enabled:
description: Enabled indicates whether to use a target allocation
mechanism for Prometheus targets or not.
type: boolean
image:
description: Image indicates the container image to use for the
OpenTelemetry TargetAllocator.
type: string
type: object
tolerations:
description: Toleration to schedule OpenTelemetry Collector pods.
This is only relevant to daemonsets, statefulsets and deployments
Expand Down
13 changes: 13 additions & 0 deletions config/crd/bases/opentelemetry.io_opentelemetrycollectors.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -430,6 +430,19 @@ spec:
description: ServiceAccount indicates the name of an existing service
account to use with this instance.
type: string
targetAllocator:
description: TargetAllocator indicates a value which determines whether
to spawn a target allocation resource or not.
properties:
enabled:
description: Enabled indicates whether to use a target allocation
mechanism for Prometheus targets or not.
type: boolean
image:
description: Image indicates the container image to use for the
OpenTelemetry TargetAllocator.
type: string
type: object
tolerations:
description: Toleration to schedule OpenTelemetry Collector pods.
This is only relevant to daemonsets, statefulsets and deployments
Expand Down
59 changes: 40 additions & 19 deletions internal/config/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,9 @@ import (
)

const (
defaultAutoDetectFrequency = 5 * time.Second
defaultCollectorConfigMapEntry = "collector.yaml"
defaultAutoDetectFrequency = 5 * time.Second
defaultCollectorConfigMapEntry = "collector.yaml"
defaultTargetAllocatorConfigMapEntry = "targetallocator.yaml"
)

// Config holds the static configuration for this operator.
Expand All @@ -44,21 +45,24 @@ type Config struct {
onChange []func() error

// config state
collectorImage string
collectorConfigMapEntry string
platform platform.Platform
version version.Version
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,
logger: logf.Log.WithName("config"),
platform: platform.Unknown,
version: version.Get(),
autoDetectFrequency: defaultAutoDetectFrequency,
collectorConfigMapEntry: defaultCollectorConfigMapEntry,
targetAllocatorConfigMapEntry: defaultTargetAllocatorConfigMapEntry,
logger: logf.Log.WithName("config"),
platform: platform.Unknown,
version: version.Get(),
}
for _, opt := range opts {
opt(&o)
Expand All @@ -70,15 +74,22 @@ func New(opts ...Option) Config {
o.collectorImage = fmt.Sprintf("otel/opentelemetry-collector:%s", o.version.OpenTelemetryCollector)
}

if len(o.targetAllocatorImage) == 0 {
// will be replcaed with target allocator image once approved and available
o.targetAllocatorImage = "raul9595/otel-loadbalancer:0.0.1"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This needs to be updated to an appropriate image.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

make sure to fetch to the default TA image version from versions.txt file similar to how we fetch the latest version of otel-collector image. This helps us to easily manage version upgrades of TA.

}

return Config{
autoDetect: o.autoDetect,
autoDetectFrequency: o.autoDetectFrequency,
collectorImage: o.collectorImage,
collectorConfigMapEntry: o.collectorConfigMapEntry,
logger: o.logger,
onChange: o.onChange,
platform: o.platform,
version: o.version,
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,
}
}

Expand Down Expand Up @@ -155,6 +166,16 @@ 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
Expand Down
29 changes: 21 additions & 8 deletions internal/config/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,14 +28,16 @@ import (
type Option func(c *options)

type options struct {
autoDetect autodetect.AutoDetect
autoDetectFrequency time.Duration
collectorImage string
collectorConfigMapEntry string
logger logr.Logger
onChange []func() error
platform platform.Platform
version version.Version
autoDetect autodetect.AutoDetect
autoDetectFrequency time.Duration
targetAllocatorImage string
collectorImage string
collectorConfigMapEntry string
targetAllocatorConfigMapEntry string
logger logr.Logger
onChange []func() error
platform platform.Platform
version version.Version
}

func WithAutoDetect(a autodetect.AutoDetect) Option {
Expand All @@ -48,6 +50,12 @@ func WithAutoDetectFrequency(t time.Duration) Option {
o.autoDetectFrequency = t
}
}
func WithTargetAllocatorImage(s string) Option {
return func(o *options) {
o.targetAllocatorImage = s
}
}

func WithCollectorImage(s string) Option {
return func(o *options) {
o.collectorImage = s
Expand All @@ -58,6 +66,11 @@ func WithCollectorConfigMapEntry(s string) Option {
o.collectorConfigMapEntry = s
}
}
func WithTargetAllocatorConfigMapEntry(s string) Option {
return func(o *options) {
o.targetAllocatorConfigMapEntry = s
}
}
func WithLogger(logger logr.Logger) Option {
return func(o *options) {
o.logger = logger
Expand Down
3 changes: 1 addition & 2 deletions pkg/collector/container_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,11 @@ package collector_test
import (
"testing"

"github.com/stretchr/testify/assert"
corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/api/resource"
logf "sigs.k8s.io/controller-runtime/pkg/log"

"github.com/stretchr/testify/assert"

"github.com/open-telemetry/opentelemetry-operator/api/v1alpha1"
"github.com/open-telemetry/opentelemetry-operator/internal/config"
. "github.com/open-telemetry/opentelemetry-operator/pkg/collector"
Expand Down
1 change: 0 additions & 1 deletion pkg/collector/deployment_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ import (
"testing"

"github.com/stretchr/testify/assert"

v1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"

Expand Down
45 changes: 45 additions & 0 deletions pkg/collector/reconcile/configmap.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import (
"fmt"
"reflect"

"gopkg.in/yaml.v2"
corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
Expand All @@ -28,6 +29,7 @@ import (

"github.com/open-telemetry/opentelemetry-operator/pkg/collector"
"github.com/open-telemetry/opentelemetry-operator/pkg/naming"
"github.com/open-telemetry/opentelemetry-operator/pkg/targetallocator"
)

// +kubebuilder:rbac:groups="",resources=configmaps,verbs=get;list;watch;create;update;patch;delete
Expand All @@ -38,6 +40,14 @@ func ConfigMaps(ctx context.Context, params Params) error {
desiredConfigMap(ctx, params),
}

if params.Instance.Spec.TargetAllocator.Enabled {
cm, err := desiredTAConfigMap(ctx, params)
if err != nil {
return fmt.Errorf("failed to parse config: %v", err)
jpkrohling marked this conversation as resolved.
Show resolved Hide resolved
}
desired = append(desired, cm)
}

// first, handle the create/update parts
if err := expectedConfigMaps(ctx, params, desired, true); err != nil {
return fmt.Errorf("failed to reconcile the expected configmaps: %v", err)
Expand Down Expand Up @@ -69,6 +79,41 @@ func desiredConfigMap(_ context.Context, params Params) corev1.ConfigMap {
}
}

func desiredTAConfigMap(_ context.Context, params Params) (corev1.ConfigMap, error) {
rsvarma95 marked this conversation as resolved.
Show resolved Hide resolved
name := naming.TAConfigMap(params.Instance)
labels := targetallocator.Labels(params.Instance)
labels["app.kubernetes.io/name"] = name

promConfig, err := GetPromConfig(params)
if err != nil {
return corev1.ConfigMap{}, err
}

taConfig := make(map[interface{}]interface{})
taConfig["label_selector"] = map[string]string{
"app.kubernetes.io/instance": fmt.Sprintf("%s.%s", params.Instance.Namespace, params.Instance.Name),
"app.kubernetes.io/managed-by": "opentelemetry-operator",
"app.kubernetes.io/component": "opentelemetry-collector",
}
taConfig["config"] = promConfig
taConfigYAML, err := yaml.Marshal(taConfig)
if err != nil {
return corev1.ConfigMap{}, err
}

return corev1.ConfigMap{
ObjectMeta: metav1.ObjectMeta{
Name: name,
Namespace: params.Instance.Namespace,
Labels: labels,
Annotations: params.Instance.Annotations,
},
Data: map[string]string{
"targetallocator.yaml": string(taConfigYAML),
},
}, nil
}

func expectedConfigMaps(ctx context.Context, params Params, expected []corev1.ConfigMap, retry bool) error {
for _, obj := range expected {
desired := obj
Expand Down
Loading