Skip to content

Commit

Permalink
chore: Move remaining api-specific logic to the utils package (#1597)
Browse files Browse the repository at this point in the history
  • Loading branch information
TeodorSAP authored Nov 8, 2024
1 parent df9667b commit cfb7d98
Show file tree
Hide file tree
Showing 31 changed files with 346 additions and 486 deletions.
6 changes: 4 additions & 2 deletions .golangci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -120,8 +120,10 @@ linters-settings:
alias: commonStatusStubs
- pkg: k8s.io/client-go/testing
alias: clienttesting
- pkg: github.com/kyma-project/telemetry-manager/internal/utils/pipelines
alias: pipelineutils
- pkg: github.com/kyma-project/telemetry-manager/internal/utils/logpipeline
alias: logpipelineutils
- pkg: github.com/kyma-project/telemetry-manager/internal/utils/sharedtypes
alias: sharedtypesutils
errcheck:
check-type-assertions: true # Reports type assertions: `a := b.(SomeStruct)`.
check-blank: true # Report assignment of errors to blank identifier: `num, _ := strconv.Atoi(numStr)`.
Expand Down
2 changes: 1 addition & 1 deletion .testcoverage.yml
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ override:
- threshold: 73
path: ^webhook/logpipeline$
- threshold: 83
path: ^internal/utils/pipelines$
path: ^internal/utils/logpipeline$

# Holds regexp rules which will exclude matched files or packages from coverage statistics
exclude:
Expand Down
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ manifests-dev: $(CONTROLLER_GEN) ## Generate WebhookConfiguration, ClusterRole a
generate: $(CONTROLLER_GEN) $(MOCKERY) $(STRINGER) ## Generate code containing DeepCopy, DeepCopyInto, and DeepCopyObject method implementations.
$(CONTROLLER_GEN) object:headerFile="hack/boilerplate.go.txt" paths="./..."
$(MOCKERY)
$(STRINGER) --type LogPipelineMode internal/utils/pipelines/logpipeline_mode.go
$(STRINGER) --type Mode internal/utils/logpipeline/logpipeline.go
$(STRINGER) --type FeatureFlag internal/featureflags/featureflags.go

.PHONY: fmt
Expand Down
54 changes: 0 additions & 54 deletions apis/telemetry/v1alpha1/logpipeline_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,6 @@ package v1alpha1

import (
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"

"github.com/kyma-project/telemetry-manager/internal/featureflags"
)

//nolint:gochecknoinits // SchemeBuilder's registration is required.
Expand Down Expand Up @@ -189,55 +187,3 @@ type LogPipelineStatus struct {
// Is active when the LogPipeline uses a `custom` output or filter; see [unsupported mode](https://github.com/kyma-project/telemetry-manager/blob/main/docs/user/02-logs.md#unsupported-mode).
UnsupportedMode *bool `json:"unsupportedMode,omitempty"`
}

func (i *LogPipelineInput) IsValid() bool {
return i != nil
}

func (o *LogPipelineOutput) IsCustomDefined() bool {
return o.Custom != ""
}

func (o *LogPipelineOutput) IsHTTPDefined() bool {
return o.HTTP != nil && o.HTTP.Host.IsValid()
}

func (o *LogPipelineOutput) IsOTLPDefined() bool {
return o.OTLP != nil
}

func (o *LogPipelineOutput) IsAnyDefined() bool {
return o.pluginCount() > 0
}

func (o *LogPipelineOutput) IsSingleDefined() bool {
return o.pluginCount() == 1
}

func (o *LogPipelineOutput) pluginCount() int {
plugins := 0
if o.IsCustomDefined() {
plugins++
}

if o.IsHTTPDefined() {
plugins++
}

if featureflags.IsEnabled(featureflags.LogPipelineOTLP) && o.IsOTLPDefined() {
plugins++
}

return plugins
}

// ContainsCustomPlugin returns true if the pipeline contains any custom filters or outputs
func (lp *LogPipeline) ContainsCustomPlugin() bool {
for _, filter := range lp.Spec.Filters {
if filter.Custom != "" {
return true
}
}

return lp.Spec.Output.IsCustomDefined()
}
84 changes: 0 additions & 84 deletions apis/telemetry/v1alpha1/logpipeline_types_test.go

This file was deleted.

24 changes: 0 additions & 24 deletions apis/telemetry/v1alpha1/shared_types.go
Original file line number Diff line number Diff line change
@@ -1,32 +1,12 @@
package v1alpha1

import (
"k8s.io/apimachinery/pkg/types"
)

type ValueType struct {
// The value as plain text.
Value string `json:"value,omitempty"`
// The value as a reference to a resource.
ValueFrom *ValueFromSource `json:"valueFrom,omitempty"`
}

func (v *ValueType) IsValid() bool {
if v == nil {
return false
}

if v.Value != "" {
return true
}

return v.ValueFrom != nil &&
v.ValueFrom.SecretKeyRef != nil &&
v.ValueFrom.SecretKeyRef.Name != "" &&
v.ValueFrom.SecretKeyRef.Key != "" &&
v.ValueFrom.SecretKeyRef.Namespace != ""
}

type ValueFromSource struct {
// Refers to the value of a specific key in a Secret. You must provide `name` and `namespace` of the Secret, as well as the name of the `key`.
SecretKeyRef *SecretKeyRef `json:"secretKeyRef,omitempty"`
Expand All @@ -44,10 +24,6 @@ type SecretKeyRef struct {
Key string `json:"key,omitempty"`
}

func (skr *SecretKeyRef) NamespacedName() types.NamespacedName {
return types.NamespacedName{Name: skr.Name, Namespace: skr.Namespace}
}

const (
OTLPProtocolHTTP string = "http"
OTLPProtocolGRPC string = "grpc"
Expand Down
44 changes: 0 additions & 44 deletions apis/telemetry/v1beta1/logpipeline_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -180,47 +180,3 @@ type LogPipelineStatus struct {
// Is active when the LogPipeline uses a `custom` output or filter; see [unsupported mode](https://github.com/kyma-project/telemetry-manager/blob/main/docs/user/02-logs.md#unsupported-mode).
UnsupportedMode *bool `json:"unsupportedMode,omitempty"`
}

func (i *LogPipelineInput) IsValid() bool {
return i != nil
}

func (o *LogPipelineOutput) IsCustomDefined() bool {
return o.Custom != ""
}

func (o *LogPipelineOutput) IsHTTPDefined() bool {
return o.HTTP != nil && o.HTTP.Host.IsValid()
}

func (o *LogPipelineOutput) IsAnyDefined() bool {
return o.pluginCount() > 0
}

func (o *LogPipelineOutput) IsSingleDefined() bool {
return o.pluginCount() == 1
}

func (o *LogPipelineOutput) pluginCount() int {
plugins := 0
if o.IsCustomDefined() {
plugins++
}

if o.IsHTTPDefined() {
plugins++
}

return plugins
}

// ContainsCustomPlugin returns true if the pipeline contains any custom filters or outputs
func (lp *LogPipeline) ContainsCustomPlugin() bool {
for _, filter := range lp.Spec.Filters {
if filter.Custom != "" {
return true
}
}

return lp.Spec.Output.IsCustomDefined()
}
84 changes: 0 additions & 84 deletions apis/telemetry/v1beta1/logpipeline_types_test.go

This file was deleted.

Loading

0 comments on commit cfb7d98

Please sign in to comment.