Skip to content

Commit

Permalink
refactoring the pool
Browse files Browse the repository at this point in the history
- Allow pod level config to select eni
- EIP feature is removed
  • Loading branch information
l1b0k committed Dec 28, 2023
1 parent 3367320 commit 95bea05
Show file tree
Hide file tree
Showing 87 changed files with 4,974 additions and 7,033 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
.idea
vendor
**/*.exe
.run
88 changes: 41 additions & 47 deletions cmd/terway-cli/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,64 +99,58 @@ func runShow(cmd *cobra.Command, args []string) error {
return err
}

const (
mappingTableHeaderStatus = "Status"
mappingTableHeaderPodName = "Pod Name"
mappingTableHeaderResourceID = "Res ID"
mappingTableHeaderFactoryResourceID = "Factory Res ID"

mappingStringErrorExists = "error exists in mapping"
)

var (
mappingStatus = map[rpc.ResourceMappingType]string{
rpc.ResourceMappingType_MappingTypeNormal: "Normal",
rpc.ResourceMappingType_MappingTypeIdle: "Idle",
rpc.ResourceMappingType_MappingTypeError: "ERROR",
}
)

func runMapping(cmd *cobra.Command, args []string) error {
placeholder := &rpc.Placeholder{}
result, err := client.GetResourceMapping(ctx, placeholder)
if err != nil {
return err
}

tableData := pterm.TableData{
{
mappingTableHeaderStatus,
mappingTableHeaderPodName,
mappingTableHeaderResourceID,
mappingTableHeaderFactoryResourceID,
},
}

for _, v := range result.Info {
clr := pterm.FgDefault
switch v.Type {
case rpc.ResourceMappingType_MappingTypeNormal:
// Idle
if v.PodName == "" {
v.Type = rpc.ResourceMappingType_MappingTypeIdle
clr = pterm.FgLightCyan
}
case rpc.ResourceMappingType_MappingTypeError:
clr = pterm.FgLightRed
err = fmt.Errorf(mappingStringErrorExists)
//
for i, r := range result.Info {
items := []pterm.BulletListItem{
{
Level: 0,
Text: fmt.Sprintf("slot %d", i),
BulletStyle: pterm.NewStyle(pterm.FgRed),
},
{
Level: 1,
Text: r.NetworkInterfaceID,
Bullet: "-",
},
{
Level: 1,
Text: r.MAC,
Bullet: "-",
},
{
Level: 1,
Text: r.Status,
Bullet: "-",
},
{
Level: 1,
Text: fmt.Sprintf("Type %s", r.Type),
Bullet: "-",
},
{
Level: 1,
Text: fmt.Sprintf("InhibitExpireAt %s", r.AllocInhibitExpireAt),
Bullet: "-",
},
}

row := []string{
clr.Sprint(mappingStatus[v.Type]),
clr.Sprint(v.PodName),
clr.Sprint(v.ResourceName),
clr.Sprint(v.FactoryResourceName),
for _, v := range r.Info {
items = append(items, pterm.BulletListItem{
Level: 2,
Text: v,
})
}
tableData = append(tableData, row)
}

if err := pterm.DefaultTable.WithHasHeader().WithData(tableData).Render(); err != nil {
return err
if err := pterm.DefaultBulletList.WithItems(items).Render(); err != nil {
return err
}
}

return err
Expand Down
5 changes: 3 additions & 2 deletions cmd/terway-cli/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ package main
import (
"context"
"fmt"
"log"
"net"
"os"
"time"

"github.com/spf13/cobra"
Expand Down Expand Up @@ -99,6 +99,7 @@ func init() {

func main() {
if err := rootCmd.Execute(); err != nil {
log.Fatalf("terway-cli error: %s", err)
_, _ = fmt.Fprintf(os.Stderr, "%v\n", err)
os.Exit(1)
}
}
9 changes: 8 additions & 1 deletion cmd/terway/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"flag"
"math/rand"
"os"
"strings"
"time"

"github.com/AliyunContainerService/terway/daemon"
Expand Down Expand Up @@ -42,13 +43,19 @@ func main() {
fs.StringVar(&daemonMode, "daemon-mode", "VPC", "terway network mode")
fs.StringVar(&logLevel, "log-level", "info", "terway log level")
fs.StringVar(&readonlyListen, "readonly-listen", utils.NormalizePath(debugSocketPath), "terway readonly listen")
ctrl.RegisterFlags(fs)
klog.InitFlags(fs)

err := fs.Parse(os.Args[1:])
if err != nil {
panic(err)
}
if strings.ToLower(logLevel) == "debug" {
_ = fs.Set("v", "5")
}

ctx := ctrl.SetupSignalHandler()
err = daemon.Run(ctx, utils.NormalizePath(defaultSocketPath), readonlyListen, utils.NormalizePath(defaultConfigPath), daemonMode, logLevel)
err = daemon.Run(ctx, utils.NormalizePath(defaultSocketPath), readonlyListen, utils.NormalizePath(defaultConfigPath), daemonMode)

if err != nil {
klog.Fatal(err)
Expand Down
22 changes: 14 additions & 8 deletions daemon/config.go
Original file line number Diff line number Diff line change
@@ -1,32 +1,38 @@
package daemon

import (
"github.com/AliyunContainerService/terway/pkg/aliyun"
"context"

"github.com/AliyunContainerService/terway/pkg/aliyun/instance"
"github.com/AliyunContainerService/terway/pkg/k8s"
"github.com/AliyunContainerService/terway/pkg/utils"
"github.com/AliyunContainerService/terway/types"
"github.com/AliyunContainerService/terway/types/daemon"
)

// getDynamicConfig returns (config, label, error) specified in node
// ("", "", nil) for no dynamic config for this node
func getDynamicConfig(k8s Kubernetes) (string, string, error) {
func getDynamicConfig(ctx context.Context, k8s k8s.Kubernetes) (string, string, error) {
label := k8s.GetNodeDynamicConfigLabel()
if label == "" {
return "", "", nil
}

cfg, err := k8s.GetDynamicConfigWithName(label)
cfg, err := k8s.GetDynamicConfigWithName(ctx, label)

return cfg, label, err
}

// the actual size for pool is minIdle and maxIdle
func getPoolConfig(cfg *daemon.Config, daemonMode string, limit *aliyun.Limits) (*types.PoolConfig, error) {
func getPoolConfig(cfg *daemon.Config, daemonMode string, limit *instance.Limits) (*types.PoolConfig, error) {

poolConfig := &types.PoolConfig{
SecurityGroupIDs: cfg.GetSecurityGroups(),
VSwitchSelectionPolicy: cfg.VSwitchSelectionPolicy,
DisableSecurityGroupCheck: cfg.DisableSecurityGroupCheck,
BatchSize: 10,
}

if cfg.ENITags == nil {
cfg.ENITags = make(map[string]string)
}
Expand All @@ -39,7 +45,7 @@ func getPoolConfig(cfg *daemon.Config, daemonMode string, limit *aliyun.Limits)
maxMemberENI := 0

switch daemonMode {
case daemonModeVPC, daemonModeENIOnly:
case daemon.ModeVPC, daemon.ModeENIOnly:
maxENI = limit.Adapters
maxENI = int(float64(maxENI)*cfg.EniCapRatio) + cfg.EniCapShift - 1

Expand Down Expand Up @@ -67,12 +73,12 @@ func getPoolConfig(cfg *daemon.Config, daemonMode string, limit *aliyun.Limits)
}

maxMemberENI = limit.MemberAdapterLimit
if cfg.ENICapPolicy == types.ENICapPolicyPreferTrunk {
if cfg.ENICapPolicy == daemon.ENICapPolicyPreferTrunk {
maxMemberENI = limit.MaxMemberAdapterLimit
}

poolConfig.MaxIPPerENI = 1
case daemonModeENIMultiIP:
case daemon.ModeENIMultiIP:
maxENI = limit.Adapters
maxENI = int(float64(maxENI)*cfg.EniCapRatio) + cfg.EniCapShift - 1

Expand Down Expand Up @@ -121,7 +127,7 @@ func getPoolConfig(cfg *daemon.Config, daemonMode string, limit *aliyun.Limits)
}

if requireMeta {
ins := aliyun.GetInstanceMeta()
ins := instance.GetInstanceMeta()
zone := ins.ZoneID
if cfg.VSwitches != nil {
zoneVswitchs, ok := cfg.VSwitches[zone]
Expand Down
10 changes: 5 additions & 5 deletions daemon/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,12 @@ import (

"github.com/stretchr/testify/assert"

"github.com/AliyunContainerService/terway/pkg/aliyun"
"github.com/AliyunContainerService/terway/pkg/aliyun/instance"
"github.com/AliyunContainerService/terway/types/daemon"
)

func init() {
aliyun.Test = true
instance.Test = true
}

func TestGetPoolConfigWithVPCMode(t *testing.T) {
Expand All @@ -20,7 +20,7 @@ func TestGetPoolConfigWithVPCMode(t *testing.T) {
EniCapRatio: 1,
RegionID: "foo",
}
limit := &aliyun.Limits{
limit := &instance.Limits{
Adapters: 10,
MemberAdapterLimit: 5,
}
Expand All @@ -37,7 +37,7 @@ func TestGetPoolConfigWithENIOnlyMode(t *testing.T) {
EniCapRatio: 1,
RegionID: "foo",
}
limit := &aliyun.Limits{
limit := &instance.Limits{
Adapters: 10,
MemberAdapterLimit: 5,
}
Expand All @@ -54,7 +54,7 @@ func TestGetPoolConfigWithENIMultiIPMode(t *testing.T) {
EniCapRatio: 1,
RegionID: "foo",
}
limit := &aliyun.Limits{
limit := &instance.Limits{
Adapters: 10,
IPv4PerAdapter: 5,
MemberAdapterLimit: 5,
Expand Down
23 changes: 0 additions & 23 deletions daemon/context.go

This file was deleted.

Loading

0 comments on commit 95bea05

Please sign in to comment.