openstack: remove unused code

This commit is contained in:
Malte Poll 2024-02-12 09:38:49 +01:00
parent c5b17fb828
commit bab27fbc69
6 changed files with 7 additions and 219 deletions

View File

@ -23,7 +23,6 @@ type imdsAPI interface {
initSecretHash(ctx context.Context) (string, error)
role(ctx context.Context) (role.Role, error)
vpcIP(ctx context.Context) (string, error)
networkIDs(ctx context.Context) ([]string, error)
}
type serversAPI interface {

View File

@ -30,8 +30,6 @@ type stubIMDSClient struct {
roleErr error
vpcIPResult string
vpcIPErr error
networkIDsResult []string
networkIDsErr error
}
func (c *stubIMDSClient) providerID(_ context.Context) (string, error) {
@ -62,10 +60,6 @@ func (c *stubIMDSClient) vpcIP(_ context.Context) (string, error) {
return c.vpcIPResult, c.vpcIPErr
}
func (c *stubIMDSClient) networkIDs(_ context.Context) ([]string, error) {
return c.networkIDsResult, c.networkIDsErr
}
type stubServersClient struct {
serversPager stubPager
subnetsPager stubPager

View File

@ -22,21 +22,18 @@ import (
// documentation of OpenStack Metadata Service: https://docs.openstack.org/nova/rocky/user/metadata-service.html
const (
imdsMetaDataURL = "http://169.254.169.254/openstack/2018-08-27/meta_data.json"
imdsNetworkDataURL = "http://169.254.169.254/openstack/2018-08-27/network_data.json"
ec2ImdsBaseURL = "http://169.254.169.254/1.0/meta-data"
maxCacheAge = 12 * time.Hour
imdsMetaDataURL = "http://169.254.169.254/openstack/2018-08-27/meta_data.json"
ec2ImdsBaseURL = "http://169.254.169.254/1.0/meta-data"
maxCacheAge = 12 * time.Hour
)
type imdsClient struct {
client httpClient
vpcIPCache string
vpcIPCacheTime time.Time
networkCache networkResponse
networkCacheTime time.Time
cache metadataResponse
cacheTime time.Time
vpcIPCache string
vpcIPCacheTime time.Time
cache metadataResponse
cacheTime time.Time
}
// providerID returns the provider ID of the instance the function is called from.
@ -226,30 +223,6 @@ func (c *imdsClient) vpcIP(ctx context.Context) (string, error) {
return c.vpcIPCache, nil
}
func (c *imdsClient) networkIDs(ctx context.Context) ([]string, error) {
if c.timeForUpdate(c.networkCacheTime) || len(c.networkCache.Networks) == 0 {
resp, err := httpGet(ctx, c.client, imdsNetworkDataURL)
if err != nil {
return nil, err
}
var networkResp networkResponse
if err := json.Unmarshal(resp, &networkResp); err != nil {
return nil, err
}
c.networkCache = networkResp
c.networkCacheTime = time.Now()
}
var networkIDs []string
for _, network := range c.networkCache.Networks {
if network.NetworkID == "" {
continue
}
networkIDs = append(networkIDs, network.NetworkID)
}
return networkIDs, nil
}
func httpGet(ctx context.Context, c httpClient, url string) ([]byte, error) {
req, err := http.NewRequestWithContext(ctx, http.MethodGet, url, http.NoBody)
if err != nil {
@ -281,16 +254,6 @@ type metadataTags struct {
Password string `json:"openstack-password,omitempty"`
}
// networkResponse contains networkResponse with only the required values.
type networkResponse struct {
Networks []metadataNetwork `json:"networks,omitempty"`
}
type metadataNetwork struct {
ID string `json:"id,omitempty"`
NetworkID string `json:"network_id,omitempty"`
}
type httpClient interface {
Do(req *http.Request) (*http.Response, error)
}

View File

@ -338,124 +338,6 @@ func TestVPCIP(t *testing.T) {
}
}
func TestNetworkIDs(t *testing.T) {
someErr := errors.New("failed")
testCases := map[string]struct {
cache networkResponse
cacheTime time.Time
client *stubHTTPClient
wantResult []string
wantCall bool
wantErr bool
}{
"cached": {
cache: networkResponse{Networks: []metadataNetwork{
{ID: "net0", NetworkID: "0000000-00000-0000-0000-000000000000"},
{ID: "net1", NetworkID: "1111111-11111-1111-1111-111111111111"},
{ID: "invalid"},
}},
cacheTime: time.Now(),
wantResult: []string{
"0000000-00000-0000-0000-000000000000",
"1111111-11111-1111-1111-111111111111",
},
wantCall: false,
},
"from http": {
client: &stubHTTPClient{
response: `
{
"networks": [
{
"id": "net0",
"network_id": "0000000-00000-0000-0000-000000000000"
},
{
"id": "net1",
"network_id": "1111111-11111-1111-1111-111111111111"
}
]
}`,
},
wantResult: []string{
"0000000-00000-0000-0000-000000000000",
"1111111-11111-1111-1111-111111111111",
},
wantCall: true,
},
"cache outdated": {
cache: networkResponse{Networks: []metadataNetwork{
{ID: "net0", NetworkID: "0000000-00000-0000-0000-000000000000"},
}},
cacheTime: time.Now().AddDate(0, 0, -1),
client: &stubHTTPClient{
response: `
{
"networks": [
{
"id": "net1",
"network_id": "1111111-11111-1111-1111-111111111111"
}
]
}`,
},
wantResult: []string{"1111111-11111-1111-1111-111111111111"},
wantCall: true,
},
"cache empty": {
cacheTime: time.Now(),
client: &stubHTTPClient{
response: `
{
"networks": [
{
"id": "net0",
"network_id": "0000000-00000-0000-0000-000000000000"
}
]
}`,
},
wantResult: []string{"0000000-00000-0000-0000-000000000000"},
wantCall: true,
},
"http error": {
client: &stubHTTPClient{err: someErr},
wantCall: true,
wantErr: true,
},
"http empty response": {
client: &stubHTTPClient{},
wantCall: true,
wantErr: true,
},
}
for name, tc := range testCases {
t.Run(name, func(t *testing.T) {
assert := assert.New(t)
imds := &imdsClient{
client: tc.client,
networkCache: tc.cache,
networkCacheTime: tc.cacheTime,
}
result, err := imds.networkIDs(context.Background())
if tc.wantErr {
assert.Error(err)
} else {
assert.NoError(err)
assert.Equal(tc.wantResult, result)
if tc.client != nil {
assert.Equal(tc.wantCall, tc.client.called)
}
}
})
}
}
func TestTimeForUpdate(t *testing.T) {
testCases := map[string]struct {
cacheTime time.Time

View File

@ -307,16 +307,6 @@ func (c *Cloud) getLoadBalancerHost(ctx context.Context) (string, error) {
return "", errors.New("no load balancer endpoint found")
}
// GetNetworkIDs returns the IDs of the networks the current instance is part of.
// This method is OpenStack specific.
func (c *Cloud) GetNetworkIDs(ctx context.Context) ([]string, error) {
networkIDs, err := c.imds.networkIDs(ctx)
if err != nil {
return nil, fmt.Errorf("retrieving network IDs: %w", err)
}
return networkIDs, nil
}
func (c *Cloud) getSubnetCIDR(uidTag string) (netip.Prefix, error) {
listSubnetsOpts := subnets.ListOpts{Tags: uidTag}
subnetsPage, err := c.api.ListSubnets(listSubnetsOpts).AllPages()

View File

@ -654,46 +654,6 @@ func TestGetLoadBalancerEndpoint(t *testing.T) {
}
}
func TestGetNetworkIDs(t *testing.T) {
someErr := fmt.Errorf("failed")
testCases := map[string]struct {
imds imdsAPI
want []string
wantErr bool
}{
"success": {
imds: &stubIMDSClient{
networkIDsResult: []string{"id1", "id2"},
},
want: []string{"id1", "id2"},
},
"fail to get network IDs": {
imds: &stubIMDSClient{
networkIDsErr: someErr,
},
wantErr: true,
},
}
for name, tc := range testCases {
t.Run(name, func(t *testing.T) {
assert := assert.New(t)
c := &Cloud{imds: tc.imds}
got, err := c.GetNetworkIDs(context.Background())
if tc.wantErr {
assert.Error(err)
} else {
assert.NoError(err)
assert.Equal(tc.want, got)
}
})
}
}
// newSubnetPager returns a subnet pager as we would get from a ListSubnets.
func newSubnetPager(nets []subnets.Subnet, err error) stubPager {
return stubPager{