Files
wild-cloud/wild-cli/internal/external/talosctl.go

286 lines
6.1 KiB
Go

package external
import (
"context"
"fmt"
)
// TalosctlTool wraps talosctl operations
type TalosctlTool struct {
*BaseTool
endpoints []string
nodes []string
talosconfig string
}
// NewTalosctlTool creates a new talosctl tool wrapper
func NewTalosctlTool() *TalosctlTool {
return &TalosctlTool{
BaseTool: NewBaseTool("talosctl", "talosctl"),
}
}
// SetEndpoints sets the Talos API endpoints
func (t *TalosctlTool) SetEndpoints(endpoints []string) {
t.endpoints = endpoints
}
// SetNodes sets the target nodes
func (t *TalosctlTool) SetNodes(nodes []string) {
t.nodes = nodes
}
// SetTalosconfig sets the talosconfig file path
func (t *TalosctlTool) SetTalosconfig(path string) {
t.talosconfig = path
}
// GenerateConfig generates Talos configuration files
func (t *TalosctlTool) GenerateConfig(ctx context.Context, clusterName, clusterEndpoint, outputDir string) error {
args := []string{
"gen", "config",
clusterName,
clusterEndpoint,
"--output-dir", outputDir,
}
if t.talosconfig != "" {
args = append([]string{"--talosconfig", t.talosconfig}, args...)
}
_, err := t.Execute(ctx, args...)
if err != nil {
return fmt.Errorf("generating config: %w", err)
}
return nil
}
// ApplyConfig applies configuration to nodes
func (t *TalosctlTool) ApplyConfig(ctx context.Context, configFile string, insecure bool) error {
args := []string{"apply-config"}
if insecure {
args = append(args, "--insecure")
}
args = append(args, "--file", configFile)
if len(t.endpoints) > 0 {
args = append(args, "--endpoints")
args = append(args, t.endpoints...)
}
if len(t.nodes) > 0 {
args = append(args, "--nodes")
args = append(args, t.nodes...)
}
if t.talosconfig != "" {
args = append([]string{"--talosconfig", t.talosconfig}, args...)
}
_, err := t.Execute(ctx, args...)
if err != nil {
return fmt.Errorf("applying config: %w", err)
}
return nil
}
// Bootstrap bootstraps the cluster
func (t *TalosctlTool) Bootstrap(ctx context.Context) error {
args := []string{"bootstrap"}
if len(t.endpoints) > 0 {
args = append(args, "--endpoints")
args = append(args, t.endpoints...)
}
if len(t.nodes) > 0 {
args = append(args, "--nodes")
args = append(args, t.nodes...)
}
if t.talosconfig != "" {
args = append([]string{"--talosconfig", t.talosconfig}, args...)
}
_, err := t.Execute(ctx, args...)
if err != nil {
return fmt.Errorf("bootstrapping cluster: %w", err)
}
return nil
}
// Kubeconfig retrieves the kubeconfig
func (t *TalosctlTool) Kubeconfig(ctx context.Context, outputFile string, force bool) error {
args := []string{"kubeconfig"}
if outputFile != "" {
args = append(args, outputFile)
}
if force {
args = append(args, "--force")
}
if len(t.endpoints) > 0 {
args = append(args, "--endpoints")
args = append(args, t.endpoints...)
}
if len(t.nodes) > 0 {
args = append(args, "--nodes")
args = append(args, t.nodes...)
}
if t.talosconfig != "" {
args = append([]string{"--talosconfig", t.talosconfig}, args...)
}
_, err := t.Execute(ctx, args...)
if err != nil {
return fmt.Errorf("getting kubeconfig: %w", err)
}
return nil
}
// Health checks the health of nodes
func (t *TalosctlTool) Health(ctx context.Context) ([]byte, error) {
args := []string{"health"}
if len(t.endpoints) > 0 {
args = append(args, "--endpoints")
args = append(args, t.endpoints...)
}
if t.talosconfig != "" {
args = append([]string{"--talosconfig", t.talosconfig}, args...)
}
output, err := t.Execute(ctx, args...)
if err != nil {
return nil, fmt.Errorf("checking health: %w", err)
}
return output, nil
}
// List lists Talos resources
func (t *TalosctlTool) List(ctx context.Context, resource string) ([]byte, error) {
args := []string{"list", resource}
if len(t.endpoints) > 0 {
args = append(args, "--endpoints")
args = append(args, t.endpoints...)
}
if len(t.nodes) > 0 {
args = append(args, "--nodes")
args = append(args, t.nodes...)
}
if t.talosconfig != "" {
args = append([]string{"--talosconfig", t.talosconfig}, args...)
}
output, err := t.Execute(ctx, args...)
if err != nil {
return nil, fmt.Errorf("listing %s: %w", resource, err)
}
return output, nil
}
// Patch applies patches to node configuration
func (t *TalosctlTool) Patch(ctx context.Context, patchFile string, configType string) error {
args := []string{"patch", configType, "--patch-file", patchFile}
if len(t.endpoints) > 0 {
args = append(args, "--endpoints")
args = append(args, t.endpoints...)
}
if len(t.nodes) > 0 {
args = append(args, "--nodes")
args = append(args, t.nodes...)
}
if t.talosconfig != "" {
args = append([]string{"--talosconfig", t.talosconfig}, args...)
}
_, err := t.Execute(ctx, args...)
if err != nil {
return fmt.Errorf("patching config: %w", err)
}
return nil
}
// Reboot reboots nodes
func (t *TalosctlTool) Reboot(ctx context.Context) error {
args := []string{"reboot"}
if len(t.endpoints) > 0 {
args = append(args, "--endpoints")
args = append(args, t.endpoints...)
}
if len(t.nodes) > 0 {
args = append(args, "--nodes")
args = append(args, t.nodes...)
}
if t.talosconfig != "" {
args = append([]string{"--talosconfig", t.talosconfig}, args...)
}
_, err := t.Execute(ctx, args...)
if err != nil {
return fmt.Errorf("rebooting nodes: %w", err)
}
return nil
}
// GenerateSecrets generates cluster secrets
func (t *TalosctlTool) GenerateSecrets(ctx context.Context) error {
args := []string{"gen", "secrets"}
if t.talosconfig != "" {
args = append([]string{"--talosconfig", t.talosconfig}, args...)
}
_, err := t.Execute(ctx, args...)
if err != nil {
return fmt.Errorf("generating secrets: %w", err)
}
return nil
}
// GenerateConfigWithSecrets generates configuration with existing secrets
func (t *TalosctlTool) GenerateConfigWithSecrets(ctx context.Context, clusterName, clusterEndpoint, secretsFile string) error {
args := []string{
"gen", "config",
"--with-secrets", secretsFile,
clusterName,
clusterEndpoint,
}
if t.talosconfig != "" {
args = append([]string{"--talosconfig", t.talosconfig}, args...)
}
_, err := t.Execute(ctx, args...)
if err != nil {
return fmt.Errorf("generating config with secrets: %w", err)
}
return nil
}