This repository has been archived by the owner on Mar 16, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 100
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Darren Shepherd <[email protected]>
- Loading branch information
1 parent
e68fb04
commit 6a839cb
Showing
10 changed files
with
241 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
package cli | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"os" | ||
"os/exec" | ||
|
||
cli "github.com/acorn-io/runtime/pkg/cli/builder" | ||
"github.com/spf13/cobra" | ||
) | ||
|
||
func NewKubectl(c CommandContext) *cobra.Command { | ||
cmd := cli.Command(&Kube{client: c.ClientFactory}, cobra.Command{ | ||
Use: "kube [flags]", | ||
Args: cobra.MinimumNArgs(1), | ||
Hidden: true, | ||
SilenceUsage: true, | ||
Short: "Run command with KUBECONFIG env set to a generated kubeconfig of the current project", | ||
Example: ` | ||
acorn -j acorn kube k9s | ||
`}) | ||
cmd.Flags().SetInterspersed(false) | ||
return cmd | ||
} | ||
|
||
type Kube struct { | ||
client ClientFactory | ||
} | ||
|
||
func (s *Kube) Run(cmd *cobra.Command, args []string) error { | ||
c, err := s.client.CreateDefault() | ||
if err != nil { | ||
return err | ||
} | ||
|
||
ctx, cancel := context.WithCancel(cmd.Context()) | ||
defer cancel() | ||
|
||
server, err := c.KubeProxyAddress(ctx) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
f, err := os.CreateTemp("", "acorn-kube") | ||
if err != nil { | ||
return err | ||
} | ||
defer func() { | ||
_ = os.Remove(f.Name()) | ||
}() | ||
|
||
_, err = f.Write([]byte(fmt.Sprintf(` | ||
apiVersion: v1 | ||
clusters: | ||
- cluster: | ||
server: "%s" | ||
name: default | ||
contexts: | ||
- context: | ||
cluster: default | ||
user: default | ||
name: default | ||
current-context: default | ||
kind: Config | ||
preferences: {} | ||
users: | ||
- name: default | ||
`, server))) | ||
if err != nil { | ||
return err | ||
} | ||
if err := f.Close(); err != nil { | ||
return err | ||
} | ||
|
||
k := exec.Command(args[0], args[1:]...) | ||
k.Env = append(os.Environ(), fmt.Sprintf("KUBECONFIG=%s", f.Name())) | ||
k.Stdin = os.Stdin | ||
k.Stdout = os.Stdout | ||
k.Stderr = os.Stderr | ||
return k.Run() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
package proxy | ||
|
||
import ( | ||
"net/http" | ||
"net/url" | ||
"strings" | ||
|
||
"k8s.io/apimachinery/pkg/util/proxy" | ||
"k8s.io/client-go/rest" | ||
"k8s.io/client-go/transport" | ||
) | ||
|
||
var ( | ||
er = &errorResponder{} | ||
) | ||
|
||
type errorResponder struct { | ||
} | ||
|
||
func (e *errorResponder) Error(w http.ResponseWriter, req *http.Request, err error) { | ||
w.WriteHeader(http.StatusInternalServerError) | ||
_, _ = w.Write([]byte(err.Error())) | ||
} | ||
|
||
// Mostly copied from "kubectl proxy" code | ||
func Handler(cfg *rest.Config) (http.Handler, error) { | ||
host := cfg.Host | ||
if !strings.HasSuffix(host, "/") { | ||
host = host + "/" | ||
} | ||
target, err := url.Parse(host) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
transport, err := rest.TransportFor(cfg) | ||
if err != nil { | ||
return nil, err | ||
} | ||
upgradeTransport, err := makeUpgradeTransport(cfg, transport) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
proxy := proxy.NewUpgradeAwareHandler(target, transport, false, false, er) | ||
proxy.UpgradeTransport = upgradeTransport | ||
proxy.UseRequestLocation = true | ||
proxy.UseLocationHost = true | ||
|
||
handler := http.Handler(proxy) | ||
|
||
if len(target.Path) > 1 { | ||
handler = prependPath(target.Path[:len(target.Path)-1], handler) | ||
} | ||
|
||
return proxyHeaders(handler), nil | ||
} | ||
|
||
func proxyHeaders(handler http.Handler) http.Handler { | ||
return http.HandlerFunc(func(rw http.ResponseWriter, req *http.Request) { | ||
req.Header.Del("Authorization") | ||
if req.Header.Get("X-Forwarded-Proto") == "" && req.TLS != nil { | ||
req.Header.Set("X-Forwarded-Proto", "https") | ||
} | ||
handler.ServeHTTP(rw, req) | ||
}) | ||
} | ||
|
||
func prependPath(prefix string, h http.Handler) http.Handler { | ||
return http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) { | ||
if len(req.URL.Path) > 1 { | ||
req.URL.Path = prefix + req.URL.Path | ||
} else { | ||
req.URL.Path = prefix | ||
} | ||
h.ServeHTTP(w, req) | ||
}) | ||
} | ||
|
||
func makeUpgradeTransport(config *rest.Config, rt http.RoundTripper) (proxy.UpgradeRequestRoundTripper, error) { | ||
transportConfig, err := config.TransportConfig() | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
upgrader, err := transport.HTTPWrappersForConfig(transportConfig, proxy.MirrorRequest) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
return proxy.NewUpgradeRequestRoundTripper(rt, upgrader), nil | ||
} |