-
Notifications
You must be signed in to change notification settings - Fork 506
/
generator.go
174 lines (160 loc) · 5.05 KB
/
generator.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
package agentmap
import (
"context"
"fmt"
"go.opentelemetry.io/otel"
core "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/util/intstr"
"github.com/datawire/k8sapi/pkg/k8sapi"
"github.com/telepresenceio/telepresence/v2/pkg/agentconfig"
"github.com/telepresenceio/telepresence/v2/pkg/install"
"github.com/telepresenceio/telepresence/v2/pkg/tracing"
)
const (
ServicePortAnnotation = agentconfig.DomainPrefix + "inject-service-port"
ServiceNameAnnotation = agentconfig.DomainPrefix + "inject-service-name"
ManagerAppName = "traffic-manager"
)
type GeneratorConfig struct {
ManagerPort uint16
AgentPort uint16
APIPort uint16
TracingPort uint16
QualifiedAgentImage string
ManagerNamespace string
LogLevel string
InitResources *core.ResourceRequirements
Resources *core.ResourceRequirements
PullSecrets []core.LocalObjectReference
EnvoyLogLevel string
EnvoyServerPort uint16
EnvoyAdminPort uint16
}
func Generate(ctx context.Context, wl k8sapi.Workload, cfg *GeneratorConfig) (sc *agentconfig.Sidecar, err error) {
ctx, span := otel.GetTracerProvider().Tracer("").Start(ctx, "agentmap.Generate")
defer tracing.EndAndRecord(span, err)
pod := wl.GetPodTemplate()
pod.Namespace = wl.GetNamespace()
cns := pod.Spec.Containers
for i := range cns {
cn := &cns[i]
if cn.Name == agentconfig.ContainerName {
continue
}
ports := cn.Ports
for pi := range ports {
if ports[pi].ContainerPort == int32(cfg.AgentPort) {
return nil, fmt.Errorf(
"the %s.%s pod container %s is exposing the same port (%d) as the %s sidecar",
pod.Name, pod.Namespace, cn.Name, cfg.AgentPort, agentconfig.ContainerName)
}
}
}
svcs, err := findServicesForPod(ctx, pod, pod.Annotations[ServiceNameAnnotation])
if err != nil {
return nil, err
}
var ccs []*agentconfig.Container
pns := make(map[int32]uint16)
portNumber := func(cnPort int32) uint16 {
if p, ok := pns[cnPort]; ok {
// Port already mapped. Reuse that mapping
return p
}
p := cfg.AgentPort + uint16(len(pns))
pns[cnPort] = p
return p
}
for _, svc := range svcs {
svcImpl, _ := k8sapi.ServiceImpl(svc)
if ccs, err = appendAgentContainerConfigs(svcImpl, pod, portNumber, ccs); err != nil {
return nil, err
}
}
if len(ccs) == 0 {
return nil, fmt.Errorf("found no service with a port that matches a container in pod %s.%s", pod.Name, pod.Namespace)
}
ag := &agentconfig.Sidecar{
AgentImage: cfg.QualifiedAgentImage,
AgentName: wl.GetName(),
LogLevel: cfg.LogLevel,
Namespace: wl.GetNamespace(),
WorkloadName: wl.GetName(),
WorkloadKind: wl.GetKind(),
ManagerHost: ManagerAppName + "." + cfg.ManagerNamespace,
ManagerPort: cfg.ManagerPort,
APIPort: cfg.APIPort,
TracingPort: cfg.TracingPort,
EnvoyLogLevel: cfg.EnvoyLogLevel,
EnvoyServerPort: cfg.EnvoyServerPort,
EnvoyAdminPort: cfg.EnvoyAdminPort,
Containers: ccs,
InitResources: cfg.InitResources,
Resources: cfg.Resources,
PullSecrets: cfg.PullSecrets,
}
ag.RecordInSpan(span)
return ag, nil
}
func appendAgentContainerConfigs(svc *core.Service, pod *core.PodTemplateSpec, portNumber func(int32) uint16, ccs []*agentconfig.Container) ([]*agentconfig.Container, error) {
portNameOrNumber := pod.Annotations[ServicePortAnnotation]
ports, err := install.FilterServicePorts(svc, portNameOrNumber)
if err != nil {
return nil, err
}
nextSvcPort:
for _, port := range ports {
cn, i := findContainerMatchingPort(&port, pod.Spec.Containers)
if cn == nil || cn.Name == agentconfig.ContainerName {
continue
}
var appPort core.ContainerPort
if i < 0 {
// Can only happen if the service port is numeric, so it's safe to use TargetPort.IntVal here
appPort = core.ContainerPort{
Protocol: port.Protocol,
ContainerPort: port.TargetPort.IntVal,
}
} else {
appPort = cn.Ports[i]
}
var appProto string
if port.AppProtocol != nil {
appProto = *port.AppProtocol
}
ic := &agentconfig.Intercept{
ServiceName: svc.Name,
ServiceUID: svc.UID,
ServicePortName: port.Name,
ServicePort: uint16(port.Port),
TargetPortNumeric: port.TargetPort.Type == intstr.Int,
Protocol: port.Protocol,
AppProtocol: appProto,
AgentPort: portNumber(appPort.ContainerPort),
ContainerPortName: appPort.Name,
ContainerPort: uint16(appPort.ContainerPort),
}
// The container might already have intercepts declared
for _, cc := range ccs {
if cc.Name == cn.Name {
cc.Intercepts = append(cc.Intercepts, ic)
continue nextSvcPort
}
}
var mounts []string
if l := len(cn.VolumeMounts); l > 0 {
mounts = make([]string, l)
for i, vm := range cn.VolumeMounts {
mounts[i] = vm.MountPath
}
}
ccs = append(ccs, &agentconfig.Container{
Name: cn.Name,
EnvPrefix: CapsBase26(uint64(len(ccs))) + "_",
MountPoint: agentconfig.MountPrefixApp + "/" + cn.Name,
Mounts: mounts,
Intercepts: []*agentconfig.Intercept{ic},
})
}
return ccs, nil
}