Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -275,7 +275,7 @@ func newOperatorConfigAndDeps(id, namespace, name string) (operator.Config, oper
if err != nil {
return operator.Config{}, operator.Dependencies{}, maskAny(fmt.Errorf("Failed to create k8b api extensions client: %s", err))
}
crCli, err := client.NewInCluster()
crCli, err := client.NewClient()
if err != nil {
return operator.Config{}, operator.Dependencies{}, maskAny(fmt.Errorf("Failed to created versioned client: %s", err))
}
Expand Down
12 changes: 6 additions & 6 deletions pkg/client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,10 @@ var (
maskAny = errors.WithStack
)

// MustNewInCluster creates an in-cluster client, or panics
// MustNewClient creates an client, or panics
// when a failure is detected.
func MustNewInCluster() versioned.Interface {
cli, err := NewInCluster()
func MustNewClient() versioned.Interface {
cli, err := NewClient()
if err != nil {
panic(err)
}
Expand All @@ -54,10 +54,10 @@ func MustNew(cfg *rest.Config) versioned.Interface {
return cli
}

// NewInCluster creates an in-cluster client, or returns an error
// NewClient creates an client, or returns an error
// when a failure is detected.
func NewInCluster() (versioned.Interface, error) {
cfg, err := k8sutil.InClusterConfig()
func NewClient() (versioned.Interface, error) {
cfg, err := k8sutil.NewKubeConfig()
if err != nil {
return nil, maskAny(err)
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/deployment/resources/servicemonitor.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ func (r *Resources) EnsureMonitoringClient() (*clientv1.MonitoringV1Client, erro

// Make a client:
var restConfig *rest.Config
restConfig, err := k8sutil.InClusterConfig()
restConfig, err := k8sutil.NewKubeConfig()
if err != nil {
return nil, maskAny(err)
}
Expand Down
55 changes: 31 additions & 24 deletions pkg/util/k8sutil/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,17 +23,45 @@
package k8sutil

import (
"net"
"fmt"
"os"

"github.com/arangodb/kube-arangodb/pkg/util"
"k8s.io/client-go/tools/clientcmd"

apiextensionsclient "k8s.io/apiextensions-apiserver/pkg/client/clientset/clientset"
"k8s.io/client-go/kubernetes"
"k8s.io/client-go/rest"
)

const Kubeconfig util.EnvironmentVariable = "KUBECONFIG"

// NewKubeConfig loads config from KUBECONFIG or as incluster
func NewKubeConfig() (*rest.Config, error) {
// If KUBECONFIG is defined use this variable
if kubeconfig, ok := Kubeconfig.Lookup(); ok {
return clientcmd.BuildConfigFromFlags("", kubeconfig)
}

// Try to load incluster config
if cfg, err := rest.InClusterConfig(); err == nil {
return cfg, nil
} else if err != rest.ErrNotInCluster {
return nil, err
}

// At the end try to use default path
home, err := os.UserHomeDir()
if err != nil {
return nil, err
}

return clientcmd.BuildConfigFromFlags("", fmt.Sprintf("%s/.kube/config", home))
}

// NewKubeClient creates a new k8s client
func NewKubeClient() (kubernetes.Interface, error) {
cfg, err := InClusterConfig()
cfg, err := NewKubeConfig()
if err != nil {
return nil, maskAny(err)
}
Expand All @@ -55,7 +83,7 @@ func MustNewKubeClient() kubernetes.Interface {

// NewKubeExtClient creates a new k8s api extensions client
func NewKubeExtClient() (apiextensionsclient.Interface, error) {
cfg, err := InClusterConfig()
cfg, err := NewKubeConfig()
if err != nil {
return nil, maskAny(err)
}
Expand All @@ -65,24 +93,3 @@ func NewKubeExtClient() (apiextensionsclient.Interface, error) {
}
return c, nil
}

// InClusterConfig loads the environment into a rest config.
func InClusterConfig() (*rest.Config, error) {
// Work around https://github.com/kubernetes/kubernetes/issues/40973
// See https://github.com/coreos/etcd-operator/issues/731#issuecomment-283804819
if len(os.Getenv("KUBERNETES_SERVICE_HOST")) == 0 {
addrs, err := net.LookupHost("kubernetes.default.svc")
if err != nil {
return nil, maskAny(err)
}
os.Setenv("KUBERNETES_SERVICE_HOST", addrs[0])
}
if len(os.Getenv("KUBERNETES_SERVICE_PORT")) == 0 {
os.Setenv("KUBERNETES_SERVICE_PORT", "443")
}
cfg, err := rest.InClusterConfig()
if err != nil {
return nil, maskAny(err)
}
return cfg, nil
}
2 changes: 1 addition & 1 deletion reboot.go
Original file line number Diff line number Diff line change
Expand Up @@ -386,7 +386,7 @@ func cmdRebootRun(cmd *cobra.Command, args []string) {
cliLog.Fatal().Err(err).Msg("Failed to create Kubernetes client")
}

extcli, err := extclient.NewInCluster()
extcli, err := extclient.NewClient()
if err != nil {
cliLog.Fatal().Err(err).Msg("failed to create arango extension client")
}
Expand Down
2 changes: 1 addition & 1 deletion tests/annotations_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,7 @@ func ensureAnnotations(t *testing.T, client kubernetes.Interface, depl *api.Aran

func TestAnnotations(t *testing.T) {
longOrSkip(t)
c := client.MustNewInCluster()
c := client.MustNewClient()
kubecli := mustNewKubeClient(t)
ns := getNamespace(t)

Expand Down
12 changes: 6 additions & 6 deletions tests/auth_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ import (
// with default authentication (on) using a generated JWT secret.
func TestAuthenticationSingleDefaultSecret(t *testing.T) {
longOrSkip(t)
c := client.MustNewInCluster()
c := client.MustNewClient()
kubecli := mustNewKubeClient(t)
ns := getNamespace(t)

Expand Down Expand Up @@ -89,7 +89,7 @@ func TestAuthenticationSingleDefaultSecret(t *testing.T) {
// with default authentication (on) using a user created JWT secret.
func TestAuthenticationSingleCustomSecret(t *testing.T) {
longOrSkip(t)
c := client.MustNewInCluster()
c := client.MustNewClient()
kubecli := mustNewKubeClient(t)
ns := getNamespace(t)
secrets := kubecli.CoreV1().Secrets(ns)
Expand Down Expand Up @@ -140,7 +140,7 @@ func TestAuthenticationSingleCustomSecret(t *testing.T) {
// with authentication set to `None`.
func TestAuthenticationNoneSingle(t *testing.T) {
longOrSkip(t)
c := client.MustNewInCluster()
c := client.MustNewClient()
kubecli := mustNewKubeClient(t)
ns := getNamespace(t)

Expand Down Expand Up @@ -179,7 +179,7 @@ func TestAuthenticationNoneSingle(t *testing.T) {
// with default authentication (on) using a generated JWT secret.
func TestAuthenticationClusterDefaultSecret(t *testing.T) {
longOrSkip(t)
c := client.MustNewInCluster()
c := client.MustNewClient()
kubecli := mustNewKubeClient(t)
ns := getNamespace(t)

Expand Down Expand Up @@ -227,7 +227,7 @@ func TestAuthenticationClusterDefaultSecret(t *testing.T) {
// with default authentication (on) using a user created JWT secret.
func TestAuthenticationClusterCustomSecret(t *testing.T) {
longOrSkip(t)
c := client.MustNewInCluster()
c := client.MustNewClient()
kubecli := mustNewKubeClient(t)
ns := getNamespace(t)
secrets := kubecli.CoreV1().Secrets(ns)
Expand Down Expand Up @@ -280,7 +280,7 @@ func TestAuthenticationClusterCustomSecret(t *testing.T) {
// with authentication set to `None`.
func TestAuthenticationNoneCluster(t *testing.T) {
longOrSkip(t)
c := client.MustNewInCluster()
c := client.MustNewClient()
kubecli := mustNewKubeClient(t)
ns := getNamespace(t)

Expand Down
4 changes: 2 additions & 2 deletions tests/backup_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -317,9 +317,9 @@ func compareBackup(t *testing.T, meta driver.BackupMeta, backup *backupApi.Arang

func TestBackupCluster(t *testing.T) {
longOrSkip(t)
c := client.MustNewInCluster()
c := client.MustNewClient()
kubecli := mustNewKubeClient(t)
deploymentClient := kubeArangoClient.MustNewInCluster()
deploymentClient := kubeArangoClient.MustNewClient()
ns := getNamespace(t)

backupPolicyClient := deploymentClient.BackupV1().ArangoBackupPolicies(ns)
Expand Down
4 changes: 2 additions & 2 deletions tests/change_args_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ import (
// with default settings and once ready changes the arguments of the agents.
func TestChangeArgsAgents(t *testing.T) {
longOrSkip(t)
c := client.MustNewInCluster()
c := client.MustNewClient()
kubecli := mustNewKubeClient(t)
ns := getNamespace(t)

Expand Down Expand Up @@ -121,7 +121,7 @@ func TestChangeArgsAgents(t *testing.T) {
// with default settings and once ready changes the arguments of the dbservers.
func TestChangeArgsDBServer(t *testing.T) {
longOrSkip(t)
c := client.MustNewInCluster()
c := client.MustNewClient()
kubecli := mustNewKubeClient(t)
ns := getNamespace(t)

Expand Down
6 changes: 3 additions & 3 deletions tests/cursor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ import (
// with default settings and runs some cursor requests on it.
func TestCursorSingle(t *testing.T) {
longOrSkip(t)
c := client.MustNewInCluster()
c := client.MustNewClient()
kubecli := mustNewKubeClient(t)
ns := getNamespace(t)

Expand Down Expand Up @@ -84,7 +84,7 @@ func TestCursorSingle(t *testing.T) {
// with default settings.
func TestCursorActiveFailover(t *testing.T) {
longOrSkip(t)
c := client.MustNewInCluster()
c := client.MustNewClient()
kubecli := mustNewKubeClient(t)
ns := getNamespace(t)

Expand Down Expand Up @@ -128,7 +128,7 @@ func TestCursorActiveFailover(t *testing.T) {
// with default settings.
func TestCursorCluster(t *testing.T) {
longOrSkip(t)
c := client.MustNewInCluster()
c := client.MustNewClient()
kubecli := mustNewKubeClient(t)
ns := getNamespace(t)

Expand Down
4 changes: 2 additions & 2 deletions tests/deployments_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ func deploymentSubTest(t *testing.T, mode api.DeploymentMode, engine api.Storage

ns := getNamespace(t)
kubecli := mustNewKubeClient(t)
c := kubeArangoClient.MustNewInCluster()
c := kubeArangoClient.MustNewClient()

// Prepare deployment config
depl := newDeployment("test-deployment-" + string(mode) + "-" + string(engine) + "-" + uniuri.NewLen(4))
Expand Down Expand Up @@ -107,7 +107,7 @@ func TestMultiDeployment(t *testing.T) {

ns := getNamespace(t)
kubecli := mustNewKubeClient(t)
c := kubeArangoClient.MustNewInCluster()
c := kubeArangoClient.MustNewClient()

// Prepare deployment configurations
depl1 := newDeployment("test-multidep-1-" + uniuri.NewLen(4))
Expand Down
2 changes: 1 addition & 1 deletion tests/environments_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ func TestEnvironmentProduction(t *testing.T) {
}
numNodes := len(nodeList.Items)

c := kubeArangoClient.MustNewInCluster()
c := kubeArangoClient.MustNewClient()
depl := newDeployment(strings.Replace(fmt.Sprintf("tprod-%s-%s-%s", mode[:2], engine[:2], uniuri.NewLen(4)), ".", "", -1))
depl.Spec.Mode = api.NewMode(mode)
depl.Spec.StorageEngine = api.NewStorageEngine(engine)
Expand Down
2 changes: 1 addition & 1 deletion tests/immutable_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ import (
// spec are reverted to their original value.
func TestImmutableFields(t *testing.T) {
longOrSkip(t)
c := client.MustNewInCluster()
c := client.MustNewClient()
kubecli := mustNewKubeClient(t)
ns := getNamespace(t)
revertTimeout := time.Second * 30
Expand Down
2 changes: 1 addition & 1 deletion tests/load_balancer_source_ranges_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ import (
func TestLoadBalancingSourceRanges(t *testing.T) {
longOrSkip(t)

c := client.MustNewInCluster()
c := client.MustNewClient()
kubecli := mustNewKubeClient(t)
ns := getNamespace(t)

Expand Down
2 changes: 1 addition & 1 deletion tests/load_balancer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ func wasForwarded(r driver.Response) bool {

// tests cursor forwarding with load-balanced conn.
func loadBalancingCursorSubtest(t *testing.T, useVst bool) {
c := client.MustNewInCluster()
c := client.MustNewClient()
kubecli := mustNewKubeClient(t)
ns := getNamespace(t)

Expand Down
6 changes: 3 additions & 3 deletions tests/member_resilience_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ import (
// After 5 times, the member should be replaced by another member with the same ID.
func TestMemberResilienceAgents(t *testing.T) {
longOrSkip(t)
c := client.MustNewInCluster()
c := client.MustNewClient()
kubecli := mustNewKubeClient(t)
ns := getNamespace(t)

Expand Down Expand Up @@ -143,7 +143,7 @@ func TestMemberResilienceAgents(t *testing.T) {
// After 5 times, the member should be replaced by another member.
func TestMemberResilienceCoordinators(t *testing.T) {
longOrSkip(t)
c := client.MustNewInCluster()
c := client.MustNewClient()
kubecli := mustNewKubeClient(t)
ns := getNamespace(t)

Expand Down Expand Up @@ -240,7 +240,7 @@ func TestMemberResilienceCoordinators(t *testing.T) {
// After 5 times, the member should be replaced by another member.
func TestMemberResilienceDBServers(t *testing.T) {
longOrSkip(t)
c := client.MustNewInCluster()
c := client.MustNewClient()
kubecli := mustNewKubeClient(t)
ns := getNamespace(t)

Expand Down
2 changes: 1 addition & 1 deletion tests/metrics_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ func TestAddingMetrics(t *testing.T) {

ns := getNamespace(t)
kubecli := mustNewKubeClient(t)
c := kubeArangoClient.MustNewInCluster()
c := kubeArangoClient.MustNewClient()

depl := newDeployment(fmt.Sprintf("%s-%s", "arangodb-metrics-test", uniuri.NewLen(4)))
depl.Spec.Mode = api.NewMode(api.DeploymentModeCluster)
Expand Down
2 changes: 1 addition & 1 deletion tests/operator_upgrade_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ const (
func TestOperatorUpgradeFrom038(t *testing.T) {
ns := getNamespace(t)
kubecli := mustNewKubeClient(t)
c := kubeArangoClient.MustNewInCluster()
c := kubeArangoClient.MustNewClient()

if err := waitForArangoDBPodsGone(ns, kubecli); err != nil {
t.Fatalf("Remaining arangodb pods did not vanish, can not start test: %v", err)
Expand Down
2 changes: 1 addition & 1 deletion tests/pc_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ func waitForPriorityOfServerGroup(kube kubernetes.Interface, c versioned.Interfa
// Then check if the pods have the desired priority. Then change the class and check that the pods are rotated.
func TestPriorityClasses(t *testing.T) {
longOrSkip(t)
c := client.MustNewInCluster()
c := client.MustNewClient()
kubecli := mustNewKubeClient(t)
ns := getNamespace(t)

Expand Down
2 changes: 1 addition & 1 deletion tests/pdb_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ func waitForPDBsOfDeployment(kube kubernetes.Interface, apiObject *api.ArangoDep
// modified accordingly.
func TestPDBCreate(t *testing.T) {
longOrSkip(t)
c := client.MustNewInCluster()
c := client.MustNewClient()
kubecli := mustNewKubeClient(t)
ns := getNamespace(t)

Expand Down
Loading