Build Customization
KubeOps integrates with the .NET build process to automatically generate Kubernetes resources during build time. This document explains how to customize this behavior.
Default Behavior
By default, KubeOps will:
- Generate Kubernetes resources after a successful build
- Use the assembly name (lowercase) as the operator name
- Generate resources in a
config
directory in your project - Only regenerate resources when:
- The assembly has changed (detected by timestamp)
- The build configuration is
Release
Build Properties
You can customize the build process by setting the following MSBuild properties in your project file:
Basic Configuration
<PropertyGroup>
<!-- Custom operator name (defaults to assembly name) -->
<OperatorName>my-custom-operator</OperatorName>
<!-- Custom output directory (defaults to 'config') -->
<KubeOpsConfigOut>$(MSBuildProjectDirectory)\k8s</KubeOpsConfigOut>
<!-- Custom Docker image name -->
<DockerImage>my-registry/my-operator</DockerImage>
<!-- Custom Docker image tag -->
<DockerImageTag>latest</DockerImageTag>
</PropertyGroup>
Disabling Automatic Generation
To disable automatic resource generation during build:
<PropertyGroup>
<!-- Disable automatic generation -->
<GenerateOperatorResources>false</GenerateOperatorResources>
</PropertyGroup>
Build Process Details
The build process consists of several MSBuild targets:
-
BaseConfig:
- Sets default values for:
OperatorName
: Assembly name (lowercase)KubeOpsCli
: Path to the KubeOps CLI toolKubeOpsConfigOut
: Output directory for generated resources
- Sets default values for:
-
GetAssemblyBeforeTimestamp:
- Captures the assembly timestamp before compilation
- Used to detect changes in the assembly
-
GetAssemblyAfterTimestamp:
- Captures the assembly timestamp after compilation
- Used to detect changes in the assembly
-
GenerateKustomizationConfig:
- Generates the Kubernetes resources
- Uses the configured Docker image and tag if specified
- Outputs resources to the configured directory
-
GenerateOperatorResources:
- Runs after
CopyFilesToOutputDirectory
- Only runs if:
- The project is an executable (
OutputType
isexe
) - The assembly has changed OR the build is in
Release
mode
- The project is an executable (
- Checks if the KubeOps CLI is installed
- Calls
GenerateKustomizationConfig
if the CLI is available
- Runs after
CLI Tool Requirements
The build process requires the KubeOps CLI tool to be installed. If it's not installed, you'll see a message:
KubeOps CLI is not installed as tool, cannot generate stuff for projects.
Please install it with 'dotnet tool install KubeOps.Cli' to use automatic build generation.
To install the CLI tool:
dotnet tool install KubeOps.Cli
Example Configurations
Basic Operator
<PropertyGroup>
<OperatorName>my-operator</OperatorName>
<DockerImage>my-registry/my-operator</DockerImage>
<DockerImageTag>1.0.0</DockerImageTag>
</PropertyGroup>
Custom Output Directory
<PropertyGroup>
<KubeOpsConfigOut>$(MSBuildProjectDirectory)\k8s\resources</KubeOpsConfigOut>
</PropertyGroup>
Development Configuration
<PropertyGroup>
<!-- Disable automatic generation in Debug mode -->
<GenerateOperatorResources Condition="'$(Configuration)' == 'Debug'">false</GenerateOperatorResources>
</PropertyGroup>
Best Practices
-
Version Control:
- Consider whether to commit generated resources
- Use
.gitignore
to exclude generated files if needed - Document the generation process in your README
-
Build Configuration:
- Use different settings for Debug and Release builds
- Consider using environment-specific configurations
- Document your build customization in the project
-
Docker Images:
- Use consistent naming conventions
- Include version information in tags
- Consider using multi-stage builds
Common Issues
-
Missing CLI Tool:
- Ensure the KubeOps CLI is installed
- Check the tool installation path
- Verify the tool version compatibility
-
Generation Timing:
- Resources might not generate in Debug mode
- Changes might not trigger regeneration
- Consider manual generation for testing
-
Output Location:
- Verify the output directory exists
- Check for permission issues
- Ensure the path is valid for your OS