Skip to content

Commit

Permalink
fix error case with intentional empty services (#439)
Browse files Browse the repository at this point in the history
  • Loading branch information
alexmasi authored Oct 9, 2023
1 parent a2d7ba9 commit 921a83f
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 8 deletions.
3 changes: 3 additions & 0 deletions topo/node/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -660,6 +660,9 @@ func (n *Impl) Pods(ctx context.Context) ([]*corev1.Pod, error) {

// Service returns the service definition for the node.
func (n *Impl) Services(ctx context.Context) ([]*corev1.Service, error) {
if len(n.Proto.Services) == 0 {
return nil, nil
}
s, err := n.KubeClient.CoreV1().Services(n.Namespace).Get(ctx, fmt.Sprintf("service-%s", n.Name()), metav1.GetOptions{})
if err != nil {
return nil, err
Expand Down
10 changes: 3 additions & 7 deletions topo/node/node_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -219,10 +219,9 @@ func TestService(t *testing.T) {
wantServiceErr string
want []*corev1.Service
}{{
desc: "no services",
node: &topopb.Node{Name: "dev1", Vendor: topopb.Vendor(1001)},
kClient: kfake.NewSimpleClientset(),
wantServiceErr: `"service-dev1" not found`,
desc: "no services",
node: &topopb.Node{Name: "dev1", Vendor: topopb.Vendor(1001)},
kClient: kfake.NewSimpleClientset(),
}, {
desc: "services valid",
node: &topopb.Node{
Expand Down Expand Up @@ -337,9 +336,6 @@ func TestService(t *testing.T) {
if s := errdiff.Check(err, tt.wantCreateErr); s != "" {
t.Fatalf("CreateService() failed: %s", s)
}
if tt.wantServiceErr != "" {
return
}

got, err := n.Services(context.Background())
if s := errdiff.Check(err, tt.wantServiceErr); s != "" {
Expand Down
2 changes: 1 addition & 1 deletion topo/topo.go
Original file line number Diff line number Diff line change
Expand Up @@ -368,7 +368,7 @@ func (m *Manager) Show(ctx context.Context) (*cpb.ShowTopologyResponse, error) {
}
for _, n := range m.topo.Nodes {
if len(n.Services) == 0 {
n.Services = map[uint32]*tpb.Service{}
continue
}
services, ok := r.Services[n.Name]
if !ok {
Expand Down
54 changes: 54 additions & 0 deletions topo/topo_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -955,6 +955,10 @@ func TestShow(t *testing.T) {
},
},
},
{
Name: "r3",
Vendor: tpb.Vendor(1004),
},
},
}

Expand Down Expand Up @@ -1013,6 +1017,16 @@ func TestShow(t *testing.T) {
Conditions: []corev1.PodCondition{{Type: corev1.PodReady, Status: corev1.ConditionTrue}},
},
},
&corev1.Pod{
ObjectMeta: metav1.ObjectMeta{
Name: "r3",
Namespace: "test",
},
Status: corev1.PodStatus{
Phase: corev1.PodRunning,
Conditions: []corev1.PodCondition{{Type: corev1.PodReady, Status: corev1.ConditionTrue}},
},
},
&corev1.Service{
ObjectMeta: metav1.ObjectMeta{
Name: "service-r1",
Expand Down Expand Up @@ -1100,6 +1114,16 @@ func TestShow(t *testing.T) {
Conditions: []corev1.PodCondition{{Type: corev1.PodReady, Status: corev1.ConditionTrue}},
},
},
&corev1.Pod{
ObjectMeta: metav1.ObjectMeta{
Name: "r3",
Namespace: "test",
},
Status: corev1.PodStatus{
Phase: corev1.PodRunning,
Conditions: []corev1.PodCondition{{Type: corev1.PodReady, Status: corev1.ConditionTrue}},
},
},
&corev1.Service{
ObjectMeta: metav1.ObjectMeta{
Name: "service-r1",
Expand Down Expand Up @@ -1209,6 +1233,16 @@ func TestShow(t *testing.T) {
Conditions: []corev1.PodCondition{{Type: corev1.PodReady, Status: corev1.ConditionTrue}},
},
},
&corev1.Pod{
ObjectMeta: metav1.ObjectMeta{
Name: "r3",
Namespace: "test",
},
Status: corev1.PodStatus{
Phase: corev1.PodRunning,
Conditions: []corev1.PodCondition{{Type: corev1.PodReady, Status: corev1.ConditionTrue}},
},
},
},
wantErr: "could not get services",
}, {
Expand Down Expand Up @@ -1236,6 +1270,16 @@ func TestShow(t *testing.T) {
Conditions: []corev1.PodCondition{{Type: corev1.PodReady, Status: corev1.ConditionTrue}},
},
},
&corev1.Pod{
ObjectMeta: metav1.ObjectMeta{
Name: "r3",
Namespace: "test",
},
Status: corev1.PodStatus{
Phase: corev1.PodRunning,
Conditions: []corev1.PodCondition{{Type: corev1.PodReady, Status: corev1.ConditionTrue}},
},
},
&corev1.Service{
ObjectMeta: metav1.ObjectMeta{
Name: "service-r1",
Expand Down Expand Up @@ -1320,6 +1364,16 @@ func TestShow(t *testing.T) {
Conditions: []corev1.PodCondition{{Type: corev1.PodReady, Status: corev1.ConditionTrue}},
},
},
&corev1.Pod{
ObjectMeta: metav1.ObjectMeta{
Name: "r3",
Namespace: "test",
},
Status: corev1.PodStatus{
Phase: corev1.PodRunning,
Conditions: []corev1.PodCondition{{Type: corev1.PodReady, Status: corev1.ConditionTrue}},
},
},
&corev1.Service{
ObjectMeta: metav1.ObjectMeta{
Name: "service-r1",
Expand Down

0 comments on commit 921a83f

Please sign in to comment.