pulumi / pulumi-kubernetes

A Pulumi resource provider for Kubernetes to manage API resources and workloads in running clusters
https://www.pulumi.com/docs/reference/clouds/kubernetes/
Apache License 2.0
397 stars 113 forks source link

CustomResource should have plain apiVersion and kind properties #3079

Open EronWright opened 3 days ago

EronWright commented 3 days ago

Proposed changes

This PR seeks to make the apiVersion and kind properties be plain (prompt) values when configuring a CustomResource. A plain value is required to eagerly compute the resource type token, e.g. kubernetes:stable.example.com/v1:CronTab or kubernetes:stable.example.com/v1:CronTabPatch.

Three APIs are considered here:

  1. creating or importing a CustomResource using resource args
  2. getting an existing custom resource using the get helpers
  3. patching a CustomResource using the "Patch" variant

A summary of changes:

Examples

Here's some examples based on existing program code; the requirement is that the program code needn't be changed.

Go SDK

Observe that the value of ApiVersion is of type pulumi.String which is an Input-compatible alias for string but is not implicitly convertible to string. For this reason, the API was left unchanged and the user would see an error at runtime if the value weren't a prompt value.

        cr, err := apiextensions.NewCustomResource(ctx, "cr", &apiextensions.CustomResourceArgs{
            ApiVersion: pulumi.String("stable.example.com/v1"),
            Kind:       pulumi.String("CronJob"),
        }, pulumi.DependsOn([]pulumi.Resource{crd}), pulumi.Provider(provider))
        if err != nil {
            return err
        }

One of the Go SDK tests actually does use an output as a value, and that's still possible here but not in the other SDKs.

                cr, err := apiextensions.NewCustomResource(...)
                crGet, err := apiextensions.GetCustomResource(ctx, "crPatched",
                    pulumi.ID(fmt.Sprintf("%s/%s", *namespace, *name)), &apiextensions.CustomResourceState{
                        ApiVersion: cr.ApiVersion,
                        Kind:       cr.Kind,
                        Metadata:   cr.Metadata,
                    },
                    pulumi.Provider(provider))

.NET SDK

The program code creates a subclass of CustomResourceArgs, supplying the apiVersion and kind as string-type constructor arguments.

class CronTabArgs : CustomResourceArgs
{
    [Input("spec")]
    public Input<CronTabSpecArgs>? Spec { get; set; }

    public CronTabArgs() : base("dotnet.example.com/v1", "CronTab")
    {
    }
}

Closes #1115

Related issues (optional)

github-actions[bot] commented 3 days ago

Does the PR have any schema changes?

Looking good! No breaking changes found. No new resources/functions.

codecov[bot] commented 3 days ago

Codecov Report

All modified and coverable lines are covered by tests :white_check_mark:

Project coverage is 36.56%. Comparing base (deae4dc) to head (40eee79). Report is 1 commits behind head on master.

:exclamation: Current head 40eee79 differs from pull request most recent head 1195967

Please upload reports for the commit 1195967 to get more accurate results.

Additional details and impacted files ```diff @@ Coverage Diff @@ ## master #3079 +/- ## ========================================== - Coverage 36.65% 36.56% -0.09% ========================================== Files 71 71 Lines 9249 9259 +10 ========================================== - Hits 3390 3386 -4 - Misses 5522 5533 +11 - Partials 337 340 +3 ```

:umbrella: View full report in Codecov by Sentry.
:loudspeaker: Have feedback on the report? Share it here.