-
-
Notifications
You must be signed in to change notification settings - Fork 82
/
helmfile.go
195 lines (165 loc) 路 6.26 KB
/
helmfile.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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
// https://github.com/roboll/helmfile#cli-reference
package exec
import (
"fmt"
c "github.com/cloudposse/atmos/pkg/config"
"github.com/cloudposse/atmos/pkg/utils"
"github.com/fatih/color"
"github.com/pkg/errors"
"github.com/spf13/cobra"
"os"
"path"
)
// ExecuteHelmfile executes helmfile commands
func ExecuteHelmfile(cmd *cobra.Command, args []string) error {
info, err := processConfigAndStacks("helmfile", cmd, args)
if err != nil {
return err
}
if info.NeedHelp == true {
return nil
}
if len(info.Stack) < 1 {
return errors.New("stack must be specified")
}
err = checkHelmfileConfig()
if err != nil {
return err
}
// Check if the component exists as helmfile component
componentPath := path.Join(c.ProcessedConfig.HelmfileDirAbsolutePath, info.ComponentFolderPrefix, info.Component)
componentPathExists, err := utils.IsDirectory(componentPath)
if err != nil || !componentPathExists {
return errors.New(fmt.Sprintf("Component '%s' does not exist in %s",
info.Component,
path.Join(c.ProcessedConfig.HelmfileDirAbsolutePath, info.ComponentFolderPrefix),
))
}
// Write variables to a file
var varFileName string
if len(info.ComponentFolderPrefix) == 0 {
varFileName = fmt.Sprintf("%s/%s/%s-%s.helmfile.vars.yaml",
c.Config.Components.Helmfile.BasePath,
info.Component,
info.ContextPrefix,
info.Component,
)
} else {
varFileName = fmt.Sprintf("%s/%s/%s/%s-%s.helmfile.vars.yaml",
c.Config.Components.Helmfile.BasePath,
info.ComponentFolderPrefix,
info.Component,
info.ContextPrefix,
info.Component,
)
}
color.Cyan("Writing variables to file:")
fmt.Println(varFileName)
err = utils.WriteToFileAsYAML(varFileName, info.ComponentVarsSection, 0644)
if err != nil {
return err
}
// Handle `helmfile deploy` custom command
if info.SubCommand == "deploy" {
info.SubCommand = "sync"
}
context := c.GetContextFromVars(info.ComponentVarsSection)
// Prepare AWS profile
helmAwsProfile := c.ReplaceContextTokens(context, c.Config.Components.Helmfile.HelmAwsProfilePattern)
color.Cyan(fmt.Sprintf("\nUsing AWS_PROFILE=%s\n\n", helmAwsProfile))
// Download kubeconfig by running `aws eks update-kubeconfig`
kubeconfigPath := fmt.Sprintf("%s/%s-kubecfg", c.Config.Components.Helmfile.KubeconfigPath, info.ContextPrefix)
clusterName := c.ReplaceContextTokens(context, c.Config.Components.Helmfile.ClusterNamePattern)
color.Cyan(fmt.Sprintf("Downloading kubeconfig from the cluster '%s' and saving it to %s\n\n", clusterName, kubeconfigPath))
err = execCommand("aws",
[]string{
"--profile",
helmAwsProfile,
"eks",
"update-kubeconfig",
fmt.Sprintf("--name=%s", clusterName),
fmt.Sprintf("--region=%s", context.Region),
fmt.Sprintf("--kubeconfig=%s", kubeconfigPath),
},
componentPath,
nil,
)
if err != nil {
return err
}
// Print command info
color.Cyan("\nCommand info:")
fmt.Println("Helmfile binary: " + info.Command)
fmt.Println("Helmfile command: " + info.SubCommand)
// https://github.com/roboll/helmfile#cli-reference
// atmos helmfile diff echo-server -s tenant1-ue2-dev --global-options "--no-color --namespace=test"
// atmos helmfile diff echo-server -s tenant1-ue2-dev --global-options "--no-color --namespace test"
// atmos helmfile diff echo-server -s tenant1-ue2-dev --global-options="--no-color --namespace=test"
// atmos helmfile diff echo-server -s tenant1-ue2-dev --global-options="--no-color --namespace test"
fmt.Println(fmt.Sprintf("Global options: %v", info.GlobalOptions))
fmt.Println(fmt.Sprintf("Arguments and flags: %v", info.AdditionalArgsAndFlags))
fmt.Println("Component: " + info.ComponentFromArg)
if len(info.BaseComponent) > 0 {
fmt.Println("Base component: " + info.BaseComponent)
}
fmt.Println("Stack: " + info.Stack)
var workingDir string
if len(info.ComponentFolderPrefix) == 0 {
workingDir = path.Join(c.Config.Components.Helmfile.BasePath, info.Component)
} else {
workingDir = path.Join(c.Config.Components.Helmfile.BasePath, info.ComponentFolderPrefix, info.Component)
}
fmt.Println(fmt.Sprintf("Working dir: %s\n\n", workingDir))
varFile := fmt.Sprintf("%s-%s.helmfile.vars.yaml", info.ContextPrefix, info.Component)
// Prepare arguments and flags
allArgsAndFlags := []string{"--state-values-file", varFile}
if info.GlobalOptions != nil && len(info.GlobalOptions) > 0 {
allArgsAndFlags = append(allArgsAndFlags, info.GlobalOptions...)
}
allArgsAndFlags = append(allArgsAndFlags, info.SubCommand)
allArgsAndFlags = append(allArgsAndFlags, info.AdditionalArgsAndFlags...)
// Prepare ENV vars
envVars := append(info.ComponentEnvList, []string{
fmt.Sprintf("AWS_PROFILE=%s", helmAwsProfile),
fmt.Sprintf("KUBECONFIG=%s", kubeconfigPath),
fmt.Sprintf("NAMESPACE=%s", context.Namespace),
fmt.Sprintf("TENANT=%s", context.Tenant),
fmt.Sprintf("ENVIRONMENT=%s", context.Environment),
fmt.Sprintf("STAGE=%s", context.Stage),
fmt.Sprintf("REGION=%s", context.Region),
fmt.Sprintf("STACK=%s", info.Stack),
}...)
color.Cyan("Using ENV vars:\n")
for _, v := range envVars {
fmt.Println(v)
}
err = execCommand(info.Command, allArgsAndFlags, componentPath, envVars)
if err != nil {
return err
}
// Cleanup
err = os.Remove(varFileName)
if err != nil {
color.Yellow("Error deleting helmfile varfile: %s\n", err)
}
return nil
}
func checkHelmfileConfig() error {
if len(c.Config.Components.Helmfile.BasePath) < 1 {
return errors.New("Base path to helmfile components must be provided in 'components.helmfile.base_path' config or " +
"'ATMOS_COMPONENTS_HELMFILE_BASE_PATH' ENV variable")
}
if len(c.Config.Components.Helmfile.KubeconfigPath) < 1 {
return errors.New("Kubeconfig path must be provided in 'components.helmfile.kubeconfig_path' config or " +
"'ATMOS_COMPONENTS_HELMFILE_KUBECONFIG_PATH' ENV variable")
}
if len(c.Config.Components.Helmfile.HelmAwsProfilePattern) < 1 {
return errors.New("Helm AWS profile pattern must be provided in 'components.helmfile.helm_aws_profile_pattern' config or " +
"'ATMOS_COMPONENTS_HELMFILE_HELM_AWS_PROFILE_PATTERN' ENV variable")
}
if len(c.Config.Components.Helmfile.ClusterNamePattern) < 1 {
return errors.New("Cluster name pattern must be provided in 'components.helmfile.cluster_name_pattern' config or " +
"'ATMOS_COMPONENTS_HELMFILE_CLUSTER_NAME_PATTERN' ENV variable")
}
return nil
}