talos/pkg/cluster/provision.go
Andrey Smirnov badbc51e63
refactor: rewrite code to include preliminary support for multi-doc
`config.Container` implements a multi-doc container which implements
both `Container` interface (encoding, validation, etc.), and `Conifg`
interface (accessing parts of the config).

Refactor `generate` and `bundle` packages to support multi-doc, and
provide backwards compatibility.

Implement a first (mostly example) machine config document for
SideroLink API URL.

Many places don't properly support multi-doc yet (e.g. config patches).

Signed-off-by: Andrey Smirnov <andrey.smirnov@talos-systems.com>
2023-05-31 18:38:05 +04:00

64 lines
1.5 KiB
Go

// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at http://mozilla.org/MPL/2.0/.
package cluster
import (
"net/netip"
"github.com/siderolabs/talos/pkg/machinery/config/machine"
"github.com/siderolabs/talos/pkg/provision"
)
// MapProvisionNodeInfosToClusterNodeInfos maps provision.NodeInfos to cluster.NodeInfos.
func MapProvisionNodeInfosToClusterNodeInfos(nodes []provision.NodeInfo) ([]NodeInfo, error) {
result := make([]NodeInfo, len(nodes))
for i, info := range nodes {
clusterNodeInfo, err := toClusterNodeInfo(info)
if err != nil {
return nil, err
}
result[i] = *clusterNodeInfo
}
return result, nil
}
// MapProvisionNodeInfosToNodeInfosByType maps provision.NodeInfos
// to cluster.NodeInfos, grouping them by machine type.
func MapProvisionNodeInfosToNodeInfosByType(nodes []provision.NodeInfo) (map[machine.Type][]NodeInfo, error) {
result := make(map[machine.Type][]NodeInfo)
for _, info := range nodes {
clusterNodeInfo, err := toClusterNodeInfo(info)
if err != nil {
return nil, err
}
result[info.Type] = append(result[info.Type], *clusterNodeInfo)
}
return result, nil
}
func toClusterNodeInfo(info provision.NodeInfo) (*NodeInfo, error) {
ips := make([]netip.Addr, len(info.IPs))
for i, ip := range info.IPs {
parsed, err := netip.ParseAddr(ip.String())
if err != nil {
return nil, err
}
ips[i] = parsed
}
return &NodeInfo{
InternalIP: ips[0],
IPs: ips,
}, nil
}