Skip to content

Commit

Permalink
Ticket #9 : Deploy & execute a function
Browse files Browse the repository at this point in the history
  • Loading branch information
thabart committed Sep 27, 2021
1 parent fb8a77b commit 417445e
Show file tree
Hide file tree
Showing 46 changed files with 1,148 additions and 25 deletions.
2 changes: 1 addition & 1 deletion Directory.Build.props
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<Project>
<PropertyGroup>
<VersionPrefix>0.0.1</VersionPrefix>
<VersionPrefix>0.0.2</VersionPrefix>
<Authors>SimpleIdServer</Authors>
<Owners>SimpleIdServer</Owners>
</PropertyGroup>
Expand Down
22 changes: 22 additions & 0 deletions default.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,28 @@ task publishHelmAndWebsite {
exec { git checkout master }
}

task buildLocalDockerImage -depends publish {
exec { docker build -f RuntimeGetSqlDockerfile -t localhost:5000/faasgetsql . }
exec { docker build -f RuntimeGetSqlDockerfile -t localhost:5000/faastransform . }
exec { docker build -f KubernetesDockerfile -t localhost:5000/faaskubernetes . }
exec { docker build -f GatewayDockerfile -t localhost:5000/faasgateway . }
exec { docker push localhost:5000/faasgetsql }
exec { docker push localhost:5000/faastransform }
exec { docker push localhost:5000/faaskubernetes }
exec { docker push localhost:5000/faasgateway }
}

task initLocalKubernetes {
exec { kubectl apply -f ./kubernetes/faas-namespace.yml }
exec { kubectl apply -f ./kubernetes/run-mssql.yml --namespace=faas }
exec { kubectl apply -f ./kubernetes/mssql-external-svc.yml --namespace=faas }
exec { kubectl apply -f ./kubernetes/mssql-internal-svc.yml --namespace=faas }
exec { kubectl apply -f ./kubernetes/run-faas-kubernetes.yml --namespace=faas }
exec { kubectl apply -f ./kubernetes/faas-kubernetes-svc.yml --namespace=faas }
exec { kubectl apply -f ./kubernetes/run-faas-gateway.yml --namespace=faas }
exec { kubectl apply -f ./kubernetes/faas-gateway-svc.yml --namespace=faas }
}

task pack -depends release, compile {
exec { dotnet pack $source_dir\FaasNet.Runtime\FaasNet.Runtime.csproj -c $config --no-build $versionSuffix --output $result_dir }
exec { dotnet pack $source_dir\FaasNet.Templates\FaasNet.Templates.csproj -c $config --no-build $versionSuffix --output $result_dir }
Expand Down
54 changes: 45 additions & 9 deletions docs/documentation/functions/create.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Create a function

Install FaasNet template
Install FaasNet template. This utility can be used to generate Function project in C#.

```
dotnet new --install FaasNet.Templates
Expand All @@ -21,8 +21,11 @@ dotnet new faasnetfn -n Function
The following files will be created :

* *Startup.cs* and *Program.cs*: the application entry point.
* *HelloWorldConfiguration.cs*: configuration of the function.
* *FunctionHandler.cs*: logic of the function.
* *HelloWorldConfiguration.cs*: configuration properties of the function.
* *FunctionHandler.cs*: contains the business logic. This class has one function which accepts one parameter and returns a JSON result. The input parameter has two distinct properties :

* Configuration: its value is coming from the gateway, it will be used to configure the behavior of the function for example : `ConnectionString` and `SQL Statement`.
* Input: value passed by caller.

In case the Visual Studio Support is needed, a solution can be created :

Expand All @@ -31,28 +34,61 @@ cd ..
dotnet new sln -n QuickStart
```

Add the Function project into the solution :
Add the Function project into the solution.

```
dotnet sln add ./src/Function/Function.csproj
```

# Deploy a function

Create the docker file. Replace the `DIRECTORY` variable by the project directory.
> [!WARNING]
> Before you start, Make sure your working environment is properly configured.
When the Function project is ready, it can be deployed to the Gateway API.

First of all, open a command prompt and execute the following command line to create a Docker file. The `DIRECTORY` variable must be replaced by the directory of the Function.csproj project.

```
FaasNet.CLI function -df <DIRECTORY>
```

Build the docker image. Replace the `DIRECTORY` variable by the project directory, replace the `NAME` variable by the name of your image.
Execute the following instruction to locally build the Docker image.
The `DIRECTORY` variable must be replaced by the directory of the Function.csproj project, and the `IMAGENAME` variable must be replaced by the name of the Docker image for example : localhost:5000/function.

```
FaasNet.CLI function -db <DIRECTORY> -t <IMAGENAME>
```

Execute the following command line to push the local Docker image into a registry. The `IMAGENAME` variable must be replaced by the name of the Docker Image.

```
FaasNet.CLI function -dp <IMAGENAME>
```

Finally, execute the latest command line to deploy the function into the Gateway API. Replace the `FUNCTIONNAME` variable by the name of your function and replace the `IMAGENAME` variable by the name of your Docker image.

```
FaasNet.CLI function deploy -name <FUNCTIONAME> -image <IMAGENAME>
```

# Execute a function

> [!WARNING]
> Before you start, Make sure you have a function deployed in the Gateway API.
Execute the following command to invoke a function. Replace the `FUNCTIONNAME` variable by the name of your function.

```
FaasNet.CLI function -db <DIRECTORY> -t <NAME>
FaasNet.CLI function invoke <FUNCTIONNAME> -input {} -configuration {'firstName':'coucou'}
```

Push the docker image into the Hub. Replace the `NAME` variable by the name of your image.
The following message is displayed

```
FaasNet.CLI function -dp <NAME>
{
"content": {
"message": "Hello 'coucou'"
}
}
```
2 changes: 1 addition & 1 deletion kubernetes/run-faas-gateway.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,6 @@ spec:
spec:
containers:
- name: faas-gateway
image: localhost:5000/gateway
image: localhost:5000/faasgateway
ports:
- containerPort: 8080
2 changes: 1 addition & 1 deletion kubernetes/run-faas-kubernetes.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,6 @@ spec:
spec:
containers:
- name: faas-kubernetes
image: localhost:5000/kubernetes
image: localhost:5000/faaskubernetes
ports:
- containerPort: 8080
22 changes: 22 additions & 0 deletions src/FaasNet.CLI/Commands/ConfigurationCommand.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
using FaasNet.CLI.Helpers;
using System.Collections.Generic;

namespace FaasNet.CLI.Commands
{
public class ConfigurationCommand : IMenuItemCommand
{
private List<IMenuItemCommand> _commands = new List<IMenuItemCommand>
{
new ConfigurationUpdateCommand(),
new ConfigurationGetCommand()
};

public string Command => "configuration";
public string Description => "Manage configuration";

public void Execute(IEnumerable<string> args)
{
MenuHelper.Execute(args, _commands);
}
}
}
41 changes: 41 additions & 0 deletions src/FaasNet.CLI/Commands/ConfigurationGetCommand.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
using FaasNet.CLI.Helpers;
using System;
using System.Collections.Generic;
using System.Linq;

namespace FaasNet.CLI.Commands
{
public class ConfigurationGetCommand : IMenuItemCommand
{
public string Command => "-get";
public string Description => "Get the configuration";

public void Execute(IEnumerable<string> args)
{
if (!args.Any())
{
Console.WriteLine("A Property with its value must be specified");
return;
}

var configuration = ConfigurationHelper.GetConfiguration();
if (configuration == null)
{
Console.WriteLine($"The configuration file '{ConfigurationHelper.ConfigurationFileName}' doesn't exist");
return;
}

var key = args.First();
if (!ConfigurationHelper.HasKey(key))
{
Console.WriteLine($"The key '{key}' cannot be configured");
return;
}

if(key == ConfigurationHelper.GatewayKey)
{
Console.WriteLine($"{key}={configuration.Provider.Gateway}");
}
}
}
}
51 changes: 51 additions & 0 deletions src/FaasNet.CLI/Commands/ConfigurationUpdateCommand.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
using FaasNet.CLI.Helpers;
using System;
using System.Collections.Generic;
using System.Linq;

namespace FaasNet.CLI.Commands
{
public class ConfigurationUpdateCommand : IMenuItemCommand
{
public string Command => "-set";
public string Description => "Update the configuration";

public void Execute(IEnumerable<string> args)
{
if (!args.Any())
{
Console.WriteLine("A Property with its value must be specified");
return;
}

var splitted = args.First().Split('=');
if (splitted.Count() != 2)
{
Console.WriteLine("The argument is not correct, it must respect the format Key=Value");
return;
}

var configuration = ConfigurationHelper.GetConfiguration();
if (configuration == null)
{
Console.WriteLine($"The configuration file '{ConfigurationHelper.ConfigurationFileName}' doesn't exist");
return;
}

var key = splitted.First();
var value = splitted.Last();
if (!ConfigurationHelper.HasKey(key))
{
Console.WriteLine($"The key '{key}' cannot be configured");
return;
}

if (key == ConfigurationHelper.GatewayKey)
{
configuration.Provider.Gateway = value;
}

ConfigurationHelper.UpdateConfiguration(configuration);
}
}
}
6 changes: 5 additions & 1 deletion src/FaasNet.CLI/Commands/FunctionCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,11 @@ public class FunctionCommand : IMenuItemCommand
{
new FunctionCreateDockerFileCommand(),
new FunctionBuildDockerFileCommand(),
new FunctionPublishDockerFileCommand()
new FunctionPublishDockerFileCommand(),
new FunctionDeployCommand(),
new FunctionRemoveCommand(),
new FunctionInvokeCommand(),
new FunctionConfigurationCommand()
};

public string Command => "function";
Expand Down
34 changes: 34 additions & 0 deletions src/FaasNet.CLI/Commands/FunctionConfigurationCommand.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
using FaasNet.CLI.Helpers;
using System;
using System.Collections.Generic;
using System.Linq;

namespace FaasNet.CLI.Commands
{
public class FunctionConfigurationCommand : IMenuItemCommand
{
public string Command => "configuration";
public string Description => "Invoke a function";

public void Execute(IEnumerable<string> args)
{
if (!args.Any())
{
Console.WriteLine("The name must be specified");
return;
}

var configuration = ConfigurationHelper.GetConfiguration();
if (configuration == null)
{
Console.WriteLine($"The configuration file '{ConfigurationHelper.ConfigurationFileName}' doesn't exist");
return;
}

var name = args.First();
var gatewayClient = new GatewayClient();
var jObj = gatewayClient.GetConfiguration(configuration.Provider.Gateway, name);
Console.WriteLine(jObj.ToString());
}
}
}
49 changes: 49 additions & 0 deletions src/FaasNet.CLI/Commands/FunctionDeployCommand.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
using FaasNet.CLI.Helpers;
using FaasNet.CLI.Parameters;
using FaasNet.Common.Configuration;
using System;
using System.Collections.Generic;
using System.Linq;

namespace FaasNet.CLI.Commands
{
public class FunctionDeployCommand : IMenuItemCommand
{
public string Command => "deploy";
public string Description => "Deploy the function into a local or remote Gateway";

public void Execute(IEnumerable<string> args)
{
if (!args.Any() || args.Count() != 4)
{
Console.WriteLine("The -name and -image must be specified");
return;
}

var configuration = ConfigurationHelper.GetConfiguration();
if (configuration == null)
{
Console.WriteLine($"The configuration file '{ConfigurationHelper.ConfigurationFileName}' doesn't exist");
return;
}

string errorMessage;
DeployFunctionParameter parameter;
if (!InputParameterParser.TryParse(args, out errorMessage, out parameter))
{
Console.WriteLine(errorMessage);
return;
}

var client = new GatewayClient();
client.PublishFunction(configuration.Provider.Gateway, parameter.Name, parameter.Image);
configuration.Functions.Add(new FaasFunctionConfiguration
{
Image = parameter.Image,
Name = parameter.Name
});
ConfigurationHelper.UpdateConfiguration(configuration);
Console.WriteLine($"The function '{parameter.Name}' is published");
}
}
}
Loading

0 comments on commit 417445e

Please sign in to comment.