Skip to content

Commit

Permalink
Merge pull request #1 from bpineau/new_fixes
Browse files Browse the repository at this point in the history
distinguish between route not found and route get real error
  • Loading branch information
bpineau committed Mar 8, 2018
2 parents ddaefa5 + c5934c6 commit f5ce044
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 13 deletions.
38 changes: 27 additions & 11 deletions pkg/hoster/gce/gce.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,11 @@ import (
"time"

"github.com/bpineau/cloud-floating-ip/config"
"golang.org/x/oauth2/google"
compute "google.golang.org/api/compute/v1"

"cloud.google.com/go/compute/metadata"
"golang.org/x/oauth2/google"
compute "google.golang.org/api/compute/v1"
"google.golang.org/api/googleapi"
)

const (
Expand Down Expand Up @@ -117,12 +118,17 @@ func (h *Hoster) Preempt() error {
DestRange: h.conf.IP,
}

// ther's no update: if a route by that name exists, we must delete it
resp, err := h.svc.Routes.Get(h.conf.Project, h.rname).Context(h.ctx).Do()
if err == nil && resp.Name != "" {
err = h.blockingWait(h.svc.Routes.Delete(h.conf.Project, h.rname).Do())
if err != nil {
fmt.Printf("Failed to delete an existing rule: %v\n", err)
// the api don't offer updates: if something already exists, delete it
err := h.blockingWait(h.svc.Routes.Delete(h.conf.Project, h.rname).Do())
if err != nil {
apierr, ok := err.(*googleapi.Error)

if !ok {
log.Fatalf("Failed to delete existing route and read error: %v", err)
}

if apierr.Code != 404 {
log.Fatalf("Failed to delete an existing rule: %v\n", err)
}
}

Expand All @@ -137,11 +143,21 @@ func (h *Hoster) Preempt() error {
// Status returns true if the floating IP address route to the instance
func (h *Hoster) Status() bool {
resp, err := h.svc.Routes.Get(h.conf.Project, h.rname).Context(h.ctx).Do()
if err != nil {
log.Fatal(err)

if err == nil {
return resp.NextHopInstance == h.selflink
}

return resp.NextHopInstance == h.selflink
// route not found is ok, means we don't "own" the IP
if apierr, ok := err.(*googleapi.Error); ok {
if apierr.Code == 404 {
return false
}
}

log.Fatalf("Failed to get route status: %v", err)

return false
}

func (h *Hoster) checkMissingParam() error {
Expand Down
4 changes: 2 additions & 2 deletions pkg/hoster/hoster.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,8 @@ func GuessHoster(name string) (Hoster, error) {
return nil, errors.New("hoster not supported: " + name)
}

for _, host := range Hosters {
if host.OnThisHoster() {
for _, h = range Hosters {
if h.OnThisHoster() {
return h, nil
}
}
Expand Down

0 comments on commit f5ce044

Please sign in to comment.