Skip to content

Commit b56e8b7

Browse files
committed
fix: support 'List' type manifests
Fixes #7636 This support a `List`-type manifests by unwrapping them into individual objects. Signed-off-by: Andrey Smirnov <andrey.smirnov@siderolabs.com>
1 parent 574d48e commit b56e8b7

File tree

3 files changed

+65
-1
lines changed

3 files changed

+65
-1
lines changed

internal/app/machined/pkg/adapters/k8s/manifest.go

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import (
1313

1414
"github.com/siderolabs/gen/slices"
1515
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
16+
"k8s.io/apimachinery/pkg/runtime"
1617
"k8s.io/apimachinery/pkg/util/yaml"
1718

1819
"github.com/siderolabs/talos/pkg/machinery/resources/k8s"
@@ -32,6 +33,8 @@ type manifest struct {
3233
}
3334

3435
// SetYAML parses manifest from YAML.
36+
//
37+
//nolint:gocyclo
3538
func (a manifest) SetYAML(yamlBytes []byte) error {
3639
a.Manifest.TypedSpec().Items = nil
3740
reader := yaml.NewYAMLReader(bufio.NewReader(bytes.NewReader(yamlBytes)))
@@ -68,7 +71,23 @@ func (a manifest) SetYAML(yamlBytes []byte) error {
6871
return fmt.Errorf("error loading JSON manifest into unstructured: %w", err)
6972
}
7073

71-
a.Manifest.TypedSpec().Items = append(a.Manifest.TypedSpec().Items, k8s.SingleManifest{Object: obj.Object})
74+
// if the manifest is a list, we will unwrap it
75+
if obj.IsList() {
76+
if err = obj.EachListItem(func(item runtime.Object) error {
77+
obj, ok := item.(*unstructured.Unstructured)
78+
if !ok {
79+
return fmt.Errorf("list item is not Unstructured: %T", item)
80+
}
81+
82+
a.Manifest.TypedSpec().Items = append(a.Manifest.TypedSpec().Items, k8s.SingleManifest{Object: obj.Object})
83+
84+
return nil
85+
}); err != nil {
86+
return fmt.Errorf("error unwrapping a List: %w", err)
87+
}
88+
} else {
89+
a.Manifest.TypedSpec().Items = append(a.Manifest.TypedSpec().Items, k8s.SingleManifest{Object: obj.Object})
90+
}
7291
}
7392

7493
return nil

internal/app/machined/pkg/adapters/k8s/manifest_test.go

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
package k8s_test
66

77
import (
8+
_ "embed"
89
"strings"
910
"testing"
1011

@@ -50,3 +51,19 @@ rules:
5051
assert.Len(t, adapter.Objects(), 1)
5152
assert.Equal(t, adapter.Objects()[0].GetKind(), "Policy")
5253
}
54+
55+
//go:embed testdata/list.yaml
56+
var listManifest []byte
57+
58+
func TestManifestSetYAMLList(t *testing.T) {
59+
manifest := k8s.NewManifest(k8s.ControlPlaneNamespaceName, "test")
60+
adapter := k8sadapter.Manifest(manifest)
61+
62+
require.NoError(t, adapter.SetYAML(listManifest))
63+
64+
assert.Len(t, adapter.Objects(), 2)
65+
assert.Equal(t, "ClusterRoleBinding", adapter.Objects()[0].GetKind())
66+
assert.Equal(t, "system:cloud-node-controller", adapter.Objects()[0].GetName())
67+
assert.Equal(t, "ClusterRoleBinding", adapter.Objects()[1].GetKind())
68+
assert.Equal(t, "system:cloud-controller-manager", adapter.Objects()[1].GetName())
69+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
apiVersion: v1
2+
items:
3+
- apiVersion: rbac.authorization.k8s.io/v1
4+
kind: ClusterRoleBinding
5+
metadata:
6+
name: system:cloud-node-controller
7+
roleRef:
8+
apiGroup: rbac.authorization.k8s.io
9+
kind: ClusterRole
10+
name: system:cloud-node-controller
11+
subjects:
12+
- kind: ServiceAccount
13+
name: cloud-node-controller
14+
namespace: kube-system
15+
- apiVersion: rbac.authorization.k8s.io/v1
16+
kind: ClusterRoleBinding
17+
metadata:
18+
name: system:cloud-controller-manager
19+
roleRef:
20+
apiGroup: rbac.authorization.k8s.io
21+
kind: ClusterRole
22+
name: system:cloud-controller-manager
23+
subjects:
24+
- kind: ServiceAccount
25+
name: cloud-controller-manager
26+
namespace: kube-system
27+
kind: List
28+
metadata: {}

0 commit comments

Comments
 (0)