Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Avoid nil pointer deference in extractHeadlessEndpoints #2031

Merged
merged 2 commits into from
Apr 3, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion source/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -284,7 +284,7 @@ func (sc *serviceSource) extractHeadlessEndpoints(svc *v1.Service, hostname stri

for _, address := range addresses {
// find pod for this address
if address.TargetRef.APIVersion != "" || address.TargetRef.Kind != "Pod" {
if address.TargetRef == nil || address.TargetRef.APIVersion != "" || address.TargetRef.Kind != "Pod" {
log.Debugf("Skipping address because its target is not a pod: %v", address)
continue
}
Expand Down
58 changes: 53 additions & 5 deletions source/service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2423,6 +2423,7 @@ func TestHeadlessServicesHostIP(t *testing.T) {
podnames []string
hostnames []string
podsReady []bool
targetRefs []*v1.ObjectReference
publishNotReadyAddresses bool
expected []*endpoint.Endpoint
expectError bool
Expand All @@ -2449,6 +2450,10 @@ func TestHeadlessServicesHostIP(t *testing.T) {
[]string{"foo-0", "foo-1"},
[]string{"foo-0", "foo-1"},
[]bool{true, true},
[]*v1.ObjectReference{
{APIVersion: "", Kind: "Pod", Name: "foo-0"},
{APIVersion: "", Kind: "Pod", Name: "foo-1"},
},
false,
[]*endpoint.Endpoint{
{DNSName: "foo-0.service.example.org", Targets: endpoint.Targets{"1.1.1.1"}},
Expand Down Expand Up @@ -2479,6 +2484,10 @@ func TestHeadlessServicesHostIP(t *testing.T) {
[]string{"foo-0", "foo-1"},
[]string{"foo-0", "foo-1"},
[]bool{true, true},
[]*v1.ObjectReference{
{APIVersion: "", Kind: "Pod", Name: "foo-0"},
{APIVersion: "", Kind: "Pod", Name: "foo-1"},
},
false,
[]*endpoint.Endpoint{},
false,
Expand Down Expand Up @@ -2506,6 +2515,10 @@ func TestHeadlessServicesHostIP(t *testing.T) {
[]string{"foo-0", "foo-1"},
[]string{"foo-0", "foo-1"},
[]bool{true, true},
[]*v1.ObjectReference{
{APIVersion: "", Kind: "Pod", Name: "foo-0"},
{APIVersion: "", Kind: "Pod", Name: "foo-1"},
},
false,
[]*endpoint.Endpoint{
{DNSName: "foo-0.service.example.org", Targets: endpoint.Targets{"1.1.1.1"}, RecordTTL: endpoint.TTL(1)},
Expand Down Expand Up @@ -2536,6 +2549,10 @@ func TestHeadlessServicesHostIP(t *testing.T) {
[]string{"foo-0", "foo-1"},
[]string{"foo-0", "foo-1"},
[]bool{true, false},
[]*v1.ObjectReference{
{APIVersion: "", Kind: "Pod", Name: "foo-0"},
{APIVersion: "", Kind: "Pod", Name: "foo-1"},
},
false,
[]*endpoint.Endpoint{
{DNSName: "foo-0.service.example.org", Targets: endpoint.Targets{"1.1.1.1"}},
Expand Down Expand Up @@ -2565,6 +2582,10 @@ func TestHeadlessServicesHostIP(t *testing.T) {
[]string{"foo-0", "foo-1"},
[]string{"foo-0", "foo-1"},
[]bool{true, false},
[]*v1.ObjectReference{
{APIVersion: "", Kind: "Pod", Name: "foo-0"},
{APIVersion: "", Kind: "Pod", Name: "foo-1"},
},
true,
[]*endpoint.Endpoint{
{DNSName: "foo-0.service.example.org", Targets: endpoint.Targets{"1.1.1.1"}},
Expand Down Expand Up @@ -2595,12 +2616,43 @@ func TestHeadlessServicesHostIP(t *testing.T) {
[]string{"foo-0", "foo-1"},
[]string{"", ""},
[]bool{true, true},
[]*v1.ObjectReference{
{APIVersion: "", Kind: "Pod", Name: "foo-0"},
{APIVersion: "", Kind: "Pod", Name: "foo-1"},
},
false,
[]*endpoint.Endpoint{
{DNSName: "service.example.org", Targets: endpoint.Targets{"1.1.1.1", "1.1.1.2"}},
},
false,
},
{
"annotated Headless services without a targetRef has no endpoints",
"",
"testing",
"foo",
v1.ServiceTypeClusterIP,
"",
"",
false,
map[string]string{"component": "foo"},
map[string]string{
hostnameAnnotationKey: "service.example.org",
},
v1.ClusterIPNone,
[]string{"1.1.1.1"},
map[string]string{
"component": "foo",
},
[]string{},
[]string{"foo-0"},
[]string{"foo-0"},
[]bool{true, true},
[]*v1.ObjectReference{nil},
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here is the test for the nil.

false,
[]*endpoint.Endpoint{},
false,
},
} {
t.Run(tc.title, func(t *testing.T) {
// Create a Kubernetes testing client
Expand Down Expand Up @@ -2648,11 +2700,7 @@ func TestHeadlessServicesHostIP(t *testing.T) {

address := v1.EndpointAddress{
IP: "4.3.2.1",
TargetRef: &v1.ObjectReference{
APIVersion: "",
Kind: "Pod",
Name: podname,
},
TargetRef: tc.targetRefs[i],
}
if tc.podsReady[i] {
addresses = append(addresses, address)
Expand Down