@@ -3,6 +3,10 @@ package kubectl
33import (
44 "context"
55 "fmt"
6+ "io/ioutil"
7+ "k8s.io/client-go/tools/clientcmd"
8+ clientcmdapi "k8s.io/client-go/tools/clientcmd/api"
9+ "os"
610 "os/exec"
711 "regexp"
812 "strings"
@@ -56,21 +60,17 @@ func (k *kustomizeBuilder) Build(ctx context.Context, dir, manifest string) ([]*
5660}
5761
5862type kubectlBuilder struct {
59- path string
60- config * latest.DeploymentConfig
61- context string
62- namespace string
63- isInCluster bool
63+ path string
64+ config * latest.DeploymentConfig
65+ kubeConfig clientcmdapi.Config
6466}
6567
6668// NewKubectlBuilder creates a new kubectl manifest builder
67- func NewKubectlBuilder (path string , config * latest.DeploymentConfig , context , namespace string , isInCluster bool ) Builder {
69+ func NewKubectlBuilder (path string , config * latest.DeploymentConfig , kubeConfig clientcmdapi. Config ) Builder {
6870 return & kubectlBuilder {
69- path : path ,
70- config : config ,
71- context : context ,
72- namespace : namespace ,
73- isInCluster : isInCluster ,
71+ path : path ,
72+ config : config ,
73+ kubeConfig : kubeConfig ,
7474 }
7575}
7676
@@ -102,14 +102,25 @@ var useOldDryRun = func(ctx context.Context, dir, path string) (bool, error) {
102102}
103103
104104func (k * kubectlBuilder ) Build (ctx context.Context , dir , manifest string ) ([]* unstructured.Unstructured , error ) {
105- args := [] string { "create" }
106- if k . context != "" && ! k . isInCluster {
107- args = append ( args , "--context" , k . context )
105+ tempFile , err := ioutil . TempFile ( "" , "" )
106+ if err != nil {
107+ return nil , err
108108 }
109- if k .namespace != "" {
110- args = append (args , "--namespace" , k .namespace )
109+ defer os .Remove (tempFile .Name ())
110+
111+ data , err := clientcmd .Write (k .kubeConfig )
112+ if err != nil {
113+ return nil , err
111114 }
112115
116+ _ , err = tempFile .Write (data )
117+ if err != nil {
118+ return nil , err
119+ }
120+ _ = tempFile .Close ()
121+
122+ args := []string {"create" }
123+
113124 // decides which --dry-run value is to be used
114125 uodr , err := useOldDryRun (ctx , dir , k .path )
115126 if err != nil {
@@ -132,7 +143,9 @@ func (k *kubectlBuilder) Build(ctx context.Context, dir, manifest string) ([]*un
132143 args = append (args , k .config .Kubectl .CreateArgs ... )
133144
134145 // Execute command
135- output , err := command .Output (ctx , dir , k .path , args ... )
146+ output , err := command .OutputWithEnv (ctx , dir , map [string ]string {
147+ "KUBECONFIG" : tempFile .Name (),
148+ }, k .path , args ... )
136149 if err != nil {
137150 exitError , ok := err .(* exec.ExitError )
138151 if ok {
0 commit comments