From 0c859e8e1f3bf60d4c1e00298da42f75c936630a Mon Sep 17 00:00:00 2001 From: Mr-Mu <14306508+mumuhhh@users.noreply.github.com> Date: Mon, 8 Dec 2025 16:45:56 +0800 Subject: [PATCH] =?UTF-8?q?bugfix:=20=E4=B8=BB=E6=9C=BA=E5=88=86=E7=BB=84?= =?UTF-8?q?=E4=B8=8D=E5=8F=AF=E8=BF=87=E6=B7=B1=EF=BC=8C=E5=B9=B6=E9=98=B2?= =?UTF-8?q?=E6=AD=A2=E6=AD=BB=E5=BE=AA=E7=8E=AF=E7=9A=84host=E5=88=86?= =?UTF-8?q?=E7=BB=84=20(#2886)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * bugfix: 主机分组不可过深,并防止死循环的host分组 * bugfix: 主机分组不可过深,并防止死循环的host分组 Signed-off-by: xuesongzuo@yunify.com bugfix: 主机分组不可过深,并防止死循环的host分组 Signed-off-by: xuesongzuo@yunify.com bugfix: 主机分组不可过深,并防止死循环的host分组 Signed-off-by: xuesongzuo@yunify.com bugfix: 主机分组不可过深,并防止死循环的host分组 Signed-off-by: xuesongzuo@yunify.com --------- Signed-off-by: xuesongzuo@yunify.com Co-authored-by: xuesongzuo@yunify.com --- pkg/variable/helper.go | 16 ++++++++++++---- pkg/variable/helper_test.go | 4 +++- 2 files changed, 15 insertions(+), 5 deletions(-) diff --git a/pkg/variable/helper.go b/pkg/variable/helper.go index be2bafb1..44f1ba70 100644 --- a/pkg/variable/helper.go +++ b/pkg/variable/helper.go @@ -33,6 +33,7 @@ import ( _const "github.com/kubesphere/kubekey/v4/pkg/const" "github.com/kubesphere/kubekey/v4/pkg/converter/tmpl" + "github.com/kubesphere/kubekey/v4/pkg/utils" ) // CombineVariables merge multiple variables into one variable. @@ -149,8 +150,10 @@ func ConvertGroup(inv kkcorev1.Inventory) map[string][]string { groups[_const.VariableGroupsAll] = all + groupsGraph := utils.NewKahnGraph() + for gn := range inv.Spec.Groups { - groups[gn] = hostsInGroup(inv, gn) + groups[gn] = hostsInGroup(inv, gn, groupsGraph) if hosts, ok := groups[gn]; ok { for _, v := range hosts { if slices.Contains(ungrouped, v) { @@ -167,16 +170,21 @@ func ConvertGroup(inv kkcorev1.Inventory) map[string][]string { // hostsInGroup get a host_name slice in a given group // if the given group contains other group. convert other group to host_name slice. -func hostsInGroup(inv kkcorev1.Inventory, groupName string) []string { +func hostsInGroup(inv kkcorev1.Inventory, groupName string, groupsGraph *utils.KahnGraph) []string { if v, ok := inv.Spec.Groups[groupName]; ok { var hosts []string for _, cg := range v.Groups { - hosts = CombineSlice(hostsInGroup(inv, cg), hosts) + if groupsGraph != nil { + if groupsGraph.AddEdgeAndCheckCycle(groupName, cg) { + klog.Warningf("group host found cycle by %s -> %s", groupName, cg) + return []string{} + } + } + hosts = CombineSlice(hostsInGroup(inv, cg, groupsGraph), hosts) } return CombineSlice(hosts, v.Hosts) } - return make([]string, 0) } diff --git a/pkg/variable/helper_test.go b/pkg/variable/helper_test.go index df3b1787..64b8f6bb 100644 --- a/pkg/variable/helper_test.go +++ b/pkg/variable/helper_test.go @@ -22,6 +22,8 @@ import ( kkcorev1 "github.com/kubesphere/kubekey/api/core/v1" "github.com/stretchr/testify/assert" "k8s.io/apimachinery/pkg/runtime" + + "github.com/kubesphere/kubekey/v4/pkg/utils" ) func TestCombineSlice(t *testing.T) { @@ -126,7 +128,7 @@ func TestHostsInGroup(t *testing.T) { for _, tc := range testcases { t.Run(tc.name, func(t *testing.T) { - assert.ElementsMatch(t, tc.except, hostsInGroup(tc.inventory, tc.groupName)) + assert.ElementsMatch(t, tc.except, hostsInGroup(tc.inventory, tc.groupName, utils.NewKahnGraph())) }) } }