-
Notifications
You must be signed in to change notification settings - Fork 226
/
sync.go
203 lines (181 loc) · 5.63 KB
/
sync.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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
// Copyright 2019 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// Package sync syncs dependencies specified in the Kptfile to local directories.
package sync
import (
"fmt"
"io"
"io/ioutil"
"os"
"path/filepath"
"github.com/GoogleContainerTools/kpt/internal/util/functions"
"github.com/GoogleContainerTools/kpt/internal/util/get"
"github.com/GoogleContainerTools/kpt/internal/util/setters"
"github.com/GoogleContainerTools/kpt/internal/util/update"
"github.com/GoogleContainerTools/kpt/pkg/kptfile"
"sigs.k8s.io/kustomize/kyaml/errors"
"sigs.k8s.io/kustomize/kyaml/yaml"
)
// Command syncs the dependencies delared in the Kptfile -- getting, updating, or
// deleting the dependencies as needed.
type Command struct {
// Dir is the path to a directory containing the Kptfile to sync
Dir string
Verbose bool
DryRun bool
StdOut io.Writer
StdErr io.Writer
}
// Run syncs all dependencies declared in the Kptfile, fetching them
// if they are missing, updating them if their versions have changed,
// and deleting them if they should not exist.
func (c Command) Run() error {
b, err := ioutil.ReadFile(filepath.Join(c.Dir, kptfile.KptFileName))
if err != nil {
return errors.WrapPrefixf(err, "failed to read Kptfile under %q", c.Dir)
}
k := &kptfile.KptFile{}
if err := yaml.Unmarshal(b, k); err != nil {
return errors.WrapPrefixf(err, "failed to unmarshal Kptfile under %q", c.Dir)
}
// validate dependencies are well formed
for i := range k.Dependencies {
if k.Dependencies[i].Name == "" {
return errors.Errorf("One or more dependencies missing 'name'")
}
if !k.Dependencies[i].EnsureNotExists {
if k.Dependencies[i].Git.Directory == "" {
return errors.Errorf("One or more dependencies missing 'git.directory'")
}
if k.Dependencies[i].Git.Ref == "" {
return errors.Errorf("One or more dependencies missing 'git.ref'")
}
if k.Dependencies[i].Git.Repo == "" {
return errors.Errorf("One or more dependencies missing 'git.repo'")
}
} else if k.Dependencies[i].Git.Directory != "" ||
k.Dependencies[i].Git.Ref != "" ||
k.Dependencies[i].Git.Repo == "" {
return errors.Errorf(
"One or more dependencies specify mutually exclusive fields " +
"'ensureNotExists' and 'git'")
}
}
for i := range k.Dependencies {
dep := k.Dependencies[i]
if err := c.sync(dep); err != nil {
return err
}
path := filepath.Join(c.Dir, dep.Name)
if dep.AutoSet {
a := setters.AutoSet{
Writer: c.StdOut,
PackagePath: path,
}
if err := a.PerformAutoSetters(); err != nil {
return err
}
}
if err := functions.RunFunctions(path, dep.Functions); err != nil {
return err
}
}
return nil
}
func (c Command) sync(dependency kptfile.Dependency) error {
path := filepath.Join(c.Dir, dependency.Name)
f, err := os.Stat(path)
// dep missing
if os.IsNotExist(err) {
if dependency.EnsureNotExists {
// dep already deleted -- no action required
return nil
}
// fetch the dep
return c.get(dependency)
}
if err != nil {
return errors.Wrap(err)
}
// verify the dep is well formed
if !f.IsDir() {
// place where dep should be fetched exists and is not a directory
return errors.Errorf("cannot sync to %q, non-directory file exists", path)
}
_, err = os.Stat(filepath.Join(path, kptfile.KptFileName))
if os.IsNotExist(err) {
// dep doesn't have a Kptfile -- something is wrong
return errors.Errorf("expected Kptfile under dependency %q", path)
}
if err != nil {
return errors.Wrap(err)
}
// delete the dep -- it exists
if dependency.EnsureNotExists {
return c.delete(dependency)
}
// read the Kptfile
b, err := ioutil.ReadFile(filepath.Join(path, kptfile.KptFileName))
if err != nil {
return errors.Wrap(err)
}
k := &kptfile.KptFile{}
if err = yaml.Unmarshal(b, k); err != nil {
return errors.Wrap(err)
}
return c.update(dependency, k)
}
// get fetches the dependency
func (c Command) get(dependency kptfile.Dependency) error {
path := filepath.Join(c.Dir, dependency.Name)
fmt.Fprintf(c.StdOut, "fetching %q from %q\n", dependency.Name, path)
if c.DryRun {
return nil
}
return get.Command{
Git: dependency.Git,
Destination: path,
Name: dependency.Name,
}.Run()
}
// update updates the version of the fetched dependency to match
func (c Command) update(dependency kptfile.Dependency, k *kptfile.KptFile) error {
path := filepath.Join(c.Dir, dependency.Name)
fmt.Fprintf(c.StdOut, "updating %q (%s) from %q to %q\n",
dependency.Name, path, k.Upstream.Git.Ref, dependency.Git.Ref)
if c.DryRun {
return nil
}
if dependency.Git.Ref == k.Upstream.Git.Ref &&
dependency.Git.Repo == k.Upstream.Git.Repo {
return nil
}
return update.Command{
Path: path,
Ref: dependency.Git.Ref,
Repo: dependency.Git.Repo,
Strategy: update.StrategyType(dependency.Strategy),
Verbose: c.Verbose,
AutoSet: dependency.AutoSet,
}.Run()
}
// delete removes the dependency if it exists
func (c Command) delete(dependency kptfile.Dependency) error {
path := filepath.Join(c.Dir, dependency.Name)
fmt.Fprintf(c.StdOut, "deleting %q from %q\n", dependency.Name, path)
if c.DryRun {
return nil
}
return os.RemoveAll(path)
}