-
Notifications
You must be signed in to change notification settings - Fork 321
/
dataplane.go
178 lines (156 loc) · 5.55 KB
/
dataplane.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
// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: MPL-2.0
package gatekeeper
import (
"fmt"
"strconv"
corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/util/intstr"
"k8s.io/utils/pointer"
"github.com/hashicorp/consul-k8s/control-plane/api-gateway/common"
"github.com/hashicorp/consul-k8s/control-plane/api/v1alpha1"
"github.com/hashicorp/consul-k8s/control-plane/connect-inject/constants"
"github.com/hashicorp/consul-k8s/control-plane/namespaces"
)
const (
allCapabilities = "ALL"
netBindCapability = "NET_BIND_SERVICE"
consulDataplaneDNSBindHost = "127.0.0.1"
consulDataplaneDNSBindPort = 8600
defaultPrometheusScrapePath = "/metrics"
defaultEnvoyProxyConcurrency = 1
volumeName = "consul-connect-inject-data"
)
func consulDataplaneContainer(config common.HelmConfig, gcc v1alpha1.GatewayClassConfig, name, namespace string) (corev1.Container, error) {
// Extract the service account token's volume mount.
var (
err error
bearerTokenFile string
)
if config.AuthMethod != "" {
bearerTokenFile = "/var/run/secrets/kubernetes.io/serviceaccount/token"
}
args, err := getDataplaneArgs(namespace, config, bearerTokenFile, name)
if err != nil {
return corev1.Container{}, err
}
probe := &corev1.Probe{
ProbeHandler: corev1.ProbeHandler{
HTTPGet: &corev1.HTTPGetAction{
Port: intstr.FromInt(constants.ProxyDefaultHealthPort),
Path: "/ready",
},
},
InitialDelaySeconds: 1,
}
container := corev1.Container{
Name: name,
Image: config.ImageDataplane,
// We need to set tmp dir to an ephemeral volume that we're mounting so that
// consul-dataplane can write files to it. Otherwise, it wouldn't be able to
// because we set file system to be read-only.
Env: []corev1.EnvVar{
{
Name: "TMPDIR",
Value: "/consul/connect-inject",
},
{
Name: "NODE_NAME",
ValueFrom: &corev1.EnvVarSource{
FieldRef: &corev1.ObjectFieldSelector{
FieldPath: "spec.nodeName",
},
},
},
{
Name: "DP_SERVICE_NODE_NAME",
Value: "$(NODE_NAME)-virtual",
},
},
VolumeMounts: []corev1.VolumeMount{
{
Name: volumeName,
MountPath: "/consul/connect-inject",
},
},
Args: args,
ReadinessProbe: probe,
}
// Configure the Readiness Address for the proxy's health check to be the Pod IP.
container.Env = append(container.Env, corev1.EnvVar{
Name: "DP_ENVOY_READY_BIND_ADDRESS",
ValueFrom: &corev1.EnvVarSource{
FieldRef: &corev1.ObjectFieldSelector{FieldPath: "status.podIP"},
},
})
// Configure the port on which the readiness probe will query the proxy for its health.
container.Ports = append(container.Ports, corev1.ContainerPort{
Name: "proxy-health",
ContainerPort: int32(constants.ProxyDefaultHealthPort),
})
// Configure the resource requests and limits for the proxy if they are set.
if gcc.Spec.DeploymentSpec.Resources != nil {
container.Resources = *gcc.Spec.DeploymentSpec.Resources
}
// If running in vanilla K8s, run as root to allow binding to privileged ports;
// otherwise, allow the user to be assigned by OpenShift.
container.SecurityContext = &corev1.SecurityContext{
ReadOnlyRootFilesystem: pointer.Bool(true),
// Drop any Linux capabilities you'd get as root other than NET_BIND_SERVICE.
Capabilities: &corev1.Capabilities{
Add: []corev1.Capability{netBindCapability},
Drop: []corev1.Capability{allCapabilities},
},
}
if !config.EnableOpenShift {
container.SecurityContext.RunAsUser = pointer.Int64(0)
}
return container, nil
}
func getDataplaneArgs(namespace string, config common.HelmConfig, bearerTokenFile string, name string) ([]string, error) {
proxyIDFileName := "/consul/connect-inject/proxyid"
envoyConcurrency := defaultEnvoyProxyConcurrency
args := []string{
"-addresses", config.ConsulConfig.Address,
"-grpc-port=" + strconv.Itoa(config.ConsulConfig.GRPCPort),
"-proxy-service-id-path=" + proxyIDFileName,
"-log-level=" + config.LogLevel,
"-log-json=" + strconv.FormatBool(config.LogJSON),
"-envoy-concurrency=" + strconv.Itoa(envoyConcurrency),
}
consulNamespace := namespaces.ConsulNamespace(namespace, config.EnableNamespaces, config.ConsulDestinationNamespace, config.EnableNamespaceMirroring, config.NamespaceMirroringPrefix)
if config.AuthMethod != "" {
args = append(args,
"-credential-type=login",
"-login-auth-method="+config.AuthMethod,
"-login-bearer-token-path="+bearerTokenFile,
"-login-meta="+fmt.Sprintf("gateway=%s/%s", namespace, name),
)
if config.ConsulPartition != "" {
args = append(args, "-login-partition="+config.ConsulPartition)
}
}
if config.EnableNamespaces {
args = append(args, "-service-namespace="+consulNamespace)
}
if config.ConsulPartition != "" {
args = append(args, "-service-partition="+config.ConsulPartition)
}
if config.TLSEnabled {
if config.ConsulTLSServerName != "" {
args = append(args, "-tls-server-name="+config.ConsulTLSServerName)
}
if config.ConsulCACert != "" {
args = append(args, "-ca-certs="+constants.LegacyConsulCAFile)
}
} else {
args = append(args, "-tls-disabled")
}
// Configure the readiness port on the dataplane sidecar if proxy health checks are enabled.
args = append(args, fmt.Sprintf("%s=%d", "-envoy-ready-bind-port", constants.ProxyDefaultHealthPort))
args = append(args, fmt.Sprintf("-envoy-admin-bind-port=%d", 19000))
// Set a default scrape path that can be overwritten by the annotation.
prometheusScrapePath := defaultPrometheusScrapePath
args = append(args, "-telemetry-prom-scrape-path="+prometheusScrapePath)
return args, nil
}