Skip to content

Commit

Permalink
fix panic reading empty targetRef from ep
Browse files Browse the repository at this point in the history
Endpoint's targetRef was being read without checking for nil value. A string targetRef version is optional and used eg on blue/green deployment. Using an empty string if nil is enough.

Should be merged to v0.8
  • Loading branch information
jcmoraisjr committed Nov 17, 2019
1 parent 12f7fa0 commit 2fc2d7a
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 4 deletions.
9 changes: 8 additions & 1 deletion pkg/converters/utils/services.go
Original file line number Diff line number Diff line change
Expand Up @@ -138,10 +138,17 @@ func newEndpointAddr(addr *api.EndpointAddress, port int) *Endpoint {
return &Endpoint{
IP: addr.IP,
Port: port,
TargetRef: fmt.Sprintf("%s/%s", addr.TargetRef.Namespace, addr.TargetRef.Name),
TargetRef: targetRefToString(addr.TargetRef),
}
}

func targetRefToString(targetRef *api.ObjectReference) string {
if targetRef == nil {
return ""
}
return fmt.Sprintf("%s/%s", targetRef.Namespace, targetRef.Name)
}

func newEndpointIP(ip string, port int) *Endpoint {
return &Endpoint{
IP: ip,
Expand Down
11 changes: 8 additions & 3 deletions pkg/converters/utils/services_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,14 @@ func TestCreateEndpoints(t *testing.T) {
for _, test := range testCases {
c := setup(t)
svc, ep := helper_test.CreateService("default/echo", test.declarePort, test.endpoints)
for _, ss := range ep.Subsets {
for i := range ss.Addresses {
ss.Addresses[i].TargetRef = nil
}
for i := range ss.NotReadyAddresses {
ss.NotReadyAddresses[i].TargetRef = nil
}
}
cache := &helper_test.CacheMock{
SvcList: []*api.Service{svc},
EpList: map[string]*api.Endpoints{"default/echo": ep},
Expand All @@ -112,9 +120,6 @@ func TestCreateEndpoints(t *testing.T) {
if port != nil {
endpoints, _, _ = CreateEndpoints(cache, svc, port)
}
for _, ep := range endpoints {
ep.TargetRef = ""
}
if !reflect.DeepEqual(endpoints, test.expected) {
t.Errorf("endpoints differ: expected=%+v actual=%+v", test.expected, endpoints)
}
Expand Down

0 comments on commit 2fc2d7a

Please sign in to comment.