Skip to content

Commit 52acb10

Browse files
authored
feat(operator build): allow disable / force / onlyonrelease for operator config (#912)
This closes #699.
1 parent c6ba4a6 commit 52acb10

File tree

3 files changed

+66
-6
lines changed

3 files changed

+66
-6
lines changed

docs/docs/operator/build-customization.mdx

Lines changed: 60 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -41,15 +41,42 @@ You can customize the build process by setting the following MSBuild properties
4141
</PropertyGroup>
4242
```
4343

44-
### Disabling Automatic Generation
44+
### Disabling or Forcing Automatic Generation
4545

46-
To disable automatic resource generation during build:
46+
The `GenerateOperatorResources` property controls when Kubernetes resources are generated during the build:
47+
48+
- **`<GenerateOperatorResources>false</GenerateOperatorResources>`**: Disables automatic resource generation for all build configurations.
49+
- **`<GenerateOperatorResources>true</GenerateOperatorResources>`**: Forces resource generation for all build configurations (including Debug and Release).
50+
- **Unset or empty**: Resources are generated **only** for `Release` builds (default behavior).
51+
52+
You can use this property in your project file as follows:
4753

4854
```xml
4955
<PropertyGroup>
50-
<!-- Disable automatic generation -->
56+
<!-- Disable automatic generation completely -->
5157
<GenerateOperatorResources>false</GenerateOperatorResources>
5258
</PropertyGroup>
59+
60+
<PropertyGroup>
61+
<!-- Force automatic generation for all builds -->
62+
<GenerateOperatorResources>true</GenerateOperatorResources>
63+
</PropertyGroup>
64+
65+
<PropertyGroup>
66+
<!-- Only generate in Release (default if not set) -->
67+
<!-- <GenerateOperatorResources></GenerateOperatorResources> -->
68+
</PropertyGroup>
69+
```
70+
71+
#### Conditional Generation Example
72+
73+
You can also conditionally enable or disable generation based on the build configuration:
74+
75+
```xml
76+
<PropertyGroup>
77+
<!-- Disable automatic generation in Debug mode -->
78+
<GenerateOperatorResources Condition="'$(Configuration)' == 'Debug'">false</GenerateOperatorResources>
79+
</PropertyGroup>
5380
```
5481

5582
## Build Process Details
@@ -168,3 +195,33 @@ dotnet tool install KubeOps.Cli
168195
- Verify the output directory exists
169196
- Check for permission issues
170197
- Ensure the path is valid for your OS
198+
199+
## Using with the CRD Installer for Local Development
200+
201+
For local development, you may want to not automatically generate resources in Debug mode and have them installed in your cluster. You can combine `GenerateOperatorResources` with the [CRD Installer utility](./utilities) for this purpose.
202+
203+
For example, in your `Program.cs`:
204+
205+
```csharp
206+
builder.Services
207+
.AddKubernetesOperator()
208+
#if DEBUG
209+
.AddCrdInstaller(c =>
210+
{
211+
c.OverwriteExisting = true;
212+
c.DeleteOnShutdown = true;
213+
})
214+
#endif
215+
.RegisterComponents();
216+
```
217+
218+
And in your project file:
219+
220+
```xml
221+
<PropertyGroup>
222+
<!-- Use the default behavior which does not generate resources in Debug mode -->
223+
<GenerateOperatorResources></GenerateOperatorResources>
224+
</PropertyGroup>
225+
```
226+
227+
This setup ensures that resources are always generated and installed automatically during development, but you can disable or restrict this behavior for production builds as needed.

examples/Operator/Operator.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
</ItemGroup>
1818

1919
<!-- <PropertyGroup>-->
20-
<!-- <KubeOpsCli>dotnet run &#45;&#45;project ../../src/KubeOps.Cli/ &#45;&#45; </KubeOpsCli>-->
20+
<!-- <KubeOpsCli>dotnet run &#45;&#45;project ../../src/KubeOps.Cli/KubeOps.Cli.csproj &#45;&#45;framework net9.0 &#45;&#45; </KubeOpsCli>-->
2121
<!-- </PropertyGroup>-->
2222
<!-- <Import Project="..\..\src\KubeOps.Operator\Build\KubeOps.Operator.targets" />-->
2323

src/KubeOps.Operator/Build/KubeOps.Operator.targets

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
<OperatorName Condition="'$(OperatorName)' == ''">$(AssemblyName.ToLowerInvariant())</OperatorName>
55
<KubeOpsCli Condition="'$(KubeOpsCli)' == ''">dotnet kubeops</KubeOpsCli>
66
<KubeOpsConfigOut Condition="'$(KubeOpsConfigOut)' == ''">$(MSBuildProjectDirectory)\config</KubeOpsConfigOut>
7+
<GenerateOperatorResources Condition="'$(GenerateOperatorResources)' == '' And '$(Configuration)' == 'Release'">true</GenerateOperatorResources>
78
</PropertyGroup>
89
</Target>
910

@@ -45,8 +46,10 @@
4546
</Exec>
4647
<Message Importance="high" Condition="$(CliInstalled) == 1"
4748
Text="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." />
49+
<Message Importance="high" Condition="'$(GenerateOperatorResources)' != 'true'"
50+
Text="Property 'GenerateOperatorResources' property did not evaluate to 'true'. Do not generate operator configs." />
4851
<CallTarget ContinueOnError="true"
4952
Targets="GenerateKustomizationConfig"
50-
Condition="$(CliInstalled) == 0" />
53+
Condition="$(CliInstalled) == 0 And '$(GenerateOperatorResources)' == 'true'" />
5154
</Target>
52-
</Project>
55+
</Project>

0 commit comments

Comments
 (0)