Merge pull request #173 from inercia/inercia/registries_file

[Feature] Add flag for customizing the global registries file (thanks @inercia )
This commit is contained in:
Thorsten Klein 2020-01-22 10:27:05 +01:00 committed by GitHub
commit f865919c6b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
9 changed files with 70 additions and 24 deletions

View File

@ -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"),

View File

@ -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
}

View File

@ -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)

View File

@ -44,6 +44,7 @@ type ClusterSpec struct {
Image string
NodeToPortSpecMap map[string][]string
PortAutoOffset int
RegistriesFile string
RegistryEnabled bool
RegistryName string
RegistryPort int

View File

@ -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)
}

View File

@ -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,
},

View File

@ -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"

View File

@ -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"

View File

@ -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
}