Skip to content

Commit 2030ff5

Browse files
authored
Merge pull request #767 from l1b0k/feat/podip_patch
daemon: add flag to control patch podIP annotations
2 parents 1b074fa + 487e0ff commit 2030ff5

File tree

5 files changed

+118
-5
lines changed

5 files changed

+118
-5
lines changed

daemon/builder.go

+3-2
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,8 @@ func (b *NetworkServiceBuilder) LoadGlobalConfig() *NetworkServiceBuilder {
8383
b.err = err
8484
return b
8585
}
86+
globalConfig.Populate()
87+
8688
switch globalConfig.IPStack {
8789
case "ipv4":
8890
b.service.enableIPv4 = true
@@ -99,7 +101,7 @@ func (b *NetworkServiceBuilder) LoadGlobalConfig() *NetworkServiceBuilder {
99101
}
100102

101103
b.service.ipamType = globalConfig.IPAMType
102-
104+
b.service.enablePatchPodIPs = *globalConfig.EnablePatchPodIPs
103105
return b
104106
}
105107

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

148150
b.config = config
149-
150151
return b
151152
}
152153

daemon/builder_test.go

+101
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package daemon
22

33
import (
44
"context"
5+
"os"
56
"testing"
67

78
"github.com/stretchr/testify/assert"
@@ -72,3 +73,103 @@ func TestInitService(t *testing.T) {
7273
})
7374
}
7475
}
76+
77+
func TestNetworkServiceBuilder_LoadGlobalConfig(t *testing.T) {
78+
tmpFile, err := os.CreateTemp("", "config-*.yaml")
79+
if err != nil {
80+
t.Fatalf("Failed to create temp file: %v", err)
81+
}
82+
defer tmpFile.Close()
83+
configContent := `
84+
{
85+
"version": "1",
86+
"max_pool_size": 5,
87+
"min_pool_size": 0,
88+
"credential_path": "/var/addon/token-config",
89+
"ipam_type": "crd"
90+
}`
91+
err = os.WriteFile(tmpFile.Name(), []byte(configContent), os.ModeDir)
92+
assert.NoError(t, err)
93+
builder := &NetworkServiceBuilder{
94+
configFilePath: tmpFile.Name(),
95+
service: &networkService{},
96+
}
97+
builder.LoadGlobalConfig()
98+
assert.True(t, *builder.config.EnablePatchPodIPs)
99+
}
100+
101+
func TestNetworkServiceBuilder_LoadGlobalConfig2(t *testing.T) {
102+
tmpFile, err := os.CreateTemp("", "config-*.yaml")
103+
if err != nil {
104+
t.Fatalf("Failed to create temp file: %v", err)
105+
}
106+
defer tmpFile.Close()
107+
configContent := `
108+
{
109+
"version": "1",
110+
"max_pool_size": 5,
111+
"min_pool_size": 0,
112+
"credential_path": "/var/addon/token-config",
113+
"enable_patch_pod_ips": false,
114+
"ipam_type": "crd"
115+
}`
116+
err = os.WriteFile(tmpFile.Name(), []byte(configContent), os.ModeDir)
117+
assert.NoError(t, err)
118+
builder := &NetworkServiceBuilder{
119+
configFilePath: tmpFile.Name(),
120+
service: &networkService{},
121+
}
122+
builder.LoadGlobalConfig()
123+
assert.False(t, *builder.config.EnablePatchPodIPs)
124+
}
125+
126+
func TestNetworkServiceBuilder_GetConfigFromFileWithMerge_1(t *testing.T) {
127+
tmpFile, err := os.CreateTemp("", "config-*.yaml")
128+
if err != nil {
129+
t.Fatalf("Failed to create temp file: %v", err)
130+
}
131+
defer tmpFile.Close()
132+
configContent := `
133+
{
134+
"version": "1",
135+
"max_pool_size": 5,
136+
"min_pool_size": 0,
137+
"credential_path": "/var/addon/token-config",
138+
"ipam_type": "crd"
139+
}`
140+
141+
dynamicCfg := ""
142+
err = os.WriteFile(tmpFile.Name(), []byte(configContent), os.ModeDir)
143+
assert.NoError(t, err)
144+
config, err := daemon.GetConfigFromFileWithMerge(tmpFile.Name(), []byte(dynamicCfg))
145+
assert.NoError(t, err)
146+
config.Populate()
147+
148+
assert.True(t, *config.EnablePatchPodIPs)
149+
}
150+
151+
func TestNetworkServiceBuilder_GetConfigFromFileWithMerge_2(t *testing.T) {
152+
tmpFile, err := os.CreateTemp("", "config-*.yaml")
153+
if err != nil {
154+
t.Fatalf("Failed to create temp file: %v", err)
155+
}
156+
defer tmpFile.Close()
157+
configContent := `
158+
{
159+
"version": "1",
160+
"max_pool_size": 5,
161+
"min_pool_size": 0,
162+
"credential_path": "/var/addon/token-config",
163+
"enable_patch_pod_ips": false,
164+
"ipam_type": "crd"
165+
}`
166+
167+
dynamicCfg := ""
168+
err = os.WriteFile(tmpFile.Name(), []byte(configContent), os.ModeDir)
169+
assert.NoError(t, err)
170+
config, err := daemon.GetConfigFromFileWithMerge(tmpFile.Name(), []byte(dynamicCfg))
171+
assert.NoError(t, err)
172+
config.Populate()
173+
174+
assert.False(t, *config.EnablePatchPodIPs)
175+
}

daemon/daemon.go

+7-3
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,8 @@ type networkService struct {
7878

7979
gcRulesOnce sync.Once
8080

81+
enablePatchPodIPs bool
82+
8183
rpc.UnimplementedTerwayBackendServer
8284
}
8385

@@ -267,9 +269,11 @@ func (n *networkService) AllocIP(ctx context.Context, r *rpc.AllocIPRequest) (*r
267269
}
268270
}
269271

270-
ips := getPodIPs(netConf)
271-
if len(ips) > 0 {
272-
_ = n.k8s.PatchPodIPInfo(pod, strings.Join(ips, ","))
272+
if n.enablePatchPodIPs {
273+
ips := getPodIPs(netConf)
274+
if len(ips) > 0 {
275+
_ = n.k8s.PatchPodIPInfo(pod, strings.Join(ips, ","))
276+
}
273277
}
274278

275279
// 4. Record resource info

types/daemon/config.go

+6
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ type Config struct {
5858
KubeClientBurst int `json:"kube_client_burst"`
5959
ResourceGroupID string `json:"resource_group_id"`
6060
RateLimit map[string]int `json:"rate_limit"`
61+
EnablePatchPodIPs *bool `json:"enable_patch_pod_ips,omitempty" mod:"default=true"`
6162
}
6263

6364
func (c *Config) GetSecurityGroups() []string {
@@ -97,6 +98,11 @@ func (c *Config) Populate() {
9798
if c.IPStack == "" {
9899
c.IPStack = string(types.IPStackIPv4)
99100
}
101+
102+
if c.EnablePatchPodIPs == nil {
103+
enable := true
104+
c.EnablePatchPodIPs = &enable
105+
}
100106
}
101107

102108
func (c *Config) Validate() error {

types/daemon/config_test.go

+1
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,7 @@ func TestPopulateSetsDefaultValues(t *testing.T) {
152152
assert.Equal(t, 1.0, cfg.EniCapRatio)
153153
assert.Equal(t, VSwitchSelectionPolicyRandom, cfg.VSwitchSelectionPolicy)
154154
assert.Equal(t, string(types.IPStackIPv4), cfg.IPStack)
155+
assert.True(t, *cfg.EnablePatchPodIPs)
155156
}
156157

157158
func TestPopulateDoesNotOverrideExistingValues(t *testing.T) {

0 commit comments

Comments
 (0)