Merge pull request #11886 from hiddeco/properly-invalidate-client

Properly invalidate client after CRD install
pull/9539/merge
Matt Farina 2 years ago committed by GitHub
commit 299a6463a0
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -34,6 +34,7 @@ import (
"github.com/pkg/errors" "github.com/pkg/errors"
v1 "k8s.io/api/core/v1" v1 "k8s.io/api/core/v1"
apierrors "k8s.io/apimachinery/pkg/api/errors" apierrors "k8s.io/apimachinery/pkg/api/errors"
"k8s.io/apimachinery/pkg/api/meta"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/cli-runtime/pkg/resource" "k8s.io/cli-runtime/pkg/resource"
"sigs.k8s.io/yaml" "sigs.k8s.io/yaml"
@ -164,22 +165,38 @@ func (i *Install) installCRDs(crds []chart.CRD) error {
totalItems = append(totalItems, res...) totalItems = append(totalItems, res...)
} }
if len(totalItems) > 0 { if len(totalItems) > 0 {
// Invalidate the local cache, since it will not have the new CRDs // Give time for the CRD to be recognized.
// present. if err := i.cfg.KubeClient.Wait(totalItems, 60*time.Second); err != nil {
return err
}
// If we have already gathered the capabilities, we need to invalidate
// the cache so that the new CRDs are recognized. This should only be
// the case when an action configuration is reused for multiple actions,
// as otherwise it is later loaded by ourselves when getCapabilities
// is called later on in the installation process.
if i.cfg.Capabilities != nil {
discoveryClient, err := i.cfg.RESTClientGetter.ToDiscoveryClient() discoveryClient, err := i.cfg.RESTClientGetter.ToDiscoveryClient()
if err != nil { if err != nil {
return err return err
} }
i.cfg.Log("Clearing discovery cache") i.cfg.Log("Clearing discovery cache")
discoveryClient.Invalidate() discoveryClient.Invalidate()
// Give time for the CRD to be recognized.
if err := i.cfg.KubeClient.Wait(totalItems, 60*time.Second); err != nil { _, _ = discoveryClient.ServerGroups()
return err
} }
// Make sure to force a rebuild of the cache. // Invalidate the REST mapper, since it will not have the new CRDs
discoveryClient.ServerGroups() // present.
restMapper, err := i.cfg.RESTClientGetter.ToRESTMapper()
if err != nil {
return err
}
if resettable, ok := restMapper.(meta.ResettableRESTMapper); ok {
i.cfg.Log("Clearing REST mapper cache")
resettable.Reset()
}
} }
return nil return nil
} }

Loading…
Cancel
Save