-
Notifications
You must be signed in to change notification settings - Fork 88
/
license_context.go
95 lines (84 loc) · 2.62 KB
/
license_context.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
90
91
92
93
94
95
package template
import (
"encoding/base64"
"encoding/json"
"fmt"
"strconv"
"text/template"
kotsv1beta1 "github.com/replicatedhq/kots/kotskinds/apis/kots/v1beta1"
)
type licenseCtx struct {
License *kotsv1beta1.License
}
// FuncMap represents the available functions in the licenseCtx.
func (ctx licenseCtx) FuncMap() template.FuncMap {
return template.FuncMap{
"LicenseFieldValue": ctx.licenseFieldValue,
"LicenseDockerCfg": ctx.licenseDockercfg,
}
}
func (ctx licenseCtx) licenseFieldValue(name string) string {
// return "" for a nil license - it's better than an error, which makes the template engine return "" for the full string
if ctx.License == nil {
return ""
}
// Update docs at https://github.com/replicatedhq/kots.io/blob/master/content/reference/template-functions/license-context.md
// when adding new values
switch name {
case "isGitOpsSupported":
return strconv.FormatBool(ctx.License.Spec.IsGitOpsSupported)
case "isIdentityServiceSupported":
return strconv.FormatBool(ctx.License.Spec.IsIdentityServiceSupported)
case "isGeoaxisSupported":
return strconv.FormatBool(ctx.License.Spec.IsGeoaxisSupported)
case "isAirgapSupported":
return strconv.FormatBool(ctx.License.Spec.IsAirgapSupported)
case "licenseType":
return ctx.License.Spec.LicenseType
case "licenseSequence":
return strconv.FormatInt(ctx.License.Spec.LicenseSequence, 10)
case "signature":
return string(ctx.License.Spec.Signature)
case "appSlug":
return ctx.License.Spec.AppSlug
case "channelID":
return ctx.License.Spec.ChannelID
case "channelName":
return ctx.License.Spec.ChannelName
case "customerName":
return ctx.License.Spec.CustomerName
case "endpoint":
return ctx.License.Spec.Endpoint
case "licenseID", "licenseId":
return ctx.License.Spec.LicenseID
default:
entitlement, ok := ctx.License.Spec.Entitlements[name]
if ok {
return fmt.Sprintf("%v", entitlement.Value.Value())
}
return ""
}
}
func (ctx licenseCtx) licenseDockercfg() string {
// return "" for a nil license - it's better than an error, which makes the template engine return "" for the full string
if ctx.License == nil {
return ""
}
auth := fmt.Sprintf("%s:%s", ctx.License.Spec.LicenseID, ctx.License.Spec.LicenseID)
encodedAuth := base64.StdEncoding.EncodeToString([]byte(auth))
dockercfg := map[string]interface{}{
"auths": map[string]interface{}{
"proxy.replicated.com": map[string]string{
"auth": encodedAuth,
},
"registry.replicated.com": map[string]string{
"auth": encodedAuth,
},
},
}
b, err := json.Marshal(dockercfg)
if err != nil {
return ""
}
return base64.StdEncoding.EncodeToString(b)
}