Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

extend copsnamespace for optional project related labels #43

Merged
merged 6 commits into from
Oct 27, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 6 additions & 5 deletions ConplementAG.CopsController.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,13 @@
<AspNetCoreHostingModel>InProcess</AspNetCoreHostingModel>
<RootNamespace>ConplementAG.CopsController</RootNamespace>
<RestorePackagesWithLockFile>true</RestorePackagesWithLockFile>
<UserSecretsId>dc526bff-7bba-4e39-adab-3033979b7f98</UserSecretsId>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.AspNetCore.Mvc.NewtonsoftJson" Version="6.0.6" />
<PackageReference Include="Microsoft.Extensions.Configuration" Version="6.0.1" />
<PackageReference Include="Serilog.AspNetCore" Version="5.0.0" />
<PackageReference Include="Serilog.Settings.Configuration" Version="3.3.0" />
<PackageReference Include="Serilog.Sinks.Console" Version="4.0.1" />
<PackageReference Include="Microsoft.AspNetCore.Mvc.NewtonsoftJson" Version="6.0.23" />
<PackageReference Include="Microsoft.Extensions.Configuration" Version="7.0.0" />
<PackageReference Include="Serilog.AspNetCore" Version="7.0.0" />
<PackageReference Include="Serilog.Settings.Configuration" Version="7.0.1" />
<PackageReference Include="Serilog.Sinks.Console" Version="4.1.0" />
</ItemGroup>
</Project>
1 change: 1 addition & 0 deletions Controllers/Sync.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using System;
using System.Net;
using Microsoft.AspNetCore.Mvc;
using Newtonsoft.Json;

namespace ConplementAG.CopsController.Controllers
{
Expand Down
12 changes: 12 additions & 0 deletions Models/CopsNamespace.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,9 @@ public partial class CopsSpec

[JsonProperty("namespaceAdminServiceAccounts")]
public CopsAdminServiceAccountSpec[] NamespaceAdminServiceAccounts { get; set; }

[JsonProperty("project")]
public CopsProjectInformations Project { get; set; }
}

public class CopsAdminServiceAccountSpec
Expand All @@ -39,4 +42,13 @@ public class CopsAdminServiceAccountSpec
[JsonProperty("namespace")]
public string Namespace { get; set; }
}

public class CopsProjectInformations
{
[JsonProperty("name")]
public string Name { get; set; }

[JsonProperty("costCenter")]
public string CostCenter { get; set; }
}
}
25 changes: 22 additions & 3 deletions Models/K8sNamespace.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using Newtonsoft.Json;
using System.Collections.Generic;
using Newtonsoft.Json;

namespace ConplementAG.CopsController.Models
{
Expand All @@ -13,17 +14,35 @@ public class K8sNamespace
[JsonProperty("metadata")]
public K8sNamespaceMetadata Metadata { get; set; }

public K8sNamespace(string name)
public K8sNamespace(string name, string projectName, string projectCostCenter)
{
Kind = "Namespace";
ApiVersion = "v1";
Metadata = new K8sNamespaceMetadata { Name = name };
Metadata = new K8sNamespaceMetadata(name, projectName, projectCostCenter);
}
}

public class K8sNamespaceMetadata
{
[JsonProperty("name")]
public string Name { get; set; }

[JsonProperty("labels")]
public Dictionary<string, string> Labels { get; set; }

public K8sNamespaceMetadata(string name, string projectName, string projectCostCenter)
{
Name = name;
Labels = new Dictionary<string, string>();
if (!string.IsNullOrWhiteSpace(projectName))
{
Labels.Add("conplement.de/project-name", projectName);
}

if (!string.IsNullOrWhiteSpace(projectName))
{
Labels.Add("conplement.de/project-cost-center", projectCostCenter);
}
}
}
}
29 changes: 20 additions & 9 deletions Properties/launchSettings.json
Original file line number Diff line number Diff line change
@@ -1,13 +1,4 @@
{
"iisSettings": {
"windowsAuthentication": false,
"anonymousAuthentication": true,
"iisExpress": {
"applicationUrl": "http://localhost:33197",
"sslPort": 44396
}
},
"$schema": "http://json.schemastore.org/launchsettings.json",
"profiles": {
"IIS Express": {
"commandName": "IISExpress",
Expand All @@ -33,6 +24,26 @@
"resourceGroup": "dbio2-neu-cops-rg",
"aksName": "dbio2-neu-cops-aks",
"subscriptionId": "d2474dd2-a3b9-47df-a489-14a4ea3056a8"
},
"WSL": {
"commandName": "WSL2",
"launchBrowser": true,
"launchUrl": "https://localhost:5001/api/sync",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development",
"ASPNETCORE_URLS": "http://localhost:5000",
"Serilog__MinimumLevel": "Verbose"
},
"distributionName": ""
}
},
"$schema": "http://json.schemastore.org/launchsettings.json",
"iisSettings": {
"windowsAuthentication": false,
"anonymousAuthentication": true,
"iisExpress": {
"applicationUrl": "http://localhost:33197",
"sslPort": 44396
}
}
}
16 changes: 7 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,37 +8,35 @@ This project is "based" on [metacontroller](https://github.com/GoogleCloudPlatfo
- Metacontroller is the simplest custom controller approach in Kubernetes, and offers easy parent / child resource tracking
- Metacontroller integrates with "business" code via webhooks, which leaves us free to select any language for the implementation
- [Azure Dev Spaces](https://docs.microsoft.com/en-us/azure/dev-spaces/), an excelent tool for development / debugging in Kubernetes, works at the moment the best with C#
- [Telepresence](https://telepresence.io) an excelent tool for development / debugging controller within Kubernetes

## Setup

Check the instructions on the [release page](https://github.com/conplementAG/cops-controller/releases).

## Development

For developing, we use Visual Studio Code and Azure Dev Spaces (running inside our CoreOps clusters).
For developing, we use Visual Studio Code and Telepresence (intercepting cluster workload and redirect traffic to local service).

1. Create / update your cops cluster to the latest state. Metacontroller, which is required for this controller to work, is delivered out of the box.

2. Install / setup Azure DevSpaces on your cluster, incl. your DevSpace
2. Install / setup telepresence in your cluster

`az aks use-dev-spaces -g ... -n ... --space space_name`
`telepresence helm install` to install telepresence
`telepresence intercept cops-controller -n coreops-cops-controller --port 5000:80` intercept the cops-controller service

If the command above fails (like to linux), then you can use the `azds controller create -tn ... -n ... -g ...` command instead.

Install the Azure DevSpaces CLI and run:
`azds space select --name your_space_name`
Make sure you are running a version of cops-controller locally listing to port 5000 ( see 5. )

3. Deploy the custom resource definitions `kubectl apply -f deployment/crds`

4. Install / setup Visual Studio Code with following extensions:
- C#
- Docker
- NuGet Package Manager
- Azure Dev Spaces

Also, you might have issues with C# and .NET Core unless you install the dotnet core build tools as well: https://github.com/OmniSharp/omnisharp-roslyn/issues/1311#issuecomment-428361674

5. Run `donet restore` / `dotnet build` and you are ready to go. For Azure DevSpace, either run `azds up` or simply start debugging through VS Code (launch configuration for Azure DevSpaces is included with the repository).
5. Run `donet restore` / `dotnet build` and you are ready to go.

Hints on development:
- When running inside Kubernetes, follow both metacontroller and this container logs. Metacontroller logs can be reached via `kubectl logs metacontroller-0 -n metacontroller -f` or a similar command.
6 changes: 3 additions & 3 deletions Services/K8sResourceFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,10 @@ private static IList<object> Create(CopsNamespace copsNamespace)
{
return new List<object>
{
new K8sNamespace(copsNamespace.Metadata.Name),
K8sRoleBinding.NamespaceFullAccess(copsNamespace.Metadata.Name, copsNamespace.Spec.NamespaceAdminUsers,
new K8sNamespace(copsNamespace.Metadata.Name, copsNamespace.Spec.Project?.Name, copsNamespace.Spec.Project?.CostCenter),
K8sRoleBinding.NamespaceFullAccess(copsNamespace.Metadata.Name, copsNamespace.Spec.NamespaceAdminUsers,
copsNamespace.Spec.NamespaceAdminServiceAccounts ?? new List<CopsAdminServiceAccountSpec>().ToArray()),
K8sClusterRoleBinding.CopsNamespaceEditBinding(copsNamespace.Metadata.Name, copsNamespace.Spec.NamespaceAdminUsers,
K8sClusterRoleBinding.CopsNamespaceEditBinding(copsNamespace.Metadata.Name, copsNamespace.Spec.NamespaceAdminUsers,
copsNamespace.Spec.NamespaceAdminServiceAccounts ?? new List<CopsAdminServiceAccountSpec>().ToArray()),
K8sClusterRole.CopsNamespaceEdit(copsNamespace.Metadata.Name),
K8SLimitRange.Default(copsNamespace.Metadata.Name)
Expand Down
2 changes: 1 addition & 1 deletion deployment/cops-controller/templates/02-controller.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ metadata:
spec:
generateSelector: true
parentResource:
apiVersion: coreops.conplement.cloud/v1
apiVersion: coreops.conplement.cloud/v2
resource: copsnamespaces
childResources:
- apiVersion: v1
Expand Down
67 changes: 66 additions & 1 deletion deployment/crds/copsnamespace.crd.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,51 @@ spec:
- cns
versions:
- name: v1
served: true
storage: false
deprecated: true
deprecationWarning: "coreops.conplement.cloud/v1 CopsNamespace is deprecated; migrate to coreops.conplement.cloud/v1 CopsNamespace by adding project informations; https://wiki.conplement.de/x/hoeOBg"
schema:
openAPIV3Schema:
required:
- spec
type: object
properties:
spec:
required:
- namespaceAdminUsers
type: object
properties:
namespaceAdminUsers:
description: |-
Array with all the namespace administrator users. You need to reference the administrators by specifying
their Conplement Azure AD User IDs, which is usually in the form of
[email protected] (notice the capital letters!)
type: array
items:
type: string
namespaceAdminServiceAccounts:
description: |-
Array with all the namespace administrator service accounts.
type: array
items:
type: object
required:
- serviceAccount
- namespace
properties:
serviceAccount:
type: string
description: |-
Service account name
namespace:
type: string
description: |-
Namespace where the service account is located
subresources:
# status enables the status subresource.
status: {}
- name: v2
served: true
storage: true
schema:
Expand Down Expand Up @@ -52,6 +97,26 @@ spec:
type: string
description: |-
Namespace where the service account is located
project:
description: |-
Project related informations
type: object
required:
- name
- costcenter
properties:
name:
type: string
description: |-
Name of the project hosted within namespace
costCenter:
type: string
description: |-
Costcenter of the project hosted with namespace
subresources:
# status enables the status subresource.
status: {}
status: {}
conversion:
# None conversion assumes the same schema for all versions and only sets the apiVersion
# field of custom resources to the proper value
strategy: None
Loading