Feat: use deferred config in CLI (#4083)

This commit is contained in:
Somefive
2022-06-01 08:31:01 +08:00
committed by GitHub
parent f04f29dff0
commit 985d49d9e6
2 changed files with 39 additions and 1 deletions
+37
View File
@@ -99,3 +99,40 @@ func NewDefaultFactory(cfg *rest.Config) Factory {
copiedCfg.Wrap(multicluster.NewSecretModeMultiClusterRoundTripper)
return &defaultFactory{cfg: &copiedCfg}
}
type deferredFactory struct {
sync.Mutex
Factory
ConfigGetter
}
// NewDeferredFactory create a factory that will only get KubeConfig until it is needed for the first time
func NewDeferredFactory(getter ConfigGetter) Factory {
return &deferredFactory{ConfigGetter: getter}
}
func (f *deferredFactory) init() {
cfg, err := f.ConfigGetter()
cmdutil.CheckErr(err)
f.Factory = NewDefaultFactory(cfg)
}
// Config return the kubeConfig
func (f *deferredFactory) Config() *rest.Config {
f.Lock()
defer f.Unlock()
if f.Factory == nil {
f.init()
}
return f.Factory.Config()
}
// Client return the kubeClient
func (f *deferredFactory) Client() client.Client {
f.Lock()
defer f.Unlock()
if f.Factory == nil {
f.init()
}
return f.Factory.Client()
}