Skip to content

Commit

Permalink
flex karpenter node-pool configurations
Browse files Browse the repository at this point in the history
Signed-off-by: Mahmoud Gaballah <mahmoud.gaballah@zalando.de>
  • Loading branch information
myaser committed Jun 3, 2024
1 parent 8b237a8 commit e58e3cb
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 2 deletions.
16 changes: 16 additions & 0 deletions api/node_pool.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,11 @@ package api
import (
"strings"

log "github.com/sirupsen/logrus"
"github.com/zalando-incubator/cluster-lifecycle-manager/pkg/kubernetes"
"gopkg.in/yaml.v2"
corev1 "k8s.io/api/core/v1"
v1 "k8s.io/client-go/applyconfigurations/core/v1"
)

// NodePool describes a node pool in a kubernetes cluster.
Expand Down Expand Up @@ -47,3 +50,16 @@ func (np NodePool) Taints() []*corev1.Taint {
}
return taints
}

func (np NodePool) KarpenterRequirements() []v1.NodeSelectorRequirementApplyConfiguration {
conf, exist := np.ConfigItems["requirements"]
if !exist {
return nil
}
var requirements []v1.NodeSelectorRequirementApplyConfiguration
err := yaml.Unmarshal([]byte(conf), &requirements)
if err != nil {
log.Errorf("Error unmarshalling requirements: %v", err)
}
return requirements
}
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ require (
require (
github.com/Masterminds/sprig/v3 v3.2.3
github.com/luci/go-render v0.0.0-20160219211803-9a04cc21af0f
github.com/samber/lo v1.39.0
)

require (
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,8 @@ github.com/sagikazarmark/locafero v0.4.0 h1:HApY1R9zGo4DBgr7dqsTH/JJxLTTsOt7u6ke
github.com/sagikazarmark/locafero v0.4.0/go.mod h1:Pe1W6UlPYUk/+wc/6KFhbORCfqzgYEpgQ3O5fPuL3H4=
github.com/sagikazarmark/slog-shim v0.1.0 h1:diDBnUNK9N/354PgrxMywXnAwEr1QZcOr6gto+ugjYE=
github.com/sagikazarmark/slog-shim v0.1.0/go.mod h1:SrcSrq8aKtyuqEI1uvTDTK1arOWRIczQRv+GVI1AkeQ=
github.com/samber/lo v1.39.0 h1:4gTz1wUhNYLhFSKl6O+8peW0v2F4BCY034GRpU9WnuA=
github.com/samber/lo v1.39.0/go.mod h1:+m/ZKRl6ClXCE2Lgf3MsQlWfh4bn1bz6CXEOxnEXnEA=
github.com/shopspring/decimal v1.2.0/go.mod h1:DKyhrW/HYNuLGql+MJL6WCR6knT2jwCFRcu2hWCYk4o=
github.com/shopspring/decimal v1.3.1 h1:2Usl1nmF/WZucqkFZhnfFYxxxu8LG21F6nPQBE5gKV8=
github.com/shopspring/decimal v1.3.1/go.mod h1:DKyhrW/HYNuLGql+MJL6WCR6knT2jwCFRcu2hWCYk4o=
Expand Down
4 changes: 4 additions & 0 deletions registry/file.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,10 @@ func (r *fileRegistry) ListClusters(_ Filter) ([]*api.Cluster, error) {

for _, cluster := range fileClusters.Clusters {
for _, nodePool := range cluster.NodePools {
if nodePool.Profile == "worker-karpenter" && len(nodePool.InstanceTypes) == 0 {
nodePool.InstanceType = ""
continue
}
if len(nodePool.InstanceTypes) == 0 {
return nil, fmt.Errorf("no instance types for cluster %s, pool %s", cluster.ID, nodePool.Name)
}
Expand Down
10 changes: 8 additions & 2 deletions registry/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"github.com/go-openapi/runtime"
httptransport "github.com/go-openapi/runtime/client"
"github.com/go-openapi/strfmt"
"github.com/samber/lo"

"github.com/zalando-incubator/cluster-lifecycle-manager/api"
apiclient "github.com/zalando-incubator/cluster-lifecycle-manager/pkg/cluster-registry/client"
Expand Down Expand Up @@ -186,13 +187,18 @@ func convertFromClusterModel(cluster *models.Cluster) (*api.Cluster, error) {
// converts a NodePool model generated from the cluster-registry swagger spec
// into an *api.NodePool struct.
func convertFromNodePoolModel(nodePool *models.NodePool) (*api.NodePool, error) {
if len(nodePool.InstanceTypes) == 0 {
var instanceType string
if lo.FromPtr(nodePool.Profile) == "worker-karpenter" && len(nodePool.InstanceTypes) == 0 {
instanceType = ""
} else if len(nodePool.InstanceTypes) == 0 {
return nil, fmt.Errorf("no instance types for pool %s", *nodePool.Name)
} else {
instanceType = nodePool.InstanceTypes[0]
}
return &api.NodePool{
DiscountStrategy: *nodePool.DiscountStrategy,
InstanceTypes: nodePool.InstanceTypes,
InstanceType: nodePool.InstanceTypes[0],
InstanceType: instanceType,
Name: *nodePool.Name,
Profile: *nodePool.Profile,
MinSize: *nodePool.MinSize,
Expand Down

0 comments on commit e58e3cb

Please sign in to comment.