fix(action): avoid nil REST client getter panic when installing CRDs

Signed-off-by: sergiochan <yuheng9211@qq.com>
(cherry picked from commit 711a4eaf40)
pull/32552/head v3.21.1
sergiochan 7 months ago committed by Scott Rigby
parent 702529f90a
commit c56dd0095f
No known key found for this signature in database
GPG Key ID: C7C6FBB5B91C1155

@ -186,6 +186,7 @@ func (i *Install) installCRDs(crds []chart.CRD) error {
return err
}
if i.cfg.RESTClientGetter != nil {
// 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,
@ -196,12 +197,12 @@ func (i *Install) installCRDs(crds []chart.CRD) error {
if err != nil {
return err
}
if discoveryClient != nil {
i.cfg.Log("Clearing discovery cache")
discoveryClient.Invalidate()
_, _ = discoveryClient.ServerGroups()
}
}
// Invalidate the REST mapper, since it will not have the new CRDs
// present.
@ -214,6 +215,7 @@ func (i *Install) installCRDs(crds []chart.CRD) error {
resettable.Reset()
}
}
}
return nil
}

@ -894,6 +894,28 @@ func TestNameAndChartGenerateName(t *testing.T) {
}
}
func TestInstallCRDsWithNilRESTClientGetter(t *testing.T) {
config := actionConfigFixture(t)
failingKubeClient := kubefake.FailingKubeClient{PrintingKubeClient: kubefake.PrintingKubeClient{Out: io.Discard}, BuildDummy: true}
config.KubeClient = &failingKubeClient
config.RESTClientGetter = nil
instAction := NewInstall(config)
crds := []chart.CRD{{
Name: "test-crd",
File: &chart.File{
Name: "crds/test-crd.yaml",
Data: []byte("kind: CustomResourceDefinition"),
},
}}
var err error
require.NotPanics(t, func() {
err = instAction.installCRDs(crds)
})
require.NoError(t, err)
}
func TestInstallWithLabels(t *testing.T) {
is := assert.New(t)
instAction := installAction(t)

Loading…
Cancel
Save