-
Notifications
You must be signed in to change notification settings - Fork 9
/
pins.go
89 lines (76 loc) · 1.82 KB
/
pins.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
80
81
82
83
84
85
86
87
88
89
// Copyright 2022 Namespace Labs Inc; All rights reserved.
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
package pins
import (
"embed"
"encoding/json"
"fmt"
"io/fs"
"namespacelabs.dev/foundation/internal/fnerrors"
)
var (
//go:embed pins.json
lib embed.FS
)
type versionsJSON struct {
Images map[string]string `json:"images"`
Defaults map[string]string `json:"defaults"`
ServerBases map[string]ServerBase `json:"serverBases"`
}
type ServerBase struct {
Base string `json:"base"`
NonRootUserID *int `json:"nonRootUserId"`
FSGroup *int `json:"fsGroup"`
}
var (
versions versionsJSON
)
func Default(name string) string {
r, err := CheckDefault(name)
if err != nil {
panic(err.Error())
}
return r
}
func Image(image string) string {
r, err := CheckImage(image)
if err != nil {
panic(err.Error())
}
return r
}
func Server(name string) *ServerBase {
srv, ok := versions.ServerBases[name]
if ok {
return &srv
}
return nil
}
func CheckDefault(name string) (string, error) {
image := versions.Defaults[name]
if image == "" {
return "", fnerrors.InternalError("%q is not present in defaults", name)
}
digest := versions.Images[image]
if digest == "" {
return "", fnerrors.InternalError("%q of default %q is not pinned", image, name)
}
return fmt.Sprintf("%s@sha256:%s", image, digest), nil
}
func CheckImage(image string) (string, error) {
digest := versions.Images[image]
if digest == "" {
return "", fnerrors.InternalError("%q is not pinned", image)
}
return fmt.Sprintf("%s@sha256:%s", image, digest), nil
}
func init() {
versionData, err := fs.ReadFile(lib, "pins.json")
if err != nil {
panic(err)
}
if err := json.Unmarshal(versionData, &versions); err != nil {
panic(err)
}
}