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

116 lines
3.4 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/.
//go:build integration_cli
package cli
import (
"context"
"fmt"
"regexp"
"strings"
"gopkg.in/yaml.v3"
"github.com/siderolabs/talos/internal/integration/base"
"github.com/siderolabs/talos/pkg/machinery/config/machine"
"github.com/siderolabs/talos/pkg/machinery/resources/cluster"
)
// HealthSuite verifies health command.
type HealthSuite struct {
base.CLISuite
}
// SuiteName ...
func (suite *HealthSuite) SuiteName() string {
return "cli.HealthSuite"
}
// TestClientSideWithExplicitNodes does successful health check run from client-side, providing the explicit set of nodes.
//
//nolint:gocyclo
func (suite *HealthSuite) TestClientSideWithExplicitNodes() {
info := suite.DiscoverNodes(context.TODO())
var args []string
for _, machineType := range []machine.Type{machine.TypeInit, machine.TypeControlPlane, machine.TypeWorker} {
for _, node := range info.NodesByType(machineType) {
switch machineType {
case machine.TypeInit:
args = append(args, "--init-node", node.IPs[0].String())
case machine.TypeControlPlane:
args = append(args, "--control-plane-nodes", node.IPs[0].String())
case machine.TypeWorker:
args = append(args, "--worker-nodes", node.IPs[0].String())
case machine.TypeUnknown:
// skip it
default:
panic(fmt.Sprintf("unexpected machine type: %v", machineType))
}
}
}
suite.testClientSide(args...)
}
// TestClientSideWithDiscovery does a health check run from client-side without explicitly specifying the nodes.
// It verifies that the check still passes, because the nodes get populated by the discovery service.
//
//nolint:gocyclo
func (suite *HealthSuite) TestClientSideWithDiscovery() {
discoveryEnabled, err := suite.isDiscoveryEnabled()
suite.Require().NoError(err)
if !discoveryEnabled {
suite.T().Skipf("skipping test: discovery is not enabled on the cluster")
}
suite.testClientSide()
}
// TestServerSide does successful health check run from server-side.
func (suite *HealthSuite) TestServerSide() {
randomControlPlaneNodeInternalIP := suite.RandomDiscoveredNodeInternalIP(machine.TypeControlPlane)
suite.RunCLI([]string{"health", "--nodes", randomControlPlaneNodeInternalIP},
base.StdoutEmpty(),
base.StderrShouldMatch(regexp.MustCompile(`waiting for all k8s nodes to report ready`)),
)
}
func (suite *HealthSuite) testClientSide(extraArgs ...string) {
args := append([]string{"--server=false"}, extraArgs...)
if suite.K8sEndpoint != "" {
args = append(args, "--k8s-endpoint", strings.Split(suite.K8sEndpoint, ":")[0])
}
suite.RunCLI(append([]string{"health"}, args...),
base.StdoutEmpty(),
base.StderrShouldMatch(regexp.MustCompile(`waiting for all k8s nodes to report ready`)),
)
}
func (suite *HealthSuite) isDiscoveryEnabled() (bool, error) {
temp := struct {
Spec cluster.ConfigSpec `yaml:"spec"`
}{}
randomControlPlaneNodeInternalIP := suite.RandomDiscoveredNodeInternalIP(machine.TypeControlPlane)
stdout, _ := suite.RunCLI([]string{"--nodes", randomControlPlaneNodeInternalIP, "get", "discoveryconfigs", "-oyaml"})
err := yaml.Unmarshal([]byte(stdout), &temp)
if err != nil {
return false, err
}
return temp.Spec.DiscoveryEnabled, nil
}
func init() {
allSuites = append(allSuites, new(HealthSuite))
}