Skip to content

Commit

Permalink
controllers: fix an idempotency bug in the portforward manager (#4771)
Browse files Browse the repository at this point in the history
  • Loading branch information
nicks committed Jul 19, 2021
1 parent 7288cb6 commit 59cbf06
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 1 deletion.
Expand Up @@ -40,7 +40,7 @@ func (r *Reconciler) manageOwnedPortForwards(ctx context.Context, nn types.Names
foundDesired = true

// If this PortForward is already in the APIServer, make sure it's up-to-date.
if apicmp.DeepEqual(pf.Spec, existingPF) {
if apicmp.DeepEqual(pf.Spec, existingPF.Spec) {
continue
}

Expand Down
41 changes: 41 additions & 0 deletions internal/controllers/core/kubernetesdiscovery/portforwards_test.go
Expand Up @@ -59,6 +59,47 @@ func TestPortForwardCreateAndUpdate(t *testing.T) {
assert.Equal(t, 4001, int(pf.Spec.Forwards[0].LocalPort))
}

func TestPortForwardIdempotent(t *testing.T) {
f := newFixture(t)

pod := f.buildPod("pod-ns", "pod", nil, nil)
key := types.NamespacedName{Name: "kd"}
kd := &v1alpha1.KubernetesDiscovery{
ObjectMeta: metav1.ObjectMeta{Name: "kd"},
Spec: v1alpha1.KubernetesDiscoverySpec{
Watches: []v1alpha1.KubernetesWatchRef{
{
UID: string(pod.UID),
Namespace: pod.Namespace,
Name: pod.Name,
},
},
PortForwardTemplateSpec: &v1alpha1.PortForwardTemplateSpec{
Forwards: []v1alpha1.Forward{
v1alpha1.Forward{LocalPort: 4000, ContainerPort: 4000},
},
},
},
}

f.Create(kd)
f.kClient.UpsertPod(pod)
f.requireObservedPods(key, ancestorMap{pod.UID: pod.UID})

// Simulate the reconcile (which would normally be invoked by the manager on status update).
f.MustReconcile(key)

var pf1 v1alpha1.PortForward
f.MustGet(types.NamespacedName{Name: "kd-pod"}, &pf1)

f.MustReconcile(key)

var pf2 v1alpha1.PortForward
f.MustGet(types.NamespacedName{Name: "kd-pod"}, &pf2)

assert.Equal(t, pf1.ObjectMeta, pf2.ObjectMeta)
}

func TestPortForwardCreateAndDelete(t *testing.T) {
f := newFixture(t)

Expand Down

0 comments on commit 59cbf06

Please sign in to comment.