Skip to content

Commit

Permalink
Adds a wait to account for the possiblity of a not ready unix socket
Browse files Browse the repository at this point in the history
  • Loading branch information
dougbtv committed Jan 4, 2024
1 parent ab7d64e commit ceb61be
Showing 1 changed file with 29 additions and 18 deletions.
47 changes: 29 additions & 18 deletions pkg/server/api/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (
"net"
"net/http"
"strings"
"time"
)

const (
Expand All @@ -43,30 +44,40 @@ func DoCNI(url string, req interface{}, socketPath string) ([]byte, error) {
return nil, fmt.Errorf("failed to marshal CNI request %v: %v", req, err)
}

client := &http.Client{
Transport: &http.Transport{
Dial: func(proto, addr string) (net.Conn, error) {
return net.Dial("unix", socketPath)
// Retry parameters
maxRetries := 60
delay := 2 * time.Second

for i := 0; i < maxRetries; i++ {
client := &http.Client{
Transport: &http.Transport{
Dial: func(proto, addr string) (net.Conn, error) {
return net.Dial("unix", socketPath)
},
},
},
}
}

resp, err := client.Post(url, "application/json", bytes.NewReader(data))
if err != nil {
return nil, fmt.Errorf("failed to send CNI request: %v", err)
}
defer resp.Body.Close()
resp, err := client.Post(url, "application/json", bytes.NewReader(data))
if err == nil {
defer resp.Body.Close()

body, err := io.ReadAll(resp.Body)
if err != nil {
return nil, fmt.Errorf("failed to read CNI result: %v", err)
}
body, err := io.ReadAll(resp.Body)
if err != nil {
return nil, fmt.Errorf("failed to read CNI result: %v", err)
}

if resp.StatusCode != http.StatusOK {
return nil, fmt.Errorf("CNI request failed with status %v: '%s'", resp.StatusCode, string(body))
}

return body, nil
}

if resp.StatusCode != http.StatusOK {
return nil, fmt.Errorf("CNI request failed with status %v: '%s'", resp.StatusCode, string(body))
// Wait for the next retry
time.Sleep(delay)
}

return body, nil
return nil, fmt.Errorf("failed to send CNI request after %d retries: %v", maxRetries, err)
}

// GetAPIEndpoint returns endpoint URL for multus-daemon
Expand Down

0 comments on commit ceb61be

Please sign in to comment.