mirror of
https://github.com/kubevela/kubevela.git
synced 2026-08-18 03:56:36 +00:00
Feat: use deferred config in CLI (#4083)
This commit is contained in:
@@ -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()
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user