8000 Support native sidecar by default by irenezhong2861 · Pull Request #56428 · istio/istio · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

Support native sidecar by default #56428

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

Open
wants to merge 22 commits into
base: master
Choose a base branch
from
Open
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
4 changes: 4 additions & 0 deletions istioctl/pkg/kubeinject/kubeinject.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,10 @@ type ExternalInjector struct {
injectorAddress string
}

func (e ExternalInjector) GetKubeClient() kube.Client {
return e.client
}

func (e ExternalInjector) Inject(pod *corev1.Pod, deploymentNS string) ([]byte, error) {
cc := e.clientConfig
if cc == nil {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@
{{- end }}
{{- end }}
{{- end }}
{{ $nativeSidecar := (or (and (not (isset .ObjectMeta.Annotations `sidecar.istio.io/nativeSidecar`)) (eq (env "ENABLE_NATIVE_SIDECARS" "false") "true")) (eq (index .ObjectMeta.Annotations `sidecar.istio.io/nativeSidecar`) "true")) }}
{{ $tproxy := (eq (annotation .ObjectMeta `sidecar.istio.io/interceptionMode` .ProxyConfig.InterceptionMode) `TPROXY`) }}
{{ $nativeSidecar := ne (index .ObjectMeta.Annotations `sidecar.istio.io/nativeSidecar` | default (printf "%t" .NativeSidecars)) "false" }}
{{- $containers := list }}
{{- range $index, $container := .Spec.Containers }}{{ if not (eq $container.Name "istio-proxy") }}{{ $containers = append $containers $container.Name }}{{end}}{{- end}}
metadata:
Expand Down
9 changes: 9 additions & 0 deletions pilot/pkg/config/kube/gateway/deploymentcontroller.go
Original file line number Diff line number Diff line change
Expand Up @@ -430,6 +430,13 @@ func (d *DeploymentController) configureIstioGateway(log *istiolog.Scope, gw gat
serviceType = corev1.ServiceType(o)
}

var nativeSidecarModeEnabled bool
if features.EnableNativeSidecars == features.NativeSidecarModeDisabled {
nativeSidecarModeEnabled = false
} else {
nativeSidecarModeEnabled = true
}

input := TemplateInput{
Gateway: &gw,
DeploymentName: model.GetOrDefault(gw.Annotations[annotation.GatewayNameOverride.Name], defaultName),
Expand All @@ -439,6 +446,7 @@ func (d *DeploymentController) configureIstioGateway(log *istiolog.Scope, gw gat

KubeVersion: kube.GetVersionAsInt(d.client),
Revision: d.revision,
NativeSidecars: nativeSidecarModeEnabled,
ServiceType: serviceType,
ProxyUID: proxyUID,
ProxyGID: proxyGID,
Expand Down Expand Up @@ -871,6 +879,7 @@ type TemplateInput struct {
ClusterID string
KubeVersion int
Revision string
NativeSidecars bool
ProxyUID int64
ProxyGID int64
CompliancePolicy string
Expand Down
4 changes: 0 additions & 4 deletions pilot/pkg/features/experimental.go
Original file line number Diff line number Diff line change
Expand Up @@ -178,10 +178,6 @@ var (
EnableDualStack = env.RegisterBoolVar("ISTIO_DUAL_STACK", false,
"If true, Istio will enable the Dual Stack feature.").Get()

// This is used in injection templates, it is not unused.
EnableNativeSidecars = env.Register("ENABLE_NATIVE_SIDECARS", false,
"If set, used Kubernetes native Sidecar container support. Requires SidecarContainer feature flag.")

Enable100ContinueHeaders = env.Register("ENABLE_100_CONTINUE_HEADERS", true,
"If enabled, istiod will proxy 100-continue headers as is").Get()

Expand Down
27 changes: 27 additions & 0 deletions pilot/pkg/features/pilot.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
"istio.io/istio/pkg/config/constants"
"istio.io/istio/pkg/env"
"istio.io/istio/pkg/jwt"
"istio.io/istio/pkg/log"
"istio.io/istio/pkg/util/sets"
)

Expand Down Expand Up @@ -289,9 +290,35 @@ var (
"If set to true, Istio will watch for ca-crl.pem file in /etc/cacerts directory "+
"and will distribute crl data in each namespace in the cluster for proxies to consume. ",
).Get()

EnableNativeSidecars = func() NativeSidecarMode {
v := env.Register("ENABLE_NATIVE_SIDECARS", "auto",
"If set to true, use Kubernetes native sidecar container support. Requires SidecarContainer feature flag. "+
"Set to true to unconditionally enable, false to unconditionally disable. "+
"Set to auto to automatically enable for supported scenarios").Get()
switch v {
case "false":
return NativeSidecarModeDisabled
case "true":
return NativeSidecarModeEnabled
case "auto":
return NativeSidecarModeAuto
default:
log.Warnf("Unknown value for ENABLE_NATIVE_SIDECARS: %s, defaulting to false", v)
return NativeSidecarModeDisabled
}
}()
)

// UnsafeFeaturesEnabled returns true if any unsafe features are enabled.
func UnsafeFeaturesEnabled() bool {
return EnableUnsafeAdminEndpoints || EnableUnsafeAssertions || EnableUnsafeDeltaTest
}

type NativeSidecarMode int

const (
NativeSidecarModeEnabled NativeSidecarMode = iota
NativeSidecarModeDisabled = iota
NativeSidecarModeAuto = iota
)
17 changes: 16 additions & 1 deletion pkg/kube/inject/inject.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ import (
"istio.io/istio/pkg/config/mesh"
common_features "istio.io/istio/pkg/features"
"istio.io/istio/pkg/kube"
"istio.io/istio/pkg/kube/kclient"
"istio.io/istio/pkg/log"
"istio.io/istio/tools/istio-iptables/pkg/constants"
)
Expand Down Expand Up @@ -108,6 +109,7 @@ type SidecarTemplateData struct {
MeshConfig *meshconfig.MeshConfig
Values map[string]any
Revision string
NativeSidecars bool
ProxyImage string
ProxyUID int64
ProxyGID int64
Expand All @@ -123,6 +125,7 @@ type (

type Injector interface {
Inject(pod *corev1.Pod, namespace string) ([]byte, error)
GetKubeClient() kube.Client
}

// Config specifies the sidecar injection configuration This includes
Expand Down Expand Up @@ -433,6 +436,7 @@ func RunTemplate(params InjectionParameters) (mergedPod *corev1.Pod, templatePod
Values: params.valuesConfig.asMap,
Revision: params.revision,
ProxyImage: ProxyImage(params.valuesConfig.asStruct, params.proxyConfig.Image, strippedPod.Annotations),
NativeSidecars: params.nativeSidecar,
ProxyUID: proxyUID,
ProxyGID: proxyGID,
InboundTrafficPolicyMode: InboundTrafficPolicyMode(meshConfig),
Expand Down Expand Up @@ -465,7 +469,7 @@ func RunTemplate(params InjectionParameters) (mergedPod *corev1.Pod, templatePod
// So if we see the proxy container in `containers` in the original pod, and in `initContainers` in the template pod,
// move the container.
// The sidecar.istio.io/nativeSidecar annotation takes precedence over the global feature flag.
native := features.EnableNativeSidecars.Get()
native := params.nativeSidecar
if mergedPod.Annotations["sidecar.istio.io/nativeSidecar"] == "true" {
native = true
} else if mergedPod.Annotations["sidecar.istio.io/nativeSidecar"] == "false" {
Expand Down Expand Up @@ -797,6 +801,16 @@ func IntoObject(injector Injector, sidecarTemplate Templates, valuesConfig Value
warningHandler(warningStr)
return out, nil
}

var nativeSidecar bool
if injector != nil && injector.GetKubeClient() != nil {
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

added this so kube-inject will also have access to nodes and be able to decide if they are eligible for native sidecar.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is pretty sketchy since at least for injection we know the Pod is being created at that time. For kube-inject it is checked into CI.

But not a big deal since kube-inject is not very common

nodes := kclient.New[*corev1.Node](injector.GetKubeClient())
nativeSidecar = DetectNativeSidecar(nodes, pod.Spec.NodeName)
} else {
// if injector or client is nil, enable native sidecars if the feature is explicitly enabled
nativeSidecar = (features.EnableNativeSidecars == features.NativeSidecarModeEnabled)
}

params := InjectionParameters{
pod: pod,
deployMeta: deploymentMetadata,
Expand All @@ -807,6 +821,7 @@ func IntoObject(injector Injector, sidecarTemplate Templates, valuesConfig Value
meshConfig: meshconfig,
proxyConfig: meshconfig.GetDefaultConfig(),
valuesConfig: valuesConfig,
nativeSidecar: nativeSidecar,
revision: revision,
proxyEnvs: map[string]string{},
injectedAnnotations: nil,
Expand Down
Loading
0