Skip to content

Commit

Permalink
Merge pull request #5055 from maksim-paskal/cached-requests-to-Hetzne…
Browse files Browse the repository at this point in the history
…r-API

Cache requests to Hetzner Cloud API
  • Loading branch information
k8s-ci-robot authored Aug 9, 2022
2 parents 6a12973 + c7d2f23 commit 23088ae
Show file tree
Hide file tree
Showing 7 changed files with 475 additions and 41 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ func (d *HetznerCloudProvider) Pricing() (cloudprovider.PricingModel, errors.Aut
// GetAvailableMachineTypes get all machine types that can be requested from
// the cloud provider. Implementation optional.
func (d *HetznerCloudProvider) GetAvailableMachineTypes() ([]string, error) {
serverTypes, err := d.manager.client.ServerType.All(d.manager.apiCallContext)
serverTypes, err := d.manager.cachedServerType.getAllServerTypes()
if err != nil {
return nil, err
}
Expand Down
58 changes: 28 additions & 30 deletions cluster-autoscaler/cloudprovider/hetzner/hetzner_manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,17 +42,19 @@ var (
// hetznerManager handles Hetzner communication and data caching of
// node groups
type hetznerManager struct {
client *hcloud.Client
nodeGroups map[string]*hetznerNodeGroup
apiCallContext context.Context
cloudInit string
image *hcloud.Image
sshKey *hcloud.SSHKey
network *hcloud.Network
firewall *hcloud.Firewall
createTimeout time.Duration
publicIPv4 bool
publicIPv6 bool
client *hcloud.Client
nodeGroups map[string]*hetznerNodeGroup
apiCallContext context.Context
cloudInit string
image *hcloud.Image
sshKey *hcloud.SSHKey
network *hcloud.Network
firewall *hcloud.Firewall
createTimeout time.Duration
publicIPv4 bool
publicIPv6 bool
cachedServerType *serverTypeCache
cachedServers *serversCache
}

func newManager() (*hetznerManager, error) {
Expand Down Expand Up @@ -160,17 +162,19 @@ func newManager() (*hetznerManager, error) {
}

m := &hetznerManager{
client: client,
nodeGroups: make(map[string]*hetznerNodeGroup),
cloudInit: string(cloudInit),
image: image,
sshKey: sshKey,
network: network,
firewall: firewall,
createTimeout: createTimeout,
apiCallContext: ctx,
publicIPv4: publicIPv4,
publicIPv6: publicIPv6,
client: client,
nodeGroups: make(map[string]*hetznerNodeGroup),
cloudInit: string(cloudInit),
image: image,
sshKey: sshKey,
network: network,
firewall: firewall,
createTimeout: createTimeout,
apiCallContext: ctx,
publicIPv4: publicIPv4,
publicIPv6: publicIPv6,
cachedServerType: newServerTypeCache(ctx, client),
cachedServers: newServersCache(ctx, client),
}

m.nodeGroups[drainingNodePoolId] = &hetznerNodeGroup{
Expand All @@ -193,13 +197,7 @@ func (m *hetznerManager) Refresh() error {
}

func (m *hetznerManager) allServers(nodeGroup string) ([]*hcloud.Server, error) {
listOptions := hcloud.ListOpts{
PerPage: 50,
LabelSelector: nodeGroupLabel + "=" + nodeGroup,
}

requestOptions := hcloud.ServerListOpts{ListOpts: listOptions}
servers, err := m.client.Server.AllWithOpts(m.apiCallContext, requestOptions)
servers, err := m.cachedServers.getServersByNodeGroupName(nodeGroup)
if err != nil {
return nil, fmt.Errorf("failed to get servers for hcloud: %v", err)
}
Expand Down Expand Up @@ -238,7 +236,7 @@ func (m *hetznerManager) serverForNode(node *apiv1.Node) (*hcloud.Server, error)
nodeIdOrName = node.Name
}

server, _, err := m.client.Server.Get(m.apiCallContext, nodeIdOrName)
server, err := m.cachedServers.getServer(nodeIdOrName)
if err != nil {
return nil, fmt.Errorf("failed to get servers for node %s error: %v", node.Name, err)
}
Expand Down
23 changes: 13 additions & 10 deletions cluster-autoscaler/cloudprovider/hetzner/hetzner_node_group.go
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,11 @@ func (n *hetznerNodeGroup) IncreaseSize(delta int) error {

n.targetSize = targetSize

// create new servers cache
if _, err := n.manager.cachedServers.servers(); err != nil {
klog.Errorf("failed to get servers: %v", err)
}

return nil
}

Expand Down Expand Up @@ -155,6 +160,11 @@ func (n *hetznerNodeGroup) DeleteNodes(nodes []*apiv1.Node) error {
}
waitGroup.Wait()

// create new servers cache
if _, err := n.manager.cachedServers.servers(); err != nil {
klog.Errorf("failed to get servers: %v", err)
}

n.resetTargetSize(-len(nodes))

return nil
Expand Down Expand Up @@ -184,12 +194,7 @@ func (n *hetznerNodeGroup) Debug() string {
// required that Instance objects returned by this method have Id field set.
// Other fields are optional.
func (n *hetznerNodeGroup) Nodes() ([]cloudprovider.Instance, error) {
listOptions := hcloud.ListOpts{
PerPage: 50,
LabelSelector: nodeGroupLabel + "=" + n.id,
}
requestOptions := hcloud.ServerListOpts{ListOpts: listOptions}
servers, err := n.manager.client.Server.AllWithOpts(n.manager.apiCallContext, requestOptions)
servers, err := n.manager.cachedServers.getServersByNodeGroupName(n.id)
if err != nil {
return nil, fmt.Errorf("failed to get servers for hcloud: %v", err)
}
Expand Down Expand Up @@ -317,7 +322,7 @@ func buildNodeGroupLabels(n *hetznerNodeGroup) map[string]string {
}

func getMachineTypeResourceList(m *hetznerManager, instanceType string) (apiv1.ResourceList, error) {
typeInfo, _, err := m.client.ServerType.Get(m.apiCallContext, instanceType)
typeInfo, err := m.cachedServerType.getServerType(instanceType)
if err != nil || typeInfo == nil {
return nil, fmt.Errorf("failed to get machine type %s info error: %v", instanceType, err)
}
Expand All @@ -332,7 +337,7 @@ func getMachineTypeResourceList(m *hetznerManager, instanceType string) (apiv1.R
}

func serverTypeAvailable(manager *hetznerManager, instanceType string, region string) (bool, error) {
serverType, _, err := manager.client.ServerType.Get(manager.apiCallContext, instanceType)
serverType, err := manager.cachedServerType.getServerType(instanceType)
if err != nil {
return false, err
}
Expand Down Expand Up @@ -421,8 +426,6 @@ func waitForServerAction(m *hetznerManager, serverName string, action *hcloud.Ac
case <-time.After(m.createTimeout):
return fmt.Errorf("timeout waiting for server %s", serverName)
}

return nil
}

func (n *hetznerNodeGroup) resetTargetSize(expectedDelta int) {
Expand Down
139 changes: 139 additions & 0 deletions cluster-autoscaler/cloudprovider/hetzner/hetzner_server_type_cache.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,139 @@
/*
Copyright 2019 The Kubernetes Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package hetzner

import (
"context"
"errors"
"sync"
"time"

"k8s.io/apimachinery/pkg/util/rand"
"k8s.io/autoscaler/cluster-autoscaler/cloudprovider/hetzner/hcloud-go/hcloud"
"k8s.io/client-go/tools/cache"
"k8s.io/klog/v2"
"k8s.io/utils/clock"
)

const (
serverTypeCacheKey = "hetzner-server-type-cache"
serverTypeCachedTTL = time.Minute * 10
serverTypeCacheMinTTL = 5
serverTypeCacheMaxTTL = 60
)

type serverTypeCache struct {
cache.Store
mngJitterClock clock.Clock
hcloudClient *hcloud.Client
hcloudClientContext context.Context
}

type serverTypeClock struct {
clock.Clock

jitter bool
sync.RWMutex
}

func (c *serverTypeClock) Since(ts time.Time) time.Duration {
since := time.Since(ts)
c.RLock()
defer c.RUnlock()
if c.jitter {
return since + (time.Second * time.Duration(rand.IntnRange(serverTypeCacheMinTTL, serverTypeCacheMaxTTL)))
}
return since
}

type serverTypeCachedObject struct {
name string
serverTypes []*hcloud.ServerType
}

func newServerTypeCache(ctx context.Context, hcloudClient *hcloud.Client) *serverTypeCache {
jc := &serverTypeClock{}
return newServerTypeCacheWithClock(
ctx,
hcloudClient,
jc,
cache.NewExpirationStore(func(obj interface{}) (s string, e error) {
return obj.(serverTypeCachedObject).name, nil
}, &cache.TTLPolicy{
TTL: serverTypeCachedTTL,
Clock: jc,
}),
)
}

func newServerTypeCacheWithClock(ctx context.Context, hcloudClient *hcloud.Client, jc clock.Clock, store cache.Store) *serverTypeCache {
return &serverTypeCache{
store,
jc,
hcloudClient,
ctx,
}
}

func (m *serverTypeCache) serverTypes() ([]*hcloud.ServerType, error) {
klog.Warning("Fetching server types from Hetzner API")

serverTypes, err := m.hcloudClient.ServerType.All(m.hcloudClientContext)
if err != nil {
return nil, err
}

cacheObject := serverTypeCachedObject{
name: serverTypeCacheKey,
serverTypes: serverTypes,
}

if err := m.Add(cacheObject); err != nil {
return nil, err
}

return serverTypes, nil
}

func (m *serverTypeCache) getAllServerTypes() ([]*hcloud.ServerType, error) {
// List expires old entries
cacheList := m.List()
klog.V(5).Infof("Current serverTypeCache len: %d\n", len(cacheList))

if obj, found, err := m.GetByKey(serverTypeCacheKey); err == nil && found {
foundServerTypes := obj.(serverTypeCachedObject)

return foundServerTypes.serverTypes, nil
}

return m.serverTypes()
}

func (m *serverTypeCache) getServerType(name string) (*hcloud.ServerType, error) {
serverTypes, err := m.getAllServerTypes()
if err != nil {
return nil, err
}

for _, serverType := range serverTypes {
if serverType.Name == name {
return serverType, nil
}
}

return nil, errors.New("server type not found")
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
/*
Copyright 2019 The Kubernetes Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package hetzner

import (
"context"
"testing"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"k8s.io/autoscaler/cluster-autoscaler/cloudprovider/hetzner/hcloud-go/hcloud"
)

func TestServerTypeCache(t *testing.T) {
c := newServerTypeCache(context.Background(), nil)

serverTypes := []*hcloud.ServerType{
{
Name: "test1",
},
{
Name: "test2",
},
}

cacheObject := serverTypeCachedObject{
name: serverTypeCacheKey,
serverTypes: serverTypes,
}

err := c.Add(cacheObject)

require.NoError(t, err)
obj, ok, err := c.GetByKey(serverTypeCacheKey)
require.NoError(t, err)
require.True(t, ok)
assert.Equal(t, serverTypeCacheKey, obj.(serverTypeCachedObject).name)
foundServerTypes := obj.(serverTypeCachedObject).serverTypes
assert.Equal(t, 2, len(foundServerTypes))
assert.Equal(t, "test1", foundServerTypes[0].Name)

foundServerType, err := c.getServerType("test2")
require.NoError(t, err)
assert.Equal(t, "test2", foundServerType.Name)

_, err = c.getServerType("test3")
require.Error(t, err)
}
Loading

0 comments on commit 23088ae

Please sign in to comment.