(ctx context.Context, req admission.Request)
| 60 | } |
| 61 | |
| 62 | func (hook *initInjector) handleInner(ctx context.Context, req admission.Request) (*admission.Response, error) { |
| 63 | log := ctrl.Log.WithName("webhooks").WithName("InitInjector") |
| 64 | // Get the incoming pod. |
| 65 | pod := &corev1.Pod{} |
| 66 | err := hook.decoder.Decode(req, pod) |
| 67 | if err != nil { |
| 68 | return nil, errors.Wrap(err, "error decoding request") |
| 69 | } |
| 70 | |
| 71 | // Check for the no-wait annotations. |
| 72 | ann, ok := pod.Annotations[NOWAIT_MIGRATOR_ANNOTATION] |
| 73 | if ok && ann == "true" { |
| 74 | resp := admission.Allowed("skipping migration wait due to annotation") |
| 75 | return &resp, nil |
| 76 | } |
| 77 | |
| 78 | // Find any Migrator objects that match this pod. |
| 79 | migrators, err := utils.ListMatchingMigrators(ctx, hook.Client, pod) |
| 80 | if err != nil { |
| 81 | return nil, errors.Wrap(err, "error listing matching migrators") |
| 82 | } |
| 83 | |
| 84 | // If we have no migrators, check if that's okay. |
| 85 | if len(migrators) == 0 && pod.Annotations != nil { |
| 86 | ann, ok := pod.Annotations[REQUIRE_MIGRATOR_ANNOTATION] |
| 87 | if ok && ann == "true" { |
| 88 | return nil, errors.New("no migrators found matching pod") |
| 89 | } |
| 90 | } |
| 91 | |
| 92 | if len(migrators) == 0 { |
| 93 | // Nothing to do. |
| 94 | resp := admission.Allowed("no migrators") |
| 95 | return &resp, nil |
| 96 | } |
| 97 | |
| 98 | patches := []jsonpatch.JsonPatchOperation{} |
| 99 | // Check that initContainers exists at all. |
| 100 | if len(pod.Spec.InitContainers) == 0 { |
| 101 | patch := jsonpatch.JsonPatchOperation{ |
| 102 | Operation: "add", |
| 103 | Path: "/spec/initContainers", |
| 104 | Value: []interface{}{}, |
| 105 | } |
| 106 | patches = append(patches, patch) |
| 107 | } |
| 108 | |
| 109 | // For each migrator, inject an initContainer. |
| 110 | for _, m := range migrators { |
| 111 | // Look for the container named in the migrator and pull the image from that. If no container |
| 112 | // matches, fall back to using the first container. |
| 113 | podIdx := 0 |
| 114 | for i, p := range pod.Spec.Containers { |
| 115 | if p.Name == m.Spec.Container { |
| 116 | podIdx = i |
| 117 | } |
| 118 | } |
| 119 | patch := jsonpatch.JsonPatchOperation{ |
no test coverage detected