Skip to content

Commit

Permalink
Merge pull request #90 from kube-logging/refactor/improve-code-structure
Browse files Browse the repository at this point in the history
refactor: improve code structure
  • Loading branch information
csatib02 authored Nov 5, 2024
2 parents 3dc5fee + 439147a commit d9cb27d
Show file tree
Hide file tree
Showing 30 changed files with 1,467 additions and 1,222 deletions.
2 changes: 1 addition & 1 deletion .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,9 @@ issues:
linters:
disable-all: true
enable:
- copyloopvar
- dupl
- errcheck
- copyloopvar
- goconst
- gocyclo
- gofmt
Expand Down
24 changes: 12 additions & 12 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,10 @@ golangci-lint:
curl -sSfL https://raw.githubusercontent.com/golangci/golangci-lint/master/install.sh | sh -s -- -b $(shell dirname $(GOLANGCI_LINT)) $(GOLANGCI_LINT_VERSION) ;\
}

.PHONY: test-e2e
test-e2e: docker-build kind-cluster ## Run e2e tests
e2e/e2e_test.sh || (echo "E2E test failed"; exit 1)

.PHONY: lint
lint: golangci-lint ## Run golangci-lint
$(GOLANGCI_LINT) run
Expand All @@ -103,23 +107,19 @@ run-delve: generate fmt vet manifests
go build -o bin/manager cmd/main.go
dlv --listen=:2345 --headless=true --api-version=2 --accept-multiclient exec ./bin/manager

.PHONY: test-e2e
test-e2e: docker-build kind-cluster ## Run e2e tests
e2e/e2e_test.sh || (echo "E2E test failed"; exit 1)

.PHONY: check-diff
check-diff: generate
git diff --exit-code

.PHONY: license-cache
license-cache: ${LICENSEI} ## Generate license cache
${LICENSEI} cache

.PHONY: license-check
license-check: ${LICENSEI} .licensei.cache ## Run license check
${LICENSEI} check
${LICENSEI} header

.PHONY: license-cache
license-cache: ${LICENSEI} ## Generate license cache
${LICENSEI} cache

##@ Build

.PHONY: build
Expand All @@ -137,10 +137,6 @@ run: manifests generate fmt vet ## Run the controller from your host.
docker-build: ## Build docker image with the manager.
$(CONTAINER_TOOL) build -t ${IMG} .

.PHONY: docker-push
docker-push: ## Push docker image with the manager.
$(CONTAINER_TOOL) push ${IMG}

# PLATFORMS defines the target platforms for the manager image be built to provide support to multiple
# architectures. (i.e. make docker-buildx IMG=myregistry/mypoperator:0.0.1). To use this option you need to:
# - be able to use docker buildx. More info: https://docs.docker.com/build/buildx/
Expand All @@ -158,6 +154,10 @@ docker-buildx: ## Build and push docker image for the manager for cross-platform
- $(CONTAINER_TOOL) buildx rm project-v3-builder
rm Dockerfile.cross

.PHONY: docker-push
docker-push: ## Push docker image with the manager.
$(CONTAINER_TOOL) push ${IMG}

##@ Deployment

ifndef ignore-not-found
Expand Down
2 changes: 1 addition & 1 deletion api/telemetry/v1alpha1/otlp_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ type BackOffConfig struct {
// Enabled indicates whether to not retry sending batches in case of export failure.
Enabled bool `json:"enabled,omitempty" yaml:"enabled,omitempty"`
// InitialInterval the time to wait after the first failure before retrying.
InitialInterval time.Duration `json:"initial_interval,omitempty" yaml:"initial_interval,omitempty" `
InitialInterval time.Duration `json:"initial_interval,omitempty" yaml:"initial_interval,omitempty"`
// RandomizationFactor is a random factor used to calculate next backoffs
// Randomized interval = RetryInterval * (1 ± RandomizationFactor)
RandomizationFactor string `json:"randomization_factor,omitempty" yaml:"randomization_factor,omitempty"`
Expand Down
2 changes: 1 addition & 1 deletion api/telemetry/v1alpha1/output_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ type Output struct {
metav1.ObjectMeta `json:"metadata,omitempty"`

Spec OutputSpec `json:"spec,omitempty" yaml:"spec,omitempty"`
Status OutputStatus `json:"status,omitempty" `
Status OutputStatus `json:"status,omitempty"`
}

// +kubebuilder:object:root=true
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ require (
go.opentelemetry.io/collector/config/configcompression v1.7.0
go.opentelemetry.io/collector/config/configopaque v1.16.0
golang.org/x/exp v0.0.0-20240506185415-9bf2ced13842
gopkg.in/yaml.v3 v3.0.1
k8s.io/api v0.30.2
k8s.io/apimachinery v0.30.2
k8s.io/client-go v0.30.2
Expand Down Expand Up @@ -100,7 +101,6 @@ require (
google.golang.org/protobuf v1.34.2 // indirect
gopkg.in/inf.v0 v0.9.1 // indirect
gopkg.in/yaml.v2 v2.4.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
k8s.io/apiextensions-apiserver v0.30.2 // indirect
k8s.io/klog/v2 v2.130.1 // indirect
k8s.io/kube-openapi v0.0.0-20240322212309-b815d8309940 // indirect
Expand Down
27 changes: 13 additions & 14 deletions internal/controller/telemetry/collector_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ import (
"sigs.k8s.io/controller-runtime/pkg/reconcile"

"github.com/kube-logging/telemetry-controller/api/telemetry/v1alpha1"
otelcolconfgen "github.com/kube-logging/telemetry-controller/internal/controller/telemetry/otel_conf_gen"
"github.com/kube-logging/telemetry-controller/internal/controller/telemetry/pipeline/components"
)

const (
Expand All @@ -62,25 +64,25 @@ type BasicAuthClientAuthConfig struct {

func (e *TenantFailedError) Error() string { return e.msg }

func (r *CollectorReconciler) buildConfigInputForCollector(ctx context.Context, collector *v1alpha1.Collector) (OtelColConfigInput, error) {
func (r *CollectorReconciler) buildConfigInputForCollector(ctx context.Context, collector *v1alpha1.Collector) (otelcolconfgen.OtelColConfigInput, error) {
logger := log.FromContext(ctx)
tenantSubscriptionMap := make(map[string][]v1alpha1.NamespacedName)
subscriptionOutputMap := make(map[v1alpha1.NamespacedName][]v1alpha1.NamespacedName)

tenants, err := r.getTenantsMatchingSelectors(ctx, collector.Spec.TenantSelector)
subscriptions := make(map[v1alpha1.NamespacedName]v1alpha1.Subscription)
outputs := []OutputWithSecretData{}
outputs := []components.OutputWithSecretData{}

if err != nil {
logger.Error(errors.WithStack(err), "failed listing tenants")
return OtelColConfigInput{}, err
return otelcolconfgen.OtelColConfigInput{}, err
}

for _, tenant := range tenants {

if tenant.Status.State == v1alpha1.StateFailed {
logger.Info("tenant %q is in failed state, retrying later", tenant.Name)
return OtelColConfigInput{}, &TenantFailedError{msg: "tenant failed"}
return otelcolconfgen.OtelColConfigInput{}, &TenantFailedError{msg: "tenant failed"}
}

subscriptionNames := tenant.Status.Subscriptions
Expand All @@ -90,7 +92,7 @@ func (r *CollectorReconciler) buildConfigInputForCollector(ctx context.Context,
queriedSubs := &v1alpha1.Subscription{}
if err = r.Client.Get(ctx, types.NamespacedName(subsName), queriedSubs); err != nil {
logger.Error(errors.WithStack(err), "failed getting subscriptions for tenant", "tenant", tenant.Name)
return OtelColConfigInput{}, err
return otelcolconfgen.OtelColConfigInput{}, err
}
subscriptions[subsName] = *queriedSubs
}
Expand All @@ -101,17 +103,17 @@ func (r *CollectorReconciler) buildConfigInputForCollector(ctx context.Context,
subscriptionOutputMap[subscription.NamespacedName()] = outputNames

for _, outputName := range outputNames {
outputWithSecretData := OutputWithSecretData{}
outputWithSecretData := components.OutputWithSecretData{}

queriedOutput := &v1alpha1.Output{}
if err = r.Client.Get(ctx, types.NamespacedName(outputName), queriedOutput); err != nil {
logger.Error(errors.WithStack(err), "failed getting outputs for subscription", "subscription", subscription.NamespacedName().String())
return OtelColConfigInput{}, err
return otelcolconfgen.OtelColConfigInput{}, err
}
outputWithSecretData.Output = *queriedOutput

if err := r.populateSecretForOutput(ctx, queriedOutput, &outputWithSecretData); err != nil {
return OtelColConfigInput{}, err
return otelcolconfgen.OtelColConfigInput{}, err
}

outputs = append(outputs, outputWithSecretData)
Expand All @@ -121,10 +123,10 @@ func (r *CollectorReconciler) buildConfigInputForCollector(ctx context.Context,
bridges, err := r.getBridges(ctx, client.ListOptions{})
if err != nil {
logger.Error(errors.WithStack(err), "failed listing bridges")
return OtelColConfigInput{}, err
return otelcolconfgen.OtelColConfigInput{}, err
}

otelConfigInput := OtelColConfigInput{
otelConfigInput := otelcolconfgen.OtelColConfigInput{
Tenants: tenants,
Subscriptions: subscriptions,
Bridges: bridges,
Expand All @@ -138,7 +140,7 @@ func (r *CollectorReconciler) buildConfigInputForCollector(ctx context.Context,
return otelConfigInput, nil
}

func (r *CollectorReconciler) populateSecretForOutput(ctx context.Context, queriedOutput *v1alpha1.Output, outputWithSecret *OutputWithSecretData) error {
func (r *CollectorReconciler) populateSecretForOutput(ctx context.Context, queriedOutput *v1alpha1.Output, outputWithSecret *components.OutputWithSecretData) error {
logger := log.FromContext(ctx)

if queriedOutput.Spec.Authentication != nil {
Expand Down Expand Up @@ -260,18 +262,15 @@ func (r *CollectorReconciler) Reconcile(ctx context.Context, req ctrl.Request) (
}

resourceReconciler := reconciler.NewReconcilerWith(r.Client, reconciler.WithLog(logger))

_, err = resourceReconciler.ReconcileResource(&otelCollector, reconciler.StatePresent)
if err != nil {
return ctrl.Result{}, err
}

tenantNames := []string{}

for _, tenant := range otelConfigInput.Tenants {
tenantNames = append(tenantNames, tenant.Name)
}

collector.Status.Tenants = normalizeStringSlice(tenantNames)

if !reflect.DeepEqual(originalCollectorStatus, collector.Status) {
Expand Down
Loading

0 comments on commit d9cb27d

Please sign in to comment.