Skip to content

Commit

Permalink
Add support for private registry
Browse files Browse the repository at this point in the history
  • Loading branch information
Sébastien Guilloux committed May 18, 2017
1 parent cbbdbca commit e7d2deb
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 2 deletions.
3 changes: 2 additions & 1 deletion gateway/handlers/functionshandler.go
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ func MakeDeleteFunctionHandler(metricsOptions metrics.MetricOptions, c *client.C
}

// MakeNewFunctionHandler creates a new function (service) inside the swarm network.
func MakeNewFunctionHandler(metricsOptions metrics.MetricOptions, c *client.Client) http.HandlerFunc {
func MakeNewFunctionHandler(metricsOptions metrics.MetricOptions, c *client.Client, encodedRegistryAuth string) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
defer r.Body.Close()
body, _ := ioutil.ReadAll(r.Body)
Expand All @@ -160,6 +160,7 @@ func MakeNewFunctionHandler(metricsOptions metrics.MetricOptions, c *client.Clie
// w.WriteHeader(http.StatusNotImplemented)

options := types.ServiceCreateOptions{}
options.EncodedRegistryAuth = encodedRegistryAuth
spec := makeSpec(&request)

response, err := c.ServiceCreate(context.Background(), spec, options)
Expand Down
32 changes: 31 additions & 1 deletion gateway/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,17 @@ package main

import (
"context"
"encoding/base64"
"encoding/json"
"log"
"net/http"
"os"
"time"

"github.com/Sirupsen/logrus"
faasHandlers "github.com/alexellis/faas/gateway/handlers"
"github.com/alexellis/faas/gateway/metrics"
"github.com/docker/docker/api/types"
"github.com/docker/docker/client"

"fmt"
Expand All @@ -26,12 +30,15 @@ func main() {
if err != nil {
log.Fatal("Error with Docker client.")
}

dockerVersion, err := dockerClient.ServerVersion(context.Background())
if err != nil {
log.Fatal("Error with Docker server.\n", err)
}
log.Printf("API version: %s, %s\n", dockerVersion.APIVersion, dockerVersion.Version)

registryAuth := getEncodedRegistryAuth()

metricsOptions := metrics.BuildMetricsOptions()
metrics.RegisterMetrics(metricsOptions)

Expand All @@ -44,7 +51,7 @@ func main() {

r.HandleFunc("/system/alert", faasHandlers.MakeAlertHandler(dockerClient))
r.HandleFunc("/system/functions", faasHandlers.MakeFunctionReader(metricsOptions, dockerClient)).Methods("GET")
r.HandleFunc("/system/functions", faasHandlers.MakeNewFunctionHandler(metricsOptions, dockerClient)).Methods("POST")
r.HandleFunc("/system/functions", faasHandlers.MakeNewFunctionHandler(metricsOptions, dockerClient, registryAuth)).Methods("POST")
r.HandleFunc("/system/functions", faasHandlers.MakeDeleteFunctionHandler(metricsOptions, dockerClient)).Methods("DELETE")

fs := http.FileServer(http.Dir("./assets/"))
Expand Down Expand Up @@ -75,3 +82,26 @@ func main() {

log.Fatal(s.ListenAndServe())
}

// getEncodedRegistryAuth creates an encoded (json+base64) registry auth string
// from the following environment variables:
// - DOCKER_REGISTRY_HOST
// - DOCKER_REGISTRY_USERNAME
// - DOCKER_REGISTRY_PASSWORD
// An empty string is returned if DOCKER_REGISTRY_HOST is not set
func getEncodedRegistryAuth() string {
var encodedRegistryAuth string
if os.Getenv("DOCKER_REGISTRY_HOST") != "" {
authConfig := types.AuthConfig{
ServerAddress: os.Getenv("DOCKER_REGISTRY_HOST"),
Username: os.Getenv("DOCKER_REGISTRY_USERNAME"),
Password: os.Getenv("DOCKER_REGISTRY_PASSWORD"),
}
jsonAuth, err := json.Marshal(authConfig)
if err != nil {
log.Fatal("Fail to encode auth as JSON", err)
}
encodedRegistryAuth = base64.StdEncoding.EncodeToString(jsonAuth)
}
return encodedRegistryAuth
}

0 comments on commit e7d2deb

Please sign in to comment.