Skip to content
This repository was archived by the owner on Jul 30, 2021. It is now read-only.

Commit 9529e84

Browse files
committed
move control plane locker from CAPA
1 parent db30c35 commit 9529e84

File tree

2 files changed

+243
-0
lines changed

2 files changed

+243
-0
lines changed
Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
/*
2+
Copyright 2019 The Kubernetes Authors.
3+
Licensed under the Apache License, Version 2.0 (the "License");
4+
you may not use this file except in compliance with the License.
5+
You may obtain a copy of the License at
6+
http://www.apache.org/licenses/LICENSE-2.0
7+
Unless required by applicable law or agreed to in writing, software
8+
distributed under the License is distributed on an "AS IS" BASIS,
9+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
10+
See the License for the specific language governing permissions and
11+
limitations under the License.
12+
*/
13+
14+
package controllers
15+
16+
import (
17+
"fmt"
18+
19+
"github.com/go-logr/logr"
20+
apicorev1 "k8s.io/api/core/v1"
21+
apierrors "k8s.io/apimachinery/pkg/api/errors"
22+
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
23+
corev1 "k8s.io/client-go/kubernetes/typed/core/v1"
24+
clusterv2 "sigs.k8s.io/cluster-api/pkg/apis/cluster/v1alpha2"
25+
)
26+
27+
// ControlPlaneInitLocker provides a locking mechanism for cluster initialization.
28+
type ControlPlaneInitLocker interface {
29+
// Acquire returns true if it acquires the lock for the cluster.
30+
Acquire(cluster *clusterv2.Cluster) bool
31+
}
32+
33+
// controlPlaneInitLocker uses a ConfigMap to synchronize cluster initialization.
34+
type controlPlaneInitLocker struct {
35+
log logr.Logger
36+
configMapClient corev1.ConfigMapsGetter
37+
}
38+
39+
var _ ControlPlaneInitLocker = &controlPlaneInitLocker{}
40+
41+
func newControlPlaneInitLocker(log logr.Logger, configMapClient corev1.ConfigMapsGetter) *controlPlaneInitLocker {
42+
return &controlPlaneInitLocker{
43+
log: log,
44+
configMapClient: configMapClient,
45+
}
46+
}
47+
48+
func (l *controlPlaneInitLocker) Acquire(cluster *clusterv2.Cluster) bool {
49+
configMapName := fmt.Sprintf("%s-controlplane", cluster.UID)
50+
log := l.log.WithValues("namespace", cluster.Namespace, "cluster-name", cluster.Name, "configmap-name", configMapName)
51+
52+
exists, err := l.configMapExists(cluster.Namespace, configMapName)
53+
if err != nil {
54+
log.Error(err, "Error checking for control plane configmap lock existence")
55+
return false
56+
}
57+
if exists {
58+
return false
59+
}
60+
61+
controlPlaneConfigMap := &apicorev1.ConfigMap{
62+
ObjectMeta: metav1.ObjectMeta{
63+
Namespace: cluster.Namespace,
64+
Name: configMapName,
65+
OwnerReferences: []metav1.OwnerReference{
66+
{
67+
APIVersion: cluster.APIVersion,
68+
Kind: cluster.Kind,
69+
Name: cluster.Name,
70+
UID: cluster.UID,
71+
},
72+
},
73+
},
74+
}
75+
76+
log.Info("Attempting to create control plane configmap lock")
77+
_, err = l.configMapClient.ConfigMaps(cluster.Namespace).Create(controlPlaneConfigMap)
78+
if err != nil {
79+
if apierrors.IsAlreadyExists(err) {
80+
// Someone else beat us to it
81+
log.Info("Control plane configmap lock already exists")
82+
} else {
83+
log.Error(err, "Error creating control plane configmap lock")
84+
}
85+
86+
// Unable to acquire
87+
return false
88+
}
89+
90+
// Successfully acquired
91+
return true
92+
}
93+
94+
func (l *controlPlaneInitLocker) configMapExists(namespace, name string) (bool, error) {
95+
_, err := l.configMapClient.ConfigMaps(namespace).Get(name, metav1.GetOptions{})
96+
if apierrors.IsNotFound(err) {
97+
return false, nil
98+
}
99+
100+
return err == nil, err
101+
}
Lines changed: 142 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,142 @@
1+
/*
2+
Copyright 2019 The Kubernetes Authors.
3+
Licensed under the Apache License, Version 2.0 (the "License");
4+
you may not use this file except in compliance with the License.
5+
You may obtain a copy of the License at
6+
http://www.apache.org/licenses/LICENSE-2.0
7+
Unless required by applicable law or agreed to in writing, software
8+
distributed under the License is distributed on an "AS IS" BASIS,
9+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
10+
See the License for the specific language governing permissions and
11+
limitations under the License.
12+
*/
13+
14+
package controllers
15+
16+
import (
17+
"testing"
18+
19+
"github.com/pkg/errors"
20+
v1 "k8s.io/api/core/v1"
21+
apierrors "k8s.io/apimachinery/pkg/api/errors"
22+
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
23+
"k8s.io/apimachinery/pkg/runtime/schema"
24+
"k8s.io/apimachinery/pkg/types"
25+
"k8s.io/apimachinery/pkg/watch"
26+
corev1client "k8s.io/client-go/kubernetes/typed/core/v1"
27+
clusterv2 "sigs.k8s.io/cluster-api/pkg/apis/cluster/v1alpha2"
28+
"sigs.k8s.io/controller-runtime/pkg/runtime/log"
29+
)
30+
31+
func TestControlPlaneInitLockerAcquire(t *testing.T) {
32+
tests := []struct {
33+
name string
34+
configMap *v1.ConfigMap
35+
getError error
36+
createError error
37+
expectAcquire bool
38+
}{
39+
{
40+
name: "configmap already exists",
41+
configMap: &v1.ConfigMap{},
42+
expectAcquire: false,
43+
},
44+
{
45+
name: "error getting configmap",
46+
getError: errors.New("get error"),
47+
expectAcquire: false,
48+
},
49+
{
50+
name: "create succeeds",
51+
getError: apierrors.NewNotFound(schema.GroupResource{Group: "", Resource: "configmaps"}, "uid1-configmap"),
52+
expectAcquire: true,
53+
},
54+
{
55+
name: "create fails",
56+
getError: apierrors.NewNotFound(schema.GroupResource{Group: "", Resource: "configmaps"}, "uid1-configmap"),
57+
createError: errors.New("create error"),
58+
expectAcquire: false,
59+
},
60+
}
61+
62+
for _, tc := range tests {
63+
t.Run(tc.name, func(t *testing.T) {
64+
l := &controlPlaneInitLocker{
65+
log: log.ZapLogger(true),
66+
configMapClient: &configMapsGetter{
67+
configMap: tc.configMap,
68+
getError: tc.getError,
69+
createError: tc.createError,
70+
},
71+
}
72+
73+
cluster := &clusterv2.Cluster{
74+
ObjectMeta: metav1.ObjectMeta{
75+
Namespace: "ns1",
76+
Name: "name1",
77+
UID: types.UID("uid1"),
78+
},
79+
}
80+
81+
acquired := l.Acquire(cluster)
82+
if tc.expectAcquire != acquired {
83+
t.Errorf("expected %t, got %t", tc.expectAcquire, acquired)
84+
}
85+
})
86+
}
87+
}
88+
89+
type configMapsGetter struct {
90+
configMap *v1.ConfigMap
91+
getError error
92+
createError error
93+
}
94+
95+
func (c *configMapsGetter) ConfigMaps(namespace string) corev1client.ConfigMapInterface {
96+
return &configMapClient{
97+
configMap: c.configMap,
98+
getError: c.getError,
99+
createError: c.createError,
100+
}
101+
}
102+
103+
type configMapClient struct {
104+
configMap *v1.ConfigMap
105+
getError error
106+
createError error
107+
}
108+
109+
func (c *configMapClient) Create(configMap *v1.ConfigMap) (*v1.ConfigMap, error) {
110+
return c.configMap, c.createError
111+
}
112+
113+
func (c *configMapClient) Get(name string, getOptions metav1.GetOptions) (*v1.ConfigMap, error) {
114+
if c.getError != nil {
115+
return nil, c.getError
116+
}
117+
return c.configMap, nil
118+
}
119+
120+
func (c *configMapClient) Update(*v1.ConfigMap) (*v1.ConfigMap, error) {
121+
panic("not implemented")
122+
}
123+
124+
func (c *configMapClient) Delete(name string, options *metav1.DeleteOptions) error {
125+
panic("not implemented")
126+
}
127+
128+
func (c *configMapClient) DeleteCollection(options *metav1.DeleteOptions, listOptions metav1.ListOptions) error {
129+
panic("not implemented")
130+
}
131+
132+
func (c *configMapClient) List(opts metav1.ListOptions) (*v1.ConfigMapList, error) {
133+
panic("not implemented")
134+
}
135+
136+
func (c *configMapClient) Watch(opts metav1.ListOptions) (watch.Interface, error) {
137+
panic("not implemented")
138+
}
139+
140+
func (c *configMapClient) Patch(name string, pt types.PatchType, data []byte, subresources ...string) (result *v1.ConfigMap, err error) {
141+
panic("not implemented")
142+
}

0 commit comments

Comments
 (0)