Skip to content

Commit

Permalink
cluster-autoscaler: support Brightbox image pattern
Browse files Browse the repository at this point in the history
cluster-autoscaler/cloudprovider/brightbox

Allow scaled workers to be built from an image name pattern as well as
an image id. This deals with long running clusters where the official
image is updated with security changes over time.
  • Loading branch information
NeilW committed Jun 8, 2023
1 parent f336239 commit 0634543
Show file tree
Hide file tree
Showing 4 changed files with 304 additions and 128 deletions.
28 changes: 18 additions & 10 deletions cluster-autoscaler/cloudprovider/brightbox/brightbox_node_group.go
Original file line number Diff line number Diff line change
Expand Up @@ -359,24 +359,32 @@ func makeNodeGroupFromAPIDetails(
if mapData["server_group"] == "" {
return nil, cloudprovider.ErrIllegalConfiguration
}
ng := brightboxNodeGroup{
id: mapData["server_group"],
minSize: minSize,
maxSize: maxSize,
Cloud: cloudclient,
}
imageID := mapData["image"]
if !(len(imageID) == 9 && strings.HasPrefix(imageID, "img-")) {
image, err := ng.GetImageByName(imageID)
if err != nil || image == nil {
return nil, cloudprovider.ErrIllegalConfiguration
}
imageID = image.Id
}
userData := mapData["user_data"]
options := &brightbox.ServerOptions{
Image: mapData["image"],
Image: imageID,
Name: &name,
ServerType: mapData["type"],
Zone: mapData["zone"],
UserData: &userData,
ServerGroups: mergeServerGroups(mapData),
}
result := brightboxNodeGroup{
id: mapData["server_group"],
minSize: minSize,
maxSize: maxSize,
serverOptions: options,
Cloud: cloudclient,
}
klog.V(4).Info(result.Debug())
return &result, nil
ng.serverOptions = options
klog.V(4).Info(ng.Debug())
return &ng, nil
}

func mergeServerGroups(data map[string]string) []string {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ import (
"context"
"fmt"
"net/http"
"regexp"
"sort"
"strings"

brightbox "k8s.io/autoscaler/cluster-autoscaler/cloudprovider/brightbox/gobrightbox"
Expand Down Expand Up @@ -166,6 +168,36 @@ func (c *Cloud) GetServerType(identifier string) (*brightbox.ServerType, error)
return client.ServerType(identifier)
}

// GetImageByName obtains the most recent available image that matches the supplied pattern
func (c *Cloud) GetImageByName(name string) (*brightbox.Image, error) {
klog.V(4).Infof("GetImageByName %q", name)
client, err := c.CloudClient()
if err != nil {
return nil, err
}
klog.V(6).Info("GetImageByName compiling regexp")
nameRe, err := regexp.Compile(name)
if err != nil {
return nil, err
}
klog.V(6).Info("GetImageByName retrieving images")
images, err := client.Images()
if err != nil {
return nil, err
}
klog.V(6).Info("GetImageByName filtering images")
filteredImages := filter(
images,
func(i brightbox.Image) bool {
return i.Official &&
i.Status == status.Available &&
nameRe.MatchString(i.Name)
},
)
klog.V(6).Infof("GetImageByName images selected (%+v)", filteredImages)
return mostRecent(filteredImages), nil
}

// GetConfigMaps obtains the list of Config Maps on the account
func (c *Cloud) GetConfigMaps() ([]brightbox.ConfigMap, error) {
klog.V(4).Info("GetConfigMaps")
Expand Down Expand Up @@ -555,3 +587,27 @@ func ErrorIfAcmeNotComplete(acme *brightbox.LoadBalancerAcme) error {
}
return nil
}

// Returns the most recent item out of a slice of items with Dates
// or nil if there are no items
func mostRecent(items []brightbox.Image) *brightbox.Image {
if len(items) == 0 {
return nil
}
sortedItems := items
sort.Slice(items, func(i, j int) bool {
return items[i].CreatedAt.Unix() > items[j].CreatedAt.Unix()
})
return &sortedItems[0]
}

// filter returns a new slice with all elements from the
// input elements for which the provided predicate function returns true.
func filter[T any](input []T, pred func(T) bool) (output []T) {
for _, v := range input {
if pred(v) {
output = append(output, v)
}
}
return output
}
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,9 @@ type CloudAccess interface {
// DestroyCloudIP issues a request to destroy the cloud ip
DestroyCloudIP(identifier string) error

// ConfigMaps retrieves a list of all config maps
Images() ([]brightbox.Image, error)

// ConfigMaps retrieves a list of all config maps
ConfigMaps() ([]brightbox.ConfigMap, error)

Expand Down
Loading

0 comments on commit 0634543

Please sign in to comment.