allow reading embedded registries.yaml from SimpleConfig file and add a test case

This commit is contained in:
iwilltry42 2021-01-18 16:44:42 +01:00
parent 4e2b1baa62
commit 064f1071de
No known key found for this signature in database
GPG Key ID: 7BA57AD1CFF16110
5 changed files with 34 additions and 13 deletions

View File

@ -27,6 +27,7 @@ import (
"fmt"
"io/ioutil"
"os"
"strings"
"github.com/docker/go-connections/nat"
cliutil "github.com/rancher/k3d/v4/cmd/util" // TODO: move parseapiport to pkg
@ -257,18 +258,27 @@ func TransformSimpleToClusterConfig(ctx context.Context, runtime runtimes.Runtim
if simpleConfig.Registries.Config != "" {
var k3sRegistry *k3s.Registry
registryConfigFile, err := os.Open(simpleConfig.Registries.Config)
if err != nil {
return nil, fmt.Errorf("Failed to open registry config file at %s: %+v", simpleConfig.Registries.Config, err)
}
configBytes, err := ioutil.ReadAll(registryConfigFile)
if err != nil {
return nil, fmt.Errorf("Failed to read registry config file at %s: %+v", registryConfigFile.Name(), err)
if strings.Contains(simpleConfig.Registries.Config, "\n") { // CASE 1: embedded registries.yaml (multiline string)
log.Debugf("Found multiline registries config embedded in SimpleConfig:\n%s", simpleConfig.Registries.Config)
if err := yaml.Unmarshal([]byte(simpleConfig.Registries.Config), &k3sRegistry); err != nil {
return nil, fmt.Errorf("Failed to read embedded registries config: %+v", err)
}
} else { // CASE 2: registries.yaml file referenced by path (single line)
registryConfigFile, err := os.Open(simpleConfig.Registries.Config)
if err != nil {
return nil, fmt.Errorf("Failed to open registry config file at %s: %+v", simpleConfig.Registries.Config, err)
}
configBytes, err := ioutil.ReadAll(registryConfigFile)
if err != nil {
return nil, fmt.Errorf("Failed to read registry config file at %s: %+v", registryConfigFile.Name(), err)
}
if err := yaml.Unmarshal(configBytes, &k3sRegistry); err != nil {
return nil, fmt.Errorf("Failed to read registry configuration: %+v", err)
}
}
if err := yaml.Unmarshal(configBytes, &k3sRegistry); err != nil {
return nil, fmt.Errorf("Failed to read registry configuration: %+v", err)
}
log.Tracef("Registry: read config from input:\n%+v", k3sRegistry)
clusterCreateOpts.Registries.Config = k3sRegistry
}

View File

@ -74,8 +74,9 @@ func FilterNodes(nodes []*k3d.Node, filters []string) ([]*k3d.Node, error) {
// if one of the filters is 'all', we only return this and drop all others
if submatches["group"] == "all" {
// TODO: filterNodes: only log if really more than one is specified
log.Warnf("Node filter 'all' set, but more were specified in '%+v'", filters)
if len(filters) > 1 {
log.Warnf("Node filter 'all' set, but more were specified in '%+v'", filters)
}
return nodes, nil
}

View File

@ -30,6 +30,11 @@ labels:
registries:
create: true
use: []
config: |
mirrors:
"my.company.registry":
endpoint:
- http://my.company.registry:5000
options:
k3d:

View File

@ -168,7 +168,7 @@ wait_for_pod_exec() {
exec_in_node() {
# $1 = container/node name
# $2 = command
docker exec "$1" "$2"
docker exec "$1" sh -c "$2"
}
docker_assert_container_label() {

View File

@ -47,6 +47,11 @@ docker_assert_container_label "k3d-$clustername-server-0" "foo=bar" || failed "E
info "Ensuring, that we have a registry node present"
$EXE node list "k3d-$clustername-registry" || failed "Expected k3d-$clustername-registry to be present"
## merged registries.yaml
info "Ensuring, that the registries.yaml file contains both registries"
exec_in_node "k3d-$clustername-server-0" "cat /etc/rancher/k3s/registries.yaml" | grep -i "my.company.registry" || failed "Expected 'my.company.registry' to be in the /etc/rancher/k3s/registries.yaml"
exec_in_node "k3d-$clustername-server-0" "cat /etc/rancher/k3s/registries.yaml" | grep -i "k3d-$clustername-registry" || failed "Expected 'k3d-$clustername-registry' to be in the /etc/rancher/k3s/registries.yaml"
# Cleanup
info "Deleting cluster $clustername..."