# KindContainerExtensions Methods

- Package: [CommunityToolkit.Aspire.Hosting.Kind](/reference/api/csharp/communitytoolkit.aspire.hosting.kind.md)
- Type: [KindContainerExtensions](/reference/api/csharp/communitytoolkit.aspire.hosting.kind/kindcontainerextensions.md)
- Kind: `Methods`
- Members: `2`

Extension methods for configuring container resources to work with Kind clusters.

<a id="withkindnetwork"></a>
<a id="withkindnetwork-iresourcebuilder-containerresource"></a>

## WithKindNetwork(IResourceBuilder<ContainerResource>)

- Name: `WithKindNetwork(IResourceBuilder<ContainerResource>)`
- Modifiers: `extension`
- Returns: `IResourceBuilder<ContainerResource>`
- Source: [GitHub](https://github.com/CommunityToolkit/Aspire/blob/d479e13473b1d29d06b974f144a0049289a3cb5c/src/CommunityToolkit.Aspire.Hosting.Kind/KindContainerExtensions.cs#L100-L142)

Connects a container resource to the Kind container network, enabling it to communicate with the Kind cluster's API server and nodes.

```csharp
public static class KindContainerExtensions
{
    public static IResourceBuilder<ContainerResource> WithKindNetwork(
        this IResourceBuilder<ContainerResource> builder)
    {
        // ...
    }
}
```

## Parameters

- `builder` (`IResourceBuilder<ContainerResource>`)
  The container resource builder.

## Returns

`IResourceBuilder<ContainerResource>` -- A reference to the `ApplicationModel.IResourceBuilder`1`.

## Remarks

Use this method when a container needs direct network connectivity to the Kind control plane.

Kind creates a separate container network ("kind") for its nodes. Aspire manages its own container network for containers it creates. Containers on different networks cannot communicate by default.

Aspire does not provide a built-in way to add containers to additional container networks, and `WithContainerRuntimeArgs("--network", "kind")` is overridden internally. See [https://github.com/microsoft/aspire/issues/14081](https://github.com/microsoft/aspire/issues/14081) for the upstream feature request.

This method works around the limitation by subscribing to the container's stopped event. When the container stops (typically because it can't reach the Kind cluster), the hook connects the container to the "kind" network via the configured container runtime and restarts it.

If the container does not already have an explicit name (via `WithContainerName`), this method assigns one using the pattern `{resource.Name}-{random}` to ensure the container can be identified by the handler.

## ATS metadata

### ATS export

- Available to Polyglot AppHosts through the Aspire Type System.

<a id="withreference"></a>
<a id="withreference-iresourcebuilder-containerresource-iresourcebuilder-kindclusterresource"></a>

## WithReference(IResourceBuilder<ContainerResource>, IResourceBuilder<KindClusterResource>)

- Name: `WithReference(IResourceBuilder<ContainerResource>, IResourceBuilder<KindClusterResource>)`
- Modifiers: `extension`
- Returns: `IResourceBuilder<ContainerResource>`
- Source: [GitHub](https://github.com/CommunityToolkit/Aspire/blob/d479e13473b1d29d06b974f144a0049289a3cb5c/src/CommunityToolkit.Aspire.Hosting.Kind/KindContainerExtensions.cs#L48-L61)

Configures a container resource to reference the Kind cluster by bind-mounting the container-compatible kubeconfig, injecting environment variables, and connecting to the Kind container network.

```csharp
public static class KindContainerExtensions
{
    public static IResourceBuilder<ContainerResource> WithReference(
        this IResourceBuilder<ContainerResource> builder,
        IResourceBuilder<KindClusterResource> kind)
    {
        // ...
    }
}
```

## Parameters

- `builder` (`IResourceBuilder<ContainerResource>`)
  The container resource builder.
- `kind` (`IResourceBuilder<KindClusterResource>`)
  The Kind cluster resource builder.

## Returns

`IResourceBuilder<ContainerResource>` -- A reference to the `ApplicationModel.IResourceBuilder`1`.

## Remarks

This overload is the primary integration path for containers. It:

- Bind-mounts the container-compatible kubeconfig into the container at `/etc/kubeconfig/config`
- Sets `KUBECONFIG` to the in-container mount path
- Sets `K8S_CLUSTER_NAME` to the Kind cluster name
- Automatically connects the container to the Kind network

The container-compatible kubeconfig uses the Kind control-plane container name ( `{clusterName}-control-plane`) instead of `127.0.0.1`, enabling container-to-container communication over the Kind container network.

## ATS metadata

### ATS export

- Capability ID: `CommunityToolkit.Aspire.Hosting.Kind/withKindContainerReference`
