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

🐛 Add unlock if bootstrap machine holding lock does not exist #5856

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
15 changes: 13 additions & 2 deletions bootstrap/kubeadm/internal/locking/control_plane_init_mutex.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,16 +62,27 @@ func (c *ControlPlaneInitMutex) Lock(ctx context.Context, cluster *clusterv1.Clu
case err != nil:
log.Error(err, "Failed to acquire lock")
return false
default: // successfully found an existing config map
default: // Successfully found an existing config map.
info, err := sema.information()
if err != nil {
log.Error(err, "Failed to get information about the existing lock")
return false
}
// the machine requesting the lock is the machine that created the lock, therefore the lock is acquired
// The machine requesting the lock is the machine that created the lock, therefore the lock is acquired.
if info.MachineName == machine.Name {
return true
}

// If the machine that created the lock can not be found unlock the mutex.
if err := c.client.Get(ctx, client.ObjectKey{
Namespace: cluster.Namespace,
Name: info.MachineName,
}, &clusterv1.Machine{}); err != nil {
log.Error(err, "Failed to get machine holding ControlPlane lock")
if apierrors.IsNotFound(err) {
c.Unlock(ctx, cluster)
}
}
log.Info("Waiting on another machine to initialize", "init-machine", info.MachineName)
return false
}
Expand Down
112 changes: 109 additions & 3 deletions bootstrap/kubeadm/internal/locking/control_plane_init_mutex_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,6 @@ func TestControlPlaneInitMutex_Lock(t *testing.T) {
g.Expect(corev1.AddToScheme(scheme)).To(Succeed())

uid := types.UID("test-uid")

tests := []struct {
name string
client client.Client
Expand Down Expand Up @@ -129,6 +128,95 @@ func TestControlPlaneInitMutex_Lock(t *testing.T) {
})
}
}

func TestControlPlaneInitMutex_LockWithMachineDeletion(t *testing.T) {
g := NewWithT(t)

scheme := runtime.NewScheme()
g.Expect(clusterv1.AddToScheme(scheme)).To(Succeed())
g.Expect(corev1.AddToScheme(scheme)).To(Succeed())

newMachineName := "new-machine"
tests := []struct {
name string
client client.Client
expectedMachineName string
}{
{
name: "should not give the lock to new machine if the machine that created it does exist",
client: &fakeClient{
Client: fake.NewClientBuilder().WithScheme(scheme).WithObjects(
&corev1.ConfigMap{
ObjectMeta: metav1.ObjectMeta{
Name: configMapName(clusterName),
Namespace: clusterNamespace},
Data: map[string]string{
"lock-information": "{\"machineName\":\"existent-machine\"}",
}},
&clusterv1.Machine{
ObjectMeta: metav1.ObjectMeta{
Name: "existent-machine",
Namespace: clusterNamespace,
},
},
).Build(),
},
expectedMachineName: "existent-machine",
},
{
name: "should give the lock to new machine if the machine that created it does not exist",
client: &fakeClient{
Client: fake.NewClientBuilder().WithScheme(scheme).WithObjects(
&corev1.ConfigMap{
ObjectMeta: metav1.ObjectMeta{
Name: configMapName(clusterName),
Namespace: clusterNamespace},
Data: map[string]string{
"lock-information": "{\"machineName\":\"non-existent-machine\"}",
}},
).Build(),
},
expectedMachineName: newMachineName,
},
}
for _, tc := range tests {
t.Run(tc.name, func(t *testing.T) {
l := &ControlPlaneInitMutex{
log: log.Log,
client: tc.client,
}

cluster := &clusterv1.Cluster{
ObjectMeta: metav1.ObjectMeta{
Namespace: clusterNamespace,
Name: clusterName,
},
}
machine := &clusterv1.Machine{
ObjectMeta: metav1.ObjectMeta{
Name: newMachineName,
},
}

g.Eventually(func(g Gomega) error {
l.Lock(ctx, cluster, machine)

cm := &corev1.ConfigMap{}
g.Expect(tc.client.Get(ctx, client.ObjectKey{
Name: configMapName(clusterName),
Namespace: cluster.Namespace,
}, cm)).To(Succeed())

info, err := semaphore{cm}.information()
g.Expect(err).To(BeNil())

g.Expect(info.MachineName).To(Equal(tc.expectedMachineName))
return nil
}, "20s").Should(Succeed())
})
}
}

func TestControlPlaneInitMutex_UnLock(t *testing.T) {
uid := types.UID("test-uid")
configMap := &corev1.ConfigMap{
Expand Down Expand Up @@ -217,7 +305,8 @@ func TestInfoLines_Lock(t *testing.T) {
}

logtester := &logtests{
InfoLog: make([]line, 0),
InfoLog: make([]line, 0),
ErrorLog: make([]line, 0),
}
l := &ControlPlaneInitMutex{
log: logtester,
Expand Down Expand Up @@ -281,7 +370,8 @@ func (fc *fakeClient) Delete(ctx context.Context, obj client.Object, opts ...cli

type logtests struct {
logr.Logger
InfoLog []line
InfoLog []line
ErrorLog []line
}

type line struct {
Expand All @@ -299,6 +389,22 @@ func (l *logtests) Info(msg string, keysAndValues ...interface{}) {
data: data,
})
}

func (l *logtests) Error(err error, msg string, keysAndValues ...interface{}) {
data := make(map[string]interface{})
for i := 0; i < len(keysAndValues); i += 2 {
data[keysAndValues[i].(string)] = keysAndValues[i+1]
}
l.ErrorLog = append(l.ErrorLog, line{
line: msg + err.Error(),
data: data,
})
}

func (l *logtests) WithValues(keysAndValues ...interface{}) logr.Logger {
return l
}

func (l *logtests) WithName(name string) logr.Logger {
return l
}