Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make a deep copy of cluster prior to calling actuator #426

Merged
merged 1 commit into from
Jul 17, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 9 additions & 7 deletions pkg/controller/cluster/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,33 +59,35 @@ func (c *ClusterControllerImpl) Init(arguments sharedinformers.ControllerInitArg

// Reconcile handles enqueued messages. The delete will be handled by finalizer.
func (c *ClusterControllerImpl) Reconcile(cluster *clusterv1.Cluster) error {
name := cluster.Name
// Deep-copy otherwise we are mutating our cache.
clusterCopy := cluster.DeepCopy()
name := clusterCopy.Name
glog.Infof("Running reconcile Cluster for %s\n", name)

if !cluster.ObjectMeta.DeletionTimestamp.IsZero() {
if !clusterCopy.ObjectMeta.DeletionTimestamp.IsZero() {
// no-op if finalizer has been removed.
if !util.Contains(cluster.ObjectMeta.Finalizers, clusterv1.ClusterFinalizer) {
if !util.Contains(clusterCopy.ObjectMeta.Finalizers, clusterv1.ClusterFinalizer) {
glog.Infof("reconciling cluster object %v causes a no-op as there is no finalizer.", name)
return nil
}

glog.Infof("reconciling cluster object %v triggers delete.", name)
if err := c.actuator.Delete(cluster); err != nil {
if err := c.actuator.Delete(clusterCopy); err != nil {
glog.Errorf("Error deleting cluster object %v; %v", name, err)
return err
}
// Remove finalizer on successful deletion.
glog.Infof("cluster object %v deletion successful, removing finalizer.", name)
cluster.ObjectMeta.Finalizers = util.Filter(cluster.ObjectMeta.Finalizers, clusterv1.ClusterFinalizer)
if _, err := c.clientSet.ClusterV1alpha1().Clusters(cluster.Namespace).Update(cluster); err != nil {
clusterCopy.ObjectMeta.Finalizers = util.Filter(clusterCopy.ObjectMeta.Finalizers, clusterv1.ClusterFinalizer)
if _, err := c.clientSet.ClusterV1alpha1().Clusters(clusterCopy.Namespace).Update(clusterCopy); err != nil {
glog.Errorf("Error removing finalizer from cluster object %v; %v", name, err)
return err
}
return nil
}

glog.Infof("reconciling cluster object %v triggers idempotent reconcile.", name)
err := c.actuator.Reconcile(cluster)
err := c.actuator.Reconcile(clusterCopy)
if err != nil {
glog.Errorf("Error reconciling cluster object %v; %v", name, err)
return err
Expand Down
9 changes: 8 additions & 1 deletion pkg/controller/cluster/reconcile_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,14 @@ func TestClusterSetControllerReconcileHandler(t *testing.T) {
t.Fatal(err)
}

finalizerRemoved := clusterUpdated && !util.Contains(clusterToTest.ObjectMeta.Finalizers, v1alpha1.ClusterFinalizer)
finalizerRemoved := false
if clusterUpdated {
updatedCluster, err := fakeClient.ClusterV1alpha1().Clusters(clusterToTest.Namespace).Get(clusterToTest.Name, metav1.GetOptions{})
if err != nil {
t.Fatalf("failed to get updated cluster.")
}
finalizerRemoved = !util.Contains(updatedCluster.ObjectMeta.Finalizers, v1alpha1.ClusterFinalizer)
}

if finalizerRemoved != test.expectFinalizerRemoved {
t.Errorf("Got finalizer removed %v, expected finalizer removed %v", finalizerRemoved, test.expectFinalizerRemoved)
Expand Down