Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(cni): delegated gateway was not correctly injected (backport of #11922) #11926

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
137 changes: 137 additions & 0 deletions pkg/plugins/runtime/k8s/webhooks/injector/injector.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package injector

Check failure on line 1 in pkg/plugins/runtime/k8s/webhooks/injector/injector.go

View workflow job for this annotation

GitHub Actions / check

: # github.com/kumahq/kuma/pkg/plugins/runtime/k8s/webhooks/injector

import (
"context"
Expand Down Expand Up @@ -121,6 +121,7 @@
pod.Annotations[kube_podcmd.DefaultContainerAnnotationName] = pod.Spec.Containers[0].Name
}

<<<<<<< HEAD

Check failure on line 124 in pkg/plugins/runtime/k8s/webhooks/injector/injector.go

View workflow job for this annotation

GitHub Actions / check

syntax error: unexpected <<, expected }

Check failure on line 124 in pkg/plugins/runtime/k8s/webhooks/injector/injector.go

View workflow job for this annotation

GitHub Actions / check

expected statement, found '<<' (typecheck)

Check failure on line 124 in pkg/plugins/runtime/k8s/webhooks/injector/injector.go

View workflow job for this annotation

GitHub Actions / test

syntax error: unexpected <<, expected }

Check failure on line 124 in pkg/plugins/runtime/k8s/webhooks/injector/injector.go

View workflow job for this annotation

GitHub Actions / test_e2e_env (kubernetes, v1.28.1-k3s1, amd64, 1, flannel, false) / e2e (0)

syntax error: unexpected <<, expected }

Check failure on line 124 in pkg/plugins/runtime/k8s/webhooks/injector/injector.go

View workflow job for this annotation

GitHub Actions / test_e2e_env (multizone, v1.28.1-k3s1, amd64, 1, flannel, false) / e2e (0)

syntax error: unexpected <<, expected }

Check failure on line 124 in pkg/plugins/runtime/k8s/webhooks/injector/injector.go

View workflow job for this annotation

GitHub Actions / test_e2e_env (universal, kind, amd64, 1, flannel, false) / e2e (0)

syntax error: unexpected <<, expected }
// inject sidecar as first container
pod.Spec.Containers = append([]kube_core.Container{patchedContainer}, pod.Spec.Containers...)

Expand All @@ -130,6 +131,98 @@
}
for key, value := range annotations {
pod.Annotations[key] = value
=======

Check failure on line 134 in pkg/plugins/runtime/k8s/webhooks/injector/injector.go

View workflow job for this annotation

GitHub Actions / check

expected statement, found '==' (typecheck)
var annotations map[string]string
var injectedInitContainer *kube_core.Container

if i.cfg.TransparentProxyConfigMapName != "" {

Check failure on line 138 in pkg/plugins/runtime/k8s/webhooks/injector/injector.go

View workflow job for this annotation

GitHub Actions / check

syntax error: non-declaration statement outside function body

Check failure on line 138 in pkg/plugins/runtime/k8s/webhooks/injector/injector.go

View workflow job for this annotation

GitHub Actions / test

syntax error: non-declaration statement outside function body

Check failure on line 138 in pkg/plugins/runtime/k8s/webhooks/injector/injector.go

View workflow job for this annotation

GitHub Actions / test_e2e_env (kubernetes, v1.28.1-k3s1, amd64, 1, flannel, false) / e2e (0)

syntax error: non-declaration statement outside function body

Check failure on line 138 in pkg/plugins/runtime/k8s/webhooks/injector/injector.go

View workflow job for this annotation

GitHub Actions / test_e2e_env (multizone, v1.28.1-k3s1, amd64, 1, flannel, false) / e2e (0)

syntax error: non-declaration statement outside function body

Check failure on line 138 in pkg/plugins/runtime/k8s/webhooks/injector/injector.go

View workflow job for this annotation

GitHub Actions / test_e2e_env (universal, kind, amd64, 1, flannel, false) / e2e (0)

syntax error: non-declaration statement outside function body
tproxyCfg, err := i.getTransparentProxyConfig(ctx, logger, pod)
if err != nil {
return err
}

tproxyCfgYAMLBytes, err := yaml.Marshal(tproxyCfg)
if err != nil {
return err
}
tproxyCfgYAML := string(tproxyCfgYAMLBytes)

if annotations, err = tproxy_k8s.ConfigToAnnotations(
tproxyCfg,
i.cfg,
pod.Annotations,
i.defaultAdminPort,
); err != nil {
return errors.Wrap(err, "could not generate annotations for pod")
}

for key, value := range annotations {
pod.Annotations[key] = value
}

if pod.Labels == nil {
pod.Labels = map[string]string{}
}
pod.Labels[metadata.KumaMeshLabel] = meshName

switch {
case !tproxyCfg.CNIMode:
initContainer := i.NewInitContainer([]string{"--config", tproxyCfgYAML})
injected, err := i.applyCustomPatches(logger, initContainer, initPatches)
if err != nil {
return err
}
injectedInitContainer = &injected
case tproxyCfg.Redirect.Inbound.Enabled:
ipFamilyMode := tproxyCfg.IPFamilyMode.String()
inboundPort := tproxyCfg.Redirect.Inbound.Port.String()
validationContainer := i.NewValidationContainer(ipFamilyMode, inboundPort, sidecarTmp.Name)
injected, err := i.applyCustomPatches(logger, validationContainer, initPatches)
if err != nil {
return err
}
injectedInitContainer = &injected
fallthrough
default:
pod.Annotations[metadata.KumaTrafficTransparentProxyConfig] = tproxyCfgYAML
}
} else { // this is legacy and deprecated - will be removed soon
if annotations, err = i.NewAnnotations(pod, logger); err != nil {
return errors.Wrap(err, "could not generate annotations for pod")
}

for key, value := range annotations {
pod.Annotations[key] = value
}

if pod.Labels == nil {
pod.Labels = map[string]string{}
}
pod.Labels[metadata.KumaMeshLabel] = meshName

podRedirect, err := tproxy_k8s.NewPodRedirectFromAnnotations(pod.Annotations)
if err != nil {
return err
}

if !i.cfg.CNIEnabled {
initContainer := i.NewInitContainer(podRedirect.AsKumactlCommandLine())
injected, err := i.applyCustomPatches(logger, initContainer, initPatches)
if err != nil {
return err
}
injectedInitContainer = &injected
} else if podRedirect.RedirectInbound {
ipFamilyMode := podRedirect.IpFamilyMode
inboundPort := fmt.Sprintf("%d", podRedirect.RedirectPortInbound)
validationContainer := i.NewValidationContainer(ipFamilyMode, inboundPort, sidecarTmp.Name)
injected, err := i.applyCustomPatches(logger, validationContainer, initPatches)
if err != nil {
return err
}
injectedInitContainer = &injected
}
>>>>>>> ebcc4be57 (fix(cni): delegated gateway was not correctly injected (#11922))

Check failure on line 225 in pkg/plugins/runtime/k8s/webhooks/injector/injector.go

View workflow job for this annotation

GitHub Actions / check

invalid character U+0023 '#'

Check failure on line 225 in pkg/plugins/runtime/k8s/webhooks/injector/injector.go

View workflow job for this annotation

GitHub Actions / check

expected statement, found '>>' (typecheck)

Check failure on line 225 in pkg/plugins/runtime/k8s/webhooks/injector/injector.go

View workflow job for this annotation

GitHub Actions / test

invalid character U+0023 '#'

Check failure on line 225 in pkg/plugins/runtime/k8s/webhooks/injector/injector.go

View workflow job for this annotation

GitHub Actions / test_e2e_env (kubernetes, v1.28.1-k3s1, amd64, 1, flannel, false) / e2e (0)

invalid character U+0023 '#'

Check failure on line 225 in pkg/plugins/runtime/k8s/webhooks/injector/injector.go

View workflow job for this annotation

GitHub Actions / test_e2e_env (multizone, v1.28.1-k3s1, amd64, 1, flannel, false) / e2e (0)

invalid character U+0023 '#'

Check failure on line 225 in pkg/plugins/runtime/k8s/webhooks/injector/injector.go

View workflow job for this annotation

GitHub Actions / test_e2e_env (universal, kind, amd64, 1, flannel, false) / e2e (0)

invalid character U+0023 '#'
}

if i.cfg.EBPF.Enabled {
Expand All @@ -150,10 +243,54 @@
})
}

<<<<<<< HEAD

Check failure on line 246 in pkg/plugins/runtime/k8s/webhooks/injector/injector.go

View workflow job for this annotation

GitHub Actions / check

expected statement, found '<<' (typecheck)
// init container
if !i.cfg.CNIEnabled {
ic, err := i.NewInitContainer(pod)
if err != nil {
=======
initFirst, _, err := metadata.Annotations(pod.Annotations).GetEnabled(metadata.KumaInitFirst)
if err != nil {
return err
}

var prependInitContainers []kube_core.Container
var appendInitContainers []kube_core.Container

if injectedInitContainer != nil {

Check failure on line 260 in pkg/plugins/runtime/k8s/webhooks/injector/injector.go

View workflow job for this annotation

GitHub Actions / check

syntax error: non-declaration statement outside function body

Check failure on line 260 in pkg/plugins/runtime/k8s/webhooks/injector/injector.go

View workflow job for this annotation

GitHub Actions / test

syntax error: non-declaration statement outside function body

Check failure on line 260 in pkg/plugins/runtime/k8s/webhooks/injector/injector.go

View workflow job for this annotation

GitHub Actions / test_e2e_env (kubernetes, v1.28.1-k3s1, amd64, 1, flannel, false) / e2e (0)

syntax error: non-declaration statement outside function body

Check failure on line 260 in pkg/plugins/runtime/k8s/webhooks/injector/injector.go

View workflow job for this annotation

GitHub Actions / test_e2e_env (multizone, v1.28.1-k3s1, amd64, 1, flannel, false) / e2e (0)

syntax error: non-declaration statement outside function body

Check failure on line 260 in pkg/plugins/runtime/k8s/webhooks/injector/injector.go

View workflow job for this annotation

GitHub Actions / test_e2e_env (universal, kind, amd64, 1, flannel, false) / e2e (0)

syntax error: non-declaration statement outside function body
if initFirst || i.sidecarContainersEnabled {
prependInitContainers = append(prependInitContainers, *injectedInitContainer)
} else {
appendInitContainers = append(appendInitContainers, *injectedInitContainer)
}
}

if i.sidecarContainersEnabled {
// inject sidecar after init
patchedContainer.RestartPolicy = pointer.To(kube_core.ContainerRestartPolicyAlways)
patchedContainer.Lifecycle = &kube_core.Lifecycle{
PreStop: &kube_core.LifecycleHandler{
Exec: &kube_core.ExecAction{
Command: []string{"killall", "-USR2", "kuma-dp"},
},
},
}
prependInitContainers = append(prependInitContainers, patchedContainer)
} else {
// inject sidecar as first container
pod.Spec.Containers = append([]kube_core.Container{patchedContainer}, pod.Spec.Containers...)
}

pod.Spec.InitContainers = append(append(prependInitContainers, pod.Spec.InitContainers...), appendInitContainers...)

disabledAppProbeProxy, err := probes.ApplicationProbeProxyDisabled(pod)
if err != nil {
return err
}

if disabledAppProbeProxy {
if err := i.overrideHTTPProbes(pod); err != nil {
>>>>>>> ebcc4be57 (fix(cni): delegated gateway was not correctly injected (#11922))

Check failure on line 293 in pkg/plugins/runtime/k8s/webhooks/injector/injector.go

View workflow job for this annotation

GitHub Actions / check

invalid character U+0023 '#' (typecheck)

Check failure on line 293 in pkg/plugins/runtime/k8s/webhooks/injector/injector.go

View workflow job for this annotation

GitHub Actions / test

invalid character U+0023 '#'

Check failure on line 293 in pkg/plugins/runtime/k8s/webhooks/injector/injector.go

View workflow job for this annotation

GitHub Actions / test_e2e_env (kubernetes, v1.28.1-k3s1, amd64, 1, flannel, false) / e2e (0)

invalid character U+0023 '#'

Check failure on line 293 in pkg/plugins/runtime/k8s/webhooks/injector/injector.go

View workflow job for this annotation

GitHub Actions / test_e2e_env (multizone, v1.28.1-k3s1, amd64, 1, flannel, false) / e2e (0)

invalid character U+0023 '#'

Check failure on line 293 in pkg/plugins/runtime/k8s/webhooks/injector/injector.go

View workflow job for this annotation

GitHub Actions / test_e2e_env (universal, kind, amd64, 1, flannel, false) / e2e (0)

invalid character U+0023 '#'
return err
}
patchedIc, err := i.applyCustomPatches(logger, ic, initPatches)
Expand Down
156 changes: 156 additions & 0 deletions pkg/plugins/runtime/k8s/webhooks/injector/injector_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -671,6 +671,162 @@
kuma.io/sidecar-injection: enabled`,
cfgFile: "inject.config.yaml",
}),
<<<<<<< HEAD

Check failure on line 674 in pkg/plugins/runtime/k8s/webhooks/injector/injector_test.go

View workflow job for this annotation

GitHub Actions / test

expected operand, found '<<'
=======
Entry("33. kuma.io/transparent-proxying-ip-family-mode", testCase{
num: "33",
mesh: `
apiVersion: kuma.io/v1alpha1
kind: Mesh
metadata:
name: default
spec: {}`,
namespace: `
apiVersion: v1
kind: Namespace
metadata:
name: default
labels:
kuma.io/sidecar-injection: enabled`,
cfgFile: "inject.config-ipv6-disabled.yaml",
}),
Entry("34. cni enabled", testCase{
num: "34",
mesh: `
apiVersion: kuma.io/v1alpha1
kind: Mesh
metadata:
name: default
spec: {}`,
namespace: `
apiVersion: v1
kind: Namespace
metadata:
name: default
labels:
kuma.io/sidecar-injection: enabled`,
cfgFile: "inject.config-cni.yaml",
}),
Entry("native sidecar with probe", testCase{
num: "35",
mesh: `
apiVersion: kuma.io/v1alpha1
kind: Mesh
metadata:
name: default
spec: {}`,
namespace: `
apiVersion: v1
kind: Namespace
metadata:
name: default
labels:
kuma.io/sidecar-injection: enabled`,
cfgFile: "inject.config.yaml",
}),
Entry("36. traffic.kuma.io/drop-invalid-packets overrides config", testCase{
num: "36",
mesh: `
apiVersion: kuma.io/v1alpha1
kind: Mesh
metadata:
name: default
spec: {}`,
namespace: `
apiVersion: v1
kind: Namespace
metadata:
name: default
labels:
kuma.io/sidecar-injection: enabled`,
cfgFile: "inject.config.yaml",
}),
Entry("37. traffic.kuma.io/iptables-logs overrides config", testCase{
num: "37",
mesh: `
apiVersion: kuma.io/v1alpha1
kind: Mesh
metadata:
name: default
spec: {}`,
namespace: `
apiVersion: v1
kind: Namespace
metadata:
name: default
labels:
kuma.io/sidecar-injection: enabled`,
cfgFile: "inject.config.yaml",
}),
Entry("38. traffic.kuma.io/exclude-outbound-ips overrides config", testCase{
num: "38",
mesh: `
apiVersion: kuma.io/v1alpha1
kind: Mesh
metadata:
name: default
spec: {}`,
namespace: `
apiVersion: v1
kind: Namespace
metadata:
name: default
labels:
kuma.io/sidecar-injection: enabled`,
cfgFile: "inject.config.yaml",
}),
Entry("39. traffic.kuma.io/exclude-inbound-ips overrides config", testCase{
num: "39",
mesh: `
apiVersion: kuma.io/v1alpha1
kind: Mesh
metadata:
name: default
spec: {}`,
namespace: `
apiVersion: v1
kind: Namespace
metadata:
name: default
labels:
kuma.io/sidecar-injection: enabled`,
cfgFile: "inject.config.yaml",
}),
Entry("40. application probe proxy: config - disabled, pod - enabled", testCase{
num: "40",
mesh: `
apiVersion: kuma.io/v1alpha1
kind: Mesh
metadata:
name: default
spec: {}`,
namespace: `
apiVersion: v1
kind: Namespace
metadata:
name: default
labels:
kuma.io/sidecar-injection: enabled`,
cfgFile: "inject.vp-disabled.config.yaml",
}),
Entry("41. gateway provided with cni enabled", testCase{
num: "41",
mesh: `
apiVersion: kuma.io/v1alpha1
kind: Mesh
metadata:
name: default
spec: {}`,
namespace: `
apiVersion: v1
kind: Namespace
metadata:
name: default
labels:
kuma.io/sidecar-injection: enabled`,
cfgFile: "inject.config-cni.yaml",
}),
>>>>>>> ebcc4be57 (fix(cni): delegated gateway was not correctly injected (#11922))
)

DescribeTable("should not inject Kuma into a Pod",
Expand Down
Loading
Loading