diff --git a/controllers/machine_reconcile_scope.go b/controllers/machine_reconcile_scope.go index 4e388ef9..f5dba2c5 100644 --- a/controllers/machine_reconcile_scope.go +++ b/controllers/machine_reconcile_scope.go @@ -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) diff --git a/controllers/machine_reconcile_scope_test.go b/controllers/machine_reconcile_scope_test.go index 7af2dc49..2561ebf0 100644 --- a/controllers/machine_reconcile_scope_test.go +++ b/controllers/machine_reconcile_scope_test.go @@ -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" diff --git a/controllers/tinkerbellmachine_controller.go b/controllers/tinkerbellmachine_controller.go index 825e9e36..71ee9fb6 100644 --- a/controllers/tinkerbellmachine_controller.go +++ b/controllers/tinkerbellmachine_controller.go @@ -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). @@ -87,10 +89,11 @@ 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. @@ -98,6 +101,7 @@ func (r *TinkerbellMachineReconciler) Reconcile(ctx context.Context, req ctrl.Re if err != nil { return ctrl.Result{}, fmt.Errorf("getting valid Machine object: %w", err) } + if machine == nil { return ctrl.Result{}, nil } @@ -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) @@ -127,11 +134,11 @@ 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, @@ -139,7 +146,7 @@ func (tmr *TinkerbellMachineReconciler) SetupWithManager( log := ctrl.LoggerFrom(ctx) clusterToObjectFunc, err := util.ClusterToTypedObjectsMapper( - tmr.Client, + r.Client, &infrastructurev1.TinkerbellMachineList{}, mgr.GetScheme(), ) @@ -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{}, @@ -159,7 +166,7 @@ func (tmr *TinkerbellMachineReconciler) SetupWithManager( ). Watches( &infrastructurev1.TinkerbellCluster{}, - handler.EnqueueRequestsFromMapFunc(tmr.TinkerbellClusterToTinkerbellMachines(ctx)), + handler.EnqueueRequestsFromMapFunc(r.TinkerbellClusterToTinkerbellMachines(ctx)), ). Watches( &clusterv1.Cluster{}, @@ -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) } @@ -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 { @@ -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: @@ -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") @@ -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 } diff --git a/controllers/tinkerbellmachine_controller_test.go b/controllers/tinkerbellmachine_controller_test.go index a71cd518..2cdb6e36 100644 --- a/controllers/tinkerbellmachine_controller_test.go +++ b/controllers/tinkerbellmachine_controller_test.go @@ -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" @@ -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), }, }, } diff --git a/main.go b/main.go index a390be10..4ede719a 100644 --- a/main.go +++ b/main.go @@ -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" @@ -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 ) @@ -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