-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathextra.go
259 lines (227 loc) · 6.42 KB
/
extra.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
package controllers
// This file has extra (not used) functions that might be useful
// (and I didn't want to delete just yet)!
// - pod exec commands
// - mini cluster ingress
// - pod listing, and ips
// - persistent volume claims
import (
"bytes"
"context"
"fmt"
"sort"
"strings"
corev1 "k8s.io/api/core/v1"
networkv1 "k8s.io/api/networking/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/client-go/kubernetes"
"k8s.io/client-go/kubernetes/scheme"
"k8s.io/client-go/tools/remotecommand"
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/client"
api "github.com/flux-framework/flux-operator/api/v1alpha2"
)
// Use a global variable for now to quickly return when broker ready
var (
brokerIsReady = false
)
// podExec executes a command to a named pod
func (r *MiniClusterReconciler) podExec(
ctx context.Context,
pod corev1.Pod,
cluster *api.MiniCluster,
command []string,
) (string, error) {
// creates the clientset
clientset, err := kubernetes.NewForConfig(r.RESTConfig)
if err != nil {
r.log.Info("🦀 PodExec", "Error with Creating Client", err)
return "", err
}
// The flux runner has the same name as the namespace
var container corev1.Container
for _, contender := range pod.Spec.Containers {
if strings.HasPrefix(contender.Name, cluster.Name) {
container = contender
}
}
// Prepare request TODO this will need to target flux runner
req := clientset.CoreV1().RESTClient().Post().Resource("pods").
Name(pod.Name).
Namespace(pod.Namespace).
SubResource("exec").
VersionedParams(
&corev1.PodExecOptions{
Container: container.Name,
Command: command,
Stdin: false,
Stdout: true,
Stderr: true,
TTY: true,
},
scheme.ParameterCodec,
)
exec, err := remotecommand.NewSPDYExecutor(r.RESTConfig, "POST", req.URL())
if err != nil {
r.log.Info("🦀 PodExec", "Error with Remote Command", err)
return "", err
}
// Prepare buffers to stream to
var stdout, stderr bytes.Buffer
// Important! stdin must be none here so it isn't expecting our input
err = exec.Stream(remotecommand.StreamOptions{
Stdout: &stdout,
Stderr: &stderr,
})
if err != nil {
r.log.Info("🦀 PodExec", "Error with Stream", err)
return "", err
}
outMsg := stdout.String()
errMsg := stderr.String()
fmt.Printf("🦀 PodExec Output\n%s", outMsg)
if errMsg != "" {
fmt.Printf("🦀 PodExec Error\n%s", errMsg)
}
return outMsg, err
}
// getMiniClusterPods returns a sorted (by name) podlist in the MiniCluster
func (r *MiniClusterReconciler) getMiniClusterPods(
ctx context.Context,
cluster *api.MiniCluster,
) *corev1.PodList {
podList := &corev1.PodList{}
opts := []client.ListOption{
client.InNamespace(cluster.Namespace),
}
err := r.Client.List(ctx, podList, opts...)
if err != nil {
r.log.Error(err, "🌀 Error listing MiniCluster pods", "Name:", podList)
return podList
}
// Ensure they are consistently sorted by name
sort.Slice(podList.Items, func(i, j int) bool {
return podList.Items[i].Name < podList.Items[j].Name
})
return podList
}
// brokerIsReady determines if broker 0 is waiting for worker nodes
func (r *MiniClusterReconciler) brokerIsReady(
ctx context.Context,
cluster *api.MiniCluster,
) (bool, error) {
// Cut out quickly if we've already done this
if brokerIsReady {
return true, nil
}
brokerPrefix := fmt.Sprintf("%s-0", cluster.Name)
makeReadyCommand := []string{
"/bin/sh",
"-c",
"touch /etc/flux/ready",
}
// See if the file exists
command := []string{
"/bin/sh",
"-c",
"ls /etc/flux",
}
pods := r.getMiniClusterPods(ctx, cluster)
for _, pod := range pods.Items {
r.log.Info("🦀 Found Pod", "Pod Name", pod.Name)
if strings.HasPrefix(pod.Name, brokerPrefix) {
r.log.Info("🦀 Found Broker", "Pod Name", pod.Name)
out, err := r.podExec(ctx, pod, cluster, command)
// Right before the broker runs, it creates this file
if !strings.Contains(out, "ready") || err != nil {
return false, fmt.Errorf("broker is not ready")
}
r.log.Info("🦀 Broker Is Ready", "Pod Name", pod.Name)
// Is the broker ready? If yes, touch files to indicate others ready
for _, worker := range pods.Items {
// Don't exec to the broker, pods that aren't for the job, or the certificate generator pod!
if worker.Name == pod.Name || !strings.HasPrefix(worker.Name, cluster.Name) {
continue
}
_, err := r.podExec(ctx, worker, cluster, makeReadyCommand)
if err != nil {
return false, err
}
r.log.Info("🦀 Worker", "Flagged Ready", worker.Name)
}
brokerIsReady = true
return true, nil
}
}
return false, fmt.Errorf("broker is not ready")
}
// getMiniClusterIPS was used when we needed to write /etc/hosts and is no longer used
func (r *MiniClusterReconciler) getMiniClusterIPS(
ctx context.Context,
cluster *api.MiniCluster,
) map[string]string {
ips := map[string]string{}
for _, pod := range r.getMiniClusterPods(ctx, cluster).Items {
// Skip init pods
if strings.Contains(pod.Name, "init") {
continue
}
// The pod isn't ready!
if pod.Status.PodIP == "" {
continue
}
ips[pod.Name] = pod.Status.PodIP
}
return ips
}
// createMiniClusterIngress exposes the service for the minicluster
func (r *MiniClusterReconciler) createMiniClusterIngress(
ctx context.Context,
cluster *api.MiniCluster,
service *corev1.Service,
) error {
pathType := networkv1.PathTypePrefix
ingressBackend := networkv1.IngressBackend{
Service: &networkv1.IngressServiceBackend{
Name: service.Name,
Port: networkv1.ServiceBackendPort{
Number: service.Spec.Ports[0].NodePort,
},
},
}
ingress := &networkv1.Ingress{
ObjectMeta: metav1.ObjectMeta{
Name: service.Name,
Namespace: service.Namespace,
},
Spec: networkv1.IngressSpec{
DefaultBackend: &ingressBackend,
Rules: []networkv1.IngressRule{
{
IngressRuleValue: networkv1.IngressRuleValue{
HTTP: &networkv1.HTTPIngressRuleValue{
Paths: []networkv1.HTTPIngressPath{
{
PathType: &pathType,
Backend: ingressBackend,
Path: "/",
},
},
},
},
},
},
},
}
err := ctrl.SetControllerReference(cluster, ingress, r.Scheme)
if err != nil {
r.log.Error(err, "🔴 Create ingress", "Service", cluster.Spec.Network.HeadlessName)
return err
}
err = r.New(ctx, ingress)
if err != nil {
r.log.Error(err, "🔴 Create ingress", "Service", cluster.Spec.Network.HeadlessName)
return err
}
return nil
}