@@ -7,11 +7,15 @@ import (
7
7
"context"
8
8
"encoding/json"
9
9
"fmt"
10
+ "io/ioutil"
10
11
"time"
11
12
12
- "k8s.io/apimachinery/pkg/types"
13
-
13
+ "github.com/pkg/errors"
14
14
"gitlab.com/chuckh/cluster-api-provider-kind/kind/actions"
15
+ v1 "k8s.io/api/core/v1"
16
+ metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
17
+ "k8s.io/apimachinery/pkg/types"
18
+ corev1 "k8s.io/client-go/kubernetes/typed/core/v1"
15
19
clusterv1 "sigs.k8s.io/cluster-api/pkg/apis/cluster/v1alpha1"
16
20
"sigs.k8s.io/cluster-api/pkg/client/clientset_generated/clientset/typed/cluster/v1alpha1"
17
21
capierror "sigs.k8s.io/cluster-api/pkg/controller/error"
@@ -22,14 +26,14 @@ import (
22
26
)
23
27
24
28
type Machine struct {
25
- ClusterAPI v1alpha1. ClusterV1alpha1Interface
26
- KubeconfigsDir string
29
+ Core corev1. CoreV1Interface
30
+ ClusterAPI v1alpha1. ClusterV1alpha1Interface
27
31
}
28
32
29
- func NewMachineActuator (kubeconfigs string , clusterapi v1alpha1.ClusterV1alpha1Interface ) * Machine {
33
+ func NewMachineActuator (clusterapi v1alpha1.ClusterV1alpha1Interface , core corev1. CoreV1Interface ) * Machine {
30
34
return & Machine {
31
- ClusterAPI : clusterapi ,
32
- KubeconfigsDir : kubeconfigs ,
35
+ Core : core ,
36
+ ClusterAPI : clusterapi ,
33
37
}
34
38
}
35
39
@@ -75,7 +79,21 @@ func (m *Machine) Create(ctx context.Context, c *clusterv1.Cluster, machine *clu
75
79
return err
76
80
}
77
81
setKindName (machine , controlPlaneNode .Name ())
78
- return m .save (old , machine )
82
+ if err := m .save (old , machine ); err != nil {
83
+ fmt .Printf ("%+v" , err )
84
+ return err
85
+ }
86
+ s , err := kubeconfigToSecret (c .Name , c .Namespace )
87
+ if err != nil {
88
+ fmt .Printf ("%+v" , err )
89
+ return err
90
+ }
91
+ // Save the secret to the management cluster
92
+ if _ , err := m .Core .Secrets (machine .GetNamespace ()).Create (s ); err != nil {
93
+ fmt .Printf ("%+v" , err )
94
+ return err
95
+ }
96
+ return nil
79
97
}
80
98
81
99
// If there are no control plane then we should hold off on joining workers
@@ -103,14 +121,22 @@ func (m *Machine) Update(ctx context.Context, cluster *clusterv1.Cluster, machin
103
121
}
104
122
105
123
func (m * Machine ) Exists (ctx context.Context , cluster * clusterv1.Cluster , machine * clusterv1.Machine ) (bool , error ) {
124
+ if getKindName (machine ) == "" {
125
+ return false , nil
126
+ }
106
127
fmt .Println ("Looking for a docker container named" , getKindName (machine ))
107
128
role := getRole (machine )
108
- nodeList , err := nodes .List (fmt .Sprintf ("label=%s=%s" , constants .NodeRoleKey , role ),
129
+ labels := []string {
130
+ fmt .Sprintf ("label=%s=%s" , constants .NodeRoleKey , role ),
109
131
fmt .Sprintf ("label=%s=%s" , constants .ClusterLabelKey , cluster .Name ),
110
- fmt .Sprintf ("name=%s" , getKindName (machine )))
132
+ fmt .Sprintf ("name=^%s$" , getKindName (machine )),
133
+ }
134
+ fmt .Printf ("using labels: %v\n " , labels )
135
+ nodeList , err := nodes .List (labels ... )
111
136
if err != nil {
112
- return true , err
137
+ return false , err
113
138
}
139
+ fmt .Printf ("found nodes: %v\n " , nodeList )
114
140
return len (nodeList ) >= 1 , nil
115
141
}
116
142
@@ -138,7 +164,9 @@ func (m *Machine) save(old, new *clusterv1.Machine) error {
138
164
}
139
165
140
166
func setKindName (machine * clusterv1.Machine , name string ) {
141
- machine .SetAnnotations (map [string ]string {"name" : name })
167
+ a := machine .GetAnnotations ()
168
+ a ["name" ] = name
169
+ machine .SetAnnotations (a )
142
170
}
143
171
144
172
func getKindName (machine * clusterv1.Machine ) string {
@@ -185,3 +213,22 @@ func (c *Cluster) Delete(cluster *clusterv1.Cluster) error {
185
213
fmt .Println ("Cluster delete is not implemented." )
186
214
return nil
187
215
}
216
+
217
+ func kubeconfigToSecret (clusterName , namespace string ) (* v1.Secret , error ) {
218
+ // open kubeconfig file
219
+ data , err := ioutil .ReadFile (actions .KubeConfigPath (clusterName ))
220
+ if err != nil {
221
+ return nil , errors .WithStack (err )
222
+ }
223
+
224
+ // write it to a secret
225
+ return & v1.Secret {
226
+ ObjectMeta : metav1.ObjectMeta {
227
+ Name : fmt .Sprintf ("kubeconfig-%s" , clusterName ),
228
+ Namespace : namespace ,
229
+ },
230
+ Data : map [string ][]byte {
231
+ "kubeconfig" : data ,
232
+ },
233
+ }, nil
234
+ }
0 commit comments