Skip to content

Commit

Permalink
Add docker cartservice
Browse files Browse the repository at this point in the history
  • Loading branch information
hfrog committed Jan 25, 2024
1 parent 14f085b commit b2a3736
Show file tree
Hide file tree
Showing 17 changed files with 882 additions and 3 deletions.
12 changes: 10 additions & 2 deletions .github/workflows/app.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
name: app
on:
- push
- workflow_dispatch
jobs:
deploy:
runs-on: ubuntu-20.04
Expand All @@ -19,7 +20,14 @@ jobs:
- run: |
cd $GITHUB_WORKSPACE/app
source "$(werf ci-env github --as-file)"
werf converge --disable-auto-host-cleanup=true
eval werf kubectl create secret docker-registry registrysecret --namespace=$(werf helm get-namespace) \
--docker-username=json_key --docker-password=\'$(echo $IMAGE_PULLER_KEY | base64 -d)\' --docker-server=cr.yandex \
--dry-run=client --output=yaml | werf kubectl apply -f -
echo $IMAGE_UPLOADER_KEY | base64 -d | werf cr login --username=json_key --password-stdin=true cr.yandex
werf converge --repo $CONTAINER_REPO
env:
WERF_ENV: dev
# werf converge --disable-auto-host-cleanup=true --namespace=$NAMESPACE # We don't build image here
IMAGE_PULLER_KEY: ${{ secrets.IMAGE_PULLER_KEY }}
IMAGE_UPLOADER_KEY: ${{ secrets.IMAGE_UPLOADER_KEY }}
CONTAINER_REPO: ${{ secrets.CONTAINER_REPO }}
4 changes: 3 additions & 1 deletion app/.helm/charts/cartservice/templates/deployment.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ spec:
runAsGroup: 1000
runAsNonRoot: true
runAsUser: 1000
imagePullSecrets:
- name: registrysecret
containers:
- name: server
securityContext:
Expand All @@ -28,7 +30,7 @@ spec:
- ALL
privileged: false
readOnlyRootFilesystem: true
image: gcr.io/google-samples/microservices-demo/cartservice:v0.9.0
image: {{ $.Values.werf.image.cartservice }}
ports:
- containerPort: 7070
env:
Expand Down
6 changes: 6 additions & 0 deletions app/cartservice/.dockerignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
**/*.sh
**/*.bat
**/bin/
**/obj/
**/out/
Dockerfile*
41 changes: 41 additions & 0 deletions app/cartservice/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
# Copyright 2021 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

# https://mcr.microsoft.com/product/dotnet/sdk
FROM mcr.microsoft.com/dotnet/sdk:8.0.101@sha256:7ef41132b2ebe6166bde36b7ba2f0d302e10307c3e0523a4539643a77233f56d as builder
WORKDIR /app
COPY cartservice.csproj .
RUN dotnet restore cartservice.csproj \
-r linux-musl-x64
COPY . .
RUN dotnet publish cartservice.csproj \
-p:PublishSingleFile=true \
-r linux-musl-x64 \
--self-contained true \
-p:PublishTrimmed=True \
-p:TrimMode=Full \
-c release \
-o /cartservice \
--no-restore

# https://mcr.microsoft.com/product/dotnet/runtime-deps
FROM mcr.microsoft.com/dotnet/runtime-deps:8.0.1-alpine3.18-amd64@sha256:0b9be3c3937cd5ce83c6d05fc01698a904537be71b93dd485ea779592d5c8c10

WORKDIR /app
COPY --from=builder /cartservice .
EXPOSE 7070
ENV DOTNET_EnableDiagnostics=0 \
ASPNETCORE_HTTP_PORTS=7070
USER 1000
ENTRYPOINT ["/app/cartservice"]
33 changes: 33 additions & 0 deletions app/cartservice/Dockerfile.debug
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
# Copyright 2021 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

FROM mcr.microsoft.com/dotnet/sdk:8.0@sha256:7ef41132b2ebe6166bde36b7ba2f0d302e10307c3e0523a4539643a77233f56d AS build
WORKDIR /app
COPY . .
RUN dotnet restore cartservice.csproj
RUN dotnet build "./cartservice.csproj" -c Debug -o /out

FROM build AS publish
RUN dotnet publish cartservice.csproj -c Debug -o /out

# Building final image used in running container
FROM mcr.microsoft.com/dotnet/aspnet:8.0@sha256:3ff67792728179308c4bf06799d8b45d155c60fddc347acf69465a496d9a20b8 AS final
# Installing procps on the container to enable debugging of .NET Core
RUN apt-get update \
&& apt-get install -y unzip procps wget
WORKDIR /app
COPY --from=publish /out .
ENV ASPNETCORE_HTTP_PORTS=7070

ENTRYPOINT ["dotnet", "cartservice.dll"]
26 changes: 26 additions & 0 deletions app/cartservice/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
// Copyright 2020 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Hosting;
using cartservice;

CreateHostBuilder(args).Build().Run();

static IHostBuilder CreateHostBuilder(string[] args) =>
Host.CreateDefaultBuilder(args)
.ConfigureWebHostDefaults(webBuilder =>
{
webBuilder.UseStartup<Startup>();
});
84 changes: 84 additions & 0 deletions app/cartservice/Startup.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
using System;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Diagnostics.HealthChecks;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Diagnostics.HealthChecks;
using Microsoft.Extensions.Hosting;
using cartservice.cartstore;
using cartservice.services;
using Microsoft.Extensions.Caching.StackExchangeRedis;

namespace cartservice
{
public class Startup
{
public Startup(IConfiguration configuration)
{
Configuration = configuration;
}

public IConfiguration Configuration { get; }

// This method gets called by the runtime. Use this method to add services to the container.
// For more information on how to configure your application, visit https://go.microsoft.com/fwlink/?LinkID=398940
public void ConfigureServices(IServiceCollection services)
{
string redisAddress = Configuration["REDIS_ADDR"];
string spannerProjectId = Configuration["SPANNER_PROJECT"];
string spannerConnectionString = Configuration["SPANNER_CONNECTION_STRING"];
string alloyDBConnectionString = Configuration["ALLOYDB_PRIMARY_IP"];

if (!string.IsNullOrEmpty(redisAddress))
{
services.AddStackExchangeRedisCache(options =>
{
options.Configuration = redisAddress;
});
services.AddSingleton<ICartStore, RedisCartStore>();
}
else if (!string.IsNullOrEmpty(spannerProjectId) || !string.IsNullOrEmpty(spannerConnectionString))
{
services.AddSingleton<ICartStore, SpannerCartStore>();
}
else if (!string.IsNullOrEmpty(alloyDBConnectionString))
{
Console.WriteLine("Creating AlloyDB cart store");
services.AddSingleton<ICartStore, AlloyDBCartStore>();
}
else
{
Console.WriteLine("Redis cache host(hostname+port) was not specified. Starting a cart service using in memory store");
services.AddDistributedMemoryCache();
services.AddSingleton<ICartStore, RedisCartStore>();
}


services.AddGrpc();
}

// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}

app.UseRouting();

app.UseEndpoints(endpoints =>
{
endpoints.MapGrpcService<CartService>();
endpoints.MapGrpcService<cartservice.services.HealthCheckService>();
endpoints.MapGet("/", async context =>
{
await context.Response.WriteAsync("Communication with gRPC endpoints must be made through a gRPC client. To learn how to create a client, visit: https://go.microsoft.com/fwlink/?linkid=2086909");
});
});
}
}
}
15 changes: 15 additions & 0 deletions app/cartservice/appsettings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft": "Warning",
"Microsoft.Hosting.Lifetime": "Information"
}
},
"AllowedHosts": "*",
"Kestrel": {
"EndpointDefaults": {
"Protocols": "Http2"
}
}
}
19 changes: 19 additions & 0 deletions app/cartservice/cartservice.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<Project Sdk="Microsoft.NET.Sdk.Web">

<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Grpc.AspNetCore" Version="2.60.0" />
<PackageReference Include="Grpc.HealthCheck" Version="2.60.0" />
<PackageReference Include="Microsoft.Extensions.Caching.StackExchangeRedis" Version="8.0.0" />
<PackageReference Include="Google.Cloud.Spanner.Data" Version="4.6.0" />
<PackageReference Include="Npgsql" Version="8.0.1" />
<PackageReference Include="Google.Cloud.SecretManager.V1" Version="2.1.0" />
</ItemGroup>

<ItemGroup>
<Protobuf Include="protos\Cart.proto" GrpcServices="Both" />
</ItemGroup>
</Project>
Loading

0 comments on commit b2a3736

Please sign in to comment.