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

Cherry-pick #4836 - Update ionos-cloud SDK and refactor #4853

Merged
Show file tree
Hide file tree
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
15 changes: 15 additions & 0 deletions cluster-autoscaler/cloudprovider/ionoscloud/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,12 @@ make make-image BUILD_TAGS=ionoscloud TAG='<tag>' REGISTRY='<registry>'
make push-image BUILD_TAGS=ionoscloud TAG='<tag>' REGISTRY='<registry>'
```

If you're using [rootless podman](https://github.com/containers/podman/blob/main/docs/tutorials/rootless_tutorial.md), create a symlink to docker and build the image using:

```sh
make make-image BUILD_TAGS=ionoscloud TAG='<tag>' REGISTRY='<registry>' DOCKER_NETWORK=host
```

If you don't have a token, generate one:

```sh
Expand All @@ -32,3 +38,12 @@ Edit [`cluster-autoscaler-standard.yaml`](./examples/cluster-autoscaler-standard
```console
kubectl apply -f examples/cluster-autoscaler-standard.yaml
```

## Development

The unit tests use mocks generated by [mockery](https://github.com/vektra/mockery) v2. To update them run:

```sh
mockery --inpackage --testonly --case snake --boilerplate-file ../../../hack/boilerplate/boilerplate.generatego.txt --name APIClient
mockery --inpackage --testonly --case snake --boilerplate-file ../../../hack/boilerplate/boilerplate.generatego.txt --name IonosCloudManager
```
48 changes: 0 additions & 48 deletions cluster-autoscaler/cloudprovider/ionoscloud/cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@ type IonosCache struct {

nodeGroups map[string]nodeGroupCacheEntry
nodesToNodeGroups map[string]string
instances map[string]cloudprovider.Instance
nodeGroupSizes map[string]int
nodeGroupTargetSizes map[string]int
nodeGroupLockTable map[string]bool
Expand All @@ -52,31 +51,12 @@ func NewIonosCache() *IonosCache {
return &IonosCache{
nodeGroups: map[string]nodeGroupCacheEntry{},
nodesToNodeGroups: map[string]string{},
instances: map[string]cloudprovider.Instance{},
nodeGroupSizes: map[string]int{},
nodeGroupTargetSizes: map[string]int{},
nodeGroupLockTable: map[string]bool{},
}
}

// GetInstancesForNodeGroup returns the list of cached instances a node group.
func (cache *IonosCache) GetInstancesForNodeGroup(id string) []cloudprovider.Instance {
cache.mutex.Lock()
defer cache.mutex.Unlock()

var nodeIds []string
for nodeId, nodeGroupId := range cache.nodesToNodeGroups {
if nodeGroupId == id {
nodeIds = append(nodeIds, nodeId)
}
}
instances := make([]cloudprovider.Instance, len(nodeIds))
for i, id := range nodeIds {
instances[i] = cache.instances[id]
}
return instances
}

// AddNodeGroup adds a node group to the cache.
func (cache *IonosCache) AddNodeGroup(newPool cloudprovider.NodeGroup) {
cache.mutex.Lock()
Expand All @@ -88,7 +68,6 @@ func (cache *IonosCache) AddNodeGroup(newPool cloudprovider.NodeGroup) {
func (cache *IonosCache) removeNodesForNodeGroupNoLock(id string) {
for nodeId, nodeGroupId := range cache.nodesToNodeGroups {
if nodeGroupId == id {
delete(cache.instances, nodeId)
delete(cache.nodesToNodeGroups, nodeId)
}
}
Expand All @@ -103,23 +82,9 @@ func (cache *IonosCache) RemoveInstanceFromCache(id string) {
klog.V(5).Infof("Removed instance %s from cache", id)
nodeGroupId := cache.nodesToNodeGroups[id]
delete(cache.nodesToNodeGroups, id)
delete(cache.instances, id)
cache.updateNodeGroupTimestampNoLock(nodeGroupId)
}

// SetInstancesCache overwrites all cached instances and node group mappings.
func (cache *IonosCache) SetInstancesCache(nodeGroupInstances map[string][]cloudprovider.Instance) {
cache.mutex.Lock()
defer cache.mutex.Unlock()

cache.nodesToNodeGroups = map[string]string{}
cache.instances = map[string]cloudprovider.Instance{}

for id, instances := range nodeGroupInstances {
cache.setInstancesCacheForNodeGroupNoLock(id, instances)
}
}

// SetInstancesCacheForNodeGroup overwrites cached instances and mappings for a node group.
func (cache *IonosCache) SetInstancesCacheForNodeGroup(id string, instances []cloudprovider.Instance) {
cache.mutex.Lock()
Expand All @@ -133,7 +98,6 @@ func (cache *IonosCache) setInstancesCacheForNodeGroupNoLock(id string, instance
for _, instance := range instances {
nodeId := convertToNodeId(instance.Id)
cache.nodesToNodeGroups[nodeId] = id
cache.instances[nodeId] = instance
}
cache.updateNodeGroupTimestampNoLock(id)
}
Expand Down Expand Up @@ -166,18 +130,6 @@ func (cache *IonosCache) GetNodeGroups() []cloudprovider.NodeGroup {
return nodeGroups
}

// GetInstances returns an unsorted list of all cached instances.
func (cache *IonosCache) GetInstances() []cloudprovider.Instance {
cache.mutex.Lock()
defer cache.mutex.Unlock()

instances := make([]cloudprovider.Instance, 0, len(cache.nodesToNodeGroups))
for _, instance := range cache.instances {
instances = append(instances, instance)
}
return instances
}

// GetNodeGroupForNode returns the node group for the given node.
// Returns nil if either the mapping or the node group is not cached.
func (cache *IonosCache) GetNodeGroupForNode(nodeId string) cloudprovider.NodeGroup {
Expand Down
60 changes: 0 additions & 60 deletions cluster-autoscaler/cloudprovider/ionoscloud/cache_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,27 +28,6 @@ func newCacheEntry(data cloudprovider.NodeGroup, ts time.Time) nodeGroupCacheEnt
return nodeGroupCacheEntry{data: data, ts: ts}
}

func TestCache_GetInstancesForNodeGroup(t *testing.T) {
cache := NewIonosCache()
cache.nodesToNodeGroups = map[string]string{
"node-1": "nodepool-1",
"node-2": "nodepool-2",
"node-3": "nodepool-1",
}
cache.instances = map[string]cloudprovider.Instance{
"node-1": {Id: convertToInstanceId("node-1")},
"node-2": {Id: convertToInstanceId("node-2")},
"node-3": {Id: convertToInstanceId("node-3")},
}

expect := []cloudprovider.Instance{
{Id: convertToInstanceId("node-1")},
{Id: convertToInstanceId("node-3")},
}
instances := cache.GetInstancesForNodeGroup("nodepool-1")
require.ElementsMatch(t, expect, instances)
}

func TestCache_AddNodeGroup(t *testing.T) {
cache := NewIonosCache()
require.Empty(t, cache.GetNodeGroups())
Expand All @@ -61,54 +40,27 @@ func TestCache_RemoveInstanceFromCache(t *testing.T) {
cache := NewIonosCache()
cache.nodeGroups["2"] = newCacheEntry(&nodePool{id: "2"}, firstTime)
cache.nodesToNodeGroups["b2"] = "2"
cache.instances["b2"] = newInstance("b2")

require.NotNil(t, cache.GetNodeGroupForNode("b2"))
require.NotEmpty(t, cache.GetInstances())
require.True(t, cache.NodeGroupNeedsRefresh("2"))

cache.RemoveInstanceFromCache("b2")
require.Nil(t, cache.GetNodeGroupForNode("b2"))
require.Empty(t, cache.GetInstances())
require.False(t, cache.NodeGroupNeedsRefresh("2"))
}

func TestCache_SetInstancesCache(t *testing.T) {
cache := NewIonosCache()
cache.nodeGroups["2"] = newCacheEntry(&nodePool{id: "2"}, timeNow())
cache.nodesToNodeGroups["b2"] = "2"
cache.instances["a3"] = newInstance("b2")
nodePoolInstances := map[string][]cloudprovider.Instance{
"1": {newInstance("a1"), newInstance("a2")},
"2": {newInstance("b1")},
}

require.NotNil(t, cache.GetNodeGroupForNode("b2"))
cache.SetInstancesCache(nodePoolInstances)

require.Nil(t, cache.GetNodeGroupForNode("b2"))
require.ElementsMatch(t, []cloudprovider.Instance{
newInstance("a1"), newInstance("a2"), newInstance("b1"),
}, cache.GetInstances())
}

func TestCache_SetInstancesCacheForNodeGroup(t *testing.T) {
cache := NewIonosCache()
cache.AddNodeGroup(&nodePool{id: "1"})
cache.AddNodeGroup(&nodePool{id: "2"})
cache.nodesToNodeGroups["a3"] = "1"
cache.nodesToNodeGroups["b1"] = "2"
cache.instances["a3"] = newInstance("b2")
cache.instances["b1"] = newInstance("b1")
instances := []cloudprovider.Instance{newInstance("a1"), newInstance("a2")}

require.NotNil(t, cache.GetNodeGroupForNode("a3"))
cache.SetInstancesCacheForNodeGroup("1", instances)

require.Nil(t, cache.GetNodeGroupForNode("a3"))
require.ElementsMatch(t, []cloudprovider.Instance{
newInstance("a1"), newInstance("a2"), newInstance("b1"),
}, cache.GetInstances())
}

func TestCache_GetNodeGroupIDs(t *testing.T) {
Expand All @@ -129,18 +81,6 @@ func TestCache_GetNodeGroups(t *testing.T) {
require.ElementsMatch(t, []*nodePool{{id: "1"}, {id: "2"}}, cache.GetNodeGroups())
}

func TestCache_GetInstances(t *testing.T) {
cache := NewIonosCache()
require.Empty(t, cache.GetInstances())
cache.nodesToNodeGroups["a1"] = "1"
cache.nodesToNodeGroups["a2"] = "1"
cache.instances["a1"] = newInstance("a1")
cache.instances["a2"] = newInstance("a2")
require.ElementsMatch(t, []cloudprovider.Instance{
newInstance("a1"), newInstance("a2"),
}, cache.GetInstances())
}

func TestCache_GetNodeGroupForNode(t *testing.T) {
cache := NewIonosCache()
require.Nil(t, cache.GetNodeGroupForNode("a1"))
Expand Down
Loading