/
overrides.go
125 lines (110 loc) · 5.04 KB
/
overrides.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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
package service
import (
"context"
"fmt"
"strings"
"github.com/hashicorp/go-multierror"
"github.com/pkg/errors"
uuid "github.com/satori/go.uuid"
"github.com/unanet/eve-sch/internal/config"
"github.com/unanet/eve/pkg/eve"
apiv1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
)
func getDeploymentContainerPorts(eveDeployment eve.DeploymentSpec) []interface{} {
var result = make([]interface{}, 0)
// Setup the Service Port
if eveDeployment.GetServicePort() != 0 {
result = append(result, map[string]interface{}{
"name": "http",
"containerPort": int64(eveDeployment.GetServicePort()),
"protocol": string(apiv1.ProtocolTCP),
})
}
// Setup the Metrics Port
if eveDeployment.GetMetricsPort() != 0 {
result = append(result, map[string]interface{}{
"name": "metrics",
"containerPort": int64(eveDeployment.GetMetricsPort()),
"protocol": string(apiv1.ProtocolTCP),
})
}
return result
}
// This needs to support a template syntax and a just a base host entry
// some artifactory registries use a subdomain/host entry while others use path based
// baseArtifactHost = "plainsight.jfrog.io" => plainsight.jfrog.io/docker-int/eve-api-v1:1.0.0
// baseArtifactHost = "unanet-%s.jfrog.io" => unanet-docker-int.jfrog.io/eve-api-v1:1.0.0
func getDockerImageName(artifact *eve.DeployArtifact) string {
baseArtifactHostCfg := config.GetConfig().BaseArtifactHost
if strings.Contains(baseArtifactHostCfg, "%") {
return fmt.Sprintf("%s/%s:%s", fmt.Sprintf(baseArtifactHostCfg, artifact.ArtifactoryFeed), artifact.ArtifactoryPath, artifact.EvalImageTag())
}
return fmt.Sprintf("%s/%s:%s", fmt.Sprintf("%s/%s", baseArtifactHostCfg, artifact.ArtifactoryFeed), artifact.ArtifactoryPath, artifact.EvalImageTag())
}
func defaultContainerEnvVars(deploymentID uuid.UUID, artifact *eve.DeployArtifact) []interface{} {
c := config.GetConfig()
if artifact.Metadata == nil {
artifact.Metadata = make(eve.MetadataField)
}
artifact.Metadata["EVE_CALLBACK_URL"] = fmt.Sprintf("http://eve-sch-v1.%s:%d/callback?id=%s", c.Namespace, c.Port, deploymentID.String())
artifact.Metadata["EVE_IMAGE_NAME"] = getDockerImageName(artifact)
var containerEnvVars = make([]interface{}, 0)
for k, v := range artifact.Metadata {
value, ok := v.(string)
if !ok {
continue
}
containerEnvVars = append(containerEnvVars, map[string]interface{}{
"name": k,
"value": value,
})
}
return containerEnvVars
}
func (s *Scheduler) overrideImagePullSecrets(ctx context.Context, definition *unstructured.Unstructured) error {
// extract spec image pull secret
ips, found, err := unstructured.NestedSlice(definition.Object, "spec", "template", "spec", "imagePullSecrets")
if err != nil || !found || ips == nil {
s.Logger(ctx).Warn("missing definition image pull secrets")
if err := unstructured.SetNestedField(definition.Object, []interface{}{map[string]interface{}{"name": "docker-cfg"}}, "spec", "template", "spec", "imagePullSecrets"); err != nil {
return errors.Wrap(err, "failed to update imagePullSecrets")
}
return nil
}
if err := unstructured.SetNestedField(ips[0].(map[string]interface{}), "docker-cfg", "name"); err != nil {
return errors.Wrap(err, "failed to update image pull secrets with docker-cfg")
}
if err := unstructured.SetNestedField(definition.Object, ips, "spec", "template", "spec", "imagePullSecrets"); err != nil {
return errors.Wrap(err, "failed to update image pull secrets")
}
return nil
}
func overrideContainers(definition *unstructured.Unstructured, eveDeployment eve.DeploymentSpec, plan *eve.NSDeploymentPlan) error {
var defContainerErrs error
// these values should always be set
// base layer overrides so that the client (api/eve) can't try and set them
var baseContainer = map[string]interface{}{
"name": eveDeployment.GetArtifact().ArtifactName,
"image": getDockerImageName(eveDeployment.GetArtifact()),
"imagePullPolicy": string(apiv1.PullAlways),
"ports": getDeploymentContainerPorts(eveDeployment),
"env": defaultContainerEnvVars(plan.DeploymentID, eveDeployment.GetArtifact()),
}
// First try and find/extract the "containers" key in the definition
// if 1 (or more) are found, then use that as the Definition spec (i.e. take what was supplied from eve-api definition)
containers, found, err := unstructured.NestedSlice(definition.Object, "spec", "template", "spec", "containers")
if err != nil || !found || containers == nil || len(containers) == 0 {
containers = []interface{}{baseContainer}
} else {
for k, v := range baseContainer {
if err := unstructured.SetNestedField(containers[0].(map[string]interface{}), v, k); err != nil {
defContainerErrs = multierror.Append(defContainerErrs, errors.Wrap(err, "failed to override base container key val"))
}
}
}
if err := unstructured.SetNestedSlice(definition.Object, containers, "spec", "template", "spec", "containers"); err != nil {
defContainerErrs = multierror.Append(defContainerErrs, errors.Wrap(err, "failed to override container values..."))
}
return defContainerErrs
}