-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathpods.go
174 lines (146 loc) · 4.52 KB
/
pods.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
/*
Copyright 2023 Lawrence Livermore National Security, LLC
(c.f. AUTHORS, NOTICE.LLNS, COPYING)
This is part of the Flux resource manager framework.
For details, see https://github.com/flux-framework.
SPDX-License-Identifier: Apache-2.0
*/
package controllers
import (
"context"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
ctrl "sigs.k8s.io/controller-runtime"
"k8s.io/apimachinery/pkg/api/errors"
"k8s.io/apimachinery/pkg/types"
api "github.com/flux-framework/flux-operator/api/v1alpha2"
)
const podLabelAppName = "app.kubernetes.io/name"
// Get labels for any pod in the cluster
func getPodLabels(cluster *api.MiniCluster) map[string]string {
if cluster.Spec.Pod.Labels == nil {
cluster.Spec.Pod.Labels = map[string]string{}
}
podLabels := cluster.Spec.Pod.Labels
podLabels["namespace"] = cluster.Namespace
podLabels[podLabelAppName] = cluster.Name
return podLabels
}
// ensure service containers are running, currently in one pod
func (r *MiniClusterReconciler) ensureServicePod(
ctx context.Context,
cluster *api.MiniCluster,
) (*corev1.Pod, ctrl.Result, error) {
// Look for an existing service container
existing, err := r.getExistingPod(ctx, cluster)
// Create a new job if it does not exist
if err != nil {
if errors.IsNotFound(err) {
pod, err := r.newServicePod(cluster)
if err != nil {
return existing, ctrl.Result{}, err
}
r.log.Info(
"✨ Creating a new MiniCluster Service Pod ✨",
"Namespace:", pod.Namespace,
"Name:", pod.Name,
)
err = r.New(ctx, pod)
if err != nil {
r.log.Error(
err,
"Failed to create new MiniCluster Service Pod",
"Namespace:", pod.Namespace,
"Name:", pod.Name,
)
return pod, ctrl.Result{}, err
}
// Successful - return and requeue
return pod, ctrl.Result{Requeue: true}, nil
} else if err != nil {
r.log.Error(err, "Failed to get MiniCluster Service Pod")
return existing, ctrl.Result{}, err
}
} else {
r.log.Info(
"🎉 Found existing MiniCluster Service Pod 🎉",
"Namespace:", existing.Namespace,
"Name:", existing.Name,
)
}
return existing, ctrl.Result{}, err
}
// getExistingPod gets an existing pod service
func (r *MiniClusterReconciler) getExistingPod(
ctx context.Context,
cluster *api.MiniCluster,
) (*corev1.Pod, error) {
existing := &corev1.Pod{}
err := r.Get(
ctx,
types.NamespacedName{
Name: cluster.Name + "-services",
Namespace: cluster.Namespace,
},
existing,
)
return existing, err
}
// newMiniCluster is used to create the MiniCluster Job
func (r *MiniClusterReconciler) newServicePod(
cluster *api.MiniCluster,
) (*corev1.Pod, error) {
setAsFQDN := false
podLabels := getPodLabels(cluster)
podServiceName := cluster.Name + "-services"
// service selector?
podLabels["job-name"] = cluster.Name
// Services can have existing volumes
existingVolumes := getExistingVolumes(cluster.ExistingServiceVolumes())
// This is an indexed-job
pod := &corev1.Pod{
ObjectMeta: metav1.ObjectMeta{
Name: podServiceName,
Namespace: cluster.Namespace,
Labels: podLabels,
Annotations: cluster.Spec.Pod.Annotations,
},
Spec: corev1.PodSpec{
// This is the headless service name
Subdomain: cluster.Spec.Network.HeadlessName,
Hostname: podServiceName,
SetHostnameAsFQDN: &setAsFQDN,
Volumes: existingVolumes,
ImagePullSecrets: getImagePullSecrets(cluster),
RestartPolicy: corev1.RestartPolicyOnFailure,
ServiceAccountName: cluster.Spec.Pod.ServiceAccountName,
AutomountServiceAccountToken: &cluster.Spec.Pod.AutomountServiceAccountToken,
NodeSelector: cluster.Spec.Pod.NodeSelector,
},
}
// Custom restart policy
if cluster.Spec.Pod.RestartPolicy != "" {
pod.Spec.RestartPolicy = corev1.RestartPolicy(cluster.Spec.Pod.RestartPolicy)
}
// Only add runClassName if defined
if cluster.Spec.Pod.RuntimeClassName != "" {
pod.Spec.RuntimeClassName = &cluster.Spec.Pod.RuntimeClassName
}
// Assemble existing volume mounts - they are added with getContainers
mounts := []corev1.VolumeMount{}
// Get containers for the service pods
// true boolean indicates this is a service container - do not generate
// a custom entrypoint, etc.
containers, err := getContainers(
cluster.Spec.Services,
podServiceName,
mounts,
true,
)
if err != nil {
return pod, err
}
pod.Spec.Containers = containers
ctrl.SetControllerReference(cluster, pod, r.Scheme)
return pod, err
}