This repository has been archived by the owner on Feb 24, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 14
/
sources.go
79 lines (65 loc) · 2.14 KB
/
sources.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
package main
import (
"context"
"crypto/sha256"
"fmt"
"io/ioutil"
"net/http"
"os"
"path/filepath"
"time"
fluxapi "github.com/fluxcd/flux/pkg/api"
fluxapi_v9 "github.com/fluxcd/flux/pkg/api/v9"
fluxhttp "github.com/fluxcd/flux/pkg/http"
fluxclient "github.com/fluxcd/flux/pkg/http/client"
"github.com/fluxcd/flux/pkg/image"
)
type HookHandler func(s fluxapi.Server, key []byte, w http.ResponseWriter, r *http.Request, config Endpoint)
var Sources = map[string]HookHandler{}
// -- used for all handlers
const timeout = 10 * time.Second
func log(msg ...interface{}) {
fmt.Fprintln(os.Stderr, msg...)
}
// --
func HandlerFromEndpoint(baseDir, apiUrl string, ep Endpoint) (string, http.Handler, error) {
// 1. find the relevant Source (e.g., DockerHub)
sourceHandler, ok := Sources[ep.Source]
if !ok {
return "", nil, fmt.Errorf("unknown source %q, check sources.go for possible values", ep.Source)
}
// 2. load the key so it can be used in the handler, and get the
// digest so it can be used to route to this handler
key, err := ioutil.ReadFile(filepath.Join(baseDir, ep.KeyPath))
if err != nil {
return "", nil, fmt.Errorf("cannot load key from %q: %s", ep.KeyPath, err.Error())
}
sha := sha256.New()
sha.Write(key)
sha.Write([]byte(ep.RegistryHost))
digest := fmt.Sprintf("%x", sha.Sum(nil))
apiClient := fluxclient.New(http.DefaultClient, fluxhttp.NewAPIRouter(), apiUrl, fluxclient.Token(""))
// 3. construct a handler from the above
return digest, http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
sourceHandler(apiClient, key, w, r, ep)
}), nil
}
func doImageNotify(s fluxapi.Server, w http.ResponseWriter, r *http.Request, img string) {
ref, err := image.ParseRef(img)
if err != nil {
http.Error(w, "Cannot parse image in webhook payload", http.StatusBadRequest)
log("could not parse image from hook payload:", img, ":", err.Error())
return
}
change := fluxapi_v9.Change{
Kind: fluxapi_v9.ImageChange,
Source: fluxapi_v9.ImageUpdate{
Name: ref.Name,
},
}
ctx := r.Context()
ctx, cancel := context.WithTimeout(ctx, timeout)
defer cancel()
s.NotifyChange(ctx, change)
w.WriteHeader(http.StatusOK)
}