mirror of
https://github.com/portainer/portainer.git
synced 2026-08-07 11:04:49 +00:00
chore(addons): allow local k8s environment in development mode [r8s-1148] (#3177)
This commit is contained in:
@@ -5,6 +5,7 @@ import (
|
||||
"fmt"
|
||||
"net"
|
||||
"net/http"
|
||||
"os"
|
||||
"strconv"
|
||||
"strings"
|
||||
"sync"
|
||||
@@ -373,7 +374,7 @@ func (factory *ClientFactory) CreateRemoteMetricsClient(endpoint *portainer.Endp
|
||||
}
|
||||
|
||||
func buildLocalConfig() (*rest.Config, error) {
|
||||
config, err := rest.InClusterConfig()
|
||||
config, err := buildLocalRestConfig()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@@ -384,6 +385,17 @@ func buildLocalConfig() (*rest.Config, error) {
|
||||
return config, nil
|
||||
}
|
||||
|
||||
// buildLocalRestConfig returns the in-cluster config, or a config built from
|
||||
// the kubeconfig at DEV_KUBECONFIG_PATH so developers can run the server
|
||||
// locally against a local cluster (same convention as the agent).
|
||||
func buildLocalRestConfig() (*rest.Config, error) {
|
||||
if devKubeConfigPath := os.Getenv("DEV_KUBECONFIG_PATH"); devKubeConfigPath != "" {
|
||||
return clientcmd.BuildConfigFromFlags("", devKubeConfigPath)
|
||||
}
|
||||
|
||||
return rest.InClusterConfig()
|
||||
}
|
||||
|
||||
func (factory *ClientFactory) MigrateEndpointIngresses(e *portainer.Endpoint, datastore dataservices.DataStore, cli *KubeClient) error {
|
||||
return datastore.UpdateTx(func(tx dataservices.DataStoreTx) error {
|
||||
environment, err := tx.Endpoint().Endpoint(e.ID)
|
||||
|
||||
@@ -1,7 +1,12 @@
|
||||
package cli
|
||||
|
||||
import (
|
||||
"os"
|
||||
"testing"
|
||||
|
||||
"github.com/portainer/portainer/api/filesystem"
|
||||
"github.com/stretchr/testify/require"
|
||||
"k8s.io/client-go/rest"
|
||||
)
|
||||
|
||||
func TestClearUserClientCache(t *testing.T) {
|
||||
@@ -21,3 +26,42 @@ func TestClearUserClientCache(t *testing.T) {
|
||||
t.Errorf("Expected not to find client cache for user after clear")
|
||||
}
|
||||
}
|
||||
|
||||
func TestBuildLocalRestConfig(t *testing.T) {
|
||||
t.Run("uses the kubeconfig at DEV_KUBECONFIG_PATH when set", func(t *testing.T) {
|
||||
kubeconfigPath := filesystem.JoinPaths(t.TempDir(), "kubeconfig")
|
||||
require.NoError(t, os.WriteFile(kubeconfigPath, []byte(`
|
||||
apiVersion: v1
|
||||
kind: Config
|
||||
clusters:
|
||||
- name: local
|
||||
cluster:
|
||||
server: https://127.0.0.1:26443
|
||||
insecure-skip-tls-verify: true
|
||||
contexts:
|
||||
- name: local
|
||||
context:
|
||||
cluster: local
|
||||
user: local
|
||||
current-context: local
|
||||
users:
|
||||
- name: local
|
||||
user:
|
||||
token: fake-token
|
||||
`), 0o600))
|
||||
t.Setenv("DEV_KUBECONFIG_PATH", kubeconfigPath)
|
||||
|
||||
config, err := buildLocalRestConfig()
|
||||
require.NoError(t, err)
|
||||
require.Equal(t, "https://127.0.0.1:26443", config.Host)
|
||||
})
|
||||
|
||||
t.Run("falls back to the in-cluster config when unset", func(t *testing.T) {
|
||||
t.Setenv("DEV_KUBECONFIG_PATH", "")
|
||||
t.Setenv("KUBERNETES_SERVICE_HOST", "")
|
||||
t.Setenv("KUBERNETES_SERVICE_PORT", "")
|
||||
|
||||
_, err := buildLocalRestConfig()
|
||||
require.ErrorIs(t, err, rest.ErrNotInCluster)
|
||||
})
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user