Skip to content

Commit

Permalink
Allow initializing the Kubernetes client with a kubeconfig
Browse files Browse the repository at this point in the history
This makes it so that the API server will first try the in-cluster
configuration and fallback to a kubeconfig. This is helpful when the API
server is running outside of the cluster (e.g. locally) so you don't
need to create token files.

Signed-off-by: mprahl <[email protected]>
  • Loading branch information
mprahl committed Dec 5, 2024
1 parent 22e85de commit 7ee7f75
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 4 deletions.
6 changes: 3 additions & 3 deletions backend/src/apiserver/client/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,15 @@
package client

import (
"os"

"github.com/kubeflow/pipelines/backend/src/common/util"
"github.com/pkg/errors"
"k8s.io/client-go/kubernetes"
"k8s.io/client-go/rest"
"os"
)

func getKubernetesClientset(clientParams util.ClientParameters) (*kubernetes.Clientset, error) {
restConfig, err := rest.InClusterConfig()
restConfig, err := util.GetKubernetesConfig()
if err != nil {
return nil, errors.Wrap(err, "Failed to initialize kubernetes client")
}
Expand Down
2 changes: 1 addition & 1 deletion backend/src/common/util/execution_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ func NewExecutionClientOrFatal(execType ExecutionType, initConnectionTimeout tim
case ArgoWorkflow:
var argoProjClient *argoclient.Clientset
operation := func() error {
restConfig, err := rest.InClusterConfig()
restConfig, err := GetKubernetesConfig()
if err != nil {
return errors.Wrap(err, "Failed to initialize the RestConfig")
}
Expand Down
20 changes: 20 additions & 0 deletions backend/src/common/util/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,26 @@ func WaitForAPIAvailable(initializeTimeout time.Duration, basePath string, apiAd
return errors.Wrapf(err, "Waiting for ml pipeline API server failed after all attempts.")
}

// GetKubernetesConfig will first try an in-cluster configuration but fallback to using a kubeconfig.
func GetKubernetesConfig() (*rest.Config, error) {
restConfig, errInCluster := rest.InClusterConfig()
if errInCluster == nil {
return restConfig, nil
}

// Fallback to using a kubeconfig
clientConfig := clientcmd.NewNonInteractiveDeferredLoadingClientConfig(
clientcmd.NewDefaultClientConfigLoadingRules(), &clientcmd.ConfigOverrides{},
)

restConfig, errKubeconfig := clientConfig.ClientConfig()
if errKubeconfig != nil {
return nil, fmt.Errorf("%w; %w", errInCluster, errKubeconfig)
}

return restConfig, nil
}

func GetKubernetesClientFromClientConfig(clientConfig clientcmd.ClientConfig) (
*kubernetes.Clientset, *rest.Config, string, error,
) {
Expand Down

0 comments on commit 7ee7f75

Please sign in to comment.