Skip to content
Open
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
10 changes: 4 additions & 6 deletions docs/docs/operator/build-customization.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -251,11 +251,9 @@ For example, in your `Program.cs`:
builder.Services
.AddKubernetesOperator()
#if DEBUG
.AddCrdInstaller(c =>
{
c.OverwriteExisting = true;
c.DeleteOnShutdown = true;
})
.AddCrdInstaller(c => c
.WithOverwriteExisting()
.WithDeleteOnShutdown())
#endif
.RegisterComponents();
```
Expand All @@ -269,4 +267,4 @@ And in your project file:
</PropertyGroup>
```

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.
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.
13 changes: 6 additions & 7 deletions docs/docs/operator/utilities.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ The CRD Installer is a powerful utility intended **only for development environm
:::

When developing operators, you may want to quickly install or update CustomResourceDefinitions (CRDs) in your cluster. The `CrdInstaller` service automates this process, making it easier to iterate on CRD changes during development.
If the Kubernetes API server is temporarily unavailable when the operator starts, the installer logs the error and retries in the background with backoff instead of stopping the host startup.

### How to Add the CRD Installer

Expand All @@ -62,14 +63,12 @@ To enable the CRD installer, add the following to your operator's `Program.cs`:
builder.Services
.AddKubernetesOperator()
#if DEBUG
.AddCrdInstaller(c =>
{
c.OverwriteExisting = true;
c.DeleteOnShutdown = true;
})
.AddCrdInstaller(c => c
.WithOverwriteExisting()
.WithDeleteOnShutdown())
#endif
.RegisterComponents();
```

- `OverwriteExisting`: If `true`, existing CRDs with the same name will be **overwritten**. This is useful for development but can be destructive if used in production, as it may cause data loss.
- `DeleteOnShutdown`: If `true`, all CRDs installed by the operator will be **deleted** when the operator shuts down. This is extremely destructive and should only be used in disposable development environments.
- `WithOverwriteExisting()`: Existing CRDs with the same name will be **overwritten**. This is useful for development but can be destructive if used in production, as it may cause data loss.
- `WithDeleteOnShutdown()`: All CRDs installed by the operator will be **deleted** when the operator shuts down. This is extremely destructive and should only be used in disposable development environments.
8 changes: 3 additions & 5 deletions examples/Operator/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,9 @@
builder.Services
.AddKubernetesOperator()
#if DEBUG
.AddCrdInstaller(c =>
{
c.OverwriteExisting = true;
c.DeleteOnShutdown = true;
})
.AddCrdInstaller(c => c
.WithOverwriteExisting()
.WithDeleteOnShutdown())
#endif
.RegisterComponents();

Expand Down
4 changes: 2 additions & 2 deletions src/KubeOps.Abstractions/Builder/IOperatorBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -78,10 +78,10 @@ IOperatorBuilder AddFinalizer<TImplementation, TEntity>(string identifier)
/// This is intended for development purposes only.
/// </summary>
/// <param name="configure">
/// Configuration action for the <see cref="CrdInstallerSettings"/>.
/// Configuration action for the <see cref="CrdInstallerSettingsBuilder"/>.
/// Determines the behavior of the CRD installer, such as whether existing CRDs
/// should be overwritten or deleted on shutdown.
/// </param>
/// <returns>The builder for chaining.</returns>
IOperatorBuilder AddCrdInstaller(Action<CrdInstallerSettings>? configure = null);
IOperatorBuilder AddCrdInstaller(Action<CrdInstallerSettingsBuilder>? configure = null);
}
8 changes: 4 additions & 4 deletions src/KubeOps.Abstractions/Crds/CrdInstallerSettings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,20 @@
namespace KubeOps.Abstractions.Crds;

/// <summary>
/// Settings for the CRD installer.
/// Immutable settings for the CRD installer. Created via <see cref="CrdInstallerSettingsBuilder.Build"/>.
/// </summary>
public sealed class CrdInstallerSettings
public sealed record CrdInstallerSettings
{
/// <summary>
/// Determines whether existing CRDs should be overwritten.
/// This is useful for development purposes and should be used with caution.
/// It is a destructive operation that may lead to data loss.
/// </summary>
public bool OverwriteExisting { get; set; } = false;
public required bool OverwriteExisting { get; init; }

/// <summary>
/// Determines whether the installed CRDs should be deleted when the operator shuts down.
/// This is a very destructive operation and should only be used in development environments.
/// </summary>
public bool DeleteOnShutdown { get; set; } = false;
public required bool DeleteOnShutdown { get; init; }
}
36 changes: 36 additions & 0 deletions src/KubeOps.Abstractions/Crds/CrdInstallerSettingsBuilder.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the Apache 2.0 License.
// See the LICENSE file in the project root for more information.

namespace KubeOps.Abstractions.Crds;

/// <summary>
/// Configures a <see cref="CrdInstallerSettings"/> instance.
/// Set properties directly or use the fluent <c>With*</c> extension methods,
/// then call <see cref="Build"/> to obtain the immutable <see cref="CrdInstallerSettings"/> record.
/// </summary>
public sealed class CrdInstallerSettingsBuilder
{
/// <summary>
/// Determines whether existing CRDs should be overwritten.
/// This is useful for development purposes and should be used with caution.
/// It is a destructive operation that may lead to data loss.
/// </summary>
public bool OverwriteExisting { get; set; }

/// <summary>
/// Determines whether the installed CRDs should be deleted when the operator shuts down.
/// This is a very destructive operation and should only be used in development environments.
/// </summary>
public bool DeleteOnShutdown { get; set; }

/// <summary>
/// Produces an immutable <see cref="CrdInstallerSettings"/> record from the current configuration.
/// </summary>
/// <returns>A fully initialised <see cref="CrdInstallerSettings"/> record.</returns>
public CrdInstallerSettings Build() => new()
{
OverwriteExisting = OverwriteExisting,
DeleteOnShutdown = DeleteOnShutdown,
};
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the Apache 2.0 License.
// See the LICENSE file in the project root for more information.

namespace KubeOps.Abstractions.Crds;

/// <summary>
/// Fluent extension methods for <see cref="CrdInstallerSettingsBuilder"/>.
/// Each method sets one property and returns the same builder instance for chaining.
/// </summary>
public static class CrdInstallerSettingsBuilderExtensions
{
/// <summary>Sets whether existing CRDs should be overwritten.</summary>
/// <param name="builder">The builder to configure.</param>
/// <param name="value"><c>true</c> to overwrite existing CRDs; <c>false</c> otherwise.</param>
/// <returns>The same <paramref name="builder"/> instance for chaining.</returns>
public static CrdInstallerSettingsBuilder WithOverwriteExisting(
this CrdInstallerSettingsBuilder builder,
bool value = true)
{
builder.OverwriteExisting = value;
return builder;
}

/// <summary>Sets whether installed CRDs should be deleted when the operator shuts down.</summary>
/// <param name="builder">The builder to configure.</param>
/// <param name="value"><c>true</c> to delete installed CRDs on shutdown; <c>false</c> otherwise.</param>
/// <returns>The same <paramref name="builder"/> instance for chaining.</returns>
public static CrdInstallerSettingsBuilder WithDeleteOnShutdown(
this CrdInstallerSettingsBuilder builder,
bool value = true)
{
builder.DeleteOnShutdown = value;
return builder;
}
}
9 changes: 5 additions & 4 deletions src/KubeOps.Operator/Builder/OperatorBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -98,11 +98,12 @@ public IOperatorBuilder AddFinalizer<TImplementation, TEntity>(string identifier
return this;
}

public IOperatorBuilder AddCrdInstaller(Action<CrdInstallerSettings>? configure = null)
public IOperatorBuilder AddCrdInstaller(Action<CrdInstallerSettingsBuilder>? configure = null)
{
var settings = new CrdInstallerSettings();
configure?.Invoke(settings);
Services.AddSingleton(settings);
var settingsBuilder = new CrdInstallerSettingsBuilder();
configure?.Invoke(settingsBuilder);

Services.AddSingleton(settingsBuilder.Build());
Services.AddHostedService<CrdInstaller>();
return this;
}
Expand Down
Loading
Loading