-
Notifications
You must be signed in to change notification settings - Fork 0
/
inject.go
880 lines (772 loc) · 27.5 KB
/
inject.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
// Copyright 2018 Istio Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package inject
import (
"bufio"
"bytes"
"crypto/sha256"
"encoding/hex"
"encoding/json"
"fmt"
"io"
"net"
"os"
"path"
"reflect"
"strconv"
"strings"
"text/template"
"github.com/ghodss/yaml"
"github.com/gogo/protobuf/types"
multierror "github.com/hashicorp/go-multierror"
"k8s.io/api/batch/v2alpha1"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/labels"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/runtime/schema"
yamlDecoder "k8s.io/apimachinery/pkg/util/yaml"
meshconfig "istio.io/api/mesh/v1alpha1"
"istio.io/istio/pilot/pkg/model"
"istio.io/istio/pkg/log"
)
// per-sidecar policy and status
var (
alwaysValidFunc = func(value string) error {
return nil
}
annotationRegistry = []*registeredAnnotation{
{"sidecar.istio.io/inject", alwaysValidFunc},
{"sidecar.istio.io/status", alwaysValidFunc},
{"sidecar.istio.io/proxyImage", alwaysValidFunc},
{"sidecar.istio.io/interceptionMode", validateInterceptionMode},
{"status.sidecar.istio.io/port", validateStatusPort},
{"readiness.status.sidecar.istio.io/initialDelaySeconds", validateUInt32},
{"readiness.status.sidecar.istio.io/periodSeconds", validateUInt32},
{"readiness.status.sidecar.istio.io/failureThreshold", validateUInt32},
{"readiness.status.sidecar.istio.io/applicationPorts", validateReadinessApplicationPorts},
{"traffic.sidecar.istio.io/includeOutboundIPRanges", ValidateIncludeIPRanges},
{"traffic.sidecar.istio.io/excludeOutboundIPRanges", ValidateExcludeIPRanges},
{"traffic.sidecar.istio.io/includeInboundPorts", ValidateIncludeInboundPorts},
{"traffic.sidecar.istio.io/excludeInboundPorts", ValidateExcludeInboundPorts},
}
annotationPolicy = annotationRegistry[0]
annotationStatus = annotationRegistry[1]
)
type annotationValidationFunc func(value string) error
func validateAnnotations(annotations map[string]string) (err error) {
for _, validator := range annotationRegistry {
if e := validator.validate(annotations); e != nil {
err = multierror.Append(err, e)
}
}
return
}
type registeredAnnotation struct {
name string
validator annotationValidationFunc
}
func (v *registeredAnnotation) getValueOrDefault(annotations map[string]string, defaultValue string) string {
if val, ok := annotations[v.name]; ok {
return val
}
return defaultValue
}
func (v *registeredAnnotation) validate(annotations map[string]string) error {
if val, ok := annotations[v.name]; ok {
if err := v.validator(val); err != nil {
return fmt.Errorf("injection failed. Invalid value for annotation %s: %s. Error: %v", v.name, val, err)
}
}
return nil
}
// InjectionPolicy determines the policy for injecting the
// sidecar proxy into the watched namespace(s).
type InjectionPolicy string
const (
// InjectionPolicyDisabled specifies that the sidecar injector
// will not inject the sidecar into resources by default for the
// namespace(s) being watched. Resources can enable injection
// using the "sidecar.istio.io/inject" annotation with value of
// true.
InjectionPolicyDisabled InjectionPolicy = "disabled"
// InjectionPolicyEnabled specifies that the sidecar injector will
// inject the sidecar into resources by default for the
// namespace(s) being watched. Resources can disable injection
// using the "sidecar.istio.io/inject" annotation with value of
// false.
InjectionPolicyEnabled InjectionPolicy = "enabled"
)
// Defaults values for injecting istio proxy into kubernetes
// resources.
const (
DefaultSidecarProxyUID = uint64(1337)
DefaultVerbosity = 2
DefaultImagePullPolicy = "IfNotPresent"
DefaultStatusPort = 15020
DefaultReadinessInitialDelaySeconds = 1
DefaultReadinessPeriodSeconds = 2
DefaultReadinessFailureThreshold = 30
DefaultIncludeIPRanges = "*"
DefaultIncludeInboundPorts = "*"
)
const (
// ProxyContainerName is used by e2e integration tests for fetching logs
ProxyContainerName = "istio-proxy"
)
// SidecarInjectionSpec collects all container types and volumes for
// sidecar mesh injection
type SidecarInjectionSpec struct {
// RewriteHTTPProbe indicates whether Kubernetes HTTP prober in the PodSpec
// will be rewritten to be redirected by pilot agent.
RewriteAppHTTPProbe bool `yaml:"rewriteAppHTTPProbe"`
InitContainers []corev1.Container `yaml:"initContainers"`
Containers []corev1.Container `yaml:"containers"`
Volumes []corev1.Volume `yaml:"volumes"`
ImagePullSecrets []corev1.LocalObjectReference `yaml:"imagePullSecrets"`
}
// SidecarTemplateData is the data object to which the templated
// version of `SidecarInjectionSpec` is applied.
type SidecarTemplateData struct {
DeploymentMeta *metav1.ObjectMeta
ObjectMeta *metav1.ObjectMeta
Spec *corev1.PodSpec
ProxyConfig *meshconfig.ProxyConfig
MeshConfig *meshconfig.MeshConfig
}
// InitImageName returns the fully qualified image name for the istio
// init image given a docker hub and tag and debug flag
func InitImageName(hub string, tag string, _ bool) string {
return hub + "/proxy_init:" + tag
}
// ProxyImageName returns the fully qualified image name for the istio
// proxy image given a docker hub and tag and whether to use debug or not.
func ProxyImageName(hub string, tag string, debug bool) string {
// Allow overriding the proxy image.
if debug {
return hub + "/proxy_debug:" + tag
}
return hub + "/proxyv2:" + tag
}
// Params describes configurable parameters for injecting istio proxy
// into a kubernetes resource.
type Params struct {
InitImage string `json:"initImage"`
RewriteAppHTTPProbe bool `json:"rewriteAppHTTPProbe"`
ProxyImage string `json:"proxyImage"`
Verbosity int `json:"verbosity"`
SidecarProxyUID uint64 `json:"sidecarProxyUID"`
Version string `json:"version"`
EnableCoreDump bool `json:"enableCoreDump"`
DebugMode bool `json:"debugMode"`
Privileged bool `json:"privileged"`
Mesh *meshconfig.MeshConfig `json:"-"`
ImagePullPolicy string `json:"imagePullPolicy"`
StatusPort int `json:"statusPort"`
ReadinessInitialDelaySeconds uint32 `json:"readinessInitialDelaySeconds"`
ReadinessPeriodSeconds uint32 `json:"readinessPeriodSeconds"`
ReadinessFailureThreshold uint32 `json:"readinessFailureThreshold"`
SDSEnabled bool `json:"sdsEnabled"`
EnableSdsTokenMount bool `json:"enableSdsTokenMount"`
// Comma separated list of IP ranges in CIDR form. If set, only redirect outbound traffic to Envoy for these IP
// ranges. All outbound traffic can be redirected with the wildcard character "*". Defaults to "*".
IncludeIPRanges string `json:"includeIPRanges"`
// Comma separated list of IP ranges in CIDR form. If set, outbound traffic will not be redirected for
// these IP ranges. Exclusions are only applied if configured to redirect all outbound traffic. By default,
// no IP ranges are excluded.
ExcludeIPRanges string `json:"excludeIPRanges"`
// Comma separated list of inbound ports for which traffic is to be redirected to Envoy. All ports can be
// redirected with the wildcard character "*". Defaults to "*".
IncludeInboundPorts string `json:"includeInboundPorts"`
// Comma separated list of inbound ports. If set, inbound traffic will not be redirected for those ports.
// Exclusions are only applied if configured to redirect all inbound traffic. By default, no ports are excluded.
ExcludeInboundPorts string `json:"excludeInboundPorts"`
}
// Validate validates the parameters and returns an error if there is configuration issue.
func (p *Params) Validate() error {
if err := ValidateIncludeIPRanges(p.IncludeIPRanges); err != nil {
return err
}
if err := ValidateExcludeIPRanges(p.ExcludeIPRanges); err != nil {
return err
}
if err := ValidateIncludeInboundPorts(p.IncludeInboundPorts); err != nil {
return err
}
return ValidateExcludeInboundPorts(p.ExcludeInboundPorts)
}
// Config specifies the sidecar injection configuration This includes
// the sidecar template and cluster-side injection policy. It is used
// by kube-inject, sidecar injector, and http endpoint.
type Config struct {
Policy InjectionPolicy `json:"policy"`
// Template is the templated version of `SidecarInjectionSpec` prior to
// expansion over the `SidecarTemplateData`.
Template string `json:"template"`
// NeverInjectSelector: Refuses the injection on pods whose labels match this selector.
// It's an array of label selectors, that will be OR'ed, meaning we will iterate
// over it and stop at the first match
// Takes precedence over AlwaysInjectSelector.
NeverInjectSelector []metav1.LabelSelector `json:"neverInjectSelector"`
// AlwaysInjectSelector: Forces the injection on pods whose labels match this selector.
// It's an array of label selectors, that will be OR'ed, meaning we will iterate
// over it and stop at the first match
AlwaysInjectSelector []metav1.LabelSelector `json:"alwaysInjectSelector"`
}
func validateCIDRList(cidrs string) error {
if len(cidrs) > 0 {
for _, cidr := range strings.Split(cidrs, ",") {
if _, _, err := net.ParseCIDR(cidr); err != nil {
return fmt.Errorf("failed parsing cidr '%s': %v", cidr, err)
}
}
}
return nil
}
func splitPorts(portsString string) []string {
return strings.Split(portsString, ",")
}
func parsePort(portStr string) (int, error) {
port, err := strconv.ParseUint(strings.TrimSpace(portStr), 10, 16)
if err != nil {
return 0, fmt.Errorf("failed parsing port '%d': %v", port, err)
}
return int(port), nil
}
func parsePorts(portsString string) ([]int, error) {
portsString = strings.TrimSpace(portsString)
ports := make([]int, 0)
if len(portsString) > 0 {
for _, portStr := range splitPorts(portsString) {
port, err := parsePort(portStr)
if err != nil {
return nil, fmt.Errorf("failed parsing port '%d': %v", port, err)
}
ports = append(ports, port)
}
}
return ports, nil
}
func validatePortList(parameterName, ports string) error {
if _, err := parsePorts(ports); err != nil {
return fmt.Errorf("%s invalid: %v", parameterName, err)
}
return nil
}
// validateInterceptionMode validates the interceptionMode annotation
func validateInterceptionMode(mode string) error {
switch mode {
case meshconfig.ProxyConfig_REDIRECT.String():
case meshconfig.ProxyConfig_TPROXY.String():
case string(model.InterceptionNone): // not a global mesh config - must be enabled for each sidecar
default:
return fmt.Errorf("interceptionMode invalid, use REDIRECT,TPROXY,NONE: %v", mode)
}
return nil
}
// ValidateIncludeIPRanges validates the includeIPRanges parameter
func ValidateIncludeIPRanges(ipRanges string) error {
if ipRanges != "*" {
if e := validateCIDRList(ipRanges); e != nil {
return fmt.Errorf("includeIPRanges invalid: %v", e)
}
}
return nil
}
// ValidateExcludeIPRanges validates the excludeIPRanges parameter
func ValidateExcludeIPRanges(ipRanges string) error {
if e := validateCIDRList(ipRanges); e != nil {
return fmt.Errorf("excludeIPRanges invalid: %v", e)
}
return nil
}
func validateReadinessApplicationPorts(ports string) error {
if ports != "*" {
return validatePortList("readinessApplicationPorts", ports)
}
return nil
}
// ValidateIncludeInboundPorts validates the includeInboundPorts parameter
func ValidateIncludeInboundPorts(ports string) error {
if ports != "*" {
return validatePortList("includeInboundPorts", ports)
}
return nil
}
// ValidateExcludeInboundPorts validates the excludeInboundPorts parameter
func ValidateExcludeInboundPorts(ports string) error {
return validatePortList("excludeInboundPorts", ports)
}
// validateStatusPort validates the statusPort parameter
func validateStatusPort(port string) error {
if _, e := parsePort(port); e != nil {
return fmt.Errorf("excludeInboundPorts invalid: %v", e)
}
return nil
}
// validateUInt32 validates that the given annotation value is a positive integer.
func validateUInt32(value string) error {
_, err := strconv.ParseUint(value, 10, 32)
return err
}
func injectRequired(ignored []string, config *Config, podSpec *corev1.PodSpec, metadata *metav1.ObjectMeta) bool { // nolint: lll
// Skip injection when host networking is enabled. The problem is
// that the iptable changes are assumed to be within the pod when,
// in fact, they are changing the routing at the host level. This
// often results in routing failures within a node which can
// affect the network provider within the cluster causing
// additional pod failures.
if podSpec.HostNetwork {
return false
}
// skip special kubernetes system namespaces
for _, namespace := range ignored {
if metadata.Namespace == namespace {
return false
}
}
annotations := metadata.GetAnnotations()
if annotations == nil {
annotations = map[string]string{}
}
var useDefault bool
var inject bool
switch strings.ToLower(annotations[annotationPolicy.name]) {
// http://yaml.org/type/bool.html
case "y", "yes", "true", "on":
inject = true
case "":
useDefault = true
}
// If an annotation is not explicitly given, check the LabelSelectors, starting with NeverInject
if useDefault {
for _, neverSelector := range config.NeverInjectSelector {
selector, err := metav1.LabelSelectorAsSelector(&neverSelector)
if err != nil {
log.Warnf("Invalid selector for NeverInjectSelector: %v (%v)", neverSelector, err)
} else {
if !selector.Empty() && selector.Matches(labels.Set(metadata.Labels)) {
log.Debugf("Explicitly disabling injection for pod %s/%s due to pod labels matching NeverInjectSelector config map entry.",
metadata.Namespace, potentialPodName(metadata))
inject = false
useDefault = false
break
}
}
}
}
// If there's no annotation nor a NeverInjectSelector, check the AlwaysInject one
if useDefault {
for _, alwaysSelector := range config.AlwaysInjectSelector {
selector, err := metav1.LabelSelectorAsSelector(&alwaysSelector)
if err != nil {
log.Warnf("Invalid selector for AlwaysInjectSelector: %v (%v)", alwaysSelector, err)
} else {
if !selector.Empty() && selector.Matches(labels.Set(metadata.Labels)) {
log.Debugf("Explicitly enabling injection for pod %s/%s due to pod labels matching AlwaysInjectSelector config map entry.",
metadata.Namespace, potentialPodName(metadata))
inject = true
useDefault = false
break
}
}
}
}
var required bool
switch config.Policy {
default: // InjectionPolicyOff
log.Errorf("Illegal value for autoInject:%s, must be one of [%s,%s]. Auto injection disabled!",
config.Policy, InjectionPolicyDisabled, InjectionPolicyEnabled)
required = false
case InjectionPolicyDisabled:
if useDefault {
required = false
} else {
required = inject
}
case InjectionPolicyEnabled:
if useDefault {
required = true
} else {
required = inject
}
}
if log.DebugEnabled() {
// Build a log message for the annotations.
annotationStr := ""
for _, a := range annotationRegistry {
annotationStr += fmt.Sprintf("%s:%s ", a.name, a.getValueOrDefault(annotations, "(unset)"))
}
log.Debugf("Sidecar injection policy for %v/%v: namespacePolicy:%v useDefault:%v inject:%v required:%v %s",
metadata.Namespace,
potentialPodName(metadata),
config.Policy,
useDefault,
inject,
required,
annotationStr)
}
return required
}
func formatDuration(in *types.Duration) string {
dur, err := types.DurationFromProto(in)
if err != nil {
return "1s"
}
return dur.String()
}
func isset(m map[string]string, key string) bool {
_, ok := m[key]
return ok
}
func directory(filepath string) string {
dir, _ := path.Split(filepath)
return dir
}
func injectionData(sidecarTemplate, version string, deploymentMetadata *metav1.ObjectMeta, spec *corev1.PodSpec,
metadata *metav1.ObjectMeta, proxyConfig *meshconfig.ProxyConfig, meshConfig *meshconfig.MeshConfig) (
*SidecarInjectionSpec, string, error) { // nolint: lll
if err := validateAnnotations(metadata.GetAnnotations()); err != nil {
log.Infof("Invalid annotations: %v %v\n", err, metadata.GetAnnotations())
return nil, "", err
}
data := SidecarTemplateData{
DeploymentMeta: deploymentMetadata,
ObjectMeta: metadata,
Spec: spec,
ProxyConfig: proxyConfig,
MeshConfig: meshConfig,
}
funcMap := template.FuncMap{
"formatDuration": formatDuration,
"isset": isset,
"excludeInboundPort": excludeInboundPort,
"includeInboundPorts": includeInboundPorts,
"applicationPorts": applicationPorts,
"annotation": annotation,
"valueOrDefault": valueOrDefault,
"toJSON": toJSON,
"fromJSON": fromJSON,
"toYaml": toYaml,
"indent": indent,
"directory": directory,
}
var tmpl bytes.Buffer
temp := template.New("inject").Delims(sidecarTemplateDelimBegin, sidecarTemplateDelimEnd)
t := template.Must(temp.Funcs(funcMap).Parse(sidecarTemplate))
if err := t.Execute(&tmpl, &data); err != nil {
log.Infof("Invalid template: %v %v\n", err, sidecarTemplate)
return nil, "", err
}
var sic SidecarInjectionSpec
if err := yaml.Unmarshal(tmpl.Bytes(), &sic); err != nil {
log.Warnf("Failed to unmarshall template %v %s", err, tmpl.String())
return nil, "", err
}
status := &SidecarInjectionStatus{Version: version}
for _, c := range sic.InitContainers {
status.InitContainers = append(status.InitContainers, c.Name)
}
for _, c := range sic.Containers {
status.Containers = append(status.Containers, c.Name)
}
for _, c := range sic.Volumes {
status.Volumes = append(status.Volumes, c.Name)
}
for _, c := range sic.ImagePullSecrets {
status.ImagePullSecrets = append(status.ImagePullSecrets, c.Name)
}
statusAnnotationValue, err := json.Marshal(status)
if err != nil {
return nil, "", fmt.Errorf("error encoded injection status: %v", err)
}
return &sic, string(statusAnnotationValue), nil
}
// IntoResourceFile injects the istio proxy into the specified
// kubernetes YAML file.
func IntoResourceFile(sidecarTemplate string, meshconfig *meshconfig.MeshConfig, in io.Reader, out io.Writer) error {
reader := yamlDecoder.NewYAMLReader(bufio.NewReaderSize(in, 4096))
for {
raw, err := reader.Read()
if err == io.EOF {
break
}
if err != nil {
return err
}
obj, err := fromRawToObject(raw)
if err != nil && !runtime.IsNotRegisteredError(err) {
return err
}
var updated []byte
if err == nil {
outObject, err := intoObject(sidecarTemplate, meshconfig, obj) // nolint: vetshadow
if err != nil {
return err
}
if updated, err = yaml.Marshal(outObject); err != nil {
return err
}
} else {
updated = raw // unchanged
}
if _, err = out.Write(updated); err != nil {
return err
}
if _, err = fmt.Fprint(out, "---\n"); err != nil {
return err
}
}
return nil
}
func fromRawToObject(raw []byte) (runtime.Object, error) {
var typeMeta metav1.TypeMeta
if err := yaml.Unmarshal(raw, &typeMeta); err != nil {
return nil, err
}
gvk := schema.FromAPIVersionAndKind(typeMeta.APIVersion, typeMeta.Kind)
obj, err := injectScheme.New(gvk)
if err != nil {
return nil, err
}
if err = yaml.Unmarshal(raw, obj); err != nil {
return nil, err
}
return obj, nil
}
func intoObject(sidecarTemplate string, meshconfig *meshconfig.MeshConfig, in runtime.Object) (interface{}, error) {
out := in.DeepCopyObject()
var deploymentMetadata *metav1.ObjectMeta
var metadata *metav1.ObjectMeta
var podSpec *corev1.PodSpec
// Handle Lists
if list, ok := out.(*corev1.List); ok {
result := list
for i, item := range list.Items {
obj, err := fromRawToObject(item.Raw)
if runtime.IsNotRegisteredError(err) {
continue
}
if err != nil {
return nil, err
}
r, err := intoObject(sidecarTemplate, meshconfig, obj) // nolint: vetshadow
if err != nil {
return nil, err
}
re := runtime.RawExtension{}
re.Object = r.(runtime.Object)
result.Items[i] = re
}
return result, nil
}
// CronJobs have JobTemplates in them, instead of Templates, so we
// special case them.
if job, ok := out.(*v2alpha1.CronJob); ok {
metadata = &job.Spec.JobTemplate.ObjectMeta
deploymentMetadata = &job.ObjectMeta
podSpec = &job.Spec.JobTemplate.Spec.Template.Spec
} else if pod, ok := out.(*corev1.Pod); ok {
metadata = &pod.ObjectMeta
deploymentMetadata = &pod.ObjectMeta
podSpec = &pod.Spec
} else {
// `in` is a pointer to an Object. Dereference it.
outValue := reflect.ValueOf(out).Elem()
deploymentMetadata = outValue.FieldByName("ObjectMeta").Addr().Interface().(*metav1.ObjectMeta)
templateValue := outValue.FieldByName("Spec").FieldByName("Template")
// `Template` is defined as a pointer in some older API
// definitions, e.g. ReplicationController
if templateValue.Kind() == reflect.Ptr {
templateValue = templateValue.Elem()
}
metadata = templateValue.FieldByName("ObjectMeta").Addr().Interface().(*metav1.ObjectMeta)
podSpec = templateValue.FieldByName("Spec").Addr().Interface().(*corev1.PodSpec)
}
// Skip injection when host networking is enabled. The problem is
// that the iptable changes are assumed to be within the pod when,
// in fact, they are changing the routing at the host level. This
// often results in routing failures within a node which can
// affect the network provider within the cluster causing
// additional pod failures.
if podSpec.HostNetwork {
fmt.Fprintf(os.Stderr, "Skipping injection because %q has host networking enabled\n", metadata.Name) //nolint: errcheck
return out, nil
}
spec, status, err := injectionData(
sidecarTemplate,
sidecarTemplateVersionHash(sidecarTemplate),
deploymentMetadata,
podSpec,
metadata,
meshconfig.DefaultConfig,
meshconfig)
if err != nil {
return nil, err
}
podSpec.InitContainers = append(podSpec.InitContainers, spec.InitContainers...)
podSpec.Containers = append(podSpec.Containers, spec.Containers...)
podSpec.Volumes = append(podSpec.Volumes, spec.Volumes...)
// Modify application containers' HTTP probe after appending injected containers.
// Because we need to extract istio-proxy's statusPort.
rewriteAppHTTPProbe(spec, podSpec)
// due to bug https://github.com/kubernetes/kubernetes/issues/57923,
// k8s sa jwt token volume mount file is only accessible to root user, not istio-proxy(the user that istio proxy runs as).
// workaround by https://kubernetes.io/docs/tasks/configure-pod-container/security-context/#set-the-security-context-for-a-pod
if meshconfig.EnableSdsTokenMount && meshconfig.SdsUdsPath != "" {
var grp = int64(1337)
podSpec.SecurityContext = &corev1.PodSecurityContext{
FSGroup: &grp,
}
}
if metadata.Annotations == nil {
metadata.Annotations = make(map[string]string)
}
metadata.Annotations[annotationStatus.name] = status
return out, nil
}
// GenerateTemplateFromParams generates a sidecar template from the legacy injection parameters
func GenerateTemplateFromParams(params *Params) (string, error) {
// Validate the parameters before we go any farther.
if err := params.Validate(); err != nil {
return "", err
}
var tmp bytes.Buffer
err := template.Must(template.New("inject").Parse(parameterizedTemplate)).Execute(&tmp, params)
return tmp.String(), err
}
func getPortsForContainer(container corev1.Container) []string {
parts := make([]string, 0)
for _, p := range container.Ports {
parts = append(parts, strconv.Itoa(int(p.ContainerPort)))
}
return parts
}
func getContainerPorts(containers []corev1.Container, shouldIncludePorts func(corev1.Container) bool) string {
parts := make([]string, 0)
for _, c := range containers {
if shouldIncludePorts(c) {
parts = append(parts, getPortsForContainer(c)...)
}
}
return strings.Join(parts, ",")
}
func applicationPorts(containers []corev1.Container) string {
return getContainerPorts(containers, func(c corev1.Container) bool {
return c.Name != ProxyContainerName
})
}
func includeInboundPorts(containers []corev1.Container) string {
// Include the ports from all containers in the deployment.
return getContainerPorts(containers, func(corev1.Container) bool { return true })
}
func toJSON(m map[string]string) string {
if m == nil {
return "{}"
}
ba, err := json.Marshal(m)
if err != nil {
log.Warnf("Unable to marshal %v", m)
return "{}"
}
return string(ba)
}
func fromJSON(j string) interface{} {
var m interface{}
err := json.Unmarshal([]byte(j), &m)
if err != nil {
log.Warnf("Unable to unmarshal %s", j)
return "{}"
}
log.Warnf("%v", m)
return m
}
func indent(spaces int, source string) string {
res := strings.Split(source, "\n")
for i, line := range res {
if i > 0 {
res[i] = fmt.Sprintf(fmt.Sprintf("%% %ds%%s", spaces), "", line)
}
}
return strings.Join(res, "\n")
}
func toYaml(value interface{}) string {
y, err := yaml.Marshal(value)
if err != nil {
log.Warnf("Unable to marshal %v", value)
return ""
}
return string(y)
}
func annotation(meta metav1.ObjectMeta, name string, defaultValue interface{}) string {
value, ok := meta.Annotations[name]
if !ok {
value = fmt.Sprint(defaultValue)
}
return value
}
func excludeInboundPort(port interface{}, excludedInboundPorts string) string {
portStr := strings.TrimSpace(fmt.Sprint(port))
if len(portStr) == 0 || portStr == "0" {
// Nothing to do.
return excludedInboundPorts
}
// Exclude the readiness port if not already excluded.
ports := splitPorts(excludedInboundPorts)
outPorts := make([]string, 0, len(ports))
for _, port := range ports {
if port == portStr {
// The port is already excluded.
return excludedInboundPorts
}
port = strings.TrimSpace(port)
if len(port) > 0 {
outPorts = append(outPorts, port)
}
}
// The port was not already excluded - exclude it now.
outPorts = append(outPorts, portStr)
return strings.Join(outPorts, ",")
}
func valueOrDefault(value string, defaultValue string) string {
if value == "" {
return defaultValue
}
return value
}
// SidecarInjectionStatus contains basic information about the
// injected sidecar. This includes the names of added containers and
// volumes.
type SidecarInjectionStatus struct {
Version string `json:"version"`
InitContainers []string `json:"initContainers"`
Containers []string `json:"containers"`
Volumes []string `json:"volumes"`
ImagePullSecrets []string `json:"imagePullSecrets"`
}
// helper function to generate a template version identifier from a
// hash of the un-executed template contents.
func sidecarTemplateVersionHash(in string) string {
hash := sha256.Sum256([]byte(in))
return hex.EncodeToString(hash[:])
}
func potentialPodName(metadata *metav1.ObjectMeta) string {
if metadata.Name != "" {
return metadata.Name
}
if metadata.GenerateName != "" {
return metadata.GenerateName + "***** (actual name not yet known)"
}
return ""
}