@@ -18,14 +18,13 @@ package webhook
1818
1919import (
2020 "context"
21- "encoding/json"
2221 "fmt"
2322 "net/http"
2423 "os"
2524
2625 "github.com/pkg/errors"
26+ jsonpatch "gomodules.xyz/jsonpatch/v2"
2727 corev1 "k8s.io/api/core/v1"
28- "k8s.io/apimachinery/pkg/api/resource"
2928 ctrl "sigs.k8s.io/controller-runtime"
3029 "sigs.k8s.io/controller-runtime/pkg/client"
3130 "sigs.k8s.io/controller-runtime/pkg/webhook"
@@ -90,6 +89,23 @@ func (hook *initInjector) handleInner(ctx context.Context, req admission.Request
9089 }
9190 }
9291
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+
93109 // For each migrator, inject an initContainer.
94110 for _ , m := range migrators {
95111 // Look for the container named in the migrator and pull the image from that. If no container
@@ -100,27 +116,26 @@ func (hook *initInjector) handleInner(ctx context.Context, req admission.Request
100116 podIdx = i
101117 }
102118 }
103- initContainer := corev1.Container {
104- Name : fmt .Sprintf ("migrate-wait-%s" , m .Name ),
105- Image : os .Getenv ("WAITER_IMAGE" ),
106- Command : []string {"/waiter" , pod .Spec .Containers [podIdx ].Image , m .Namespace , m .Name , os .Getenv ("API_HOSTNAME" )},
107- Resources : corev1.ResourceRequirements {
108- Requests : corev1.ResourceList {
109- corev1 .ResourceMemory : resource .MustParse ("16M" ),
110- corev1 .ResourceCPU : resource .MustParse ("10m" ),
119+ patch := jsonpatch.JsonPatchOperation {
120+ Operation : "add" ,
121+ Path : "/spec/initContainers/-" ,
122+ Value : map [string ]interface {}{
123+ "name" : fmt .Sprintf ("migrate-wait-%s" , m .Name ),
124+ "image" : os .Getenv ("WAITER_IMAGE" ),
125+ "command" : []string {"/waiter" , pod .Spec .Containers [podIdx ].Image , m .Namespace , m .Name , os .Getenv ("API_HOSTNAME" )},
126+ "resources" : map [string ]interface {}{
127+ "requests" : map [string ]string {
128+ "memory" : "16M" ,
129+ "cpu" : "10m" ,
130+ },
111131 },
112132 },
113133 }
114134 log .Info ("Injecting init container" , "pod" , fmt .Sprintf ("%s/%s" , req .Namespace , req .Name ), "migrator" , fmt .Sprintf ("%s/%s" , m .Namespace , m .Name ))
115- pod .Spec .InitContainers = append (pod .Spec .InitContainers , initContainer )
116- }
117-
118- marshaledPod , err := json .Marshal (pod )
119- if err != nil {
120- return nil , errors .Wrap (err , "error encoding response object" )
135+ patches = append (patches , patch )
121136 }
122137
123- resp := admission .PatchResponseFromRaw ( req . Object . Raw , marshaledPod )
138+ resp := admission .Patched ( "injecting init containers" , patches ... )
124139 return & resp , nil
125140}
126141
0 commit comments