Skip to content

Commit

Permalink
Improve openapi lazy loading to not refetch
Browse files Browse the repository at this point in the history
  • Loading branch information
rquitales committed Dec 12, 2023
1 parent 7aa82a7 commit ad58335
Showing 1 changed file with 11 additions and 5 deletions.
16 changes: 11 additions & 5 deletions provider/pkg/provider/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -195,16 +195,22 @@ func (k *kubeProvider) getResources() (k8sopenapi.Resources, error) {
return k.resources, nil
}

// openAPIResourcesGetter is a simple implementation of k8sopenapi.OpenAPIResourcesGetter that returns a fixed set of
// resources. Our current use case already has the resources available, so we just need to wrap them in this struct to
// satisfy the interface.
type openAPIResourcesGetter struct {
k *kubeProvider
resources k8sopenapi.Resources
}

func (o *openAPIResourcesGetter) OpenAPISchema() (k8sopenapi.Resources, error) {
return o.k.getResources()
return o.resources, nil
}

func (k *kubeProvider) getResourcesLazy() k8sopenapi.OpenAPIResourcesGetter {
return &openAPIResourcesGetter{k}
// getResourcesLazy returns a openAPIResourcesGetter which implements k8sopenapi.OpenAPIResourcesGetter. This is
// required since the upstream openapi.ValidateAgainstSchema function requires a k8sopenapi.OpenAPIResourcesGetter since
// v0.29.0 of the k8s.io/kubectl library for lazy loading.
func (k *kubeProvider) getResourcesLazy(resources k8sopenapi.Resources) k8sopenapi.OpenAPIResourcesGetter {
return &openAPIResourcesGetter{resources}
}

func (k *kubeProvider) invalidateResources() {
Expand Down Expand Up @@ -1442,7 +1448,7 @@ func (k *kubeProvider) Check(ctx context.Context, req *pulumirpc.CheckRequest) (
}

// Validate the object according to the OpenAPI schema for its GVK.
err = openapi.ValidateAgainstSchema(k.getResourcesLazy(), resources, newInputs)
err = openapi.ValidateAgainstSchema(k.getResourcesLazy(resources), resources, newInputs)
if err != nil {
resourceNotFound := apierrors.IsNotFound(err) ||
strings.Contains(err.Error(), "is not supported by the server")
Expand Down

0 comments on commit ad58335

Please sign in to comment.