diff --git a/pkg/asset/agent/joiner/clusterinfo.go b/pkg/asset/agent/joiner/clusterinfo.go index 6e981101db..1a3143df10 100644 --- a/pkg/asset/agent/joiner/clusterinfo.go +++ b/pkg/asset/agent/joiner/clusterinfo.go @@ -204,10 +204,13 @@ func (ci *ClusterInfo) retrieveProxy() error { if err != nil { return err } + // Remove any duplicates in noProxy + noProxy := ci.removeDuplicates(proxy.Spec.NoProxy) + ci.Proxy = &types.Proxy{ HTTPProxy: proxy.Spec.HTTPProxy, HTTPSProxy: proxy.Spec.HTTPSProxy, - NoProxy: proxy.Spec.NoProxy, + NoProxy: noProxy, } return nil @@ -688,3 +691,31 @@ func (ci *ClusterInfo) reportResult(ctx context.Context) error { return workflowreport.GetReport(ctx).StageResult(workflow.StageClusterInspection, string(data)) } + +func (ci *ClusterInfo) removeDuplicates(input string) string { + entries := strings.Split(strings.TrimSpace(input), ",") + + duplicates := []string{} + uniqueMap := make(map[string]bool) + var uniqueList []string + for _, entry := range entries { + trimmedEntry := strings.TrimSpace(entry) + + // Check if the entry is not empty and hasn't been added to the map yet + if trimmedEntry == "" { + continue + } + if _, exists := uniqueMap[trimmedEntry]; !exists { + // If it doesn't exist, add it to the map and the unique list + uniqueMap[trimmedEntry] = true + uniqueList = append(uniqueList, trimmedEntry) + } else { + duplicates = append(duplicates, trimmedEntry) + } + } + + if len(duplicates) > 0 { + logrus.Infof("Duplicates detected in noProxy: %v", duplicates) + } + return strings.Join(uniqueList, ",") +} diff --git a/pkg/asset/agent/joiner/clusterinfo_test.go b/pkg/asset/agent/joiner/clusterinfo_test.go index 7b15ffb547..747e903111 100644 --- a/pkg/asset/agent/joiner/clusterinfo_test.go +++ b/pkg/asset/agent/joiner/clusterinfo_test.go @@ -252,6 +252,31 @@ passwd: return clusterInfo }, }, + { + name: "duplicate noProxy entries are removed", + workflow: workflow.AgentWorkflowTypeAddNodes, + objs: func(t *testing.T) ([]runtime.Object, []runtime.Object, []runtime.Object) { + t.Helper() + objs, ocObjs, ocMachineConfigObjs := defaultObjects()(t) + for i, o := range ocObjs { + if proxy, ok := o.(*configv1.Proxy); ok { + proxy.Spec.NoProxy = "172.22.0.0/24,192.168.111.0/24,.ostest.test.metalkube.org,172.30.0.0/16,192.168.111.0/24" + ocObjs[i] = proxy + break + } + } + return objs, ocObjs, ocMachineConfigObjs + }, + overrideExpectedClusterInfo: func(clusterInfo ClusterInfo) ClusterInfo { + t.Helper() + clusterInfo.Proxy = &types.Proxy{ + HTTPProxy: "http://proxy", + HTTPSProxy: "https://proxy", + NoProxy: "172.22.0.0/24,192.168.111.0/24,.ostest.test.metalkube.org,172.30.0.0/16", + } + return clusterInfo + }, + }, } for _, tc := range cases { t.Run(tc.name, func(t *testing.T) {