@@ -18,14 +18,13 @@ package webhook
18
18
19
19
import (
20
20
"context"
21
- "encoding/json"
22
21
"fmt"
23
22
"net/http"
24
23
"os"
25
24
26
25
"github.com/pkg/errors"
26
+ jsonpatch "gomodules.xyz/jsonpatch/v2"
27
27
corev1 "k8s.io/api/core/v1"
28
- "k8s.io/apimachinery/pkg/api/resource"
29
28
ctrl "sigs.k8s.io/controller-runtime"
30
29
"sigs.k8s.io/controller-runtime/pkg/client"
31
30
"sigs.k8s.io/controller-runtime/pkg/webhook"
@@ -90,6 +89,23 @@ func (hook *initInjector) handleInner(ctx context.Context, req admission.Request
90
89
}
91
90
}
92
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
+
93
109
// For each migrator, inject an initContainer.
94
110
for _ , m := range migrators {
95
111
// 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
100
116
podIdx = i
101
117
}
102
118
}
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
+ },
111
131
},
112
132
},
113
133
}
114
134
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 )
121
136
}
122
137
123
- resp := admission .PatchResponseFromRaw ( req . Object . Raw , marshaledPod )
138
+ resp := admission .Patched ( "injecting init containers" , patches ... )
124
139
return & resp , nil
125
140
}
126
141
0 commit comments