Skip to content

Commit

Permalink
set ServiceInternalTrafficPolicyLocal when using daemonset mode (open…
Browse files Browse the repository at this point in the history
…-telemetry#1517)

* set ServiceInternalTrafficPolicyLocal when using daemonset mode

Signed-off-by: hejianpeng <[email protected]>

* remove redundant empty lines

Signed-off-by: hejianpeng <[email protected]>

* add changelog

Signed-off-by: hejianpeng <[email protected]>

---------

Signed-off-by: hejianpeng <[email protected]>
  • Loading branch information
zirain authored Mar 10, 2023
1 parent 8d1fb4e commit 41eebfa
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 6 deletions.
16 changes: 16 additions & 0 deletions .chloggen/1517-fix-service-internal-traffic-policy.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# One of 'breaking', 'deprecation', 'new_component', 'enhancement', 'bug_fix'
change_type: bug_fix

# The name of the component, or a single word describing the area of concern, (e.g. operator, target allocator, github action)
component: "operator"

# A brief description of the change. Surround your text with quotes ("") if it needs to start with a backtick (`).
note: "Set `ServiceInternalTrafficPolicy`` to `Local` when using daemonset mode."

# One or more tracking issues related to the change
issues: [1401]

# (Optional) One or more lines of additional information to render under the primary note.
# These lines will be padded with 2 spaces and then inserted directly into the document.
# Use pipe (|) for multiline entries.
subtext:
12 changes: 9 additions & 3 deletions pkg/collector/reconcile/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,11 @@ func desiredService(ctx context.Context, params Params) *corev1.Service {
return nil
}

trafficPolicy := corev1.ServiceInternalTrafficPolicyCluster
if params.Instance.Spec.Mode == v1alpha1.ModeDaemonSet {
trafficPolicy = corev1.ServiceInternalTrafficPolicyLocal
}

return &corev1.Service{
ObjectMeta: metav1.ObjectMeta{
Name: naming.Service(params.Instance),
Expand All @@ -122,9 +127,10 @@ func desiredService(ctx context.Context, params Params) *corev1.Service {
Annotations: params.Instance.Annotations,
},
Spec: corev1.ServiceSpec{
Selector: collector.SelectorLabels(params.Instance),
ClusterIP: "",
Ports: ports,
InternalTrafficPolicy: &trafficPolicy,
Selector: collector.SelectorLabels(params.Instance),
ClusterIP: "",
Ports: ports,
},
}
}
Expand Down
28 changes: 25 additions & 3 deletions pkg/collector/reconcile/service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,23 @@ func TestDesiredService(t *testing.T) {

})

t.Run("should return service with local internal traffic policy", func(t *testing.T) {

grpc := "grpc"
jaegerPorts := v1.ServicePort{
Name: "jaeger-grpc",
Protocol: "TCP",
Port: 14250,
AppProtocol: &grpc,
}
p := paramsWithMode(v1alpha1.ModeDaemonSet)
ports := append(p.Instance.Spec.Ports, jaegerPorts)
expected := serviceWithInternalTrafficPolicy("test-collector", ports, v1.ServiceInternalTrafficPolicyLocal)
actual := desiredService(context.Background(), p)

assert.Equal(t, expected, *actual)
})

}

func TestExpectedServices(t *testing.T) {
Expand Down Expand Up @@ -230,6 +247,10 @@ func TestMonitoringService(t *testing.T) {
}

func service(name string, ports []v1.ServicePort) v1.Service {
return serviceWithInternalTrafficPolicy(name, ports, v1.ServiceInternalTrafficPolicyCluster)
}

func serviceWithInternalTrafficPolicy(name string, ports []v1.ServicePort, internalTrafficPolicy v1.ServiceInternalTrafficPolicyType) v1.Service {
labels := collector.Labels(params().Instance, []string{})
labels["app.kubernetes.io/name"] = name

Expand All @@ -241,9 +262,10 @@ func service(name string, ports []v1.ServicePort) v1.Service {
Annotations: params().Instance.Annotations,
},
Spec: v1.ServiceSpec{
Selector: collector.SelectorLabels(params().Instance),
ClusterIP: "",
Ports: ports,
InternalTrafficPolicy: &internalTrafficPolicy,
Selector: collector.SelectorLabels(params().Instance),
ClusterIP: "",
Ports: ports,
},
}
}
5 changes: 5 additions & 0 deletions pkg/collector/reconcile/suite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,10 @@ func TestMain(m *testing.M) {
}

func params() Params {
return paramsWithMode(v1alpha1.ModeDeployment)
}

func paramsWithMode(mode v1alpha1.Mode) Params {
replicas := int32(2)
configYAML, err := os.ReadFile("../testdata/test.yaml")
if err != nil {
Expand Down Expand Up @@ -207,6 +211,7 @@ func params() Params {
}},
Replicas: &replicas,
Config: string(configYAML),
Mode: mode,
},
},
Scheme: testScheme,
Expand Down

0 comments on commit 41eebfa

Please sign in to comment.