From 5111fab6f96cad4dcc1812ee4f007a7d086b5553 Mon Sep 17 00:00:00 2001 From: Alvaro Saurin Date: Tue, 21 Jan 2020 19:06:57 +0100 Subject: [PATCH] Argument for customizing the global registries file Signed-off-by: Alvaro Saurin --- cli/commands.go | 22 ++++++++++++++++++++++ cli/container.go | 4 ++-- cli/registry.go | 33 +++++++++++++++------------------ cli/types.go | 1 + cli/util.go | 6 ++++++ main.go | 4 ++++ tests/01-basic.sh | 1 + tests/02-registry.sh | 11 +++++++---- tests/common.sh | 12 ++++++++++++ 9 files changed, 70 insertions(+), 24 deletions(-) diff --git a/cli/commands.go b/cli/commands.go index a5844dfb..d01d64d9 100644 --- a/cli/commands.go +++ b/cli/commands.go @@ -209,6 +209,27 @@ func CreateCluster(c *cli.Context) error { volumesSpec.DefaultVolumes = append(volumesSpec.DefaultVolumes, fmt.Sprintf("%s:/images", imageVolume.Name)) + /* + * --registry-file + * check if there is a registries file + */ + registriesFile := "" + if c.IsSet("registries-file") { + registriesFile = c.String("registries-file") + if !fileExists(registriesFile) { + log.Fatalf("registries-file %q does not exists", registriesFile) + } + } else { + registriesFile, err = getGlobalRegistriesConfFilename() + if err != nil { + log.Fatal(err) + } + if !fileExists(registriesFile) { + // if the default registries file does not exists, go ahead but do not try to load it + registriesFile = "" + } + } + /* * clusterSpec * Defines, with which specifications, the cluster and the nodes inside should be created @@ -223,6 +244,7 @@ func CreateCluster(c *cli.Context) error { Image: image, NodeToPortSpecMap: portmap, PortAutoOffset: c.Int("port-auto-offset"), + RegistriesFile: registriesFile, RegistryEnabled: c.Bool("enable-registry"), RegistryName: c.String("registry-name"), RegistryPort: c.Int("registry-port"), diff --git a/cli/container.go b/cli/container.go index 64bb0d67..908cbea0 100644 --- a/cli/container.go +++ b/cli/container.go @@ -152,7 +152,7 @@ func createServer(spec *ClusterSpec) (string, error) { } // copy the registry configuration - if spec.RegistryEnabled { + if spec.RegistryEnabled || len(spec.RegistriesFile) > 0 { if err := writeRegistriesConfigInContainer(spec, id); err != nil { return "", err } @@ -250,7 +250,7 @@ func createWorker(spec *ClusterSpec, postfix int) (string, error) { } // copy the registry configuration - if spec.RegistryEnabled { + if spec.RegistryEnabled || len(spec.RegistriesFile) > 0 { if err := writeRegistriesConfigInContainer(spec, id); err != nil { return "", err } diff --git a/cli/registry.go b/cli/registry.go index 49dc3bfe..b8f38f59 100644 --- a/cli/registry.go +++ b/cli/registry.go @@ -4,7 +4,6 @@ import ( "context" "fmt" "io/ioutil" - "os" "path" "time" @@ -73,34 +72,32 @@ func getGlobalRegistriesConfFilename() (string, error) { // writeRegistriesConfigInContainer creates a valid registries configuration file in a container func writeRegistriesConfigInContainer(spec *ClusterSpec, ID string) error { - globalRegistriesConfig, err := getGlobalRegistriesConfFilename() - if err != nil { - return err - } registryInternalAddress := fmt.Sprintf("%s:%d", spec.RegistryName, defaultRegistryPort) registryExternalAddress := fmt.Sprintf("%s:%d", spec.RegistryName, spec.RegistryPort) privRegistries := &Registry{} - // if ~/.k3d/registries.yaml exists, load it - privRegistryFile, err := ioutil.ReadFile(globalRegistriesConfig) - if err != nil { - if !os.IsNotExist(err) { - return err + // load the base registry file + if len(spec.RegistriesFile) > 0 { + log.Printf("Using registries definitions from %q...\n", spec.RegistriesFile) + privRegistryFile, err := ioutil.ReadFile(spec.RegistriesFile) + if err != nil { + return err // the file must exist at this point } - } else { - if err := yaml.Unmarshal([]byte(privRegistryFile), &privRegistries); err != nil { + if err := yaml.Unmarshal(privRegistryFile, &privRegistries); err != nil { return err } } - if len(privRegistries.Mirrors) == 0 { - privRegistries.Mirrors = map[string]Mirror{} - } + if spec.RegistryEnabled { + if len(privRegistries.Mirrors) == 0 { + privRegistries.Mirrors = map[string]Mirror{} + } - // the add the private registry - privRegistries.Mirrors[registryExternalAddress] = Mirror{ - Endpoints: []string{fmt.Sprintf("http://%s", registryInternalAddress)}, + // the add the private registry + privRegistries.Mirrors[registryExternalAddress] = Mirror{ + Endpoints: []string{fmt.Sprintf("http://%s", registryInternalAddress)}, + } } d, err := yaml.Marshal(&privRegistries) diff --git a/cli/types.go b/cli/types.go index 3fe29e2b..a8020835 100644 --- a/cli/types.go +++ b/cli/types.go @@ -44,6 +44,7 @@ type ClusterSpec struct { Image string NodeToPortSpecMap map[string][]string PortAutoOffset int + RegistriesFile string RegistryEnabled bool RegistryName string RegistryPort int diff --git a/cli/util.go b/cli/util.go index 49bef40c..a9fae4bb 100644 --- a/cli/util.go +++ b/cli/util.go @@ -4,6 +4,7 @@ import ( "fmt" "math/rand" "net" + "os" "strconv" "strings" "time" @@ -120,3 +121,8 @@ func parseAPIPort(portSpec string) (*apiPort, error) { return port, nil } + +func fileExists(filename string) bool { + _, err := os.Stat(filename) + return !os.IsNotExist(err) +} diff --git a/main.go b/main.go index 1aaa1605..bab9fcdb 100644 --- a/main.go +++ b/main.go @@ -136,6 +136,10 @@ func main() { Value: defaultRegistryPort, Usage: "Port of the local registry container", }, + cli.StringFlag{ + Name: "registries-file", + Usage: "registries.yaml config file", + }, }, Action: run.CreateCluster, }, diff --git a/tests/01-basic.sh b/tests/01-basic.sh index 71597806..f4421731 100755 --- a/tests/01-basic.sh +++ b/tests/01-basic.sh @@ -21,6 +21,7 @@ $EXE create --wait 60 --name "c2" --api-port 6444 || failed "could not create cl info "Checking we have access to both clusters..." check_k3d_clusters "c1" "c2" || failed "error checking cluster" +dump_registries_yaml_in "c1" "c2" info "Deleting clusters..." $EXE delete --name "c1" || failed "could not delete the cluster c1" diff --git a/tests/02-registry.sh b/tests/02-registry.sh index a18cd64d..78ae1711 100755 --- a/tests/02-registry.sh +++ b/tests/02-registry.sh @@ -13,9 +13,11 @@ REGISTRY_PORT="5000" REGISTRY="$REGISTRY_NAME:$REGISTRY_PORT" TEST_IMAGE="nginx:latest" -check_registry() { - check_url $REGISTRY/v2/_catalog -} +FIXTURES_DIR=$CURR_DIR/fixtures + +# a dummy registries.yaml file +REGISTRIES_YAML=$FIXTURES_DIR/01-registries-empty.yaml + ######################################################################################### @@ -32,9 +34,10 @@ fi info "Creating two clusters (with a registry)..." $EXE create --wait 60 --name "c1" --api-port 6443 --enable-registry || failed "could not create cluster c1" -$EXE create --wait 60 --name "c2" --api-port 6444 --enable-registry || failed "could not create cluster c2" +$EXE create --wait 60 --name "c2" --api-port 6444 --enable-registry --registries-file "$REGISTRIES_YAML" || failed "could not create cluster c2" check_k3d_clusters "c1" "c2" || failed "error checking cluster" +dump_registries_yaml_in "c1" "c2" check_registry || abort "local registry not available at $REGISTRY" passed "Local registry running at $REGISTRY" diff --git a/tests/common.sh b/tests/common.sh index b536cdbb..94009aa9 100755 --- a/tests/common.sh +++ b/tests/common.sh @@ -51,6 +51,13 @@ passed() { fi } +dump_registries_yaml_in() { + for cluster in $@ ; do + info "registries.yaml in cluster $cluster:" + docker exec -t k3d-$cluster-server cat /etc/rancher/k3s/registries.yaml + done +} + # checks that a URL is available, with an optional error message check_url() { command_exists curl || abort "curl is not installed" @@ -71,3 +78,8 @@ check_k3d_clusters() { done return 0 } + +check_registry() { + check_url $REGISTRY/v2/_catalog +} +