Skip to content

Commit

Permalink
Add proxy configuration at the context level
Browse files Browse the repository at this point in the history
  • Loading branch information
placintaalexandru committed Apr 25, 2024
1 parent 7a45005 commit 3a816b3
Show file tree
Hide file tree
Showing 6 changed files with 47 additions and 7 deletions.
35 changes: 30 additions & 5 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,14 @@ package cmd
import (
"errors"
"fmt"
"net/http"
"net/url"
"os"
"runtime/debug"
"strings"

"github.com/derailed/k9s/internal/config/data"
"k8s.io/client-go/rest"
"k8s.io/client-go/tools/clientcmd/api"

"github.com/derailed/k9s/internal/client"
Expand Down Expand Up @@ -128,11 +131,6 @@ func loadConfiguration() (*config.Config, error) {
k8sCfg := client.NewConfig(k8sFlags)
k9sCfg := config.NewConfig(k8sCfg)
var errs error
conn, err := client.InitConnection(k8sCfg)
k9sCfg.SetConnection(conn)
if err != nil {
errs = errors.Join(errs, err)
}

if err := k9sCfg.Load(config.AppConfigFile, false); err != nil {
errs = errors.Join(errs, err)
Expand All @@ -142,14 +140,41 @@ func loadConfiguration() (*config.Config, error) {
log.Error().Err(err).Msgf("config refine failed")
errs = errors.Join(errs, err)
}

ctx, err := k9sCfg.CurrentContext()

if err != nil {
errs = errors.Join(errs, err)
}

if ctx.Proxy != nil {
k8sFlags.WrapConfigFn = func(cfg *rest.Config) *rest.Config {
cfg.Proxy = func(*http.Request) (*url.URL, error) {
log.Debug().Msgf("[Proxy]: %s", *&ctx.Proxy.Address)
return url.Parse(ctx.Proxy.Address)
}

return cfg
}
}

conn, err := client.InitConnection(k8sCfg)

if err != nil {
errs = errors.Join(errs, err)
}

// Try to access server version if that fail. Connectivity issue?
if !conn.CheckConnectivity() {
errs = errors.Join(errs, fmt.Errorf("cannot connect to context: %s", k9sCfg.K9s.ActiveContextName()))
}

if !conn.ConnectionOK() {
errs = errors.Join(errs, fmt.Errorf("k8s connection failed for context: %s", k9sCfg.K9s.ActiveContextName()))
}

k9sCfg.SetConnection(conn)

log.Info().Msg("✅ Kubernetes connectivity")
if err := k9sCfg.Save(false); err != nil {
log.Error().Err(err).Msg("Config save")
Expand Down
2 changes: 1 addition & 1 deletion internal/client/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ func (c *Config) CallTimeout() time.Duration {
}

func (c *Config) RESTConfig() (*restclient.Config, error) {
return c.clientConfig().ClientConfig()
return c.flags.ToRESTConfig()
}

// Flags returns configuration flags.
Expand Down
1 change: 1 addition & 0 deletions internal/config/data/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ type Context struct {
View *View `yaml:"view"`
FeatureGates FeatureGates `yaml:"featureGates"`
PortForwardAddress string `yaml:"portForwardAddress"`
Proxy *Proxy `yaml:"proxy"`
mx sync.RWMutex
}

Expand Down
6 changes: 6 additions & 0 deletions internal/config/data/proxy.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package data

// Proxy tracks a context's proxy configuration.
type Proxy struct {
Address string `yaml:"address"`
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,4 @@ k9s:
featureGates:
nodeShell: true
portForwardAddress: localhost
proxy: null
9 changes: 8 additions & 1 deletion internal/config/json/schemas/context.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,13 @@
"readOnly": {"type": "boolean"},
"skin": { "type": "string" },
"portForwardAddress": { "type": "string" },
"proxy": {
"type": "object",
"additionalProperties": false,
"properties": {
"address": {"type": "string"}
}
},
"namespace": {
"type": "object",
"additionalProperties": false,
Expand Down Expand Up @@ -41,4 +48,4 @@
}
},
"required": ["k9s"]
}
}

0 comments on commit 3a816b3

Please sign in to comment.