Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add nodepool id & instance charge type labels for node #417

Merged
merged 1 commit into from
Oct 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 32 additions & 2 deletions pkg/controller/node/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,13 @@ const (
MAX_BATCH_NUM = 50
)

const (
ecsTagNodePoolID = "ack.alibabacloud.com/nodepool-id"

LabelNodePoolID = "node.alibabacloud.com/nodepool-id"
LabelInstanceChargeType = "node.alibabacloud.com/instance-charge-type"
)

var ErrNotFound = errors.New("instance not found")

type nodeModifier func(*v1.Node)
Expand Down Expand Up @@ -239,9 +246,8 @@ func setFields(node *v1.Node, ins *prvd.NodeAttribute, cfgRoute bool) {
if node.Spec.ProviderID == "" && ins.InstanceID != "" {
prvdId := fmt.Sprintf("%s.%s", ins.Region, ins.InstanceID)
klog.V(5).Infof(
"node %s,Adding provider id from cloud provider: %s=%s, %s=%s",
"node %s,Adding provider id from cloud provider: %s",
node.Name,
node.Spec.ProviderID,
prvdId,
)
modify := func(n *v1.Node) {
Expand All @@ -250,6 +256,30 @@ func setFields(node *v1.Node, ins *prvd.NodeAttribute, cfgRoute bool) {
modifiers = append(modifiers, modify)
}

if ins.InstanceChargeType != "" {
klog.V(5).Infof(
"node %s, Adding node label from cloud provider: %s=%s",
node.Name,
LabelInstanceChargeType, ins.InstanceChargeType,
)
modify := func(n *v1.Node) {
n.Labels[LabelInstanceChargeType] = ins.InstanceChargeType
}
modifiers = append(modifiers, modify)
}

if nodePoolID, ok := ins.Tags[ecsTagNodePoolID]; ok {
klog.V(5).Infof(
"node %s, Adding node label from cloud provider: %s=%s",
node.Name,
LabelNodePoolID, nodePoolID,
)
modify := func(n *v1.Node) {
n.Labels[LabelNodePoolID] = nodePoolID
}
modifiers = append(modifiers, modify)
}

modifiers = append(modifiers, removeCloudTaints)

if cfgRoute && !helper.HasExcludeLabel(node) {
Expand Down
6 changes: 6 additions & 0 deletions pkg/controller/node/node_controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,12 @@ func TestSyncCloudNode(t *testing.T) {
if region, ok := updatedNode.Labels[v1.LabelTopologyRegion]; !ok || region != vmock.RegionID {
t.Errorf("node label LabelTopologyRegion not equal, expect %s, got %s", vmock.RegionID, region)
}
if nodePoolID, ok := updatedNode.Labels[LabelNodePoolID]; !ok || nodePoolID != vmock.NodePoolID {
t.Errorf("node label LabelNodePoolID not equal, expect %s, got %s", vmock.NodePoolID, nodePoolID)
}
if instanceChargeType, ok := updatedNode.Labels[LabelInstanceChargeType]; !ok || instanceChargeType != vmock.InstanceChargeType {
t.Errorf("node label LabelInstanceChargeType not equal, expect %s, got %s", vmock.InstanceChargeType, instanceChargeType)
}
if len(updatedNode.Status.Addresses) == 0 {
t.Error("node address is empty")
}
Expand Down
34 changes: 24 additions & 10 deletions pkg/provider/alibaba/ecs/ecs.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,12 +58,19 @@ func (e *ECSProvider) ListInstances(ctx context.Context, ids []string) (map[stri
mins[id] = nil
for _, n := range insList {
if strings.Contains(id, n.InstanceId) {
tags := map[string]string{}
for _, tag := range n.Tags.Tag {
tags[tag.TagKey] = tag.TagValue
}

mins[id] = &prvd.NodeAttribute{
InstanceID: n.InstanceId,
InstanceType: n.InstanceType,
Addresses: findAddress(&n),
Zone: n.ZoneId,
Region: n.RegionId,
InstanceID: n.InstanceId,
InstanceType: n.InstanceType,
Addresses: findAddress(&n),
Zone: n.ZoneId,
Region: n.RegionId,
InstanceChargeType: n.InstanceChargeType,
Tags: tags,
}
break
}
Expand Down Expand Up @@ -102,12 +109,19 @@ func (e *ECSProvider) GetInstancesByIP(ctx context.Context, ips []string) (*prvd

ins := resp.Instances.Instance[0]

tags := map[string]string{}
for _, tag := range ins.Tags.Tag {
tags[tag.TagKey] = tag.TagValue
}

return &prvd.NodeAttribute{
InstanceID: ins.InstanceId,
InstanceType: ins.InstanceType,
Addresses: findAddress(&ins),
Zone: ins.ZoneId,
Region: e.auth.Region,
InstanceID: ins.InstanceId,
InstanceType: ins.InstanceType,
Addresses: findAddress(&ins),
Zone: ins.ZoneId,
Region: e.auth.Region,
InstanceChargeType: ins.InstanceChargeType,
Tags: tags,
}, nil
}

Expand Down
13 changes: 8 additions & 5 deletions pkg/provider/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,11 +67,14 @@ type IMetaData interface {

// NodeAttribute node attribute from cloud instance
type NodeAttribute struct {
InstanceID string
Addresses []v1.NodeAddress
InstanceType string
Zone string
Region string
InstanceID string
Addresses []v1.NodeAddress
InstanceType string
Zone string
Region string
NodePool string
InstanceChargeType string
Tags map[string]string
}

type IInstance interface {
Expand Down
20 changes: 14 additions & 6 deletions pkg/provider/vmock/ecs.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,14 @@ type MockECS struct {
var _ prvd.IInstance = &MockECS{}

const (
ZoneID = "cn-hangzhou-a"
RegionID = "cn-hangzhou"
InstanceIP = "192.0.168.68"
InstanceType = "ecs.c6.xlarge"
ZoneID = "cn-hangzhou-a"
RegionID = "cn-hangzhou"
InstanceIP = "192.0.168.68"
InstanceType = "ecs.c6.xlarge"
NodePoolID = "np-123456"
InstanceChargeType = "PostPaid"

tagKeyNodePoolID = "ack.alibabacloud.com/nodepool-id"
)

func (d *MockECS) ListInstances(ctx context.Context, ids []string) (map[string]*prvd.NodeAttribute, error) {
Expand All @@ -39,8 +43,12 @@ func (d *MockECS) ListInstances(ctx context.Context, ids []string) (map[string]*
Address: InstanceIP,
},
},
Zone: ZoneID,
Region: RegionID,
Zone: ZoneID,
Region: RegionID,
InstanceChargeType: InstanceChargeType,
Tags: map[string]string{
tagKeyNodePoolID: NodePoolID,
},
}
}
return mins, nil
Expand Down