1
0
mirror of synced 2026-02-06 19:05:55 +00:00

Use kustomize API instead of binary

This commit is contained in:
stefanprodan
2020-05-12 08:18:16 +03:00
parent 186945532f
commit 1e7d2e7dce
5 changed files with 82 additions and 59 deletions

View File

@@ -53,10 +53,6 @@ func runCheckCmd(cmd *cobra.Command, args []string) error {
checkFailed = true
}
if !kustomizeCheck(ctx, ">=3.5.0") {
checkFailed = true
}
if !kubernetesCheck(">=1.14.0") {
checkFailed = true
}
@@ -125,45 +121,6 @@ func kubectlCheck(ctx context.Context, version string) bool {
return true
}
func kustomizeCheck(ctx context.Context, version string) bool {
_, err := exec.LookPath("kustomize")
if err != nil {
logFailure("kustomize not found")
return false
}
command := "kustomize version --short | awk '{ print $1 }' | cut -c2-"
output, err := utils.execCommand(ctx, ModeCapture, command)
if err != nil {
logFailure("kustomize version can't be determined")
return false
}
if strings.Contains(output, "kustomize/") {
command = "kustomize version --short | awk '{ print $1 }' | cut -c12-"
output, err = utils.execCommand(ctx, ModeCapture, command)
if err != nil {
logFailure("kustomize version can't be determined")
return false
}
}
v, err := semver.ParseTolerant(output)
if err != nil {
logFailure("kustomize version can't be parsed")
return false
}
rng, _ := semver.ParseRange(version)
if !rng(v) {
logFailure("kustomize version must be %s", version)
return false
}
logSuccess("kustomize %s %s", v.String(), version)
return true
}
func kubernetesCheck(version string) bool {
cfg, err := clientcmd.BuildConfigFromFlags("", kubeconfig)
if err != nil {

View File

@@ -6,9 +6,12 @@ import (
"io/ioutil"
"os"
"path"
"path/filepath"
"strings"
"github.com/spf13/cobra"
"sigs.k8s.io/kustomize/api/filesys"
"sigs.k8s.io/kustomize/api/krusty"
)
var installCmd = &cobra.Command{
@@ -73,12 +76,11 @@ func installCmdRun(cmd *cobra.Command, args []string) error {
}
manifest := path.Join(tmpDir, fmt.Sprintf("%s.yaml", namespace))
command := fmt.Sprintf("kustomize build %s > %s", kustomizePath, manifest)
if _, err := utils.execCommand(ctx, ModeStderrOS, command); err != nil {
return fmt.Errorf("install failed")
if err := buildKustomization(kustomizePath, manifest); err != nil {
return fmt.Errorf("install failed: %w", err)
}
command = fmt.Sprintf("cat %s", manifest)
command := fmt.Sprintf("cat %s", manifest)
if yaml, err := utils.execCommand(ctx, ModeCapture, command); err != nil {
return fmt.Errorf("install failed: %w", err)
} else {
@@ -202,3 +204,30 @@ func genInstallManifests(version string, namespace string, components []string,
return nil
}
func buildKustomization(base, manifests string) error {
kfile := filepath.Join(base, "kustomization.yaml")
fs := filesys.MakeFsOnDisk()
if !fs.Exists(kfile) {
return fmt.Errorf("%s not found", kfile)
}
opt := krusty.MakeDefaultOptions()
k := krusty.MakeKustomizer(fs, opt)
m, err := k.Run(base)
if err != nil {
return err
}
resources, err := m.AsYaml()
if err != nil {
return err
}
if err := fs.WriteFile(manifests, resources); err != nil {
return err
}
return nil
}