-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathcommon.go
85 lines (78 loc) · 2.3 KB
/
common.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
package agentmanifest
import (
"bytes"
"context"
"encoding/base64"
"io"
"net/url"
"path"
"text/template"
"github.com/glasskube/distr/internal/env"
"github.com/glasskube/distr/internal/resources"
"github.com/glasskube/distr/internal/types"
)
var (
loginEndpoint string
manifestEndpoint string
resourcesEndpoint string
statusEndpoint string
)
func init() {
if u, err := url.Parse(env.Host()); err != nil {
panic(err)
} else {
u = u.JoinPath("api/v1/agent")
loginEndpoint = u.JoinPath("login").String()
manifestEndpoint = u.JoinPath("manifest").String()
resourcesEndpoint = u.JoinPath("resources").String()
statusEndpoint = u.JoinPath("status").String()
}
}
func Get(ctx context.Context, deploymentTarget types.DeploymentTargetWithCreatedBy, secret *string) (io.Reader, error) {
if tmpl, err := getTemplate(deploymentTarget); err != nil {
return nil, err
} else {
var buf bytes.Buffer
return &buf, tmpl.Execute(&buf, getTemplateData(deploymentTarget, secret))
}
}
func getTemplateData(
deploymentTarget types.DeploymentTargetWithCreatedBy,
secret *string,
) map[string]any {
result := map[string]any{
"agentInterval": env.AgentInterval(),
"registryHost": env.RegistryHost(),
"agentDockerConfig": base64.StdEncoding.EncodeToString(env.AgentDockerConfig()),
"agentVersion": deploymentTarget.AgentVersion.Name,
"agentVersionId": deploymentTarget.AgentVersion.ID,
"targetId": deploymentTarget.ID,
"targetSecret": secret,
"loginEndpoint": loginEndpoint,
"manifestEndpoint": manifestEndpoint,
"resourcesEndpoint": resourcesEndpoint,
"statusEndpoint": statusEndpoint,
}
if deploymentTarget.Namespace != nil {
result["targetNamespace"] = *deploymentTarget.Namespace
}
if deploymentTarget.Scope != nil {
result["targetScope"] = *deploymentTarget.Scope
}
return result
}
func getTemplate(deploymentTarget types.DeploymentTargetWithCreatedBy) (*template.Template, error) {
if deploymentTarget.Type == types.DeploymentTypeDocker {
return resources.GetTemplate(path.Join(
"agent/docker",
deploymentTarget.AgentVersion.ComposeFileRevision,
"docker-compose.yaml",
))
} else {
return resources.GetTemplate(path.Join(
"agent/kubernetes",
deploymentTarget.AgentVersion.ManifestFileRevision,
"manifest.yaml.tmpl",
))
}
}