This repository has been archived by the owner on Oct 17, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
support context and namespace kubectl flags (#5)
* support context and namespace kubectl flags Signed-off-by: VJ Patel <[email protected]>
- Loading branch information
Showing
7 changed files
with
87 additions
and
19 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
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 |
---|---|---|
@@ -1,35 +1,57 @@ | ||
package kubernetes | ||
|
||
import ( | ||
"fmt" | ||
"io" | ||
"log" | ||
"os/exec" | ||
|
||
"github.com/pkg/errors" | ||
) | ||
|
||
// KubectlOpts represents options/flags to pass to kubectl | ||
type KubectlOpts struct { | ||
Context string | ||
Namespace string | ||
} | ||
|
||
// Apply config using kubectl | ||
func Apply(config string) error { | ||
cmd := exec.Command("kubectl", "apply", "-f", "-") | ||
func Apply(resources string, opts *KubectlOpts) error { | ||
shCmd := GetCmd(opts) | ||
cmd := exec.Command(shCmd[0], shCmd[1:]...) | ||
stdin, err := cmd.StdinPipe() | ||
if err != nil { | ||
return errors.Wrap(err, "could not create stdin pipe") | ||
} | ||
go func() { | ||
defer stdin.Close() | ||
_, err := io.WriteString(stdin, config) | ||
_, err := io.WriteString(stdin, resources) | ||
if err != nil { | ||
log.Fatal(err) | ||
} | ||
}() | ||
|
||
output, err := cmd.CombinedOutput() | ||
if err != nil { | ||
return errors.Wrap(err, config) | ||
return errors.Wrap(err, resources) | ||
} | ||
if !cmd.ProcessState.Success() { | ||
return errors.Wrap(err, string(output)) | ||
} | ||
log.Println(string(output)) | ||
return nil | ||
} | ||
|
||
// GetCmd returns the kubectl command | ||
func GetCmd(opts *KubectlOpts) []string { | ||
cmd := []string{"kubectl", "apply", "-f", "-"} | ||
|
||
if opts.Context != "" { | ||
cmd = append(cmd, fmt.Sprintf(`--context=%s`, opts.Context)) | ||
} | ||
if opts.Namespace != "" { | ||
cmd = append(cmd, fmt.Sprintf(`--namespace=%s`, opts.Namespace)) | ||
} | ||
|
||
return cmd | ||
} |
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,27 @@ | ||
package kubernetes | ||
|
||
import ( | ||
"strings" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestGetCmd(t *testing.T) { | ||
var tests = []struct { | ||
desc string | ||
inOpts KubectlOpts | ||
outCmd string | ||
}{ | ||
{"none", KubectlOpts{}, "kubectl apply -f -"}, | ||
{"namespace", KubectlOpts{Namespace: "default"}, `kubectl apply -f - --namespace=default`}, | ||
{"context", KubectlOpts{Context: "minikube"}, `kubectl apply -f - --context=minikube`}, | ||
{"namespace&context", KubectlOpts{Context: "minikube", Namespace: "default"}, `kubectl apply -f - --context=minikube --namespace=default`}, | ||
} | ||
for _, tt := range tests { | ||
t.Run(tt.desc, func(t *testing.T) { | ||
outCmd := strings.Join(GetCmd(&tt.inOpts), " ") | ||
assert.Equal(t, tt.outCmd, outCmd) | ||
}) | ||
} | ||
} |