1
1
package pods
2
2
3
3
import (
4
+ "context"
4
5
"fmt"
6
+ "github.com/pkg/errors"
7
+ "sigs.k8s.io/controller-runtime/pkg/client"
5
8
"sort"
6
9
"strings"
7
10
@@ -25,7 +28,7 @@ const (
25
28
HostsRewriteContainerName = "vcluster-rewrite-hosts"
26
29
)
27
30
28
- func translatePod (pPod * corev1.Pod , vPod * corev1.Pod , services []* corev1.Service , clusterDomain , dnsIP , kubeIP , serviceAccount string , translator ImageTranslator , enableOverrideHosts bool , overrideHostsImage string ) error {
31
+ func translatePod (pPod * corev1.Pod , vPod * corev1.Pod , vClient client. Client , services []* corev1.Service , clusterDomain , dnsIP , kubeIP , serviceAccount string , translator ImageTranslator , enableOverrideHosts bool , overrideHostsImage string ) error {
29
32
pPod .Status = corev1.PodStatus {}
30
33
pPod .Spec .DeprecatedServiceAccount = ""
31
34
pPod .Spec .ServiceAccountName = serviceAccount
@@ -188,6 +191,13 @@ func translatePod(pPod *corev1.Pod, vPod *corev1.Pod, services []*corev1.Service
188
191
if pPod .Spec .Volumes [i ].PersistentVolumeClaim != nil {
189
192
pPod .Spec .Volumes [i ].PersistentVolumeClaim .ClaimName = translate .PhysicalName (pPod .Spec .Volumes [i ].PersistentVolumeClaim .ClaimName , vPod .Namespace )
190
193
}
194
+ if pPod .Spec .Volumes [i ].Projected != nil {
195
+ // get old service account name
196
+ err := translateProjectedVolume (pPod .Spec .Volumes [i ].Projected , vClient , vPod )
197
+ if err != nil {
198
+ return err
199
+ }
200
+ }
191
201
}
192
202
193
203
// we add an annotation if the pod has a replica set or statefulset owner
@@ -205,6 +215,85 @@ func translatePod(pPod *corev1.Pod, vPod *corev1.Pod, services []*corev1.Service
205
215
return nil
206
216
}
207
217
218
+ func secretNameFromServiceAccount (vClient client.Client , vPod * corev1.Pod ) (string , error ) {
219
+ vServiceAccount := ""
220
+ if vPod .Spec .ServiceAccountName != "" {
221
+ vServiceAccount = vPod .Spec .ServiceAccountName
222
+ } else if vPod .Spec .DeprecatedServiceAccount != "" {
223
+ vServiceAccount = vPod .Spec .DeprecatedServiceAccount
224
+ }
225
+
226
+ secretList := & corev1.SecretList {}
227
+ err := vClient .List (context .Background (), secretList , client .InNamespace (vPod .Namespace ))
228
+ if err != nil {
229
+ return "" , errors .Wrap (err , "list secrets in " + vPod .Namespace )
230
+ }
231
+ for _ , secret := range secretList .Items {
232
+ if secret .Annotations ["kubernetes.io/service-account.name" ] == vServiceAccount {
233
+ return secret .Name , nil
234
+ }
235
+ }
236
+
237
+ return "" , nil
238
+ }
239
+
240
+ func translateProjectedVolume (projectedVolume * corev1.ProjectedVolumeSource , vClient client.Client , vPod * corev1.Pod ) error {
241
+ for i := range projectedVolume .Sources {
242
+ if projectedVolume .Sources [i ].Secret != nil {
243
+ projectedVolume .Sources [i ].Secret .Name = translate .PhysicalName (projectedVolume .Sources [i ].Secret .Name , vPod .Namespace )
244
+ }
245
+ if projectedVolume .Sources [i ].ConfigMap != nil {
246
+ projectedVolume .Sources [i ].ConfigMap .Name = translate .PhysicalName (projectedVolume .Sources [i ].ConfigMap .Name , vPod .Namespace )
247
+ }
248
+ if projectedVolume .Sources [i ].DownwardAPI != nil {
249
+ for j := range projectedVolume .Sources [i ].DownwardAPI .Items {
250
+ translateFieldRef (projectedVolume .Sources [i ].DownwardAPI .Items [j ].FieldRef )
251
+ }
252
+ }
253
+ if projectedVolume .Sources [i ].ServiceAccountToken != nil {
254
+ secretName , err := secretNameFromServiceAccount (vClient , vPod )
255
+ if err != nil {
256
+ return err
257
+ } else if secretName == "" {
258
+ return fmt .Errorf ("couldn't find service account secret for pod %s/%s" , vPod .Namespace , vPod .Name )
259
+ }
260
+
261
+ allRights := int32 (0644 )
262
+ projectedVolume .Sources [i ].Secret = & corev1.SecretProjection {
263
+ LocalObjectReference : corev1.LocalObjectReference {
264
+ Name : translate .PhysicalName (secretName , vPod .Namespace ),
265
+ },
266
+ Items : []corev1.KeyToPath {
267
+ {
268
+ Path : projectedVolume .Sources [i ].ServiceAccountToken .Path ,
269
+ Key : "token" ,
270
+ Mode : & allRights ,
271
+ },
272
+ },
273
+ }
274
+ projectedVolume .Sources [i ].ServiceAccountToken = nil
275
+ }
276
+ }
277
+
278
+ return nil
279
+ }
280
+
281
+ func translateFieldRef (fieldSelector * corev1.ObjectFieldSelector ) {
282
+ if fieldSelector == nil {
283
+ return
284
+ }
285
+ switch fieldSelector .FieldPath {
286
+ case "metadata.name" :
287
+ fieldSelector .FieldPath = "metadata.annotations['" + NameAnnotation + "']"
288
+ case "metadata.namespace" :
289
+ fieldSelector .FieldPath = "metadata.annotations['" + NamespaceAnnotation + "']"
290
+ case "metadata.uid" :
291
+ fieldSelector .FieldPath = "metadata.annotations['" + UIDAnnotation + "']"
292
+ case "spec.serviceAccountName" :
293
+ fieldSelector .FieldPath = "metadata.annotations['" + ServiceAccountNameAnnotation + "']"
294
+ }
295
+ }
296
+
208
297
func stripHostRewriteContainer (pPod * corev1.Pod ) * corev1.Pod {
209
298
if pPod .Annotations == nil || pPod .Annotations [HostsRewrittenAnnotation ] != "true" {
210
299
return pPod
0 commit comments