Skip to content

Commit

Permalink
Fix linting issues
Browse files Browse the repository at this point in the history
Signed-off-by: Jacob Weinstock <[email protected]>
  • Loading branch information
jacobweinstock committed Dec 20, 2023
1 parent 973e5b6 commit 6b32e85
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 24 deletions.
4 changes: 3 additions & 1 deletion controllers/machine_reconcile_scope.go
Original file line number Diff line number Diff line change
Expand Up @@ -519,8 +519,10 @@ func (scope *machineReconcileScope) assignedHardware() (*tinkv1.Hardware, error)
//nolint:lll
func byHardwareAffinity(hardware []tinkv1.Hardware, preferred []infrastructurev1.WeightedHardwareAffinityTerm) (func(i int, j int) bool, error) {
scores := map[client.ObjectKey]int32{}
// compute scores for each item based on the preferred term weightss
// compute scores for each item based on the preferred term weights
for _, term := range preferred {
term := term

selector, err := metav1.LabelSelectorAsSelector(&term.HardwareAffinityTerm.LabelSelector)
if err != nil {
return nil, fmt.Errorf("constructing label selector: %w", err)
Expand Down
2 changes: 1 addition & 1 deletion controllers/machine_reconcile_scope_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ See the License for the specific language governing permissions and
limitations under the License.
*/

package controllers
package controllers //nolint:testpackage

import (
"testing"
Expand Down
49 changes: 33 additions & 16 deletions controllers/tinkerbellmachine_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,8 @@ type TinkerbellMachineReconciler struct {
// +kubebuilder:rbac:groups=bmc.tinkerbell.org,resources=jobs,verbs=get;list;watch;create

// Reconcile ensures that all Tinkerbell machines are aligned with a given spec.
//
//nolint:funlen,cyclop
func (r *TinkerbellMachineReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.Result, error) {
// If the TinkerbellMachineReconciler instant is invalid we can't continue. There's also no way
// for us to recover the TinkerbellMachineReconciler instance (i.e. there's a programmer error).
Expand Down Expand Up @@ -87,17 +89,19 @@ func (r *TinkerbellMachineReconciler) Reconcile(ctx context.Context, req ctrl.Re
if err != nil {
return ctrl.Result{}, fmt.Errorf("initialize patch helper: %w", err)
}

scope.patchHelper = patchHelper

if scope.MachineScheduledForDeletion() {
return ctrl.Result{}, scope.DeleteMachineWithDependencies() //nolint:wrapcheck
return ctrl.Result{}, scope.DeleteMachineWithDependencies()
}

// We must be bound to a CAPI Machine object before we can continue.
machine, err := scope.getReadyMachine()
if err != nil {
return ctrl.Result{}, fmt.Errorf("getting valid Machine object: %w", err)
}

if machine == nil {
return ctrl.Result{}, nil
}
Expand All @@ -108,8 +112,11 @@ func (r *TinkerbellMachineReconciler) Reconcile(ctx context.Context, req ctrl.Re
if err != nil {
return ctrl.Result{}, fmt.Errorf("receiving bootstrap cloud config: %w", err)
}

if bootstrapCloudConfig == "" {
return ctrl.Result{RequeueAfter: 30 * time.Second}, nil
const requeueAfter = 30 * time.Second

return ctrl.Result{RequeueAfter: requeueAfter}, nil
}

tinkerbellCluster, err := scope.getReadyTinkerbellCluster(machine)
Expand All @@ -127,19 +134,19 @@ func (r *TinkerbellMachineReconciler) Reconcile(ctx context.Context, req ctrl.Re
scope.bootstrapCloudConfig = bootstrapCloudConfig
scope.tinkerbellCluster = tinkerbellCluster

return ctrl.Result{}, scope.Reconcile() //nolint:wrapcheck
return ctrl.Result{}, scope.Reconcile()
}

// SetupWithManager configures reconciler with a given manager.
func (tmr *TinkerbellMachineReconciler) SetupWithManager(
func (r *TinkerbellMachineReconciler) SetupWithManager(
ctx context.Context,
mgr ctrl.Manager,
options controller.Options,
) error {
log := ctrl.LoggerFrom(ctx)

clusterToObjectFunc, err := util.ClusterToTypedObjectsMapper(
tmr.Client,
r.Client,
&infrastructurev1.TinkerbellMachineList{},
mgr.GetScheme(),
)
Expand All @@ -149,7 +156,7 @@ func (tmr *TinkerbellMachineReconciler) SetupWithManager(

builder := ctrl.NewControllerManagedBy(mgr).
WithOptions(options).
WithEventFilter(predicates.ResourceNotPausedAndHasFilterLabel(log, tmr.WatchFilterValue)).
WithEventFilter(predicates.ResourceNotPausedAndHasFilterLabel(log, r.WatchFilterValue)).
For(&infrastructurev1.TinkerbellMachine{}).
Watches(
&clusterv1.Machine{},
Expand All @@ -159,7 +166,7 @@ func (tmr *TinkerbellMachineReconciler) SetupWithManager(
).
Watches(
&infrastructurev1.TinkerbellCluster{},
handler.EnqueueRequestsFromMapFunc(tmr.TinkerbellClusterToTinkerbellMachines(ctx)),
handler.EnqueueRequestsFromMapFunc(r.TinkerbellClusterToTinkerbellMachines(ctx)),
).
Watches(
&clusterv1.Cluster{},
Expand All @@ -168,14 +175,24 @@ func (tmr *TinkerbellMachineReconciler) SetupWithManager(
).
Watches(
&tinkv1.Workflow{},
handler.EnqueueRequestForOwner(mgr.GetScheme(), mgr.GetRESTMapper(), &infrastructurev1.TinkerbellMachine{}, handler.OnlyControllerOwner()),
handler.EnqueueRequestForOwner(
mgr.GetScheme(),
mgr.GetRESTMapper(),
&infrastructurev1.TinkerbellMachine{},
handler.OnlyControllerOwner(),
),
).
Watches(
&rufiov1.Job{},
handler.EnqueueRequestForOwner(mgr.GetScheme(), mgr.GetRESTMapper(), &infrastructurev1.TinkerbellMachine{}, handler.OnlyControllerOwner()),
handler.EnqueueRequestForOwner(
mgr.GetScheme(),
mgr.GetRESTMapper(),
&infrastructurev1.TinkerbellMachine{},
handler.OnlyControllerOwner(),
),
)

if err := builder.Complete(tmr); err != nil {
if err := builder.Complete(r); err != nil {
return fmt.Errorf("failed to create controller: %w", err)
}

Expand All @@ -184,7 +201,7 @@ func (tmr *TinkerbellMachineReconciler) SetupWithManager(

// TinkerbellClusterToTinkerbellMachines is a handler.ToRequestsFunc to be used to enqeue requests for reconciliation
// of TinkerbellMachines.
func (tmr *TinkerbellMachineReconciler) TinkerbellClusterToTinkerbellMachines(ctx context.Context) handler.MapFunc {
func (r *TinkerbellMachineReconciler) TinkerbellClusterToTinkerbellMachines(ctx context.Context) handler.MapFunc {
log := ctrl.LoggerFrom(ctx)

return func(ctx context.Context, o client.Object) []ctrl.Request {
Expand All @@ -207,7 +224,7 @@ func (tmr *TinkerbellMachineReconciler) TinkerbellClusterToTinkerbellMachines(ct
return nil
}

cluster, err := util.GetOwnerCluster(ctx, tmr.Client, c.ObjectMeta)
cluster, err := util.GetOwnerCluster(ctx, r.Client, c.ObjectMeta)

switch {
case apierrors.IsNotFound(err) || cluster == nil:
Expand All @@ -220,7 +237,7 @@ func (tmr *TinkerbellMachineReconciler) TinkerbellClusterToTinkerbellMachines(ct
return nil
}

machines, err := collections.GetFilteredMachinesForCluster(ctx, tmr.Client, cluster)
machines, err := collections.GetFilteredMachinesForCluster(ctx, r.Client, cluster)
if err != nil {
log.Error(err, "failed to get Machines for Cluster")

Expand All @@ -244,12 +261,12 @@ func (tmr *TinkerbellMachineReconciler) TinkerbellClusterToTinkerbellMachines(ct
}

// validate validates if context configuration has all required fields properly populated.
func (tmr *TinkerbellMachineReconciler) validate() error {
if tmr == nil {
func (r *TinkerbellMachineReconciler) validate() error {
if r == nil {
return ErrConfigurationNil
}

if tmr.Client == nil {
if r.Client == nil {
return ErrMissingClient
}

Expand Down
6 changes: 3 additions & 3 deletions controllers/tinkerbellmachine_controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ import (
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/types"
"k8s.io/utils/pointer"
"k8s.io/utils/ptr"
clusterv1 "sigs.k8s.io/cluster-api/api/v1beta1"
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/client"
Expand Down Expand Up @@ -144,9 +144,9 @@ func validMachine(name, namespace, clusterName string) *clusterv1.Machine {
},
},
Spec: clusterv1.MachineSpec{
Version: pointer.String("1.19.4"),
Version: ptr.To[string]("1.19.4"),
Bootstrap: clusterv1.Bootstrap{
DataSecretName: pointer.String(name),
DataSecretName: ptr.To[string](name),
},
},
}
Expand Down
7 changes: 4 additions & 3 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ import (
cgrecord "k8s.io/client-go/tools/record"
"k8s.io/component-base/version"
"k8s.io/klog/v2"
"k8s.io/klog/v2/klogr"
"k8s.io/klog/v2/textlogger"
clusterv1 "sigs.k8s.io/cluster-api/api/v1beta1"
"sigs.k8s.io/cluster-api/util/record"
ctrl "sigs.k8s.io/controller-runtime"
Expand All @@ -39,9 +39,10 @@ import (
rufiov1 "github.com/tinkerbell/rufio/api/v1alpha1"
tinkv1 "github.com/tinkerbell/tink/api/v1alpha1"

metricsserver "sigs.k8s.io/controller-runtime/pkg/metrics/server"

infrastructurev1 "github.com/tinkerbell/cluster-api-provider-tinkerbell/api/v1beta1"
"github.com/tinkerbell/cluster-api-provider-tinkerbell/controllers"
metricsserver "sigs.k8s.io/controller-runtime/pkg/metrics/server"
// +kubebuilder:scaffold:imports
)

Expand Down Expand Up @@ -269,7 +270,7 @@ func main() { //nolint:funlen
}()
}

ctrl.SetLogger(klogr.New())
ctrl.SetLogger(textlogger.NewLogger(&textlogger.Config{}))

// Machine and cluster operations can create enough events to trigger the event recorder spam filter
// Setting the burst size higher ensures all events will be recorded and submitted to the API
Expand Down

0 comments on commit 6b32e85

Please sign in to comment.