Skip to content

Commit fe2ee9b

Browse files
authored
Merge pull request #215 from daimaxiaxie/add-imagefamily
added ImageFamily field
2 parents 6b198ea + dc1ac6e commit fe2ee9b

File tree

18 files changed

+467
-225
lines changed

18 files changed

+467
-225
lines changed

pkg/apis/v1alpha1/ecsnodeclass.go

Lines changed: 52 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ package v1alpha1
1818

1919
import (
2020
"fmt"
21+
"log"
22+
"strings"
2123

2224
"github.com/mitchellh/hashstructure/v2"
2325
"github.com/samber/lo"
@@ -315,15 +317,34 @@ func (in *ECSNodeClass) Hash() string {
315317
})))
316318
}
317319

318-
// ImageFamily If an alias is specified, return alias, or be 'Custom' (enforced via validation).
319-
func (in *ECSNodeClass) ImageFamily() string {
320-
if term, ok := lo.Find(in.Spec.ImageSelectorTerms, func(t ImageSelectorTerm) bool {
321-
return t.Alias != ""
322-
}); ok {
323-
return ImageFamilyFromAlias(term.Alias)
320+
func (in *ECSNodeClass) Alias() *Alias {
321+
term, ok := lo.Find(in.Spec.ImageSelectorTerms, func(term ImageSelectorTerm) bool {
322+
return term.Alias != ""
323+
})
324+
if !ok {
325+
return nil
324326
}
325-
// Unreachable: validation enforces that one of the above conditions must be met
326-
return ""
327+
return NewAlias(term.Alias)
328+
}
329+
330+
type Alias struct {
331+
Family string
332+
Version string
333+
}
334+
335+
const (
336+
AliasVersionLatest = "latest"
337+
)
338+
339+
func NewAlias(item string) *Alias {
340+
return &Alias{
341+
Family: imageFamilyFromAlias(item),
342+
Version: imageVersionFromAlias(item),
343+
}
344+
}
345+
346+
func (a *Alias) String() string {
347+
return fmt.Sprintf("%s@%s", a.Family, a.Version)
327348
}
328349

329350
// ECSNodeClassList contains a list of ECSNodeClass
@@ -334,8 +355,29 @@ type ECSNodeClassList struct {
334355
Items []ECSNodeClass `json:"items"`
335356
}
336357

337-
func ImageFamilyFromAlias(alias string) string {
338-
return alias
358+
func imageFamilyFromAlias(alias string) string {
359+
components := strings.Split(alias, "@")
360+
if len(components) > 2 {
361+
log.Fatalf("failed to parse Image alias %q, invalid format", alias)
362+
}
363+
family, ok := lo.Find([]string{
364+
ImageFamilyAlibabaCloudLinux3,
365+
ImageFamilyContainerOS,
366+
}, func(family string) bool {
367+
return strings.ToLower(family) == components[0]
368+
})
369+
if !ok {
370+
log.Fatalf("%q is an invalid alias family", components[0])
371+
}
372+
return family
373+
}
374+
375+
func imageVersionFromAlias(alias string) string {
376+
components := strings.Split(alias, "@")
377+
if len(components) != 2 {
378+
return AliasVersionLatest
379+
}
380+
return components[1]
339381
}
340382

341383
func (sd *SystemDisk) GetGiBSize() int32 {

pkg/apis/v1alpha1/labels.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@ var (
7070
}
7171
ImageFamilyAlibabaCloudLinux3 = "AlibabaCloudLinux3"
7272
ImageFamilyContainerOS = "ContainerOS"
73+
ImageFamilyCustom = "Custom"
7374
ECSAMDCPUModelValue = "AMD"
7475
ECSIntelCPUModelValue = "Intel"
7576
ResourceNVIDIAGPU corev1.ResourceName = "nvidia.com/gpu"

pkg/apis/v1alpha1/zz_generated.deepcopy.go

Lines changed: 15 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pkg/operator/operator.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ import (
3030

3131
alicache "github.com/cloudpilot-ai/karpenter-provider-alibabacloud/pkg/cache"
3232
"github.com/cloudpilot-ai/karpenter-provider-alibabacloud/pkg/operator/options"
33-
"github.com/cloudpilot-ai/karpenter-provider-alibabacloud/pkg/providers/ack"
33+
"github.com/cloudpilot-ai/karpenter-provider-alibabacloud/pkg/providers/cluster"
3434
"github.com/cloudpilot-ai/karpenter-provider-alibabacloud/pkg/providers/imagefamily"
3535
"github.com/cloudpilot-ai/karpenter-provider-alibabacloud/pkg/providers/instance"
3636
"github.com/cloudpilot-ai/karpenter-provider-alibabacloud/pkg/providers/instancetype"
@@ -81,7 +81,7 @@ func NewOperator(ctx context.Context, operator *operator.Operator) (context.Cont
8181
log.FromContext(ctx).Error(err, "Failed to create ACK client")
8282
os.Exit(1)
8383
}
84-
clusterID := options.FromContext(ctx).ClusterID
84+
8585
region := *ecsClient.RegionId
8686

8787
pricingProvider, err := pricing.NewDefaultProvider(ctx, region)
@@ -93,16 +93,16 @@ func NewOperator(ctx context.Context, operator *operator.Operator) (context.Cont
9393
versionProvider := version.NewDefaultProvider(operator.KubernetesInterface, cache.New(alicache.KubernetesVersionTTL, alicache.DefaultCleanupInterval))
9494
vSwitchProvider := vswitch.NewDefaultProvider(region, vpcClient, cache.New(alicache.DefaultTTL, alicache.DefaultCleanupInterval), cache.New(alicache.AvailableIPAddressTTL, alicache.DefaultCleanupInterval))
9595
securityGroupProvider := securitygroup.NewDefaultProvider(region, ecsClient, cache.New(alicache.DefaultTTL, alicache.DefaultCleanupInterval))
96-
imageProvider := imagefamily.NewDefaultProvider(region, ecsClient, ackClient, versionProvider, cache.New(alicache.DefaultTTL, alicache.DefaultCleanupInterval))
96+
clusterProvider := cluster.NewClusterProvider(ctx, ackClient, region)
97+
imageProvider := imagefamily.NewDefaultProvider(region, ecsClient, clusterProvider, versionProvider, cache.New(alicache.DefaultTTL, alicache.DefaultCleanupInterval))
9798
imageResolver := imagefamily.NewDefaultResolver(region, ecsClient, cache.New(alicache.InstanceTypeAvailableDiskTTL, alicache.DefaultCleanupInterval))
98-
ackProvider := ack.NewDefaultProvider(clusterID, ackClient, cache.New(alicache.ClusterAttachScriptTTL, alicache.DefaultCleanupInterval))
9999

100100
unavailableOfferingsCache := alicache.NewUnavailableOfferings()
101101
instanceTypeProvider := instancetype.NewDefaultProvider(
102102
*ecsClient.RegionId, ecsClient,
103103
cache.New(alicache.InstanceTypesAndZonesTTL, alicache.DefaultCleanupInterval),
104104
unavailableOfferingsCache,
105-
pricingProvider, ackProvider)
105+
pricingProvider, clusterProvider)
106106

107107
instanceProvider := instance.NewDefaultProvider(
108108
ctx,
@@ -111,7 +111,7 @@ func NewOperator(ctx context.Context, operator *operator.Operator) (context.Cont
111111
unavailableOfferingsCache,
112112
imageResolver,
113113
vSwitchProvider,
114-
ackProvider,
114+
clusterProvider,
115115
)
116116

117117
return ctx, &Operator{

pkg/operator/options/options.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ type Options struct {
4141
Interruption bool
4242
TelemetryShare bool
4343
APGCreationQPS int
44+
ClusterType string
4445
}
4546

4647
func (o *Options) AddFlags(fs *coreoptions.FlagSet) {
@@ -52,6 +53,7 @@ func (o *Options) AddFlags(fs *coreoptions.FlagSet) {
5253
fs.BoolVar(&o.Interruption, "interruption", env.WithDefaultBool("INTERRUPTION", true), "Enable interruption handling.")
5354
fs.BoolVar(&o.TelemetryShare, "telemetry-share", env.WithDefaultBool("TELEMETRY_SHARE", true), "Enable telemetry sharing.")
5455
fs.IntVar(&o.APGCreationQPS, "apg-qps", int(env.WithDefaultInt64("APG_CREATION_QPS", 100)), "The QPS limit for creating AutoProvisionGroup.")
56+
fs.StringVar(&o.ClusterType, "cluster-type", env.WithDefaultString("CLUSTER_TYPE", "ACKManaged"), "Type of cluster, which specifies the method to generate userdata. The default is ACKManaged, with an option for Custom configuration.")
5557
}
5658

5759
func (o *Options) Parse(fs *coreoptions.FlagSet, args ...string) error {

0 commit comments

Comments
 (0)