-
Notifications
You must be signed in to change notification settings - Fork 323
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Backport of Add refreshes and retries to server-acl-init job (#3137) …
…to release/1.2.x (#3251) Add refreshes and retries to server-acl-init job
- Loading branch information
1 parent
8bb0c10
commit 3183bb2
Showing
11 changed files
with
341 additions
and
101 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
```release-note:bug | ||
control-plane: fixes an issue with the server-acl-init job where the job would fail on upgrades due to consul server ip address changes. | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
package consul | ||
|
||
import ( | ||
"time" | ||
|
||
capi "github.com/hashicorp/consul/api" | ||
) | ||
|
||
type DynamicClient struct { | ||
ConsulClient *capi.Client | ||
Config *Config | ||
watcher ServerConnectionManager | ||
} | ||
|
||
func NewDynamicClientFromConnMgr(config *Config, watcher ServerConnectionManager) (*DynamicClient, error) { | ||
client, err := NewClientFromConnMgr(config, watcher) | ||
if err != nil { | ||
return nil, err | ||
} | ||
return &DynamicClient{ | ||
ConsulClient: client, | ||
Config: config, | ||
watcher: watcher, | ||
}, nil | ||
} | ||
|
||
func (d *DynamicClient) RefreshClient() error { | ||
var err error | ||
var client *capi.Client | ||
// If the watcher is not set then we did not create the client using NewDynamicClientFromConnMgr and are using it in | ||
// testing | ||
// TODO: Use watcher in testing ;) | ||
if d.watcher == nil { | ||
return nil | ||
} | ||
client, err = NewClientFromConnMgr(d.Config, d.watcher) | ||
if err != nil { | ||
return err | ||
} | ||
d.ConsulClient = client | ||
return nil | ||
} | ||
|
||
func NewDynamicClientWithTimeout(config *capi.Config, consulAPITimeout time.Duration) (*DynamicClient, error) { | ||
client, err := NewClient(config, consulAPITimeout) | ||
if err != nil { | ||
return nil, err | ||
} | ||
return &DynamicClient{ | ||
ConsulClient: client, | ||
Config: &Config{ | ||
APIClientConfig: config, | ||
}, | ||
}, nil | ||
} | ||
|
||
func NewDynamicClient(config *capi.Config) (*DynamicClient, error) { | ||
// defaultTimeout is taken from flags.go.. | ||
defaultTimeout := 5 * time.Second | ||
client, err := NewDynamicClientWithTimeout(config, defaultTimeout) | ||
if err != nil { | ||
return nil, err | ||
} | ||
return client, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
package consul | ||
|
||
import ( | ||
"fmt" | ||
"net" | ||
"net/http" | ||
"net/http/httptest" | ||
"net/url" | ||
"strconv" | ||
"testing" | ||
|
||
"github.com/hashicorp/consul-server-connection-manager/discovery" | ||
capi "github.com/hashicorp/consul/api" | ||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestRefreshDynamicClient(t *testing.T) { | ||
// Create a server | ||
consulServer := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { | ||
fmt.Fprintln(w, "\"leader\"") | ||
})) | ||
defer consulServer.Close() | ||
|
||
serverURL, err := url.Parse(consulServer.URL) | ||
require.NoError(t, err) | ||
serverIP := net.ParseIP(serverURL.Hostname()) | ||
|
||
port, err := strconv.Atoi(serverURL.Port()) | ||
require.NoError(t, err) | ||
|
||
connMgr := &MockServerConnectionManager{} | ||
|
||
// Use a bad IP so that the client call fails | ||
badState := discovery.State{ | ||
Address: discovery.Addr{ | ||
TCPAddr: net.TCPAddr{ | ||
IP: net.ParseIP("126.0.0.1"), | ||
Port: port, | ||
}, | ||
}, | ||
} | ||
|
||
goodState := discovery.State{ | ||
Address: discovery.Addr{ | ||
TCPAddr: net.TCPAddr{ | ||
IP: serverIP, | ||
Port: port, | ||
}, | ||
}, | ||
} | ||
|
||
// testify/mock has a weird behaviour when returning function calls. You cannot update On("State") to return | ||
// something different but instead need to load up the returns. Here we are simulating a bad consul server manager | ||
// state and then a good one | ||
connMgr.On("State").Return(badState, nil).Once() | ||
connMgr.On("State").Return(goodState, nil).Once() | ||
|
||
cfg := capi.DefaultConfig() | ||
client, err := NewDynamicClientFromConnMgr(&Config{APIClientConfig: cfg, HTTPPort: port, GRPCPort: port}, connMgr) | ||
require.NoError(t, err) | ||
|
||
// Make a request to the bad ip of the server | ||
_, err = client.ConsulClient.Status().Leader() | ||
require.Error(t, err) | ||
require.Contains(t, err.Error(), "connection refused") | ||
|
||
// Refresh the client and make a call to the server now that consul-server-connection-manager state is good | ||
err = client.RefreshClient() | ||
require.NoError(t, err) | ||
leader, err := client.ConsulClient.Status().Leader() | ||
require.NoError(t, err) | ||
require.Equal(t, "leader", leader) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.