Skip to content

Commit

Permalink
Merge pull request #3938 from johngmyers/internal-service
Browse files Browse the repository at this point in the history
Use ServiceIP for ClusterIP Services with internal-hostname annotation
  • Loading branch information
k8s-ci-robot authored Sep 26, 2023
2 parents 8251b6d + 7044fdf commit 9ad15cf
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 16 deletions.
2 changes: 1 addition & 1 deletion docs/annotations/annotations.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ The following table documents which sources support which annotations:
| Traefik | | Yes | | Yes | Yes | Yes |

[^1]: Unless the `--ignore-hostname-annotation` flag is specified.
[^2]: Only behaves differently than `hostname` for `Service`s of type `LoadBalancer`.
[^2]: Only behaves differently than `hostname` for `Service`s of type `ClusterIP` or `LoadBalancer`.
[^3]: Also supported on `Pods` referenced from a headless `Service`'s `Endpoints`.
[^4]: The annotation should be on the `Gateway`

Expand Down
3 changes: 2 additions & 1 deletion docs/sources/service.md
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,8 @@ or the `--publish-host-ip` flag was specified, uses the Pod's `status.hostIP` fi

### ClusterIP (not headless)

1. If the `--publish-internal-services` flag is specified, uses the `spec.ServiceIP`.
1. If the hostname came from an `external-dns.alpha.kubernetes.io/internal-hostname` annotation
or the `--publish-internal-services` flag was specified, uses the `spec.ServiceIP`.

2. Otherwise, does not create any targets.

Expand Down
2 changes: 1 addition & 1 deletion source/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -505,7 +505,7 @@ func (sc *serviceSource) generateEndpoints(svc *v1.Service, hostname string, pro
case v1.ServiceTypeClusterIP:
if svc.Spec.ClusterIP == v1.ClusterIPNone {
endpoints = append(endpoints, sc.extractHeadlessEndpoints(svc, hostname, ttl)...)
} else if sc.publishInternal {
} else if useClusterIP || sc.publishInternal {
targets = extractServiceIps(svc)
}
case v1.ServiceTypeNodePort:
Expand Down
44 changes: 31 additions & 13 deletions source/service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -910,7 +910,25 @@ func testServiceSourceEndpoints(t *testing.T) {
expected: []*endpoint.Endpoint{},
},
{
title: "internal-host annotated services return an endpoint with Cluster IP",
title: "internal-host annotated and host annotated clusterip services return an endpoint with Cluster IP",
svcNamespace: "testing",
svcName: "foo",
svcType: v1.ServiceTypeClusterIP,
labels: map[string]string{},
annotations: map[string]string{
hostnameAnnotationKey: "foo.example.org.",
internalHostnameAnnotationKey: "foo.internal.example.org.",
},
clusterIP: "1.1.1.1",
externalIPs: []string{},
lbs: []string{"1.2.3.4"},
serviceTypesFilter: []string{},
expected: []*endpoint.Endpoint{
{DNSName: "foo.internal.example.org", RecordType: endpoint.RecordTypeA, Targets: endpoint.Targets{"1.1.1.1"}},
},
},
{
title: "internal-host annotated loadbalancer services return an endpoint with Cluster IP",
svcNamespace: "testing",
svcName: "foo",
svcType: v1.ServiceTypeLoadBalancer,
Expand All @@ -927,7 +945,7 @@ func testServiceSourceEndpoints(t *testing.T) {
},
},
{
title: "internal-host annotated and host annotated services return an endpoint with Cluster IP and an endpoint with lb IP",
title: "internal-host annotated and host annotated loadbalancer services return an endpoint with Cluster IP and an endpoint with lb IP",
svcNamespace: "testing",
svcName: "foo",
svcType: v1.ServiceTypeLoadBalancer,
Expand Down Expand Up @@ -1816,10 +1834,10 @@ func TestServiceSourceNodePortServices(t *testing.T) {
},
},
}},
podNames: []string{"pod-0"},
nodeIndex: []int{1},
phases: []v1.PodPhase{v1.PodRunning},
conditions: []v1.PodCondition{{Type: v1.PodReady, Status: v1.ConditionFalse}},
podNames: []string{"pod-0"},
nodeIndex: []int{1},
phases: []v1.PodPhase{v1.PodRunning},
conditions: []v1.PodCondition{{Type: v1.PodReady, Status: v1.ConditionFalse}},
deletionTimestamp: []*metav1.Time{{}},
},
{
Expand Down Expand Up @@ -1867,7 +1885,7 @@ func TestServiceSourceNodePortServices(t *testing.T) {
{Type: v1.PodReady, Status: v1.ConditionFalse},
{Type: v1.PodReady, Status: v1.ConditionFalse},
},
deletionTimestamp: []*metav1.Time{{},{}},
deletionTimestamp: []*metav1.Time{{}, {}},
},
{
title: "annotated NodePort services with ExternalTrafficPolicy=Local return pods in Ready & Running state",
Expand Down Expand Up @@ -1911,7 +1929,7 @@ func TestServiceSourceNodePortServices(t *testing.T) {
{Type: v1.PodReady, Status: v1.ConditionTrue},
{Type: v1.PodReady, Status: v1.ConditionFalse},
},
deletionTimestamp: []*metav1.Time{{},{}},
deletionTimestamp: []*metav1.Time{{}, {}},
},
{
title: "annotated NodePort services with ExternalTrafficPolicy=Local return pods in Ready & Running state & not in Terminating",
Expand Down Expand Up @@ -2254,14 +2272,14 @@ func TestServiceSourceNodePortServices(t *testing.T) {
NodeName: tc.nodes[tc.nodeIndex[i]].Name,
},
ObjectMeta: metav1.ObjectMeta{
Namespace: tc.svcNamespace,
Name: podname,
Labels: tc.labels,
Annotations: tc.annotations,
Namespace: tc.svcNamespace,
Name: podname,
Labels: tc.labels,
Annotations: tc.annotations,
DeletionTimestamp: tc.deletionTimestamp[i],
},
Status: v1.PodStatus{
Phase: tc.phases[i],
Phase: tc.phases[i],
Conditions: []v1.PodCondition{tc.conditions[i]},
},
}
Expand Down

0 comments on commit 9ad15cf

Please sign in to comment.