-
Notifications
You must be signed in to change notification settings - Fork 344
/
agent.go
215 lines (195 loc) · 6.7 KB
/
agent.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
package deployment
import (
"fmt"
"sort"
"strconv"
"strings"
"github.com/operator-framework/operator-lib/proxy"
appsv1 "k8s.io/api/apps/v1"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/util/intstr"
v1 "github.com/jaegertracing/jaeger-operator/apis/v1"
"github.com/jaegertracing/jaeger-operator/pkg/account"
"github.com/jaegertracing/jaeger-operator/pkg/autodetect"
"github.com/jaegertracing/jaeger-operator/pkg/config/ca"
"github.com/jaegertracing/jaeger-operator/pkg/service"
"github.com/jaegertracing/jaeger-operator/pkg/util"
)
// Agent builds pods for jaegertracing/jaeger-agent
type Agent struct {
jaeger *v1.Jaeger
}
// NewAgent builds a new Agent struct based on the given spec
func NewAgent(jaeger *v1.Jaeger) *Agent {
return &Agent{jaeger: jaeger}
}
// Get returns a Agent pod
func (a *Agent) Get() *appsv1.DaemonSet {
if !strings.EqualFold(a.jaeger.Spec.Agent.Strategy, "daemonset") {
a.jaeger.Logger().V(-1).Info(
"skipping agent daemonset",
"strategy", a.jaeger.Spec.Agent.Strategy,
)
return nil
}
args := a.jaeger.Spec.Agent.Options.ToArgs()
// we only add the grpc host if we are adding the reporter type and there's no explicit value yet
if len(util.FindItem("--reporter.grpc.host-port=", args)) == 0 {
args = append(args, fmt.Sprintf("--reporter.grpc.host-port=dns:///%s.%s:14250", service.GetNameForHeadlessCollectorService(a.jaeger), a.jaeger.Namespace))
}
// Enable tls by default for openshift platform
if autodetect.OperatorConfiguration.GetPlatform() == autodetect.OpenShiftPlatform {
if len(util.FindItem("--reporter.grpc.tls.enabled=", args)) == 0 {
args = append(args, "--reporter.grpc.tls.enabled=true")
args = append(args, fmt.Sprintf("--reporter.grpc.tls.ca=%s", ca.ServiceCAPath))
args = append(args, fmt.Sprintf("--reporter.grpc.tls.server-name=%s.%s.svc.cluster.local", service.GetNameForHeadlessCollectorService(a.jaeger), a.jaeger.Namespace))
}
}
zkCompactTrft := util.GetPort("--processor.zipkin-compact.server-host-port=", args, 5775)
configRest := util.GetPort("--http-server.host-port=", args, 5778)
jgCompactTrft := util.GetPort("--processor.jaeger-compact.server-host-port=", args, 6831)
jgBinaryTrft := util.GetPort("--processor.jaeger-binary.server-host-port=", args, 6832)
adminPort := util.GetAdminPort(args, 14271)
trueVar := true
falseVar := false
labels := util.Labels(a.name(), "agent", *a.jaeger)
baseCommonSpec := v1.JaegerCommonSpec{
Annotations: map[string]string{
"prometheus.io/scrape": "true",
"prometheus.io/port": strconv.Itoa(int(adminPort)),
"linkerd.io/inject": "disabled",
},
Labels: labels,
}
commonSpec := util.Merge([]v1.JaegerCommonSpec{a.jaeger.Spec.Agent.JaegerCommonSpec, a.jaeger.Spec.JaegerCommonSpec, baseCommonSpec})
_, ok := commonSpec.Annotations["sidecar.istio.io/inject"]
if !ok {
commonSpec.Annotations["sidecar.istio.io/inject"] = "false"
}
ca.Update(a.jaeger, commonSpec)
ca.AddServiceCA(a.jaeger, commonSpec)
// ensure we have a consistent order of the arguments
// see https://github.com/jaegertracing/jaeger-operator/issues/334
sort.Strings(args)
hostNetwork := false
dnsPolicy := a.jaeger.Spec.Agent.DNSPolicy
if a.jaeger.Spec.Agent.HostNetwork != nil {
hostNetwork = *a.jaeger.Spec.Agent.HostNetwork
if dnsPolicy == "" {
dnsPolicy = corev1.DNSClusterFirstWithHostNet
}
}
priorityClassName := ""
if a.jaeger.Spec.Agent.PriorityClassName != "" {
priorityClassName = a.jaeger.Spec.Agent.PriorityClassName
}
livenessProbe := &corev1.Probe{
ProbeHandler: corev1.ProbeHandler{
HTTPGet: &corev1.HTTPGetAction{
Path: "/",
Port: intstr.FromInt(int(adminPort)),
},
},
InitialDelaySeconds: 5,
PeriodSeconds: 15,
FailureThreshold: 5,
}
if a.jaeger.Spec.Agent.LivenessProbe != nil {
livenessProbe = a.jaeger.Spec.Agent.LivenessProbe
}
return &appsv1.DaemonSet{
TypeMeta: metav1.TypeMeta{
APIVersion: "apps/v1",
Kind: "DaemonSet",
},
ObjectMeta: metav1.ObjectMeta{
Name: fmt.Sprintf("%s-agent-daemonset", a.jaeger.Name),
Namespace: a.jaeger.Namespace,
Labels: commonSpec.Labels,
OwnerReferences: []metav1.OwnerReference{{
APIVersion: a.jaeger.APIVersion,
Kind: a.jaeger.Kind,
Name: a.jaeger.Name,
UID: a.jaeger.UID,
Controller: &trueVar,
}},
},
Spec: appsv1.DaemonSetSpec{
Selector: &metav1.LabelSelector{
MatchLabels: commonSpec.Labels,
},
Template: corev1.PodTemplateSpec{
ObjectMeta: metav1.ObjectMeta{
Labels: commonSpec.Labels,
Annotations: commonSpec.Annotations,
},
Spec: corev1.PodSpec{
ImagePullSecrets: a.jaeger.Spec.Agent.ImagePullSecrets,
Containers: []corev1.Container{{
Image: util.ImageName(a.jaeger.Spec.Agent.Image, "jaeger-agent-image"),
Name: "jaeger-agent-daemonset",
Args: args,
Env: proxy.ReadProxyVarsFromEnv(),
Ports: []corev1.ContainerPort{
{
ContainerPort: zkCompactTrft,
HostPort: zkCompactTrft,
Name: "zk-compact-trft",
Protocol: corev1.ProtocolUDP,
},
{
ContainerPort: configRest,
HostPort: configRest,
Name: "config-rest",
},
{
ContainerPort: jgCompactTrft,
HostPort: jgCompactTrft,
Name: "jg-compact-trft",
Protocol: corev1.ProtocolUDP,
},
{
ContainerPort: jgBinaryTrft,
HostPort: jgBinaryTrft,
Name: "jg-binary-trft",
Protocol: corev1.ProtocolUDP,
},
{
ContainerPort: adminPort,
HostPort: adminPort,
Name: "admin-http",
},
},
LivenessProbe: livenessProbe,
ReadinessProbe: &corev1.Probe{
ProbeHandler: corev1.ProbeHandler{
HTTPGet: &corev1.HTTPGetAction{
Path: "/",
Port: intstr.FromInt(int(adminPort)),
},
},
InitialDelaySeconds: 1,
},
Resources: commonSpec.Resources,
VolumeMounts: commonSpec.VolumeMounts,
ImagePullPolicy: commonSpec.ImagePullPolicy,
SecurityContext: commonSpec.ContainerSecurityContext,
}},
DNSPolicy: dnsPolicy,
HostNetwork: hostNetwork,
PriorityClassName: priorityClassName,
Volumes: commonSpec.Volumes,
Affinity: commonSpec.Affinity,
Tolerations: commonSpec.Tolerations,
SecurityContext: commonSpec.SecurityContext,
ServiceAccountName: account.JaegerServiceAccountFor(a.jaeger, account.AgentComponent),
EnableServiceLinks: &falseVar,
},
},
},
}
}
func (a *Agent) name() string {
return fmt.Sprintf("%s-agent", a.jaeger.Name)
}