talos/pkg/provision/access/adapter.go
Utku Ozdemir 8d2be5e315
feat: extend node definition used in health checks
Introduce `cluster.NodeInfo` to represent the basic info about a node which can be used in the health checks. This information, where possible, will be populated by the discovery service in following PRs. Part of siderolabs#5554.

Signed-off-by: Utku Ozdemir <utku.ozdemir@siderolabs.com>
2022-06-13 14:13:42 +02:00

87 lines
2.1 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 access
import (
"github.com/talos-systems/talos/pkg/cluster"
"github.com/talos-systems/talos/pkg/machinery/config/types/v1alpha1/machine"
"github.com/talos-systems/talos/pkg/provision"
)
// Adapter provides cluster access via provision.Cluster.
type Adapter struct {
cluster.ConfigClientProvider
cluster.KubernetesClient
cluster.APICrashDumper
cluster.APIBootstrapper
cluster.Info
cluster.ApplyConfigClient
}
type infoWrapper struct {
clusterInfo provision.ClusterInfo
nodes []cluster.NodeInfo
nodesByType map[machine.Type][]cluster.NodeInfo
}
func (wrapper *infoWrapper) Nodes() []cluster.NodeInfo {
return wrapper.nodes
}
func (wrapper *infoWrapper) NodesByType(t machine.Type) []cluster.NodeInfo {
return wrapper.nodesByType[t]
}
// NewAdapter returns ClusterAccess object from Cluster.
func NewAdapter(clusterInfo provision.Cluster, opts ...provision.Option) *Adapter {
options := provision.DefaultOptions()
for _, opt := range opts {
if err := opt(&options); err != nil {
panic(err)
}
}
c := clusterInfo.Info()
nodeInfos, err := cluster.MapProvisionNodeInfosToClusterNodeInfos(c.Nodes)
if err != nil {
panic(err)
}
nodeInfosByType, err := cluster.MapProvisionNodeInfosToNodeInfosByType(c.Nodes)
if err != nil {
panic(err)
}
infoW := &infoWrapper{
clusterInfo: c,
nodes: nodeInfos,
nodesByType: nodeInfosByType,
}
configProvider := cluster.ConfigClientProvider{
DefaultClient: options.TalosClient,
TalosConfig: options.TalosConfig,
}
return &Adapter{
ConfigClientProvider: configProvider,
KubernetesClient: cluster.KubernetesClient{
ClientProvider: &configProvider,
ForceEndpoint: options.ForceEndpoint,
},
APICrashDumper: cluster.APICrashDumper{
ClientProvider: &configProvider,
Info: infoW,
},
APIBootstrapper: cluster.APIBootstrapper{
ClientProvider: &configProvider,
Info: infoW,
},
Info: infoW,
}
}