Skip to content

Commit

Permalink
Added linters (open-telemetry#1287)
Browse files Browse the repository at this point in the history
* Added linters

* Small changes into golanci

Signed-off-by: Yuri Sa <yurimsa@gmail.com>

* Small changes into golanci

Signed-off-by: Yuri Sa <yurimsa@gmail.com>

Signed-off-by: Yuri Sa <yurimsa@gmail.com>
  • Loading branch information
yuriolisa authored Nov 30, 2022
1 parent 5d17567 commit 17c39e9
Show file tree
Hide file tree
Showing 21 changed files with 99 additions and 93 deletions.
6 changes: 6 additions & 0 deletions .golangci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -24,3 +24,9 @@ linters:
- exhaustive
- godot
- unparam
- gosimple
- unused
- staticcheck
- ineffassign
- typecheck
- unparam
40 changes: 20 additions & 20 deletions apis/v1alpha1/instrumentation_webhook.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,59 +86,59 @@ func (r *Instrumentation) Default() {
var _ webhook.Validator = &Instrumentation{}

// ValidateCreate implements webhook.Validator so a webhook will be registered for the type.
func (in *Instrumentation) ValidateCreate() error {
instrumentationlog.Info("validate create", "name", in.Name)
return in.validate()
func (r *Instrumentation) ValidateCreate() error {
instrumentationlog.Info("validate create", "name", r.Name)
return r.validate()
}

// ValidateUpdate implements webhook.Validator so a webhook will be registered for the type.
func (in *Instrumentation) ValidateUpdate(old runtime.Object) error {
instrumentationlog.Info("validate update", "name", in.Name)
return in.validate()
func (r *Instrumentation) ValidateUpdate(old runtime.Object) error {
instrumentationlog.Info("validate update", "name", r.Name)
return r.validate()
}

// ValidateDelete implements webhook.Validator so a webhook will be registered for the type.
func (in *Instrumentation) ValidateDelete() error {
instrumentationlog.Info("validate delete", "name", in.Name)
func (r *Instrumentation) ValidateDelete() error {
instrumentationlog.Info("validate delete", "name", r.Name)
return nil
}

func (in *Instrumentation) validate() error {
switch in.Spec.Sampler.Type {
func (r *Instrumentation) validate() error {
switch r.Spec.Sampler.Type {
case TraceIDRatio, ParentBasedTraceIDRatio:
if in.Spec.Sampler.Argument != "" {
rate, err := strconv.ParseFloat(in.Spec.Sampler.Argument, 64)
if r.Spec.Sampler.Argument != "" {
rate, err := strconv.ParseFloat(r.Spec.Sampler.Argument, 64)
if err != nil {
return fmt.Errorf("spec.sampler.argument is not a number: %s", in.Spec.Sampler.Argument)
return fmt.Errorf("spec.sampler.argument is not a number: %s", r.Spec.Sampler.Argument)
}
if rate < 0 || rate > 1 {
return fmt.Errorf("spec.sampler.argument should be in rage [0..1]: %s", in.Spec.Sampler.Argument)
return fmt.Errorf("spec.sampler.argument should be in rage [0..1]: %s", r.Spec.Sampler.Argument)
}
}
case AlwaysOn, AlwaysOff, JaegerRemote, ParentBasedAlwaysOn, ParentBasedAlwaysOff, XRaySampler:
}

// validate env vars
if err := in.validateEnv(in.Spec.Env); err != nil {
if err := r.validateEnv(r.Spec.Env); err != nil {
return err
}
if err := in.validateEnv(in.Spec.Java.Env); err != nil {
if err := r.validateEnv(r.Spec.Java.Env); err != nil {
return err
}
if err := in.validateEnv(in.Spec.NodeJS.Env); err != nil {
if err := r.validateEnv(r.Spec.NodeJS.Env); err != nil {
return err
}
if err := in.validateEnv(in.Spec.Python.Env); err != nil {
if err := r.validateEnv(r.Spec.Python.Env); err != nil {
return err
}
if err := in.validateEnv(in.Spec.DotNet.Env); err != nil {
if err := r.validateEnv(r.Spec.DotNet.Env); err != nil {
return err
}

return nil
}

func (in *Instrumentation) validateEnv(envs []corev1.EnvVar) error {
func (r *Instrumentation) validateEnv(envs []corev1.EnvVar) error {
for _, env := range envs {
if !strings.HasPrefix(env.Name, envPrefix) && !strings.HasPrefix(env.Name, envSplunkPrefix) {
return fmt.Errorf("env name should start with \"OTEL_\" or \"SPLUNK_\": %s", env.Name)
Expand Down
4 changes: 2 additions & 2 deletions cmd/otel-allocator/discovery/discovery.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ type discoveryHook interface {
SetConfig(map[string][]*relabel.Config)
}

func NewManager(log logr.Logger, ctx context.Context, logger log.Logger, hook discoveryHook, options ...func(*discovery.Manager)) *Manager {
func NewManager(ctx context.Context, log logr.Logger, logger log.Logger, hook discoveryHook, options ...func(*discovery.Manager)) *Manager {
manager := discovery.NewManager(ctx, logger, options...)

go func() {
Expand Down Expand Up @@ -110,7 +110,7 @@ func (m *Manager) Watch(fn func(targets map[string]*target.Item)) {
targets := map[string]*target.Item{}

for jobName, tgs := range tsets {
var count float64 = 0
var count float64
for _, tg := range tgs {
for _, t := range tg.Targets {
count++
Expand Down
2 changes: 1 addition & 1 deletion cmd/otel-allocator/discovery/discovery_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ func TestDiscovery(t *testing.T) {
want: []string{"prom.domain:9004", "prom.domain:9005", "promfile.domain:1001", "promfile.domain:3000"},
},
}
manager := NewManager(ctrl.Log.WithName("test"), context.Background(), gokitlog.NewNopLogger(), nil)
manager := NewManager(context.Background(), ctrl.Log.WithName("test"), gokitlog.NewNopLogger(), nil)
defer close(manager.close)

results := make(chan []string)
Expand Down
6 changes: 3 additions & 3 deletions cmd/otel-allocator/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,12 +96,12 @@ func main() {
}()

// creates a new discovery manager
discoveryManager := lbdiscovery.NewManager(log, ctx, gokitlog.NewNopLogger(), allocatorPrehook)
discoveryManager := lbdiscovery.NewManager(ctx, log, gokitlog.NewNopLogger(), allocatorPrehook)
defer discoveryManager.Close()

discoveryManager.Watch(allocator.SetTargets)

k8sclient, err := configureFileDiscovery(log, allocator, discoveryManager, context.Background(), cliConf)
k8sclient, err := configureFileDiscovery(context.Background(), log, allocator, discoveryManager, cliConf)
if err != nil {
setupLog.Error(err, "Can't start the k8s client")
os.Exit(1)
Expand Down Expand Up @@ -184,7 +184,7 @@ func newServer(log logr.Logger, allocator allocation.Allocator, discoveryManager
return s
}

func configureFileDiscovery(log logr.Logger, allocator allocation.Allocator, discoveryManager *lbdiscovery.Manager, ctx context.Context, cliConfig config.CLIConfig) (*collector.Client, error) {
func configureFileDiscovery(ctx context.Context, log logr.Logger, allocator allocation.Allocator, discoveryManager *lbdiscovery.Manager, cliConfig config.CLIConfig) (*collector.Client, error) {
cfg, err := config.Load(*cliConfig.ConfigFilePath)
if err != nil {
return nil, err
Expand Down
4 changes: 2 additions & 2 deletions cmd/otel-allocator/prehook/relabel.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,11 +60,11 @@ func (tf *RelabelConfigTargetFilter) Apply(targets map[string]*target.Item) map[
keepTarget := true
lset := convertLabelToPromLabelSet(tItem.Label)
for _, cfg := range tf.relabelCfg[tItem.JobName] {
if new_lset := relabel.Process(lset, cfg); new_lset == nil {
if newLset := relabel.Process(lset, cfg); newLset == nil {
keepTarget = false
break // inner loop
} else {
lset = new_lset
lset = newLset
}
}

Expand Down
4 changes: 2 additions & 2 deletions controllers/opentelemetrycollector_controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -229,7 +229,7 @@ func TestContinueOnRecoverableFailure(t *testing.T) {
{
Name: "should-fail",
Do: func(context.Context, reconcile.Params) error {
return errors.New("should fail!")
return errors.New("should fail")
},
BailOnError: false,
},
Expand All @@ -255,7 +255,7 @@ func TestBreakOnUnrecoverableError(t *testing.T) {
// prepare
cfg := config.New()
taskCalled := false
expectedErr := errors.New("should fail!")
expectedErr := errors.New("should fail")
nsn := types.NamespacedName{Name: "my-instance", Namespace: "default"}
reconciler := controllers.NewReconciler(controllers.Params{
Client: k8sClient,
Expand Down
2 changes: 1 addition & 1 deletion pkg/collector/daemonset.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ func DaemonSet(cfg config.Config, logger logr.Logger, otelcol v1alpha1.OpenTelem
Tolerations: otelcol.Spec.Tolerations,
NodeSelector: otelcol.Spec.NodeSelector,
HostNetwork: otelcol.Spec.HostNetwork,
DNSPolicy: getDnsPolicy(otelcol),
DNSPolicy: getDNSPolicy(otelcol),
SecurityContext: otelcol.Spec.PodSecurityContext,
PriorityClassName: otelcol.Spec.PriorityClassName,
Affinity: otelcol.Spec.Affinity,
Expand Down
24 changes: 12 additions & 12 deletions pkg/collector/daemonset_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -177,20 +177,20 @@ func TestDaemonsetFilterLabels(t *testing.T) {

func TestDaemonSetNodeSelector(t *testing.T) {
// Test default
otelcol_1 := v1alpha1.OpenTelemetryCollector{
otelcol1 := v1alpha1.OpenTelemetryCollector{
ObjectMeta: metav1.ObjectMeta{
Name: "my-instance",
},
}

cfg := config.New()

d1 := DaemonSet(cfg, logger, otelcol_1)
d1 := DaemonSet(cfg, logger, otelcol1)

assert.Empty(t, d1.Spec.Template.Spec.NodeSelector)

// Test nodeSelector
otelcol_2 := v1alpha1.OpenTelemetryCollector{
otelcol2 := v1alpha1.OpenTelemetryCollector{
ObjectMeta: metav1.ObjectMeta{
Name: "my-instance-nodeselector",
},
Expand All @@ -204,25 +204,25 @@ func TestDaemonSetNodeSelector(t *testing.T) {

cfg = config.New()

d2 := DaemonSet(cfg, logger, otelcol_2)
d2 := DaemonSet(cfg, logger, otelcol2)
assert.Equal(t, d2.Spec.Template.Spec.NodeSelector, map[string]string{"node-key": "node-value"})
}

func TestDaemonSetPriorityClassName(t *testing.T) {
otelcol_1 := v1alpha1.OpenTelemetryCollector{
otelcol1 := v1alpha1.OpenTelemetryCollector{
ObjectMeta: metav1.ObjectMeta{
Name: "my-instance",
},
}

cfg := config.New()

d1 := DaemonSet(cfg, logger, otelcol_1)
d1 := DaemonSet(cfg, logger, otelcol1)
assert.Empty(t, d1.Spec.Template.Spec.PriorityClassName)

priorityClassName := "test-class"

otelcol_2 := v1alpha1.OpenTelemetryCollector{
otelcol2 := v1alpha1.OpenTelemetryCollector{
ObjectMeta: metav1.ObjectMeta{
Name: "my-instance-priortyClassName",
},
Expand All @@ -233,23 +233,23 @@ func TestDaemonSetPriorityClassName(t *testing.T) {

cfg = config.New()

d2 := DaemonSet(cfg, logger, otelcol_2)
d2 := DaemonSet(cfg, logger, otelcol2)
assert.Equal(t, priorityClassName, d2.Spec.Template.Spec.PriorityClassName)
}

func TestDaemonSetAffinity(t *testing.T) {
otelcol_1 := v1alpha1.OpenTelemetryCollector{
otelcol1 := v1alpha1.OpenTelemetryCollector{
ObjectMeta: metav1.ObjectMeta{
Name: "my-instance",
},
}

cfg := config.New()

d1 := DaemonSet(cfg, logger, otelcol_1)
d1 := DaemonSet(cfg, logger, otelcol1)
assert.Nil(t, d1.Spec.Template.Spec.Affinity)

otelcol_2 := v1alpha1.OpenTelemetryCollector{
otelcol2 := v1alpha1.OpenTelemetryCollector{
ObjectMeta: metav1.ObjectMeta{
Name: "my-instance-priortyClassName",
},
Expand All @@ -260,7 +260,7 @@ func TestDaemonSetAffinity(t *testing.T) {

cfg = config.New()

d2 := DaemonSet(cfg, logger, otelcol_2)
d2 := DaemonSet(cfg, logger, otelcol2)
assert.NotNil(t, d2.Spec.Template.Spec.Affinity)
assert.Equal(t, *testAffinityValue, *d2.Spec.Template.Spec.Affinity)
}
2 changes: 1 addition & 1 deletion pkg/collector/deployment.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ func Deployment(cfg config.Config, logger logr.Logger, otelcol v1alpha1.OpenTele
ServiceAccountName: ServiceAccountName(otelcol),
Containers: []corev1.Container{Container(cfg, logger, otelcol)},
Volumes: Volumes(cfg, otelcol),
DNSPolicy: getDnsPolicy(otelcol),
DNSPolicy: getDNSPolicy(otelcol),
HostNetwork: otelcol.Spec.HostNetwork,
Tolerations: otelcol.Spec.Tolerations,
NodeSelector: otelcol.Spec.NodeSelector,
Expand Down
Loading

0 comments on commit 17c39e9

Please sign in to comment.