diff --git a/daemon/builder.go b/daemon/builder.go index c7e6d8b1..b01a7ad9 100644 --- a/daemon/builder.go +++ b/daemon/builder.go @@ -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 @@ -95,7 +97,7 @@ func (b *NetworkServiceBuilder) LoadGlobalConfig() *NetworkServiceBuilder { b.config = globalConfig b.service.ipamType = globalConfig.IPAMType - + b.service.enablePatchPodIPs = *globalConfig.EnablePatchPodIPs return b } diff --git a/daemon/builder_test.go b/daemon/builder_test.go index 12129f7a..0cf3a3ac 100644 --- a/daemon/builder_test.go +++ b/daemon/builder_test.go @@ -2,6 +2,7 @@ package daemon import ( "context" + "os" "testing" "github.com/stretchr/testify/assert" @@ -82,3 +83,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) +} diff --git a/daemon/daemon.go b/daemon/daemon.go index 294588c6..9f11548d 100644 --- a/daemon/daemon.go +++ b/daemon/daemon.go @@ -71,6 +71,8 @@ type networkService struct { gcRulesOnce sync.Once + enablePatchPodIPs bool + rpc.UnimplementedTerwayBackendServer } @@ -261,9 +263,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 diff --git a/types/daemon/config.go b/types/daemon/config.go index 3b307aea..a9e1b489 100644 --- a/types/daemon/config.go +++ b/types/daemon/config.go @@ -58,6 +58,7 @@ type Config struct { KubeClientQPS float32 `json:"kube_client_qps"` KubeClientBurst int `json:"kube_client_burst"` ResourceGroupID string `json:"resource_group_id"` + EnablePatchPodIPs *bool `json:"enable_patch_pod_ips,omitempty" mod:"default=true"` } func (c *Config) GetSecurityGroups() []string { @@ -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 {