bugfix: 主机分组不可过深,并防止死循环的host分组

Signed-off-by: xuesongzuo@yunify.com <xuesongzuo@yunify.com>

bugfix: 主机分组不可过深,并防止死循环的host分组

Signed-off-by: xuesongzuo@yunify.com <xuesongzuo@yunify.com>

bugfix: 主机分组不可过深,并防止死循环的host分组

Signed-off-by: xuesongzuo@yunify.com <xuesongzuo@yunify.com>

bugfix: 主机分组不可过深,并防止死循环的host分组

Signed-off-by: xuesongzuo@yunify.com <xuesongzuo@yunify.com>
This commit is contained in:
xuesongzuo@yunify.com 2025-12-08 13:04:43 +08:00
parent 2bc0cfaeb3
commit 806aafd75e
2 changed files with 6 additions and 3 deletions

View File

@ -25,7 +25,6 @@ import (
"github.com/cockroachdb/errors"
kkcorev1 "github.com/kubesphere/kubekey/api/core/v1"
"github.com/kubesphere/kubekey/v4/pkg/utils"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/util/json"
@ -34,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.
@ -176,7 +176,8 @@ func hostsInGroup(inv kkcorev1.Inventory, groupName string, groupsGraph *utils.K
for _, cg := range v.Groups {
if groupsGraph != nil {
if groupsGraph.AddEdgeAndCheckCycle(groupName, cg) {
continue
klog.Warningf("group host found cycle by %s -> %s", groupName, cg)
return []string{}
}
}
hosts = CombineSlice(hostsInGroup(inv, cg, groupsGraph), hosts)

View File

@ -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, nil))
assert.ElementsMatch(t, tc.except, hostsInGroup(tc.inventory, tc.groupName, utils.NewKahnGraph()))
})
}
}