Skip to content

Commit

Permalink
Merge pull request #5764 from brightbox/brightbox-provider-additions
Browse files Browse the repository at this point in the history
cluster-autoscaler: support Brightbox image pattern
  • Loading branch information
k8s-ci-robot committed Jun 26, 2023
2 parents 0733535 + f761f83 commit 3248bf3
Show file tree
Hide file tree
Showing 6 changed files with 307 additions and 131 deletions.
2 changes: 1 addition & 1 deletion cluster-autoscaler/cloudprovider/brightbox/Makefile
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
export BUILD_TAGS=brightbox
export REGISTRY=brightbox
export REGISTRY?=brightbox
export GOARCH?=$(shell go env GOARCH)
ifndef TAG
override TAG=dev
Expand Down
4 changes: 2 additions & 2 deletions cluster-autoscaler/cloudprovider/brightbox/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -161,8 +161,8 @@ $ make build

This builds an autoscaler containing only the Brightbox Cloud provider,
tagged as `brightbox/cluster-autoscaler-brightbox:dev`. To build any
other version add a TAG variable
other version add a TAG variable and/or a REGISTRY variable

```
make build TAG=1.1x
make build TAG=1.1x REGISTRY=cr.brightbox.com/acc-xxxxx/<registry-name>
```
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 3248bf3

Please sign in to comment.