Skip to content

Commit

Permalink
Merge pull request #7 from tsuru/fix-tsuru-app-address-empty-ip
Browse files Browse the repository at this point in the history
v1alpha1.TsuruAppAddress: avoid to write a empty IP address when resolver fails
  • Loading branch information
wpjunior authored Jul 4, 2023
2 parents 17d71da + 4140883 commit 532961c
Show file tree
Hide file tree
Showing 3 changed files with 139 additions and 3 deletions.
21 changes: 20 additions & 1 deletion controllers/acl_dns_entry_controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,28 @@ import (
"sigs.k8s.io/controller-runtime/pkg/client/fake"
)

type fakeResolver struct{}
type fakeResolver struct {
hosts map[string][]string
errors map[string]error
}

func (f *fakeResolver) LookupIPAddr(ctx context.Context, host string) ([]net.IPAddr, error) {
if addrs, ok := f.hosts[host]; ok {
result := []net.IPAddr{}

for _, addr := range addrs {
result = append(result, net.IPAddr{
IP: net.ParseIP(addr),
})
}

return result, nil
}

if err, ok := f.errors[host]; ok {
return nil, err
}

if host == "www.google.com.br" {
return []net.IPAddr{
{
Expand Down
8 changes: 6 additions & 2 deletions controllers/tsuru_app_address_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ package controllers
import (
"context"
"errors"
"fmt"
"net"
"reflect"
"sort"
Expand Down Expand Up @@ -103,8 +104,11 @@ func (r *TsuruAppAddressReconciler) FillStatus(ctx context.Context, appAddress *
for _, addr := range addrs {
ipAddrs, err := r.resolveAddress(ctx, addr)
if err != nil {
// TODO: set feedback on app
continue
return err
}

if len(ipAddrs) == 0 {
return fmt.Errorf("host %s returned a empty string by resolver", addr)
}

for _, ipAddr := range ipAddrs {
Expand Down
113 changes: 113 additions & 0 deletions controllers/tsuru_app_address_controller_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
package controllers

import (
"context"
"errors"
"testing"
"time"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"github.com/tsuru/acl-operator/api/scheme"
"github.com/tsuru/acl-operator/api/v1alpha1"
v1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/types"
controllerruntime "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/client/fake"
)

func TestControllerResolveEmpty(t *testing.T) {
tsuruAppAddress := &v1alpha1.TsuruAppAddress{
ObjectMeta: v1.ObjectMeta{
Name: "my-other-app",
},
Spec: v1alpha1.TsuruAppAddressSpec{
Name: "my-other-app",
},
Status: v1alpha1.ResourceAddressStatus{
IPs: []string{"10.1.1.57"},
Pool: "my-pool",
Ready: true,
UpdatedAt: time.Now().UTC().Add(time.Hour * -1).String(),
},
}

controller := &TsuruAppAddressReconciler{
Client: fake.NewClientBuilder().WithScheme(scheme.Scheme).WithRuntimeObjects(tsuruAppAddress).Build(),
Scheme: scheme.Scheme,
TsuruAPI: &fakeTsuruAPI{},
Resolver: &fakeResolver{
hosts: map[string][]string{
"myapp.io": []string{},
},
},
}

_, err := controller.Reconcile(context.Background(), controllerruntime.Request{
NamespacedName: types.NamespacedName{
Name: tsuruAppAddress.Name,
Namespace: tsuruAppAddress.Namespace,
},
})

require.NoError(t, err)

existingTsuruAppAddress := &v1alpha1.TsuruAppAddress{}
err = controller.Client.Get(context.Background(), types.NamespacedName{
Name: tsuruAppAddress.Name,
Namespace: tsuruAppAddress.Namespace,
}, existingTsuruAppAddress)
require.NoError(t, err)

assert.Equal(t, []string{"10.1.1.57"}, existingTsuruAppAddress.Status.IPs)
assert.False(t, existingTsuruAppAddress.Status.Ready)
assert.Equal(t, "host myapp.io returned a empty string by resolver", existingTsuruAppAddress.Status.Reason)
}

func TestControllerResolveWithError(t *testing.T) {
tsuruAppAddress := &v1alpha1.TsuruAppAddress{
ObjectMeta: v1.ObjectMeta{
Name: "my-other-app",
},
Spec: v1alpha1.TsuruAppAddressSpec{
Name: "my-other-app",
},
Status: v1alpha1.ResourceAddressStatus{
IPs: []string{"10.1.1.57"},
Pool: "my-pool",
Ready: true,
UpdatedAt: time.Now().UTC().Add(time.Hour * -1).String(),
},
}

controller := &TsuruAppAddressReconciler{
Client: fake.NewClientBuilder().WithScheme(scheme.Scheme).WithRuntimeObjects(tsuruAppAddress).Build(),
Scheme: scheme.Scheme,
TsuruAPI: &fakeTsuruAPI{},
Resolver: &fakeResolver{
errors: map[string]error{
"myapp.io": errors.New("a error"),
},
},
}

_, err := controller.Reconcile(context.Background(), controllerruntime.Request{
NamespacedName: types.NamespacedName{
Name: tsuruAppAddress.Name,
Namespace: tsuruAppAddress.Namespace,
},
})

require.NoError(t, err)

existingTsuruAppAddress := &v1alpha1.TsuruAppAddress{}
err = controller.Client.Get(context.Background(), types.NamespacedName{
Name: tsuruAppAddress.Name,
Namespace: tsuruAppAddress.Namespace,
}, existingTsuruAppAddress)
require.NoError(t, err)

assert.Equal(t, []string{"10.1.1.57"}, existingTsuruAppAddress.Status.IPs)
assert.False(t, existingTsuruAppAddress.Status.Ready)
assert.Equal(t, "a error", existingTsuruAppAddress.Status.Reason)
}

0 comments on commit 532961c

Please sign in to comment.