Skip to content

Commit

Permalink
chore: Fix linter errors
Browse files Browse the repository at this point in the history
  • Loading branch information
rg0now committed Oct 4, 2024
1 parent 88bd7fd commit efb7534
Show file tree
Hide file tree
Showing 46 changed files with 310 additions and 373 deletions.
11 changes: 5 additions & 6 deletions .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,8 @@ linters:
- errchkjson
- errorlint
- exhaustive
- exportloopref
- ginkgolinter
- goconst
- gocritic
- gocyclo
- gofmt
- goimports
- goprintffuncname
Expand All @@ -24,13 +21,13 @@ linters:
- govet
- importas
- ineffassign
- makezero
# - makezero
- misspell
- nakedret
- nilerr
- nolintlint
- prealloc
- revive
# - revive
- staticcheck
- stylecheck
- tagliatelle
Expand Down Expand Up @@ -168,8 +165,10 @@ issues:
text: "G304: Potential file inclusion via variable"
- linters:
- dupl
- errcheck
- goconst
path: _test\.go

run:
go: "1.22"
timeout: 10m
Expand Down
2 changes: 2 additions & 0 deletions internal/buildinfo/build_info.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,14 @@ package buildinfo

import "fmt"

// BuildInfo holds all sorts of information about the build of an executable artifact.
type BuildInfo struct {
Version string
CommitHash string
BuildDate string
}

// String returns the build into as a string.
func (i BuildInfo) String() string {
return fmt.Sprintf("version %s (%s) built on %s", i.Version, i.CommitHash, i.BuildDate)
}
5 changes: 5 additions & 0 deletions internal/testutils/testutils.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package testutils
import "k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"

var (
// TestSvc is an object def used for testing.
TestSvc = &unstructured.Unstructured{
Object: map[string]any{
"apiVersion": "v1",
Expand All @@ -27,6 +28,7 @@ var (
},
}

// TestEndpointSlice is an object def used for testing.
TestEndpointSlice = &unstructured.Unstructured{
Object: map[string]any{
"apiVersion": "discovery.k8s.io/v1",
Expand Down Expand Up @@ -77,6 +79,7 @@ var (
},
}

// TestConfigMap is an object def used for testing.
TestConfigMap = &unstructured.Unstructured{
Object: map[string]any{
"apiVersion": "v1",
Expand All @@ -92,6 +95,7 @@ var (
},
}

// TestDeployment is an object def used for testing.
TestDeployment = &unstructured.Unstructured{
Object: map[string]any{
"apiVersion": "apps/v1",
Expand Down Expand Up @@ -129,6 +133,7 @@ var (
},
}

// TestPod is an object def used for testing.
TestPod = &unstructured.Unstructured{
Object: map[string]any{
"apiVersion": "/v1",
Expand Down
6 changes: 3 additions & 3 deletions pkg/api/operator/v1alpha1/controller_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,12 +72,12 @@ type Target struct {
Type TargetType `json:"type,omitempty"`
}

// TargetType represents the type of a target.
type TargetType string

const (
// Updates defines a target type that will fully overwrite the target resource with the
// controller ouutput.
// Updater is a target that will fully overwrite the target resource with the update.
Updater TargetType = "Updater"
// Updates defines a target type that will applies the fully overwrite the target resource.
// Patcher is a target that applies the update as a patch to the target resource.
Patcher TargetType = "Patcher"
)
12 changes: 7 additions & 5 deletions pkg/api/operator/v1alpha1/operator_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,16 +106,18 @@ const (
//
// Controllers may raise this condition with other reasons, but should prefer to use the
// reasons listed above to improve interoperability.

// ControllerConditionReady represents the Ready condition.
ControllerConditionReady ControllerConditionType = "Ready"

// This reason is used with the "Ready" condition when the condition is true.
// ControllerReasonReady is used with the "Ready" condition when the condition is true.
ControllerReasonReady ControllerConditionReason = "Ready"

// This reason is used with the "Ready" condition when reconciliation has failed for some
// input resources.
// ControllerReasonReconciliationFailed is used with the "Ready" condition when
// reconciliation has failed for some input resources.
ControllerReasonReconciliationFailed ControllerConditionReason = "ReconciliationFailed"

// This reason is used with the "Ready" condition when the controller is not ready for
// processing events.
// ControllerReasonNotReady is used with the "Ready" condition when the controller is not
// ready for processing events.
ControllerReasonNotReady ControllerConditionReason = "NotReady"
)
1 change: 1 addition & 0 deletions pkg/api/view/v1alpha1/group_version.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ var (
AddToScheme = SchemeBuilder.AddToScheme
)

// NewGVK returns the full GVK of a view.
func NewGVK(view string) schema.GroupVersionKind {
return GroupVersion.WithKind(view)
}
2 changes: 1 addition & 1 deletion pkg/cache/composite_cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ func (cc *CompositeCache) Start(ctx context.Context) error {

// we must run this in a goroutine, otherwise the default cache cannot start up
// ignore the returned error: viewcache.Start() never errs
go cc.viewCache.Start(ctx)
go cc.viewCache.Start(ctx) //nolint:errcheck

return cc.defaultCache.Start(ctx)
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/cache/composite_cache_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ var _ = Describe("CompositeCache", func() {
err = cache.List(ctx, list)
Expect(err).NotTo(HaveOccurred())
Expect(list.Items).To(HaveLen(1))
Expect(object.DeepEqual(&list.Items[0], pod)).NotTo(BeFalse())
Expect(object.DeepEqual(&list.Items[0], pod)).To(BeTrue())
})

It("should return an empty list when cache is empty", func() {
Expand Down
6 changes: 5 additions & 1 deletion pkg/cache/store.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,11 @@ func (s *Store) GetByKey(key string) (object.Object, bool, error) {
// given list. Store takes ownership of the list, you should not reference
// it after calling this function.
func (s *Store) Replace(objs []object.Object, arg string) error {
return s.Replace(objs, arg)
as := make([]any, len(objs))
for i := range objs {
as[i] = objs[i]
}
return s.Store.Replace(as, arg)
}

// Resync is meaningless in the terms appearing here but has
Expand Down
16 changes: 5 additions & 11 deletions pkg/cache/view_cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,10 @@ const DefaultWatchChannelBuffer = 256
var _ cache.Cache = &ViewCache{}

type ViewCache struct {
mu sync.RWMutex
caches map[schema.GroupVersionKind]toolscache.Indexer
informers map[schema.GroupVersionKind]*ViewCacheInformer
informersMux sync.RWMutex
logger, log logr.Logger
mu sync.RWMutex
caches map[schema.GroupVersionKind]toolscache.Indexer
informers map[schema.GroupVersionKind]*ViewCacheInformer
logger, log logr.Logger
}

func NewViewCache(opts Options) *ViewCache {
Expand All @@ -45,7 +44,6 @@ func NewViewCache(opts Options) *ViewCache {
return c
}

// cache handlers
func (c *ViewCache) RegisterCacheForKind(gvk schema.GroupVersionKind) error {
c.log.V(1).Info("registering cache for new GVK", "gvk", gvk)

Expand Down Expand Up @@ -88,7 +86,6 @@ func (c *ViewCache) GetCacheForKind(gvk schema.GroupVersionKind) (toolscache.Ind
return indexer, nil
}

// informer handlers
func (c *ViewCache) RegisterInformerForKind(gvk schema.GroupVersionKind) error {
c.log.V(4).Info("registering informer for new GVK", "gvk", gvk)

Expand Down Expand Up @@ -241,14 +238,12 @@ func (c *ViewCache) Delete(obj object.Object) error {
return nil
}

// indexer is unimplemented
func (c *ViewCache) IndexField(ctx context.Context, obj client.Object, field string, extractValue client.IndexerFunc) error {
gvk := obj.GetObjectKind().GroupVersionKind()
c.log.Info("IndexField called on ViewCache", "gvk", gvk)
return errors.New("field indexing is not supported for ViewCache")
}

// client.Reader
func (c *ViewCache) Get(ctx context.Context, key client.ObjectKey, obj client.Object, opts ...client.GetOption) error {
target, ok := obj.(object.Object)
if !ok {
Expand Down Expand Up @@ -319,7 +314,6 @@ func (c *ViewCache) Dump(ctx context.Context, gvk schema.GroupVersionKind) []str
return ret
}

// watcher
func (c *ViewCache) Watch(ctx context.Context, list client.ObjectList, opts ...client.ListOption) (watch.Interface, error) {
gvk := list.GetObjectKind().GroupVersionKind()

Expand Down Expand Up @@ -358,7 +352,7 @@ func (c *ViewCache) Watch(ctx context.Context, list client.ObjectList, opts ...c

c.log.V(5).Info("stopping watcher", "gvk", gvk)

informer.RemoveEventHandler(handlerReg)
informer.RemoveEventHandler(handlerReg) //nolint:errcheck
watcher.Stop()
}()

Expand Down
4 changes: 3 additions & 1 deletion pkg/cache/view_cache_informer.go
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ func (c *ViewCacheInformer) TriggerEvent(eventType toolscache.DeltaType, oldObj,
c.log.V(8).Info("trigger-event: sending event to handler", "event", eventType,
"object", object.Dump(newObj), "handler-id", handler.id)

switch eventType {
switch eventType { //nolint:exhaustive
case toolscache.Added:
handler.OnAdd(object.DeepCopy(newObj), false)
events++
Expand All @@ -142,6 +142,8 @@ func (c *ViewCacheInformer) TriggerEvent(eventType toolscache.DeltaType, oldObj,
case toolscache.Deleted:
handler.OnDelete(object.DeepCopy(newObj))
events++
default:
c.log.V(4).Info("trigger-event: ignoring event", "event", eventType)
}
}
}
Expand Down
18 changes: 9 additions & 9 deletions pkg/controller/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ type Options struct {

var _ runtimeManager.Runnable = &Controller{}

// implementation
// Controller is a dcontroller reconciler.
type Controller struct {
name, kind string
config opv1a1.Controller
Expand Down Expand Up @@ -192,34 +192,34 @@ func (c *Controller) Start(ctx context.Context) error {
func (c *Controller) GetStatus(gen int64) opv1a1.ControllerStatus {
status := opv1a1.ControllerStatus{Name: c.name}

condition := metav1.Condition{}
if c.errReporter.IsEmpty() {
var condition metav1.Condition
switch {
case c.errReporter.IsEmpty():
condition = metav1.Condition{
Type: string(opv1a1.ControllerConditionReady),
Status: metav1.ConditionTrue,
ObservedGeneration: gen,
LastTransitionTime: metav1.Now(),
Reason: string(opv1a1.ControllerReasonReady),
Message: fmt.Sprintf("Controller is up and running"),
Message: "Controller is up and running",
}
} else if c.errReporter.IsCritical() {
case c.errReporter.IsCritical():
condition = metav1.Condition{
Type: string(opv1a1.ControllerConditionReady),
Status: metav1.ConditionFalse,
ObservedGeneration: gen,
LastTransitionTime: metav1.Now(),
Reason: string(opv1a1.ControllerReasonNotReady),
Message: fmt.Sprintf(fmt.Sprintf("Controller failed to start due to a critcal error")),
Message: "Controller failed to start due to a critcal error",
}

} else {
default:
condition = metav1.Condition{
Type: string(opv1a1.ControllerConditionReady),
Status: metav1.ConditionUnknown,
ObservedGeneration: gen,
LastTransitionTime: metav1.Now(),
Reason: string(opv1a1.ControllerReasonReconciliationFailed),
Message: fmt.Sprintf(fmt.Sprintf("Controller seems functional but there were reconciliation errors")),
Message: "Controller seems functional but there were reconciliation errors",
}
}

Expand Down
Loading

0 comments on commit efb7534

Please sign in to comment.