Added support for kube-context flag, to specify kubernetes context (#1595)

This commit is contained in:
Rahul Bhati
2020-05-11 18:54:06 +05:30
committed by GitHub
parent 3e8cbe112f
commit 2d69542265
8 changed files with 26 additions and 20 deletions
+4 -4
View File
@@ -40,7 +40,7 @@ import (
// is found by looking for a service in the same namespace and using
// its targetPort. Once the port forward is started, wait for it to
// start accepting connections before returning.
func SetupPortForward(namespace, labelSelector string) (string, error) {
func SetupPortForward(namespace, labelSelector string, kubeContext string) (string, error) {
console.Verbose(2, "Setting up port forward to %s in namespace %s",
labelSelector, namespace)
@@ -63,7 +63,7 @@ func SetupPortForward(namespace, labelSelector string) (string, error) {
console.Verbose(2, "Starting port forward from local port %v", localPort)
go func() {
err := runPortForward(labelSelector, localPort, namespace)
err := runPortForward(labelSelector, localPort, namespace, kubeContext)
if err != nil {
fmt.Printf("Error forwarding to port %v: %s", localPort, err.Error())
os.Exit(1)
@@ -103,8 +103,8 @@ func findFreePort() (string, error) {
}
// runPortForward creates a local port forward to the specified pod
func runPortForward(labelSelector string, localPort string, ns string) error {
config, clientset, err := GetKubernetesClient()
func runPortForward(labelSelector string, localPort string, ns string, kubeContext string) error {
config, clientset, err := GetKubernetesClient(kubeContext)
if err != nil {
return err
}
+8 -6
View File
@@ -18,7 +18,6 @@ package util
import (
"fmt"
"github.com/fission/fission/pkg/controller/client/rest"
"os"
"os/user"
"path/filepath"
@@ -26,6 +25,8 @@ import (
"strconv"
"strings"
"github.com/fission/fission/pkg/controller/client/rest"
"github.com/hashicorp/go-multierror"
"github.com/pkg/errors"
v1 "k8s.io/api/core/v1"
@@ -49,13 +50,13 @@ func GetFissionNamespace() string {
return fissionNamespace
}
func GetApplicationUrl(selector string) (string, error) {
func GetApplicationUrl(selector string, kubeContext string) (string, error) {
var serverUrl string
// Use FISSION_URL env variable if set; otherwise, port-forward to controller.
fissionUrl := os.Getenv("FISSION_URL")
if len(fissionUrl) == 0 {
fissionNamespace := GetFissionNamespace()
localPort, err := SetupPortForward(fissionNamespace, selector)
localPort, err := SetupPortForward(fissionNamespace, selector, kubeContext)
if err != nil {
return "", err
}
@@ -102,7 +103,7 @@ func KubifyName(old string) string {
// GetKubernetesClient builds a new kubernetes client. If the KUBECONFIG
// environment variable is empty or doesn't exist, ~/.kube/config is used for
// the kube config path
func GetKubernetesClient() (*restclient.Config, *kubernetes.Clientset, error) {
func GetKubernetesClient(kubeContext string) (*restclient.Config, *kubernetes.Clientset, error) {
loadingRules := clientcmd.NewDefaultClientConfigLoadingRules()
kubeConfigPath := os.Getenv("KUBECONFIG")
@@ -131,7 +132,7 @@ func GetKubernetesClient() (*restclient.Config, *kubernetes.Clientset, error) {
}
config, err := clientcmd.NewNonInteractiveDeferredLoadingClientConfig(
loadingRules, &clientcmd.ConfigOverrides{}).ClientConfig()
loadingRules, &clientcmd.ConfigOverrides{CurrentContext: kubeContext}).ClientConfig()
if err != nil {
return nil, nil, errors.Wrap(err, "Failed to build Kubernetes config")
}
@@ -206,9 +207,10 @@ func GetServer(input cli.Input) (c client.Interface, err error) {
func GetServerURL(input cli.Input) (serverUrl string, err error) {
serverUrl = input.GlobalString(flagkey.Server)
kubeContext := input.String(flagkey.KubeContext)
if len(serverUrl) == 0 {
// starts local portforwarder etc.
serverUrl, err = GetApplicationUrl("application=fission-api")
serverUrl, err = GetApplicationUrl("application=fission-api", kubeContext)
if err != nil {
return "", err
}