-
Notifications
You must be signed in to change notification settings - Fork 98
/
helm_deployment.go
157 lines (125 loc) · 5.8 KB
/
helm_deployment.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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
package helm_deployment
import (
"encoding/json"
"errors"
"fmt"
"github.com/open-horizon/anax/cli/cliutils"
"github.com/open-horizon/anax/cli/dev"
"github.com/open-horizon/anax/cli/plugin_registry"
"github.com/open-horizon/anax/helm"
"github.com/open-horizon/anax/i18n"
"github.com/open-horizon/rsapss-tool/sign"
"path/filepath"
)
func init() {
plugin_registry.Register("helm", NewHelmDeploymentConfigPlugin())
}
type HelmDeploymentConfigPlugin struct {
}
func NewHelmDeploymentConfigPlugin() plugin_registry.DeploymentConfigPlugin {
return new(HelmDeploymentConfigPlugin)
}
func (p *HelmDeploymentConfigPlugin) Sign(dep map[string]interface{}, keyFilePath string, ctx plugin_registry.PluginContext) (bool, string, string, error) {
// get message printer
msgPrinter := i18n.GetMessagePrinter()
if owned, err := p.Validate(dep); !owned || err != nil {
return owned, "", "", err
}
// Grab the archive file from the deployment config. The archive file might be relative to the
// service definition file.
filePath := dep["chart_archive"].(string)
if filePath = filepath.Clean(filePath); filePath == "." {
return true, "", "", errors.New(msgPrinter.Sprintf("cleaned %v resulted in an empty string.", dep["chart_archive"].(string)))
}
if currentDir, ok := (ctx.Get("currentDir")).(string); !ok {
return true, "", "", errors.New(msgPrinter.Sprintf("plugin context must include 'currentDir' as the current directory of the service definition file"))
} else if !filepath.IsAbs(filePath) {
filePath = filepath.Join(currentDir, filePath)
}
// Get the base 64 encoding of the Helm chart archive, and put it into the deployment config.
if b64, err := helm.ConvertFileToB64String(filePath); err != nil {
return true, "", "", errors.New(msgPrinter.Sprintf("unable to read chart archive %v, error %v", dep["chart_archive"], err))
} else {
dep["chart_archive"] = b64
}
// Stringify and sign the deployment string.
deployment, err := json.Marshal(dep)
if err != nil {
return true, "", "", errors.New(msgPrinter.Sprintf("failed to marshal deployment string %v, error %v", dep, err))
}
depStr := string(deployment)
sig, err := sign.Input(keyFilePath, deployment)
if err != nil {
return true, "", "", errors.New(msgPrinter.Sprintf("problem signing deployment string with %s: %v", keyFilePath, err))
}
return true, depStr, sig, nil
}
// This function does not open the helm chart package contents to try to extract container images.
// This could be done in the future if necessary for this kind of deployment.
func (p *HelmDeploymentConfigPlugin) GetContainerImages(dep interface{}) (bool, []string, error) {
owned, err := p.Validate(dep)
return owned, []string{}, err
}
func (p *HelmDeploymentConfigPlugin) DefaultConfig(imageInfo interface{}) interface{} {
return map[string]interface{}{
"chart_archive": "",
"release_name": "",
}
}
func (p *HelmDeploymentConfigPlugin) Validate(dep interface{}) (bool, error) {
// get message printer
msgPrinter := i18n.GetMessagePrinter()
if dc, ok := dep.(map[string]interface{}); !ok {
return false, nil
} else if c, ok := dc["chart_archive"]; !ok {
return false, nil
} else if r, ok := dc["release_name"]; !ok {
return false, nil
} else if ca, ok := c.(string); !ok {
return true, errors.New(msgPrinter.Sprintf("chart_archive must have a string type value, has %T", c))
} else if rn, ok := r.(string); !ok {
return true, errors.New(msgPrinter.Sprintf("release_name must have a string type value, has %T", r))
} else if len(ca) == 0 || len(rn) == 0 {
return true, errors.New(msgPrinter.Sprintf("chart_archive and release_name must be non-empty strings"))
} else {
return true, nil
}
}
func (p *HelmDeploymentConfigPlugin) StartTest(homeDirectory string, userInputFile string, configFiles []string, configType string, noFSS bool, userCreds string) bool {
// get message printer
msgPrinter := i18n.GetMessagePrinter()
// Run verification before trying to start anything.
dev.ServiceValidate(homeDirectory, userInputFile, configFiles, configType, userCreds)
// Perform the common execution setup.
dir, _, _ := dev.CommonExecutionSetup(homeDirectory, userInputFile, dev.SERVICE_COMMAND, dev.SERVICE_START_COMMAND)
// Get the service definition, so that we can look at the user input variable definitions.
serviceDef, sderr := dev.GetServiceDefinition(dir, dev.SERVICE_DEFINITION_FILE)
if sderr != nil {
cliutils.Fatal(cliutils.CLI_GENERAL_ERROR, fmt.Sprintf("'%v %v' %v", dev.SERVICE_COMMAND, dev.SERVICE_START_COMMAND, sderr))
}
// Now that we have the service def, we can check if we own the deployment config object.
if owned, err := p.Validate(serviceDef.Deployment); !owned || err != nil {
return false
}
cliutils.Fatal(cliutils.CLI_GENERAL_ERROR, msgPrinter.Sprintf("'%v %v' not supported for Helm deployments", dev.SERVICE_COMMAND, dev.SERVICE_START_COMMAND))
// For the compiler
return true
}
func (p *HelmDeploymentConfigPlugin) StopTest(homeDirectory string) bool {
// get message printer
msgPrinter := i18n.GetMessagePrinter()
// Perform the common execution setup.
dir, _, _ := dev.CommonExecutionSetup(homeDirectory, "", dev.SERVICE_COMMAND, dev.SERVICE_START_COMMAND)
// Get the service definition, so that we can look at the user input variable definitions.
serviceDef, sderr := dev.GetServiceDefinition(dir, dev.SERVICE_DEFINITION_FILE)
if sderr != nil {
cliutils.Fatal(cliutils.CLI_GENERAL_ERROR, fmt.Sprintf("'%v %v' %v", dev.SERVICE_COMMAND, dev.SERVICE_START_COMMAND, sderr))
}
// Now that we have the service def, we can check if we own the deployment config object.
if owned, err := p.Validate(serviceDef.Deployment); !owned || err != nil {
return false
}
cliutils.Fatal(cliutils.CLI_GENERAL_ERROR, msgPrinter.Sprintf("'%v %v' not supported for Helm deployments", dev.SERVICE_COMMAND, dev.SERVICE_START_COMMAND))
// For the compiler
return true
}