@@ -23,6 +23,7 @@ import (
23
23
"time"
24
24
25
25
"github.com/chuckha/cluster-api-provider-docker/kind/actions"
26
+ apicorev1 "k8s.io/api/core/v1"
26
27
"k8s.io/apimachinery/pkg/types"
27
28
corev1 "k8s.io/client-go/kubernetes/typed/core/v1"
28
29
clusterv1 "sigs.k8s.io/cluster-api/pkg/apis/cluster/v1alpha1"
@@ -78,14 +79,19 @@ func (m *Machine) Create(ctx context.Context, c *clusterv1.Cluster, machine *clu
78
79
if setValue == clusterAPIControlPlaneSetLabel {
79
80
if len (controlPlanes ) > 0 {
80
81
fmt .Println ("Adding a control plane" )
81
- controlPlaneNode , err := actions .AddControlPlane (c .Name , machine .Spec .Versions .ControlPlane )
82
+ controlPlaneNode , err := actions .AddControlPlane (c .Name , machine .GetName (), machine . Spec .Versions .ControlPlane )
82
83
if err != nil {
83
84
fmt .Printf ("%+v" , err )
84
85
return err
85
86
}
86
- name := providerID (controlPlaneNode .Name ())
87
- machine .Spec .ProviderID = & name
88
- return m .save (old , machine )
87
+ nodeUID , err := actions .GetNodeRefUID (c .GetName (), controlPlaneNode .Name ())
88
+ if err != nil {
89
+ fmt .Printf ("%+v" , err )
90
+ return err
91
+ }
92
+ providerID := providerID (controlPlaneNode .Name ())
93
+ machine .Spec .ProviderID = & providerID
94
+ return m .save (old , machine , getNodeRef (controlPlaneNode .Name (), nodeUID ))
89
95
}
90
96
91
97
fmt .Println ("Creating a brand new cluster" )
@@ -99,16 +105,20 @@ func (m *Machine) Create(ctx context.Context, c *clusterv1.Cluster, machine *clu
99
105
fmt .Printf ("%+v\n " , err )
100
106
return err
101
107
}
102
- controlPlaneNode , err := actions .CreateControlPlane (c .Name , lbip , machine .Spec .Versions .ControlPlane )
108
+ controlPlaneNode , err := actions .CreateControlPlane (c .Name , machine . GetName (), lbip , machine .Spec .Versions .ControlPlane )
103
109
if err != nil {
104
110
fmt .Printf ("%+v\n " , err )
105
111
return err
106
112
}
107
-
113
+ nodeUID , err := actions .GetNodeRefUID (c .GetName (), controlPlaneNode .Name ())
114
+ if err != nil {
115
+ fmt .Printf ("%+v" , err )
116
+ return err
117
+ }
108
118
// set the machine's providerID
109
- name := providerID (controlPlaneNode .Name ())
110
- machine .Spec .ProviderID = & name
111
- if err := m .save (old , machine ); err != nil {
119
+ providerID := providerID (controlPlaneNode .Name ())
120
+ machine .Spec .ProviderID = & providerID
121
+ if err := m .save (old , machine , getNodeRef ( controlPlaneNode . Name (), nodeUID ) ); err != nil {
112
122
fmt .Printf ("%+v\n " , err )
113
123
return err
114
124
}
@@ -132,18 +142,37 @@ func (m *Machine) Create(ctx context.Context, c *clusterv1.Cluster, machine *clu
132
142
}
133
143
134
144
fmt .Println ("Creating a new worker node" )
135
- worker , err := actions .AddWorker (c .Name , machine .Spec .Versions .Kubelet )
145
+ worker , err := actions .AddWorker (c .Name , machine .GetName (), machine . Spec .Versions .Kubelet )
136
146
if err != nil {
137
147
fmt .Printf ("%+v" , err )
138
148
return err
139
149
}
140
- name := providerID (worker .Name ())
141
- machine .Spec .ProviderID = & name
142
- return m .save (old , machine )
150
+ providerID := providerID (worker .Name ())
151
+ machine .Spec .ProviderID = & providerID
152
+ nodeUID , err := actions .GetNodeRefUID (c .GetName (), worker .Name ())
153
+ if err != nil {
154
+ fmt .Printf ("%+v" , err )
155
+ return err
156
+ }
157
+ return m .save (old , machine , getNodeRef (worker .Name (), nodeUID ))
143
158
}
144
159
160
+ // Delete returns nil when the machine no longer exists or when a successful delete has happened.
145
161
func (m * Machine ) Delete (ctx context.Context , cluster * clusterv1.Cluster , machine * clusterv1.Machine ) error {
146
- return actions .DeleteNode (cluster .Name , providerNameToLookupID (* machine .Spec .ProviderID ))
162
+ exists , err := m .Exists (ctx , cluster , machine )
163
+ if err != nil {
164
+ return err
165
+ }
166
+ if exists {
167
+ setValue := getRole (machine )
168
+ if setValue == clusterAPIControlPlaneSetLabel {
169
+ fmt .Printf ("Deleting a control plane: %q\n " , machine .GetName ())
170
+ return actions .DeleteControlPlane (cluster .Name , machine .GetName ())
171
+ }
172
+ fmt .Printf ("Deleting a worker: %q\n " , machine .GetName ())
173
+ return actions .DeleteWorker (cluster .Name , machine .GetName ())
174
+ }
175
+ return nil
147
176
}
148
177
149
178
func (m * Machine ) Update (ctx context.Context , cluster * clusterv1.Cluster , machine * clusterv1.Machine ) error {
@@ -152,16 +181,16 @@ func (m *Machine) Update(ctx context.Context, cluster *clusterv1.Cluster, machin
152
181
}
153
182
154
183
func (m * Machine ) Exists (ctx context.Context , cluster * clusterv1.Cluster , machine * clusterv1.Machine ) (bool , error ) {
155
- if machine .Spec .ProviderID = = nil {
156
- return false , nil
184
+ if machine .Spec .ProviderID ! = nil {
185
+ return true , nil
157
186
}
158
- fmt . Println ( "Looking for a docker container named" , providerNameToLookupID ( * machine . Spec . ProviderID ))
187
+
159
188
role := getRole (machine )
160
189
kindRole := CAPIroleToKindRole (role )
161
190
labels := []string {
162
191
fmt .Sprintf ("label=%s=%s" , constants .NodeRoleKey , kindRole ),
163
192
fmt .Sprintf ("label=%s=%s" , constants .ClusterLabelKey , cluster .Name ),
164
- fmt .Sprintf ("name=^%s$" , providerNameToLookupID ( * machine .Spec . ProviderID )),
193
+ fmt .Sprintf ("name=^%s$" , machine .GetName ( )),
165
194
}
166
195
fmt .Printf ("using labels: %v\n " , labels )
167
196
nodeList , err := nodes .List (labels ... )
@@ -172,7 +201,8 @@ func (m *Machine) Exists(ctx context.Context, cluster *clusterv1.Cluster, machin
172
201
return len (nodeList ) >= 1 , nil
173
202
}
174
203
175
- func (m * Machine ) save (old , new * clusterv1.Machine ) error {
204
+ // patches the object and saves the status.
205
+ func (m * Machine ) save (old , new * clusterv1.Machine , noderef * apicorev1.ObjectReference ) error {
176
206
fmt .Println ("updating machine" )
177
207
p , err := patch .NewJSONPatch (old , new )
178
208
if err != nil {
@@ -186,19 +216,22 @@ func (m *Machine) save(old, new *clusterv1.Machine) error {
186
216
fmt .Printf ("%+v\n " , err )
187
217
return err
188
218
}
189
- if _ , err := m .ClusterAPI .Machines (old .Namespace ).Patch (new .Name , types .JSONPatchType , pb ); err != nil {
219
+ new , err = m .ClusterAPI .Machines (old .Namespace ).Patch (new .Name , types .JSONPatchType , pb )
220
+ if err != nil {
190
221
fmt .Printf ("%+v\n " , err )
191
222
return err
192
223
}
193
224
fmt .Println ("updated machine" )
194
225
}
226
+ // set the noderef after so we don't try and patch it in during the first update
227
+ new .Status .NodeRef = noderef
228
+ if _ , err := m .ClusterAPI .Machines (old .Namespace ).UpdateStatus (new ); err != nil {
229
+ fmt .Printf ("%+v\n " , err )
230
+ return err
231
+ }
195
232
return nil
196
233
}
197
234
198
- func providerNameToLookupID (providerName string ) string {
199
- return providerName [len ("docker://" ):]
200
- }
201
-
202
235
func providerID (name string ) string {
203
236
return fmt .Sprintf ("docker://%s" , name )
204
237
}
@@ -210,3 +243,12 @@ func CAPIroleToKindRole(CAPIRole string) string {
210
243
}
211
244
return CAPIRole
212
245
}
246
+
247
+ func getNodeRef (name , uid string ) * apicorev1.ObjectReference {
248
+ return & apicorev1.ObjectReference {
249
+ Kind : "Node" ,
250
+ APIVersion : apicorev1 .SchemeGroupVersion .String (),
251
+ Name : name ,
252
+ UID : types .UID (uid ),
253
+ }
254
+ }
0 commit comments