-
Notifications
You must be signed in to change notification settings - Fork 447
/
Copy pathKubernetesDeployAction.cs
303 lines (278 loc) · 15.3 KB
/
KubernetesDeployAction.cs
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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
using Azure.Functions.Cli.Common;
using Azure.Functions.Cli.Helpers;
using Azure.Functions.Cli.Interfaces;
using Azure.Functions.Cli.Kubernetes;
using Azure.Functions.Cli.Kubernetes.KEDA;
using Azure.Functions.Cli.Kubernetes.Models;
using Azure.Functions.Cli.Kubernetes.Models.Kubernetes;
using Colors.Net;
using Fclp;
using Microsoft.Extensions.FileSystemGlobbing;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
namespace Azure.Functions.Cli.Actions.KubernetesActions
{
[Action(Name = "deploy", Context = Context.Kubernetes, HelpText = "")]
class KubernetesDeployAction : BaseAction
{
private readonly ISecretsManager _secretsManager;
public string Registry { get; set; }
public string Name { get; set; } = string.Empty;
public string Namespace { get; set; }
public string PullSecret { get; set; } = string.Empty;
public bool NoDocker { get; set; }
public bool UseConfigMap { get; set; }
public bool DryRun { get; private set; }
public string ImageName { get; private set; }
public string ConfigMapName { get; private set; }
public string SecretsCollectionName { get; private set; }
public string KeysSecretCollectionName { get; private set; }
public bool MountFuncKeysAsContainerVolume { get; private set; }
public int? PollingInterval { get; private set; }
public int? CooldownPeriod { get; private set; }
public string ServiceType { get; set; } = "LoadBalancer";
public IEnumerable<string> ServiceTypes { get; set; } = new string[] { "ClusterIP", "NodePort", "LoadBalancer" };
public bool IgnoreErrors { get; private set; } = false;
public int? MaxReplicaCount { get; private set; }
public int? MinReplicaCount { get; private set; }
public KedaVersion? KedaVersion { get; private set; } = Kubernetes.KEDA.KedaVersion.v2;
public bool ShowServiceFqdn { get; set; } = false;
public bool WriteConfigs { get; set; } = false;
public string ConfigFile { get; set; } = "functions.yaml";
public bool UseGitHashAsImageVersion { get; set; } = false;
public string HashFilesPattern { get; set; } = "";
public bool BuildImage { get; set; } = true;
public IDictionary<string, string> KeysSecretAnnotations { get; private set; }
public KubernetesDeployAction(ISecretsManager secretsManager)
{
_secretsManager = secretsManager;
}
public override ICommandLineParserResult ParseArgs(string[] args)
{
SetFlag<string>("name", "The name used for the deployment and other artifacts in kubernetes", n =>
{
KubernetesHelper.ValidateKubernetesName(n);
Name = n;
}, isRequired: true);
SetFlag<string>("image-name", "Image to use for the pod deployment and to read functions from", n => ImageName = n);
SetFlag<KedaVersion>("keda-version", $"Defines the version of KEDA to use. Default: {Kubernetes.KEDA.KedaVersion.v2}. Options are: {Kubernetes.KEDA.KedaVersion.v1} or {Kubernetes.KEDA.KedaVersion.v2}", n => KedaVersion = n);
SetFlag<string>("registry", "When set, a docker build is run and the image is pushed to that registry/name. This is mutually exclusive with --image-name. For docker hub, use username.", r => Registry = r);
SetFlag<string>("namespace", "Kubernetes namespace to deploy to. Default: Current namespace in Kubernetes config.", ns => Namespace = ns);
SetFlag<string>("pull-secret", "The secret holding a private registry credentials", s => PullSecret = s);
SetFlag<int>("polling-interval", "The polling interval for checking non-http triggers. Default: 30 (seconds)", p => PollingInterval = p);
SetFlag<int>("cooldown-period", "The cooldown period for the deployment before scaling back to 0 after all triggers are no longer active. Default: 300 (seconds)", p => CooldownPeriod = p);
SetFlag<int>("min-replicas", "Minimum replica count", m => MinReplicaCount = m);
SetFlag<int>("max-replicas", "Maximum replica count to scale to by HPA", m => MaxReplicaCount = m);
SetFlag<string>("keys-secret-name", "The name of a kubernetes secret collection to use for the function app keys (host keys, function keys etc.)", ksn => KeysSecretCollectionName = ksn);
SetFlag<bool>("mount-funckeys-as-containervolume", "The flag indicating to mount the func app keys as container volume", kmv => MountFuncKeysAsContainerVolume = kmv);
SetFlag<string>("secret-name", "The name of an existing kubernetes secret collection, containing func app settings, to use in the deployment instead of creating new a new one based upon local.settings.json", sn => SecretsCollectionName = sn);
SetFlag<string>("config-map-name", "The name of an existing config map with func app settings to use in the deployment", cm => ConfigMapName = cm);
SetFlag<string>("service-type", "Kubernetes Service Type. Default LoadBalancer Valid options: " + string.Join(",", ServiceTypes), s =>
{
if (!string.IsNullOrEmpty(s) && !ServiceTypes.Contains(s))
{
throw new CliArgumentsException($"serviceType {ServiceType} is not supported. Valid options are: {string.Join(",", ServiceTypes)}");
}
ServiceType = s;
});
SetFlag<bool>("no-docker", "With --image-name, the core-tools will inspect the functions inside the image. This will require mounting the image filesystem. Passing --no-docker uses current directory for functions.", nd => NoDocker = nd);
SetFlag<bool>("use-config-map", "Use a ConfigMap/V1 instead of a Secret/V1 object for function app settings configurations", c => UseConfigMap = c);
SetFlag<bool>("dry-run", "Show the deployment template", f => DryRun = f);
SetFlag<bool>("ignore-errors", "Proceed with the deployment if a resource returns an error. Default: false", f => IgnoreErrors = f);
SetFlag<bool>("show-service-fqdn", "display Http Trigger URL with kubernetes FQDN rather than IP. Default: false", f => ShowServiceFqdn = f);
SetFlag<bool>("use-git-hash-version", "Use the githash as the version for the image", f => UseGitHashAsImageVersion = f);
SetFlag<bool>("write-configs", "Output the kubernetes configurations as YAML files instead of deploying", f => WriteConfigs = f);
SetFlag<string>("config-file", "if --write-configs is true, write configs to this file (default: 'functions.yaml')", f => ConfigFile = f);
SetFlag<string>("hash-files", "Files to hash to determine the image version", f => HashFilesPattern = f);
SetFlag<bool>("image-build", "If false, skip the docker build", f => BuildImage = f);
SetFlag<string>("keys-secret-annotations", "The annotations to add to the keys secret e.g. key1=val1,key2=val2", a => KeysSecretAnnotations = a.Split(',').Select(s => s.Split('=')).ToDictionary(k => k[0], v => v[1]));
return base.ParseArgs(args);
}
public override async Task RunAsync()
{
(var resolvedImageName, var shouldBuild) = await ResolveImageName();
TriggersPayload triggers = null;
if (DryRun)
{
if (shouldBuild)
{
// don't build on a --dry-run.
// read files from the local dir
triggers = await GetTriggersLocalFiles();
}
else
{
triggers = await DockerHelpers.GetTriggersFromDockerImage(resolvedImageName);
}
}
else if (BuildImage)
{
if (shouldBuild)
{
await DockerHelpers.DockerBuild(resolvedImageName, Environment.CurrentDirectory);
}
// This needs to be fixed to run after the build.
triggers = await DockerHelpers.GetTriggersFromDockerImage(resolvedImageName);
}
else
{
triggers = await GetTriggersLocalFiles();
}
(var resources, var funcKeys) = await KubernetesHelper.GetFunctionsDeploymentResources(
Name,
resolvedImageName,
Namespace,
triggers,
_secretsManager.GetSecrets(),
PullSecret,
SecretsCollectionName,
ConfigMapName,
UseConfigMap,
PollingInterval,
CooldownPeriod,
ServiceType,
MinReplicaCount,
MaxReplicaCount,
KeysSecretCollectionName,
MountFuncKeysAsContainerVolume,
KedaVersion,
KeysSecretAnnotations
);
if (DryRun)
{
ColoredConsole.WriteLine(KubernetesHelper.SerializeResources(resources, OutputSerializationOptions.Yaml));
}
else
{
Task kubernetesTask = null;
Task imageTask = ((BuildImage && shouldBuild) ? DockerHelpers.DockerPush(resolvedImageName, false) : null);
if (WriteConfigs)
{
var yaml = KubernetesHelper.SerializeResources(resources, OutputSerializationOptions.Yaml);
kubernetesTask = File.WriteAllTextAsync(ConfigFile, yaml);
Console.Write($"Configuration written to {ConfigFile}");
return;
}
else
{
Func<Task> resourceTaskFn = () => {
var serialized = KubernetesHelper.SerializeResources(resources, OutputSerializationOptions.Yaml);
return KubectlHelper.KubectlApply(serialized, showOutput: true, ignoreError: IgnoreErrors, @namespace: Namespace);
};
if (!await KubernetesHelper.NamespaceExists(Namespace))
{
kubernetesTask = KubernetesHelper.CreateNamespace(Namespace).ContinueWith((result) => {
return resourceTaskFn();
});
}
else
{
kubernetesTask = resourceTaskFn();
}
}
if (imageTask != null)
{
await imageTask;
}
await kubernetesTask;
var httpService = resources
.Where(i => i is ServiceV1)
.Cast<ServiceV1>()
.FirstOrDefault(s => s.Metadata.Name.Contains("http"));
var httpDeployment = resources
.Where(i => i is DeploymentV1Apps)
.Cast<DeploymentV1Apps>()
.FirstOrDefault(d => d.Metadata.Name.Contains("http"));
if (httpDeployment != null)
{
await KubernetesHelper.WaitForDeploymentRollout(httpDeployment);
//Print the function keys message to the console
await KubernetesHelper.PrintFunctionsInfo(httpDeployment, httpService, funcKeys, triggers, ShowServiceFqdn);
}
}
}
private async Task<TriggersPayload> GetTriggersLocalFiles()
{
var functionsPath = Environment.CurrentDirectory;
if (GlobalCoreToolsSettings.CurrentWorkerRuntime == WorkerRuntime.dotnet ||
GlobalCoreToolsSettings.CurrentWorkerRuntime == WorkerRuntime.dotnetIsolated)
{
if (DotnetHelpers.CanDotnetBuild())
{
var outputPath = Path.Combine("bin", "output");
await DotnetHelpers.BuildDotnetProject(outputPath, string.Empty, showOutput: false);
functionsPath = Path.Combine(Environment.CurrentDirectory, outputPath);
}
}
var functionsJsons = GlobalCoreToolsSettings.CurrentWorkerRuntime == WorkerRuntime.dotnetIsolated
? ReadFunctionsMetadata(functionsPath)
: ReadFunctionJsons(functionsPath);
var hostJson = JsonConvert.DeserializeObject<JObject>(FileSystemHelpers.ReadAllTextFromFile("host.json"));
return new TriggersPayload
{
HostJson = hostJson,
FunctionsJson = functionsJsons
};
}
private static Dictionary<string, JObject> ReadFunctionsMetadata(string functionsPath)
{
var functionsMetadataPath = Path.Combine(functionsPath, "functions.metadata");
if (!FileSystemHelpers.FileExists(functionsMetadataPath))
{
return new();
}
var functionsMetadataContents = FileSystemHelpers.ReadAllTextFromFile(functionsMetadataPath);
var functionsMetadata = JsonConvert.DeserializeObject<JArray>(functionsMetadataContents);
return functionsMetadata
.Where(x => x["bindings"] != null)
.ToDictionary(k => k["name"].ToString(), v => v.ToObject<JObject>());
}
private static Dictionary<string, JObject> ReadFunctionJsons(string functionsPath)
{
var functionJsonFiles = FileSystemHelpers
.GetDirectories(functionsPath)
.Select(d => Path.Combine(d, "function.json"))
.Where(FileSystemHelpers.FileExists)
.Select(f => (filePath: f, content: FileSystemHelpers.ReadAllTextFromFile(f)));
return functionJsonFiles
.Select(t => (t.filePath, jObject: JsonConvert.DeserializeObject<JObject>(t.content)))
.Where(b => b.jObject["bindings"] != null)
.ToDictionary(k => Path.GetFileName(Path.GetDirectoryName(k.filePath)), v => v.jObject);
}
private async Task<(string, bool)> ResolveImageName()
{
var version = "latest";
if (UseGitHashAsImageVersion) {
if (HashFilesPattern.Length > 0)
{
var matcher = new Matcher();
matcher.AddInclude(HashFilesPattern);
var matches = MatcherExtensions.GetResultsInFullPath(matcher, "./");
version = GitHelpers.ActionsHashFiles(matches);
}
else
{
(var stdout, var err, var exit) = await GitHelpers.GitHash();
if (exit != 0) {
throw new CliException("Git describe failed: " + err);
}
version = stdout;
}
}
if (!string.IsNullOrEmpty(Registry))
{
return ($"{Registry}/{Name}:{version}", true && !NoDocker);
}
else if (!string.IsNullOrEmpty(ImageName))
{
return ($"{ImageName}", false);
}
throw new CliArgumentsException("either --image-name or --registry is required.");
}
}
}