Skip to content

Commit

Permalink
fix(builder): initializes rest config before invoking any partial bui…
Browse files Browse the repository at this point in the history
…lder (opendatahub-io#792)

This way we can obtain interact with the cluster from partial builders instead of defering it to Apply phase.
  • Loading branch information
bartoszmajsak authored and zdtsw committed Jan 15, 2024
1 parent 94fbc7b commit 0b5f7ee
Showing 1 changed file with 32 additions and 23 deletions.
55 changes: 32 additions & 23 deletions pkg/feature/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ type partialBuilder func(f *Feature) error

type featureBuilder struct {
name string
config *rest.Config
builders []partialBuilder
}

Expand All @@ -43,8 +44,7 @@ func (fb *featureBuilder) For(spec *v1.DSCInitializationSpec) *featureBuilder {
}

func (fb *featureBuilder) UsingConfig(config *rest.Config) *featureBuilder {
fb.builders = append(fb.builders, createClients(config))

fb.config = config
return fb
}

Expand Down Expand Up @@ -150,32 +150,20 @@ func (fb *featureBuilder) Load() (*Feature, error) {
Enabled: true,
}

for i := range fb.builders {
if err := fb.builders[i](feature); err != nil {
return nil, err
}
}

// UsingConfig builder wasn't called while constructing this feature.
// Get default settings and create needed clients.
if feature.Client == nil {
restCfg, err := config.GetConfig()
if errors.Is(err, rest.ErrNotInCluster) {
// rollback to local kubeconfig - this can be helpful when running the process locally i.e. while debugging
kubeconfig := clientcmd.NewNonInteractiveDeferredLoadingClientConfig(
&clientcmd.ClientConfigLoadingRules{ExplicitPath: clientcmd.RecommendedHomeFile},
&clientcmd.ConfigOverrides{},
)

restCfg, err = kubeconfig.ClientConfig()
if err != nil {
return nil, err
}
} else if err != nil {
if fb.config == nil {
if err := fb.withDefaultClient(); err != nil {
return nil, err
}
}

if err := createClients(fb.config)(feature); err != nil {
return nil, err
}

if err := createClients(restCfg)(feature); err != nil {
for i := range fb.builders {
if err := fb.builders[i](feature); err != nil {
return nil, err
}
}
Expand All @@ -188,3 +176,24 @@ func (fb *featureBuilder) Load() (*Feature, error) {

return feature, nil
}

func (fb *featureBuilder) withDefaultClient() error {
restCfg, err := config.GetConfig()
if errors.Is(err, rest.ErrNotInCluster) {
// rollback to local kubeconfig - this can be helpful when running the process locally i.e. while debugging
kubeconfig := clientcmd.NewNonInteractiveDeferredLoadingClientConfig(
&clientcmd.ClientConfigLoadingRules{ExplicitPath: clientcmd.RecommendedHomeFile},
&clientcmd.ConfigOverrides{},
)

restCfg, err = kubeconfig.ClientConfig()
if err != nil {
return err
}
} else if err != nil {
return err
}

fb.config = restCfg
return nil
}

0 comments on commit 0b5f7ee

Please sign in to comment.