Skip to content
This repository was archived by the owner on Dec 15, 2021. It is now read-only.

877 allow controller to use token #885

Merged
3 changes: 1 addition & 2 deletions cmd/function-controller/function-controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ import (
"github.com/kubeless/kubeless/pkg/version"
"github.com/sirupsen/logrus"
"github.com/spf13/cobra"
"k8s.io/client-go/rest"
)

const (
Expand All @@ -53,7 +52,7 @@ var rootCmd = &cobra.Command{
FunctionClient: kubelessClient,
}

restCfg, err := rest.InClusterConfig()
restCfg, err := utils.GetOverriddenClientConfig()
if err != nil {
logrus.Fatalf("Cannot get REST client: %v", err)
}
Expand Down
14 changes: 11 additions & 3 deletions pkg/utils/k8sutil.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ const (

// GetClient returns a k8s clientset to the request from inside of cluster
func GetClient() kubernetes.Interface {
config, err := rest.InClusterConfig()
config, err := GetOverriddenClientConfig()
if err != nil {
logrus.Fatalf("Can not get kubernetes config: %v", err)
}
Expand Down Expand Up @@ -123,7 +123,11 @@ func GetAPIExtensionsClientOutOfCluster() clientsetAPIExtensions.Interface {

// GetAPIExtensionsClientInCluster returns a k8s clientset to access APIExtensions from inside of cluster
func GetAPIExtensionsClientInCluster() clientsetAPIExtensions.Interface {
config, err := rest.InClusterConfig()
config, err := GetOverriddenClientConfig()
if err != nil {
config, err = rest.InClusterConfig()
}

if err != nil {
logrus.Fatalf("Can not get kubernetes config: %v", err)
}
Expand All @@ -136,10 +140,14 @@ func GetAPIExtensionsClientInCluster() clientsetAPIExtensions.Interface {

// GetFunctionClientInCluster returns function clientset to the request from inside of cluster
func GetFunctionClientInCluster() (versioned.Interface, error) {
config, err := rest.InClusterConfig()
config, err := GetOverriddenClientConfig()
if err != nil {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the error could be related to something else, it's better to avoid having to retrieve the config twice. In fact if KUBELESS_TOKEN_FILE_PATH is not set you are already returning the result of rest.InClusterConfig() so you don't need this if block.

config, err = rest.InClusterConfig()
}
if err != nil {
return nil, err
}

kubelessClient, err := versioned.NewForConfig(config)
if err != nil {
return nil, err
Expand Down
19 changes: 19 additions & 0 deletions pkg/utils/kubelessutil.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ import (
kubelessApi "github.com/kubeless/kubeless/pkg/apis/kubeless/v1beta1"
"github.com/kubeless/kubeless/pkg/langruntime"
"github.com/sirupsen/logrus"
"io/ioutil"
batchv1 "k8s.io/api/batch/v1"
"k8s.io/api/core/v1"
"k8s.io/api/extensions/v1beta1"
Expand All @@ -40,6 +41,7 @@ import (
"k8s.io/apimachinery/pkg/types"
"k8s.io/apimachinery/pkg/util/intstr"
"k8s.io/client-go/kubernetes"
"k8s.io/client-go/rest"
)

// GetFunctionPort returns the port for a function service
Expand Down Expand Up @@ -760,6 +762,23 @@ func GetOwnerReference(kind, apiVersion, name string, uid types.UID) ([]metav1.O
}, nil
}

// GetControllerRestClientConfig returns necessary Config object to authenticate k8s clients if env variable is set
func GetOverriddenClientConfig() (*rest.Config, error) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd call this GetInClusterConfig so you don't need to know about what are you overriding.

config, err := rest.InClusterConfig()

tokenFile := os.Getenv("KUBELESS_TOKEN_FILE_PATH")
if len(tokenFile) == 0 {
return config, err
}
tokenBytes, err := ioutil.ReadFile(tokenFile)
if err != nil {
return nil, fmt.Errorf("unable to read file containing oauth token: %s", err)
}
config.BearerToken = string(tokenBytes)

return config, nil
}

func getConfigLocation(apiExtensionsClientset clientsetAPIExtensions.Interface) (ConfigLocation, error) {
configLocation := ConfigLocation{}
controllerNamespace := os.Getenv("KUBELESS_NAMESPACE")
Expand Down