Skip to content

Commit

Permalink
Merge pull request #3507 from telepresenceio/thallgren/kubeconfig-stdin
Browse files Browse the repository at this point in the history
Add ability to read kubeconfig from stdin.
  • Loading branch information
thallgren authored Feb 13, 2024
2 parents 09001ee + 047d3c7 commit d7ed014
Show file tree
Hide file tree
Showing 17 changed files with 804 additions and 567 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.yml
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,12 @@ items:
- version: 2.18.0
date: "2024-2-9"
notes:
- type: feature
title: Add option to load the kubeconfig yaml from stdin during connect.
body: >-
This allows another process with a kubeconfig already loaded in memory
to directly pass it to <code>telepresence connect</code> without needing a separate
file. Simply use a dash "-" as the filename for the <code>--kubeconfig</code> flag.
- type: feature
title: Include the image for the traffic-agent in the output of the version and status commands.
body: >-
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ require (
github.com/spf13/cobra v1.8.0
github.com/spf13/pflag v1.0.5
github.com/stretchr/testify v1.8.4
github.com/telepresenceio/telepresence/rpc/v2 v2.18.0
github.com/telepresenceio/telepresence/rpc/v2 v2.18.1-test.0
github.com/vishvananda/netlink v1.2.1-beta.2
go.opentelemetry.io/contrib/instrumentation/google.golang.org/grpc/otelgrpc v0.46.1
go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp v0.46.1
Expand Down
14 changes: 7 additions & 7 deletions integration_test/install_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,9 +49,9 @@ func init() {
})
}

func getHelmConfig(ctx context.Context, configFlags *genericclioptions.ConfigFlags, namespace string) (*action.Configuration, error) {
func getHelmConfig(ctx context.Context, clientGetter genericclioptions.RESTClientGetter, namespace string) (*action.Configuration, error) {
helmConfig := &action.Configuration{}
err := helmConfig.Init(configFlags, namespace, "secrets", func(format string, args ...any) {
err := helmConfig.Init(clientGetter, namespace, "secrets", func(format string, args ...any) {
ctx := dlog.WithField(ctx, "source", "helm")
dlog.Infof(ctx, format, args...)
})
Expand All @@ -68,7 +68,7 @@ func (is *installSuite) Test_UpgradeRetainsValues() {
defer is.UninstallTrafficManager(ctx, is.ManagerNamespace())

ctx, kc := is.cluster(ctx, "", is.ManagerNamespace())
helmConfig, err := getHelmConfig(ctx, kc.ConfigFlags, is.ManagerNamespace())
helmConfig, err := getHelmConfig(ctx, kc.Kubeconfig, is.ManagerNamespace())
rq.NoError(err)

getValues := func() (map[string]any, error) {
Expand Down Expand Up @@ -334,12 +334,12 @@ func (is *installSuite) Test_RemoveManager_canUninstall() {
ctx, kc := is.cluster(ctx, "", is.ManagerNamespace())

require.NoError(ensureTrafficManager(ctx, kc))
require.NoError(helm.DeleteTrafficManager(ctx, kc.ConfigFlags, kc.GetManagerNamespace(), true, &helm.Request{}))
require.NoError(helm.DeleteTrafficManager(ctx, kc.Kubeconfig, kc.GetManagerNamespace(), true, &helm.Request{}))
// We want to make sure that we can re-install the manager after it's been uninstalled,
// so try to ensureManager again.
require.NoError(ensureTrafficManager(ctx, kc))
// Uninstall the manager one last time -- this should behave the same way as the previous uninstall
require.NoError(helm.DeleteTrafficManager(ctx, kc.ConfigFlags, kc.GetManagerNamespace(), true, &helm.Request{}))
require.NoError(helm.DeleteTrafficManager(ctx, kc.Kubeconfig, kc.GetManagerNamespace(), true, &helm.Request{}))
}

func (is *installSuite) Test_EnsureManager_upgrades_and_values() {
Expand Down Expand Up @@ -393,7 +393,7 @@ func (is *installSuite) Test_No_Upgrade() {
jvp, err := json.Marshal(vp)
require.NoError(err)

require.NoError(helm.EnsureTrafficManager(ctx, kc.ConfigFlags, kc.GetManagerNamespace(), &helm.Request{
require.NoError(helm.EnsureTrafficManager(ctx, kc.Kubeconfig, kc.GetManagerNamespace(), &helm.Request{
Type: helm.Upgrade,
ValuesJson: jvp,
}))
Expand Down Expand Up @@ -437,7 +437,7 @@ func (is *installSuite) cluster(ctx context.Context, context, managerNamespace s
func ensureTrafficManager(ctx context.Context, kc *k8s.Cluster) error {
return helm.EnsureTrafficManager(
ctx,
kc.ConfigFlags,
kc.Kubeconfig,
kc.GetManagerNamespace(),
&helm.Request{Type: helm.Install})
}
2 changes: 1 addition & 1 deletion packaging/bundle.wxs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<Wix xmlns="http://wixtoolset.org/schemas/v4/wxs">
<Bundle Name="Telepresence" Manufacturer="Ambassador Labs" Version="2.18.0"
<Bundle Name="Telepresence" Manufacturer="Ambassador Labs" Version="2.18.1"
UpgradeCode="82fd5464-dd21-44bd-a44d-053cea4da740">

<BootstrapperApplication>
Expand Down
2 changes: 1 addition & 1 deletion packaging/telepresence.wxs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<Wix xmlns="http://wixtoolset.org/schemas/v4/wxs">
<Package Name="Telepresence" Language="1033" Codepage="1252" Version="2.18.0"
<Package Name="Telepresence" Language="1033" Codepage="1252" Version="2.18.1"
Manufacturer="Ambassador Labs" InstallerVersion="100" ProductCode="*"
UpgradeCode="fdac1021-3405-4097-84e3-1e683ba9eee5">
<SummaryInformation Keywords="Installer" Description="Ambassador's Telepresence Installer"
Expand Down
4 changes: 2 additions & 2 deletions pkg/client/cli/cmd/helm.go
Original file line number Diff line number Diff line change
Expand Up @@ -201,10 +201,10 @@ func (ha *HelmCommand) run(cmd *cobra.Command, _ []string) error {
}

if ha.Type() == helm.Uninstall {
err = helm.DeleteTrafficManager(ctx, cluster.ConfigFlags, cluster.GetManagerNamespace(), false, &ha.Request)
err = helm.DeleteTrafficManager(ctx, cluster.Kubeconfig, cluster.GetManagerNamespace(), false, &ha.Request)
} else {
dlog.Debug(ctx, "ensuring that traffic-manager exists")
err = helm.EnsureTrafficManager(cluster.WithK8sInterface(ctx), cluster.ConfigFlags, cluster.GetManagerNamespace(), &ha.Request)
err = helm.EnsureTrafficManager(cluster.WithK8sInterface(ctx), cluster.Kubeconfig, cluster.GetManagerNamespace(), &ha.Request)
}
if err != nil {
return err
Expand Down
12 changes: 11 additions & 1 deletion pkg/client/cli/daemon/request.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"errors"
"fmt"
"io"
"net/netip"
"os"
"regexp"
Expand Down Expand Up @@ -117,19 +118,28 @@ func InitRequest(cmd *cobra.Command) *Request {
type requestKey struct{}

func (cr *Request) CommitFlags(cmd *cobra.Command) error {
var err error
cr.kubeFlagSet.VisitAll(func(flag *pflag.Flag) {
if flag.Changed {
var v string
if sv, ok := flag.Value.(pflag.SliceValue); ok {
v = slice.AsCSV(sv.GetSlice())
} else {
v = flag.Value.String()
if flag.Name == "kubeconfig" && v == "-" {
// Read kubeconfig from stdin
cr.KubeconfigData, err = io.ReadAll(cmd.InOrStdin())
return // kubernetes will not understand "-"
}
}
cr.KubeFlags[flag.Name] = v
}
})
if err != nil {
return err
}
cr.addKubeconfigEnv()
err := cr.setGlobalConnectFlags(cmd)
err = cr.setGlobalConnectFlags(cmd)
if err != nil {
return errcat.User.New(err)
}
Expand Down
28 changes: 14 additions & 14 deletions pkg/client/cli/helm/install.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,9 @@ type Request struct {
NoHooks bool
}

func getHelmConfig(ctx context.Context, configFlags *genericclioptions.ConfigFlags, namespace string) (*action.Configuration, error) {
func getHelmConfig(ctx context.Context, clientGetter genericclioptions.RESTClientGetter, namespace string) (*action.Configuration, error) {
helmConfig := &action.Configuration{}
err := helmConfig.Init(configFlags, namespace, helmDriver, func(format string, args ...any) {
err := helmConfig.Init(clientGetter, namespace, helmDriver, func(format string, args ...any) {
ctx := dlog.WithField(ctx, "source", "helm")
dlog.Infof(ctx, format, args...)
})
Expand Down Expand Up @@ -181,9 +181,9 @@ func uninstallExisting(ctx context.Context, helmConfig *action.Configuration, re
})
}

func isInstalled(ctx context.Context, configFlags *genericclioptions.ConfigFlags, releaseName, namespace string) (*release.Release, *action.Configuration, error) {
func isInstalled(ctx context.Context, clientGetter genericclioptions.RESTClientGetter, releaseName, namespace string) (*release.Release, *action.Configuration, error) {
dlog.Debug(ctx, "getHelmConfig")
helmConfig, err := getHelmConfig(ctx, configFlags, namespace)
helmConfig, err := getHelmConfig(ctx, clientGetter, namespace)
if err != nil {
err = fmt.Errorf("failed to initialize helm config: %w", err)
return nil, nil, err
Expand Down Expand Up @@ -225,15 +225,15 @@ func isInstalled(ctx context.Context, configFlags *genericclioptions.ConfigFlags
return existing, helmConfig, nil
}

func EnsureTrafficManager(ctx context.Context, configFlags *genericclioptions.ConfigFlags, namespace string, req *Request) error {
func EnsureTrafficManager(ctx context.Context, clientGetter genericclioptions.RESTClientGetter, namespace string, req *Request) error {
if req.Crds {
dlog.Debug(ctx, "loading build-in helm chart")
crdChart, err := loadCRDChart()
if err != nil {
return fmt.Errorf("unable to load built-in helm chart: %w", err)
}

err = ensureIsInstalled(ctx, configFlags, crdChart, crdReleaseName, namespace, req)
err = ensureIsInstalled(ctx, clientGetter, crdChart, crdReleaseName, namespace, req)
if err != nil {
return fmt.Errorf("failed to install traffic manager CRDs: %w", err)
}
Expand All @@ -245,7 +245,7 @@ func EnsureTrafficManager(ctx context.Context, configFlags *genericclioptions.Co
return fmt.Errorf("unable to load built-in helm chart: %w", err)
}

err = ensureIsInstalled(ctx, configFlags, coreChart, trafficManagerReleaseName, namespace, req)
err = ensureIsInstalled(ctx, clientGetter, coreChart, trafficManagerReleaseName, namespace, req)
if err != nil {
return fmt.Errorf("failed to install traffic manager: %w", err)
}
Expand All @@ -255,10 +255,10 @@ func EnsureTrafficManager(ctx context.Context, configFlags *genericclioptions.Co

// EnsureTrafficManager ensures the traffic manager is installed.
func ensureIsInstalled(
ctx context.Context, configFlags *genericclioptions.ConfigFlags, chrt *chart.Chart,
ctx context.Context, clientGetter genericclioptions.RESTClientGetter, chrt *chart.Chart,
releaseName, namespace string, req *Request,
) error {
existing, helmConfig, err := isInstalled(ctx, configFlags, releaseName, namespace)
existing, helmConfig, err := isInstalled(ctx, clientGetter, releaseName, namespace)
if err != nil {
return fmt.Errorf("err detecting %s: %w", releaseName, err)
}
Expand Down Expand Up @@ -329,17 +329,17 @@ func ensureIsInstalled(

// DeleteTrafficManager deletes the traffic manager.
func DeleteTrafficManager(
ctx context.Context, configFlags *genericclioptions.ConfigFlags, namespace string, errOnFail bool, req *Request,
ctx context.Context, clientGetter genericclioptions.RESTClientGetter, namespace string, errOnFail bool, req *Request,
) error {
if !req.Crds {
err := ensureIsDeleted(ctx, configFlags, trafficManagerReleaseName, namespace, errOnFail, req)
err := ensureIsDeleted(ctx, clientGetter, trafficManagerReleaseName, namespace, errOnFail, req)
if err != nil {
return err
}
return nil
}

err := ensureIsDeleted(ctx, configFlags, crdReleaseName, namespace, errOnFail, req)
err := ensureIsDeleted(ctx, clientGetter, crdReleaseName, namespace, errOnFail, req)
if err != nil {
return err
}
Expand All @@ -349,12 +349,12 @@ func DeleteTrafficManager(

func ensureIsDeleted(
ctx context.Context,
configFlags *genericclioptions.ConfigFlags,
clientGetter genericclioptions.RESTClientGetter,
releaseName, namespace string,
errOnFail bool,
req *Request,
) error {
helmConfig, err := getHelmConfig(ctx, configFlags, namespace)
helmConfig, err := getHelmConfig(ctx, clientGetter, namespace)
if err != nil {
return fmt.Errorf("failed to initialize helm config: %w", err)
}
Expand Down
Loading

0 comments on commit d7ed014

Please sign in to comment.