@@ -22,6 +22,8 @@ import (
22
22
"encoding/base64"
23
23
"fmt"
24
24
"io/ioutil"
25
+ "k8s.io/apimachinery/pkg/api/errors"
26
+ metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
25
27
"os"
26
28
"path/filepath"
27
29
"strconv"
@@ -31,8 +33,6 @@ import (
31
33
"github.com/SparebankenVest/azure-key-vault-to-kubernetes/pkg/docker/registry"
32
34
"github.com/spf13/viper"
33
35
corev1 "k8s.io/api/core/v1"
34
- "k8s.io/apimachinery/pkg/api/errors"
35
- metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
36
36
"k8s.io/apimachinery/pkg/types"
37
37
"k8s.io/client-go/kubernetes"
38
38
"k8s.io/klog/v2"
@@ -201,18 +201,6 @@ func (p podWebHook) mutateContainers(ctx context.Context, containers []corev1.Co
201
201
}... )
202
202
203
203
if useAuthService {
204
- _ , err := p .clientset .CoreV1 ().Secrets (p .namespace ).Create (context .TODO (), authServiceSecret , metav1.CreateOptions {})
205
- if err != nil {
206
- if errors .IsAlreadyExists (err ) {
207
- _ , err = p .clientset .CoreV1 ().Secrets (p .namespace ).Update (context .TODO (), authServiceSecret , metav1.UpdateOptions {})
208
- if err != nil {
209
- return false , err
210
- }
211
- } else {
212
- return false , err
213
- }
214
- }
215
-
216
204
container .VolumeMounts = append (container .VolumeMounts , []corev1.VolumeMount {
217
205
{
218
206
Name : authSecretVolumeName ,
@@ -289,6 +277,21 @@ func (p podWebHook) mutatePodSpec(ctx context.Context, pod *corev1.Pod) error {
289
277
}
290
278
}
291
279
280
+ if p .useAuthService && (len (podSpec .InitContainers ) > 0 || len (podSpec .Containers ) > 0 ) {
281
+ klog .InfoS ("create authentication service secret" , klog .KRef (p .namespace , pod .Name ))
282
+ _ , err := p .clientset .CoreV1 ().Secrets (p .namespace ).Create (context .TODO (), authServiceSecret , metav1.CreateOptions {})
283
+ if err != nil {
284
+ if errors .IsAlreadyExists (err ) {
285
+ _ , err = p .clientset .CoreV1 ().Secrets (p .namespace ).Update (context .TODO (), authServiceSecret , metav1.UpdateOptions {})
286
+ if err != nil {
287
+ return err
288
+ }
289
+ } else {
290
+ return err
291
+ }
292
+ }
293
+ }
294
+
292
295
klog .InfoS ("mutate init-containers" , klog .KRef (p .namespace , pod .Name ))
293
296
initContainersMutated , err := p .mutateContainers (ctx , podSpec .InitContainers , podSpec , authServiceSecret )
294
297
if err != nil {
0 commit comments