Skip to content

Commit

Permalink
prevent constantly shadowing the client package with a client variable
Browse files Browse the repository at this point in the history
On-behalf-of: @SAP christoph.mewes@sap.com
  • Loading branch information
xrstf committed Feb 4, 2025
1 parent bd8a00d commit 9c74d90
Show file tree
Hide file tree
Showing 8 changed files with 36 additions and 32 deletions.
8 changes: 6 additions & 2 deletions .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,12 @@ run:
allow-parallel-runners: true

issues:
max-same-issues: 0

# don't skip warning about doc comments
# don't exclude the default set of lint
exclude-use-default: false

linters:
disable-all: true
enable:
Expand All @@ -31,8 +34,6 @@ linters:
- unused

linters-settings:
max-same-issues: 0

revive:
rules:
- name: comment-spacings
Expand All @@ -55,3 +56,6 @@ linters-settings:
alias: apierrors
- pkg: k8s.io/apimachinery/pkg/util/errors
alias: kerrors
# Controller Runtime (otherwise this will usually lead to shadowing a local "client" variable)
- pkg: sigs.k8s.io/controller-runtime/pkg/client
alias: ctrlruntimeclient
4 changes: 2 additions & 2 deletions internal/controller/cacheserver_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,15 @@ import (

"k8s.io/apimachinery/pkg/runtime"
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/client"
ctrlruntimeclient "sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/log"

operatorv1alpha1 "github.com/kcp-dev/kcp-operator/sdk/apis/operator/v1alpha1"
)

// CacheServerReconciler reconciles a CacheServer object
type CacheServerReconciler struct {
client.Client
ctrlruntimeclient.Client
Scheme *runtime.Scheme
}

Expand Down
14 changes: 7 additions & 7 deletions internal/controller/frontproxy_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ import (
kerrors "k8s.io/apimachinery/pkg/util/errors"
utilruntime "k8s.io/apimachinery/pkg/util/runtime"
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/client"
ctrlruntimeclient "sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/handler"
"sigs.k8s.io/controller-runtime/pkg/log"
"sigs.k8s.io/controller-runtime/pkg/reconcile"
Expand All @@ -46,25 +46,25 @@ import (

// FrontProxyReconciler reconciles a FrontProxy object
type FrontProxyReconciler struct {
client.Client
ctrlruntimeclient.Client
Scheme *runtime.Scheme
}

// SetupWithManager sets up the controller with the Manager.
func (r *FrontProxyReconciler) SetupWithManager(mgr ctrl.Manager) error {
rootShardHandler := handler.TypedEnqueueRequestsFromMapFunc(func(ctx context.Context, obj client.Object) []reconcile.Request {
rootShardHandler := handler.TypedEnqueueRequestsFromMapFunc(func(ctx context.Context, obj ctrlruntimeclient.Object) []reconcile.Request {
rootShard := obj.(*operatorv1alpha1.RootShard)

var fpList operatorv1alpha1.FrontProxyList
if err := mgr.GetClient().List(ctx, &fpList, &client.ListOptions{Namespace: rootShard.Namespace}); err != nil {
if err := mgr.GetClient().List(ctx, &fpList, &ctrlruntimeclient.ListOptions{Namespace: rootShard.Namespace}); err != nil {
utilruntime.HandleError(err)
return nil
}

var requests []reconcile.Request
for _, frontProxy := range fpList.Items {
if ref := frontProxy.Spec.RootShard.Reference; ref != nil && ref.Name == rootShard.Name {
requests = append(requests, reconcile.Request{NamespacedName: client.ObjectKeyFromObject(&frontProxy)})
requests = append(requests, reconcile.Request{NamespacedName: ctrlruntimeclient.ObjectKeyFromObject(&frontProxy)})
}
}

Expand Down Expand Up @@ -95,7 +95,7 @@ func (r *FrontProxyReconciler) Reconcile(ctx context.Context, req ctrl.Request)

var frontProxy operatorv1alpha1.FrontProxy
if err := r.Client.Get(ctx, req.NamespacedName, &frontProxy); err != nil {
if client.IgnoreNotFound(err) != nil {
if ctrlruntimeclient.IgnoreNotFound(err) != nil {
return ctrl.Result{}, fmt.Errorf("failed to get FrontProxy object: %w", err)
}

Expand Down Expand Up @@ -204,7 +204,7 @@ func (r *FrontProxyReconciler) reconcileStatus(ctx context.Context, oldFrontProx

// only patch the status if there are actual changes.
if !equality.Semantic.DeepEqual(oldFrontProxy.Status, frontProxy.Status) {
if err := r.Client.Status().Patch(ctx, frontProxy, client.MergeFrom(oldFrontProxy)); err != nil {
if err := r.Client.Status().Patch(ctx, frontProxy, ctrlruntimeclient.MergeFrom(oldFrontProxy)); err != nil {
errs = append(errs, err)
}
}
Expand Down
4 changes: 2 additions & 2 deletions internal/controller/kubeconfig_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ import (
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/types"
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/client"
ctrlruntimeclient "sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/log"

"github.com/kcp-dev/kcp-operator/internal/reconciling"
Expand All @@ -42,7 +42,7 @@ import (

// KubeconfigReconciler reconciles a Kubeconfig object
type KubeconfigReconciler struct {
client.Client
ctrlruntimeclient.Client
Scheme *runtime.Scheme
}

Expand Down
8 changes: 4 additions & 4 deletions internal/controller/rootshard_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ import (
"k8s.io/apimachinery/pkg/types"
kerrors "k8s.io/apimachinery/pkg/util/errors"
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/client"
ctrlruntimeclient "sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/log"

"github.com/kcp-dev/kcp-operator/internal/reconciling"
Expand All @@ -43,7 +43,7 @@ import (

// RootShardReconciler reconciles a RootShard object
type RootShardReconciler struct {
client.Client
ctrlruntimeclient.Client
Scheme *runtime.Scheme
}

Expand Down Expand Up @@ -82,7 +82,7 @@ func (r *RootShardReconciler) Reconcile(ctx context.Context, req ctrl.Request) (

var rootShard operatorv1alpha1.RootShard
if err := r.Client.Get(ctx, req.NamespacedName, &rootShard); err != nil {
if client.IgnoreNotFound(err) != nil {
if ctrlruntimeclient.IgnoreNotFound(err) != nil {
return ctrl.Result{}, fmt.Errorf("failed to find %s/%s: %w", req.Namespace, req.Name, err)
}

Expand Down Expand Up @@ -199,7 +199,7 @@ func (r *RootShardReconciler) reconcileStatus(ctx context.Context, oldRootShard

// only patch the status if there are actual changes.
if !equality.Semantic.DeepEqual(oldRootShard.Status, rootShard.Status) {
if err := r.Client.Status().Patch(ctx, rootShard, client.MergeFrom(oldRootShard)); err != nil {
if err := r.Client.Status().Patch(ctx, rootShard, ctrlruntimeclient.MergeFrom(oldRootShard)); err != nil {
errs = append(errs, err)
}
}
Expand Down
14 changes: 7 additions & 7 deletions internal/controller/shard_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ import (
kerrors "k8s.io/apimachinery/pkg/util/errors"
utilruntime "k8s.io/apimachinery/pkg/util/runtime"
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/client"
ctrlruntimeclient "sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/handler"
"sigs.k8s.io/controller-runtime/pkg/log"
"sigs.k8s.io/controller-runtime/pkg/reconcile"
Expand All @@ -46,25 +46,25 @@ import (

// ShardReconciler reconciles a Shard object
type ShardReconciler struct {
client.Client
ctrlruntimeclient.Client
Scheme *runtime.Scheme
}

// SetupWithManager sets up the controller with the Manager.
func (r *ShardReconciler) SetupWithManager(mgr ctrl.Manager) error {
rootShardHandler := handler.TypedEnqueueRequestsFromMapFunc(func(ctx context.Context, obj client.Object) []reconcile.Request {
rootShardHandler := handler.TypedEnqueueRequestsFromMapFunc(func(ctx context.Context, obj ctrlruntimeclient.Object) []reconcile.Request {
rootShard := obj.(*operatorv1alpha1.RootShard)

var shards operatorv1alpha1.ShardList
if err := mgr.GetClient().List(ctx, &shards, &client.ListOptions{Namespace: rootShard.Namespace}); err != nil {
if err := mgr.GetClient().List(ctx, &shards, &ctrlruntimeclient.ListOptions{Namespace: rootShard.Namespace}); err != nil {
utilruntime.HandleError(err)
return nil
}

var requests []reconcile.Request
for _, shard := range shards.Items {
if ref := shard.Spec.RootShard.Reference; ref != nil && ref.Name == rootShard.Name {
requests = append(requests, reconcile.Request{NamespacedName: client.ObjectKeyFromObject(&shard)})
requests = append(requests, reconcile.Request{NamespacedName: ctrlruntimeclient.ObjectKeyFromObject(&shard)})
}
}

Expand Down Expand Up @@ -94,7 +94,7 @@ func (r *ShardReconciler) Reconcile(ctx context.Context, req ctrl.Request) (res

var s operatorv1alpha1.Shard
if err := r.Client.Get(ctx, req.NamespacedName, &s); err != nil {
if client.IgnoreNotFound(err) != nil {
if ctrlruntimeclient.IgnoreNotFound(err) != nil {
return ctrl.Result{}, fmt.Errorf("failed to get shard: %w", err)
}

Expand Down Expand Up @@ -189,7 +189,7 @@ func (r *ShardReconciler) reconcileStatus(ctx context.Context, oldShard *operato

// only patch the status if there are actual changes.
if !equality.Semantic.DeepEqual(oldShard.Status, newShard.Status) {
if err := r.Client.Status().Patch(ctx, newShard, client.MergeFrom(oldShard)); err != nil {
if err := r.Client.Status().Patch(ctx, newShard, ctrlruntimeclient.MergeFrom(oldShard)); err != nil {
errs = append(errs, err)
}
}
Expand Down
6 changes: 3 additions & 3 deletions internal/controller/suite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ import (

"k8s.io/client-go/kubernetes/scheme"
"k8s.io/client-go/rest"
"sigs.k8s.io/controller-runtime/pkg/client"
ctrlruntimeclient "sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/envtest"
logf "sigs.k8s.io/controller-runtime/pkg/log"
"sigs.k8s.io/controller-runtime/pkg/log/zap"
Expand All @@ -41,7 +41,7 @@ import (
// http://onsi.github.io/ginkgo/ to learn more about Ginkgo.

var cfg *rest.Config
var k8sClient client.Client
var k8sClient ctrlruntimeclient.Client
var testEnv *envtest.Environment
var ctx context.Context
var cancel context.CancelFunc
Expand Down Expand Up @@ -88,7 +88,7 @@ var _ = BeforeSuite(func() {

// +kubebuilder:scaffold:scheme

k8sClient, err = client.New(cfg, client.Options{Scheme: scheme.Scheme})
k8sClient, err = ctrlruntimeclient.New(cfg, ctrlruntimeclient.Options{Scheme: scheme.Scheme})
Expect(err).NotTo(HaveOccurred())
Expect(k8sClient).NotTo(BeNil())

Expand Down
10 changes: 5 additions & 5 deletions internal/controller/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ import (
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/types"
"k8s.io/utils/ptr"
"sigs.k8s.io/controller-runtime/pkg/client"
ctrlruntimeclient "sigs.k8s.io/controller-runtime/pkg/client"

operatorv1alpha1 "github.com/kcp-dev/kcp-operator/sdk/apis/operator/v1alpha1"
)
Expand All @@ -35,9 +35,9 @@ func deploymentReady(dep appsv1.Deployment) bool {
return dep.Status.UpdatedReplicas == dep.Status.ReadyReplicas && dep.Status.ReadyReplicas == ptr.Deref(dep.Spec.Replicas, 0)
}

func getDeploymentAvailableCondition(ctx context.Context, c client.Client, key types.NamespacedName) (metav1.Condition, error) {
func getDeploymentAvailableCondition(ctx context.Context, client ctrlruntimeclient.Client, key types.NamespacedName) (metav1.Condition, error) {
var dep appsv1.Deployment
if err := c.Get(ctx, key, &dep); client.IgnoreNotFound(err) != nil {
if err := client.Get(ctx, key, &dep); ctrlruntimeclient.IgnoreNotFound(err) != nil {
return metav1.Condition{}, err
}

Expand Down Expand Up @@ -90,7 +90,7 @@ func updateCondition(conditions []metav1.Condition, newCondition metav1.Conditio
return conditions
}

func fetchRootShard(ctx context.Context, c client.Client, namespace string, ref *corev1.LocalObjectReference) (metav1.Condition, *operatorv1alpha1.RootShard) {
func fetchRootShard(ctx context.Context, client ctrlruntimeclient.Client, namespace string, ref *corev1.LocalObjectReference) (metav1.Condition, *operatorv1alpha1.RootShard) {
if ref == nil {
return metav1.Condition{
Type: string(operatorv1alpha1.ConditionTypeRootShard),
Expand All @@ -101,7 +101,7 @@ func fetchRootShard(ctx context.Context, c client.Client, namespace string, ref
}

rootShard := &operatorv1alpha1.RootShard{}
if err := c.Get(ctx, types.NamespacedName{Name: ref.Name, Namespace: namespace}, rootShard); err != nil {
if err := client.Get(ctx, types.NamespacedName{Name: ref.Name, Namespace: namespace}, rootShard); err != nil {
return metav1.Condition{
Type: string(operatorv1alpha1.ConditionTypeRootShard),
Status: metav1.ConditionFalse,
Expand Down

0 comments on commit 9c74d90

Please sign in to comment.