-
Notifications
You must be signed in to change notification settings - Fork 13
/
apply.go
140 lines (114 loc) · 2.92 KB
/
apply.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
package main
import (
"fmt"
"io/ioutil"
"os"
"os/exec"
"path/filepath"
"strings"
"time"
"github.com/spf13/cobra"
"sigs.k8s.io/kustomize/api/filesys"
"github.com/stefanprodan/kustomizer/pkg/engine"
)
var applyCmd = &cobra.Command{
Use: "apply [path]",
Short: "Apply kustomization and prune previous applied Kubernetes objects",
RunE: applyCmdRun,
}
var (
group string
name string
revision string
timeout time.Duration
cfgNamespace string
buildWithKustomize bool
dryRun bool
)
func init() {
applyCmd.Flags().StringVar(&group, "group", "kustomizer", "group")
applyCmd.Flags().StringVarP(&name, "name", "", "", "name of this kustomization")
applyCmd.Flags().StringVarP(&revision, "revision", "r", "", "revision of this kustomization")
applyCmd.Flags().StringVarP(&cfgNamespace, "gc-namespace", "", "default", "namespace to store the GC snapshot ConfigMap")
applyCmd.Flags().DurationVar(&timeout, "timeout", 5*time.Minute, "timeout for this operation")
applyCmd.Flags().BoolVar(&buildWithKustomize, "use-kustomize", false, "use Kustomize binary for build operations")
applyCmd.Flags().BoolVar(&dryRun, "dry-run", false, "dry-run apply")
rootCmd.AddCommand(applyCmd)
}
func applyCmdRun(cmd *cobra.Command, args []string) error {
if len(args) < 1 {
return fmt.Errorf("path is required")
}
base := args[0]
fs := filesys.MakeFsOnDisk()
tmpDir, err := ioutil.TempDir("", name)
if err != nil {
return fmt.Errorf("tmp dir error: %w", err)
}
defer os.RemoveAll(tmpDir)
if !strings.HasSuffix(base, "/") {
base += "/"
}
c := fmt.Sprintf("cp -r %s* %s", base, tmpDir)
command := exec.Command("/bin/sh", "-c", c)
if err := command.Run(); err != nil {
return fmt.Errorf("%s command failed", c)
}
base = tmpDir
revisor, err := engine.NewRevisior(group, name, revision)
if err != nil {
return err
}
transformer, err := engine.NewTransformer(fs, revisor)
if err != nil {
return err
}
err = transformer.Generate(base)
if err != nil {
return err
}
generator, err := engine.NewGenerator(fs, revisor)
if err != nil {
return err
}
err = generator.Generate(base)
if err != nil {
return err
}
builder, err := engine.NewBuilder(fs)
if err != nil {
return err
}
manifest := filepath.Join(base, revisor.ManifestFile())
if buildWithKustomize {
if err = builder.Build(base, manifest); err != nil {
return err
}
} else {
if err = builder.Generate(base, manifest); err != nil {
return err
}
}
applier, err := engine.NewApplier(fs, timeout)
if err != nil {
return err
}
err = applier.Run(manifest, dryRun)
if err != nil {
return err
}
gc, err := engine.NewGarbageCollector(revisor, timeout)
if err != nil {
return err
}
write := func(obj string) {
if !strings.Contains(obj, "No resources found") {
fmt.Println(obj)
}
}
err = gc.Run(manifest, cfgNamespace, write)
if err != nil {
return err
}
return nil
}