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 32 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
2 changes: 2 additions & 0 deletions .github/workflows/publish-images.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ jobs:
- name: Set env vars for the job
run: |
grep -v '\#' versions.txt | grep opentelemetry-collector | awk -F= '{print "OTELCOL_VERSION="$2}' >> $GITHUB_ENV
grep -v '\#' versions.txt | grep targetallocator | awk -F= '{print "TARGETALLOCATOR_VERSION="$2}' >> $GITHUB_ENV
echo "VERSION_DATE=$(date -u +'%Y-%m-%dT%H:%M:%SZ')" >> $GITHUB_ENV
echo "VERSION=$(git describe --tags | sed 's/^v//')" >> $GITHUB_ENV
Expand Down Expand Up @@ -72,5 +73,6 @@ jobs:
VERSION=${{ env.VERSION }}
VERSION_DATE=${{ env.VERSION_DATE }}
OTELCOL_VERSION=${{ env.OTELCOL_VERSION }}
TARGETALLOCATOR_VERSION=${{ env.TARGETALLOCATOR_VERSION }}
cache-from: type=local,src=/tmp/.buildx-cache
cache-to: type=local,dest=/tmp/.buildx-cache
3 changes: 2 additions & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,10 @@ ARG VERSION_PKG
ARG VERSION
ARG VERSION_DATE
ARG OTELCOL_VERSION
ARG TARGETALLOCATOR_VERSION

# Build
RUN CGO_ENABLED=0 GOOS=linux GOARCH=amd64 GO111MODULE=on go build -ldflags="-X ${VERSION_PKG}.version=${VERSION} -X ${VERSION_PKG}.buildDate=${VERSION_DATE} -X ${VERSION_PKG}.otelCol=${OTELCOL_VERSION}" -a -o manager main.go
RUN CGO_ENABLED=0 GOOS=linux GOARCH=amd64 GO111MODULE=on go build -ldflags="-X ${VERSION_PKG}.version=${VERSION} -X ${VERSION_PKG}.buildDate=${VERSION_DATE} -X ${VERSION_PKG}.otelCol=${OTELCOL_VERSION} -X ${VERSION_PKG}.targetAllocator=${TARGETALLOCATOR_VERSION}" -a -o manager main.go

# Use distroless as minimal base image to package the manager binary
# Refer to https://github.com/GoogleContainerTools/distroless for more details
Expand Down
5 changes: 3 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ VERSION_DATE ?= $(shell date -u +'%Y-%m-%dT%H:%M:%SZ')
VERSION_PKG ?= "github.com/open-telemetry/opentelemetry-operator/internal/version"
OTELCOL_VERSION ?= "$(shell grep -v '\#' versions.txt | grep opentelemetry-collector | awk -F= '{print $$2}')"
OPERATOR_VERSION ?= "$(shell grep -v '\#' versions.txt | grep operator | awk -F= '{print $$2}')"
LD_FLAGS ?= "-X ${VERSION_PKG}.version=${VERSION} -X ${VERSION_PKG}.buildDate=${VERSION_DATE} -X ${VERSION_PKG}.otelCol=${OTELCOL_VERSION}"
TARGETALLOCATOR_VERSION ?= "$(shell grep -v '\#' versions.txt | grep targetallocator | awk -F= '{print $$2}')"
LD_FLAGS ?= "-X ${VERSION_PKG}.version=${VERSION} -X ${VERSION_PKG}.buildDate=${VERSION_DATE} -X ${VERSION_PKG}.otelCol=${OTELCOL_VERSION} -X ${VERSION_PKG}.targetAllocator=${TARGETALLOCATOR_VERSION}"

# Image URL to use all building/pushing image targets
IMG_PREFIX ?= quay.io/${USER}
Expand Down Expand Up @@ -122,7 +123,7 @@ set-test-image-vars:

# Build the container image, used only for local dev purposes
container:
docker build -t ${IMG} --build-arg VERSION_PKG=${VERSION_PKG} --build-arg VERSION=${VERSION} --build-arg VERSION_DATE=${VERSION_DATE} --build-arg OTELCOL_VERSION=${OTELCOL_VERSION} .
docker build -t ${IMG} --build-arg VERSION_PKG=${VERSION_PKG} --build-arg VERSION=${VERSION} --build-arg VERSION_DATE=${VERSION_DATE} --build-arg OTELCOL_VERSION=${OTELCOL_VERSION} --build-arg TARGETALLOCATOR_VERSION=${TARGETALLOCATOR_VERSION} .

# Push the container image, used only for local dev purposes
container-push:
Expand Down
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
15 changes: 15 additions & 0 deletions api/v1alpha1/opentelemetrycollector_webhook.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ import (
ctrl "sigs.k8s.io/controller-runtime"
logf "sigs.k8s.io/controller-runtime/pkg/log"
"sigs.k8s.io/controller-runtime/pkg/webhook"

ta "github.com/open-telemetry/opentelemetry-operator/pkg/targetallocator/adapters"
)

// log is for logging in this package.
Expand Down Expand Up @@ -91,5 +93,18 @@ 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)
}

// validate Prometheus config for target allocation
if r.Spec.TargetAllocator.Enabled {
_, err := ta.ConfigToPromConfig(r.Spec.Config)
if err != nil {
return fmt.Errorf("the OpenTelemetry Spec Prometheus configuration is incorrect, %s", err)
}
}

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
58 changes: 39 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,21 @@ func New(opts ...Option) Config {
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,
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 +165,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
23 changes: 19 additions & 4 deletions internal/version/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,10 @@ import (
)

var (
version string
buildDate string
otelCol string
version string
buildDate string
otelCol string
targetAllocator string
)

// Version holds this Operator's version as well as the version of some of the components it uses.
Expand All @@ -32,6 +33,7 @@ type Version struct {
BuildDate string `json:"build-date"`
OpenTelemetryCollector string `json:"opentelemetry-collector-version"`
Go string `json:"go-version"`
TargetAllocator string `json:"target-allocator-version"`
}

// Get returns the Version object with the relevant information.
Expand All @@ -41,16 +43,18 @@ func Get() Version {
BuildDate: buildDate,
OpenTelemetryCollector: OpenTelemetryCollector(),
Go: runtime.Version(),
TargetAllocator: TargetAllocator(),
}
}

func (v Version) String() string {
return fmt.Sprintf(
"Version(Operator='%v', BuildDate='%v', OpenTelemetryCollector='%v', Go='%v')",
"Version(Operator='%v', BuildDate='%v', OpenTelemetryCollector='%v', Go='%v', TargetAllocator='%v')",
v.Operator,
v.BuildDate,
v.OpenTelemetryCollector,
v.Go,
v.TargetAllocator,
)
}

Expand All @@ -64,3 +68,14 @@ func OpenTelemetryCollector() string {
// fallback value, useful for tests
return "0.0.0"
}

// TargetAllocator returns the default TargetAllocator to use when no versions are specified via CLI or configuration.
func TargetAllocator() string {
if len(targetAllocator) > 0 {
// this should always be set, as it's specified during the build
return targetAllocator
}

// fallback value, useful for tests
return "0.0.0"
}
15 changes: 15 additions & 0 deletions internal/version/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,3 +34,18 @@ func TestVersionFromBuild(t *testing.T) {
assert.Equal(t, otelCol, OpenTelemetryCollector())
assert.Contains(t, Get().String(), otelCol)
}

func TestTargetAllocatorFallbackVersion(t *testing.T) {
assert.Equal(t, "0.0.0", TargetAllocator())
}

func TestTargetAllocatorVersionFromBuild(t *testing.T) {
// prepare
targetAllocator = "0.0.2" // set during the build
defer func() {
targetAllocator = ""
}()

assert.Equal(t, targetAllocator, TargetAllocator())
assert.Contains(t, Get().String(), targetAllocator)
}
1 change: 1 addition & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ func main() {
logger.Info("Starting the OpenTelemetry Operator",
"opentelemetry-operator", v.Operator,
"opentelemetry-collector", v.OpenTelemetryCollector,
"opentelemetry-targetallocator", v.TargetAllocator,
"build-date", v.BuildDate,
"go-version", v.Go,
"go-arch", runtime.GOARCH,
Expand Down
Loading