Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adds a wait to account for the possiblity of a not ready unix socket #1201

Closed
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading