Skip to content

Commit

Permalink
fixing reconcile issues
Browse files Browse the repository at this point in the history
  • Loading branch information
BrennenMM7 committed Jul 24, 2024
1 parent 3c6e674 commit 8ac4b26
Show file tree
Hide file tree
Showing 3 changed files with 4 additions and 44 deletions.
10 changes: 0 additions & 10 deletions exp/controllers/gcpmachinepool_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ import (
"sigs.k8s.io/cluster-api-provider-gcp/cloud/services/compute/instancegroups"
infrav1exp "sigs.k8s.io/cluster-api-provider-gcp/exp/api/v1beta1"
"sigs.k8s.io/cluster-api-provider-gcp/util/reconciler"
clusterv1 "sigs.k8s.io/cluster-api/api/v1beta1"
kubeadmv1 "sigs.k8s.io/cluster-api/bootstrap/kubeadm/api/v1beta1"
expclusterv1 "sigs.k8s.io/cluster-api/exp/api/v1beta1"
"sigs.k8s.io/cluster-api/util"
Expand Down Expand Up @@ -107,15 +106,6 @@ func (r *GCPMachinePoolReconciler) SetupWithManager(ctx context.Context, mgr ctr
return errors.Wrap(err, "failed adding a watch for GCPMachinePoolMachine")
}

// Add a watch on clusterv1.Cluster object for unpause & ready notifications.
if err := c.Watch(
source.Kind(mgr.GetCache(), &clusterv1.Cluster{}),
handler.EnqueueRequestsFromMapFunc(util.ClusterToInfrastructureMapFunc(ctx, gvk, mgr.GetClient(), &infrav1exp.GCPMachinePool{})),
predicates.ClusterUnpausedAndInfrastructureReady(log),
); err != nil {
return errors.Wrap(err, "failed adding a watch for ready clusters")
}

return nil
}

Expand Down
16 changes: 0 additions & 16 deletions exp/controllers/gcpmachinepoolmachine_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,14 +30,12 @@ import (
"sigs.k8s.io/cluster-api-provider-gcp/cloud/services/compute/instancegroupinstances"
infrav1exp "sigs.k8s.io/cluster-api-provider-gcp/exp/api/v1beta1"
"sigs.k8s.io/cluster-api-provider-gcp/util/reconciler"
clusterv1 "sigs.k8s.io/cluster-api/api/v1beta1"
"sigs.k8s.io/cluster-api/util"
"sigs.k8s.io/cluster-api/util/annotations"
"sigs.k8s.io/cluster-api/util/predicates"
"sigs.k8s.io/cluster-api/util/record"
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/client/apiutil"
"sigs.k8s.io/controller-runtime/pkg/controller"
"sigs.k8s.io/controller-runtime/pkg/controller/controllerutil"
"sigs.k8s.io/controller-runtime/pkg/handler"
Expand Down Expand Up @@ -65,11 +63,6 @@ type GCPMachinePoolMachineReconciler struct {
func (r *GCPMachinePoolMachineReconciler) SetupWithManager(ctx context.Context, mgr ctrl.Manager, options controller.Options) error {
log := log.FromContext(ctx).WithValues("controller", "GCPMachinePoolMachine")

gvk, err := apiutil.GVKForObject(new(infrav1exp.GCPMachinePoolMachine), mgr.GetScheme())
if err != nil {
return errors.Wrapf(err, "failed to find GVK for GCPMachinePool")
}

c, err := ctrl.NewControllerManagedBy(mgr).
WithOptions(options).
For(&infrav1exp.GCPMachinePoolMachine{}).
Expand All @@ -88,15 +81,6 @@ func (r *GCPMachinePoolMachineReconciler) SetupWithManager(ctx context.Context,
return errors.Wrap(err, "failed adding a watch for GCPMachinePool")
}

// Add a watch on clusterv1.Cluster object for unpause & ready notifications.
if err := c.Watch(
source.Kind(mgr.GetCache(), &clusterv1.Cluster{}),
handler.EnqueueRequestsFromMapFunc(util.ClusterToInfrastructureMapFunc(ctx, gvk, mgr.GetClient(), &infrav1exp.GCPMachinePoolMachine{})),
predicates.ClusterUnpausedAndInfrastructureReady(log),
); err != nil {
return errors.Wrap(err, "failed adding a watch for ready clusters")
}

return nil
}

Expand Down
22 changes: 4 additions & 18 deletions exp/controllers/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,6 @@ package controllers

import (
"context"
"crypto/sha256"
"encoding/json"
"fmt"

"github.com/go-logr/logr"
Expand Down Expand Up @@ -235,22 +233,10 @@ func MachinePoolModelHasChanged(logger logr.Logger) predicate.Funcs {

newGmp := e.ObjectNew.(*infrav1exp.GCPMachinePool)

// If the spec has changed, we need to update the model
oldSpec, err := json.Marshal(oldGmp.Spec)
if err != nil {
log.Error(err, "failed to marshal old spec")
return false
}
oldHash := sha256.Sum256(oldSpec)

newSpec, err := json.Marshal(newGmp.Spec)
if err != nil {
log.Error(err, "failed to marshal new spec")
return false
}
newHash := sha256.Sum256(newSpec)

shouldUpdate := oldHash != newHash
// if any of these are not equal, run the update
shouldUpdate := oldGmp.Spec.Image != newGmp.Spec.Image ||
oldGmp.Status.Replicas != newGmp.Status.Replicas ||
oldGmp.Status.Ready != newGmp.Status.Ready

if shouldUpdate {
log.Info("machine pool predicate", "shouldUpdate", shouldUpdate)
Expand Down

0 comments on commit 8ac4b26

Please sign in to comment.