Skip to content

Commit

Permalink
daemon: add flag to control patch podIP annotations
Browse files Browse the repository at this point in the history
Signed-off-by: l1b0k <[email protected]>
  • Loading branch information
l1b0k committed Jan 16, 2025
1 parent d4a58b4 commit 487e0ff
Show file tree
Hide file tree
Showing 5 changed files with 118 additions and 5 deletions.
5 changes: 3 additions & 2 deletions daemon/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,8 @@ func (b *NetworkServiceBuilder) LoadGlobalConfig() *NetworkServiceBuilder {
b.err = err
return b
}
globalConfig.Populate()

switch globalConfig.IPStack {
case "ipv4":
b.service.enableIPv4 = true
Expand All @@ -99,7 +101,7 @@ func (b *NetworkServiceBuilder) LoadGlobalConfig() *NetworkServiceBuilder {
}

b.service.ipamType = globalConfig.IPAMType

b.service.enablePatchPodIPs = *globalConfig.EnablePatchPodIPs
return b
}

Expand Down Expand Up @@ -146,7 +148,6 @@ func (b *NetworkServiceBuilder) LoadDynamicConfig() *NetworkServiceBuilder {
serviceLog.Info("got config", "config", fmt.Sprintf("%+v", config))

b.config = config

return b
}

Expand Down
101 changes: 101 additions & 0 deletions daemon/builder_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package daemon

import (
"context"
"os"
"testing"

"github.com/stretchr/testify/assert"
Expand Down Expand Up @@ -72,3 +73,103 @@ func TestInitService(t *testing.T) {
})
}
}

func TestNetworkServiceBuilder_LoadGlobalConfig(t *testing.T) {
tmpFile, err := os.CreateTemp("", "config-*.yaml")
if err != nil {
t.Fatalf("Failed to create temp file: %v", err)
}
defer tmpFile.Close()
configContent := `
{
"version": "1",
"max_pool_size": 5,
"min_pool_size": 0,
"credential_path": "/var/addon/token-config",
"ipam_type": "crd"
}`
err = os.WriteFile(tmpFile.Name(), []byte(configContent), os.ModeDir)
assert.NoError(t, err)
builder := &NetworkServiceBuilder{
configFilePath: tmpFile.Name(),
service: &networkService{},
}
builder.LoadGlobalConfig()
assert.True(t, *builder.config.EnablePatchPodIPs)
}

func TestNetworkServiceBuilder_LoadGlobalConfig2(t *testing.T) {
tmpFile, err := os.CreateTemp("", "config-*.yaml")
if err != nil {
t.Fatalf("Failed to create temp file: %v", err)
}
defer tmpFile.Close()
configContent := `
{
"version": "1",
"max_pool_size": 5,
"min_pool_size": 0,
"credential_path": "/var/addon/token-config",
"enable_patch_pod_ips": false,
"ipam_type": "crd"
}`
err = os.WriteFile(tmpFile.Name(), []byte(configContent), os.ModeDir)
assert.NoError(t, err)
builder := &NetworkServiceBuilder{
configFilePath: tmpFile.Name(),
service: &networkService{},
}
builder.LoadGlobalConfig()
assert.False(t, *builder.config.EnablePatchPodIPs)
}

func TestNetworkServiceBuilder_GetConfigFromFileWithMerge_1(t *testing.T) {
tmpFile, err := os.CreateTemp("", "config-*.yaml")
if err != nil {
t.Fatalf("Failed to create temp file: %v", err)
}
defer tmpFile.Close()
configContent := `
{
"version": "1",
"max_pool_size": 5,
"min_pool_size": 0,
"credential_path": "/var/addon/token-config",
"ipam_type": "crd"
}`

dynamicCfg := ""
err = os.WriteFile(tmpFile.Name(), []byte(configContent), os.ModeDir)
assert.NoError(t, err)
config, err := daemon.GetConfigFromFileWithMerge(tmpFile.Name(), []byte(dynamicCfg))
assert.NoError(t, err)
config.Populate()

assert.True(t, *config.EnablePatchPodIPs)
}

func TestNetworkServiceBuilder_GetConfigFromFileWithMerge_2(t *testing.T) {
tmpFile, err := os.CreateTemp("", "config-*.yaml")
if err != nil {
t.Fatalf("Failed to create temp file: %v", err)
}
defer tmpFile.Close()
configContent := `
{
"version": "1",
"max_pool_size": 5,
"min_pool_size": 0,
"credential_path": "/var/addon/token-config",
"enable_patch_pod_ips": false,
"ipam_type": "crd"
}`

dynamicCfg := ""
err = os.WriteFile(tmpFile.Name(), []byte(configContent), os.ModeDir)
assert.NoError(t, err)
config, err := daemon.GetConfigFromFileWithMerge(tmpFile.Name(), []byte(dynamicCfg))
assert.NoError(t, err)
config.Populate()

assert.False(t, *config.EnablePatchPodIPs)
}
10 changes: 7 additions & 3 deletions daemon/daemon.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,8 @@ type networkService struct {

gcRulesOnce sync.Once

enablePatchPodIPs bool

rpc.UnimplementedTerwayBackendServer
}

Expand Down Expand Up @@ -267,9 +269,11 @@ func (n *networkService) AllocIP(ctx context.Context, r *rpc.AllocIPRequest) (*r
}
}

ips := getPodIPs(netConf)
if len(ips) > 0 {
_ = n.k8s.PatchPodIPInfo(pod, strings.Join(ips, ","))
if n.enablePatchPodIPs {
ips := getPodIPs(netConf)
if len(ips) > 0 {
_ = n.k8s.PatchPodIPInfo(pod, strings.Join(ips, ","))
}
}

// 4. Record resource info
Expand Down
6 changes: 6 additions & 0 deletions types/daemon/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ type Config struct {
KubeClientBurst int `json:"kube_client_burst"`
ResourceGroupID string `json:"resource_group_id"`
RateLimit map[string]int `json:"rate_limit"`
EnablePatchPodIPs *bool `json:"enable_patch_pod_ips,omitempty" mod:"default=true"`
}

func (c *Config) GetSecurityGroups() []string {
Expand Down Expand Up @@ -97,6 +98,11 @@ func (c *Config) Populate() {
if c.IPStack == "" {
c.IPStack = string(types.IPStackIPv4)
}

if c.EnablePatchPodIPs == nil {
enable := true
c.EnablePatchPodIPs = &enable
}
}

func (c *Config) Validate() error {
Expand Down
1 change: 1 addition & 0 deletions types/daemon/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,7 @@ func TestPopulateSetsDefaultValues(t *testing.T) {
assert.Equal(t, 1.0, cfg.EniCapRatio)
assert.Equal(t, VSwitchSelectionPolicyRandom, cfg.VSwitchSelectionPolicy)
assert.Equal(t, string(types.IPStackIPv4), cfg.IPStack)
assert.True(t, *cfg.EnablePatchPodIPs)
}

func TestPopulateDoesNotOverrideExistingValues(t *testing.T) {
Expand Down

0 comments on commit 487e0ff

Please sign in to comment.