mirror of
https://github.com/kubernetes-sigs/kustomize.git
synced 2026-06-29 17:41:13 +00:00
Compare commits
80 Commits
cmd/config
...
api/v0.3.2
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
0c9a3756b4 | ||
|
|
3f417c7b5b | ||
|
|
4526cb14e8 | ||
|
|
595e41a3ec | ||
|
|
118ba7eefe | ||
|
|
488bc5aceb | ||
|
|
2786287444 | ||
|
|
4e7446540c | ||
|
|
90ecc5d30a | ||
|
|
8840085a32 | ||
|
|
1bd62ffce9 | ||
|
|
344e6f18dd | ||
|
|
8b9d374170 | ||
|
|
982ad409bd | ||
|
|
9555095de9 | ||
|
|
c6cc457f45 | ||
|
|
6d58848970 | ||
|
|
8a2c886ab2 | ||
|
|
2f5be62387 | ||
|
|
a46046dac5 | ||
|
|
1404d2749d | ||
|
|
9fe9a2500a | ||
|
|
6186e4edb7 | ||
|
|
54e92f1ab0 | ||
|
|
04f5e6c953 | ||
|
|
9000eb7f81 | ||
|
|
abeab51cae | ||
|
|
b154af8be4 | ||
|
|
ccd129f7a5 | ||
|
|
e2b56910f9 | ||
|
|
ed83b2d8fa | ||
|
|
bda865e9e4 | ||
|
|
77b59760c1 | ||
|
|
4628705494 | ||
|
|
e619cec090 | ||
|
|
0cca76fbb8 | ||
|
|
e473433cba | ||
|
|
0cae0feb9b | ||
|
|
2437e1ffe7 | ||
|
|
00f7656f1b | ||
|
|
32c280664d | ||
|
|
abc57e481b | ||
|
|
594a3bf0d2 | ||
|
|
2094f23414 | ||
|
|
7b1a5f85ed | ||
|
|
7190ea2688 | ||
|
|
6bdb4fe2a6 | ||
|
|
62964bfcb4 | ||
|
|
e13c26b2f8 | ||
|
|
647731a6ad | ||
|
|
426407a1b2 | ||
|
|
3276e74d2d | ||
|
|
bbceb49fc4 | ||
|
|
950660ff63 | ||
|
|
f749a4a194 | ||
|
|
79cfdb0976 | ||
|
|
9ec4100ee1 | ||
|
|
6e7f7ce194 | ||
|
|
b1f514632a | ||
|
|
745b58b3d0 | ||
|
|
142c105500 | ||
|
|
5f8a8b545b | ||
|
|
ee659a70e4 | ||
|
|
837df94d67 | ||
|
|
6b90f13281 | ||
|
|
db5e2c42b0 | ||
|
|
d489bdedd7 | ||
|
|
8b10aea859 | ||
|
|
a7a28a85a4 | ||
|
|
2c8736ccb2 | ||
|
|
9062a83276 | ||
|
|
5ee6380b1c | ||
|
|
2ab884c879 | ||
|
|
5e1ddf38db | ||
|
|
011804e14d | ||
|
|
7753a04fdc | ||
|
|
f0d81c4fac | ||
|
|
fa8f504ff4 | ||
|
|
a47eff804b | ||
|
|
0988f74d39 |
@@ -36,6 +36,7 @@ install: true
|
|||||||
script:
|
script:
|
||||||
- make verify-kustomize
|
- make verify-kustomize
|
||||||
- ./travis/kyaml-pre-commit.sh
|
- ./travis/kyaml-pre-commit.sh
|
||||||
|
- ./travis/check-go-mod.sh
|
||||||
|
|
||||||
# TBD. Suppressing for now.
|
# TBD. Suppressing for now.
|
||||||
notifications:
|
notifications:
|
||||||
|
|||||||
@@ -52,7 +52,7 @@ func (p *NamespaceTransformerPlugin) Transform(m resmap.ResMap) error {
|
|||||||
|
|
||||||
matches := m.GetMatchingResourcesByCurrentId(r.CurId().Equals)
|
matches := m.GetMatchingResourcesByCurrentId(r.CurId().Equals)
|
||||||
if len(matches) != 1 {
|
if len(matches) != 1 {
|
||||||
return fmt.Errorf("namespace tranformation produces ID conflict: %#v", matches)
|
return fmt.Errorf("namespace tranformation produces ID conflict: %+v", matches)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
|
|||||||
@@ -136,6 +136,7 @@ func main() {
|
|||||||
github.QueryWith(
|
github.QueryWith(
|
||||||
github.Filename("kustomization.yaml"),
|
github.Filename("kustomization.yaml"),
|
||||||
github.Filename("kustomization.yml"),
|
github.Filename("kustomization.yml"),
|
||||||
|
github.Filename("kustomization"),
|
||||||
github.User(user)),
|
github.User(user)),
|
||||||
)
|
)
|
||||||
} else if repo != "" {
|
} else if repo != "" {
|
||||||
@@ -143,13 +144,15 @@ func main() {
|
|||||||
github.QueryWith(
|
github.QueryWith(
|
||||||
github.Filename("kustomization.yaml"),
|
github.Filename("kustomization.yaml"),
|
||||||
github.Filename("kustomization.yml"),
|
github.Filename("kustomization.yml"),
|
||||||
|
github.Filename("kustomization"),
|
||||||
github.Repo(repo)),
|
github.Repo(repo)),
|
||||||
)
|
)
|
||||||
} else {
|
} else {
|
||||||
return github.NewCrawler(githubToken, retryCount, clientCache,
|
return github.NewCrawler(githubToken, retryCount, clientCache,
|
||||||
github.QueryWith(
|
github.QueryWith(
|
||||||
github.Filename("kustomization.yaml"),
|
github.Filename("kustomization.yaml"),
|
||||||
github.Filename("kustomization.yml")),
|
github.Filename("kustomization.yml"),
|
||||||
|
github.Filename("kustomization")),
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
14
api/internal/crawl/cmd/kustomize_stats/Dockerfile
Normal file
14
api/internal/crawl/cmd/kustomize_stats/Dockerfile
Normal file
@@ -0,0 +1,14 @@
|
|||||||
|
FROM golang:1.11 AS build
|
||||||
|
|
||||||
|
ARG GO111MODULE=on
|
||||||
|
|
||||||
|
WORKDIR /go/src/sigs.k8s.io/kustomize/api/internal/crawl
|
||||||
|
COPY . /go/src/sigs.k8s.io/kustomize/api/internal/crawl
|
||||||
|
|
||||||
|
RUN go mod download
|
||||||
|
RUN CGO_ENABLED=0 go install ./cmd/kustomize_stats
|
||||||
|
|
||||||
|
FROM scratch
|
||||||
|
COPY --from=build /etc/ssl/certs/ca-certificates.crt /etc/ssl/certs/
|
||||||
|
COPY --from=build /go/bin/kustomize_stats /
|
||||||
|
ENTRYPOINT ["/kustomize_stats"]
|
||||||
150
api/internal/crawl/cmd/kustomize_stats/main.go
Normal file
150
api/internal/crawl/cmd/kustomize_stats/main.go
Normal file
@@ -0,0 +1,150 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"flag"
|
||||||
|
"fmt"
|
||||||
|
"log"
|
||||||
|
"path/filepath"
|
||||||
|
"sort"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"sigs.k8s.io/kustomize/api/internal/crawl/index"
|
||||||
|
"sigs.k8s.io/kustomize/api/konfig"
|
||||||
|
)
|
||||||
|
|
||||||
|
// iterateArr adds each item in arr into countMap.
|
||||||
|
func iterateArr(arr []string, countMap map[string]int) {
|
||||||
|
for _, item := range arr {
|
||||||
|
if _, ok := countMap[item]; !ok {
|
||||||
|
countMap[item] = 1
|
||||||
|
} else {
|
||||||
|
countMap[item]++
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
// isKustomizationFile determines whether a file path is a kustomization file
|
||||||
|
func isKustomizationFile(path string) bool {
|
||||||
|
basename := filepath.Base(path)
|
||||||
|
for _, name := range konfig.RecognizedKustomizationFileNames() {
|
||||||
|
if basename == name {
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
|
// SortMapKeyByValue takes a map as its input, sorts its keys according to their values
|
||||||
|
// in the map, and outputs the sorted keys as a slice.
|
||||||
|
func SortMapKeyByValue(m map[string]int) []string {
|
||||||
|
keys := make([]string, 0, len(m))
|
||||||
|
for key := range m {
|
||||||
|
keys = append(keys, key)
|
||||||
|
}
|
||||||
|
// sort keys according to their values in the map m
|
||||||
|
sort.Slice(keys, func(i, j int) bool {return m[keys[i]] > m[keys[j]]})
|
||||||
|
return keys
|
||||||
|
}
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
topKindsPtr := flag.Int(
|
||||||
|
"kinds", -1,
|
||||||
|
`the number of kubernetes object kinds to be listed according to their popularities.
|
||||||
|
By default, all the kinds will be listed.
|
||||||
|
If you only want to list the 10 most popular kinds, set the flag to 10.`)
|
||||||
|
topIdentifiersPtr := flag.Int(
|
||||||
|
"identifiers", -1,
|
||||||
|
`the number of identifiers to be listed according to their popularities.
|
||||||
|
By default, all the identifiers will be listed.
|
||||||
|
If you only want to list the 10 most popular identifiers, set the flag to 10.`)
|
||||||
|
topKustomizeFeaturesPtr := flag.Int(
|
||||||
|
"kustomize-features", -1,
|
||||||
|
`the number of kustomize features to be listed according to their popularities.
|
||||||
|
By default, all the features will be listed.
|
||||||
|
If you only want to list the 10 most popular features, set the flag to 10.`)
|
||||||
|
flag.Parse()
|
||||||
|
|
||||||
|
ctx := context.Background()
|
||||||
|
idx, err := index.NewKustomizeIndex(ctx)
|
||||||
|
if err != nil {
|
||||||
|
log.Fatalf("Could not create an index: %v\n", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
// count tracks the number of documents in the index
|
||||||
|
count := 0
|
||||||
|
|
||||||
|
// kustomizationFilecount tracks the number of kustomization files in the index
|
||||||
|
kustomizationFilecount := 0
|
||||||
|
|
||||||
|
kindsMap := make(map[string]int)
|
||||||
|
identifiersMap := make(map[string]int)
|
||||||
|
kustomizeIdentifiersMap := make(map[string]int)
|
||||||
|
|
||||||
|
// ids tracks the unique IDs of the documents in the index
|
||||||
|
ids := make(map[string]struct{})
|
||||||
|
|
||||||
|
// get all the documents in the index
|
||||||
|
query := []byte(`{ "query":{ "match_all":{} } }`)
|
||||||
|
it := idx.IterateQuery(query, 10000, 60*time.Second)
|
||||||
|
for it.Next() {
|
||||||
|
for _, hit := range it.Value().Hits.Hits {
|
||||||
|
// check whether there is any duplicate IDs in the index
|
||||||
|
if _, ok := ids[hit.ID]; !ok {
|
||||||
|
ids[hit.ID] = struct{}{}
|
||||||
|
} else {
|
||||||
|
fmt.Printf("Found duplicate ID (%s)\n", hit.ID)
|
||||||
|
}
|
||||||
|
|
||||||
|
count++
|
||||||
|
iterateArr(hit.Document.Kinds, kindsMap)
|
||||||
|
iterateArr(hit.Document.Identifiers, identifiersMap)
|
||||||
|
|
||||||
|
if isKustomizationFile(hit.Document.FilePath) {
|
||||||
|
kustomizationFilecount++
|
||||||
|
iterateArr(hit.Document.Identifiers, kustomizeIdentifiersMap)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if err := it.Err(); err != nil {
|
||||||
|
fmt.Printf("Error iterating: %v\n", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
sortedKindsMapKeys := SortMapKeyByValue(kindsMap)
|
||||||
|
sortedIdentifiersMapKeys := SortMapKeyByValue(identifiersMap)
|
||||||
|
sortedKustomizeIdentifiersMapKeys := SortMapKeyByValue(kustomizeIdentifiersMap)
|
||||||
|
|
||||||
|
fmt.Printf(`The count of unique document IDs in the kustomize index: %d
|
||||||
|
There are %d documents in the kustomize index.
|
||||||
|
%d kinds of kubernetes objects are customized:`, len(ids), count, len(kindsMap))
|
||||||
|
fmt.Printf("\n")
|
||||||
|
|
||||||
|
kindCount := 0
|
||||||
|
for _, key := range sortedKindsMapKeys {
|
||||||
|
if *topKindsPtr < 0 || (*topKindsPtr >= 0 && kindCount < *topKindsPtr) {
|
||||||
|
fmt.Printf("\tkind `%s` is customimzed in %d documents\n", key, kindsMap[key])
|
||||||
|
kindCount++
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fmt.Printf("%d kinds of identifiers are found:\n", len(identifiersMap))
|
||||||
|
identifierCount := 0
|
||||||
|
for _, key := range sortedIdentifiersMapKeys {
|
||||||
|
if *topIdentifiersPtr < 0 || (*topIdentifiersPtr >= 0 && identifierCount < *topIdentifiersPtr) {
|
||||||
|
fmt.Printf("\tidentifier `%s` appears in %d documents\n", key, identifiersMap[key])
|
||||||
|
identifierCount++
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fmt.Printf(`There are %d kustomization files in the kustomize index.
|
||||||
|
%d kinds of kustomize features are found:`, kustomizationFilecount, len(kustomizeIdentifiersMap))
|
||||||
|
fmt.Printf("\n")
|
||||||
|
kustomizeFeatureCount := 0
|
||||||
|
for _, key := range sortedKustomizeIdentifiersMapKeys {
|
||||||
|
if *topKustomizeFeaturesPtr < 0 || (*topKustomizeFeaturesPtr >= 0 && kustomizeFeatureCount < *topKustomizeFeaturesPtr) {
|
||||||
|
fmt.Printf("\tfeature `%s` is used in %d documents\n", key, kustomizeIdentifiersMap[key])
|
||||||
|
kustomizeFeatureCount++
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
8
api/internal/crawl/cmd/log-parser/README.md
Normal file
8
api/internal/crawl/cmd/log-parser/README.md
Normal file
@@ -0,0 +1,8 @@
|
|||||||
|
This binary takes as its input a json file including GKE logs (which can be
|
||||||
|
[exported](https://cloud.google.com/logging/docs/export/configure_export_v2) into
|
||||||
|
[Cloud Storage](https://cloud.google.com/storage/docs/)),
|
||||||
|
and extracts the `textPayload` field of each log entry.
|
||||||
|
|
||||||
|
Here is an log entry example:
|
||||||
|
|
||||||
|
{"insertId":"1sxuh4jg5lw6w10","labels":{"compute.googleapis.com/resource_name":"gke-crawler2-default-pool-5e55ea05-gzgv","container.googleapis.com/namespace_name":"default","container.googleapis.com/pod_name":"kustomize-stats-5bczg","container.googleapis.com/stream":"stdout"},"logName":"projects/haiyanmeng-gke-dev/logs/kustomize-stats","receiveTimestamp":"2020-01-06T23:33:07.012831742Z","resource":{"labels":{"cluster_name":"crawler2","container_name":"kustomize-stats","instance_id":"8183086081854184383","namespace_id":"default","pod_id":"kustomize-stats-5bczg","project_id":"haiyanmeng-gke-dev","zone":"us-central1-a"},"type":"container"},"severity":"INFO","textPayload":"The kustomize index already exists\n","timestamp":"2020-01-06T23:32:46.628930547Z"}
|
||||||
48
api/internal/crawl/cmd/log-parser/main.go
Normal file
48
api/internal/crawl/cmd/log-parser/main.go
Normal file
@@ -0,0 +1,48 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"bufio"
|
||||||
|
"encoding/json"
|
||||||
|
"fmt"
|
||||||
|
"log"
|
||||||
|
"os"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
if len(os.Args) != 2 {
|
||||||
|
log.Fatalf("The usage of the command is: \n\t%s <log-file.json>", os.Args[0])
|
||||||
|
}
|
||||||
|
|
||||||
|
file, err := os.Open(os.Args[1])
|
||||||
|
if err != nil {
|
||||||
|
log.Fatal(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
closeFile := func(file *os.File) {
|
||||||
|
if err := file.Close(); err != nil {
|
||||||
|
log.Fatal(err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
defer closeFile(file)
|
||||||
|
|
||||||
|
scanner := bufio.NewScanner(file)
|
||||||
|
for scanner.Scan() {
|
||||||
|
line := scanner.Text()
|
||||||
|
|
||||||
|
var entry interface{}
|
||||||
|
if err := json.Unmarshal([]byte(line), &entry); err != nil {
|
||||||
|
log.Printf("failed to unmarshal a log entry: %s\n", line)
|
||||||
|
}
|
||||||
|
|
||||||
|
m := entry.(map[string]interface{})
|
||||||
|
if payload, ok := m["textPayload"]; ok {
|
||||||
|
fmt.Printf("%s", payload)
|
||||||
|
} else {
|
||||||
|
log.Printf("the log entry does not have the `textPayload` field: %s\n", line)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if err := scanner.Err(); err != nil {
|
||||||
|
log.Fatal(err)
|
||||||
|
}
|
||||||
|
}
|
||||||
20
api/internal/crawl/config/crawler/kustomize_stats/job.yaml
Normal file
20
api/internal/crawl/config/crawler/kustomize_stats/job.yaml
Normal file
@@ -0,0 +1,20 @@
|
|||||||
|
apiVersion: batch/v1
|
||||||
|
kind: Job
|
||||||
|
metadata:
|
||||||
|
name: kustomize-stats
|
||||||
|
spec:
|
||||||
|
template:
|
||||||
|
spec:
|
||||||
|
restartPolicy: OnFailure
|
||||||
|
containers:
|
||||||
|
- name: kustomize-stats
|
||||||
|
image: gcr.io/haiyanmeng-gke-dev/kustomize_stats:v1
|
||||||
|
imagePullPolicy: Always
|
||||||
|
command: ["/kustomize_stats"]
|
||||||
|
args: ["--kinds=50", "--identifiers=50", "--kustomize-features=50"]
|
||||||
|
env:
|
||||||
|
- name: ELASTICSEARCH_URL
|
||||||
|
valueFrom:
|
||||||
|
configMapKeyRef:
|
||||||
|
name: elasticsearch-config
|
||||||
|
key: es-url
|
||||||
@@ -0,0 +1,3 @@
|
|||||||
|
resources:
|
||||||
|
- ../base
|
||||||
|
- job.yaml
|
||||||
@@ -97,6 +97,17 @@ func (gc githubCrawler) Crawl(
|
|||||||
// it will try to add each string in konfig.RecognizedKustomizationFileNames() to
|
// it will try to add each string in konfig.RecognizedKustomizationFileNames() to
|
||||||
// d.FilePath, and try to fetch the document again.
|
// d.FilePath, and try to fetch the document again.
|
||||||
func (gc githubCrawler) FetchDocument(_ context.Context, d *doc.Document) error {
|
func (gc githubCrawler) FetchDocument(_ context.Context, d *doc.Document) error {
|
||||||
|
// set the default branch if it is empty
|
||||||
|
if d.DefaultBranch == "" {
|
||||||
|
url := gc.client.ReposRequest(d.RepositoryFullName())
|
||||||
|
defaultBranch, err := gc.client.GetDefaultBranch(url)
|
||||||
|
if err != nil {
|
||||||
|
logger.Printf(
|
||||||
|
"(error: %v) setting default_branch to master\n", err)
|
||||||
|
defaultBranch = "master"
|
||||||
|
}
|
||||||
|
d.DefaultBranch = defaultBranch
|
||||||
|
}
|
||||||
repoURL := d.RepositoryURL + "/" + d.FilePath + "?ref=" + d.DefaultBranch
|
repoURL := d.RepositoryURL + "/" + d.FilePath + "?ref=" + d.DefaultBranch
|
||||||
repoSpec, err := git.NewRepoSpecFromUrl(repoURL)
|
repoSpec, err := git.NewRepoSpecFromUrl(repoURL)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -107,9 +118,13 @@ func (gc githubCrawler) FetchDocument(_ context.Context, d *doc.Document) error
|
|||||||
"/" + repoSpec.Ref + "/" + repoSpec.Path
|
"/" + repoSpec.Ref + "/" + repoSpec.Path
|
||||||
|
|
||||||
handle := func(resp *http.Response, err error, path string) error {
|
handle := func(resp *http.Response, err error, path string) error {
|
||||||
|
if resp == nil {
|
||||||
|
return fmt.Errorf("empty http response (url: %s; path: %s), error: %v",
|
||||||
|
url, path, err)
|
||||||
|
}
|
||||||
if err == nil && resp.StatusCode == http.StatusOK {
|
if err == nil && resp.StatusCode == http.StatusOK {
|
||||||
d.IsSame = httpclient.FromCache(resp.Header)
|
d.IsSame = httpclient.FromCache(resp.Header)
|
||||||
defer resp.Body.Close()
|
defer CloseResponseBody(resp)
|
||||||
data, err := ioutil.ReadAll(resp.Body)
|
data, err := ioutil.ReadAll(resp.Body)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
@@ -296,7 +311,10 @@ func (gcl GhClient) GetFileData(k GhFileSpec) ([]byte, error) {
|
|||||||
return nil, fmt.Errorf("%+v: could not read '%s' metadata: %v",
|
return nil, fmt.Errorf("%+v: could not read '%s' metadata: %v",
|
||||||
k, url, err)
|
k, url, err)
|
||||||
}
|
}
|
||||||
resp.Body.Close()
|
|
||||||
|
if err := resp.Body.Close(); err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
type githubContentRawURL struct {
|
type githubContentRawURL struct {
|
||||||
DownloadURL string `json:"download_url,omitempty"`
|
DownloadURL string `json:"download_url,omitempty"`
|
||||||
@@ -315,18 +333,24 @@ func (gcl GhClient) GetFileData(k GhFileSpec) ([]byte, error) {
|
|||||||
k, rawURL.DownloadURL, err)
|
k, rawURL.DownloadURL, err)
|
||||||
}
|
}
|
||||||
|
|
||||||
defer resp.Body.Close()
|
defer CloseResponseBody(resp)
|
||||||
data, err = ioutil.ReadAll(resp.Body)
|
data, err = ioutil.ReadAll(resp.Body)
|
||||||
return data, err
|
return data, err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func CloseResponseBody(resp *http.Response) {
|
||||||
|
if err := resp.Body.Close(); err != nil {
|
||||||
|
log.Printf("failed to close response body: %v", err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func (gcl GhClient) GetDefaultBranch(url string) (string, error) {
|
func (gcl GhClient) GetDefaultBranch(url string) (string, error) {
|
||||||
resp, err := gcl.GetReposData(url)
|
resp, err := gcl.GetReposData(url)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return "", fmt.Errorf(
|
return "", fmt.Errorf(
|
||||||
"'%s' could not get default_branch: %v", url, err)
|
"'%s' could not get default_branch: %v", url, err)
|
||||||
}
|
}
|
||||||
defer resp.Body.Close()
|
defer CloseResponseBody(resp)
|
||||||
data, err := ioutil.ReadAll(resp.Body)
|
data, err := ioutil.ReadAll(resp.Body)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return "", fmt.Errorf(
|
return "", fmt.Errorf(
|
||||||
@@ -378,7 +402,7 @@ func (gcl GhClient) GetFileCreationTime(
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
defer resp.Body.Close()
|
defer CloseResponseBody(resp)
|
||||||
data, err := ioutil.ReadAll(resp.Body)
|
data, err := ioutil.ReadAll(resp.Body)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return defaultTime, fmt.Errorf(
|
return defaultTime, fmt.Errorf(
|
||||||
@@ -501,7 +525,7 @@ func (gcl GhClient) parseGithubResponse(getRequest string) GhResponseInfo {
|
|||||||
}
|
}
|
||||||
|
|
||||||
var data []byte
|
var data []byte
|
||||||
defer resp.Body.Close()
|
defer CloseResponseBody(resp)
|
||||||
data, requestInfo.Error = ioutil.ReadAll(resp.Body)
|
data, requestInfo.Error = ioutil.ReadAll(resp.Body)
|
||||||
if requestInfo.Error != nil {
|
if requestInfo.Error != nil {
|
||||||
return requestInfo
|
return requestInfo
|
||||||
@@ -566,7 +590,7 @@ func (gcl GhClient) Do(query string) (*http.Response, error) {
|
|||||||
// gcl.client.Do: a non-2xx status code doesn't cause an error.
|
// gcl.client.Do: a non-2xx status code doesn't cause an error.
|
||||||
// See https://golang.org/pkg/net/http/#Client.Do for more info.
|
// See https://golang.org/pkg/net/http/#Client.Do for more info.
|
||||||
resp, err := gcl.client.Do(req)
|
resp, err := gcl.client.Do(req)
|
||||||
if resp.StatusCode != http.StatusOK {
|
if resp != nil && resp.StatusCode != http.StatusOK {
|
||||||
err = fmt.Errorf("GhClient.Do(%s) failed with response code: %d",
|
err = fmt.Errorf("GhClient.Do(%s) failed with response code: %d",
|
||||||
query, resp.StatusCode)
|
query, resp.StatusCode)
|
||||||
}
|
}
|
||||||
@@ -580,7 +604,7 @@ func (gcl GhClient) getWithRetry(
|
|||||||
|
|
||||||
retryCount := gcl.retryCount
|
retryCount := gcl.retryCount
|
||||||
|
|
||||||
for resp.StatusCode == http.StatusForbidden && retryCount > 0 {
|
for resp != nil && resp.StatusCode == http.StatusForbidden && retryCount > 0 {
|
||||||
retryTime := resp.Header.Get("Retry-After")
|
retryTime := resp.Header.Get("Retry-After")
|
||||||
i, errAtoi := strconv.Atoi(retryTime)
|
i, errAtoi := strconv.Atoi(retryTime)
|
||||||
if errAtoi != nil {
|
if errAtoi != nil {
|
||||||
|
|||||||
@@ -116,9 +116,11 @@ type RequestConfig struct {
|
|||||||
// the URL method to get the string value of the URL. See request.CopyWith, to
|
// the URL method to get the string value of the URL. See request.CopyWith, to
|
||||||
// understand why the request object is useful.
|
// understand why the request object is useful.
|
||||||
func (rc RequestConfig) CodeSearchRequestWith(query Query) request {
|
func (rc RequestConfig) CodeSearchRequestWith(query Query) request {
|
||||||
req := rc.makeRequest("search/code", query)
|
vals := url.Values{
|
||||||
req.vals.Set("sort", "indexed")
|
"sort": []string{"indexed"},
|
||||||
req.vals.Set("order", "desc")
|
"order": []string{"desc"},
|
||||||
|
}
|
||||||
|
req := rc.makeRequest("search/code", query, vals)
|
||||||
return req
|
return req
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -126,27 +128,25 @@ func (rc RequestConfig) CodeSearchRequestWith(query Query) request {
|
|||||||
// query for the Github API to find the dowload information of this filepath.
|
// query for the Github API to find the dowload information of this filepath.
|
||||||
func (rc RequestConfig) ContentsRequest(fullRepoName, path string) string {
|
func (rc RequestConfig) ContentsRequest(fullRepoName, path string) string {
|
||||||
uri := fmt.Sprintf("repos/%s/contents/%s", fullRepoName, path)
|
uri := fmt.Sprintf("repos/%s/contents/%s", fullRepoName, path)
|
||||||
return rc.makeRequest(uri, Query{}).URL()
|
return rc.makeRequest(uri, Query{}, url.Values{}).URL()
|
||||||
}
|
}
|
||||||
|
|
||||||
func (rc RequestConfig) ReposRequest(fullRepoName string) string {
|
func (rc RequestConfig) ReposRequest(fullRepoName string) string {
|
||||||
uri := fmt.Sprintf("repos/%s", fullRepoName)
|
uri := fmt.Sprintf("repos/%s", fullRepoName)
|
||||||
return rc.makeRequest(uri, Query{}).URL()
|
return rc.makeRequest(uri, Query{}, url.Values{}).URL()
|
||||||
}
|
|
||||||
|
|
||||||
func escapeSpace(s string) string {
|
|
||||||
return strings.Replace(s, " ", "%20", -1)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// CommitsRequest given the repo name, and a filepath returns a formatted query
|
// CommitsRequest given the repo name, and a filepath returns a formatted query
|
||||||
// for the Github API to find the commits that affect this file.
|
// for the Github API to find the commits that affect this file.
|
||||||
func (rc RequestConfig) CommitsRequest(fullRepoName, path string) string {
|
func (rc RequestConfig) CommitsRequest(fullRepoName, path string) string {
|
||||||
uri := fmt.Sprintf("repos/%s/commits", fullRepoName)
|
uri := fmt.Sprintf("repos/%s/commits", fullRepoName)
|
||||||
return rc.makeRequest(uri, Query{Path(escapeSpace(path))}).URL()
|
vals := url.Values{
|
||||||
|
"path": []string{path},
|
||||||
|
}
|
||||||
|
return rc.makeRequest(uri, Query{}, vals).URL()
|
||||||
}
|
}
|
||||||
|
|
||||||
func (rc RequestConfig) makeRequest(path string, query Query) request {
|
func (rc RequestConfig) makeRequest(path string, query Query, vals url.Values) request {
|
||||||
vals := url.Values{}
|
|
||||||
vals.Set(perPageArg, fmt.Sprint(rc.perPage))
|
vals.Set(perPageArg, fmt.Sprint(rc.perPage))
|
||||||
|
|
||||||
return request{
|
return request{
|
||||||
|
|||||||
@@ -101,7 +101,7 @@ func TestGithubSearchQuery(t *testing.T) {
|
|||||||
"examples/helloWorld/kustomization.yaml?per_page=100",
|
"examples/helloWorld/kustomization.yaml?per_page=100",
|
||||||
|
|
||||||
expectedCommitsQuery: "https://api.github.com/repos/kubernetes-sigs/kustomize/commits?" +
|
expectedCommitsQuery: "https://api.github.com/repos/kubernetes-sigs/kustomize/commits?" +
|
||||||
"q=path:examples/helloWorld/kustomization.yaml&per_page=100",
|
"path=examples%2FhelloWorld%2Fkustomization.yaml&per_page=100",
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
rc: RequestConfig{
|
rc: RequestConfig{
|
||||||
@@ -121,7 +121,7 @@ func TestGithubSearchQuery(t *testing.T) {
|
|||||||
"examples%201/helloWorld/kustomization.yaml?per_page=100",
|
"examples%201/helloWorld/kustomization.yaml?per_page=100",
|
||||||
|
|
||||||
expectedCommitsQuery: "https://api.github.com/repos/kubernetes-sigs/kustomize/commits?" +
|
expectedCommitsQuery: "https://api.github.com/repos/kubernetes-sigs/kustomize/commits?" +
|
||||||
"q=path:examples%201/helloWorld/kustomization.yaml&per_page=100",
|
"path=examples+1%2FhelloWorld%2Fkustomization.yaml&per_page=100",
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -78,6 +78,9 @@ func (doc *KustomizationDocument) GetResources() ([]*Document, error) {
|
|||||||
|
|
||||||
res := make([]*Document, 0, len(k.Resources))
|
res := make([]*Document, 0, len(k.Resources))
|
||||||
for _, r := range k.Resources {
|
for _, r := range k.Resources {
|
||||||
|
if strings.TrimSpace(r) == "" {
|
||||||
|
continue
|
||||||
|
}
|
||||||
next, err := doc.Document.FromRelativePath(r)
|
next, err := doc.Document.FromRelativePath(r)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Printf("GetResources error: %v\n", err)
|
fmt.Printf("GetResources error: %v\n", err)
|
||||||
|
|||||||
@@ -8,6 +8,6 @@ require (
|
|||||||
github.com/gorilla/mux v1.7.3
|
github.com/gorilla/mux v1.7.3
|
||||||
github.com/gregjones/httpcache v0.0.0-20190611155906-901d90724c79
|
github.com/gregjones/httpcache v0.0.0-20190611155906-901d90724c79
|
||||||
github.com/rs/cors v1.7.0
|
github.com/rs/cors v1.7.0
|
||||||
sigs.k8s.io/kustomize/api v0.3.0
|
sigs.k8s.io/kustomize/api v0.3.1
|
||||||
sigs.k8s.io/yaml v1.1.0
|
sigs.k8s.io/yaml v1.1.0
|
||||||
)
|
)
|
||||||
|
|||||||
@@ -133,13 +133,8 @@ github.com/googleapis/gax-go/v2 v2.0.4/go.mod h1:0Wqv26UfaUD9n4G6kQubkQ+KchISgw+
|
|||||||
github.com/googleapis/gnostic v0.0.0-20170729233727-0c5108395e2d h1:7XGaL1e6bYS1yIonGp9761ExpPPV1ui0SAC59Yube9k=
|
github.com/googleapis/gnostic v0.0.0-20170729233727-0c5108395e2d h1:7XGaL1e6bYS1yIonGp9761ExpPPV1ui0SAC59Yube9k=
|
||||||
github.com/googleapis/gnostic v0.0.0-20170729233727-0c5108395e2d/go.mod h1:sJBsCZ4ayReDTBIg8b9dl28c5xFWyhBTVRp3pOg5EKY=
|
github.com/googleapis/gnostic v0.0.0-20170729233727-0c5108395e2d/go.mod h1:sJBsCZ4ayReDTBIg8b9dl28c5xFWyhBTVRp3pOg5EKY=
|
||||||
github.com/gophercloud/gophercloud v0.1.0/go.mod h1:vxM41WHh5uqHVBMZHzuwNOHh8XEoIEcSTewFxm1c5g8=
|
github.com/gophercloud/gophercloud v0.1.0/go.mod h1:vxM41WHh5uqHVBMZHzuwNOHh8XEoIEcSTewFxm1c5g8=
|
||||||
github.com/gorilla/context v0.0.0-20160226214623-1ea25387ff6f/go.mod h1:kBGZzfjB9CEq2AlWe17Uuf7NDRt0dE0s8S51q0aT7Yg=
|
|
||||||
github.com/gorilla/mux v1.6.0/go.mod h1:1lud6UwP+6orDFRuTfBEV8e9/aOM/c4fVVCaMa2zaAs=
|
|
||||||
github.com/gorilla/mux v1.7.3 h1:gnP5JzjVOuiZD07fKKToCAOjS0yOpj/qPETTXCCS6hw=
|
github.com/gorilla/mux v1.7.3 h1:gnP5JzjVOuiZD07fKKToCAOjS0yOpj/qPETTXCCS6hw=
|
||||||
github.com/gorilla/mux v1.7.3/go.mod h1:1lud6UwP+6orDFRuTfBEV8e9/aOM/c4fVVCaMa2zaAs=
|
github.com/gorilla/mux v1.7.3/go.mod h1:1lud6UwP+6orDFRuTfBEV8e9/aOM/c4fVVCaMa2zaAs=
|
||||||
github.com/gorilla/securecookie v0.0.0-20160422134519-667fe4e3466a/go.mod h1:ra0sb63/xPlUeL+yeDciTfxMRAA+MP+HVt/4epWDjd4=
|
|
||||||
github.com/gorilla/sessions v0.0.0-20160922145804-ca9ada445741/go.mod h1:+WVp8kdw6VhyKExm03PAMRn2ZxnPtm58pV0dBVPdhHE=
|
|
||||||
github.com/gorilla/websocket v1.2.0/go.mod h1:E7qHFY5m1UJ88s3WnNqhKjPHQ0heANvMoAMk2YaljkQ=
|
|
||||||
github.com/gorilla/websocket v1.4.0/go.mod h1:E7qHFY5m1UJ88s3WnNqhKjPHQ0heANvMoAMk2YaljkQ=
|
github.com/gorilla/websocket v1.4.0/go.mod h1:E7qHFY5m1UJ88s3WnNqhKjPHQ0heANvMoAMk2YaljkQ=
|
||||||
github.com/gostaticanalysis/analysisutil v0.0.0-20190318220348-4088753ea4d3/go.mod h1:eEOZF4jCKGi+aprrirO9e7WKB3beBRtWgqGunKl6pKE=
|
github.com/gostaticanalysis/analysisutil v0.0.0-20190318220348-4088753ea4d3/go.mod h1:eEOZF4jCKGi+aprrirO9e7WKB3beBRtWgqGunKl6pKE=
|
||||||
github.com/gregjones/httpcache v0.0.0-20180305231024-9cad4c3443a7/go.mod h1:FecbI9+v66THATjSRHfNgh1IVFe/9kFxbXtjV0ctIMA=
|
github.com/gregjones/httpcache v0.0.0-20180305231024-9cad4c3443a7/go.mod h1:FecbI9+v66THATjSRHfNgh1IVFe/9kFxbXtjV0ctIMA=
|
||||||
@@ -198,7 +193,6 @@ github.com/modern-go/reflect2 v0.0.0-20180320133207-05fbef0ca5da/go.mod h1:bx2lN
|
|||||||
github.com/modern-go/reflect2 v0.0.0-20180701023420-4b7aa43c6742/go.mod h1:bx2lNnkwVCuqBIxFjflWJWanXIb3RllmbCylyMrvgv0=
|
github.com/modern-go/reflect2 v0.0.0-20180701023420-4b7aa43c6742/go.mod h1:bx2lNnkwVCuqBIxFjflWJWanXIb3RllmbCylyMrvgv0=
|
||||||
github.com/modern-go/reflect2 v1.0.1 h1:9f412s+6RmYXLWZSEzVVgPGK7C2PphHj5RJrvfx9AWI=
|
github.com/modern-go/reflect2 v1.0.1 h1:9f412s+6RmYXLWZSEzVVgPGK7C2PphHj5RJrvfx9AWI=
|
||||||
github.com/modern-go/reflect2 v1.0.1/go.mod h1:bx2lNnkwVCuqBIxFjflWJWanXIb3RllmbCylyMrvgv0=
|
github.com/modern-go/reflect2 v1.0.1/go.mod h1:bx2lNnkwVCuqBIxFjflWJWanXIb3RllmbCylyMrvgv0=
|
||||||
github.com/monopole/mdrip v1.0.1/go.mod h1:/7E04hlzRG9Jrp6WILZfYYm/REoJWL2l+MlsCO1eH74=
|
|
||||||
github.com/mozilla/tls-observatory v0.0.0-20190404164649-a3c1b6cfecfd/go.mod h1:SrKMQvPiws7F7iqYp8/TX+IhxCYhzr6N/1yb8cwHsGk=
|
github.com/mozilla/tls-observatory v0.0.0-20190404164649-a3c1b6cfecfd/go.mod h1:SrKMQvPiws7F7iqYp8/TX+IhxCYhzr6N/1yb8cwHsGk=
|
||||||
github.com/munnerz/goautoneg v0.0.0-20120707110453-a547fc61f48d/go.mod h1:+n7T8mK8HuQTcFwEeznm/DIxMOiR9yIdICNftLE1DvQ=
|
github.com/munnerz/goautoneg v0.0.0-20120707110453-a547fc61f48d/go.mod h1:+n7T8mK8HuQTcFwEeznm/DIxMOiR9yIdICNftLE1DvQ=
|
||||||
github.com/mwitkow/go-conntrack v0.0.0-20161129095857-cc309e4a2223/go.mod h1:qRWi+5nqEBWmkhHvq77mSJWrCKwh8bxhgT7d/eI7P4U=
|
github.com/mwitkow/go-conntrack v0.0.0-20161129095857-cc309e4a2223/go.mod h1:qRWi+5nqEBWmkhHvq77mSJWrCKwh8bxhgT7d/eI7P4U=
|
||||||
@@ -240,7 +234,6 @@ github.com/shirou/gopsutil v0.0.0-20190901111213-e4ec7b275ada/go.mod h1:WWnYX4lz
|
|||||||
github.com/shirou/w32 v0.0.0-20160930032740-bb4de0191aa4/go.mod h1:qsXQc7+bwAM3Q1u/4XEfrquwF8Lw7D7y5cD8CuHnfIc=
|
github.com/shirou/w32 v0.0.0-20160930032740-bb4de0191aa4/go.mod h1:qsXQc7+bwAM3Q1u/4XEfrquwF8Lw7D7y5cD8CuHnfIc=
|
||||||
github.com/shurcooL/go v0.0.0-20180423040247-9e1955d9fb6e/go.mod h1:TDJrrUr11Vxrven61rcy3hJMUqaf/CLWYhHNPmT14Lk=
|
github.com/shurcooL/go v0.0.0-20180423040247-9e1955d9fb6e/go.mod h1:TDJrrUr11Vxrven61rcy3hJMUqaf/CLWYhHNPmT14Lk=
|
||||||
github.com/shurcooL/go-goon v0.0.0-20170922171312-37c2f522c041/go.mod h1:N5mDOmsrJOB+vfqUK+7DmDyjhSLIIBnXo9lvZJj3MWQ=
|
github.com/shurcooL/go-goon v0.0.0-20170922171312-37c2f522c041/go.mod h1:N5mDOmsrJOB+vfqUK+7DmDyjhSLIIBnXo9lvZJj3MWQ=
|
||||||
github.com/shurcooL/sanitized_anchor_name v1.0.0/go.mod h1:1NzhyTcUVG4SuEtjjoZeVRXNmyL/1OwPU0+IJeTBvfc=
|
|
||||||
github.com/sirupsen/logrus v1.2.0/go.mod h1:LxeOpSwHxABJmUn/MG1IvRgCAasNZTLOkJPxbbu5VWo=
|
github.com/sirupsen/logrus v1.2.0/go.mod h1:LxeOpSwHxABJmUn/MG1IvRgCAasNZTLOkJPxbbu5VWo=
|
||||||
github.com/sirupsen/logrus v1.4.2/go.mod h1:tLMulIdttU9McNUspp0xgXVQah82FyeX6MwdIuYE2rE=
|
github.com/sirupsen/logrus v1.4.2/go.mod h1:tLMulIdttU9McNUspp0xgXVQah82FyeX6MwdIuYE2rE=
|
||||||
github.com/soheilhy/cmux v0.1.4/go.mod h1:IM3LyeVVIOuxMH7sFAkER9+bJ4dT7Ms6E4xg4kGIyLM=
|
github.com/soheilhy/cmux v0.1.4/go.mod h1:IM3LyeVVIOuxMH7sFAkER9+bJ4dT7Ms6E4xg4kGIyLM=
|
||||||
@@ -387,7 +380,6 @@ gopkg.in/fsnotify.v1 v1.4.7/go.mod h1:Tz8NjZHkW78fSQdbUxIjBTcgA1z1m8ZHf0WmKUhAMy
|
|||||||
gopkg.in/inf.v0 v0.9.1 h1:73M5CoZyi3ZLMOyDlQh031Cx6N9NDJ2Vvfl76EDAgDc=
|
gopkg.in/inf.v0 v0.9.1 h1:73M5CoZyi3ZLMOyDlQh031Cx6N9NDJ2Vvfl76EDAgDc=
|
||||||
gopkg.in/inf.v0 v0.9.1/go.mod h1:cWUDdTG/fYaXco+Dcufb5Vnc6Gp2YChqWtbxRZE0mXw=
|
gopkg.in/inf.v0 v0.9.1/go.mod h1:cWUDdTG/fYaXco+Dcufb5Vnc6Gp2YChqWtbxRZE0mXw=
|
||||||
gopkg.in/resty.v1 v1.12.0/go.mod h1:mDo4pnntr5jdWRML875a/NmxYqAlA73dVijT2AXvQQo=
|
gopkg.in/resty.v1 v1.12.0/go.mod h1:mDo4pnntr5jdWRML875a/NmxYqAlA73dVijT2AXvQQo=
|
||||||
gopkg.in/russross/blackfriday.v2 v2.0.0/go.mod h1:6sSBNz/GtOm/pJTuh5UmBK2ZHfmnxGbl2NZg1UliSOI=
|
|
||||||
gopkg.in/tomb.v1 v1.0.0-20141024135613-dd632973f1e7 h1:uRGJdciOHaEIrze2W8Q3AKkepLTh2hOroT7a+7czfdQ=
|
gopkg.in/tomb.v1 v1.0.0-20141024135613-dd632973f1e7 h1:uRGJdciOHaEIrze2W8Q3AKkepLTh2hOroT7a+7czfdQ=
|
||||||
gopkg.in/tomb.v1 v1.0.0-20141024135613-dd632973f1e7/go.mod h1:dt/ZhP58zS4L8KSrWDmTeBkI65Dw0HsyUHuEVlX15mw=
|
gopkg.in/tomb.v1 v1.0.0-20141024135613-dd632973f1e7/go.mod h1:dt/ZhP58zS4L8KSrWDmTeBkI65Dw0HsyUHuEVlX15mw=
|
||||||
gopkg.in/yaml.v2 v2.0.0-20170812160011-eb3733d160e7/go.mod h1:JAlM8MvJe8wmxCU4Bli9HhUf9+ttbYbLASfIpnQbh74=
|
gopkg.in/yaml.v2 v2.0.0-20170812160011-eb3733d160e7/go.mod h1:JAlM8MvJe8wmxCU4Bli9HhUf9+ttbYbLASfIpnQbh74=
|
||||||
@@ -415,8 +407,8 @@ k8s.io/utils v0.0.0-20191114184206-e782cd3c129f/go.mod h1:sZAwmy6armz5eXlNoLmJcl
|
|||||||
mvdan.cc/interfacer v0.0.0-20180901003855-c20040233aed/go.mod h1:Xkxe497xwlCKkIaQYRfC7CSLworTXY9RMqwhhCm+8Nc=
|
mvdan.cc/interfacer v0.0.0-20180901003855-c20040233aed/go.mod h1:Xkxe497xwlCKkIaQYRfC7CSLworTXY9RMqwhhCm+8Nc=
|
||||||
mvdan.cc/lint v0.0.0-20170908181259-adc824a0674b/go.mod h1:2odslEg/xrtNQqCYg2/jCoyKnw3vv5biOc3JnIcYfL4=
|
mvdan.cc/lint v0.0.0-20170908181259-adc824a0674b/go.mod h1:2odslEg/xrtNQqCYg2/jCoyKnw3vv5biOc3JnIcYfL4=
|
||||||
mvdan.cc/unparam v0.0.0-20190720180237-d51796306d8f/go.mod h1:4G1h5nDURzA3bwVMZIVpwbkw+04kSxk3rAtzlimaUJw=
|
mvdan.cc/unparam v0.0.0-20190720180237-d51796306d8f/go.mod h1:4G1h5nDURzA3bwVMZIVpwbkw+04kSxk3rAtzlimaUJw=
|
||||||
sigs.k8s.io/kustomize/api v0.3.0 h1:riR/YsL75nGb+aIPFdIRiqu21+OZbAXQybDS7+FUYRg=
|
sigs.k8s.io/kustomize/api v0.3.1 h1:oqMIXvS6tFEUVuKIRUKDa05eC4Hh+cb9JYg8Zhp2d24=
|
||||||
sigs.k8s.io/kustomize/api v0.3.0/go.mod h1:DWNMJBV1xvLruMpihGgnIPznMwHpwUSrxz6v3gnw5kw=
|
sigs.k8s.io/kustomize/api v0.3.1/go.mod h1:A+ATnlHqzictQfQC1q3KB/T6MSr0UWQsrrLxMWkge2E=
|
||||||
sigs.k8s.io/structured-merge-diff v0.0.0-20190525122527-15d366b2352e/go.mod h1:wWxsB5ozmmv/SG7nM11ayaAW51xMvak/t1r0CSlcokI=
|
sigs.k8s.io/structured-merge-diff v0.0.0-20190525122527-15d366b2352e/go.mod h1:wWxsB5ozmmv/SG7nM11ayaAW51xMvak/t1r0CSlcokI=
|
||||||
sigs.k8s.io/yaml v1.1.0 h1:4A07+ZFc2wgJwo8YNlQpr1rVlgUDlxXHhPJciaPY5gs=
|
sigs.k8s.io/yaml v1.1.0 h1:4A07+ZFc2wgJwo8YNlQpr1rVlgUDlxXHhPJciaPY5gs=
|
||||||
sigs.k8s.io/yaml v1.1.0/go.mod h1:UJmg0vDUVViEyp3mgSv9WPwZCDxu4rQW1olrI1uml+o=
|
sigs.k8s.io/yaml v1.1.0/go.mod h1:UJmg0vDUVViEyp3mgSv9WPwZCDxu4rQW1olrI1uml+o=
|
||||||
|
|||||||
@@ -309,9 +309,9 @@ func (idx *index) Exists(id string) (bool, error) {
|
|||||||
op.WithPretty(),
|
op.WithPretty(),
|
||||||
)
|
)
|
||||||
|
|
||||||
if !res.IsError() {
|
if res != nil && !res.IsError() {
|
||||||
return true, nil
|
return true, nil
|
||||||
} else if res.StatusCode == 404 {
|
} else if res != nil && res.StatusCode == 404 {
|
||||||
return false, nil
|
return false, nil
|
||||||
} else {
|
} else {
|
||||||
return false, idx.responseErrorOrNil(
|
return false, idx.responseErrorOrNil(
|
||||||
|
|||||||
180
api/internal/crawl/search_cmds/creationTime.md
Normal file
180
api/internal/crawl/search_cmds/creationTime.md
Normal file
@@ -0,0 +1,180 @@
|
|||||||
|
Find out the largest value of the `creationTime` field:
|
||||||
|
```
|
||||||
|
curl -X POST "${ElasticSearchURL}:9200/kustomize/_search?size=0&pretty" -H 'Content-Type: application/json' -d'
|
||||||
|
{
|
||||||
|
"aggs" : {
|
||||||
|
"max_creationTime" : { "max" : { "field" : "creationTime" } }
|
||||||
|
}
|
||||||
|
}
|
||||||
|
'
|
||||||
|
```
|
||||||
|
|
||||||
|
Find out the smallest value of the `creationTime` field:
|
||||||
|
```
|
||||||
|
curl -X POST "${ElasticSearchURL}:9200/kustomize/_search?size=0&pretty" -H 'Content-Type: application/json' -d'
|
||||||
|
{
|
||||||
|
"aggs" : {
|
||||||
|
"min_creationTime" : { "min" : { "field" : "creationTime" } }
|
||||||
|
}
|
||||||
|
}
|
||||||
|
'
|
||||||
|
```
|
||||||
|
|
||||||
|
Find out the smallest value of the `creationTime` field of all the kustomization files:
|
||||||
|
```
|
||||||
|
curl -X POST "${ElasticSearchURL}:9200/kustomize/_search?size=0&pretty" -H 'Content-Type: application/json' -d'
|
||||||
|
{
|
||||||
|
"query": {
|
||||||
|
"bool": {
|
||||||
|
"filter": [
|
||||||
|
{ "regexp": { "filePath": ".*/kustomization((.yaml)?|(.yml)?)/*" }}
|
||||||
|
]
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"aggs" : {
|
||||||
|
"min_creationTime" : { "min" : { "field" : "creationTime" } }
|
||||||
|
}
|
||||||
|
}
|
||||||
|
'
|
||||||
|
```
|
||||||
|
|
||||||
|
Find out the smallest value of the `creationTime` field of all kustomize resource files:
|
||||||
|
```
|
||||||
|
curl -X POST "${ElasticSearchURL}:9200/kustomize/_search?size=0&pretty" -H 'Content-Type: application/json' -d'
|
||||||
|
{
|
||||||
|
"query": {
|
||||||
|
"bool": {
|
||||||
|
"must_not": {
|
||||||
|
"regexp": { "filePath": ".*/kustomization((.yaml)?|(.yml)?)/*" }
|
||||||
|
}
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"aggs" : {
|
||||||
|
"min_creationTime" : { "min" : { "field" : "creationTime" } }
|
||||||
|
}
|
||||||
|
}
|
||||||
|
'
|
||||||
|
```
|
||||||
|
|
||||||
|
Query all the documents whose `creationTime` <= `2016-07-29T17:38:26.000Z`:
|
||||||
|
```
|
||||||
|
curl -X GET "${ElasticSearchURL}:9200/kustomize/_search?pretty" -H 'Content-Type: application/json' -d'
|
||||||
|
{
|
||||||
|
"query": {
|
||||||
|
"range": {
|
||||||
|
"creationTime": {
|
||||||
|
"lte": "2016-07-29T17:38:26.000Z"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
'
|
||||||
|
```
|
||||||
|
|
||||||
|
Query all the documents whose `creationTime` falls within the specific range:
|
||||||
|
```
|
||||||
|
curl -X GET "${ElasticSearchURL}:9200/kustomize/_search?pretty" -H 'Content-Type: application/json' -d'
|
||||||
|
{
|
||||||
|
"query": {
|
||||||
|
"range": {
|
||||||
|
"creationTime": {
|
||||||
|
"gte": "2016-07-29T17:38:26.000Z",
|
||||||
|
"lte": "2016-08-29T17:38:26.000Z"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
'
|
||||||
|
```
|
||||||
|
|
||||||
|
Aggregate how many new kustomization files were added into Github each month:
|
||||||
|
```
|
||||||
|
curl -X GET "${ElasticSearchURL}:9200/kustomize/_search?size=0&pretty" -H 'Content-Type: application/json' -d'
|
||||||
|
{
|
||||||
|
"query": {
|
||||||
|
"bool": {
|
||||||
|
"filter": [
|
||||||
|
{ "regexp": { "filePath": ".*/kustomization((.yaml)?|(.yml)?)/*" }}
|
||||||
|
]
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"aggs" : {
|
||||||
|
"newFiles_over_time" : {
|
||||||
|
"date_histogram" : {
|
||||||
|
"field" : "creationTime",
|
||||||
|
"interval" : "month"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
'
|
||||||
|
```
|
||||||
|
|
||||||
|
Aggregate how many new kustomize resource files were added into Github each month:
|
||||||
|
```
|
||||||
|
curl -X GET "${ElasticSearchURL}:9200/kustomize/_search?size=0&pretty" -H 'Content-Type: application/json' -d'
|
||||||
|
{
|
||||||
|
"query": {
|
||||||
|
"bool": {
|
||||||
|
"must_not": [
|
||||||
|
{ "regexp": { "filePath": ".*/kustomization((.yaml)?|(.yml)?)/*" }}
|
||||||
|
]
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"aggs" : {
|
||||||
|
"newFiles_over_time" : {
|
||||||
|
"date_histogram" : {
|
||||||
|
"field" : "creationTime",
|
||||||
|
"interval" : "month"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
'
|
||||||
|
```
|
||||||
|
|
||||||
|
Aggregate how many new kustomization files were added into Github each year:
|
||||||
|
```
|
||||||
|
curl -X GET "${ElasticSearchURL}:9200/kustomize/_search?size=0&pretty" -H 'Content-Type: application/json' -d'
|
||||||
|
{
|
||||||
|
"query": {
|
||||||
|
"bool": {
|
||||||
|
"filter": [
|
||||||
|
{ "regexp": { "filePath": ".*/kustomization((.yaml)?|(.yml)?)/*" }}
|
||||||
|
]
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"aggs" : {
|
||||||
|
"newFiles_over_time" : {
|
||||||
|
"date_histogram" : {
|
||||||
|
"field" : "creationTime",
|
||||||
|
"interval" : "year"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
'
|
||||||
|
```
|
||||||
|
|
||||||
|
Aggregate how many new kustomize resource files were added into Github each year:
|
||||||
|
```
|
||||||
|
curl -X GET "${ElasticSearchURL}:9200/kustomize/_search?size=0&pretty" -H 'Content-Type: application/json' -d'
|
||||||
|
{
|
||||||
|
"query": {
|
||||||
|
"bool": {
|
||||||
|
"must_not": [
|
||||||
|
{ "regexp": { "filePath": ".*/kustomization((.yaml)?|(.yml)?)/*" }}
|
||||||
|
]
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"aggs" : {
|
||||||
|
"newFiles_over_time" : {
|
||||||
|
"date_histogram" : {
|
||||||
|
"field" : "creationTime",
|
||||||
|
"interval" : "year"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
'
|
||||||
|
```
|
||||||
32
api/internal/crawl/search_cmds/defaultBranch.md
Normal file
32
api/internal/crawl/search_cmds/defaultBranch.md
Normal file
@@ -0,0 +1,32 @@
|
|||||||
|
Count distinct values of the `defaultBranch` field:
|
||||||
|
```
|
||||||
|
curl -X POST "${ElasticSearchURL}:9200/kustomize/_search?size=0&pretty" -H 'Content-Type: application/json' -d'
|
||||||
|
{
|
||||||
|
"aggs" : {
|
||||||
|
"defaultBranch_count" : {
|
||||||
|
"cardinality" : {
|
||||||
|
"field" : "defaultBranch",
|
||||||
|
"precision_threshold": 40000
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
'
|
||||||
|
```
|
||||||
|
|
||||||
|
List all the github branches where kustomization files and kustomize resource files live,
|
||||||
|
and how many kustomization files and kustomize resource files live in each branch:
|
||||||
|
```
|
||||||
|
curl -X GET "${ElasticSearchURL}:9200/kustomize/_search?pretty" -H 'Content-Type: application/json' -d'
|
||||||
|
{
|
||||||
|
"aggs" : {
|
||||||
|
"defaultBranch" : {
|
||||||
|
"terms" : {
|
||||||
|
"field" : "defaultBranch",
|
||||||
|
"size": 41
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
'
|
||||||
|
```
|
||||||
55
api/internal/crawl/search_cmds/fieldExistence.md
Normal file
55
api/internal/crawl/search_cmds/fieldExistence.md
Normal file
@@ -0,0 +1,55 @@
|
|||||||
|
Count the documents whose `document` field is empty (The reason why the `document` field
|
||||||
|
of a document is empty is because of empty documents):
|
||||||
|
```
|
||||||
|
curl -X GET "${ElasticSearchURL}:9200/kustomize/_search?pretty" -H 'Content-Type: application/json' -d'
|
||||||
|
{
|
||||||
|
"size": 10000,
|
||||||
|
"query": {
|
||||||
|
"bool": {
|
||||||
|
"must_not": {
|
||||||
|
"exists": {
|
||||||
|
"field": "document"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
'
|
||||||
|
```
|
||||||
|
|
||||||
|
Find all the documents having the `creationTime` field set:
|
||||||
|
```
|
||||||
|
curl -X GET "${ElasticSearchURL}:9200/kustomize/_search?pretty" -H 'Content-Type: application/json' -d'
|
||||||
|
{
|
||||||
|
"query": {
|
||||||
|
"exists": {
|
||||||
|
"field": "creationTime"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
'
|
||||||
|
```
|
||||||
|
|
||||||
|
Find all the documents whose `creationTime` field is not set:
|
||||||
|
```
|
||||||
|
curl -X GET "${ElasticSearchURL}:9200/kustomize/_search?pretty" -H 'Content-Type: application/json' -d'
|
||||||
|
{
|
||||||
|
"size": 10000,
|
||||||
|
"query": {
|
||||||
|
"bool": {
|
||||||
|
"must_not": {
|
||||||
|
"exists": {
|
||||||
|
"field": "creationTime"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
'
|
||||||
|
```
|
||||||
|
|
||||||
|
The following fields of a document in the kustomize index are always non-empty:
|
||||||
|
`repositoryUrl`, `filePath`, `defaultBranch`.
|
||||||
|
|
||||||
|
The following fields of a document in the kustomize index may be empty:
|
||||||
|
`kinds`, `identifiers`, `values`.
|
||||||
66
api/internal/crawl/search_cmds/keyword_search.md
Normal file
66
api/internal/crawl/search_cmds/keyword_search.md
Normal file
@@ -0,0 +1,66 @@
|
|||||||
|
Count the documents in the index whose `repositoryUrl` field starts with
|
||||||
|
`https://github.com/`:
|
||||||
|
```
|
||||||
|
curl -X GET "${ElasticSearchURL}:9200/kustomize/_search?pretty" -H 'Content-Type: application/json' -d'
|
||||||
|
{
|
||||||
|
"query": {
|
||||||
|
"bool": {
|
||||||
|
"filter": [
|
||||||
|
{ "regexp": { "repositoryUrl": "https://github.com/.*" }}
|
||||||
|
]
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
'
|
||||||
|
```
|
||||||
|
|
||||||
|
Count the documents in the index whose `repositoryUrl` field does not start with
|
||||||
|
`https://github.com/`:
|
||||||
|
```
|
||||||
|
curl -X GET "${ElasticSearchURL}:9200/kustomize/_search?pretty" -H 'Content-Type: application/json' -d'
|
||||||
|
{
|
||||||
|
"query": {
|
||||||
|
"bool": {
|
||||||
|
"must_not": [
|
||||||
|
{ "regexp": { "repositoryUrl": "https://github.com/.*" }}
|
||||||
|
]
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
'
|
||||||
|
```
|
||||||
|
|
||||||
|
Search all the documents matching the given `repositoryUrl` and `filePath`, and return
|
||||||
|
a version for each search hit:
|
||||||
|
```
|
||||||
|
curl -X GET "${ElasticSearchURL}:9200/kustomize/_search?pretty" -H 'Content-Type: application/json' -d'
|
||||||
|
{
|
||||||
|
"size": 10000,
|
||||||
|
"version": true,
|
||||||
|
"query": {
|
||||||
|
"bool": {
|
||||||
|
"filter": [
|
||||||
|
{ "regexp": { "repositoryUrl": "git@github.com:talos-systems/talos-controller-manager" }},
|
||||||
|
{ "regexp": { "filePath": "hack/config.*" }}
|
||||||
|
]
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
'
|
||||||
|
```
|
||||||
|
|
||||||
|
Search all the documents whose filePath ends with one of these following three filenames:
|
||||||
|
`kustomization.yaml`, `kustomization.yml`, `kustomization`:
|
||||||
|
```
|
||||||
|
curl -X GET "${ElasticSearchURL}:9200/kustomize/_search?pretty" -H 'Content-Type: application/json' -d'
|
||||||
|
{
|
||||||
|
"query": {
|
||||||
|
"bool": {
|
||||||
|
"filter": [
|
||||||
|
{ "regexp": { "filePath": ".*/kustomization((.yaml)?|(.yml)?)/*" }}
|
||||||
|
]
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
'
|
||||||
|
```
|
||||||
19
api/internal/crawl/search_cmds/misc.md
Normal file
19
api/internal/crawl/search_cmds/misc.md
Normal file
@@ -0,0 +1,19 @@
|
|||||||
|
Check the health status of an ElasticSearch cluster:
|
||||||
|
```
|
||||||
|
curl -X GET "${ElasticSearchURL}:9200/_cat/health?v&pretty"
|
||||||
|
```
|
||||||
|
|
||||||
|
Check the indices in an ElasticSearch cluster:
|
||||||
|
```
|
||||||
|
curl "${ElasticSearchURL}:9200/_cat/indices?v"
|
||||||
|
```
|
||||||
|
|
||||||
|
Get the mapping of the index:
|
||||||
|
```
|
||||||
|
curl -X GET "${ElasticSearchURL}:9200/kustomize/_mapping?pretty"
|
||||||
|
```
|
||||||
|
|
||||||
|
Delete the kustomize index from the ElasticSearch cluster (**Use this command with caution**):
|
||||||
|
```
|
||||||
|
curl -X DELETE "${ElasticSearchURL}:9200/kustomize?pretty"
|
||||||
|
```
|
||||||
125
api/internal/crawl/search_cmds/repositoryUrl.md
Normal file
125
api/internal/crawl/search_cmds/repositoryUrl.md
Normal file
@@ -0,0 +1,125 @@
|
|||||||
|
Count distinct values of the `repositoryUrl` field:
|
||||||
|
```
|
||||||
|
curl -X POST "${ElasticSearchURL}:9200/kustomize/_search?size=0&pretty" -H 'Content-Type: application/json' -d'
|
||||||
|
{
|
||||||
|
"aggs" : {
|
||||||
|
"repositoryUrl_count" : {
|
||||||
|
"cardinality" : {
|
||||||
|
"field" : "repositoryUrl",
|
||||||
|
"precision_threshold": 40000
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
'
|
||||||
|
```
|
||||||
|
|
||||||
|
Count how many Github repositories include kustomization files:
|
||||||
|
```
|
||||||
|
curl -X POST "${ElasticSearchURL}:9200/kustomize/_search?size=0&pretty" -H 'Content-Type: application/json' -d'
|
||||||
|
{
|
||||||
|
"query": {
|
||||||
|
"bool": {
|
||||||
|
"filter": [
|
||||||
|
{ "regexp": { "filePath": ".*/kustomization((.yaml)?|(.yml)?)/*" }}
|
||||||
|
]
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"aggs" : {
|
||||||
|
"repositoryUrl_count" : {
|
||||||
|
"cardinality" : {
|
||||||
|
"field" : "repositoryUrl",
|
||||||
|
"precision_threshold": 40000
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
'
|
||||||
|
```
|
||||||
|
|
||||||
|
Count how many Github repositories include kustomize resource files:
|
||||||
|
```
|
||||||
|
curl -X POST "${ElasticSearchURL}:9200/kustomize/_search?size=0&pretty" -H 'Content-Type: application/json' -d'
|
||||||
|
{
|
||||||
|
"query": {
|
||||||
|
"bool": {
|
||||||
|
"must_not": {
|
||||||
|
"regexp": { "filePath": ".*/kustomization((.yaml)?|(.yml)?)/*" }
|
||||||
|
}
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"aggs" : {
|
||||||
|
"repositoryUrl_count" : {
|
||||||
|
"cardinality" : {
|
||||||
|
"field" : "repositoryUrl",
|
||||||
|
"precision_threshold": 40000
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
'
|
||||||
|
```
|
||||||
|
|
||||||
|
List all the github repositories including kustomization files and kustomize resource files,
|
||||||
|
and how many kustomization files and kustomize resource files each github repository includes
|
||||||
|
(the github repository including the most kustomization files is listed first):
|
||||||
|
```
|
||||||
|
curl -X GET "${ElasticSearchURL}:9200/kustomize/_search?size=0&pretty" -H 'Content-Type: application/json' -d'
|
||||||
|
{
|
||||||
|
"aggs" : {
|
||||||
|
"repositoryUrl" : {
|
||||||
|
"terms" : {
|
||||||
|
"field" : "repositoryUrl",
|
||||||
|
"size": 2082
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
'
|
||||||
|
```
|
||||||
|
|
||||||
|
List the top 20 Github repositories including the most amount of kustomization files:
|
||||||
|
```
|
||||||
|
curl -X GET "${ElasticSearchURL}:9200/kustomize/_search?size=0&pretty" -H 'Content-Type: application/json' -d'
|
||||||
|
{
|
||||||
|
"query": {
|
||||||
|
"bool": {
|
||||||
|
"filter": [
|
||||||
|
{ "regexp": { "filePath": ".*/kustomization((.yaml)?|(.yml)?)/*" }}
|
||||||
|
]
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"aggs" : {
|
||||||
|
"repositoryUrl" : {
|
||||||
|
"terms" : {
|
||||||
|
"field" : "repositoryUrl",
|
||||||
|
"size": 20
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
'
|
||||||
|
```
|
||||||
|
|
||||||
|
List the top 20 Github repositories including the most amount of kustomize resource files:
|
||||||
|
```
|
||||||
|
curl -X GET "${ElasticSearchURL}:9200/kustomize/_search?size=0&pretty" -H 'Content-Type: application/json' -d'
|
||||||
|
{
|
||||||
|
"query": {
|
||||||
|
"bool": {
|
||||||
|
"must_not": {
|
||||||
|
"regexp": { "filePath": ".*/kustomization((.yaml)?|(.yml)?)/*" }
|
||||||
|
}
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"aggs" : {
|
||||||
|
"repositoryUrl" : {
|
||||||
|
"terms" : {
|
||||||
|
"field" : "repositoryUrl",
|
||||||
|
"size": 20
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
'
|
||||||
|
```
|
||||||
148
api/internal/crawl/search_cmds/text_search.md
Normal file
148
api/internal/crawl/search_cmds/text_search.md
Normal file
@@ -0,0 +1,148 @@
|
|||||||
|
Search for all the kustomize resource files including a Deployment object:
|
||||||
|
```
|
||||||
|
curl -X GET "${ElasticSearchURL}:9200/kustomize/_search?pretty" -H 'Content-Type: application/json' -d'
|
||||||
|
{
|
||||||
|
"query": {
|
||||||
|
"match" : {
|
||||||
|
"kinds" : {
|
||||||
|
"query" : "Deployment"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
'
|
||||||
|
```
|
||||||
|
|
||||||
|
Search for all the kustomize resource files including a Deployment object, but only
|
||||||
|
including the `kinds` field in the result:
|
||||||
|
```
|
||||||
|
curl -X GET "${ElasticSearchURL}:9200/kustomize/_search?pretty" -H 'Content-Type: application/json' -d'
|
||||||
|
{
|
||||||
|
"_source": {
|
||||||
|
"includes": ["kinds"]
|
||||||
|
},
|
||||||
|
"query": {
|
||||||
|
"match" : {
|
||||||
|
"kinds" : {
|
||||||
|
"query" : "Deployment"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
'
|
||||||
|
```
|
||||||
|
|
||||||
|
Search for all the kustomize resource files including both a Deployment object and
|
||||||
|
a Service object:
|
||||||
|
```
|
||||||
|
curl -X GET "${ElasticSearchURL}:9200/kustomize/_search?pretty" -H 'Content-Type: application/json' -d'
|
||||||
|
{
|
||||||
|
"query": {
|
||||||
|
"match" : {
|
||||||
|
"kinds" : {
|
||||||
|
"query" : "Deployment Service",
|
||||||
|
"operator" : "and"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
'
|
||||||
|
```
|
||||||
|
|
||||||
|
Count the number of documents including Deployment and the number of documents
|
||||||
|
including Service:
|
||||||
|
```
|
||||||
|
curl -X GET "${ElasticSearchURL}:9200/kustomize/_search?pretty" -H 'Content-Type: application/json' -d'
|
||||||
|
{
|
||||||
|
"size": 0,
|
||||||
|
"aggs" : {
|
||||||
|
"messages" : {
|
||||||
|
"filters" : {
|
||||||
|
"filters" : {
|
||||||
|
"Deployment" : { "match" : { "kinds" : "Deployment" }},
|
||||||
|
"Service" : { "match" : { "kinds" : "Service" }}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
'
|
||||||
|
```
|
||||||
|
|
||||||
|
Search for all the kustomization files involving CRDs:
|
||||||
|
```
|
||||||
|
curl -X GET "${ElasticSearchURL}:9200/kustomize/_search?pretty" -H 'Content-Type: application/json' -d'
|
||||||
|
{
|
||||||
|
"size": 10000,
|
||||||
|
"query": {
|
||||||
|
"match" : {
|
||||||
|
"identifiers" : {
|
||||||
|
"query" : "crds"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
'
|
||||||
|
```
|
||||||
|
|
||||||
|
Search for all the kustomization files defining configMapGenerator:
|
||||||
|
```
|
||||||
|
curl -X GET "${ElasticSearchURL}:9200/kustomize/_search?pretty" -H 'Content-Type: application/json' -d'
|
||||||
|
{
|
||||||
|
"size": 10000,
|
||||||
|
"query": {
|
||||||
|
"match" : {
|
||||||
|
"identifiers" : {
|
||||||
|
"query" : "configMapGenerator"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
'
|
||||||
|
```
|
||||||
|
|
||||||
|
Search for all the documents having a `kind` field:
|
||||||
|
```
|
||||||
|
curl -X GET "${ElasticSearchURL}:9200/kustomize/_search?pretty" -H 'Content-Type: application/json' -d'
|
||||||
|
{
|
||||||
|
"query": {
|
||||||
|
"bool": {
|
||||||
|
"filter": [
|
||||||
|
{ "match" : { "identifiers" : { "query" : "kind" }}}
|
||||||
|
]
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
'
|
||||||
|
```
|
||||||
|
|
||||||
|
Search for all the kuostmization files having a `kind` field:
|
||||||
|
```
|
||||||
|
curl -X GET "${ElasticSearchURL}:9200/kustomize/_search?pretty" -H 'Content-Type: application/json' -d'
|
||||||
|
{
|
||||||
|
"query": {
|
||||||
|
"bool": {
|
||||||
|
"filter": [
|
||||||
|
{ "regexp": { "filePath": ".*/kustomization((.yaml)?|(.yml)?)" }},
|
||||||
|
{ "match" : { "identifiers" : { "query" : "kind" }}}
|
||||||
|
]
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
'
|
||||||
|
```
|
||||||
|
|
||||||
|
Search for all the kustomization files defining the `generatorOptions:disableNameSuffixHash` feature:
|
||||||
|
```
|
||||||
|
curl -X GET "${ElasticSearchURL}:9200/kustomize/_search?pretty" -H 'Content-Type: application/json' -d'
|
||||||
|
{
|
||||||
|
"query": {
|
||||||
|
"match" : {
|
||||||
|
"identifiers" : {
|
||||||
|
"query" : "generatorOptions:disableNameSuffixHash"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
'
|
||||||
|
```
|
||||||
@@ -153,9 +153,9 @@ FRUIT=banana
|
|||||||
LEGUME=chickpea
|
LEGUME=chickpea
|
||||||
`)
|
`)
|
||||||
th.WriteF("/app/overlay/configmap/dummy.txt",
|
th.WriteF("/app/overlay/configmap/dummy.txt",
|
||||||
`Lorem ipsum dolor sit amet, consectetur
|
`Lorem ipsum dolor sit amet, consectetur
|
||||||
adipiscing elit, sed do eiusmod tempor
|
adipiscing elit, sed do eiusmod tempor
|
||||||
incididunt ut labore et dolore magna aliqua.
|
incididunt ut labore et dolore magna aliqua.
|
||||||
`)
|
`)
|
||||||
th.WriteF("/app/overlay/deployment/deployment.yaml", `
|
th.WriteF("/app/overlay/deployment/deployment.yaml", `
|
||||||
apiVersion: apps/v1
|
apiVersion: apps/v1
|
||||||
@@ -292,8 +292,10 @@ metadata:
|
|||||||
---
|
---
|
||||||
apiVersion: v1
|
apiVersion: v1
|
||||||
data:
|
data:
|
||||||
nonsense: "Lorem ipsum dolor sit amet, consectetur\nadipiscing elit, sed do eiusmod
|
nonsense: |
|
||||||
tempor\nincididunt ut labore et dolore magna aliqua. \n"
|
Lorem ipsum dolor sit amet, consectetur
|
||||||
|
adipiscing elit, sed do eiusmod tempor
|
||||||
|
incididunt ut labore et dolore magna aliqua.
|
||||||
kind: ConfigMap
|
kind: ConfigMap
|
||||||
metadata:
|
metadata:
|
||||||
annotations:
|
annotations:
|
||||||
@@ -302,6 +304,6 @@ metadata:
|
|||||||
app: mungebot
|
app: mungebot
|
||||||
org: kubernetes
|
org: kubernetes
|
||||||
repo: test-infra
|
repo: test-infra
|
||||||
name: test-infra-app-config-f462h769f9
|
name: test-infra-app-config-4mt28b5bg2
|
||||||
`)
|
`)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -9,6 +9,7 @@ import (
|
|||||||
"fmt"
|
"fmt"
|
||||||
"os"
|
"os"
|
||||||
"path"
|
"path"
|
||||||
|
"regexp"
|
||||||
"strings"
|
"strings"
|
||||||
"unicode"
|
"unicode"
|
||||||
"unicode/utf8"
|
"unicode/utf8"
|
||||||
@@ -85,11 +86,17 @@ func (kvl *loader) keyValuesFromFileSources(sources []string) ([]types.Pair, err
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
kvs = append(kvs, types.Pair{Key: k, Value: string(content)})
|
kvs = append(kvs, types.Pair{Key: k, Value: trimTrailingSpacesInLines(string(content))})
|
||||||
}
|
}
|
||||||
return kvs, nil
|
return kvs, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// trimTrailingSpacesInLines takes string with multiple lines and trims the trailing white spaces and tabs from each line.
|
||||||
|
func trimTrailingSpacesInLines(str string) string {
|
||||||
|
re := regexp.MustCompile(`\s*\n`)
|
||||||
|
return re.ReplaceAllString(str, "\n")
|
||||||
|
}
|
||||||
|
|
||||||
func (kvl *loader) keyValuesFromEnvFiles(paths []string) ([]types.Pair, error) {
|
func (kvl *loader) keyValuesFromEnvFiles(paths []string) ([]types.Pair, error) {
|
||||||
var kvs []types.Pair
|
var kvs []types.Pair
|
||||||
for _, p := range paths {
|
for _, p := range paths {
|
||||||
|
|||||||
@@ -95,3 +95,12 @@ func TestKeyValuesFromFileSources(t *testing.T) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestTrimTrailingSpacesInLines(t *testing.T) {
|
||||||
|
input := "\"fooKey\": \"fooValue\" \t\n\t\"barKey\": \"barValue\""
|
||||||
|
expected := "\"fooKey\": \"fooValue\"\n\t\"barKey\": \"barValue\""
|
||||||
|
res := trimTrailingSpacesInLines(input)
|
||||||
|
if !reflect.DeepEqual(res, expected) {
|
||||||
|
t.Errorf("Trim trailing spaces in lines should succeed, got: %s exptected: %s", res, expected)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
@@ -78,7 +78,10 @@ func NewConfigCommand(name string) *cobra.Command {
|
|||||||
root.AddCommand(commands.CountCommand(name))
|
root.AddCommand(commands.CountCommand(name))
|
||||||
root.AddCommand(commands.RunFnCommand(name))
|
root.AddCommand(commands.RunFnCommand(name))
|
||||||
root.AddCommand(commands.SetCommand(name))
|
root.AddCommand(commands.SetCommand(name))
|
||||||
|
root.AddCommand(commands.ListSettersCommand(name))
|
||||||
root.AddCommand(commands.CreateSetterCommand(name))
|
root.AddCommand(commands.CreateSetterCommand(name))
|
||||||
|
root.AddCommand(commands.SinkCommand(name))
|
||||||
|
root.AddCommand(commands.SourceCommand(name))
|
||||||
|
|
||||||
root.AddCommand(&cobra.Command{
|
root.AddCommand(&cobra.Command{
|
||||||
Use: "docs-merge",
|
Use: "docs-merge",
|
||||||
@@ -92,8 +95,13 @@ func NewConfigCommand(name string) *cobra.Command {
|
|||||||
})
|
})
|
||||||
root.AddCommand(&cobra.Command{
|
root.AddCommand(&cobra.Command{
|
||||||
Use: "docs-fn",
|
Use: "docs-fn",
|
||||||
Short: "[Alpha] Documentation for writing containerized functions run by run.",
|
Short: "[Alpha] Documentation for developing and invoking Configuration Functions.",
|
||||||
Long: api.ConfigFnLong,
|
Long: api.FunctionsImplLong,
|
||||||
|
})
|
||||||
|
root.AddCommand(&cobra.Command{
|
||||||
|
Use: "docs-fn-spec",
|
||||||
|
Short: "[Alpha] Documentation for Configuration Functions Specification.",
|
||||||
|
Long: api.FunctionsSpecLong,
|
||||||
})
|
})
|
||||||
root.AddCommand(&cobra.Command{
|
root.AddCommand(&cobra.Command{
|
||||||
Use: "docs-io-annotations",
|
Use: "docs-io-annotations",
|
||||||
|
|||||||
@@ -1,281 +0,0 @@
|
|||||||
# Configuration Functions API Semantics
|
|
||||||
|
|
||||||
Configuration Functions are functions packaged as executables in containers which enable
|
|
||||||
**shift-left practices**. They configure applications and infrastructure through
|
|
||||||
Kubernetes style Resource Configuration, but run locally pre-commit.
|
|
||||||
|
|
||||||
Configuration functions enable shift-left practices (client-side) through:
|
|
||||||
|
|
||||||
- Pre-commit / delivery validation and linting of configuration
|
|
||||||
- e.g. Fail if any containers don't have PodSecurityPolicy or CPU / Memory limits
|
|
||||||
- Implementation of abstractions as client actuated APIs (e.g. templating)
|
|
||||||
- e.g. Create a client-side *"CRD"* for generating configuration checked into git
|
|
||||||
- Aspect Orient configuration / Injection of cross-cutting configuration
|
|
||||||
- e.g. T-Shirt size containers by annotating Resources with `small`, `medium`, `large`
|
|
||||||
and inject the cpu and memory resources into containers accordingly.
|
|
||||||
- e.g. Inject `init` and `side-car` containers into Resources based off of Resource
|
|
||||||
Type, annotations, etc.
|
|
||||||
|
|
||||||
Performing these on the client rather than the server enables:
|
|
||||||
|
|
||||||
- Configuration to be reviewed prior to being sent to the API server
|
|
||||||
- Configuration to be validated as part of the CD pipeline
|
|
||||||
- Configuration for Resources to validated holistically rather than individually
|
|
||||||
per-Resource -- e.g. ensure the `Service.selector` and `Deployment.spec.template` labels
|
|
||||||
match.
|
|
||||||
- MutatingWebHooks are scoped to a single Resource instance at a time.
|
|
||||||
- Low-level tweaks to the output of high-level abstractions -- e.g. add an `init container`
|
|
||||||
to a client *"CRD"* Resource after it was generated.
|
|
||||||
- Composition and layering of multiple functions together
|
|
||||||
- Compose generation, injection, validation together
|
|
||||||
|
|
||||||
Configuration Functions are implemented as executable programs published in containers which:
|
|
||||||
|
|
||||||
- Accept as input (stdin):
|
|
||||||
- A list of Resource Configuration
|
|
||||||
- A Function Configuration (to configure the function itself)
|
|
||||||
- Emit as output (stdout + exit):
|
|
||||||
- A list of Resource Configuration
|
|
||||||
- An exit code for success / failure
|
|
||||||
|
|
||||||
### Function Specification
|
|
||||||
|
|
||||||
- Functions **SHOULD** be published as container images containing a `CMD` invoking an executable.
|
|
||||||
- Functions **MUST** accept input on STDIN a `ResourceList` containing the Resources and
|
|
||||||
`functionConfig`.
|
|
||||||
- Functions **MUST** emit output on STDOUT a `ResourceList` containing the modified
|
|
||||||
Resources.
|
|
||||||
- Functions **MUST** exit non-0 on failure, and exit 0 on success.
|
|
||||||
- Functions **MAY** emit output on STDERR with error messaging.
|
|
||||||
- Functions performing validation **SHOULD** exit failure and emit error messaging
|
|
||||||
on a validation failure.
|
|
||||||
- Functions generating Resources **SHOULD** retain non-conflicting changes on the
|
|
||||||
generated Resources -- e.g. 1. the function generates a Deployment, but doesn't
|
|
||||||
specify `cpu`, 2. the user sets the `cpu` on the generated Resource, 3. the
|
|
||||||
function should keep the `cpu` when regenerating the Resource a second time.
|
|
||||||
- Functions **SHOULD** be usable outside `kustomize config run` -- e.g. though pipeline
|
|
||||||
mechanisms such as Tekton.
|
|
||||||
|
|
||||||
#### Input Format
|
|
||||||
|
|
||||||
Functions must accept on STDIN:
|
|
||||||
|
|
||||||
`ResourceList`:
|
|
||||||
- contains `items` field, same as `List.items`
|
|
||||||
- contains `functionConfig` field -- a single item with the configuration for the function itself
|
|
||||||
|
|
||||||
Example `ResourceList` Input:
|
|
||||||
|
|
||||||
apiVersion: config.kubernetes.io/v1alpha1
|
|
||||||
kind: ResourceList
|
|
||||||
functionConfig:
|
|
||||||
apiVersion: example.com/v1beta1
|
|
||||||
kind: Nginx
|
|
||||||
metadata:
|
|
||||||
name: my-instance
|
|
||||||
annotations:
|
|
||||||
config.kubernetes.io/local-config: "true"
|
|
||||||
spec:
|
|
||||||
replicas: 5
|
|
||||||
items:
|
|
||||||
- apiVersion: apps/v1
|
|
||||||
kind: Deployment
|
|
||||||
metadata:
|
|
||||||
name: my-instance
|
|
||||||
spec:
|
|
||||||
replicas: 3
|
|
||||||
...
|
|
||||||
- apiVersion: v1
|
|
||||||
kind: Service
|
|
||||||
metadata:
|
|
||||||
name: my-instance
|
|
||||||
spec:
|
|
||||||
...
|
|
||||||
|
|
||||||
#### Output Format
|
|
||||||
|
|
||||||
Functions must emit on STDOUT:
|
|
||||||
|
|
||||||
`ResourceList`:
|
|
||||||
- contains `items` field, same as `List.items`
|
|
||||||
|
|
||||||
Example `ResourceList` Output:
|
|
||||||
|
|
||||||
apiVersion: config.kubernetes.io/v1alpha1
|
|
||||||
kind: ResourceList
|
|
||||||
items:
|
|
||||||
- apiVersion: apps/v1
|
|
||||||
kind: Deployment
|
|
||||||
metadata:
|
|
||||||
name: my-instance
|
|
||||||
spec:
|
|
||||||
replicas: 5
|
|
||||||
...
|
|
||||||
- apiVersion: v1
|
|
||||||
kind: Service
|
|
||||||
metadata:
|
|
||||||
name: my-instance
|
|
||||||
spec:
|
|
||||||
...
|
|
||||||
|
|
||||||
#### Container Environment
|
|
||||||
|
|
||||||
When run by `kustomize config run`, functions are run in containers with the
|
|
||||||
following environment:
|
|
||||||
|
|
||||||
- Network: `none`
|
|
||||||
- User: `nobody`
|
|
||||||
- Security Options: `no-new-privileges`
|
|
||||||
- Volumes: the volume containing the `functionConfig` yaml is mounted under `/local` as `ro`
|
|
||||||
|
|
||||||
### Example Function Implementation
|
|
||||||
|
|
||||||
Following is an example for implementing an nginx abstraction using a config
|
|
||||||
function.
|
|
||||||
|
|
||||||
#### `nginx-template.sh`
|
|
||||||
|
|
||||||
`nginx-template.sh` is a simple bash script which uses a *heredoc* as a templating solution
|
|
||||||
for generating Resources from the functionConfig input fields.
|
|
||||||
|
|
||||||
The script wraps itself using `config run wrap -- $0` which will:
|
|
||||||
|
|
||||||
1. Parse the `ResourceList.functionConfig` (provided to the container stdin) into env vars
|
|
||||||
2. Merge the stdout into the original list of Resources
|
|
||||||
3. Defaults filenames for newly generated Resources (if they are not set as annotations)
|
|
||||||
to `config/NAME_KIND.yaml`
|
|
||||||
4. Format the output
|
|
||||||
|
|
||||||
#!/bin/bash
|
|
||||||
# script must run wrapped by `kustomize config run wrap`
|
|
||||||
# for parsing input the functionConfig into env vars
|
|
||||||
if [ -z ${WRAPPED} ]; then
|
|
||||||
export WRAPPED=true
|
|
||||||
config run wrap -- $0
|
|
||||||
exit $?
|
|
||||||
fi
|
|
||||||
|
|
||||||
cat <<End-of-message
|
|
||||||
apiVersion: v1
|
|
||||||
kind: Service
|
|
||||||
metadata:
|
|
||||||
name: ${NAME}
|
|
||||||
labels:
|
|
||||||
app: nginx
|
|
||||||
instance: ${NAME}
|
|
||||||
spec:
|
|
||||||
ports:
|
|
||||||
- port: 80
|
|
||||||
targetPort: 80
|
|
||||||
name: http
|
|
||||||
selector:
|
|
||||||
app: nginx
|
|
||||||
instance: ${NAME}
|
|
||||||
---
|
|
||||||
apiVersion: apps/v1
|
|
||||||
kind: Deployment
|
|
||||||
metadata:
|
|
||||||
name: ${NAME}
|
|
||||||
labels:
|
|
||||||
app: nginx
|
|
||||||
instance: ${NAME}
|
|
||||||
spec:
|
|
||||||
replicas: ${REPLICAS}
|
|
||||||
selector:
|
|
||||||
matchLabels:
|
|
||||||
app: nginx
|
|
||||||
instance: ${NAME}
|
|
||||||
template:
|
|
||||||
metadata:
|
|
||||||
labels:
|
|
||||||
app: nginx
|
|
||||||
instance: ${NAME}
|
|
||||||
spec:
|
|
||||||
containers:
|
|
||||||
- name: nginx
|
|
||||||
image: nginx:1.7.9
|
|
||||||
ports:
|
|
||||||
- containerPort: 80
|
|
||||||
End-of-message
|
|
||||||
|
|
||||||
#### `Dockerfile`
|
|
||||||
|
|
||||||
`Dockerfile` installs `kustomize config` and copies the script into the container image.
|
|
||||||
|
|
||||||
FROM golang:1.13-stretch
|
|
||||||
RUN go get sigs.k8s.io/kustomize/cmd/config
|
|
||||||
RUN mv /go/bin/config /usr/bin/config
|
|
||||||
COPY nginx-template.sh /usr/bin/nginx-template.sh
|
|
||||||
CMD ["nginx-template.sh]
|
|
||||||
|
|
||||||
### Example Function Usage
|
|
||||||
|
|
||||||
Following is an example of running the `kustomize config run` using the preceding API.
|
|
||||||
|
|
||||||
#### `nginx.yaml` (Input)
|
|
||||||
|
|
||||||
`dir/nginx.yaml` contains a reference to the Function. The contents of `nginx.yaml`
|
|
||||||
are passed to the Function through the `ResourceList.functionConfig` field.
|
|
||||||
|
|
||||||
apiVersion: example.com/v1beta1
|
|
||||||
kind: Nginx
|
|
||||||
metadata:
|
|
||||||
name: my-instance
|
|
||||||
annotations:
|
|
||||||
config.kubernetes.io/local-config: "true"
|
|
||||||
configFn:
|
|
||||||
container:
|
|
||||||
image: gcr.io/example-functions/nginx-template:v1.0.0
|
|
||||||
spec:
|
|
||||||
replicas: 5
|
|
||||||
|
|
||||||
- `configFn.container.image`: the image to use for this API
|
|
||||||
- `annotations[config.kubernetes.io/local-config]`: mark this as not a Resource that should
|
|
||||||
be applied
|
|
||||||
|
|
||||||
#### `kustomize config run dir/` (Output)
|
|
||||||
|
|
||||||
`dir/my-instance_deployment.yaml` contains the Deployment:
|
|
||||||
|
|
||||||
apiVersion: apps/v1
|
|
||||||
kind: Deployment
|
|
||||||
metadata:
|
|
||||||
name: my-instance
|
|
||||||
labels:
|
|
||||||
app: nginx
|
|
||||||
instance: my-instance
|
|
||||||
spec:
|
|
||||||
replicas: 5
|
|
||||||
selector:
|
|
||||||
matchLabels:
|
|
||||||
app: nginx
|
|
||||||
instance: my-instance
|
|
||||||
template:
|
|
||||||
metadata:
|
|
||||||
labels:
|
|
||||||
app: nginx
|
|
||||||
instance: my-instance
|
|
||||||
spec:
|
|
||||||
containers:
|
|
||||||
- name: nginx
|
|
||||||
image: nginx:1.7.9
|
|
||||||
ports:
|
|
||||||
- containerPort: 80
|
|
||||||
|
|
||||||
`dir/my-instance_service.yaml` contains the Service:
|
|
||||||
|
|
||||||
apiVersion: v1
|
|
||||||
kind: Service
|
|
||||||
metadata:
|
|
||||||
name: my-instance
|
|
||||||
labels:
|
|
||||||
app: nginx
|
|
||||||
instance: my-instance
|
|
||||||
spec:
|
|
||||||
ports:
|
|
||||||
- port: 80
|
|
||||||
targetPort: 80
|
|
||||||
name: http
|
|
||||||
selector:
|
|
||||||
app: nginx
|
|
||||||
instance: my-instance
|
|
||||||
181
cmd/config/docs/api-conventions/functions-impl.md
Normal file
181
cmd/config/docs/api-conventions/functions-impl.md
Normal file
@@ -0,0 +1,181 @@
|
|||||||
|
# Running Configuration Functions using kustomize CLI
|
||||||
|
|
||||||
|
Configuration functions can be implemented using any toolchain and invoked using any
|
||||||
|
container workflow orchestrator including Tekton, Cloud Build, or run directly using `docker run`.
|
||||||
|
|
||||||
|
Run `config help docs-fn-spec` to see the Configuration Functions Specification.
|
||||||
|
|
||||||
|
`kustomize config run` is an example orchestrator for invoking Configuration Functions. This
|
||||||
|
document describes how to implement and invoke an example function.
|
||||||
|
|
||||||
|
## Example Function Implementation
|
||||||
|
|
||||||
|
Following is an example for implementing an nginx abstraction using a configuration
|
||||||
|
function.
|
||||||
|
|
||||||
|
### `nginx-template.sh`
|
||||||
|
|
||||||
|
`nginx-template.sh` is a simple bash script which uses a _heredoc_ as a templating solution
|
||||||
|
for generating Resources from the functionConfig input fields.
|
||||||
|
|
||||||
|
The script wraps itself using `config run wrap -- $0` which will:
|
||||||
|
|
||||||
|
1. Parse the `ResourceList.functionConfig` (provided to the container stdin) into env vars
|
||||||
|
2. Merge the stdout into the original list of Resources
|
||||||
|
3. Defaults filenames for newly generated Resources (if they are not set as annotations)
|
||||||
|
to `config/NAME_KIND.yaml`
|
||||||
|
4. Format the output
|
||||||
|
|
||||||
|
```bash
|
||||||
|
#!/bin/bash
|
||||||
|
# script must run wrapped by "kustomize config run wrap"
|
||||||
|
# for parsing input the functionConfig into env vars
|
||||||
|
if [ -z ${WRAPPED} ]; then
|
||||||
|
export WRAPPED=true
|
||||||
|
config run wrap -- $0
|
||||||
|
exit $?
|
||||||
|
fi
|
||||||
|
|
||||||
|
cat <<End-of-message
|
||||||
|
apiVersion: v1
|
||||||
|
kind: Service
|
||||||
|
metadata:
|
||||||
|
name: ${NAME}
|
||||||
|
labels:
|
||||||
|
app: nginx
|
||||||
|
instance: ${NAME}
|
||||||
|
spec:
|
||||||
|
ports:
|
||||||
|
- port: 80
|
||||||
|
targetPort: 80
|
||||||
|
name: http
|
||||||
|
selector:
|
||||||
|
app: nginx
|
||||||
|
instance: ${NAME}
|
||||||
|
---
|
||||||
|
apiVersion: apps/v1
|
||||||
|
kind: Deployment
|
||||||
|
metadata:
|
||||||
|
name: ${NAME}
|
||||||
|
labels:
|
||||||
|
app: nginx
|
||||||
|
instance: ${NAME}
|
||||||
|
spec:
|
||||||
|
replicas: ${REPLICAS}
|
||||||
|
selector:
|
||||||
|
matchLabels:
|
||||||
|
app: nginx
|
||||||
|
instance: ${NAME}
|
||||||
|
template:
|
||||||
|
metadata:
|
||||||
|
labels:
|
||||||
|
app: nginx
|
||||||
|
instance: ${NAME}
|
||||||
|
spec:
|
||||||
|
containers:
|
||||||
|
- name: nginx
|
||||||
|
image: nginx:1.7.9
|
||||||
|
ports:
|
||||||
|
- containerPort: 80
|
||||||
|
End-of-message
|
||||||
|
```
|
||||||
|
|
||||||
|
### Dockerfile
|
||||||
|
|
||||||
|
`Dockerfile` installs `kustomize config` and copies the script into the container image.
|
||||||
|
|
||||||
|
```
|
||||||
|
FROM golang:1.13-stretch
|
||||||
|
RUN go get sigs.k8s.io/kustomize/cmd/config
|
||||||
|
RUN mv /go/bin/config /usr/bin/config
|
||||||
|
COPY nginx-template.sh /usr/bin/nginx-template.sh
|
||||||
|
CMD ["nginx-template.sh]
|
||||||
|
```
|
||||||
|
|
||||||
|
## Example Function Usage
|
||||||
|
|
||||||
|
Following is an example of running the `kustomize config run` using the preceding API.
|
||||||
|
|
||||||
|
When run by `kustomize config run`, functions are run in containers with the
|
||||||
|
following environment:
|
||||||
|
|
||||||
|
- Network: `none`
|
||||||
|
- User: `nobody`
|
||||||
|
- Security Options: `no-new-privileges`
|
||||||
|
- Volumes: the volume containing the `functionConfig` yaml is mounted under `/local` as `ro`
|
||||||
|
|
||||||
|
### Input
|
||||||
|
|
||||||
|
`dir/nginx.yaml` contains a reference to the Function. The contents of `nginx.yaml`
|
||||||
|
are passed to the Function through the `ResourceList.functionConfig` field.
|
||||||
|
|
||||||
|
```yaml
|
||||||
|
apiVersion: example.com/v1beta1
|
||||||
|
kind: Nginx
|
||||||
|
metadata:
|
||||||
|
name: my-instance
|
||||||
|
annotations:
|
||||||
|
config.kubernetes.io/local-config: "true"
|
||||||
|
config.k8s.io/function: |
|
||||||
|
container:
|
||||||
|
image: gcr.io/example-functions/nginx-template:v1.0.0
|
||||||
|
spec:
|
||||||
|
replicas: 5
|
||||||
|
```
|
||||||
|
|
||||||
|
- `annotations[config.k8s.io/function].container.image`: the image to use for this API
|
||||||
|
- `annotations[config.kubernetes.io/local-config]`: mark this as not a Resource that should
|
||||||
|
be applied
|
||||||
|
|
||||||
|
### Output
|
||||||
|
|
||||||
|
The function is invoked using by runing `kustomize config run dir/`.
|
||||||
|
|
||||||
|
`dir/my-instance_deployment.yaml` contains the Deployment:
|
||||||
|
|
||||||
|
```yaml
|
||||||
|
apiVersion: apps/v1
|
||||||
|
kind: Deployment
|
||||||
|
metadata:
|
||||||
|
name: my-instance
|
||||||
|
labels:
|
||||||
|
app: nginx
|
||||||
|
instance: my-instance
|
||||||
|
spec:
|
||||||
|
replicas: 5
|
||||||
|
selector:
|
||||||
|
matchLabels:
|
||||||
|
app: nginx
|
||||||
|
instance: my-instance
|
||||||
|
template:
|
||||||
|
metadata:
|
||||||
|
labels:
|
||||||
|
app: nginx
|
||||||
|
instance: my-instance
|
||||||
|
spec:
|
||||||
|
containers:
|
||||||
|
- name: nginx
|
||||||
|
image: nginx:1.7.9
|
||||||
|
ports:
|
||||||
|
- containerPort: 80
|
||||||
|
```
|
||||||
|
|
||||||
|
`dir/my-instance_service.yaml` contains the Service:
|
||||||
|
|
||||||
|
```yaml
|
||||||
|
apiVersion: v1
|
||||||
|
kind: Service
|
||||||
|
metadata:
|
||||||
|
name: my-instance
|
||||||
|
labels:
|
||||||
|
app: nginx
|
||||||
|
instance: my-instance
|
||||||
|
spec:
|
||||||
|
ports:
|
||||||
|
- port: 80
|
||||||
|
targetPort: 80
|
||||||
|
name: http
|
||||||
|
selector:
|
||||||
|
app: nginx
|
||||||
|
instance: my-instance
|
||||||
|
```
|
||||||
186
cmd/config/docs/api-conventions/functions-spec.md
Normal file
186
cmd/config/docs/api-conventions/functions-spec.md
Normal file
@@ -0,0 +1,186 @@
|
|||||||
|
# Configuration Functions Specification
|
||||||
|
|
||||||
|
This document specifies a standard for client-side functions that operate on
|
||||||
|
Kubernetes declarative configurations. This standard enables creating
|
||||||
|
small, interoperable, and language-independent executable programs packaged as
|
||||||
|
containers that can be chained together as part of a configuration management pipeline.
|
||||||
|
The end result of such a pipeline are fully rendered configurations that can then be
|
||||||
|
applied to a control plane (e.g. Using ‘kubectl apply’ for Kubernetes control plane).
|
||||||
|
As such, although this document references Kubernetes Resource Model and API conventions,
|
||||||
|
it is completely decoupled from Kuberentes API machinery and does not depend on any
|
||||||
|
in-cluster components.
|
||||||
|
|
||||||
|
This document references terms described in [Kubernetes API Conventions][1].
|
||||||
|
|
||||||
|
The key words "MUST", "MUST NOT", "REQUIRED", "SHALL", "SHALL NOT", "SHOULD",
|
||||||
|
"SHOULD NOT", "RECOMMENDED", "MAY", and "OPTIONAL" in this document are to be
|
||||||
|
interpreted as described in [RFC 2119][2].
|
||||||
|
|
||||||
|
## Use Cases
|
||||||
|
|
||||||
|
_Configuration functions_ enable shift-left practices (client-side) through:
|
||||||
|
|
||||||
|
- Pre-commit / delivery validation and linting of configuration
|
||||||
|
- e.g. Fail if any containers don't have PodSecurityPolicy or CPU / Memory limits
|
||||||
|
- Implementation of abstractions as client actuated APIs (e.g. templating)
|
||||||
|
- e.g. Create a client-side _"CRD"_ for generating configuration checked into git
|
||||||
|
- Aspect Orient configuration / Injection of cross-cutting configuration
|
||||||
|
- e.g. T-Shirt size containers by annotating Resources with `small`, `medium`, `large`
|
||||||
|
and inject the cpu and memory resources into containers accordingly.
|
||||||
|
- e.g. Inject `init` and `side-car` containers into Resources based off of Resource
|
||||||
|
Type, annotations, etc.
|
||||||
|
|
||||||
|
Performing these on the client rather than the server enables:
|
||||||
|
|
||||||
|
- Configuration to be reviewed prior to being sent to the API server
|
||||||
|
- Configuration to be validated as part of the CI?CD pipeline
|
||||||
|
- Configuration for Resources to validated holistically rather than individually
|
||||||
|
per-Resource
|
||||||
|
- e.g. ensure the `Service.selector` and `Deployment.spec.template` labels
|
||||||
|
match.
|
||||||
|
- e.g. MutatingWebHooks are scoped to a single Resource instance at a time.
|
||||||
|
- Low-level tweaks to the output of high-level abstractions
|
||||||
|
- e.g. add an `init container` to a client _"CRD"_ Resource after it was generated.
|
||||||
|
- Composition and layering of multiple functions together
|
||||||
|
- Compose generation, injection, validation together
|
||||||
|
|
||||||
|
## Spec
|
||||||
|
|
||||||
|
### Input Type
|
||||||
|
|
||||||
|
A function MUST accept as input a single [Kubernetes List type][3].
|
||||||
|
The `items` field in the input will contain a sequence of [Object types][3].
|
||||||
|
A function MAY not support [Simple types][3] and List types.
|
||||||
|
|
||||||
|
An example using `v1/ConfigMapList` as input:
|
||||||
|
|
||||||
|
```yaml
|
||||||
|
apiVersion: v1
|
||||||
|
kind: ConfigMapList
|
||||||
|
items:
|
||||||
|
- apiVersion: v1
|
||||||
|
kind: ConfigMap
|
||||||
|
metadata:
|
||||||
|
name: config1
|
||||||
|
data:
|
||||||
|
p1: v1
|
||||||
|
p2: v2
|
||||||
|
- apiVersion: v1
|
||||||
|
kind: ConfigMap
|
||||||
|
metadata:
|
||||||
|
name: config2
|
||||||
|
```
|
||||||
|
|
||||||
|
An example using `v1/List` as input:
|
||||||
|
|
||||||
|
```yaml
|
||||||
|
apiVersion: v1
|
||||||
|
kind: List
|
||||||
|
items:
|
||||||
|
spec:
|
||||||
|
- apiVersion: foo-corp.com/v1
|
||||||
|
kind: FulfillmentCenter
|
||||||
|
metadata:
|
||||||
|
name: staging
|
||||||
|
address: "100 Main St."
|
||||||
|
- apiVersion: rbac.authorization.k8s.io/v1
|
||||||
|
kind: ClusterRole
|
||||||
|
metadata:
|
||||||
|
name: namespace-reader
|
||||||
|
rules:
|
||||||
|
- resources:
|
||||||
|
- namespaces
|
||||||
|
apiGroups:
|
||||||
|
- ""
|
||||||
|
verbs:
|
||||||
|
- get
|
||||||
|
- watch
|
||||||
|
- list
|
||||||
|
```
|
||||||
|
|
||||||
|
In addition, a function MUST accept as input a List of kind `ResourceList` where the
|
||||||
|
`functionConfig` field, if present, will contain the invocation-specific configuration passed to the function
|
||||||
|
by the orchestrator.
|
||||||
|
Functions MAY consider this field optional so that they can be triggered in an ad-hoc fashion.
|
||||||
|
|
||||||
|
An example using `config.kubernetes.io/v1beta1/ResourceList` as input:
|
||||||
|
|
||||||
|
```yaml
|
||||||
|
apiVersion: config.kubernetes.io/v1beta1
|
||||||
|
kind: ResourceList
|
||||||
|
functionConfig:
|
||||||
|
apiVersion: foo-corp.com/v1
|
||||||
|
kind: FulfillmentCenter
|
||||||
|
metadata:
|
||||||
|
name: staging
|
||||||
|
metadata:
|
||||||
|
annotations:
|
||||||
|
config.k8s.io/function: |
|
||||||
|
container:
|
||||||
|
image: gcr.io/example/foo:v1.0.0
|
||||||
|
spec:
|
||||||
|
address: "100 Main St."
|
||||||
|
items:
|
||||||
|
- apiVersion: rbac.authorization.k8s.io/v1
|
||||||
|
kind: ClusterRole
|
||||||
|
metadata:
|
||||||
|
name: namespace-reader
|
||||||
|
rules:
|
||||||
|
- resources:
|
||||||
|
- namespaces
|
||||||
|
apiGroups:
|
||||||
|
- ""
|
||||||
|
verbs:
|
||||||
|
- get
|
||||||
|
- watch
|
||||||
|
- list
|
||||||
|
```
|
||||||
|
|
||||||
|
Here `FulfillmentCenter` kind with name `staging` is passed as the invocation-specific configuration
|
||||||
|
to the function.
|
||||||
|
|
||||||
|
### Output Type
|
||||||
|
|
||||||
|
A function’s output MUST be the same as the input specification above
|
||||||
|
-- i.e. `ResourceList` or `List`.
|
||||||
|
This is necessary to enable chaining two or more functions together in a pipeline.
|
||||||
|
The serialization format of the output SHOULD match that of its input on each invocation
|
||||||
|
-- e.g. if the input was a `ResourceList`, the output should also be a `ResourceList`.
|
||||||
|
|
||||||
|
### Serialization Format
|
||||||
|
|
||||||
|
A function MUST support YAML as a serialization format for the input and output.
|
||||||
|
A function MUST use utf8 encoding (as YAML is a superset of JSON, JSON will also be supported
|
||||||
|
by any conforming function).
|
||||||
|
|
||||||
|
### Operations
|
||||||
|
|
||||||
|
A function MAY Create, Update, or Delete any number of items in the `items` field and output the
|
||||||
|
resultant list.
|
||||||
|
|
||||||
|
A function MAY modify annotations with prefix `config.kubernetes.io`, but must be careful about
|
||||||
|
doing so since they’re used for orchestration purposes and will likely impact subsequent functions
|
||||||
|
in the pipeline.
|
||||||
|
|
||||||
|
A function SHOULD preserve comments when input serialization format is YAML.
|
||||||
|
This allows for human authoring of configuration to coexist with changes made by functions.
|
||||||
|
|
||||||
|
### Containerization
|
||||||
|
|
||||||
|
A function MUST be implemented as a container.
|
||||||
|
|
||||||
|
A function container MUST be capable of running as a non-root user if it does not require
|
||||||
|
access to host filesystem or makes network calls.
|
||||||
|
|
||||||
|
### stdin/stdout/stderr and Exit Codes
|
||||||
|
|
||||||
|
A function MUST accept input from stdin and emit output to stdout.
|
||||||
|
|
||||||
|
Any error messages MUST be emitted to stderr.
|
||||||
|
|
||||||
|
An exit code of zero indicates function execution was successful.
|
||||||
|
A non-zero exit code indicates a failure.
|
||||||
|
|
||||||
|
[1]: https://github.com/kubernetes/community/blob/master/contributors/devel/sig-architecture/api-conventions.md
|
||||||
|
[2]: https://tools.ietf.org/html/rfc2119
|
||||||
|
[3]: https://github.com/kubernetes/community/blob/master/contributors/devel/sig-architecture/api-conventions.md#types-kinds
|
||||||
23
cmd/config/docs/commands/list-setters.md
Normal file
23
cmd/config/docs/commands/list-setters.md
Normal file
@@ -0,0 +1,23 @@
|
|||||||
|
## set
|
||||||
|
|
||||||
|
[Alpha] List setters for Resources.
|
||||||
|
|
||||||
|
### Synopsis
|
||||||
|
|
||||||
|
List setters for Resources.
|
||||||
|
|
||||||
|
DIR
|
||||||
|
|
||||||
|
A directory containing Resource configuration.
|
||||||
|
|
||||||
|
NAME
|
||||||
|
|
||||||
|
Optional. The name of the setter to display.
|
||||||
|
|
||||||
|
### Examples
|
||||||
|
|
||||||
|
Show setters:
|
||||||
|
|
||||||
|
$ config set DIR/
|
||||||
|
NAME DESCRIPTION VALUE TYPE COUNT SETBY
|
||||||
|
name-prefix '' PREFIX string 2
|
||||||
@@ -22,8 +22,8 @@ order they appear in the file).
|
|||||||
|
|
||||||
#### Config Functions:
|
#### Config Functions:
|
||||||
|
|
||||||
Config functions are specified as Kubernetes types containing a metadata.configFn.container.image
|
Config functions are specified as Kubernetes types containing a metadata.annotations.[config.kubernetes.io/function]
|
||||||
field. This field tells run how to invoke the container.
|
field specifying an image for the container to run. This image tells run how to invoke the container.
|
||||||
|
|
||||||
Example config function:
|
Example config function:
|
||||||
|
|
||||||
@@ -31,17 +31,17 @@ order they appear in the file).
|
|||||||
apiVersion: fn.example.com/v1beta1
|
apiVersion: fn.example.com/v1beta1
|
||||||
kind: ExampleFunctionKind
|
kind: ExampleFunctionKind
|
||||||
metadata:
|
metadata:
|
||||||
configFn:
|
|
||||||
container:
|
|
||||||
# function is invoked as a container running this image
|
|
||||||
image: gcr.io/example/examplefunction:v1.0.1
|
|
||||||
annotations:
|
annotations:
|
||||||
|
config.kubernetes.io/function: |
|
||||||
|
container:
|
||||||
|
# function is invoked as a container running this image
|
||||||
|
image: gcr.io/example/examplefunction:v1.0.1
|
||||||
config.kubernetes.io/local-config: "true" # tools should ignore this
|
config.kubernetes.io/local-config: "true" # tools should ignore this
|
||||||
spec:
|
spec:
|
||||||
configField: configValue
|
configField: configValue
|
||||||
|
|
||||||
In the preceding example, 'kustomize config run example/' would identify the function by
|
In the preceding example, 'kustomize config run example/' would identify the function by
|
||||||
the metadata.configFn field. It would then write all Resources in the directory to
|
the metadata.annotations.[config.kubernetes.io/function] field. It would then write all Resources in the directory to
|
||||||
a container stdin (running the gcr.io/example/examplefunction:v1.0.1 image). It
|
a container stdin (running the gcr.io/example/examplefunction:v1.0.1 image). It
|
||||||
would then write the container stdout back to example/, replacing the directory
|
would then write the container stdout back to example/, replacing the directory
|
||||||
file contents.
|
file contents.
|
||||||
|
|||||||
@@ -59,19 +59,19 @@ To create a custom setter for a field see: `kustomize help config create-setter`
|
|||||||
List setters: Show the possible setters
|
List setters: Show the possible setters
|
||||||
|
|
||||||
$ config set DIR/
|
$ config set DIR/
|
||||||
NAME DESCRIPTION VALUE TYPE COUNT OWNER
|
NAME DESCRIPTION VALUE TYPE COUNT SETBY
|
||||||
name-prefix '' PREFIX string 2
|
name-prefix '' PREFIX string 2
|
||||||
|
|
||||||
Perform substitution: set a new value, owner and description
|
Perform set: set a new value, owner and description
|
||||||
|
|
||||||
$ kustomize config set DIR/ name-prefix "test" --description "test environment" --set-by "dev"
|
$ kustomize config set DIR/ name-prefix "test" --description "test environment" --set-by "dev"
|
||||||
performed 2 substitutions
|
set 2 values
|
||||||
|
|
||||||
Show substitutions: Show the new values
|
List setters: Show the new values
|
||||||
|
|
||||||
$ config set dir
|
$ config set DIR/
|
||||||
NAME DESCRIPTION VALUE TYPE COUNT SUBSTITUTED OWNER
|
NAME DESCRIPTION VALUE TYPE COUNT SETBY
|
||||||
prefix 'test environment' test string 2 true dev
|
name-prefix 'test environment' test string 2 dev
|
||||||
|
|
||||||
New Resource YAML:
|
New Resource YAML:
|
||||||
|
|
||||||
|
|||||||
18
cmd/config/docs/commands/sink.md
Normal file
18
cmd/config/docs/commands/sink.md
Normal file
@@ -0,0 +1,18 @@
|
|||||||
|
## sink
|
||||||
|
|
||||||
|
[Alpha] Implement a Sink by writing input to a local directory.
|
||||||
|
|
||||||
|
### Synopsis
|
||||||
|
|
||||||
|
[Alpha] Implement a Sink by writing input to a local directory.
|
||||||
|
|
||||||
|
kustomize config sink DIR
|
||||||
|
|
||||||
|
DIR:
|
||||||
|
Path to local directory.
|
||||||
|
|
||||||
|
`sink` writes its input to a directory
|
||||||
|
|
||||||
|
### Examples
|
||||||
|
|
||||||
|
kustomize config source DIR/ | your-function | kustomize config sink DIR/
|
||||||
21
cmd/config/docs/commands/source.md
Normal file
21
cmd/config/docs/commands/source.md
Normal file
@@ -0,0 +1,21 @@
|
|||||||
|
## source
|
||||||
|
|
||||||
|
[Alpha] Implement a Source by reading a local directory.
|
||||||
|
|
||||||
|
### Synopsis
|
||||||
|
|
||||||
|
[Alpha] Implement a Source by reading a local directory.
|
||||||
|
|
||||||
|
kustomize config source DIR
|
||||||
|
|
||||||
|
DIR:
|
||||||
|
Path to local directory.
|
||||||
|
|
||||||
|
`source` emits configuration to act as input to a function
|
||||||
|
|
||||||
|
### Examples
|
||||||
|
|
||||||
|
# emity configuration directory as input source to a function
|
||||||
|
kustomize config source DIR/
|
||||||
|
|
||||||
|
kustomize config source DIR/ | your-function | kustomize config sink DIR/
|
||||||
@@ -9,7 +9,6 @@ require (
|
|||||||
github.com/spf13/cobra v0.0.5
|
github.com/spf13/cobra v0.0.5
|
||||||
github.com/spf13/pflag v1.0.5
|
github.com/spf13/pflag v1.0.5
|
||||||
github.com/stretchr/testify v1.4.0
|
github.com/stretchr/testify v1.4.0
|
||||||
gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15 // indirect
|
|
||||||
k8s.io/apimachinery v0.17.0
|
k8s.io/apimachinery v0.17.0
|
||||||
sigs.k8s.io/kustomize/kyaml v0.0.0
|
sigs.k8s.io/kustomize/kyaml v0.0.0
|
||||||
)
|
)
|
||||||
|
|||||||
@@ -101,6 +101,8 @@ github.com/posener/complete/v2 v2.0.1-alpha.12/go.mod h1://JlL91cS2JV7rOl6LVHrRq
|
|||||||
github.com/posener/script v1.0.4 h1:nSuXW5ZdmFnQIueLB2s0qvs4oNsUloM1Zydzh75v42w=
|
github.com/posener/script v1.0.4 h1:nSuXW5ZdmFnQIueLB2s0qvs4oNsUloM1Zydzh75v42w=
|
||||||
github.com/posener/script v1.0.4/go.mod h1:Rg3ijooqulo05aGLyGsHoLmIOUzHUVK19WVgrYBPU/E=
|
github.com/posener/script v1.0.4/go.mod h1:Rg3ijooqulo05aGLyGsHoLmIOUzHUVK19WVgrYBPU/E=
|
||||||
github.com/russross/blackfriday v1.5.2/go.mod h1:JO/DiYxRf+HjHt06OyowR9PTA263kcR/rfWxYHBV53g=
|
github.com/russross/blackfriday v1.5.2/go.mod h1:JO/DiYxRf+HjHt06OyowR9PTA263kcR/rfWxYHBV53g=
|
||||||
|
github.com/sergi/go-diff v1.1.0 h1:we8PVUC3FE2uYfodKH/nBHMSetSfHDR6scGdBi+erh0=
|
||||||
|
github.com/sergi/go-diff v1.1.0/go.mod h1:STckp+ISIX8hZLjrqAeVduY0gWCT9IjLuqbuNXdaHfM=
|
||||||
github.com/spf13/afero v1.1.2/go.mod h1:j4pytiNVoe2o6bmDsKpLACNPDBIoEAkihy7loJ1B0CQ=
|
github.com/spf13/afero v1.1.2/go.mod h1:j4pytiNVoe2o6bmDsKpLACNPDBIoEAkihy7loJ1B0CQ=
|
||||||
github.com/spf13/cast v1.3.0/go.mod h1:Qx5cxh0v+4UWYiBimWS+eyWzqEqokIECu5etghLkUJE=
|
github.com/spf13/cast v1.3.0/go.mod h1:Qx5cxh0v+4UWYiBimWS+eyWzqEqokIECu5etghLkUJE=
|
||||||
github.com/spf13/cobra v0.0.5 h1:f0B+LkLX6DtmRH1isoNA9VTtNUK9K8xYd28JNNfOv/s=
|
github.com/spf13/cobra v0.0.5 h1:f0B+LkLX6DtmRH1isoNA9VTtNUK9K8xYd28JNNfOv/s=
|
||||||
|
|||||||
@@ -121,6 +121,13 @@ func (r *CatRunner) runE(c *cobra.Command, args []string) error {
|
|||||||
out = o
|
out = o
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// remove this annotation explicitly, the ByteWriter won't clear it by
|
||||||
|
// default because it doesn't set it
|
||||||
|
clear := []string{"config.kubernetes.io/path"}
|
||||||
|
if r.KeepAnnotations {
|
||||||
|
clear = nil
|
||||||
|
}
|
||||||
|
|
||||||
var outputs []kio.Writer
|
var outputs []kio.Writer
|
||||||
outputs = append(outputs, kio.ByteWriter{
|
outputs = append(outputs, kio.ByteWriter{
|
||||||
Writer: out,
|
Writer: out,
|
||||||
@@ -129,6 +136,7 @@ func (r *CatRunner) runE(c *cobra.Command, args []string) error {
|
|||||||
WrappingAPIVersion: r.WrapApiVersion,
|
WrappingAPIVersion: r.WrapApiVersion,
|
||||||
FunctionConfig: functionConfig,
|
FunctionConfig: functionConfig,
|
||||||
Style: yaml.GetStyle(r.Styles...),
|
Style: yaml.GetStyle(r.Styles...),
|
||||||
|
ClearAnnotations: clear,
|
||||||
})
|
})
|
||||||
|
|
||||||
return handleError(c, kio.Pipeline{Inputs: inputs, Filters: fltr, Outputs: outputs}.Execute())
|
return handleError(c, kio.Pipeline{Inputs: inputs, Filters: fltr, Outputs: outputs}.Execute())
|
||||||
|
|||||||
@@ -90,8 +90,6 @@ metadata:
|
|||||||
name: foo
|
name: foo
|
||||||
annotations:
|
annotations:
|
||||||
app: nginx2
|
app: nginx2
|
||||||
config.kubernetes.io/package: '.'
|
|
||||||
config.kubernetes.io/path: 'f1.yaml'
|
|
||||||
spec:
|
spec:
|
||||||
replicas: 1
|
replicas: 1
|
||||||
---
|
---
|
||||||
@@ -100,8 +98,6 @@ metadata:
|
|||||||
name: foo
|
name: foo
|
||||||
annotations:
|
annotations:
|
||||||
app: nginx
|
app: nginx
|
||||||
config.kubernetes.io/package: '.'
|
|
||||||
config.kubernetes.io/path: 'f1.yaml'
|
|
||||||
spec:
|
spec:
|
||||||
selector:
|
selector:
|
||||||
app: nginx
|
app: nginx
|
||||||
@@ -114,8 +110,6 @@ metadata:
|
|||||||
app: nginx
|
app: nginx
|
||||||
annotations:
|
annotations:
|
||||||
app: nginx
|
app: nginx
|
||||||
config.kubernetes.io/package: '.'
|
|
||||||
config.kubernetes.io/path: 'f2.yaml'
|
|
||||||
spec:
|
spec:
|
||||||
replicas: 3
|
replicas: 3
|
||||||
`, b.String()) {
|
`, b.String()) {
|
||||||
@@ -196,8 +190,6 @@ metadata:
|
|||||||
name: foo
|
name: foo
|
||||||
annotations:
|
annotations:
|
||||||
app: nginx2
|
app: nginx2
|
||||||
config.kubernetes.io/package: '.'
|
|
||||||
config.kubernetes.io/path: 'f1.yaml'
|
|
||||||
spec:
|
spec:
|
||||||
replicas: 1
|
replicas: 1
|
||||||
---
|
---
|
||||||
@@ -206,8 +198,6 @@ metadata:
|
|||||||
name: foo
|
name: foo
|
||||||
annotations:
|
annotations:
|
||||||
app: nginx
|
app: nginx
|
||||||
config.kubernetes.io/package: '.'
|
|
||||||
config.kubernetes.io/path: 'f1.yaml'
|
|
||||||
spec:
|
spec:
|
||||||
selector:
|
selector:
|
||||||
app: nginx
|
app: nginx
|
||||||
@@ -218,8 +208,6 @@ metadata:
|
|||||||
name: foo
|
name: foo
|
||||||
annotations:
|
annotations:
|
||||||
config.kubernetes.io/local-config: "true"
|
config.kubernetes.io/local-config: "true"
|
||||||
config.kubernetes.io/package: '.'
|
|
||||||
config.kubernetes.io/path: 'f2.yaml'
|
|
||||||
configFn:
|
configFn:
|
||||||
container:
|
container:
|
||||||
image: gcr.io/example/image:version
|
image: gcr.io/example/image:version
|
||||||
@@ -233,8 +221,6 @@ metadata:
|
|||||||
name: bar
|
name: bar
|
||||||
annotations:
|
annotations:
|
||||||
app: nginx
|
app: nginx
|
||||||
config.kubernetes.io/package: '.'
|
|
||||||
config.kubernetes.io/path: 'f2.yaml'
|
|
||||||
spec:
|
spec:
|
||||||
replicas: 3
|
replicas: 3
|
||||||
`, b.String()) {
|
`, b.String()) {
|
||||||
@@ -314,8 +300,6 @@ metadata:
|
|||||||
name: foo
|
name: foo
|
||||||
annotations:
|
annotations:
|
||||||
config.kubernetes.io/local-config: "true"
|
config.kubernetes.io/local-config: "true"
|
||||||
config.kubernetes.io/package: '.'
|
|
||||||
config.kubernetes.io/path: 'f2.yaml'
|
|
||||||
configFn:
|
configFn:
|
||||||
container:
|
container:
|
||||||
image: gcr.io/example/reconciler:v1
|
image: gcr.io/example/reconciler:v1
|
||||||
@@ -414,8 +398,6 @@ metadata:
|
|||||||
name: foo
|
name: foo
|
||||||
annotations:
|
annotations:
|
||||||
app: nginx2
|
app: nginx2
|
||||||
config.kubernetes.io/package: '.'
|
|
||||||
config.kubernetes.io/path: 'f1.yaml'
|
|
||||||
spec:
|
spec:
|
||||||
replicas: 1
|
replicas: 1
|
||||||
---
|
---
|
||||||
@@ -424,8 +406,6 @@ metadata:
|
|||||||
name: foo
|
name: foo
|
||||||
annotations:
|
annotations:
|
||||||
app: nginx
|
app: nginx
|
||||||
config.kubernetes.io/package: '.'
|
|
||||||
config.kubernetes.io/path: 'f1.yaml'
|
|
||||||
spec:
|
spec:
|
||||||
selector:
|
selector:
|
||||||
app: nginx
|
app: nginx
|
||||||
@@ -438,8 +418,6 @@ metadata:
|
|||||||
app: nginx
|
app: nginx
|
||||||
annotations:
|
annotations:
|
||||||
app: nginx
|
app: nginx
|
||||||
config.kubernetes.io/package: '.'
|
|
||||||
config.kubernetes.io/path: 'f2.yaml'
|
|
||||||
spec:
|
spec:
|
||||||
replicas: 3
|
replicas: 3
|
||||||
`, string(actual)) {
|
`, string(actual)) {
|
||||||
@@ -536,8 +514,6 @@ metadata:
|
|||||||
name: foo
|
name: foo
|
||||||
annotations:
|
annotations:
|
||||||
app: nginx2
|
app: nginx2
|
||||||
config.kubernetes.io/package: '.'
|
|
||||||
config.kubernetes.io/path: 'f1.yaml'
|
|
||||||
spec:
|
spec:
|
||||||
replicas: 1
|
replicas: 1
|
||||||
---
|
---
|
||||||
@@ -546,8 +522,6 @@ metadata:
|
|||||||
name: foo
|
name: foo
|
||||||
annotations:
|
annotations:
|
||||||
app: nginx
|
app: nginx
|
||||||
config.kubernetes.io/package: '.'
|
|
||||||
config.kubernetes.io/path: 'f1.yaml'
|
|
||||||
spec:
|
spec:
|
||||||
selector:
|
selector:
|
||||||
app: nginx
|
app: nginx
|
||||||
@@ -560,8 +534,6 @@ metadata:
|
|||||||
app: nginx
|
app: nginx
|
||||||
annotations:
|
annotations:
|
||||||
app: nginx
|
app: nginx
|
||||||
config.kubernetes.io/package: '.'
|
|
||||||
config.kubernetes.io/path: 'f2.yaml'
|
|
||||||
spec:
|
spec:
|
||||||
replicas: 3
|
replicas: 3
|
||||||
`, string(actual)) {
|
`, string(actual)) {
|
||||||
|
|||||||
47
cmd/config/internal/commands/cmdlistsetters.go
Normal file
47
cmd/config/internal/commands/cmdlistsetters.go
Normal file
@@ -0,0 +1,47 @@
|
|||||||
|
// Copyright 2019 The Kubernetes Authors.
|
||||||
|
// SPDX-License-Identifier: Apache-2.0
|
||||||
|
|
||||||
|
package commands
|
||||||
|
|
||||||
|
import (
|
||||||
|
"github.com/spf13/cobra"
|
||||||
|
"sigs.k8s.io/kustomize/cmd/config/internal/generateddocs/commands"
|
||||||
|
"sigs.k8s.io/kustomize/kyaml/setters"
|
||||||
|
)
|
||||||
|
|
||||||
|
// NewListSettersRunner returns a command runner.
|
||||||
|
func NewListSettersRunner(parent string) *ListSettersRunner {
|
||||||
|
r := &ListSettersRunner{}
|
||||||
|
c := &cobra.Command{
|
||||||
|
Use: "list-setters DIR [NAME]",
|
||||||
|
Args: cobra.RangeArgs(1, 2),
|
||||||
|
Short: commands.ListSettersShort,
|
||||||
|
Long: commands.ListSettersLong,
|
||||||
|
Example: commands.ListSettersExamples,
|
||||||
|
PreRunE: r.preRunE,
|
||||||
|
RunE: r.runE,
|
||||||
|
}
|
||||||
|
fixDocs(parent, c)
|
||||||
|
r.Command = c
|
||||||
|
return r
|
||||||
|
}
|
||||||
|
|
||||||
|
func ListSettersCommand(parent string) *cobra.Command {
|
||||||
|
return NewListSettersRunner(parent).Command
|
||||||
|
}
|
||||||
|
|
||||||
|
type ListSettersRunner struct {
|
||||||
|
Command *cobra.Command
|
||||||
|
Lookup setters.LookupSetters
|
||||||
|
}
|
||||||
|
|
||||||
|
func (r *ListSettersRunner) preRunE(c *cobra.Command, args []string) error {
|
||||||
|
if len(args) > 1 {
|
||||||
|
r.Lookup.Name = args[1]
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (r *ListSettersRunner) runE(c *cobra.Command, args []string) error {
|
||||||
|
return handleError(c, lookup(r.Lookup, c, args))
|
||||||
|
}
|
||||||
@@ -5,6 +5,7 @@ package commands
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"os"
|
||||||
|
|
||||||
"github.com/olekukonko/tablewriter"
|
"github.com/olekukonko/tablewriter"
|
||||||
"github.com/spf13/cobra"
|
"github.com/spf13/cobra"
|
||||||
@@ -63,14 +64,14 @@ func (r *SetRunner) runE(c *cobra.Command, args []string) error {
|
|||||||
return handleError(c, r.perform(c, args))
|
return handleError(c, r.perform(c, args))
|
||||||
}
|
}
|
||||||
|
|
||||||
return handleError(c, r.lookup(c, args))
|
return handleError(c, lookup(r.Lookup, c, args))
|
||||||
}
|
}
|
||||||
|
|
||||||
func (r *SetRunner) lookup(c *cobra.Command, args []string) error {
|
func lookup(l setters.LookupSetters, c *cobra.Command, args []string) error {
|
||||||
// lookup the setters
|
// lookup the setters
|
||||||
err := kio.Pipeline{
|
err := kio.Pipeline{
|
||||||
Inputs: []kio.Reader{&kio.LocalPackageReader{PackagePath: args[0]}},
|
Inputs: []kio.Reader{&kio.LocalPackageReader{PackagePath: args[0]}},
|
||||||
Filters: []kio.Filter{&r.Lookup},
|
Filters: []kio.Filter{&l},
|
||||||
}.Execute()
|
}.Execute()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
@@ -86,8 +87,8 @@ func (r *SetRunner) lookup(c *cobra.Command, args []string) error {
|
|||||||
table.SetHeader([]string{
|
table.SetHeader([]string{
|
||||||
"NAME", "DESCRIPTION", "VALUE", "TYPE", "COUNT", "SETBY",
|
"NAME", "DESCRIPTION", "VALUE", "TYPE", "COUNT", "SETBY",
|
||||||
})
|
})
|
||||||
for i := range r.Lookup.SetterCounts {
|
for i := range l.SetterCounts {
|
||||||
s := r.Lookup.SetterCounts[i]
|
s := l.SetterCounts[i]
|
||||||
v := s.Value
|
v := s.Value
|
||||||
if s.Value == "" {
|
if s.Value == "" {
|
||||||
v = s.Value
|
v = s.Value
|
||||||
@@ -102,6 +103,11 @@ func (r *SetRunner) lookup(c *cobra.Command, args []string) error {
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
table.Render()
|
table.Render()
|
||||||
|
|
||||||
|
if len(l.SetterCounts) == 0 {
|
||||||
|
// exit non-0 if no matching setters are found
|
||||||
|
os.Exit(1)
|
||||||
|
}
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -76,7 +76,6 @@ metadata:
|
|||||||
annotations:
|
annotations:
|
||||||
app: nginx2
|
app: nginx2
|
||||||
config.kubernetes.io/index: '0'
|
config.kubernetes.io/index: '0'
|
||||||
config.kubernetes.io/package: '.'
|
|
||||||
config.kubernetes.io/path: 'f1.yaml'
|
config.kubernetes.io/path: 'f1.yaml'
|
||||||
spec:
|
spec:
|
||||||
replicas: 1
|
replicas: 1
|
||||||
@@ -87,7 +86,6 @@ metadata:
|
|||||||
annotations:
|
annotations:
|
||||||
app: nginx
|
app: nginx
|
||||||
config.kubernetes.io/index: '1'
|
config.kubernetes.io/index: '1'
|
||||||
config.kubernetes.io/package: '.'
|
|
||||||
config.kubernetes.io/path: 'f1.yaml'
|
config.kubernetes.io/path: 'f1.yaml'
|
||||||
spec:
|
spec:
|
||||||
selector:
|
selector:
|
||||||
|
|||||||
48
cmd/config/internal/commands/sink.go
Normal file
48
cmd/config/internal/commands/sink.go
Normal file
@@ -0,0 +1,48 @@
|
|||||||
|
// Copyright 2019 The Kubernetes Authors.
|
||||||
|
// SPDX-License-Identifier: Apache-2.0
|
||||||
|
|
||||||
|
package commands
|
||||||
|
|
||||||
|
import (
|
||||||
|
"github.com/spf13/cobra"
|
||||||
|
"sigs.k8s.io/kustomize/cmd/config/internal/generateddocs/commands"
|
||||||
|
"sigs.k8s.io/kustomize/kyaml/kio"
|
||||||
|
)
|
||||||
|
|
||||||
|
// GetSinkRunner returns a command for Sink.
|
||||||
|
func GetSinkRunner(name string) *SinkRunner {
|
||||||
|
r := &SinkRunner{}
|
||||||
|
c := &cobra.Command{
|
||||||
|
Use: "sink DIR",
|
||||||
|
Short: commands.SinkShort,
|
||||||
|
Long: commands.SinkLong,
|
||||||
|
Example: commands.SinkExamples,
|
||||||
|
RunE: r.runE,
|
||||||
|
Args: cobra.ExactArgs(1),
|
||||||
|
}
|
||||||
|
fixDocs(name, c)
|
||||||
|
r.Command = c
|
||||||
|
return r
|
||||||
|
}
|
||||||
|
|
||||||
|
func SinkCommand(name string) *cobra.Command {
|
||||||
|
return GetSinkRunner(name).Command
|
||||||
|
}
|
||||||
|
|
||||||
|
// SinkRunner contains the run function
|
||||||
|
type SinkRunner struct {
|
||||||
|
Command *cobra.Command
|
||||||
|
}
|
||||||
|
|
||||||
|
func (r *SinkRunner) runE(c *cobra.Command, args []string) error {
|
||||||
|
err := kio.Pipeline{
|
||||||
|
Inputs: []kio.Reader{&kio.ByteReader{Reader: c.InOrStdin()}},
|
||||||
|
Outputs: []kio.Writer{
|
||||||
|
&kio.LocalPackageWriter{
|
||||||
|
PackagePath: args[0],
|
||||||
|
ClearAnnotations: []string{"config.kubernetes.io/path"},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
}.Execute()
|
||||||
|
return handleError(c, err)
|
||||||
|
}
|
||||||
140
cmd/config/internal/commands/sink_test.go
Normal file
140
cmd/config/internal/commands/sink_test.go
Normal file
@@ -0,0 +1,140 @@
|
|||||||
|
// Copyright 2019 The Kubernetes Authors.
|
||||||
|
// SPDX-License-Identifier: Apache-2.0
|
||||||
|
|
||||||
|
package commands_test
|
||||||
|
|
||||||
|
import (
|
||||||
|
"bytes"
|
||||||
|
"io/ioutil"
|
||||||
|
"os"
|
||||||
|
"path/filepath"
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
"github.com/stretchr/testify/assert"
|
||||||
|
"sigs.k8s.io/kustomize/cmd/config/internal/commands"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestSinkCommand(t *testing.T) {
|
||||||
|
d, err := ioutil.TempDir("", "kustomize-source-test")
|
||||||
|
if !assert.NoError(t, err) {
|
||||||
|
t.FailNow()
|
||||||
|
}
|
||||||
|
defer os.RemoveAll(d)
|
||||||
|
|
||||||
|
// fmt the files
|
||||||
|
b := &bytes.Buffer{}
|
||||||
|
r := commands.GetSinkRunner("")
|
||||||
|
r.Command.SetIn(bytes.NewBufferString(`apiVersion: config.kubernetes.io/v1alpha1
|
||||||
|
kind: ResourceList
|
||||||
|
items:
|
||||||
|
- kind: Deployment
|
||||||
|
metadata:
|
||||||
|
labels:
|
||||||
|
app: nginx2
|
||||||
|
name: foo
|
||||||
|
annotations:
|
||||||
|
app: nginx2
|
||||||
|
config.kubernetes.io/index: '0'
|
||||||
|
config.kubernetes.io/path: 'f1.yaml'
|
||||||
|
spec:
|
||||||
|
replicas: 1
|
||||||
|
- kind: Service
|
||||||
|
metadata:
|
||||||
|
name: foo
|
||||||
|
annotations:
|
||||||
|
app: nginx
|
||||||
|
config.kubernetes.io/index: '1'
|
||||||
|
config.kubernetes.io/path: 'f1.yaml'
|
||||||
|
spec:
|
||||||
|
selector:
|
||||||
|
app: nginx
|
||||||
|
- apiVersion: v1
|
||||||
|
kind: Abstraction
|
||||||
|
metadata:
|
||||||
|
name: foo
|
||||||
|
annotations:
|
||||||
|
config.kubernetes.io/function: |
|
||||||
|
container:
|
||||||
|
image: gcr.io/example/reconciler:v1
|
||||||
|
config.kubernetes.io/local-config: "true"
|
||||||
|
config.kubernetes.io/index: '0'
|
||||||
|
config.kubernetes.io/path: 'f2.yaml'
|
||||||
|
spec:
|
||||||
|
replicas: 3
|
||||||
|
- apiVersion: apps/v1
|
||||||
|
kind: Deployment
|
||||||
|
metadata:
|
||||||
|
labels:
|
||||||
|
app: nginx
|
||||||
|
name: bar
|
||||||
|
annotations:
|
||||||
|
app: nginx
|
||||||
|
config.kubernetes.io/index: '1'
|
||||||
|
config.kubernetes.io/path: 'f2.yaml'
|
||||||
|
spec:
|
||||||
|
replicas: 3
|
||||||
|
`))
|
||||||
|
r.Command.SetArgs([]string{d})
|
||||||
|
r.Command.SetOut(b)
|
||||||
|
if !assert.NoError(t, r.Command.Execute()) {
|
||||||
|
t.FailNow()
|
||||||
|
}
|
||||||
|
|
||||||
|
actual, err := ioutil.ReadFile(filepath.Join(d, "f1.yaml"))
|
||||||
|
if !assert.NoError(t, err) {
|
||||||
|
t.FailNow()
|
||||||
|
}
|
||||||
|
expected := `kind: Deployment
|
||||||
|
metadata:
|
||||||
|
labels:
|
||||||
|
app: nginx2
|
||||||
|
name: foo
|
||||||
|
annotations:
|
||||||
|
app: nginx2
|
||||||
|
spec:
|
||||||
|
replicas: 1
|
||||||
|
---
|
||||||
|
kind: Service
|
||||||
|
metadata:
|
||||||
|
name: foo
|
||||||
|
annotations:
|
||||||
|
app: nginx
|
||||||
|
spec:
|
||||||
|
selector:
|
||||||
|
app: nginx
|
||||||
|
`
|
||||||
|
if !assert.Equal(t, expected, string(actual)) {
|
||||||
|
t.FailNow()
|
||||||
|
}
|
||||||
|
|
||||||
|
actual, err = ioutil.ReadFile(filepath.Join(d, "f2.yaml"))
|
||||||
|
if !assert.NoError(t, err) {
|
||||||
|
t.FailNow()
|
||||||
|
}
|
||||||
|
expected = `apiVersion: v1
|
||||||
|
kind: Abstraction
|
||||||
|
metadata:
|
||||||
|
name: foo
|
||||||
|
annotations:
|
||||||
|
config.kubernetes.io/function: |
|
||||||
|
container:
|
||||||
|
image: gcr.io/example/reconciler:v1
|
||||||
|
config.kubernetes.io/local-config: "true"
|
||||||
|
spec:
|
||||||
|
replicas: 3
|
||||||
|
---
|
||||||
|
apiVersion: apps/v1
|
||||||
|
kind: Deployment
|
||||||
|
metadata:
|
||||||
|
labels:
|
||||||
|
app: nginx
|
||||||
|
name: bar
|
||||||
|
annotations:
|
||||||
|
app: nginx
|
||||||
|
spec:
|
||||||
|
replicas: 3
|
||||||
|
`
|
||||||
|
if !assert.Equal(t, expected, string(actual)) {
|
||||||
|
t.FailNow()
|
||||||
|
}
|
||||||
|
}
|
||||||
77
cmd/config/internal/commands/source.go
Normal file
77
cmd/config/internal/commands/source.go
Normal file
@@ -0,0 +1,77 @@
|
|||||||
|
// Copyright 2019 The Kubernetes Authors.
|
||||||
|
// SPDX-License-Identifier: Apache-2.0
|
||||||
|
|
||||||
|
package commands
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
|
||||||
|
"github.com/spf13/cobra"
|
||||||
|
"sigs.k8s.io/kustomize/cmd/config/internal/generateddocs/commands"
|
||||||
|
"sigs.k8s.io/kustomize/kyaml/kio"
|
||||||
|
"sigs.k8s.io/kustomize/kyaml/yaml"
|
||||||
|
)
|
||||||
|
|
||||||
|
// GetSourceRunner returns a command for Source.
|
||||||
|
func GetSourceRunner(name string) *SourceRunner {
|
||||||
|
r := &SourceRunner{}
|
||||||
|
c := &cobra.Command{
|
||||||
|
Use: "source DIR",
|
||||||
|
Short: commands.SourceShort,
|
||||||
|
Long: commands.SourceLong,
|
||||||
|
Example: commands.SourceExamples,
|
||||||
|
RunE: r.runE,
|
||||||
|
Args: cobra.ExactArgs(1),
|
||||||
|
}
|
||||||
|
fixDocs(name, c)
|
||||||
|
c.Flags().StringVar(&r.WrapKind, "wrap-kind", kio.ResourceListKind,
|
||||||
|
"output using this format.")
|
||||||
|
c.Flags().StringVar(&r.WrapApiVersion, "wrap-version", kio.ResourceListAPIVersion,
|
||||||
|
"output using this format.")
|
||||||
|
c.Flags().StringVar(&r.FunctionConfig, "function-config", "",
|
||||||
|
"path to function config.")
|
||||||
|
r.Command = c
|
||||||
|
_ = c.MarkFlagFilename("function-config", "yaml", "json", "yml")
|
||||||
|
return r
|
||||||
|
}
|
||||||
|
|
||||||
|
func SourceCommand(name string) *cobra.Command {
|
||||||
|
return GetSourceRunner(name).Command
|
||||||
|
}
|
||||||
|
|
||||||
|
// SourceRunner contains the run function
|
||||||
|
type SourceRunner struct {
|
||||||
|
WrapKind string
|
||||||
|
WrapApiVersion string
|
||||||
|
FunctionConfig string
|
||||||
|
Command *cobra.Command
|
||||||
|
}
|
||||||
|
|
||||||
|
func (r *SourceRunner) runE(c *cobra.Command, args []string) error {
|
||||||
|
// if there is a function-config specified, emit it
|
||||||
|
var functionConfig *yaml.RNode
|
||||||
|
if r.FunctionConfig != "" {
|
||||||
|
configs, err := kio.LocalPackageReader{PackagePath: r.FunctionConfig}.Read()
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
if len(configs) != 1 {
|
||||||
|
return fmt.Errorf("expected exactly 1 functionConfig, found %d", len(configs))
|
||||||
|
}
|
||||||
|
functionConfig = configs[0]
|
||||||
|
}
|
||||||
|
|
||||||
|
var outputs []kio.Writer
|
||||||
|
outputs = append(outputs, kio.ByteWriter{
|
||||||
|
Writer: c.OutOrStdout(),
|
||||||
|
KeepReaderAnnotations: true,
|
||||||
|
WrappingKind: r.WrapKind,
|
||||||
|
WrappingAPIVersion: r.WrapApiVersion,
|
||||||
|
FunctionConfig: functionConfig,
|
||||||
|
})
|
||||||
|
|
||||||
|
err := kio.Pipeline{
|
||||||
|
Inputs: []kio.Reader{kio.LocalPackageReader{PackagePath: args[0]}},
|
||||||
|
Outputs: outputs}.Execute()
|
||||||
|
return handleError(c, err)
|
||||||
|
}
|
||||||
136
cmd/config/internal/commands/source_test.go
Normal file
136
cmd/config/internal/commands/source_test.go
Normal file
@@ -0,0 +1,136 @@
|
|||||||
|
// Copyright 2019 The Kubernetes Authors.
|
||||||
|
// SPDX-License-Identifier: Apache-2.0
|
||||||
|
|
||||||
|
package commands_test
|
||||||
|
|
||||||
|
import (
|
||||||
|
"bytes"
|
||||||
|
"io/ioutil"
|
||||||
|
"os"
|
||||||
|
"path/filepath"
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
"github.com/stretchr/testify/assert"
|
||||||
|
"sigs.k8s.io/kustomize/cmd/config/internal/commands"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestSourceCommand(t *testing.T) {
|
||||||
|
d, err := ioutil.TempDir("", "kustomize-source-test")
|
||||||
|
if !assert.NoError(t, err) {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
defer os.RemoveAll(d)
|
||||||
|
|
||||||
|
err = ioutil.WriteFile(filepath.Join(d, "f1.yaml"), []byte(`
|
||||||
|
kind: Deployment
|
||||||
|
metadata:
|
||||||
|
labels:
|
||||||
|
app: nginx2
|
||||||
|
name: foo
|
||||||
|
annotations:
|
||||||
|
app: nginx2
|
||||||
|
spec:
|
||||||
|
replicas: 1
|
||||||
|
---
|
||||||
|
kind: Service
|
||||||
|
metadata:
|
||||||
|
name: foo
|
||||||
|
annotations:
|
||||||
|
app: nginx
|
||||||
|
spec:
|
||||||
|
selector:
|
||||||
|
app: nginx
|
||||||
|
`), 0600)
|
||||||
|
if !assert.NoError(t, err) {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
err = ioutil.WriteFile(filepath.Join(d, "f2.yaml"), []byte(`
|
||||||
|
apiVersion: v1
|
||||||
|
kind: Abstraction
|
||||||
|
metadata:
|
||||||
|
name: foo
|
||||||
|
annotations:
|
||||||
|
config.kubernetes.io/function: |
|
||||||
|
container:
|
||||||
|
image: gcr.io/example/reconciler:v1
|
||||||
|
config.kubernetes.io/local-config: "true"
|
||||||
|
spec:
|
||||||
|
replicas: 3
|
||||||
|
---
|
||||||
|
apiVersion: apps/v1
|
||||||
|
kind: Deployment
|
||||||
|
metadata:
|
||||||
|
labels:
|
||||||
|
app: nginx
|
||||||
|
name: bar
|
||||||
|
annotations:
|
||||||
|
app: nginx
|
||||||
|
spec:
|
||||||
|
replicas: 3
|
||||||
|
`), 0600)
|
||||||
|
if !assert.NoError(t, err) {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// fmt the files
|
||||||
|
b := &bytes.Buffer{}
|
||||||
|
r := commands.GetSourceRunner("")
|
||||||
|
r.Command.SetArgs([]string{d})
|
||||||
|
r.Command.SetOut(b)
|
||||||
|
if !assert.NoError(t, r.Command.Execute()) {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
if !assert.Equal(t, `apiVersion: config.kubernetes.io/v1alpha1
|
||||||
|
kind: ResourceList
|
||||||
|
items:
|
||||||
|
- kind: Deployment
|
||||||
|
metadata:
|
||||||
|
labels:
|
||||||
|
app: nginx2
|
||||||
|
name: foo
|
||||||
|
annotations:
|
||||||
|
app: nginx2
|
||||||
|
config.kubernetes.io/index: '0'
|
||||||
|
config.kubernetes.io/path: 'f1.yaml'
|
||||||
|
spec:
|
||||||
|
replicas: 1
|
||||||
|
- kind: Service
|
||||||
|
metadata:
|
||||||
|
name: foo
|
||||||
|
annotations:
|
||||||
|
app: nginx
|
||||||
|
config.kubernetes.io/index: '1'
|
||||||
|
config.kubernetes.io/path: 'f1.yaml'
|
||||||
|
spec:
|
||||||
|
selector:
|
||||||
|
app: nginx
|
||||||
|
- apiVersion: v1
|
||||||
|
kind: Abstraction
|
||||||
|
metadata:
|
||||||
|
name: foo
|
||||||
|
annotations:
|
||||||
|
config.kubernetes.io/function: |
|
||||||
|
container:
|
||||||
|
image: gcr.io/example/reconciler:v1
|
||||||
|
config.kubernetes.io/local-config: "true"
|
||||||
|
config.kubernetes.io/index: '0'
|
||||||
|
config.kubernetes.io/path: 'f2.yaml'
|
||||||
|
spec:
|
||||||
|
replicas: 3
|
||||||
|
- apiVersion: apps/v1
|
||||||
|
kind: Deployment
|
||||||
|
metadata:
|
||||||
|
labels:
|
||||||
|
app: nginx
|
||||||
|
name: bar
|
||||||
|
annotations:
|
||||||
|
app: nginx
|
||||||
|
config.kubernetes.io/index: '1'
|
||||||
|
config.kubernetes.io/path: 'f2.yaml'
|
||||||
|
spec:
|
||||||
|
replicas: 3
|
||||||
|
`, b.String()) {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -104,7 +104,6 @@ metadata:
|
|||||||
namespace: default
|
namespace: default
|
||||||
annotations:
|
annotations:
|
||||||
app: nginx2
|
app: nginx2
|
||||||
config.kubernetes.io/package: .
|
|
||||||
config.kubernetes.io/path: f1.yaml
|
config.kubernetes.io/path: f1.yaml
|
||||||
spec:
|
spec:
|
||||||
replicas: 1
|
replicas: 1
|
||||||
@@ -118,7 +117,6 @@ metadata:
|
|||||||
namespace: default
|
namespace: default
|
||||||
annotations:
|
annotations:
|
||||||
app: nginx2
|
app: nginx2
|
||||||
config.kubernetes.io/package: .
|
|
||||||
config.kubernetes.io/path: f1.yaml
|
config.kubernetes.io/path: f1.yaml
|
||||||
spec:
|
spec:
|
||||||
replicas: 1
|
replicas: 1
|
||||||
@@ -132,7 +130,6 @@ metadata:
|
|||||||
namespace: default
|
namespace: default
|
||||||
annotations:
|
annotations:
|
||||||
app: nginx2
|
app: nginx2
|
||||||
config.kubernetes.io/package: .
|
|
||||||
config.kubernetes.io/path: f1.yaml
|
config.kubernetes.io/path: f1.yaml
|
||||||
spec:
|
spec:
|
||||||
replicas: 1
|
replicas: 1
|
||||||
@@ -146,7 +143,6 @@ metadata:
|
|||||||
namespace: default2
|
namespace: default2
|
||||||
annotations:
|
annotations:
|
||||||
app: nginx2
|
app: nginx2
|
||||||
config.kubernetes.io/package: .
|
|
||||||
config.kubernetes.io/path: f1.yaml
|
config.kubernetes.io/path: f1.yaml
|
||||||
spec:
|
spec:
|
||||||
replicas: 1
|
replicas: 1
|
||||||
@@ -160,7 +156,6 @@ metadata:
|
|||||||
namespace: default
|
namespace: default
|
||||||
annotations:
|
annotations:
|
||||||
app: nginx3
|
app: nginx3
|
||||||
config.kubernetes.io/package: .
|
|
||||||
config.kubernetes.io/path: f1.yaml
|
config.kubernetes.io/path: f1.yaml
|
||||||
spec:
|
spec:
|
||||||
replicas: 1
|
replicas: 1
|
||||||
@@ -171,8 +166,7 @@ metadata:
|
|||||||
app: nginx
|
app: nginx
|
||||||
annotations:
|
annotations:
|
||||||
app: nginx
|
app: nginx
|
||||||
config.kubernetes.io/package: bar-package
|
config.kubernetes.io/path: bar-package/f2.yaml
|
||||||
config.kubernetes.io/path: f2.yaml
|
|
||||||
name: bar
|
name: bar
|
||||||
spec:
|
spec:
|
||||||
replicas: 3
|
replicas: 3
|
||||||
@@ -183,7 +177,6 @@ metadata:
|
|||||||
namespace: default
|
namespace: default
|
||||||
annotations:
|
annotations:
|
||||||
app: nginx
|
app: nginx
|
||||||
config.kubernetes.io/package: .
|
|
||||||
config.kubernetes.io/path: f1.yaml
|
config.kubernetes.io/path: f1.yaml
|
||||||
spec:
|
spec:
|
||||||
selector:
|
selector:
|
||||||
|
|||||||
@@ -4,288 +4,6 @@
|
|||||||
// Code generated by "mdtogo"; DO NOT EDIT.
|
// Code generated by "mdtogo"; DO NOT EDIT.
|
||||||
package api
|
package api
|
||||||
|
|
||||||
var ConfigFnLong = `# Configuration Functions API Semantics
|
|
||||||
|
|
||||||
Configuration Functions are functions packaged as executables in containers which enable
|
|
||||||
**shift-left practices**. They configure applications and infrastructure through
|
|
||||||
Kubernetes style Resource Configuration, but run locally pre-commit.
|
|
||||||
|
|
||||||
Configuration functions enable shift-left practices (client-side) through:
|
|
||||||
|
|
||||||
- Pre-commit / delivery validation and linting of configuration
|
|
||||||
- e.g. Fail if any containers don't have PodSecurityPolicy or CPU / Memory limits
|
|
||||||
- Implementation of abstractions as client actuated APIs (e.g. templating)
|
|
||||||
- e.g. Create a client-side *"CRD"* for generating configuration checked into git
|
|
||||||
- Aspect Orient configuration / Injection of cross-cutting configuration
|
|
||||||
- e.g. T-Shirt size containers by annotating Resources with ` + "`" + `small` + "`" + `, ` + "`" + `medium` + "`" + `, ` + "`" + `large` + "`" + `
|
|
||||||
and inject the cpu and memory resources into containers accordingly.
|
|
||||||
- e.g. Inject ` + "`" + `init` + "`" + ` and ` + "`" + `side-car` + "`" + ` containers into Resources based off of Resource
|
|
||||||
Type, annotations, etc.
|
|
||||||
|
|
||||||
Performing these on the client rather than the server enables:
|
|
||||||
|
|
||||||
- Configuration to be reviewed prior to being sent to the API server
|
|
||||||
- Configuration to be validated as part of the CD pipeline
|
|
||||||
- Configuration for Resources to validated holistically rather than individually
|
|
||||||
per-Resource -- e.g. ensure the ` + "`" + `Service.selector` + "`" + ` and ` + "`" + `Deployment.spec.template` + "`" + ` labels
|
|
||||||
match.
|
|
||||||
- MutatingWebHooks are scoped to a single Resource instance at a time.
|
|
||||||
- Low-level tweaks to the output of high-level abstractions -- e.g. add an ` + "`" + `init container` + "`" + `
|
|
||||||
to a client *"CRD"* Resource after it was generated.
|
|
||||||
- Composition and layering of multiple functions together
|
|
||||||
- Compose generation, injection, validation together
|
|
||||||
|
|
||||||
Configuration Functions are implemented as executable programs published in containers which:
|
|
||||||
|
|
||||||
- Accept as input (stdin):
|
|
||||||
- A list of Resource Configuration
|
|
||||||
- A Function Configuration (to configure the function itself)
|
|
||||||
- Emit as output (stdout + exit):
|
|
||||||
- A list of Resource Configuration
|
|
||||||
- An exit code for success / failure
|
|
||||||
|
|
||||||
### Function Specification
|
|
||||||
|
|
||||||
- Functions **SHOULD** be published as container images containing a ` + "`" + `CMD` + "`" + ` invoking an executable.
|
|
||||||
- Functions **MUST** accept input on STDIN a ` + "`" + `ResourceList` + "`" + ` containing the Resources and
|
|
||||||
` + "`" + `functionConfig` + "`" + `.
|
|
||||||
- Functions **MUST** emit output on STDOUT a ` + "`" + `ResourceList` + "`" + ` containing the modified
|
|
||||||
Resources.
|
|
||||||
- Functions **MUST** exit non-0 on failure, and exit 0 on success.
|
|
||||||
- Functions **MAY** emit output on STDERR with error messaging.
|
|
||||||
- Functions performing validation **SHOULD** exit failure and emit error messaging
|
|
||||||
on a validation failure.
|
|
||||||
- Functions generating Resources **SHOULD** retain non-conflicting changes on the
|
|
||||||
generated Resources -- e.g. 1. the function generates a Deployment, but doesn't
|
|
||||||
specify ` + "`" + `cpu` + "`" + `, 2. the user sets the ` + "`" + `cpu` + "`" + ` on the generated Resource, 3. the
|
|
||||||
function should keep the ` + "`" + `cpu` + "`" + ` when regenerating the Resource a second time.
|
|
||||||
- Functions **SHOULD** be usable outside ` + "`" + `kustomize config run` + "`" + ` -- e.g. though pipeline
|
|
||||||
mechanisms such as Tekton.
|
|
||||||
|
|
||||||
#### Input Format
|
|
||||||
|
|
||||||
Functions must accept on STDIN:
|
|
||||||
|
|
||||||
` + "`" + `ResourceList` + "`" + `:
|
|
||||||
- contains ` + "`" + `items` + "`" + ` field, same as ` + "`" + `List.items` + "`" + `
|
|
||||||
- contains ` + "`" + `functionConfig` + "`" + ` field -- a single item with the configuration for the function itself
|
|
||||||
|
|
||||||
Example ` + "`" + `ResourceList` + "`" + ` Input:
|
|
||||||
|
|
||||||
apiVersion: config.kubernetes.io/v1alpha1
|
|
||||||
kind: ResourceList
|
|
||||||
functionConfig:
|
|
||||||
apiVersion: example.com/v1beta1
|
|
||||||
kind: Nginx
|
|
||||||
metadata:
|
|
||||||
name: my-instance
|
|
||||||
annotations:
|
|
||||||
config.kubernetes.io/local-config: "true"
|
|
||||||
spec:
|
|
||||||
replicas: 5
|
|
||||||
items:
|
|
||||||
- apiVersion: apps/v1
|
|
||||||
kind: Deployment
|
|
||||||
metadata:
|
|
||||||
name: my-instance
|
|
||||||
spec:
|
|
||||||
replicas: 3
|
|
||||||
...
|
|
||||||
- apiVersion: v1
|
|
||||||
kind: Service
|
|
||||||
metadata:
|
|
||||||
name: my-instance
|
|
||||||
spec:
|
|
||||||
...
|
|
||||||
|
|
||||||
#### Output Format
|
|
||||||
|
|
||||||
Functions must emit on STDOUT:
|
|
||||||
|
|
||||||
` + "`" + `ResourceList` + "`" + `:
|
|
||||||
- contains ` + "`" + `items` + "`" + ` field, same as ` + "`" + `List.items` + "`" + `
|
|
||||||
|
|
||||||
Example ` + "`" + `ResourceList` + "`" + ` Output:
|
|
||||||
|
|
||||||
apiVersion: config.kubernetes.io/v1alpha1
|
|
||||||
kind: ResourceList
|
|
||||||
items:
|
|
||||||
- apiVersion: apps/v1
|
|
||||||
kind: Deployment
|
|
||||||
metadata:
|
|
||||||
name: my-instance
|
|
||||||
spec:
|
|
||||||
replicas: 5
|
|
||||||
...
|
|
||||||
- apiVersion: v1
|
|
||||||
kind: Service
|
|
||||||
metadata:
|
|
||||||
name: my-instance
|
|
||||||
spec:
|
|
||||||
...
|
|
||||||
|
|
||||||
#### Container Environment
|
|
||||||
|
|
||||||
When run by ` + "`" + `kustomize config run` + "`" + `, functions are run in containers with the
|
|
||||||
following environment:
|
|
||||||
|
|
||||||
- Network: ` + "`" + `none` + "`" + `
|
|
||||||
- User: ` + "`" + `nobody` + "`" + `
|
|
||||||
- Security Options: ` + "`" + `no-new-privileges` + "`" + `
|
|
||||||
- Volumes: the volume containing the ` + "`" + `functionConfig` + "`" + ` yaml is mounted under ` + "`" + `/local` + "`" + ` as ` + "`" + `ro` + "`" + `
|
|
||||||
|
|
||||||
### Example Function Implementation
|
|
||||||
|
|
||||||
Following is an example for implementing an nginx abstraction using a config
|
|
||||||
function.
|
|
||||||
|
|
||||||
#### ` + "`" + `nginx-template.sh` + "`" + `
|
|
||||||
|
|
||||||
` + "`" + `nginx-template.sh` + "`" + ` is a simple bash script which uses a *heredoc* as a templating solution
|
|
||||||
for generating Resources from the functionConfig input fields.
|
|
||||||
|
|
||||||
The script wraps itself using ` + "`" + `config run wrap -- $0` + "`" + ` which will:
|
|
||||||
|
|
||||||
1. Parse the ` + "`" + `ResourceList.functionConfig` + "`" + ` (provided to the container stdin) into env vars
|
|
||||||
2. Merge the stdout into the original list of Resources
|
|
||||||
3. Defaults filenames for newly generated Resources (if they are not set as annotations)
|
|
||||||
to ` + "`" + `config/NAME_KIND.yaml` + "`" + `
|
|
||||||
4. Format the output
|
|
||||||
|
|
||||||
#!/bin/bash
|
|
||||||
# script must run wrapped by ` + "`" + `kustomize config run wrap` + "`" + `
|
|
||||||
# for parsing input the functionConfig into env vars
|
|
||||||
if [ -z ${WRAPPED} ]; then
|
|
||||||
export WRAPPED=true
|
|
||||||
config run wrap -- $0
|
|
||||||
exit $?
|
|
||||||
fi
|
|
||||||
|
|
||||||
cat <<End-of-message
|
|
||||||
apiVersion: v1
|
|
||||||
kind: Service
|
|
||||||
metadata:
|
|
||||||
name: ${NAME}
|
|
||||||
labels:
|
|
||||||
app: nginx
|
|
||||||
instance: ${NAME}
|
|
||||||
spec:
|
|
||||||
ports:
|
|
||||||
- port: 80
|
|
||||||
targetPort: 80
|
|
||||||
name: http
|
|
||||||
selector:
|
|
||||||
app: nginx
|
|
||||||
instance: ${NAME}
|
|
||||||
---
|
|
||||||
apiVersion: apps/v1
|
|
||||||
kind: Deployment
|
|
||||||
metadata:
|
|
||||||
name: ${NAME}
|
|
||||||
labels:
|
|
||||||
app: nginx
|
|
||||||
instance: ${NAME}
|
|
||||||
spec:
|
|
||||||
replicas: ${REPLICAS}
|
|
||||||
selector:
|
|
||||||
matchLabels:
|
|
||||||
app: nginx
|
|
||||||
instance: ${NAME}
|
|
||||||
template:
|
|
||||||
metadata:
|
|
||||||
labels:
|
|
||||||
app: nginx
|
|
||||||
instance: ${NAME}
|
|
||||||
spec:
|
|
||||||
containers:
|
|
||||||
- name: nginx
|
|
||||||
image: nginx:1.7.9
|
|
||||||
ports:
|
|
||||||
- containerPort: 80
|
|
||||||
End-of-message
|
|
||||||
|
|
||||||
#### ` + "`" + `Dockerfile` + "`" + `
|
|
||||||
|
|
||||||
` + "`" + `Dockerfile` + "`" + ` installs ` + "`" + `kustomize config` + "`" + ` and copies the script into the container image.
|
|
||||||
|
|
||||||
FROM golang:1.13-stretch
|
|
||||||
RUN go get sigs.k8s.io/kustomize/cmd/config
|
|
||||||
RUN mv /go/bin/config /usr/bin/config
|
|
||||||
COPY nginx-template.sh /usr/bin/nginx-template.sh
|
|
||||||
CMD ["nginx-template.sh]
|
|
||||||
|
|
||||||
### Example Function Usage
|
|
||||||
|
|
||||||
Following is an example of running the ` + "`" + `kustomize config run` + "`" + ` using the preceding API.
|
|
||||||
|
|
||||||
#### ` + "`" + `nginx.yaml` + "`" + ` (Input)
|
|
||||||
|
|
||||||
` + "`" + `dir/nginx.yaml` + "`" + ` contains a reference to the Function. The contents of ` + "`" + `nginx.yaml` + "`" + `
|
|
||||||
are passed to the Function through the ` + "`" + `ResourceList.functionConfig` + "`" + ` field.
|
|
||||||
|
|
||||||
apiVersion: example.com/v1beta1
|
|
||||||
kind: Nginx
|
|
||||||
metadata:
|
|
||||||
name: my-instance
|
|
||||||
annotations:
|
|
||||||
config.kubernetes.io/local-config: "true"
|
|
||||||
configFn:
|
|
||||||
container:
|
|
||||||
image: gcr.io/example-functions/nginx-template:v1.0.0
|
|
||||||
spec:
|
|
||||||
replicas: 5
|
|
||||||
|
|
||||||
- ` + "`" + `configFn.container.image` + "`" + `: the image to use for this API
|
|
||||||
- ` + "`" + `annotations[config.kubernetes.io/local-config]` + "`" + `: mark this as not a Resource that should
|
|
||||||
be applied
|
|
||||||
|
|
||||||
#### ` + "`" + `kustomize config run dir/` + "`" + ` (Output)
|
|
||||||
|
|
||||||
` + "`" + `dir/my-instance_deployment.yaml` + "`" + ` contains the Deployment:
|
|
||||||
|
|
||||||
apiVersion: apps/v1
|
|
||||||
kind: Deployment
|
|
||||||
metadata:
|
|
||||||
name: my-instance
|
|
||||||
labels:
|
|
||||||
app: nginx
|
|
||||||
instance: my-instance
|
|
||||||
spec:
|
|
||||||
replicas: 5
|
|
||||||
selector:
|
|
||||||
matchLabels:
|
|
||||||
app: nginx
|
|
||||||
instance: my-instance
|
|
||||||
template:
|
|
||||||
metadata:
|
|
||||||
labels:
|
|
||||||
app: nginx
|
|
||||||
instance: my-instance
|
|
||||||
spec:
|
|
||||||
containers:
|
|
||||||
- name: nginx
|
|
||||||
image: nginx:1.7.9
|
|
||||||
ports:
|
|
||||||
- containerPort: 80
|
|
||||||
|
|
||||||
` + "`" + `dir/my-instance_service.yaml` + "`" + ` contains the Service:
|
|
||||||
|
|
||||||
apiVersion: v1
|
|
||||||
kind: Service
|
|
||||||
metadata:
|
|
||||||
name: my-instance
|
|
||||||
labels:
|
|
||||||
app: nginx
|
|
||||||
instance: my-instance
|
|
||||||
spec:
|
|
||||||
ports:
|
|
||||||
- port: 80
|
|
||||||
targetPort: 80
|
|
||||||
name: http
|
|
||||||
selector:
|
|
||||||
app: nginx
|
|
||||||
instance: my-instance`
|
|
||||||
|
|
||||||
var ConfigIoLong = `# Configuration IO API Semantics
|
var ConfigIoLong = `# Configuration IO API Semantics
|
||||||
|
|
||||||
Resource Configuration may be read / written from / to sources such as directories,
|
Resource Configuration may be read / written from / to sources such as directories,
|
||||||
@@ -346,6 +64,355 @@ Example:
|
|||||||
annotations:
|
annotations:
|
||||||
config.kubernetes.io/local-config: "true"`
|
config.kubernetes.io/local-config: "true"`
|
||||||
|
|
||||||
|
var FunctionsImplShort = `Following is an example for implementing an nginx abstraction using a configuration`
|
||||||
|
var FunctionsImplLong = `# Running Configuration Functions using kustomize CLI
|
||||||
|
|
||||||
|
Configuration functions can be implemented using any toolchain and invoked using any
|
||||||
|
container workflow orchestrator including Tekton, Cloud Build, or run directly using ` + "`" + `docker run` + "`" + `.
|
||||||
|
|
||||||
|
Run ` + "`" + `config help docs-fn-spec` + "`" + ` to see the Configuration Functions Specification.
|
||||||
|
|
||||||
|
` + "`" + `kustomize config run` + "`" + ` is an example orchestrator for invoking Configuration Functions. This
|
||||||
|
document describes how to implement and invoke an example function.
|
||||||
|
|
||||||
|
function.
|
||||||
|
|
||||||
|
### ` + "`" + `nginx-template.sh` + "`" + `
|
||||||
|
|
||||||
|
` + "`" + `nginx-template.sh` + "`" + ` is a simple bash script which uses a _heredoc_ as a templating solution
|
||||||
|
for generating Resources from the functionConfig input fields.
|
||||||
|
|
||||||
|
The script wraps itself using ` + "`" + `config run wrap -- $0` + "`" + ` which will:
|
||||||
|
|
||||||
|
1. Parse the ` + "`" + `ResourceList.functionConfig` + "`" + ` (provided to the container stdin) into env vars
|
||||||
|
2. Merge the stdout into the original list of Resources
|
||||||
|
3. Defaults filenames for newly generated Resources (if they are not set as annotations)
|
||||||
|
to ` + "`" + `config/NAME_KIND.yaml` + "`" + `
|
||||||
|
4. Format the output
|
||||||
|
|
||||||
|
#!/bin/bash
|
||||||
|
# script must run wrapped by "kustomize config run wrap"
|
||||||
|
# for parsing input the functionConfig into env vars
|
||||||
|
if [ -z ${WRAPPED} ]; then
|
||||||
|
export WRAPPED=true
|
||||||
|
config run wrap -- $0
|
||||||
|
exit $?
|
||||||
|
fi
|
||||||
|
|
||||||
|
cat <<End-of-message
|
||||||
|
apiVersion: v1
|
||||||
|
kind: Service
|
||||||
|
metadata:
|
||||||
|
name: ${NAME}
|
||||||
|
labels:
|
||||||
|
app: nginx
|
||||||
|
instance: ${NAME}
|
||||||
|
spec:
|
||||||
|
ports:
|
||||||
|
- port: 80
|
||||||
|
targetPort: 80
|
||||||
|
name: http
|
||||||
|
selector:
|
||||||
|
app: nginx
|
||||||
|
instance: ${NAME}
|
||||||
|
---
|
||||||
|
apiVersion: apps/v1
|
||||||
|
kind: Deployment
|
||||||
|
metadata:
|
||||||
|
name: ${NAME}
|
||||||
|
labels:
|
||||||
|
app: nginx
|
||||||
|
instance: ${NAME}
|
||||||
|
spec:
|
||||||
|
replicas: ${REPLICAS}
|
||||||
|
selector:
|
||||||
|
matchLabels:
|
||||||
|
app: nginx
|
||||||
|
instance: ${NAME}
|
||||||
|
template:
|
||||||
|
metadata:
|
||||||
|
labels:
|
||||||
|
app: nginx
|
||||||
|
instance: ${NAME}
|
||||||
|
spec:
|
||||||
|
containers:
|
||||||
|
- name: nginx
|
||||||
|
image: nginx:1.7.9
|
||||||
|
ports:
|
||||||
|
- containerPort: 80
|
||||||
|
End-of-message
|
||||||
|
|
||||||
|
### Dockerfile
|
||||||
|
|
||||||
|
` + "`" + `Dockerfile` + "`" + ` installs ` + "`" + `kustomize config` + "`" + ` and copies the script into the container image.
|
||||||
|
|
||||||
|
FROM golang:1.13-stretch
|
||||||
|
RUN go get sigs.k8s.io/kustomize/cmd/config
|
||||||
|
RUN mv /go/bin/config /usr/bin/config
|
||||||
|
COPY nginx-template.sh /usr/bin/nginx-template.sh
|
||||||
|
CMD ["nginx-template.sh]
|
||||||
|
|
||||||
|
## Example Function Usage
|
||||||
|
|
||||||
|
Following is an example of running the ` + "`" + `kustomize config run` + "`" + ` using the preceding API.
|
||||||
|
|
||||||
|
When run by ` + "`" + `kustomize config run` + "`" + `, functions are run in containers with the
|
||||||
|
following environment:
|
||||||
|
|
||||||
|
- Network: ` + "`" + `none` + "`" + `
|
||||||
|
- User: ` + "`" + `nobody` + "`" + `
|
||||||
|
- Security Options: ` + "`" + `no-new-privileges` + "`" + `
|
||||||
|
- Volumes: the volume containing the ` + "`" + `functionConfig` + "`" + ` yaml is mounted under ` + "`" + `/local` + "`" + ` as ` + "`" + `ro` + "`" + `
|
||||||
|
|
||||||
|
### Input
|
||||||
|
|
||||||
|
` + "`" + `dir/nginx.yaml` + "`" + ` contains a reference to the Function. The contents of ` + "`" + `nginx.yaml` + "`" + `
|
||||||
|
are passed to the Function through the ` + "`" + `ResourceList.functionConfig` + "`" + ` field.
|
||||||
|
|
||||||
|
apiVersion: example.com/v1beta1
|
||||||
|
kind: Nginx
|
||||||
|
metadata:
|
||||||
|
name: my-instance
|
||||||
|
annotations:
|
||||||
|
config.kubernetes.io/local-config: "true"
|
||||||
|
config.k8s.io/function: |
|
||||||
|
container:
|
||||||
|
image: gcr.io/example-functions/nginx-template:v1.0.0
|
||||||
|
spec:
|
||||||
|
replicas: 5
|
||||||
|
|
||||||
|
- ` + "`" + `annotations[config.k8s.io/function].container.image` + "`" + `: the image to use for this API
|
||||||
|
- ` + "`" + `annotations[config.kubernetes.io/local-config]` + "`" + `: mark this as not a Resource that should
|
||||||
|
be applied
|
||||||
|
|
||||||
|
### Output
|
||||||
|
|
||||||
|
The function is invoked using by runing ` + "`" + `kustomize config run dir/` + "`" + `.
|
||||||
|
|
||||||
|
` + "`" + `dir/my-instance_deployment.yaml` + "`" + ` contains the Deployment:
|
||||||
|
|
||||||
|
apiVersion: apps/v1
|
||||||
|
kind: Deployment
|
||||||
|
metadata:
|
||||||
|
name: my-instance
|
||||||
|
labels:
|
||||||
|
app: nginx
|
||||||
|
instance: my-instance
|
||||||
|
spec:
|
||||||
|
replicas: 5
|
||||||
|
selector:
|
||||||
|
matchLabels:
|
||||||
|
app: nginx
|
||||||
|
instance: my-instance
|
||||||
|
template:
|
||||||
|
metadata:
|
||||||
|
labels:
|
||||||
|
app: nginx
|
||||||
|
instance: my-instance
|
||||||
|
spec:
|
||||||
|
containers:
|
||||||
|
- name: nginx
|
||||||
|
image: nginx:1.7.9
|
||||||
|
ports:
|
||||||
|
- containerPort: 80
|
||||||
|
|
||||||
|
` + "`" + `dir/my-instance_service.yaml` + "`" + ` contains the Service:
|
||||||
|
|
||||||
|
apiVersion: v1
|
||||||
|
kind: Service
|
||||||
|
metadata:
|
||||||
|
name: my-instance
|
||||||
|
labels:
|
||||||
|
app: nginx
|
||||||
|
instance: my-instance
|
||||||
|
spec:
|
||||||
|
ports:
|
||||||
|
- port: 80
|
||||||
|
targetPort: 80
|
||||||
|
name: http
|
||||||
|
selector:
|
||||||
|
app: nginx
|
||||||
|
instance: my-instance`
|
||||||
|
|
||||||
|
var FunctionsSpecShort = `_Configuration functions_ enable shift-left practices (client-side) through:`
|
||||||
|
var FunctionsSpecLong = `# Configuration Functions Specification
|
||||||
|
|
||||||
|
This document specifies a standard for client-side functions that operate on
|
||||||
|
Kubernetes declarative configurations. This standard enables creating
|
||||||
|
small, interoperable, and language-independent executable programs packaged as
|
||||||
|
containers that can be chained together as part of a configuration management pipeline.
|
||||||
|
The end result of such a pipeline are fully rendered configurations that can then be
|
||||||
|
applied to a control plane (e.g. Using ‘kubectl apply’ for Kubernetes control plane).
|
||||||
|
As such, although this document references Kubernetes Resource Model and API conventions,
|
||||||
|
it is completely decoupled from Kuberentes API machinery and does not depend on any
|
||||||
|
in-cluster components.
|
||||||
|
|
||||||
|
This document references terms described in [Kubernetes API Conventions][1].
|
||||||
|
|
||||||
|
The key words "MUST", "MUST NOT", "REQUIRED", "SHALL", "SHALL NOT", "SHOULD",
|
||||||
|
"SHOULD NOT", "RECOMMENDED", "MAY", and "OPTIONAL" in this document are to be
|
||||||
|
interpreted as described in [RFC 2119][2].
|
||||||
|
|
||||||
|
|
||||||
|
- Pre-commit / delivery validation and linting of configuration
|
||||||
|
- e.g. Fail if any containers don't have PodSecurityPolicy or CPU / Memory limits
|
||||||
|
- Implementation of abstractions as client actuated APIs (e.g. templating)
|
||||||
|
- e.g. Create a client-side _"CRD"_ for generating configuration checked into git
|
||||||
|
- Aspect Orient configuration / Injection of cross-cutting configuration
|
||||||
|
- e.g. T-Shirt size containers by annotating Resources with ` + "`" + `small` + "`" + `, ` + "`" + `medium` + "`" + `, ` + "`" + `large` + "`" + `
|
||||||
|
and inject the cpu and memory resources into containers accordingly.
|
||||||
|
- e.g. Inject ` + "`" + `init` + "`" + ` and ` + "`" + `side-car` + "`" + ` containers into Resources based off of Resource
|
||||||
|
Type, annotations, etc.
|
||||||
|
|
||||||
|
Performing these on the client rather than the server enables:
|
||||||
|
|
||||||
|
- Configuration to be reviewed prior to being sent to the API server
|
||||||
|
- Configuration to be validated as part of the CI?CD pipeline
|
||||||
|
- Configuration for Resources to validated holistically rather than individually
|
||||||
|
per-Resource
|
||||||
|
- e.g. ensure the ` + "`" + `Service.selector` + "`" + ` and ` + "`" + `Deployment.spec.template` + "`" + ` labels
|
||||||
|
match.
|
||||||
|
- e.g. MutatingWebHooks are scoped to a single Resource instance at a time.
|
||||||
|
- Low-level tweaks to the output of high-level abstractions
|
||||||
|
- e.g. add an ` + "`" + `init container` + "`" + ` to a client _"CRD"_ Resource after it was generated.
|
||||||
|
- Composition and layering of multiple functions together
|
||||||
|
- Compose generation, injection, validation together
|
||||||
|
|
||||||
|
## Spec
|
||||||
|
|
||||||
|
### Input Type
|
||||||
|
|
||||||
|
A function MUST accept as input a single [Kubernetes List type][3].
|
||||||
|
The ` + "`" + `items` + "`" + ` field in the input will contain a sequence of [Object types][3].
|
||||||
|
A function MAY not support [Simple types][3] and List types.
|
||||||
|
|
||||||
|
An example using ` + "`" + `v1/ConfigMapList` + "`" + ` as input:
|
||||||
|
|
||||||
|
apiVersion: v1
|
||||||
|
kind: ConfigMapList
|
||||||
|
items:
|
||||||
|
- apiVersion: v1
|
||||||
|
kind: ConfigMap
|
||||||
|
metadata:
|
||||||
|
name: config1
|
||||||
|
data:
|
||||||
|
p1: v1
|
||||||
|
p2: v2
|
||||||
|
- apiVersion: v1
|
||||||
|
kind: ConfigMap
|
||||||
|
metadata:
|
||||||
|
name: config2
|
||||||
|
|
||||||
|
An example using ` + "`" + `v1/List` + "`" + ` as input:
|
||||||
|
|
||||||
|
apiVersion: v1
|
||||||
|
kind: List
|
||||||
|
items:
|
||||||
|
spec:
|
||||||
|
- apiVersion: foo-corp.com/v1
|
||||||
|
kind: FulfillmentCenter
|
||||||
|
metadata:
|
||||||
|
name: staging
|
||||||
|
address: "100 Main St."
|
||||||
|
- apiVersion: rbac.authorization.k8s.io/v1
|
||||||
|
kind: ClusterRole
|
||||||
|
metadata:
|
||||||
|
name: namespace-reader
|
||||||
|
rules:
|
||||||
|
- resources:
|
||||||
|
- namespaces
|
||||||
|
apiGroups:
|
||||||
|
- ""
|
||||||
|
verbs:
|
||||||
|
- get
|
||||||
|
- watch
|
||||||
|
- list
|
||||||
|
|
||||||
|
In addition, a function MUST accept as input a List of kind ` + "`" + `ResourceList` + "`" + ` where the
|
||||||
|
` + "`" + `functionConfig` + "`" + ` field, if present, will contain the invocation-specific configuration passed to the function
|
||||||
|
by the orchestrator.
|
||||||
|
Functions MAY consider this field optional so that they can be triggered in an ad-hoc fashion.
|
||||||
|
|
||||||
|
An example using ` + "`" + `config.kubernetes.io/v1beta1/ResourceList` + "`" + ` as input:
|
||||||
|
|
||||||
|
apiVersion: config.kubernetes.io/v1beta1
|
||||||
|
kind: ResourceList
|
||||||
|
functionConfig:
|
||||||
|
apiVersion: foo-corp.com/v1
|
||||||
|
kind: FulfillmentCenter
|
||||||
|
metadata:
|
||||||
|
name: staging
|
||||||
|
metadata:
|
||||||
|
annotations:
|
||||||
|
config.k8s.io/function: |
|
||||||
|
container:
|
||||||
|
image: gcr.io/example/foo:v1.0.0
|
||||||
|
spec:
|
||||||
|
address: "100 Main St."
|
||||||
|
items:
|
||||||
|
- apiVersion: rbac.authorization.k8s.io/v1
|
||||||
|
kind: ClusterRole
|
||||||
|
metadata:
|
||||||
|
name: namespace-reader
|
||||||
|
rules:
|
||||||
|
- resources:
|
||||||
|
- namespaces
|
||||||
|
apiGroups:
|
||||||
|
- ""
|
||||||
|
verbs:
|
||||||
|
- get
|
||||||
|
- watch
|
||||||
|
- list
|
||||||
|
|
||||||
|
Here ` + "`" + `FulfillmentCenter` + "`" + ` kind with name ` + "`" + `staging` + "`" + ` is passed as the invocation-specific configuration
|
||||||
|
to the function.
|
||||||
|
|
||||||
|
### Output Type
|
||||||
|
|
||||||
|
A function’s output MUST be the same as the input specification above
|
||||||
|
-- i.e. ` + "`" + `ResourceList` + "`" + ` or ` + "`" + `List` + "`" + `.
|
||||||
|
This is necessary to enable chaining two or more functions together in a pipeline.
|
||||||
|
The serialization format of the output SHOULD match that of its input on each invocation
|
||||||
|
-- e.g. if the input was a ` + "`" + `ResourceList` + "`" + `, the output should also be a ` + "`" + `ResourceList` + "`" + `.
|
||||||
|
|
||||||
|
### Serialization Format
|
||||||
|
|
||||||
|
A function MUST support YAML as a serialization format for the input and output.
|
||||||
|
A function MUST use utf8 encoding (as YAML is a superset of JSON, JSON will also be supported
|
||||||
|
by any conforming function).
|
||||||
|
|
||||||
|
### Operations
|
||||||
|
|
||||||
|
A function MAY Create, Update, or Delete any number of items in the ` + "`" + `items` + "`" + ` field and output the
|
||||||
|
resultant list.
|
||||||
|
|
||||||
|
A function MAY modify annotations with prefix ` + "`" + `config.kubernetes.io` + "`" + `, but must be careful about
|
||||||
|
doing so since they’re used for orchestration purposes and will likely impact subsequent functions
|
||||||
|
in the pipeline.
|
||||||
|
|
||||||
|
A function SHOULD preserve comments when input serialization format is YAML.
|
||||||
|
This allows for human authoring of configuration to coexist with changes made by functions.
|
||||||
|
|
||||||
|
### Containerization
|
||||||
|
|
||||||
|
A function MUST be implemented as a container.
|
||||||
|
|
||||||
|
A function container MUST be capable of running as a non-root user if it does not require
|
||||||
|
access to host filesystem or makes network calls.
|
||||||
|
|
||||||
|
### stdin/stdout/stderr and Exit Codes
|
||||||
|
|
||||||
|
A function MUST accept input from stdin and emit output to stdout.
|
||||||
|
|
||||||
|
Any error messages MUST be emitted to stderr.
|
||||||
|
|
||||||
|
An exit code of zero indicates function execution was successful.
|
||||||
|
A non-zero exit code indicates a failure.
|
||||||
|
|
||||||
|
[1]: https://github.com/kubernetes/community/blob/master/contributors/devel/sig-architecture/api-conventions.md
|
||||||
|
[2]: https://tools.ietf.org/html/rfc2119
|
||||||
|
[3]: https://github.com/kubernetes/community/blob/master/contributors/devel/sig-architecture/api-conventions.md#types-kinds`
|
||||||
|
|
||||||
var Merge2Long = `# Merge (2-way)
|
var Merge2Long = `# Merge (2-way)
|
||||||
|
|
||||||
2-way merges fields from a source to a destination, overriding the destination fields
|
2-way merges fields from a source to a destination, overriding the destination fields
|
||||||
|
|||||||
@@ -142,6 +142,25 @@ var GrepExamples = `
|
|||||||
# look for Resources matching a specific container image
|
# look for Resources matching a specific container image
|
||||||
kustomize config grep "spec.template.spec.containers[name=nginx].image=nginx:1\.7\.9" my-dir/ | kustomize config tree`
|
kustomize config grep "spec.template.spec.containers[name=nginx].image=nginx:1\.7\.9" my-dir/ | kustomize config tree`
|
||||||
|
|
||||||
|
var ListSettersShort = `[Alpha] List setters for Resources.`
|
||||||
|
var ListSettersLong = `
|
||||||
|
List setters for Resources.
|
||||||
|
|
||||||
|
DIR
|
||||||
|
|
||||||
|
A directory containing Resource configuration.
|
||||||
|
|
||||||
|
NAME
|
||||||
|
|
||||||
|
Optional. The name of the setter to display.
|
||||||
|
`
|
||||||
|
var ListSettersExamples = `
|
||||||
|
Show setters:
|
||||||
|
|
||||||
|
$ config set DIR/
|
||||||
|
NAME DESCRIPTION VALUE TYPE COUNT SETBY
|
||||||
|
name-prefix '' PREFIX string 2`
|
||||||
|
|
||||||
var MergeShort = `[Alpha] Merge Resource configuration files`
|
var MergeShort = `[Alpha] Merge Resource configuration files`
|
||||||
var MergeLong = `
|
var MergeLong = `
|
||||||
[Alpha] Merge Resource configuration files
|
[Alpha] Merge Resource configuration files
|
||||||
@@ -201,8 +220,8 @@ order they appear in the file).
|
|||||||
|
|
||||||
#### Config Functions:
|
#### Config Functions:
|
||||||
|
|
||||||
Config functions are specified as Kubernetes types containing a metadata.configFn.container.image
|
Config functions are specified as Kubernetes types containing a metadata.annotations.[config.kubernetes.io/function]
|
||||||
field. This field tells run how to invoke the container.
|
field specifying an image for the container to run. This image tells run how to invoke the container.
|
||||||
|
|
||||||
Example config function:
|
Example config function:
|
||||||
|
|
||||||
@@ -210,17 +229,17 @@ order they appear in the file).
|
|||||||
apiVersion: fn.example.com/v1beta1
|
apiVersion: fn.example.com/v1beta1
|
||||||
kind: ExampleFunctionKind
|
kind: ExampleFunctionKind
|
||||||
metadata:
|
metadata:
|
||||||
configFn:
|
|
||||||
container:
|
|
||||||
# function is invoked as a container running this image
|
|
||||||
image: gcr.io/example/examplefunction:v1.0.1
|
|
||||||
annotations:
|
annotations:
|
||||||
|
config.kubernetes.io/function: |
|
||||||
|
container:
|
||||||
|
# function is invoked as a container running this image
|
||||||
|
image: gcr.io/example/examplefunction:v1.0.1
|
||||||
config.kubernetes.io/local-config: "true" # tools should ignore this
|
config.kubernetes.io/local-config: "true" # tools should ignore this
|
||||||
spec:
|
spec:
|
||||||
configField: configValue
|
configField: configValue
|
||||||
|
|
||||||
In the preceding example, 'kustomize config run example/' would identify the function by
|
In the preceding example, 'kustomize config run example/' would identify the function by
|
||||||
the metadata.configFn field. It would then write all Resources in the directory to
|
the metadata.annotations.[config.kubernetes.io/function] field. It would then write all Resources in the directory to
|
||||||
a container stdin (running the gcr.io/example/examplefunction:v1.0.1 image). It
|
a container stdin (running the gcr.io/example/examplefunction:v1.0.1 image). It
|
||||||
would then write the container stdout back to example/, replacing the directory
|
would then write the container stdout back to example/, replacing the directory
|
||||||
file contents.
|
file contents.
|
||||||
@@ -286,19 +305,19 @@ var SetExamples = `
|
|||||||
List setters: Show the possible setters
|
List setters: Show the possible setters
|
||||||
|
|
||||||
$ config set DIR/
|
$ config set DIR/
|
||||||
NAME DESCRIPTION VALUE TYPE COUNT OWNER
|
NAME DESCRIPTION VALUE TYPE COUNT SETBY
|
||||||
name-prefix '' PREFIX string 2
|
name-prefix '' PREFIX string 2
|
||||||
|
|
||||||
Perform substitution: set a new value, owner and description
|
Perform set: set a new value, owner and description
|
||||||
|
|
||||||
$ kustomize config set DIR/ name-prefix "test" --description "test environment" --set-by "dev"
|
$ kustomize config set DIR/ name-prefix "test" --description "test environment" --set-by "dev"
|
||||||
performed 2 substitutions
|
set 2 values
|
||||||
|
|
||||||
Show substitutions: Show the new values
|
List setters: Show the new values
|
||||||
|
|
||||||
$ config set dir
|
$ config set DIR/
|
||||||
NAME DESCRIPTION VALUE TYPE COUNT SUBSTITUTED OWNER
|
NAME DESCRIPTION VALUE TYPE COUNT SETBY
|
||||||
prefix 'test environment' test string 2 true dev
|
name-prefix 'test environment' test string 2 dev
|
||||||
|
|
||||||
New Resource YAML:
|
New Resource YAML:
|
||||||
|
|
||||||
@@ -313,6 +332,37 @@ var SetExamples = `
|
|||||||
name: test-app2 # {"description":"test environment","type":"string","x-kustomize":{"setBy":"dev","partialFieldSetters":[{"name":"name-prefix","value":"test"}]}}
|
name: test-app2 # {"description":"test environment","type":"string","x-kustomize":{"setBy":"dev","partialFieldSetters":[{"name":"name-prefix","value":"test"}]}}
|
||||||
...`
|
...`
|
||||||
|
|
||||||
|
var SinkShort = `[Alpha] Implement a Sink by writing input to a local directory.`
|
||||||
|
var SinkLong = `
|
||||||
|
[Alpha] Implement a Sink by writing input to a local directory.
|
||||||
|
|
||||||
|
kustomize config sink DIR
|
||||||
|
|
||||||
|
DIR:
|
||||||
|
Path to local directory.
|
||||||
|
|
||||||
|
` + "`" + `sink` + "`" + ` writes its input to a directory
|
||||||
|
`
|
||||||
|
var SinkExamples = `
|
||||||
|
kustomize config source DIR/ | your-function | kustomize config sink DIR/`
|
||||||
|
|
||||||
|
var SourceShort = `[Alpha] Implement a Source by reading a local directory.`
|
||||||
|
var SourceLong = `
|
||||||
|
[Alpha] Implement a Source by reading a local directory.
|
||||||
|
|
||||||
|
kustomize config source DIR
|
||||||
|
|
||||||
|
DIR:
|
||||||
|
Path to local directory.
|
||||||
|
|
||||||
|
` + "`" + `source` + "`" + ` emits configuration to act as input to a function
|
||||||
|
`
|
||||||
|
var SourceExamples = `
|
||||||
|
# emity configuration directory as input source to a function
|
||||||
|
kustomize config source DIR/
|
||||||
|
|
||||||
|
kustomize config source DIR/ | your-function | kustomize config sink DIR/`
|
||||||
|
|
||||||
var TreeShort = `[Alpha] Display Resource structure from a directory or stdin.`
|
var TreeShort = `[Alpha] Display Resource structure from a directory or stdin.`
|
||||||
var TreeLong = `
|
var TreeLong = `
|
||||||
[Alpha] Display Resource structure from a directory or stdin.
|
[Alpha] Display Resource structure from a directory or stdin.
|
||||||
|
|||||||
@@ -9,6 +9,7 @@ package main
|
|||||||
import (
|
import (
|
||||||
"os"
|
"os"
|
||||||
|
|
||||||
|
"sigs.k8s.io/kustomize/cmd/config/complete"
|
||||||
"sigs.k8s.io/kustomize/cmd/config/configcobra"
|
"sigs.k8s.io/kustomize/cmd/config/configcobra"
|
||||||
"sigs.k8s.io/kustomize/kyaml/commandutil"
|
"sigs.k8s.io/kustomize/kyaml/commandutil"
|
||||||
)
|
)
|
||||||
@@ -16,7 +17,10 @@ import (
|
|||||||
func main() {
|
func main() {
|
||||||
// enable the config commands
|
// enable the config commands
|
||||||
os.Setenv(commandutil.EnableAlphaCommmandsEnvName, "true")
|
os.Setenv(commandutil.EnableAlphaCommmandsEnvName, "true")
|
||||||
if err := configcobra.NewConfigCommand("").Execute(); err != nil {
|
cmd := configcobra.NewConfigCommand("")
|
||||||
|
complete.Complete(cmd).Complete("config")
|
||||||
|
|
||||||
|
if err := cmd.Execute(); err != nil {
|
||||||
os.Exit(1)
|
os.Exit(1)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -4,6 +4,7 @@ go 1.13
|
|||||||
|
|
||||||
require (
|
require (
|
||||||
github.com/spf13/cobra v0.0.5
|
github.com/spf13/cobra v0.0.5
|
||||||
|
k8s.io/apimachinery v0.17.0
|
||||||
k8s.io/cli-runtime v0.17.0
|
k8s.io/cli-runtime v0.17.0
|
||||||
k8s.io/client-go v0.17.0
|
k8s.io/client-go v0.17.0
|
||||||
k8s.io/component-base v0.17.0 // indirect
|
k8s.io/component-base v0.17.0 // indirect
|
||||||
|
|||||||
@@ -85,6 +85,7 @@ github.com/go-openapi/jsonreference v0.19.3/go.mod h1:rjx6GuL8TTa9VaixXglHmQmIL9
|
|||||||
github.com/go-openapi/spec v0.0.0-20160808142527-6aced65f8501/go.mod h1:J8+jY1nAiCcj+friV/PDoE1/3eeccG9LYBs0tYvLOWc=
|
github.com/go-openapi/spec v0.0.0-20160808142527-6aced65f8501/go.mod h1:J8+jY1nAiCcj+friV/PDoE1/3eeccG9LYBs0tYvLOWc=
|
||||||
github.com/go-openapi/spec v0.19.3 h1:0XRyw8kguri6Yw4SxhsQA/atC88yqrk0+G4YhI2wabc=
|
github.com/go-openapi/spec v0.19.3 h1:0XRyw8kguri6Yw4SxhsQA/atC88yqrk0+G4YhI2wabc=
|
||||||
github.com/go-openapi/spec v0.19.3/go.mod h1:FpwSN1ksY1eteniUU7X0N/BgJ7a4WvBFVA8Lj9mJglo=
|
github.com/go-openapi/spec v0.19.3/go.mod h1:FpwSN1ksY1eteniUU7X0N/BgJ7a4WvBFVA8Lj9mJglo=
|
||||||
|
github.com/go-openapi/spec v0.19.5 h1:Xm0Ao53uqnk9QE/LlYV5DEU09UAgpliA85QoT9LzqPw=
|
||||||
github.com/go-openapi/spec v0.19.5/go.mod h1:Hm2Jr4jv8G1ciIAo+frC/Ft+rR2kQDh8JHKHb3gWUSk=
|
github.com/go-openapi/spec v0.19.5/go.mod h1:Hm2Jr4jv8G1ciIAo+frC/Ft+rR2kQDh8JHKHb3gWUSk=
|
||||||
github.com/go-openapi/swag v0.0.0-20160704191624-1d0bd113de87/go.mod h1:DXUve3Dpr1UfpPtxFw+EFuQ41HhCWZfha5jSVRG7C7I=
|
github.com/go-openapi/swag v0.0.0-20160704191624-1d0bd113de87/go.mod h1:DXUve3Dpr1UfpPtxFw+EFuQ41HhCWZfha5jSVRG7C7I=
|
||||||
github.com/go-openapi/swag v0.19.2 h1:jvO6bCMBEilGwMfHhrd61zIID4oIFdwb76V17SM88dE=
|
github.com/go-openapi/swag v0.19.2 h1:jvO6bCMBEilGwMfHhrd61zIID4oIFdwb76V17SM88dE=
|
||||||
@@ -212,6 +213,7 @@ github.com/prometheus/procfs v0.0.2/go.mod h1:TjEm7ze935MbeOT/UhFTIMYKhuLP4wbCsT
|
|||||||
github.com/remyoudompheng/bigfft v0.0.0-20170806203942-52369c62f446/go.mod h1:uYEyJGbgTkfkS4+E/PavXkNJcbFIpEtjt2B0KDQ5+9M=
|
github.com/remyoudompheng/bigfft v0.0.0-20170806203942-52369c62f446/go.mod h1:uYEyJGbgTkfkS4+E/PavXkNJcbFIpEtjt2B0KDQ5+9M=
|
||||||
github.com/russross/blackfriday v1.5.2 h1:HyvC0ARfnZBqnXwABFeSZHpKvJHJJfPz81GNueLj0oo=
|
github.com/russross/blackfriday v1.5.2 h1:HyvC0ARfnZBqnXwABFeSZHpKvJHJJfPz81GNueLj0oo=
|
||||||
github.com/russross/blackfriday v1.5.2/go.mod h1:JO/DiYxRf+HjHt06OyowR9PTA263kcR/rfWxYHBV53g=
|
github.com/russross/blackfriday v1.5.2/go.mod h1:JO/DiYxRf+HjHt06OyowR9PTA263kcR/rfWxYHBV53g=
|
||||||
|
github.com/sergi/go-diff v1.1.0/go.mod h1:STckp+ISIX8hZLjrqAeVduY0gWCT9IjLuqbuNXdaHfM=
|
||||||
github.com/sirupsen/logrus v1.2.0/go.mod h1:LxeOpSwHxABJmUn/MG1IvRgCAasNZTLOkJPxbbu5VWo=
|
github.com/sirupsen/logrus v1.2.0/go.mod h1:LxeOpSwHxABJmUn/MG1IvRgCAasNZTLOkJPxbbu5VWo=
|
||||||
github.com/sirupsen/logrus v1.4.2 h1:SPIRibHv4MatM3XXNO2BJeFLZwZ2LvZgfQ5+UNI2im4=
|
github.com/sirupsen/logrus v1.4.2 h1:SPIRibHv4MatM3XXNO2BJeFLZwZ2LvZgfQ5+UNI2im4=
|
||||||
github.com/sirupsen/logrus v1.4.2/go.mod h1:tLMulIdttU9McNUspp0xgXVQah82FyeX6MwdIuYE2rE=
|
github.com/sirupsen/logrus v1.4.2/go.mod h1:tLMulIdttU9McNUspp0xgXVQah82FyeX6MwdIuYE2rE=
|
||||||
@@ -330,6 +332,8 @@ gopkg.in/alecthomas/kingpin.v2 v2.2.6/go.mod h1:FMv+mEhP44yOT+4EoQTLFTRgOQ1FBLks
|
|||||||
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
|
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
|
||||||
gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127 h1:qIbj1fsPNlZgppZ+VLlY7N33q108Sa+fhmuc+sWQYwY=
|
gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127 h1:qIbj1fsPNlZgppZ+VLlY7N33q108Sa+fhmuc+sWQYwY=
|
||||||
gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
|
gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
|
||||||
|
gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15 h1:YR8cESwS4TdDjEe65xsg0ogRM/Nc3DYOhEAlW+xobZo=
|
||||||
|
gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
|
||||||
gopkg.in/fsnotify.v1 v1.4.7 h1:xOHLXZwVvI9hhs+cLKq5+I5onOuwQLhQwiu63xxlHs4=
|
gopkg.in/fsnotify.v1 v1.4.7 h1:xOHLXZwVvI9hhs+cLKq5+I5onOuwQLhQwiu63xxlHs4=
|
||||||
gopkg.in/fsnotify.v1 v1.4.7/go.mod h1:Tz8NjZHkW78fSQdbUxIjBTcgA1z1m8ZHf0WmKUhAMys=
|
gopkg.in/fsnotify.v1 v1.4.7/go.mod h1:Tz8NjZHkW78fSQdbUxIjBTcgA1z1m8ZHf0WmKUhAMys=
|
||||||
gopkg.in/inf.v0 v0.9.1 h1:73M5CoZyi3ZLMOyDlQh031Cx6N9NDJ2Vvfl76EDAgDc=
|
gopkg.in/inf.v0 v0.9.1 h1:73M5CoZyi3ZLMOyDlQh031Cx6N9NDJ2Vvfl76EDAgDc=
|
||||||
|
|||||||
@@ -17,9 +17,6 @@ import (
|
|||||||
cmdutil "k8s.io/kubectl/pkg/cmd/util"
|
cmdutil "k8s.io/kubectl/pkg/cmd/util"
|
||||||
"k8s.io/kubectl/pkg/util/i18n"
|
"k8s.io/kubectl/pkg/util/i18n"
|
||||||
"sigs.k8s.io/kustomize/kyaml/commandutil"
|
"sigs.k8s.io/kustomize/kyaml/commandutil"
|
||||||
|
|
||||||
// initialize auth
|
|
||||||
_ "k8s.io/client-go/plugin/pkg/client/auth"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
// GetCommand returns a command from kubectl to install
|
// GetCommand returns a command from kubectl to install
|
||||||
|
|||||||
145
cmd/kubectl/kubectlcobra/grouping.go
Normal file
145
cmd/kubectl/kubectlcobra/grouping.go
Normal file
@@ -0,0 +1,145 @@
|
|||||||
|
// Copyright 2020 The Kubernetes Authors.
|
||||||
|
// SPDX-License-Identifier: Apache-2.0
|
||||||
|
|
||||||
|
// package kubectlcobra contains cobra commands from kubectl
|
||||||
|
package kubectlcobra
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
|
||||||
|
"k8s.io/apimachinery/pkg/api/meta"
|
||||||
|
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
|
||||||
|
"k8s.io/apimachinery/pkg/runtime"
|
||||||
|
"k8s.io/cli-runtime/pkg/resource"
|
||||||
|
)
|
||||||
|
|
||||||
|
const GroupingLabel = "kustomize.k8s.io/group-id"
|
||||||
|
|
||||||
|
// isGroupingObject returns true if the passed object has the
|
||||||
|
// grouping label.
|
||||||
|
// TODO(seans3): Check type is ConfigMap.
|
||||||
|
func isGroupingObject(obj runtime.Object) bool {
|
||||||
|
if obj == nil {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
accessor, err := meta.Accessor(obj)
|
||||||
|
if err == nil {
|
||||||
|
labels := accessor.GetLabels()
|
||||||
|
_, exists := labels[GroupingLabel]
|
||||||
|
if exists {
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
|
// findGroupingObject returns the "Grouping" object (ConfigMap with
|
||||||
|
// grouping label) if it exists, and a boolean describing if it was found.
|
||||||
|
func findGroupingObject(infos []*resource.Info) (*resource.Info, bool) {
|
||||||
|
for _, info := range infos {
|
||||||
|
if info != nil && isGroupingObject(info.Object) {
|
||||||
|
return info, true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return nil, false
|
||||||
|
}
|
||||||
|
|
||||||
|
// sortGroupingObject reorders the infos slice to place the grouping
|
||||||
|
// object in the first position. Returns true if grouping object found,
|
||||||
|
// false otherwise.
|
||||||
|
func sortGroupingObject(infos []*resource.Info) bool {
|
||||||
|
for i, info := range infos {
|
||||||
|
if info != nil && isGroupingObject(info.Object) {
|
||||||
|
// If the grouping object is not already in the first position,
|
||||||
|
// swap the grouping object with the first object.
|
||||||
|
if i > 0 {
|
||||||
|
infos[0], infos[i] = infos[i], infos[0]
|
||||||
|
}
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
|
// Adds the inventory of all objects (passed as infos) to the
|
||||||
|
// grouping object. Returns an error if a grouping object does not
|
||||||
|
// exist, or we are unable to successfully add the inventory to
|
||||||
|
// the grouping object; nil otherwise. Each object is in
|
||||||
|
// unstructured.Unstructured format.
|
||||||
|
func addInventoryToGroupingObj(infos []*resource.Info) error {
|
||||||
|
|
||||||
|
// Iterate through the objects (infos), creating an Inventory struct
|
||||||
|
// as metadata for the object, or if it's the grouping object, store it.
|
||||||
|
var groupingObj *unstructured.Unstructured
|
||||||
|
inventoryMap := map[string]string{}
|
||||||
|
for _, info := range infos {
|
||||||
|
obj := info.Object
|
||||||
|
if isGroupingObject(obj) {
|
||||||
|
// If we have more than one grouping object--error.
|
||||||
|
if groupingObj != nil {
|
||||||
|
return fmt.Errorf("Error--applying more than one grouping object.")
|
||||||
|
}
|
||||||
|
var ok bool
|
||||||
|
groupingObj, ok = obj.(*unstructured.Unstructured)
|
||||||
|
if !ok {
|
||||||
|
return fmt.Errorf("Grouping object is not an Unstructured: %#v", groupingObj)
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
if obj == nil {
|
||||||
|
return fmt.Errorf("Creating inventory; object is nil")
|
||||||
|
}
|
||||||
|
gk := obj.GetObjectKind().GroupVersionKind().GroupKind()
|
||||||
|
inventory, err := createInventory(info.Namespace, info.Name, gk)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
inventoryMap[inventory.String()] = ""
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// If we've found the grouping object, store the object metadata inventory
|
||||||
|
// in the grouping config map.
|
||||||
|
if groupingObj == nil {
|
||||||
|
return fmt.Errorf("Grouping object not found")
|
||||||
|
}
|
||||||
|
err := unstructured.SetNestedStringMap(groupingObj.UnstructuredContent(), inventoryMap, "data")
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// retrieveInventoryFromGroupingObj returns a slice of pointers to the
|
||||||
|
// inventory metadata. This function finds the grouping object, then
|
||||||
|
// parses the stored resource metadata into Inventory structs. Returns
|
||||||
|
// an error if there is a problem parsing the data into Inventory
|
||||||
|
// structs, or if the grouping object is not in Unstructured format; nil
|
||||||
|
// otherwise. If a grouping object does not exist, or it does not have a
|
||||||
|
// "data" map, then returns an empty slice and no error.
|
||||||
|
func retrieveInventoryFromGroupingObj(infos []*resource.Info) ([]*Inventory, error) {
|
||||||
|
inventory := []*Inventory{}
|
||||||
|
groupingInfo, exists := findGroupingObject(infos)
|
||||||
|
if exists {
|
||||||
|
groupingObj, ok := groupingInfo.Object.(*unstructured.Unstructured)
|
||||||
|
if !ok {
|
||||||
|
err := fmt.Errorf("Grouping object is not an Unstructured: %#v", groupingObj)
|
||||||
|
return inventory, err
|
||||||
|
}
|
||||||
|
invMap, exists, err := unstructured.NestedStringMap(groupingObj.Object, "data")
|
||||||
|
if err != nil {
|
||||||
|
err := fmt.Errorf("Error retrieving inventory from grouping object.")
|
||||||
|
return inventory, err
|
||||||
|
}
|
||||||
|
if exists {
|
||||||
|
for invStr := range invMap {
|
||||||
|
inv, err := parseInventory(invStr)
|
||||||
|
if err != nil {
|
||||||
|
return inventory, err
|
||||||
|
}
|
||||||
|
inventory = append(inventory, inv)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return inventory, nil
|
||||||
|
}
|
||||||
420
cmd/kubectl/kubectlcobra/grouping_test.go
Normal file
420
cmd/kubectl/kubectlcobra/grouping_test.go
Normal file
@@ -0,0 +1,420 @@
|
|||||||
|
// Copyright 2020 The Kubernetes Authors.
|
||||||
|
// SPDX-License-Identifier: Apache-2.0
|
||||||
|
|
||||||
|
// package kubectlcobra contains cobra commands from kubectl
|
||||||
|
package kubectlcobra
|
||||||
|
|
||||||
|
import (
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
|
||||||
|
"k8s.io/apimachinery/pkg/runtime"
|
||||||
|
"k8s.io/apimachinery/pkg/runtime/schema"
|
||||||
|
"k8s.io/cli-runtime/pkg/resource"
|
||||||
|
)
|
||||||
|
|
||||||
|
var testNamespace = "test-grouping-namespace"
|
||||||
|
var groupingObjName = "test-grouping-obj"
|
||||||
|
var pod1Name = "pod-1"
|
||||||
|
var pod2Name = "pod-2"
|
||||||
|
var pod3Name = "pod-3"
|
||||||
|
|
||||||
|
var groupingObj = unstructured.Unstructured{
|
||||||
|
Object: map[string]interface{}{
|
||||||
|
"apiVersion": "v1",
|
||||||
|
"kind": "ConfigMap",
|
||||||
|
"metadata": map[string]interface{}{
|
||||||
|
"name": groupingObjName,
|
||||||
|
"namespace": testNamespace,
|
||||||
|
"labels": map[string]interface{}{
|
||||||
|
GroupingLabel: "true",
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
var groupingInfo = &resource.Info{
|
||||||
|
Namespace: testNamespace,
|
||||||
|
Name: groupingObjName,
|
||||||
|
Object: &groupingObj,
|
||||||
|
}
|
||||||
|
|
||||||
|
var pod1 = unstructured.Unstructured{
|
||||||
|
Object: map[string]interface{}{
|
||||||
|
"apiVersion": "v1",
|
||||||
|
"kind": "Pod",
|
||||||
|
"metadata": map[string]interface{}{
|
||||||
|
"name": pod1Name,
|
||||||
|
"namespace": testNamespace,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
var pod1Info = &resource.Info{
|
||||||
|
Namespace: testNamespace,
|
||||||
|
Name: pod1Name,
|
||||||
|
Object: &pod1,
|
||||||
|
}
|
||||||
|
|
||||||
|
var pod2 = unstructured.Unstructured{
|
||||||
|
Object: map[string]interface{}{
|
||||||
|
"apiVersion": "v1",
|
||||||
|
"kind": "Pod",
|
||||||
|
"metadata": map[string]interface{}{
|
||||||
|
"name": pod2Name,
|
||||||
|
"namespace": testNamespace,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
var pod2Info = &resource.Info{
|
||||||
|
Namespace: testNamespace,
|
||||||
|
Name: pod2Name,
|
||||||
|
Object: &pod2,
|
||||||
|
}
|
||||||
|
|
||||||
|
var pod3 = unstructured.Unstructured{
|
||||||
|
Object: map[string]interface{}{
|
||||||
|
"apiVersion": "v1",
|
||||||
|
"kind": "Pod",
|
||||||
|
"metadata": map[string]interface{}{
|
||||||
|
"name": pod3Name,
|
||||||
|
"namespace": testNamespace,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
var pod3Info = &resource.Info{
|
||||||
|
Namespace: testNamespace,
|
||||||
|
Name: pod3Name,
|
||||||
|
Object: &pod3,
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestIsGroupingObject(t *testing.T) {
|
||||||
|
tests := []struct {
|
||||||
|
obj runtime.Object
|
||||||
|
isGrouping bool
|
||||||
|
}{
|
||||||
|
{
|
||||||
|
obj: nil,
|
||||||
|
isGrouping: false,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
obj: &groupingObj,
|
||||||
|
isGrouping: true,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
obj: &pod2,
|
||||||
|
isGrouping: false,
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, test := range tests {
|
||||||
|
grouping := isGroupingObject(test.obj)
|
||||||
|
if test.isGrouping && !grouping {
|
||||||
|
t.Errorf("Grouping object not identified: %#v", test.obj)
|
||||||
|
}
|
||||||
|
if !test.isGrouping && grouping {
|
||||||
|
t.Errorf("Non-grouping object identifed as grouping obj: %#v", test.obj)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestFindGroupingObject(t *testing.T) {
|
||||||
|
tests := []struct {
|
||||||
|
infos []*resource.Info
|
||||||
|
exists bool
|
||||||
|
name string
|
||||||
|
}{
|
||||||
|
{
|
||||||
|
infos: []*resource.Info{},
|
||||||
|
exists: false,
|
||||||
|
name: "",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
infos: []*resource.Info{nil},
|
||||||
|
exists: false,
|
||||||
|
name: "",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
infos: []*resource.Info{groupingInfo},
|
||||||
|
exists: true,
|
||||||
|
name: groupingObjName,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
infos: []*resource.Info{pod1Info},
|
||||||
|
exists: false,
|
||||||
|
name: "",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
infos: []*resource.Info{pod1Info, pod2Info, pod3Info},
|
||||||
|
exists: false,
|
||||||
|
name: "",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
infos: []*resource.Info{pod1Info, pod2Info, groupingInfo, pod3Info},
|
||||||
|
exists: true,
|
||||||
|
name: groupingObjName,
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, test := range tests {
|
||||||
|
groupingObj, found := findGroupingObject(test.infos)
|
||||||
|
if test.exists && !found {
|
||||||
|
t.Errorf("Should have found grouping object")
|
||||||
|
}
|
||||||
|
if !test.exists && found {
|
||||||
|
t.Errorf("Grouping object found, but it does not exist: %#v", groupingObj)
|
||||||
|
}
|
||||||
|
if test.exists && found && test.name != groupingObj.Name {
|
||||||
|
t.Errorf("Grouping object name does not match: %s/%s", test.name, groupingObj.Name)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestSortGroupingObject(t *testing.T) {
|
||||||
|
tests := []struct {
|
||||||
|
infos []*resource.Info
|
||||||
|
sorted bool
|
||||||
|
}{
|
||||||
|
{
|
||||||
|
infos: []*resource.Info{},
|
||||||
|
sorted: false,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
infos: []*resource.Info{groupingInfo},
|
||||||
|
sorted: true,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
infos: []*resource.Info{pod1Info},
|
||||||
|
sorted: false,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
infos: []*resource.Info{pod1Info, pod2Info},
|
||||||
|
sorted: false,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
infos: []*resource.Info{groupingInfo, pod1Info},
|
||||||
|
sorted: true,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
infos: []*resource.Info{pod1Info, groupingInfo},
|
||||||
|
sorted: true,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
infos: []*resource.Info{pod1Info, pod2Info, groupingInfo, pod3Info},
|
||||||
|
sorted: true,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
infos: []*resource.Info{pod1Info, pod2Info, pod3Info, groupingInfo},
|
||||||
|
sorted: true,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
infos: []*resource.Info{groupingInfo, pod1Info, pod2Info, pod3Info},
|
||||||
|
sorted: true,
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, test := range tests {
|
||||||
|
wasSorted := sortGroupingObject(test.infos)
|
||||||
|
if wasSorted && !test.sorted {
|
||||||
|
t.Errorf("Grouping object was sorted, but it shouldn't have been")
|
||||||
|
}
|
||||||
|
if !wasSorted && test.sorted {
|
||||||
|
t.Errorf("Grouping object was NOT sorted, but it should have been")
|
||||||
|
}
|
||||||
|
if wasSorted {
|
||||||
|
first := test.infos[0]
|
||||||
|
if !isGroupingObject(first.Object) {
|
||||||
|
t.Errorf("Grouping object was not sorted into first position")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestAddRetrieveInventoryToFromGroupingObject(t *testing.T) {
|
||||||
|
tests := []struct {
|
||||||
|
infos []*resource.Info
|
||||||
|
expected []*Inventory
|
||||||
|
isError bool
|
||||||
|
}{
|
||||||
|
// No grouping object is an error.
|
||||||
|
{
|
||||||
|
infos: []*resource.Info{},
|
||||||
|
isError: true,
|
||||||
|
},
|
||||||
|
// No grouping object is an error.
|
||||||
|
{
|
||||||
|
infos: []*resource.Info{pod1Info, pod2Info},
|
||||||
|
isError: true,
|
||||||
|
},
|
||||||
|
// Grouping object without other objects is OK.
|
||||||
|
{
|
||||||
|
infos: []*resource.Info{groupingInfo},
|
||||||
|
expected: []*Inventory{},
|
||||||
|
isError: false,
|
||||||
|
},
|
||||||
|
// More than one grouping object is an error.
|
||||||
|
{
|
||||||
|
infos: []*resource.Info{groupingInfo, groupingInfo},
|
||||||
|
expected: []*Inventory{},
|
||||||
|
isError: true,
|
||||||
|
},
|
||||||
|
// More than one grouping object is an error.
|
||||||
|
{
|
||||||
|
infos: []*resource.Info{groupingInfo, pod1Info, groupingInfo},
|
||||||
|
expected: []*Inventory{},
|
||||||
|
isError: true,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
infos: []*resource.Info{groupingInfo, pod1Info},
|
||||||
|
expected: []*Inventory{
|
||||||
|
&Inventory{
|
||||||
|
Namespace: testNamespace,
|
||||||
|
Name: pod1Name,
|
||||||
|
GroupKind: schema.GroupKind{
|
||||||
|
Group: "",
|
||||||
|
Kind: "Pod",
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
isError: false,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
infos: []*resource.Info{pod1Info, groupingInfo},
|
||||||
|
expected: []*Inventory{
|
||||||
|
&Inventory{
|
||||||
|
Namespace: testNamespace,
|
||||||
|
Name: pod1Name,
|
||||||
|
GroupKind: schema.GroupKind{
|
||||||
|
Group: "",
|
||||||
|
Kind: "Pod",
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
isError: false,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
infos: []*resource.Info{pod1Info, pod2Info, groupingInfo, pod3Info},
|
||||||
|
expected: []*Inventory{
|
||||||
|
&Inventory{
|
||||||
|
Namespace: testNamespace,
|
||||||
|
Name: pod1Name,
|
||||||
|
GroupKind: schema.GroupKind{
|
||||||
|
Group: "",
|
||||||
|
Kind: "Pod",
|
||||||
|
},
|
||||||
|
},
|
||||||
|
&Inventory{
|
||||||
|
Namespace: testNamespace,
|
||||||
|
Name: pod2Name,
|
||||||
|
GroupKind: schema.GroupKind{
|
||||||
|
Group: "",
|
||||||
|
Kind: "Pod",
|
||||||
|
},
|
||||||
|
},
|
||||||
|
&Inventory{
|
||||||
|
Namespace: testNamespace,
|
||||||
|
Name: pod3Name,
|
||||||
|
GroupKind: schema.GroupKind{
|
||||||
|
Group: "",
|
||||||
|
Kind: "Pod",
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
isError: false,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
infos: []*resource.Info{pod1Info, pod2Info, pod3Info, groupingInfo},
|
||||||
|
expected: []*Inventory{
|
||||||
|
&Inventory{
|
||||||
|
Namespace: testNamespace,
|
||||||
|
Name: pod1Name,
|
||||||
|
GroupKind: schema.GroupKind{
|
||||||
|
Group: "",
|
||||||
|
Kind: "Pod",
|
||||||
|
},
|
||||||
|
},
|
||||||
|
&Inventory{
|
||||||
|
Namespace: testNamespace,
|
||||||
|
Name: pod2Name,
|
||||||
|
GroupKind: schema.GroupKind{
|
||||||
|
Group: "",
|
||||||
|
Kind: "Pod",
|
||||||
|
},
|
||||||
|
},
|
||||||
|
&Inventory{
|
||||||
|
Namespace: testNamespace,
|
||||||
|
Name: pod3Name,
|
||||||
|
GroupKind: schema.GroupKind{
|
||||||
|
Group: "",
|
||||||
|
Kind: "Pod",
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
isError: false,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
infos: []*resource.Info{groupingInfo, pod1Info, pod2Info, pod3Info},
|
||||||
|
expected: []*Inventory{
|
||||||
|
&Inventory{
|
||||||
|
Namespace: testNamespace,
|
||||||
|
Name: pod1Name,
|
||||||
|
GroupKind: schema.GroupKind{
|
||||||
|
Group: "",
|
||||||
|
Kind: "Pod",
|
||||||
|
},
|
||||||
|
},
|
||||||
|
&Inventory{
|
||||||
|
Namespace: testNamespace,
|
||||||
|
Name: pod2Name,
|
||||||
|
GroupKind: schema.GroupKind{
|
||||||
|
Group: "",
|
||||||
|
Kind: "Pod",
|
||||||
|
},
|
||||||
|
},
|
||||||
|
&Inventory{
|
||||||
|
Namespace: testNamespace,
|
||||||
|
Name: pod3Name,
|
||||||
|
GroupKind: schema.GroupKind{
|
||||||
|
Group: "",
|
||||||
|
Kind: "Pod",
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
isError: false,
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, test := range tests {
|
||||||
|
err := addInventoryToGroupingObj(test.infos)
|
||||||
|
if test.isError && err == nil {
|
||||||
|
t.Errorf("Should have produced an error, but returned none.")
|
||||||
|
}
|
||||||
|
if !test.isError {
|
||||||
|
if err != nil {
|
||||||
|
t.Errorf("Received error when expecting none (%s)\n", err)
|
||||||
|
} else {
|
||||||
|
retrieved, err := retrieveInventoryFromGroupingObj(test.infos)
|
||||||
|
if err != nil {
|
||||||
|
t.Errorf("Error retrieving inventory: %s\n", err)
|
||||||
|
}
|
||||||
|
if len(test.expected) != len(retrieved) {
|
||||||
|
t.Errorf("Expected inventory for %d resources, actual %d",
|
||||||
|
len(test.expected), len(retrieved))
|
||||||
|
}
|
||||||
|
for _, expected := range test.expected {
|
||||||
|
found := false
|
||||||
|
for _, actual := range retrieved {
|
||||||
|
if expected.Equals(actual) {
|
||||||
|
found = true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if !found {
|
||||||
|
t.Errorf("Expected inventory (%s) not found", expected)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
83
cmd/kubectl/kubectlcobra/inventory.go
Normal file
83
cmd/kubectl/kubectlcobra/inventory.go
Normal file
@@ -0,0 +1,83 @@
|
|||||||
|
// Copyright 2020 The Kubernetes Authors.
|
||||||
|
// SPDX-License-Identifier: Apache-2.0
|
||||||
|
|
||||||
|
// package kubectlcobra contains cobra commands from kubectl
|
||||||
|
package kubectlcobra
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"strings"
|
||||||
|
|
||||||
|
"k8s.io/apimachinery/pkg/runtime/schema"
|
||||||
|
)
|
||||||
|
|
||||||
|
// Separates inventory fields. This string is allowable as a
|
||||||
|
// ConfigMap key, but it is not allowed as a character in
|
||||||
|
// resource name.
|
||||||
|
const fieldSeparator = "_"
|
||||||
|
|
||||||
|
// Inventory organizes and stores the indentifying information
|
||||||
|
// for an object. This struct (as a string) is stored in a
|
||||||
|
// grouping object to keep track of sets of applied objects.
|
||||||
|
type Inventory struct {
|
||||||
|
Namespace string
|
||||||
|
Name string
|
||||||
|
GroupKind schema.GroupKind
|
||||||
|
}
|
||||||
|
|
||||||
|
// createInventory returns a pointer to an Inventory struct filled
|
||||||
|
// with the passed values. This function validates the passed fields
|
||||||
|
// and returns an error for bad parameters.
|
||||||
|
func createInventory(namespace string,
|
||||||
|
name string, gk schema.GroupKind) (*Inventory, error) {
|
||||||
|
|
||||||
|
// Namespace can be empty, but name cannot.
|
||||||
|
name = strings.TrimSpace(name)
|
||||||
|
if name == "" {
|
||||||
|
return nil, fmt.Errorf("Empty name for inventory object")
|
||||||
|
}
|
||||||
|
if gk.Empty() {
|
||||||
|
return nil, fmt.Errorf("Empty GroupKind for inventory object")
|
||||||
|
}
|
||||||
|
|
||||||
|
return &Inventory{
|
||||||
|
Namespace: strings.TrimSpace(namespace),
|
||||||
|
Name: name,
|
||||||
|
GroupKind: gk,
|
||||||
|
}, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// parseInventory takes a string, splits it into its five fields,
|
||||||
|
// and returns a pointer to an Inventory struct storing the
|
||||||
|
// five fields. Example inventory string:
|
||||||
|
//
|
||||||
|
// test-namespace/test-name/apps/v1/ReplicaSet
|
||||||
|
//
|
||||||
|
// Returns an error if unable to parse and create the Inventory
|
||||||
|
// struct.
|
||||||
|
func parseInventory(inv string) (*Inventory, error) {
|
||||||
|
parts := strings.Split(inv, fieldSeparator)
|
||||||
|
if len(parts) == 4 {
|
||||||
|
gk := schema.GroupKind{
|
||||||
|
Group: strings.TrimSpace(parts[2]),
|
||||||
|
Kind: strings.TrimSpace(parts[3]),
|
||||||
|
}
|
||||||
|
return createInventory(parts[0], parts[1], gk)
|
||||||
|
}
|
||||||
|
return nil, fmt.Errorf("Unable to decode inventory: %s\n", inv)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Equals returns true if the Inventory structs are identical;
|
||||||
|
// false otherwise.
|
||||||
|
func (i *Inventory) Equals(other *Inventory) bool {
|
||||||
|
return i.String() == other.String()
|
||||||
|
}
|
||||||
|
|
||||||
|
// String create a string version of the Inventory struct.
|
||||||
|
func (i *Inventory) String() string {
|
||||||
|
return fmt.Sprintf("%s%s%s%s%s%s%s",
|
||||||
|
i.Namespace, fieldSeparator,
|
||||||
|
i.Name, fieldSeparator,
|
||||||
|
i.GroupKind.Group, fieldSeparator,
|
||||||
|
i.GroupKind.Kind)
|
||||||
|
}
|
||||||
218
cmd/kubectl/kubectlcobra/inventory_test.go
Normal file
218
cmd/kubectl/kubectlcobra/inventory_test.go
Normal file
@@ -0,0 +1,218 @@
|
|||||||
|
// Copyright 2020 The Kubernetes Authors.
|
||||||
|
// SPDX-License-Identifier: Apache-2.0
|
||||||
|
|
||||||
|
// package kubectlcobra contains cobra commands from kubectl
|
||||||
|
package kubectlcobra
|
||||||
|
|
||||||
|
import (
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
"k8s.io/apimachinery/pkg/runtime/schema"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestCreateInventory(t *testing.T) {
|
||||||
|
tests := []struct {
|
||||||
|
namespace string
|
||||||
|
name string
|
||||||
|
gk schema.GroupKind
|
||||||
|
expected string
|
||||||
|
isError bool
|
||||||
|
}{
|
||||||
|
{
|
||||||
|
namespace: " \n",
|
||||||
|
name: " test-name\t",
|
||||||
|
gk: schema.GroupKind{
|
||||||
|
Group: "apps",
|
||||||
|
Kind: "ReplicaSet",
|
||||||
|
},
|
||||||
|
expected: "_test-name_apps_ReplicaSet",
|
||||||
|
isError: false,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
namespace: "test-namespace ",
|
||||||
|
name: " test-name\t",
|
||||||
|
gk: schema.GroupKind{
|
||||||
|
Group: "apps",
|
||||||
|
Kind: "ReplicaSet",
|
||||||
|
},
|
||||||
|
expected: "test-namespace_test-name_apps_ReplicaSet",
|
||||||
|
isError: false,
|
||||||
|
},
|
||||||
|
// Error with empty name.
|
||||||
|
{
|
||||||
|
namespace: "test-namespace ",
|
||||||
|
name: " \t",
|
||||||
|
gk: schema.GroupKind{
|
||||||
|
Group: "apps",
|
||||||
|
Kind: "ReplicaSet",
|
||||||
|
},
|
||||||
|
expected: "",
|
||||||
|
isError: true,
|
||||||
|
},
|
||||||
|
// Error with empty GroupKind.
|
||||||
|
{
|
||||||
|
namespace: "test-namespace",
|
||||||
|
name: "test-name",
|
||||||
|
gk: schema.GroupKind{},
|
||||||
|
expected: "",
|
||||||
|
isError: true,
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, test := range tests {
|
||||||
|
inv, err := createInventory(test.namespace, test.name, test.gk)
|
||||||
|
if !test.isError {
|
||||||
|
if err != nil {
|
||||||
|
t.Errorf("Error creating inventory when it should have worked.")
|
||||||
|
} else if test.expected != inv.String() {
|
||||||
|
t.Errorf("Expected inventory (%s) != created inventory(%s)\n", test.expected, inv.String())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if test.isError && err == nil {
|
||||||
|
t.Errorf("Should have returned an error in createInventory()")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestInventoryEqual(t *testing.T) {
|
||||||
|
tests := []struct {
|
||||||
|
inventory1 *Inventory
|
||||||
|
inventory2 *Inventory
|
||||||
|
isEqual bool
|
||||||
|
}{
|
||||||
|
// Two equal inventories without a namespace
|
||||||
|
{
|
||||||
|
inventory1: &Inventory{
|
||||||
|
Name: "test-inv",
|
||||||
|
GroupKind: schema.GroupKind{
|
||||||
|
Group: "apps",
|
||||||
|
Kind: "Deployment",
|
||||||
|
},
|
||||||
|
},
|
||||||
|
inventory2: &Inventory{
|
||||||
|
Name: "test-inv",
|
||||||
|
GroupKind: schema.GroupKind{
|
||||||
|
Group: "apps",
|
||||||
|
Kind: "Deployment",
|
||||||
|
},
|
||||||
|
},
|
||||||
|
isEqual: true,
|
||||||
|
},
|
||||||
|
// Two equal inventories with a namespace
|
||||||
|
{
|
||||||
|
inventory1: &Inventory{
|
||||||
|
Namespace: "test-namespace",
|
||||||
|
Name: "test-inv",
|
||||||
|
GroupKind: schema.GroupKind{
|
||||||
|
Group: "apps",
|
||||||
|
Kind: "Deployment",
|
||||||
|
},
|
||||||
|
},
|
||||||
|
inventory2: &Inventory{
|
||||||
|
Namespace: "test-namespace",
|
||||||
|
Name: "test-inv",
|
||||||
|
GroupKind: schema.GroupKind{
|
||||||
|
Group: "apps",
|
||||||
|
Kind: "Deployment",
|
||||||
|
},
|
||||||
|
},
|
||||||
|
isEqual: true,
|
||||||
|
},
|
||||||
|
// One inventory with a namespace, one without -- not equal.
|
||||||
|
{
|
||||||
|
inventory1: &Inventory{
|
||||||
|
Name: "test-inv",
|
||||||
|
GroupKind: schema.GroupKind{
|
||||||
|
Group: "apps",
|
||||||
|
Kind: "Deployment",
|
||||||
|
},
|
||||||
|
},
|
||||||
|
inventory2: &Inventory{
|
||||||
|
Namespace: "test-namespace",
|
||||||
|
Name: "test-inv",
|
||||||
|
GroupKind: schema.GroupKind{
|
||||||
|
Group: "apps",
|
||||||
|
Kind: "Deployment",
|
||||||
|
},
|
||||||
|
},
|
||||||
|
isEqual: false,
|
||||||
|
},
|
||||||
|
// One inventory with a Deployment, one with a ReplicaSet -- not equal.
|
||||||
|
{
|
||||||
|
inventory1: &Inventory{
|
||||||
|
Name: "test-inv",
|
||||||
|
GroupKind: schema.GroupKind{
|
||||||
|
Group: "apps",
|
||||||
|
Kind: "Deployment",
|
||||||
|
},
|
||||||
|
},
|
||||||
|
inventory2: &Inventory{
|
||||||
|
Name: "test-inv",
|
||||||
|
GroupKind: schema.GroupKind{
|
||||||
|
Group: "apps",
|
||||||
|
Kind: "ReplicaSet",
|
||||||
|
},
|
||||||
|
},
|
||||||
|
isEqual: false,
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, test := range tests {
|
||||||
|
actual := test.inventory1.Equals(test.inventory2)
|
||||||
|
if test.isEqual && !actual {
|
||||||
|
t.Errorf("Expected inventories equal, but actual is not: (%s)/(%s)\n", test.inventory1, test.inventory2)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestParseInventory(t *testing.T) {
|
||||||
|
tests := []struct {
|
||||||
|
invStr string
|
||||||
|
inventory *Inventory
|
||||||
|
isError bool
|
||||||
|
}{
|
||||||
|
{
|
||||||
|
invStr: "_test-name_apps_ReplicaSet\t",
|
||||||
|
inventory: &Inventory{
|
||||||
|
Name: "test-name",
|
||||||
|
GroupKind: schema.GroupKind{
|
||||||
|
Group: "apps",
|
||||||
|
Kind: "ReplicaSet",
|
||||||
|
},
|
||||||
|
},
|
||||||
|
isError: false,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
invStr: "test-namespace_test-name_apps_Deployment",
|
||||||
|
inventory: &Inventory{
|
||||||
|
Namespace: "test-namespace",
|
||||||
|
Name: "test-name",
|
||||||
|
GroupKind: schema.GroupKind{
|
||||||
|
Group: "apps",
|
||||||
|
Kind: "Deployment",
|
||||||
|
},
|
||||||
|
},
|
||||||
|
isError: false,
|
||||||
|
},
|
||||||
|
// Not enough fields -- error
|
||||||
|
{
|
||||||
|
invStr: "_test-name_apps",
|
||||||
|
inventory: &Inventory{},
|
||||||
|
isError: true,
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, test := range tests {
|
||||||
|
actual, err := parseInventory(test.invStr)
|
||||||
|
if !test.isError {
|
||||||
|
if err != nil {
|
||||||
|
t.Errorf("Error parsing inventory when it should have worked.")
|
||||||
|
} else if !test.inventory.Equals(actual) {
|
||||||
|
t.Errorf("Expected inventory (%s) != parsed inventory (%s)\n", test.inventory, actual)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if test.isError && err == nil {
|
||||||
|
t.Errorf("Should have returned an error in parseInventory()")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -8,6 +8,10 @@ import (
|
|||||||
|
|
||||||
"sigs.k8s.io/kustomize/cmd/kubectl/kubectlcobra"
|
"sigs.k8s.io/kustomize/cmd/kubectl/kubectlcobra"
|
||||||
"sigs.k8s.io/kustomize/kyaml/commandutil"
|
"sigs.k8s.io/kustomize/kyaml/commandutil"
|
||||||
|
|
||||||
|
// This is here rather than in the libraries because of
|
||||||
|
// https://github.com/kubernetes-sigs/kustomize/issues/2060
|
||||||
|
_ "k8s.io/client-go/plugin/pkg/client/auth"
|
||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
|
|||||||
@@ -15,7 +15,7 @@ linters:
|
|||||||
- dogsled
|
- dogsled
|
||||||
- dupl
|
- dupl
|
||||||
- errcheck
|
- errcheck
|
||||||
- funlen
|
# - funlen
|
||||||
# - gochecknoinits
|
# - gochecknoinits
|
||||||
- goconst
|
- goconst
|
||||||
- gocritic
|
- gocritic
|
||||||
|
|||||||
20
cmd/resource/fixgomod.sh
Executable file
20
cmd/resource/fixgomod.sh
Executable file
@@ -0,0 +1,20 @@
|
|||||||
|
#!/bin/bash
|
||||||
|
# Copyright 2019 The Kubernetes Authors.
|
||||||
|
# SPDX-License-Identifier: Apache-2.0
|
||||||
|
|
||||||
|
set -e
|
||||||
|
|
||||||
|
: "${kyaml_major?Need to source VERSIONS}"
|
||||||
|
: "${kyaml_minor?Need to source VERSIONS}"
|
||||||
|
: "${kyaml_patch?Need to source VERSIONS}"
|
||||||
|
|
||||||
|
: "${kstatus_major?Need to source VERSIONS}"
|
||||||
|
: "${kstatus_minor?Need to source VERSIONS}"
|
||||||
|
: "${kstatus_patch?Need to source VERSIONS}"
|
||||||
|
|
||||||
|
|
||||||
|
go mod edit -dropreplace=sigs.k8s.io/kustomize/kyaml@v0.0.0
|
||||||
|
go mod edit -require=sigs.k8s.io/kustomize/kyaml@v$kyaml_major.$kyaml_minor.$kyaml_patch
|
||||||
|
|
||||||
|
go mod edit -dropreplace=sigs.k8s.io/kustomize/kstatus@v0.0.0
|
||||||
|
go mod edit -require=sigs.k8s.io/kustomize/kstatus@v$kstatus_major.$kstatus_minor.$kstatus_patch
|
||||||
@@ -3,18 +3,20 @@ module sigs.k8s.io/kustomize/cmd/resource
|
|||||||
go 1.12
|
go 1.12
|
||||||
|
|
||||||
require (
|
require (
|
||||||
|
github.com/acarl005/stripansi v0.0.0-20180116102854-5a71ef0e047d
|
||||||
github.com/pkg/errors v0.8.1
|
github.com/pkg/errors v0.8.1
|
||||||
github.com/spf13/cobra v0.0.5
|
github.com/spf13/cobra v0.0.5
|
||||||
|
github.com/stretchr/testify v1.4.0
|
||||||
golang.org/x/net v0.0.0-20191004110552-13f9640d40b9 // indirect
|
golang.org/x/net v0.0.0-20191004110552-13f9640d40b9 // indirect
|
||||||
|
k8s.io/api v0.0.0-20190918155943-95b840bb6a1f
|
||||||
k8s.io/apimachinery v0.0.0-20190913080033-27d36303b655
|
k8s.io/apimachinery v0.0.0-20190913080033-27d36303b655
|
||||||
k8s.io/client-go v0.0.0-20190918160344-1fbdaa4c8d90
|
k8s.io/client-go v0.0.0-20190918160344-1fbdaa4c8d90
|
||||||
sigs.k8s.io/controller-runtime v0.4.0
|
sigs.k8s.io/controller-runtime v0.4.0
|
||||||
sigs.k8s.io/kustomize/cmd/mdtogo v0.0.0-20191222005333-3900166fdf4f // indirect
|
sigs.k8s.io/kustomize/kstatus v0.0.0
|
||||||
sigs.k8s.io/kustomize/kstatus v0.0.0-20191204200457-7c1b477ff62d
|
sigs.k8s.io/kustomize/kyaml v0.0.0
|
||||||
sigs.k8s.io/kustomize/kyaml v0.0.0-20191202204815-0a19a5dbd9b8
|
|
||||||
)
|
)
|
||||||
|
|
||||||
replace (
|
replace (
|
||||||
sigs.k8s.io/kustomize/kstatus v0.0.0-20191204200457-7c1b477ff62d => ../../kstatus
|
sigs.k8s.io/kustomize/kstatus v0.0.0 => ../../kstatus
|
||||||
sigs.k8s.io/kustomize/kyaml v0.0.0-20191202204815-0a19a5dbd9b8 => ../../kyaml
|
sigs.k8s.io/kustomize/kyaml v0.0.0 => ../../kyaml
|
||||||
)
|
)
|
||||||
|
|||||||
@@ -3,21 +3,31 @@ cloud.google.com/go v0.34.0/go.mod h1:aQUYkXzVsufM+DwF1aE+0xfcU+56JwCaLick0ClmMT
|
|||||||
cloud.google.com/go v0.38.0 h1:ROfEUZz+Gh5pa62DJWXSaonyu3StP6EA6lPEXPI6mCo=
|
cloud.google.com/go v0.38.0 h1:ROfEUZz+Gh5pa62DJWXSaonyu3StP6EA6lPEXPI6mCo=
|
||||||
cloud.google.com/go v0.38.0/go.mod h1:990N+gfupTy94rShfmMCWGDn0LpTmnzTp2qbd1dvSRU=
|
cloud.google.com/go v0.38.0/go.mod h1:990N+gfupTy94rShfmMCWGDn0LpTmnzTp2qbd1dvSRU=
|
||||||
github.com/Azure/go-ansiterm v0.0.0-20170929234023-d6e3b3328b78/go.mod h1:LmzpDX56iTiv29bbRTIsUNlaFfuhWRQBWjQdVyAevI8=
|
github.com/Azure/go-ansiterm v0.0.0-20170929234023-d6e3b3328b78/go.mod h1:LmzpDX56iTiv29bbRTIsUNlaFfuhWRQBWjQdVyAevI8=
|
||||||
|
github.com/Azure/go-autorest/autorest v0.9.0 h1:MRvx8gncNaXJqOoLmhNjUAKh33JJF8LyxPhomEtOsjs=
|
||||||
github.com/Azure/go-autorest/autorest v0.9.0/go.mod h1:xyHB1BMZT0cuDHU7I0+g046+BFDTQ8rEZB0s4Yfa6bI=
|
github.com/Azure/go-autorest/autorest v0.9.0/go.mod h1:xyHB1BMZT0cuDHU7I0+g046+BFDTQ8rEZB0s4Yfa6bI=
|
||||||
|
github.com/Azure/go-autorest/autorest/adal v0.5.0 h1:q2gDruN08/guU9vAjuPWff0+QIrpH6ediguzdAzXAUU=
|
||||||
github.com/Azure/go-autorest/autorest/adal v0.5.0/go.mod h1:8Z9fGy2MpX0PvDjB1pEgQTmVqjGhiHBW7RJJEciWzS0=
|
github.com/Azure/go-autorest/autorest/adal v0.5.0/go.mod h1:8Z9fGy2MpX0PvDjB1pEgQTmVqjGhiHBW7RJJEciWzS0=
|
||||||
|
github.com/Azure/go-autorest/autorest/date v0.1.0 h1:YGrhWfrgtFs84+h0o46rJrlmsZtyZRg470CqAXTZaGM=
|
||||||
github.com/Azure/go-autorest/autorest/date v0.1.0/go.mod h1:plvfp3oPSKwf2DNjlBjWF/7vwR+cUD/ELuzDCXwHUVA=
|
github.com/Azure/go-autorest/autorest/date v0.1.0/go.mod h1:plvfp3oPSKwf2DNjlBjWF/7vwR+cUD/ELuzDCXwHUVA=
|
||||||
github.com/Azure/go-autorest/autorest/mocks v0.1.0/go.mod h1:OTyCOPRA2IgIlWxVYxBee2F5Gr4kF2zd2J5cFRaIDN0=
|
github.com/Azure/go-autorest/autorest/mocks v0.1.0/go.mod h1:OTyCOPRA2IgIlWxVYxBee2F5Gr4kF2zd2J5cFRaIDN0=
|
||||||
|
github.com/Azure/go-autorest/autorest/mocks v0.2.0 h1:Ww5g4zThfD/6cLb4z6xxgeyDa7QDkizMkJKe0ysZXp0=
|
||||||
github.com/Azure/go-autorest/autorest/mocks v0.2.0/go.mod h1:OTyCOPRA2IgIlWxVYxBee2F5Gr4kF2zd2J5cFRaIDN0=
|
github.com/Azure/go-autorest/autorest/mocks v0.2.0/go.mod h1:OTyCOPRA2IgIlWxVYxBee2F5Gr4kF2zd2J5cFRaIDN0=
|
||||||
|
github.com/Azure/go-autorest/logger v0.1.0 h1:ruG4BSDXONFRrZZJ2GUXDiUyVpayPmb1GnWeHDdaNKY=
|
||||||
github.com/Azure/go-autorest/logger v0.1.0/go.mod h1:oExouG+K6PryycPJfVSxi/koC6LSNgds39diKLz7Vrc=
|
github.com/Azure/go-autorest/logger v0.1.0/go.mod h1:oExouG+K6PryycPJfVSxi/koC6LSNgds39diKLz7Vrc=
|
||||||
|
github.com/Azure/go-autorest/tracing v0.5.0 h1:TRn4WjSnkcSy5AEG3pnbtFSwNtwzjr4VYyQflFE619k=
|
||||||
github.com/Azure/go-autorest/tracing v0.5.0/go.mod h1:r/s2XiOKccPW3HrqB+W0TQzfbtp2fGCgRFtBroKn4Dk=
|
github.com/Azure/go-autorest/tracing v0.5.0/go.mod h1:r/s2XiOKccPW3HrqB+W0TQzfbtp2fGCgRFtBroKn4Dk=
|
||||||
github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU=
|
github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU=
|
||||||
github.com/BurntSushi/xgb v0.0.0-20160522181843-27f122750802/go.mod h1:IVnqGOEym/WlBOVXweHU+Q+/VP0lqqI8lqeDx9IjBqo=
|
github.com/BurntSushi/xgb v0.0.0-20160522181843-27f122750802/go.mod h1:IVnqGOEym/WlBOVXweHU+Q+/VP0lqqI8lqeDx9IjBqo=
|
||||||
github.com/NYTimes/gziphandler v0.0.0-20170623195520-56545f4a5d46/go.mod h1:3wb06e3pkSAbeQ52E9H9iFoQsEEwGN64994WTCIhntQ=
|
github.com/NYTimes/gziphandler v0.0.0-20170623195520-56545f4a5d46/go.mod h1:3wb06e3pkSAbeQ52E9H9iFoQsEEwGN64994WTCIhntQ=
|
||||||
github.com/PuerkitoBio/purell v1.0.0/go.mod h1:c11w/QuzBsJSee3cPx9rAFu61PvFxuPbtSwDGJws/X0=
|
github.com/PuerkitoBio/purell v1.0.0/go.mod h1:c11w/QuzBsJSee3cPx9rAFu61PvFxuPbtSwDGJws/X0=
|
||||||
github.com/PuerkitoBio/purell v1.1.0/go.mod h1:c11w/QuzBsJSee3cPx9rAFu61PvFxuPbtSwDGJws/X0=
|
github.com/PuerkitoBio/purell v1.1.0/go.mod h1:c11w/QuzBsJSee3cPx9rAFu61PvFxuPbtSwDGJws/X0=
|
||||||
|
github.com/PuerkitoBio/purell v1.1.1 h1:WEQqlqaGbrPkxLJWfBwQmfEAE1Z7ONdDLqrN38tNFfI=
|
||||||
github.com/PuerkitoBio/purell v1.1.1/go.mod h1:c11w/QuzBsJSee3cPx9rAFu61PvFxuPbtSwDGJws/X0=
|
github.com/PuerkitoBio/purell v1.1.1/go.mod h1:c11w/QuzBsJSee3cPx9rAFu61PvFxuPbtSwDGJws/X0=
|
||||||
github.com/PuerkitoBio/urlesc v0.0.0-20160726150825-5bd2802263f2/go.mod h1:uGdkoq3SwY9Y+13GIhn11/XLaGBb4BfwItxLd5jeuXE=
|
github.com/PuerkitoBio/urlesc v0.0.0-20160726150825-5bd2802263f2/go.mod h1:uGdkoq3SwY9Y+13GIhn11/XLaGBb4BfwItxLd5jeuXE=
|
||||||
|
github.com/PuerkitoBio/urlesc v0.0.0-20170810143723-de5bf2ad4578 h1:d+Bc7a5rLufV/sSk/8dngufqelfh6jnri85riMAaF/M=
|
||||||
github.com/PuerkitoBio/urlesc v0.0.0-20170810143723-de5bf2ad4578/go.mod h1:uGdkoq3SwY9Y+13GIhn11/XLaGBb4BfwItxLd5jeuXE=
|
github.com/PuerkitoBio/urlesc v0.0.0-20170810143723-de5bf2ad4578/go.mod h1:uGdkoq3SwY9Y+13GIhn11/XLaGBb4BfwItxLd5jeuXE=
|
||||||
|
github.com/acarl005/stripansi v0.0.0-20180116102854-5a71ef0e047d h1:licZJFw2RwpHMqeKTCYkitsPqHNxTmd4SNR5r94FGM8=
|
||||||
|
github.com/acarl005/stripansi v0.0.0-20180116102854-5a71ef0e047d/go.mod h1:asat636LX7Bqt5lYEZ27JNDcqxfjdBQuJ/MM4CN/Lzo=
|
||||||
github.com/armon/consul-api v0.0.0-20180202201655-eb2c6b5be1b6/go.mod h1:grANhF5doyWs3UAsr3K4I6qtAmlQcZDesFNEHPZAzj8=
|
github.com/armon/consul-api v0.0.0-20180202201655-eb2c6b5be1b6/go.mod h1:grANhF5doyWs3UAsr3K4I6qtAmlQcZDesFNEHPZAzj8=
|
||||||
github.com/asaskevich/govalidator v0.0.0-20180720115003-f9ffefc3facf/go.mod h1:lB+ZfQJz7igIIfQNfa7Ml4HSf2uFQQRzpGGRXenZAgY=
|
github.com/asaskevich/govalidator v0.0.0-20180720115003-f9ffefc3facf/go.mod h1:lB+ZfQJz7igIIfQNfa7Ml4HSf2uFQQRzpGGRXenZAgY=
|
||||||
github.com/asaskevich/govalidator v0.0.0-20190424111038-f61b66f89f4a/go.mod h1:lB+ZfQJz7igIIfQNfa7Ml4HSf2uFQQRzpGGRXenZAgY=
|
github.com/asaskevich/govalidator v0.0.0-20190424111038-f61b66f89f4a/go.mod h1:lB+ZfQJz7igIIfQNfa7Ml4HSf2uFQQRzpGGRXenZAgY=
|
||||||
@@ -39,6 +49,7 @@ github.com/davecgh/go-spew v0.0.0-20151105211317-5215b55f46b2/go.mod h1:J7Y8YcW2
|
|||||||
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
||||||
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
|
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
|
||||||
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
||||||
|
github.com/dgrijalva/jwt-go v3.2.0+incompatible h1:7qlOGliEKZXTDg6OTjfoBKDXWrumCAMpl/TFQ4/5kLM=
|
||||||
github.com/dgrijalva/jwt-go v3.2.0+incompatible/go.mod h1:E3ru+11k8xSBh+hMPgOLZmtrrCbhqsmaPHjLKYnJCaQ=
|
github.com/dgrijalva/jwt-go v3.2.0+incompatible/go.mod h1:E3ru+11k8xSBh+hMPgOLZmtrrCbhqsmaPHjLKYnJCaQ=
|
||||||
github.com/docker/docker v0.7.3-0.20190327010347-be7ac8be2ae0/go.mod h1:eEKB0N0r5NX/I1kEveEz05bcu8tLC/8azJZsviup8Sk=
|
github.com/docker/docker v0.7.3-0.20190327010347-be7ac8be2ae0/go.mod h1:eEKB0N0r5NX/I1kEveEz05bcu8tLC/8azJZsviup8Sk=
|
||||||
github.com/docker/go-units v0.3.3/go.mod h1:fgPhTUdO+D/Jk86RDLlptpiXQzgHJF7gydDDbaIK4Dk=
|
github.com/docker/go-units v0.3.3/go.mod h1:fgPhTUdO+D/Jk86RDLlptpiXQzgHJF7gydDDbaIK4Dk=
|
||||||
@@ -74,10 +85,12 @@ github.com/go-openapi/jsonpointer v0.0.0-20160704185906-46af16f9f7b1/go.mod h1:+
|
|||||||
github.com/go-openapi/jsonpointer v0.17.0/go.mod h1:cOnomiV+CVVwFLk0A/MExoFMjwdsUdVpsRhURCKh+3M=
|
github.com/go-openapi/jsonpointer v0.17.0/go.mod h1:cOnomiV+CVVwFLk0A/MExoFMjwdsUdVpsRhURCKh+3M=
|
||||||
github.com/go-openapi/jsonpointer v0.18.0/go.mod h1:cOnomiV+CVVwFLk0A/MExoFMjwdsUdVpsRhURCKh+3M=
|
github.com/go-openapi/jsonpointer v0.18.0/go.mod h1:cOnomiV+CVVwFLk0A/MExoFMjwdsUdVpsRhURCKh+3M=
|
||||||
github.com/go-openapi/jsonpointer v0.19.2/go.mod h1:3akKfEdA7DF1sugOqz1dVQHBcuDBPKZGEoHC/NkiQRg=
|
github.com/go-openapi/jsonpointer v0.19.2/go.mod h1:3akKfEdA7DF1sugOqz1dVQHBcuDBPKZGEoHC/NkiQRg=
|
||||||
|
github.com/go-openapi/jsonpointer v0.19.3 h1:gihV7YNZK1iK6Tgwwsxo2rJbD1GTbdm72325Bq8FI3w=
|
||||||
github.com/go-openapi/jsonpointer v0.19.3/go.mod h1:Pl9vOtqEWErmShwVjC8pYs9cog34VGT37dQOVbmoatg=
|
github.com/go-openapi/jsonpointer v0.19.3/go.mod h1:Pl9vOtqEWErmShwVjC8pYs9cog34VGT37dQOVbmoatg=
|
||||||
github.com/go-openapi/jsonreference v0.0.0-20160704190145-13c6e3589ad9/go.mod h1:W3Z9FmVs9qj+KR4zFKmDPGiLdk1D9Rlm7cyMvf57TTg=
|
github.com/go-openapi/jsonreference v0.0.0-20160704190145-13c6e3589ad9/go.mod h1:W3Z9FmVs9qj+KR4zFKmDPGiLdk1D9Rlm7cyMvf57TTg=
|
||||||
github.com/go-openapi/jsonreference v0.17.0/go.mod h1:g4xxGn04lDIRh0GJb5QlpE3HfopLOL6uZrK/VgnsK9I=
|
github.com/go-openapi/jsonreference v0.17.0/go.mod h1:g4xxGn04lDIRh0GJb5QlpE3HfopLOL6uZrK/VgnsK9I=
|
||||||
github.com/go-openapi/jsonreference v0.18.0/go.mod h1:g4xxGn04lDIRh0GJb5QlpE3HfopLOL6uZrK/VgnsK9I=
|
github.com/go-openapi/jsonreference v0.18.0/go.mod h1:g4xxGn04lDIRh0GJb5QlpE3HfopLOL6uZrK/VgnsK9I=
|
||||||
|
github.com/go-openapi/jsonreference v0.19.2 h1:o20suLFB4Ri0tuzpWtyHlh7E7HnkqTNLq6aR6WVNS1w=
|
||||||
github.com/go-openapi/jsonreference v0.19.2/go.mod h1:jMjeRr2HHw6nAVajTXJ4eiUwohSTlpa0o73RUL1owJc=
|
github.com/go-openapi/jsonreference v0.19.2/go.mod h1:jMjeRr2HHw6nAVajTXJ4eiUwohSTlpa0o73RUL1owJc=
|
||||||
github.com/go-openapi/loads v0.17.0/go.mod h1:72tmFy5wsWx89uEVddd0RjRWPZm92WRLhf7AC+0+OOU=
|
github.com/go-openapi/loads v0.17.0/go.mod h1:72tmFy5wsWx89uEVddd0RjRWPZm92WRLhf7AC+0+OOU=
|
||||||
github.com/go-openapi/loads v0.18.0/go.mod h1:72tmFy5wsWx89uEVddd0RjRWPZm92WRLhf7AC+0+OOU=
|
github.com/go-openapi/loads v0.18.0/go.mod h1:72tmFy5wsWx89uEVddd0RjRWPZm92WRLhf7AC+0+OOU=
|
||||||
@@ -89,6 +102,7 @@ github.com/go-openapi/spec v0.0.0-20160808142527-6aced65f8501/go.mod h1:J8+jY1nA
|
|||||||
github.com/go-openapi/spec v0.17.0/go.mod h1:XkF/MOi14NmjsfZ8VtAKf8pIlbZzyoTvZsdfssdxcBI=
|
github.com/go-openapi/spec v0.17.0/go.mod h1:XkF/MOi14NmjsfZ8VtAKf8pIlbZzyoTvZsdfssdxcBI=
|
||||||
github.com/go-openapi/spec v0.18.0/go.mod h1:XkF/MOi14NmjsfZ8VtAKf8pIlbZzyoTvZsdfssdxcBI=
|
github.com/go-openapi/spec v0.18.0/go.mod h1:XkF/MOi14NmjsfZ8VtAKf8pIlbZzyoTvZsdfssdxcBI=
|
||||||
github.com/go-openapi/spec v0.19.2/go.mod h1:sCxk3jxKgioEJikev4fgkNmwS+3kuYdJtcsZsD5zxMY=
|
github.com/go-openapi/spec v0.19.2/go.mod h1:sCxk3jxKgioEJikev4fgkNmwS+3kuYdJtcsZsD5zxMY=
|
||||||
|
github.com/go-openapi/spec v0.19.5 h1:Xm0Ao53uqnk9QE/LlYV5DEU09UAgpliA85QoT9LzqPw=
|
||||||
github.com/go-openapi/spec v0.19.5/go.mod h1:Hm2Jr4jv8G1ciIAo+frC/Ft+rR2kQDh8JHKHb3gWUSk=
|
github.com/go-openapi/spec v0.19.5/go.mod h1:Hm2Jr4jv8G1ciIAo+frC/Ft+rR2kQDh8JHKHb3gWUSk=
|
||||||
github.com/go-openapi/strfmt v0.17.0/go.mod h1:P82hnJI0CXkErkXi8IKjPbNBM6lV6+5pLP5l494TcyU=
|
github.com/go-openapi/strfmt v0.17.0/go.mod h1:P82hnJI0CXkErkXi8IKjPbNBM6lV6+5pLP5l494TcyU=
|
||||||
github.com/go-openapi/strfmt v0.18.0/go.mod h1:P82hnJI0CXkErkXi8IKjPbNBM6lV6+5pLP5l494TcyU=
|
github.com/go-openapi/strfmt v0.18.0/go.mod h1:P82hnJI0CXkErkXi8IKjPbNBM6lV6+5pLP5l494TcyU=
|
||||||
@@ -97,6 +111,7 @@ github.com/go-openapi/swag v0.0.0-20160704191624-1d0bd113de87/go.mod h1:DXUve3Dp
|
|||||||
github.com/go-openapi/swag v0.17.0/go.mod h1:AByQ+nYG6gQg71GINrmuDXCPWdL640yX49/kXLo40Tg=
|
github.com/go-openapi/swag v0.17.0/go.mod h1:AByQ+nYG6gQg71GINrmuDXCPWdL640yX49/kXLo40Tg=
|
||||||
github.com/go-openapi/swag v0.18.0/go.mod h1:AByQ+nYG6gQg71GINrmuDXCPWdL640yX49/kXLo40Tg=
|
github.com/go-openapi/swag v0.18.0/go.mod h1:AByQ+nYG6gQg71GINrmuDXCPWdL640yX49/kXLo40Tg=
|
||||||
github.com/go-openapi/swag v0.19.2/go.mod h1:POnQmlKehdgb5mhVOsnJFsivZCEZ/vjK9gh66Z9tfKk=
|
github.com/go-openapi/swag v0.19.2/go.mod h1:POnQmlKehdgb5mhVOsnJFsivZCEZ/vjK9gh66Z9tfKk=
|
||||||
|
github.com/go-openapi/swag v0.19.5 h1:lTz6Ys4CmqqCQmZPBlbQENR1/GucA2bzYTE12Pw4tFY=
|
||||||
github.com/go-openapi/swag v0.19.5/go.mod h1:POnQmlKehdgb5mhVOsnJFsivZCEZ/vjK9gh66Z9tfKk=
|
github.com/go-openapi/swag v0.19.5/go.mod h1:POnQmlKehdgb5mhVOsnJFsivZCEZ/vjK9gh66Z9tfKk=
|
||||||
github.com/go-openapi/validate v0.18.0/go.mod h1:Uh4HdOzKt19xGIGm1qHf/ofbX1YQ4Y+MYsct2VUrAJ4=
|
github.com/go-openapi/validate v0.18.0/go.mod h1:Uh4HdOzKt19xGIGm1qHf/ofbX1YQ4Y+MYsct2VUrAJ4=
|
||||||
github.com/go-openapi/validate v0.19.2/go.mod h1:1tRCw7m3jtI8eNWEEliiAqUIcBztB2KDnRCRMUi7GTA=
|
github.com/go-openapi/validate v0.19.2/go.mod h1:1tRCw7m3jtI8eNWEEliiAqUIcBztB2KDnRCRMUi7GTA=
|
||||||
@@ -132,6 +147,7 @@ github.com/googleapis/gax-go/v2 v2.0.4/go.mod h1:0Wqv26UfaUD9n4G6kQubkQ+KchISgw+
|
|||||||
github.com/googleapis/gnostic v0.0.0-20170729233727-0c5108395e2d/go.mod h1:sJBsCZ4ayReDTBIg8b9dl28c5xFWyhBTVRp3pOg5EKY=
|
github.com/googleapis/gnostic v0.0.0-20170729233727-0c5108395e2d/go.mod h1:sJBsCZ4ayReDTBIg8b9dl28c5xFWyhBTVRp3pOg5EKY=
|
||||||
github.com/googleapis/gnostic v0.3.1 h1:WeAefnSUHlBb0iJKwxFDZdbfGwkd7xRNuV+IpXMJhYk=
|
github.com/googleapis/gnostic v0.3.1 h1:WeAefnSUHlBb0iJKwxFDZdbfGwkd7xRNuV+IpXMJhYk=
|
||||||
github.com/googleapis/gnostic v0.3.1/go.mod h1:on+2t9HRStVgn95RSsFWFz+6Q0Snyqv1awfrALZdbtU=
|
github.com/googleapis/gnostic v0.3.1/go.mod h1:on+2t9HRStVgn95RSsFWFz+6Q0Snyqv1awfrALZdbtU=
|
||||||
|
github.com/gophercloud/gophercloud v0.1.0 h1:P/nh25+rzXouhytV2pUHBb65fnds26Ghl8/391+sT5o=
|
||||||
github.com/gophercloud/gophercloud v0.1.0/go.mod h1:vxM41WHh5uqHVBMZHzuwNOHh8XEoIEcSTewFxm1c5g8=
|
github.com/gophercloud/gophercloud v0.1.0/go.mod h1:vxM41WHh5uqHVBMZHzuwNOHh8XEoIEcSTewFxm1c5g8=
|
||||||
github.com/gorilla/websocket v1.4.0/go.mod h1:E7qHFY5m1UJ88s3WnNqhKjPHQ0heANvMoAMk2YaljkQ=
|
github.com/gorilla/websocket v1.4.0/go.mod h1:E7qHFY5m1UJ88s3WnNqhKjPHQ0heANvMoAMk2YaljkQ=
|
||||||
github.com/gregjones/httpcache v0.0.0-20170728041850-787624de3eb7/go.mod h1:FecbI9+v66THATjSRHfNgh1IVFe/9kFxbXtjV0ctIMA=
|
github.com/gregjones/httpcache v0.0.0-20170728041850-787624de3eb7/go.mod h1:FecbI9+v66THATjSRHfNgh1IVFe/9kFxbXtjV0ctIMA=
|
||||||
@@ -170,6 +186,7 @@ github.com/mailru/easyjson v0.0.0-20160728113105-d5b7844b561a/go.mod h1:C1wdFJiN
|
|||||||
github.com/mailru/easyjson v0.0.0-20180823135443-60711f1a8329/go.mod h1:C1wdFJiN94OJF2b5HbByQZoLdCWB1Yqtg26g4irojpc=
|
github.com/mailru/easyjson v0.0.0-20180823135443-60711f1a8329/go.mod h1:C1wdFJiN94OJF2b5HbByQZoLdCWB1Yqtg26g4irojpc=
|
||||||
github.com/mailru/easyjson v0.0.0-20190312143242-1de009706dbe/go.mod h1:C1wdFJiN94OJF2b5HbByQZoLdCWB1Yqtg26g4irojpc=
|
github.com/mailru/easyjson v0.0.0-20190312143242-1de009706dbe/go.mod h1:C1wdFJiN94OJF2b5HbByQZoLdCWB1Yqtg26g4irojpc=
|
||||||
github.com/mailru/easyjson v0.0.0-20190614124828-94de47d64c63/go.mod h1:C1wdFJiN94OJF2b5HbByQZoLdCWB1Yqtg26g4irojpc=
|
github.com/mailru/easyjson v0.0.0-20190614124828-94de47d64c63/go.mod h1:C1wdFJiN94OJF2b5HbByQZoLdCWB1Yqtg26g4irojpc=
|
||||||
|
github.com/mailru/easyjson v0.0.0-20190626092158-b2ccc519800e h1:hB2xlXdHp/pmPZq0y3QnmWAArdw9PqbmotexnWx/FU8=
|
||||||
github.com/mailru/easyjson v0.0.0-20190626092158-b2ccc519800e/go.mod h1:C1wdFJiN94OJF2b5HbByQZoLdCWB1Yqtg26g4irojpc=
|
github.com/mailru/easyjson v0.0.0-20190626092158-b2ccc519800e/go.mod h1:C1wdFJiN94OJF2b5HbByQZoLdCWB1Yqtg26g4irojpc=
|
||||||
github.com/matttproud/golang_protobuf_extensions v1.0.1 h1:4hp9jkHxhMHkqkrB3Ix0jegS5sx/RkqARlsWZ6pIwiU=
|
github.com/matttproud/golang_protobuf_extensions v1.0.1 h1:4hp9jkHxhMHkqkrB3Ix0jegS5sx/RkqARlsWZ6pIwiU=
|
||||||
github.com/matttproud/golang_protobuf_extensions v1.0.1/go.mod h1:D8He9yQNgCq6Z5Ld7szi9bcBfOoFv/3dc6xSMkL2PC0=
|
github.com/matttproud/golang_protobuf_extensions v1.0.1/go.mod h1:D8He9yQNgCq6Z5Ld7szi9bcBfOoFv/3dc6xSMkL2PC0=
|
||||||
@@ -215,6 +232,7 @@ github.com/prometheus/procfs v0.0.0-20181204211112-1dc9a6cbc91a h1:9a8MnZMP0X2nL
|
|||||||
github.com/prometheus/procfs v0.0.0-20181204211112-1dc9a6cbc91a/go.mod h1:c3At6R/oaqEKCNdg8wHV1ftS6bRYblBhIjjI8uT2IGk=
|
github.com/prometheus/procfs v0.0.0-20181204211112-1dc9a6cbc91a/go.mod h1:c3At6R/oaqEKCNdg8wHV1ftS6bRYblBhIjjI8uT2IGk=
|
||||||
github.com/remyoudompheng/bigfft v0.0.0-20170806203942-52369c62f446/go.mod h1:uYEyJGbgTkfkS4+E/PavXkNJcbFIpEtjt2B0KDQ5+9M=
|
github.com/remyoudompheng/bigfft v0.0.0-20170806203942-52369c62f446/go.mod h1:uYEyJGbgTkfkS4+E/PavXkNJcbFIpEtjt2B0KDQ5+9M=
|
||||||
github.com/russross/blackfriday v1.5.2/go.mod h1:JO/DiYxRf+HjHt06OyowR9PTA263kcR/rfWxYHBV53g=
|
github.com/russross/blackfriday v1.5.2/go.mod h1:JO/DiYxRf+HjHt06OyowR9PTA263kcR/rfWxYHBV53g=
|
||||||
|
github.com/sergi/go-diff v1.1.0/go.mod h1:STckp+ISIX8hZLjrqAeVduY0gWCT9IjLuqbuNXdaHfM=
|
||||||
github.com/sirupsen/logrus v1.4.2/go.mod h1:tLMulIdttU9McNUspp0xgXVQah82FyeX6MwdIuYE2rE=
|
github.com/sirupsen/logrus v1.4.2/go.mod h1:tLMulIdttU9McNUspp0xgXVQah82FyeX6MwdIuYE2rE=
|
||||||
github.com/soheilhy/cmux v0.1.3/go.mod h1:IM3LyeVVIOuxMH7sFAkER9+bJ4dT7Ms6E4xg4kGIyLM=
|
github.com/soheilhy/cmux v0.1.3/go.mod h1:IM3LyeVVIOuxMH7sFAkER9+bJ4dT7Ms6E4xg4kGIyLM=
|
||||||
github.com/spf13/afero v1.1.2/go.mod h1:j4pytiNVoe2o6bmDsKpLACNPDBIoEAkihy7loJ1B0CQ=
|
github.com/spf13/afero v1.1.2/go.mod h1:j4pytiNVoe2o6bmDsKpLACNPDBIoEAkihy7loJ1B0CQ=
|
||||||
@@ -349,8 +367,10 @@ google.golang.org/appengine v1.5.0/go.mod h1:xpcJRLb0r/rnEns0DIKYYv+WjYCduHsrkT7
|
|||||||
google.golang.org/genproto v0.0.0-20180817151627-c66870c02cf8/go.mod h1:JiN7NxoALGmiZfu7CAH4rXhgtRTLTxftemlI0sWmxmc=
|
google.golang.org/genproto v0.0.0-20180817151627-c66870c02cf8/go.mod h1:JiN7NxoALGmiZfu7CAH4rXhgtRTLTxftemlI0sWmxmc=
|
||||||
google.golang.org/genproto v0.0.0-20190307195333-5fe7a883aa19/go.mod h1:VzzqZJRnGkLBvHegQrXjBqPurQTc5/KpmUdxsrq26oE=
|
google.golang.org/genproto v0.0.0-20190307195333-5fe7a883aa19/go.mod h1:VzzqZJRnGkLBvHegQrXjBqPurQTc5/KpmUdxsrq26oE=
|
||||||
google.golang.org/genproto v0.0.0-20190418145605-e7d98fc518a7/go.mod h1:VzzqZJRnGkLBvHegQrXjBqPurQTc5/KpmUdxsrq26oE=
|
google.golang.org/genproto v0.0.0-20190418145605-e7d98fc518a7/go.mod h1:VzzqZJRnGkLBvHegQrXjBqPurQTc5/KpmUdxsrq26oE=
|
||||||
|
google.golang.org/genproto v0.0.0-20190502173448-54afdca5d873 h1:nfPFGzJkUDX6uBmpN/pSw7MbOAWegH5QDQuoXFHedLg=
|
||||||
google.golang.org/genproto v0.0.0-20190502173448-54afdca5d873/go.mod h1:VzzqZJRnGkLBvHegQrXjBqPurQTc5/KpmUdxsrq26oE=
|
google.golang.org/genproto v0.0.0-20190502173448-54afdca5d873/go.mod h1:VzzqZJRnGkLBvHegQrXjBqPurQTc5/KpmUdxsrq26oE=
|
||||||
google.golang.org/grpc v1.19.0/go.mod h1:mqu4LbDTu4XGKhr4mRzUsmM4RtVoemTSY81AxZiDr8c=
|
google.golang.org/grpc v1.19.0/go.mod h1:mqu4LbDTu4XGKhr4mRzUsmM4RtVoemTSY81AxZiDr8c=
|
||||||
|
google.golang.org/grpc v1.23.0 h1:AzbTB6ux+okLTzP8Ru1Xs41C303zdcfEht7MQnYJt5A=
|
||||||
google.golang.org/grpc v1.23.0/go.mod h1:Y5yQAOtifL1yxbo5wqy6BxZv8vAUGQwXBOALyacEbxg=
|
google.golang.org/grpc v1.23.0/go.mod h1:Y5yQAOtifL1yxbo5wqy6BxZv8vAUGQwXBOALyacEbxg=
|
||||||
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
|
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
|
||||||
gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
|
gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
|
||||||
@@ -407,9 +427,6 @@ modernc.org/strutil v1.0.0/go.mod h1:lstksw84oURvj9y3tn8lGvRxyRC1S2+g5uuIzNfIOBs
|
|||||||
modernc.org/xc v1.0.0/go.mod h1:mRNCo0bvLjGhHO9WsyuKVU4q0ceiDDDoEeWDJHrNx8I=
|
modernc.org/xc v1.0.0/go.mod h1:mRNCo0bvLjGhHO9WsyuKVU4q0ceiDDDoEeWDJHrNx8I=
|
||||||
sigs.k8s.io/controller-runtime v0.4.0 h1:wATM6/m+3w8lj8FXNaO6Fs/rq/vqoOjO1Q116Z9NPsg=
|
sigs.k8s.io/controller-runtime v0.4.0 h1:wATM6/m+3w8lj8FXNaO6Fs/rq/vqoOjO1Q116Z9NPsg=
|
||||||
sigs.k8s.io/controller-runtime v0.4.0/go.mod h1:ApC79lpY3PHW9xj/w9pj+lYkLgwAAUZwfXkME1Lajns=
|
sigs.k8s.io/controller-runtime v0.4.0/go.mod h1:ApC79lpY3PHW9xj/w9pj+lYkLgwAAUZwfXkME1Lajns=
|
||||||
sigs.k8s.io/kustomize v2.0.3+incompatible h1:JUufWFNlI44MdtnjUqVnvh29rR37PQFzPbLXqhyOyX0=
|
|
||||||
sigs.k8s.io/kustomize/cmd/mdtogo v0.0.0-20191222005333-3900166fdf4f h1:Wdh26pJ0THtsuSB1DCkaLc1Ssv2NDddB7E7vCSdTHdg=
|
|
||||||
sigs.k8s.io/kustomize/cmd/mdtogo v0.0.0-20191222005333-3900166fdf4f/go.mod h1:arffnBwv6VTLUY3hxATxJ2fwNMWy92GSXm6UXEjFddQ=
|
|
||||||
sigs.k8s.io/structured-merge-diff v0.0.0-20190525122527-15d366b2352e/go.mod h1:wWxsB5ozmmv/SG7nM11ayaAW51xMvak/t1r0CSlcokI=
|
sigs.k8s.io/structured-merge-diff v0.0.0-20190525122527-15d366b2352e/go.mod h1:wWxsB5ozmmv/SG7nM11ayaAW51xMvak/t1r0CSlcokI=
|
||||||
sigs.k8s.io/structured-merge-diff v0.0.0-20190817042607-6149e4549fca/go.mod h1:IIgPezJWb76P0hotTxzDbWsMYB8APh18qZnxkomBpxA=
|
sigs.k8s.io/structured-merge-diff v0.0.0-20190817042607-6149e4549fca/go.mod h1:IIgPezJWb76P0hotTxzDbWsMYB8APh18qZnxkomBpxA=
|
||||||
sigs.k8s.io/testing_frameworks v0.1.2 h1:vK0+tvjF0BZ/RYFeZ1E6BYBwHJJXhjuZ3TdsEKH+UQM=
|
sigs.k8s.io/testing_frameworks v0.1.2 h1:vK0+tvjF0BZ/RYFeZ1E6BYBwHJJXhjuZ3TdsEKH+UQM=
|
||||||
|
|||||||
@@ -10,7 +10,9 @@ import (
|
|||||||
|
|
||||||
"sigs.k8s.io/kustomize/cmd/resource/status"
|
"sigs.k8s.io/kustomize/cmd/resource/status"
|
||||||
|
|
||||||
_ "k8s.io/client-go/plugin/pkg/client/auth/gcp"
|
// This is here rather than in the libraries because of
|
||||||
|
// https://github.com/kubernetes-sigs/kustomize/issues/2060
|
||||||
|
_ "k8s.io/client-go/plugin/pkg/client/auth"
|
||||||
)
|
)
|
||||||
|
|
||||||
var root = &cobra.Command{
|
var root = &cobra.Command{
|
||||||
|
|||||||
@@ -16,7 +16,9 @@ import (
|
|||||||
|
|
||||||
// GetEventsRunner returns a command EventsRunner.
|
// GetEventsRunner returns a command EventsRunner.
|
||||||
func GetEventsRunner() *EventsRunner {
|
func GetEventsRunner() *EventsRunner {
|
||||||
r := &EventsRunner{}
|
r := &EventsRunner{
|
||||||
|
createClientFunc: createClient,
|
||||||
|
}
|
||||||
c := &cobra.Command{
|
c := &cobra.Command{
|
||||||
Use: "events DIR...",
|
Use: "events DIR...",
|
||||||
Short: commands.EventsShort,
|
Short: commands.EventsShort,
|
||||||
@@ -46,13 +48,15 @@ type EventsRunner struct {
|
|||||||
Interval time.Duration
|
Interval time.Duration
|
||||||
Timeout time.Duration
|
Timeout time.Duration
|
||||||
Command *cobra.Command
|
Command *cobra.Command
|
||||||
|
|
||||||
|
createClientFunc createClientFunc
|
||||||
}
|
}
|
||||||
|
|
||||||
func (r *EventsRunner) runE(c *cobra.Command, args []string) error {
|
func (r *EventsRunner) runE(c *cobra.Command, args []string) error {
|
||||||
ctx := context.Background()
|
ctx := context.Background()
|
||||||
|
|
||||||
// Create a client and use it to set up a new resolver.
|
// Create a client and use it to set up a new resolver.
|
||||||
client, err := getClient()
|
client, err := r.createClientFunc()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return errors.Wrap(err, "error creating client")
|
return errors.Wrap(err, "error creating client")
|
||||||
}
|
}
|
||||||
|
|||||||
298
cmd/resource/status/cmd/events_test.go
Normal file
298
cmd/resource/status/cmd/events_test.go
Normal file
@@ -0,0 +1,298 @@
|
|||||||
|
package cmd
|
||||||
|
|
||||||
|
import (
|
||||||
|
"bytes"
|
||||||
|
"fmt"
|
||||||
|
"reflect"
|
||||||
|
"regexp"
|
||||||
|
"strings"
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
"github.com/stretchr/testify/assert"
|
||||||
|
"sigs.k8s.io/kustomize/kstatus/status"
|
||||||
|
"sigs.k8s.io/kustomize/kstatus/wait"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestEventsNoResources(t *testing.T) {
|
||||||
|
inBuffer := &bytes.Buffer{}
|
||||||
|
outBuffer := &bytes.Buffer{}
|
||||||
|
|
||||||
|
fakeClient := &FakeClient{}
|
||||||
|
|
||||||
|
r := GetEventsRunner()
|
||||||
|
r.createClientFunc = newClientFunc(fakeClient)
|
||||||
|
r.Command.SetArgs([]string{})
|
||||||
|
r.Command.SetIn(inBuffer)
|
||||||
|
r.Command.SetOut(outBuffer)
|
||||||
|
|
||||||
|
err := r.Command.Execute()
|
||||||
|
if !assert.NoError(t, err) {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
eventOutput := parseEventOutput(t, outBuffer.String())
|
||||||
|
|
||||||
|
if want, got := 1, len(eventOutput.events); want != got {
|
||||||
|
t.Errorf("expected %d events, but got %d", want, got)
|
||||||
|
}
|
||||||
|
|
||||||
|
event := eventOutput.events[0]
|
||||||
|
if want, got := status.CurrentStatus, event.aggStatus; want != got {
|
||||||
|
t.Errorf("expected agg status %s, but got %s", want, got)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestEventsMultipleUpdates(t *testing.T) {
|
||||||
|
inBuffer := &bytes.Buffer{}
|
||||||
|
outBuffer := &bytes.Buffer{}
|
||||||
|
|
||||||
|
_, err := fmt.Fprint(inBuffer, `
|
||||||
|
apiVersion: apps/v1
|
||||||
|
kind: Deployment
|
||||||
|
metadata:
|
||||||
|
name: bar
|
||||||
|
namespace: default
|
||||||
|
`)
|
||||||
|
if !assert.NoError(t, err) {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
fakeClient := &FakeClient{
|
||||||
|
resourceCallbackMap: map[string]ResourceGetCallback{
|
||||||
|
"Deployment": createDeploymentStatusFunc(),
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
r := GetEventsRunner()
|
||||||
|
r.createClientFunc = newClientFunc(fakeClient)
|
||||||
|
r.Command.SetArgs([]string{})
|
||||||
|
r.Command.SetIn(inBuffer)
|
||||||
|
r.Command.SetOut(outBuffer)
|
||||||
|
|
||||||
|
err = r.Command.Execute()
|
||||||
|
if !assert.NoError(t, err) {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
eventOutput := parseEventOutput(t, outBuffer.String())
|
||||||
|
|
||||||
|
aggStatuses := eventOutput.allAggStatuses()
|
||||||
|
expectedAggStatuses := []status.Status{
|
||||||
|
status.InProgressStatus,
|
||||||
|
status.InProgressStatus,
|
||||||
|
status.InProgressStatus,
|
||||||
|
status.InProgressStatus,
|
||||||
|
status.CurrentStatus,
|
||||||
|
status.CurrentStatus,
|
||||||
|
}
|
||||||
|
if !reflect.DeepEqual(aggStatuses, expectedAggStatuses) {
|
||||||
|
t.Errorf("expected agg statuses to be %s, but got %s", joinStatuses(expectedAggStatuses),
|
||||||
|
joinStatuses(aggStatuses))
|
||||||
|
}
|
||||||
|
|
||||||
|
resources := eventOutput.allResources()
|
||||||
|
if want, got := 1, len(resources); want != got {
|
||||||
|
t.Errorf("expected %d resource, but got %d", want, got)
|
||||||
|
}
|
||||||
|
|
||||||
|
resource := resources[0]
|
||||||
|
resourceStatuses := eventOutput.statusesForResource(resource)
|
||||||
|
expectedResourceStatuses := []status.Status{
|
||||||
|
status.InProgressStatus,
|
||||||
|
status.InProgressStatus,
|
||||||
|
status.InProgressStatus,
|
||||||
|
status.InProgressStatus,
|
||||||
|
status.CurrentStatus,
|
||||||
|
}
|
||||||
|
if !reflect.DeepEqual(resourceStatuses, expectedResourceStatuses) {
|
||||||
|
t.Errorf("expected statuses to be %s, but got %s", joinStatuses(expectedResourceStatuses),
|
||||||
|
joinStatuses(resourceStatuses))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestEventsMultipleResources(t *testing.T) {
|
||||||
|
inBuffer := &bytes.Buffer{}
|
||||||
|
outBuffer := &bytes.Buffer{}
|
||||||
|
|
||||||
|
_, err := fmt.Fprint(inBuffer, `
|
||||||
|
apiVersion: v1
|
||||||
|
kind: List
|
||||||
|
items:
|
||||||
|
- apiVersion: v1
|
||||||
|
kind: Pod
|
||||||
|
metadata:
|
||||||
|
name: foo
|
||||||
|
namespace: default
|
||||||
|
- apiVersion: v1
|
||||||
|
kind: Service
|
||||||
|
metadata:
|
||||||
|
name: bar
|
||||||
|
namespace: default
|
||||||
|
`)
|
||||||
|
if !assert.NoError(t, err) {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
fakeClient := &FakeClient{
|
||||||
|
resourceCallbackMap: map[string]ResourceGetCallback{
|
||||||
|
"Pod": createPodStatusFunc(),
|
||||||
|
"Service": createServiceStatusFunc(),
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
r := GetEventsRunner()
|
||||||
|
r.createClientFunc = newClientFunc(fakeClient)
|
||||||
|
r.Command.SetArgs([]string{})
|
||||||
|
r.Command.SetIn(inBuffer)
|
||||||
|
r.Command.SetOut(outBuffer)
|
||||||
|
|
||||||
|
err = r.Command.Execute()
|
||||||
|
if !assert.NoError(t, err) {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
eventOutput := parseEventOutput(t, outBuffer.String())
|
||||||
|
|
||||||
|
aggStatuses := eventOutput.allAggStatuses()
|
||||||
|
expectedAggStatuses := []status.Status{
|
||||||
|
status.UnknownStatus,
|
||||||
|
status.CurrentStatus,
|
||||||
|
status.CurrentStatus,
|
||||||
|
}
|
||||||
|
if !reflect.DeepEqual(aggStatuses, expectedAggStatuses) {
|
||||||
|
t.Errorf("expected agg statuses to be %s, but got %s", joinStatuses(expectedAggStatuses),
|
||||||
|
joinStatuses(aggStatuses))
|
||||||
|
}
|
||||||
|
|
||||||
|
resources := eventOutput.allResources()
|
||||||
|
if want, got := 2, len(resources); got != want {
|
||||||
|
t.Errorf("expected %d resource, but got %d", want, got)
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, resource := range resources {
|
||||||
|
resourceStatuses := eventOutput.statusesForResource(resource)
|
||||||
|
if want, got := status.CurrentStatus, resourceStatuses[len(resourceStatuses)-1]; want != got {
|
||||||
|
t.Errorf("expected resource %q to have final status %s, but got %s", resource.name, want, got)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
type EventOutput struct {
|
||||||
|
events []EventOutputLine
|
||||||
|
unknownLines []string
|
||||||
|
}
|
||||||
|
|
||||||
|
func (e *EventOutput) allAggStatuses() []status.Status {
|
||||||
|
var aggStatuses []status.Status
|
||||||
|
for _, event := range e.events {
|
||||||
|
aggStatuses = append(aggStatuses, event.aggStatus)
|
||||||
|
}
|
||||||
|
return aggStatuses
|
||||||
|
}
|
||||||
|
|
||||||
|
func (e *EventOutput) allResources() []ResourceIdentifier {
|
||||||
|
var resources []ResourceIdentifier
|
||||||
|
seenResources := make(map[ResourceIdentifier]bool)
|
||||||
|
for _, event := range e.events {
|
||||||
|
if !event.isResourceUpdateEvent() {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
r := event.identifier
|
||||||
|
if _, found := seenResources[r]; !found {
|
||||||
|
resources = append(resources, r)
|
||||||
|
seenResources[r] = true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return resources
|
||||||
|
}
|
||||||
|
|
||||||
|
func (e *EventOutput) statusesForResource(resource ResourceIdentifier) []status.Status {
|
||||||
|
var statuses []status.Status
|
||||||
|
for _, event := range e.events {
|
||||||
|
if !event.isResourceUpdateEvent() {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
if event.identifier.Equals(resource) {
|
||||||
|
statuses = append(statuses, event.status)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return statuses
|
||||||
|
}
|
||||||
|
|
||||||
|
type EventOutputLine struct {
|
||||||
|
eventType string
|
||||||
|
aggStatus status.Status
|
||||||
|
identifier ResourceIdentifier
|
||||||
|
status status.Status
|
||||||
|
message string
|
||||||
|
}
|
||||||
|
|
||||||
|
func (e *EventOutputLine) isResourceUpdateEvent() bool {
|
||||||
|
return e.eventType == string(wait.ResourceUpdate)
|
||||||
|
}
|
||||||
|
|
||||||
|
var (
|
||||||
|
eventRegex = regexp.MustCompile(`^\s*` +
|
||||||
|
`(?P<eventType>\S+)\s+` +
|
||||||
|
`(?P<aggStatus>\S+)\s+` +
|
||||||
|
`((?P<resourceType>\S+)\s+` +
|
||||||
|
`(?P<namespace>\S+)\s+` +
|
||||||
|
`(?P<name>\S+)\s+` +
|
||||||
|
`(?P<status>\S+)\s+` +
|
||||||
|
`(?P<message>.*\S)){0,1}` +
|
||||||
|
`\s*$`)
|
||||||
|
eventHeaderRegex = regexp.MustCompile(`^\s*` +
|
||||||
|
`EVENT TYPE\s+` +
|
||||||
|
`AGG STATUS\s+` +
|
||||||
|
`TYPE\s+` +
|
||||||
|
`NAMESPACE\s+` +
|
||||||
|
`NAME\s+` +
|
||||||
|
`STATUS\s+` +
|
||||||
|
`MESSAGE` +
|
||||||
|
`\s*$`)
|
||||||
|
)
|
||||||
|
|
||||||
|
func parseEventOutput(_ *testing.T, output string) EventOutput {
|
||||||
|
var eventOutput EventOutput
|
||||||
|
lines := strings.Split(output, "\n")
|
||||||
|
for _, line := range lines {
|
||||||
|
if len(line) == 0 {
|
||||||
|
continue // Ignore empty lines
|
||||||
|
}
|
||||||
|
match := eventHeaderRegex.FindStringSubmatch(line)
|
||||||
|
if match != nil {
|
||||||
|
continue // Ignore headers
|
||||||
|
}
|
||||||
|
match = eventRegex.FindStringSubmatch(line)
|
||||||
|
if match == nil {
|
||||||
|
eventOutput.unknownLines = append(eventOutput.unknownLines, line)
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
eventOutputLine := EventOutputLine{
|
||||||
|
eventType: match[1],
|
||||||
|
aggStatus: status.FromStringOrDie(match[2]),
|
||||||
|
}
|
||||||
|
|
||||||
|
if eventOutputLine.eventType == string(wait.ResourceUpdate) {
|
||||||
|
resourceType := match[4]
|
||||||
|
parts := strings.Split(resourceType, "/")
|
||||||
|
var identifier ResourceIdentifier
|
||||||
|
if len(parts) == 2 {
|
||||||
|
identifier.apiVersion = parts[0]
|
||||||
|
identifier.kind = parts[1]
|
||||||
|
} else {
|
||||||
|
identifier.apiVersion = strings.Join(parts[:2], "/")
|
||||||
|
identifier.kind = parts[2]
|
||||||
|
}
|
||||||
|
identifier.namespace = match[5]
|
||||||
|
identifier.name = match[6]
|
||||||
|
eventOutputLine.identifier = identifier
|
||||||
|
eventOutputLine.status = status.FromStringOrDie(match[7])
|
||||||
|
eventOutputLine.message = match[8]
|
||||||
|
}
|
||||||
|
|
||||||
|
eventOutput.events = append(eventOutput.events, eventOutputLine)
|
||||||
|
}
|
||||||
|
return eventOutput
|
||||||
|
}
|
||||||
@@ -17,7 +17,9 @@ import (
|
|||||||
|
|
||||||
// GetFetchRunner returns a command FetchRunner.
|
// GetFetchRunner returns a command FetchRunner.
|
||||||
func GetFetchRunner() *FetchRunner {
|
func GetFetchRunner() *FetchRunner {
|
||||||
r := &FetchRunner{}
|
r := &FetchRunner{
|
||||||
|
createClientFunc: createClient,
|
||||||
|
}
|
||||||
c := &cobra.Command{
|
c := &cobra.Command{
|
||||||
Use: "fetch DIR...",
|
Use: "fetch DIR...",
|
||||||
Short: commands.FetchShort,
|
Short: commands.FetchShort,
|
||||||
@@ -41,18 +43,20 @@ func FetchCommand() *cobra.Command {
|
|||||||
type FetchRunner struct {
|
type FetchRunner struct {
|
||||||
IncludeSubpackages bool
|
IncludeSubpackages bool
|
||||||
Command *cobra.Command
|
Command *cobra.Command
|
||||||
|
|
||||||
|
createClientFunc createClientFunc
|
||||||
}
|
}
|
||||||
|
|
||||||
func (r *FetchRunner) runE(c *cobra.Command, args []string) error {
|
func (r *FetchRunner) runE(c *cobra.Command, args []string) error {
|
||||||
ctx := context.Background()
|
ctx := context.Background()
|
||||||
|
|
||||||
// Create a new client and use it to set up a resolver.
|
// Create a new client and use it to set up a resolver.
|
||||||
client, err := getClient()
|
k8sClient, err := r.createClientFunc()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return errors.Wrap(err, "error creating client")
|
return errors.Wrap(err, "error creating k8sClient")
|
||||||
}
|
}
|
||||||
|
|
||||||
resolver := wait.NewResolver(client, time.Minute)
|
resolver := wait.NewResolver(k8sClient, time.Minute)
|
||||||
|
|
||||||
// Set up a CaptureIdentifierFilter and run all inputs through the
|
// Set up a CaptureIdentifierFilter and run all inputs through the
|
||||||
// filter with the pipeline to capture the inventory of resources
|
// filter with the pipeline to capture the inventory of resources
|
||||||
|
|||||||
230
cmd/resource/status/cmd/fetch_test.go
Normal file
230
cmd/resource/status/cmd/fetch_test.go
Normal file
@@ -0,0 +1,230 @@
|
|||||||
|
package cmd
|
||||||
|
|
||||||
|
import (
|
||||||
|
"bytes"
|
||||||
|
"fmt"
|
||||||
|
"io/ioutil"
|
||||||
|
"os"
|
||||||
|
"path/filepath"
|
||||||
|
"strings"
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
"github.com/acarl005/stripansi"
|
||||||
|
"github.com/stretchr/testify/assert"
|
||||||
|
appsv1 "k8s.io/api/apps/v1"
|
||||||
|
v1 "k8s.io/api/core/v1"
|
||||||
|
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
||||||
|
"sigs.k8s.io/controller-runtime/pkg/client/fake"
|
||||||
|
"sigs.k8s.io/kustomize/kstatus/status"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestEmptyManifest(t *testing.T) {
|
||||||
|
inBuffer := &bytes.Buffer{}
|
||||||
|
outBuffer := &bytes.Buffer{}
|
||||||
|
|
||||||
|
fakeClient := fake.NewFakeClientWithScheme(scheme)
|
||||||
|
|
||||||
|
r := GetFetchRunner()
|
||||||
|
r.createClientFunc = newClientFunc(fakeClient)
|
||||||
|
r.Command.SetArgs([]string{})
|
||||||
|
r.Command.SetIn(inBuffer)
|
||||||
|
r.Command.SetOut(outBuffer)
|
||||||
|
|
||||||
|
err := r.Command.Execute()
|
||||||
|
if !assert.NoError(t, err) {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
output := outBuffer.String()
|
||||||
|
lines := strings.Split(output, "\n")
|
||||||
|
|
||||||
|
if want, got := 2, len(lines); want != got {
|
||||||
|
t.Errorf("Expected %d lines, but got %d", want, got)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestFetchStatusFromManifestStdIn(t *testing.T) {
|
||||||
|
inBuffer := &bytes.Buffer{}
|
||||||
|
outBuffer := &bytes.Buffer{}
|
||||||
|
|
||||||
|
_, err := fmt.Fprint(inBuffer, `
|
||||||
|
apiVersion: apps/v1
|
||||||
|
kind: Deployment
|
||||||
|
metadata:
|
||||||
|
name: bar
|
||||||
|
namespace: default
|
||||||
|
`)
|
||||||
|
if !assert.NoError(t, err) {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
deployment := createDeployment("bar", "default", 42, appsv1.DeploymentStatus{
|
||||||
|
ObservedGeneration: 1,
|
||||||
|
})
|
||||||
|
|
||||||
|
fakeClient := fake.NewFakeClientWithScheme(scheme, deployment)
|
||||||
|
|
||||||
|
r := GetFetchRunner()
|
||||||
|
r.createClientFunc = newClientFunc(fakeClient)
|
||||||
|
r.Command.SetArgs([]string{})
|
||||||
|
r.Command.SetIn(inBuffer)
|
||||||
|
r.Command.SetOut(outBuffer)
|
||||||
|
|
||||||
|
err = r.Command.Execute()
|
||||||
|
if !assert.NoError(t, err) {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
cleanOutput := stripansi.Strip(outBuffer.String())
|
||||||
|
tableOutput := parseTableOutput(t, cleanOutput)
|
||||||
|
|
||||||
|
expectedResource := ResourceIdentifier{
|
||||||
|
apiVersion: "apps/v1",
|
||||||
|
kind: "Deployment",
|
||||||
|
namespace: "default",
|
||||||
|
name: "bar",
|
||||||
|
}
|
||||||
|
expectedStatus := status.InProgressStatus
|
||||||
|
expectedMessage := "Deployment generation is 2, but latest observed generation is 1"
|
||||||
|
|
||||||
|
verifyOutputContains(t, tableOutput, expectedResource, expectedStatus, expectedMessage)
|
||||||
|
}
|
||||||
|
|
||||||
|
//nolint:funlen
|
||||||
|
func TestFetchStatusFromManifestsFiles(t *testing.T) {
|
||||||
|
d, err := ioutil.TempDir("", "status-fetch-test")
|
||||||
|
if !assert.NoError(t, err) {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
defer os.RemoveAll(d)
|
||||||
|
|
||||||
|
err = ioutil.WriteFile(filepath.Join(d, "dep.yaml"), []byte(`
|
||||||
|
apiVersion: apps/v1
|
||||||
|
kind: Deployment
|
||||||
|
metadata:
|
||||||
|
name: foo
|
||||||
|
namespace: default
|
||||||
|
`), 0600)
|
||||||
|
if !assert.NoError(t, err) {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
err = ioutil.WriteFile(filepath.Join(d, "svc.yaml"), []byte(`
|
||||||
|
apiVersion: v1
|
||||||
|
kind: Service
|
||||||
|
metadata:
|
||||||
|
name: foo
|
||||||
|
namespace: default
|
||||||
|
`), 0600)
|
||||||
|
if !assert.NoError(t, err) {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
replicas := int32(42)
|
||||||
|
deployment := createDeployment("foo", "default", replicas, appsv1.DeploymentStatus{
|
||||||
|
ObservedGeneration: 2,
|
||||||
|
Replicas: replicas,
|
||||||
|
ReadyReplicas: replicas,
|
||||||
|
AvailableReplicas: replicas,
|
||||||
|
UpdatedReplicas: replicas,
|
||||||
|
Conditions: []appsv1.DeploymentCondition{
|
||||||
|
{
|
||||||
|
Type: appsv1.DeploymentAvailable,
|
||||||
|
Status: v1.ConditionTrue,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
})
|
||||||
|
service := createService("foo", "default")
|
||||||
|
|
||||||
|
fakeClient := fake.NewFakeClientWithScheme(scheme, deployment, service)
|
||||||
|
|
||||||
|
outBuffer := &bytes.Buffer{}
|
||||||
|
|
||||||
|
r := GetFetchRunner()
|
||||||
|
r.createClientFunc = newClientFunc(fakeClient)
|
||||||
|
r.Command.SetArgs([]string{d})
|
||||||
|
r.Command.SetOut(outBuffer)
|
||||||
|
|
||||||
|
err = r.Command.Execute()
|
||||||
|
if !assert.NoError(t, err) {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
cleanOutput := stripansi.Strip(outBuffer.String())
|
||||||
|
tableOutput := parseTableOutput(t, cleanOutput)
|
||||||
|
|
||||||
|
expectedDeploymentResource := ResourceIdentifier{
|
||||||
|
apiVersion: "apps/v1",
|
||||||
|
kind: "Deployment",
|
||||||
|
namespace: "default",
|
||||||
|
name: "foo",
|
||||||
|
}
|
||||||
|
expectedDeploymentStatus := status.CurrentStatus
|
||||||
|
expectedDeploymentMessage := "Deployment is available. Replicas: 42"
|
||||||
|
verifyOutputContains(t, tableOutput, expectedDeploymentResource, expectedDeploymentStatus, expectedDeploymentMessage)
|
||||||
|
|
||||||
|
expectedServiceResource := ResourceIdentifier{
|
||||||
|
apiVersion: "v1",
|
||||||
|
kind: "Service",
|
||||||
|
namespace: "default",
|
||||||
|
name: "foo",
|
||||||
|
}
|
||||||
|
expectedServiceStatus := status.CurrentStatus
|
||||||
|
expectedServiceMessage := "Service is ready"
|
||||||
|
|
||||||
|
verifyOutputContains(t, tableOutput, expectedServiceResource, expectedServiceStatus, expectedServiceMessage)
|
||||||
|
}
|
||||||
|
|
||||||
|
func createDeployment(name, namespace string, replicas int32, status appsv1.DeploymentStatus) *appsv1.Deployment {
|
||||||
|
return &appsv1.Deployment{
|
||||||
|
TypeMeta: metav1.TypeMeta{
|
||||||
|
APIVersion: "apps/v1",
|
||||||
|
Kind: "Deployment",
|
||||||
|
},
|
||||||
|
ObjectMeta: metav1.ObjectMeta{
|
||||||
|
Name: name,
|
||||||
|
Namespace: namespace,
|
||||||
|
Generation: 2,
|
||||||
|
},
|
||||||
|
Spec: appsv1.DeploymentSpec{
|
||||||
|
Replicas: &replicas,
|
||||||
|
},
|
||||||
|
Status: status,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func createService(name, namespace string) *v1.Service {
|
||||||
|
return &v1.Service{
|
||||||
|
TypeMeta: metav1.TypeMeta{
|
||||||
|
APIVersion: "v1",
|
||||||
|
Kind: "Service",
|
||||||
|
},
|
||||||
|
ObjectMeta: metav1.ObjectMeta{
|
||||||
|
Name: name,
|
||||||
|
Namespace: namespace,
|
||||||
|
},
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func verifyOutputContains(t *testing.T, tableOutput TableOutput, resource ResourceIdentifier, status status.Status, message string) {
|
||||||
|
if len(tableOutput.Frames) == 0 {
|
||||||
|
t.Fatalf("expected match for resource %s, but output had no frames", resource.name)
|
||||||
|
}
|
||||||
|
firstFrame := tableOutput.Frames[0]
|
||||||
|
var foundResource ResourceOutput
|
||||||
|
match := false
|
||||||
|
for _, resourceOutput := range firstFrame.Resources {
|
||||||
|
if resourceOutput.identifier.Equals(resource) {
|
||||||
|
foundResource = resourceOutput
|
||||||
|
match = true
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if !match {
|
||||||
|
t.Errorf("expected match for resource %s, but didn't find it", resource.name)
|
||||||
|
}
|
||||||
|
if want, got := status, foundResource.status; want != got {
|
||||||
|
t.Errorf("expected status %s for resource %s, but got %s", want, resource.name, got)
|
||||||
|
}
|
||||||
|
if want, got := message, foundResource.message; !strings.HasPrefix(want, got) {
|
||||||
|
t.Errorf("expected message %s for resource %s, but got %s", want, resource.name, got)
|
||||||
|
}
|
||||||
|
}
|
||||||
245
cmd/resource/status/cmd/helpers_test.go
Normal file
245
cmd/resource/status/cmd/helpers_test.go
Normal file
@@ -0,0 +1,245 @@
|
|||||||
|
package cmd
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"fmt"
|
||||||
|
"regexp"
|
||||||
|
"strings"
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
|
||||||
|
"k8s.io/apimachinery/pkg/runtime"
|
||||||
|
"sigs.k8s.io/controller-runtime/pkg/client"
|
||||||
|
"sigs.k8s.io/kustomize/kstatus/status"
|
||||||
|
)
|
||||||
|
|
||||||
|
type TableOutput struct {
|
||||||
|
Frames []TableOutputFrame
|
||||||
|
UnknownRows []string
|
||||||
|
}
|
||||||
|
|
||||||
|
func (t *TableOutput) allAggStatuses() []status.Status {
|
||||||
|
var statuses []status.Status
|
||||||
|
for _, frame := range t.Frames {
|
||||||
|
if frame.AggregateStatus != "" {
|
||||||
|
statuses = append(statuses, frame.AggregateStatus)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return statuses
|
||||||
|
}
|
||||||
|
|
||||||
|
func (t *TableOutput) dedupedAggStatuses() []status.Status {
|
||||||
|
var dedupedStatuses []status.Status
|
||||||
|
statuses := t.allAggStatuses()
|
||||||
|
var previousStatus status.Status
|
||||||
|
for _, s := range statuses {
|
||||||
|
if s != previousStatus {
|
||||||
|
dedupedStatuses = append(dedupedStatuses, s)
|
||||||
|
previousStatus = s
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return dedupedStatuses
|
||||||
|
}
|
||||||
|
|
||||||
|
func (t *TableOutput) resources() []ResourceIdentifier {
|
||||||
|
seenResources := make(map[ResourceIdentifier]bool)
|
||||||
|
var resources []ResourceIdentifier
|
||||||
|
for _, frame := range t.Frames {
|
||||||
|
for _, resource := range frame.Resources {
|
||||||
|
r := resource.identifier
|
||||||
|
_, found := seenResources[r]
|
||||||
|
if !found {
|
||||||
|
seenResources[r] = true
|
||||||
|
resources = append(resources, r)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return resources
|
||||||
|
}
|
||||||
|
|
||||||
|
func (t *TableOutput) dedupedStatusesForResource(resource ResourceIdentifier) []status.Status {
|
||||||
|
var dedupedStatuses []status.Status
|
||||||
|
var previousStatus status.Status
|
||||||
|
for _, frame := range t.Frames {
|
||||||
|
for _, r := range frame.Resources {
|
||||||
|
if r.identifier.Equals(resource) {
|
||||||
|
if r.status != previousStatus {
|
||||||
|
previousStatus = r.status
|
||||||
|
dedupedStatuses = append(dedupedStatuses, r.status)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return dedupedStatuses
|
||||||
|
}
|
||||||
|
|
||||||
|
type TableOutputFrame struct {
|
||||||
|
AggregateStatus status.Status
|
||||||
|
Resources []ResourceOutput
|
||||||
|
}
|
||||||
|
|
||||||
|
type ResourceIdentifier struct {
|
||||||
|
apiVersion string
|
||||||
|
kind string
|
||||||
|
name string
|
||||||
|
namespace string
|
||||||
|
}
|
||||||
|
|
||||||
|
func (r ResourceIdentifier) Equals(identifier ResourceIdentifier) bool {
|
||||||
|
return r.apiVersion == identifier.apiVersion &&
|
||||||
|
r.kind == identifier.kind &&
|
||||||
|
r.namespace == identifier.namespace &&
|
||||||
|
r.name == identifier.name
|
||||||
|
}
|
||||||
|
|
||||||
|
type ResourceOutput struct {
|
||||||
|
identifier ResourceIdentifier
|
||||||
|
status status.Status
|
||||||
|
message string
|
||||||
|
}
|
||||||
|
|
||||||
|
var (
|
||||||
|
headerRegex = regexp.MustCompile(`^\s*TYPE\s+NAMESPACE\s+NAME\s+STATUS\s+MESSAGE\s*$`)
|
||||||
|
resourceRegex = regexp.MustCompile(`^(?P<resourceType>\S+)\s+(?P<namespace>\S+)\s+(?P<name>\S+)\s+(?P<status>\S+)\s+(?P<message>.*\S)\s*$`)
|
||||||
|
aggStatusRegex = regexp.MustCompile(`^\s*AggregateStatus: (?P<aggregateStatus>\S+)\s*$`)
|
||||||
|
)
|
||||||
|
|
||||||
|
func parseTableOutput(_ *testing.T, output string) TableOutput {
|
||||||
|
tableOutput := TableOutput{}
|
||||||
|
|
||||||
|
lines := strings.Split(output, "\n")
|
||||||
|
|
||||||
|
hasAggStatus := false
|
||||||
|
var currentFrame TableOutputFrame
|
||||||
|
for i, line := range lines {
|
||||||
|
if len(line) == 0 {
|
||||||
|
continue // We don't care about empty lines.
|
||||||
|
}
|
||||||
|
|
||||||
|
// Check for lines with aggregate status. They are not always present, but if they are,
|
||||||
|
// they always start a new frame of output.
|
||||||
|
match := aggStatusRegex.FindStringSubmatch(line)
|
||||||
|
if match != nil {
|
||||||
|
hasAggStatus = true
|
||||||
|
if i != 0 {
|
||||||
|
tableOutput.Frames = append(tableOutput.Frames, currentFrame)
|
||||||
|
}
|
||||||
|
currentFrame = TableOutputFrame{
|
||||||
|
AggregateStatus: status.FromStringOrDie(match[1]),
|
||||||
|
}
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
match = headerRegex.FindStringSubmatch(line)
|
||||||
|
if match != nil {
|
||||||
|
if !hasAggStatus {
|
||||||
|
if i != 0 {
|
||||||
|
tableOutput.Frames = append(tableOutput.Frames, currentFrame)
|
||||||
|
}
|
||||||
|
currentFrame = TableOutputFrame{}
|
||||||
|
}
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
match = resourceRegex.FindStringSubmatch(line)
|
||||||
|
if match != nil {
|
||||||
|
var identifier ResourceIdentifier
|
||||||
|
resourceType := match[1]
|
||||||
|
parts := strings.Split(resourceType, "/")
|
||||||
|
if len(parts) == 2 {
|
||||||
|
identifier.apiVersion = parts[0]
|
||||||
|
identifier.kind = parts[1]
|
||||||
|
} else {
|
||||||
|
identifier.apiVersion = strings.Join(parts[:2], "/")
|
||||||
|
identifier.kind = parts[2]
|
||||||
|
}
|
||||||
|
identifier.namespace = match[2]
|
||||||
|
identifier.name = match[3]
|
||||||
|
|
||||||
|
res := ResourceOutput{
|
||||||
|
identifier: identifier,
|
||||||
|
}
|
||||||
|
res.status = status.FromStringOrDie(match[4])
|
||||||
|
res.message = match[5]
|
||||||
|
currentFrame.Resources = append(currentFrame.Resources, res)
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
tableOutput.UnknownRows = append(tableOutput.UnknownRows, line)
|
||||||
|
}
|
||||||
|
tableOutput.Frames = append(tableOutput.Frames, currentFrame)
|
||||||
|
return tableOutput
|
||||||
|
}
|
||||||
|
|
||||||
|
func createDeploymentStatusFunc() func(*unstructured.Unstructured) error {
|
||||||
|
metadataMap := map[string]interface{}{
|
||||||
|
"generation": int64(2),
|
||||||
|
}
|
||||||
|
specMap := map[string]interface{}{
|
||||||
|
"replicas": int64(2),
|
||||||
|
}
|
||||||
|
statusMap := map[string]interface{}{
|
||||||
|
"observedGeneration": int64(2),
|
||||||
|
"replicas": int64(4),
|
||||||
|
"updatedReplicas": int64(4),
|
||||||
|
"readyReplicas": int64(4),
|
||||||
|
}
|
||||||
|
var conditions = make([]interface{}, 0)
|
||||||
|
conditions = append(conditions, map[string]interface{}{
|
||||||
|
"type": "Available",
|
||||||
|
"status": "True",
|
||||||
|
})
|
||||||
|
callbackCount := int64(0)
|
||||||
|
return func(deployment *unstructured.Unstructured) error {
|
||||||
|
_ = unstructured.SetNestedMap(deployment.Object, metadataMap, "metadata")
|
||||||
|
_ = unstructured.SetNestedMap(deployment.Object, specMap, "spec")
|
||||||
|
statusMap["availableReplicas"] = callbackCount
|
||||||
|
_ = unstructured.SetNestedMap(deployment.Object, statusMap, "status")
|
||||||
|
_ = unstructured.SetNestedSlice(deployment.Object, conditions, "status", "conditions")
|
||||||
|
callbackCount++
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func createServiceStatusFunc() func(*unstructured.Unstructured) error {
|
||||||
|
return func(*unstructured.Unstructured) error {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func createPodStatusFunc() func(*unstructured.Unstructured) error {
|
||||||
|
statusMap := map[string]interface{}{
|
||||||
|
"phase": "Succeeded",
|
||||||
|
}
|
||||||
|
return func(pod *unstructured.Unstructured) error {
|
||||||
|
_ = unstructured.SetNestedMap(pod.Object, statusMap, "status")
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
type ResourceGetCallback func(resource *unstructured.Unstructured) error
|
||||||
|
|
||||||
|
type FakeClient struct {
|
||||||
|
resourceCallbackMap map[string]ResourceGetCallback
|
||||||
|
}
|
||||||
|
|
||||||
|
func (f *FakeClient) Get(_ context.Context, _ client.ObjectKey, obj runtime.Object) error {
|
||||||
|
kind := obj.GetObjectKind().GroupVersionKind().Kind
|
||||||
|
callbackFunc, found := f.resourceCallbackMap[kind]
|
||||||
|
if !found {
|
||||||
|
return fmt.Errorf("no callback func found for kind %s", kind)
|
||||||
|
}
|
||||||
|
u := obj.(*unstructured.Unstructured)
|
||||||
|
return callbackFunc(u)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (f *FakeClient) List(ctx context.Context, list runtime.Object, opts ...client.ListOption) error {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func joinStatuses(statuses []status.Status) string {
|
||||||
|
var stringStatuses []string
|
||||||
|
for _, s := range statuses {
|
||||||
|
stringStatuses = append(stringStatuses, s.String())
|
||||||
|
}
|
||||||
|
return strings.Join(stringStatuses, ",")
|
||||||
|
}
|
||||||
@@ -138,10 +138,10 @@ func (s *TablePrinter) Print() {
|
|||||||
|
|
||||||
func (s *TablePrinter) PrintUntil(stop <-chan struct{}, interval time.Duration) <-chan struct{} {
|
func (s *TablePrinter) PrintUntil(stop <-chan struct{}, interval time.Duration) <-chan struct{} {
|
||||||
completed := make(chan struct{})
|
completed := make(chan struct{})
|
||||||
|
setColor(s.out, WHITE)
|
||||||
|
s.printTable(s.statusInfo.CurrentStatus(), false)
|
||||||
go func() {
|
go func() {
|
||||||
defer close(completed)
|
defer close(completed)
|
||||||
setColor(s.out, WHITE)
|
|
||||||
s.printTable(s.statusInfo.CurrentStatus(), false)
|
|
||||||
ticker := time.NewTicker(interval)
|
ticker := time.NewTicker(interval)
|
||||||
for {
|
for {
|
||||||
select {
|
select {
|
||||||
@@ -179,10 +179,13 @@ func (s *TablePrinter) printTable(data StatusData, deleteUp bool) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (s *TablePrinter) printTableRow(rowData []RowData) {
|
func (s *TablePrinter) printTableRow(rowData []RowData) {
|
||||||
for _, row := range rowData {
|
for i, row := range rowData {
|
||||||
setColor(s.out, row.color)
|
setColor(s.out, row.color)
|
||||||
format := fmt.Sprintf("%%-%ds ", row.width)
|
format := fmt.Sprintf("%%-%ds", row.width)
|
||||||
printOrDie(s.out, format, trimString(row.content, row.width))
|
printOrDie(s.out, format, trimString(row.content, row.width))
|
||||||
|
if i != len(rowData)-1 {
|
||||||
|
printOrDie(s.out, " ")
|
||||||
|
}
|
||||||
setColor(s.out, WHITE)
|
setColor(s.out, WHITE)
|
||||||
}
|
}
|
||||||
printOrDie(s.out, "\n")
|
printOrDie(s.out, "\n")
|
||||||
|
|||||||
@@ -22,9 +22,11 @@ func init() {
|
|||||||
_ = clientgoscheme.AddToScheme(scheme)
|
_ = clientgoscheme.AddToScheme(scheme)
|
||||||
}
|
}
|
||||||
|
|
||||||
// getClient returns a client for talking to a Kubernetes cluster. The client
|
type createClientFunc func() (client.Reader, error)
|
||||||
|
|
||||||
|
// createClient returns a client for talking to a Kubernetes cluster. The client
|
||||||
// is from controller-runtime.
|
// is from controller-runtime.
|
||||||
func getClient() (client.Client, error) {
|
func createClient() (client.Reader, error) {
|
||||||
config := ctrl.GetConfigOrDie()
|
config := ctrl.GetConfigOrDie()
|
||||||
mapper, err := apiutil.NewDiscoveryRESTMapper(config)
|
mapper, err := apiutil.NewDiscoveryRESTMapper(config)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -33,6 +35,12 @@ func getClient() (client.Client, error) {
|
|||||||
return client.New(config, client.Options{Scheme: scheme, Mapper: mapper})
|
return client.New(config, client.Options{Scheme: scheme, Mapper: mapper})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func newClientFunc(c client.Reader) func() (client.Reader, error) {
|
||||||
|
return func() (client.Reader, error) {
|
||||||
|
return c, nil
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// CaptureIdentifiersFilter implements the Filter interface in the kio package. It
|
// CaptureIdentifiersFilter implements the Filter interface in the kio package. It
|
||||||
// captures the identifiers for all resources passed through the pipeline.
|
// captures the identifiers for all resources passed through the pipeline.
|
||||||
type CaptureIdentifiersFilter struct {
|
type CaptureIdentifiersFilter struct {
|
||||||
|
|||||||
@@ -18,7 +18,9 @@ import (
|
|||||||
|
|
||||||
// GetWaitRunner return a command WaitRunner.
|
// GetWaitRunner return a command WaitRunner.
|
||||||
func GetWaitRunner() *WaitRunner {
|
func GetWaitRunner() *WaitRunner {
|
||||||
r := &WaitRunner{}
|
r := &WaitRunner{
|
||||||
|
createClientFunc: createClient,
|
||||||
|
}
|
||||||
c := &cobra.Command{
|
c := &cobra.Command{
|
||||||
Use: "wait DIR...",
|
Use: "wait DIR...",
|
||||||
Short: commands.WaitShort,
|
Short: commands.WaitShort,
|
||||||
@@ -48,6 +50,8 @@ type WaitRunner struct {
|
|||||||
Interval time.Duration
|
Interval time.Duration
|
||||||
Timeout time.Duration
|
Timeout time.Duration
|
||||||
Command *cobra.Command
|
Command *cobra.Command
|
||||||
|
|
||||||
|
createClientFunc createClientFunc
|
||||||
}
|
}
|
||||||
|
|
||||||
// runE implements the logic of the command and will call the Wait command in the wait
|
// runE implements the logic of the command and will call the Wait command in the wait
|
||||||
@@ -55,7 +59,7 @@ type WaitRunner struct {
|
|||||||
// TablePrinter to display the information.
|
// TablePrinter to display the information.
|
||||||
func (r *WaitRunner) runE(c *cobra.Command, args []string) error {
|
func (r *WaitRunner) runE(c *cobra.Command, args []string) error {
|
||||||
ctx := context.Background()
|
ctx := context.Background()
|
||||||
client, err := getClient()
|
client, err := r.createClientFunc()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return errors.Wrap(err, "error creating client")
|
return errors.Wrap(err, "error creating client")
|
||||||
}
|
}
|
||||||
|
|||||||
176
cmd/resource/status/cmd/wait_test.go
Normal file
176
cmd/resource/status/cmd/wait_test.go
Normal file
@@ -0,0 +1,176 @@
|
|||||||
|
package cmd
|
||||||
|
|
||||||
|
import (
|
||||||
|
"bytes"
|
||||||
|
"fmt"
|
||||||
|
"reflect"
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
"github.com/acarl005/stripansi"
|
||||||
|
"github.com/stretchr/testify/assert"
|
||||||
|
"sigs.k8s.io/kustomize/kstatus/status"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestWaitNoResources(t *testing.T) {
|
||||||
|
inBuffer := &bytes.Buffer{}
|
||||||
|
outBuffer := &bytes.Buffer{}
|
||||||
|
|
||||||
|
fakeClient := &FakeClient{}
|
||||||
|
|
||||||
|
r := GetWaitRunner()
|
||||||
|
r.createClientFunc = newClientFunc(fakeClient)
|
||||||
|
r.Command.SetArgs([]string{})
|
||||||
|
r.Command.SetIn(inBuffer)
|
||||||
|
r.Command.SetOut(outBuffer)
|
||||||
|
|
||||||
|
err := r.Command.Execute()
|
||||||
|
if !assert.NoError(t, err) {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
cleanOutput := stripansi.Strip(outBuffer.String())
|
||||||
|
tableOutput := parseTableOutput(t, cleanOutput)
|
||||||
|
|
||||||
|
if want, got := 2, len(tableOutput.Frames); want != got {
|
||||||
|
t.Errorf("expected %d frames, but found %d", want, got)
|
||||||
|
}
|
||||||
|
|
||||||
|
aggStatuses := tableOutput.allAggStatuses()
|
||||||
|
expectedAggStatuses := []status.Status{
|
||||||
|
status.UnknownStatus,
|
||||||
|
status.CurrentStatus,
|
||||||
|
}
|
||||||
|
if !reflect.DeepEqual(aggStatuses, expectedAggStatuses) {
|
||||||
|
t.Errorf("expected agg statuses to be %s, but got %s", joinStatuses(expectedAggStatuses),
|
||||||
|
joinStatuses(aggStatuses))
|
||||||
|
}
|
||||||
|
|
||||||
|
resources := tableOutput.resources()
|
||||||
|
if want, got := 0, len(resources); want != got {
|
||||||
|
t.Errorf("expected %d resources, but found %d", want, got)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestWaitMultipleUpdates(t *testing.T) {
|
||||||
|
inBuffer := &bytes.Buffer{}
|
||||||
|
outBuffer := &bytes.Buffer{}
|
||||||
|
|
||||||
|
_, err := fmt.Fprint(inBuffer, `
|
||||||
|
apiVersion: apps/v1
|
||||||
|
kind: Deployment
|
||||||
|
metadata:
|
||||||
|
name: bar
|
||||||
|
namespace: default
|
||||||
|
`)
|
||||||
|
if !assert.NoError(t, err) {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
fakeClient := &FakeClient{
|
||||||
|
resourceCallbackMap: map[string]ResourceGetCallback{
|
||||||
|
"Deployment": createDeploymentStatusFunc(),
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
r := GetWaitRunner()
|
||||||
|
r.createClientFunc = newClientFunc(fakeClient)
|
||||||
|
r.Command.SetArgs([]string{})
|
||||||
|
r.Command.SetIn(inBuffer)
|
||||||
|
r.Command.SetOut(outBuffer)
|
||||||
|
|
||||||
|
err = r.Command.Execute()
|
||||||
|
if !assert.NoError(t, err) {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
cleanOutput := stripansi.Strip(outBuffer.String())
|
||||||
|
tableOutput := parseTableOutput(t, cleanOutput)
|
||||||
|
|
||||||
|
aggStatuses := tableOutput.dedupedAggStatuses()
|
||||||
|
expectedStatuses := []status.Status{
|
||||||
|
status.UnknownStatus,
|
||||||
|
status.InProgressStatus,
|
||||||
|
status.CurrentStatus,
|
||||||
|
}
|
||||||
|
if !reflect.DeepEqual(aggStatuses, expectedStatuses) {
|
||||||
|
t.Errorf("expected deduped agg statuses to be %s, but got %s", joinStatuses(expectedStatuses),
|
||||||
|
joinStatuses(aggStatuses))
|
||||||
|
}
|
||||||
|
|
||||||
|
resources := tableOutput.resources()
|
||||||
|
if want, got := 1, len(resources); got != want {
|
||||||
|
t.Errorf("expected %d resource, but got %d", want, got)
|
||||||
|
}
|
||||||
|
|
||||||
|
resource := resources[0]
|
||||||
|
resourceStatuses := tableOutput.dedupedStatusesForResource(resource)
|
||||||
|
expectedResourceStatuses := []status.Status{
|
||||||
|
status.InProgressStatus,
|
||||||
|
status.CurrentStatus,
|
||||||
|
}
|
||||||
|
if !reflect.DeepEqual(expectedResourceStatuses, resourceStatuses) {
|
||||||
|
t.Errorf("expected resource %q to have statuses %s, but got %s", resource.name,
|
||||||
|
joinStatuses(expectedResourceStatuses), joinStatuses(resourceStatuses))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestWaitMultipleResources(t *testing.T) {
|
||||||
|
inBuffer := &bytes.Buffer{}
|
||||||
|
outBuffer := &bytes.Buffer{}
|
||||||
|
|
||||||
|
_, err := fmt.Fprint(inBuffer, `
|
||||||
|
apiVersion: v1
|
||||||
|
kind: List
|
||||||
|
items:
|
||||||
|
- apiVersion: v1
|
||||||
|
kind: Pod
|
||||||
|
metadata:
|
||||||
|
name: foo
|
||||||
|
namespace: default
|
||||||
|
- apiVersion: v1
|
||||||
|
kind: Service
|
||||||
|
metadata:
|
||||||
|
name: bar
|
||||||
|
namespace: default
|
||||||
|
`)
|
||||||
|
if !assert.NoError(t, err) {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
fakeClient := &FakeClient{
|
||||||
|
resourceCallbackMap: map[string]ResourceGetCallback{
|
||||||
|
"Pod": createPodStatusFunc(),
|
||||||
|
"Service": createServiceStatusFunc(),
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
r := GetWaitRunner()
|
||||||
|
r.createClientFunc = newClientFunc(fakeClient)
|
||||||
|
r.Command.SetArgs([]string{})
|
||||||
|
r.Command.SetIn(inBuffer)
|
||||||
|
r.Command.SetOut(outBuffer)
|
||||||
|
|
||||||
|
err = r.Command.Execute()
|
||||||
|
if !assert.NoError(t, err) {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
cleanOutput := stripansi.Strip(outBuffer.String())
|
||||||
|
tableOutput := parseTableOutput(t, cleanOutput)
|
||||||
|
|
||||||
|
aggStatuses := tableOutput.dedupedAggStatuses()
|
||||||
|
if want, got := status.CurrentStatus, aggStatuses[len(aggStatuses)-1]; want != got {
|
||||||
|
t.Errorf("expected final agg statuses to be %s, but got %s", want, got)
|
||||||
|
}
|
||||||
|
|
||||||
|
resources := tableOutput.resources()
|
||||||
|
if want, got := 2, len(resources); got != want {
|
||||||
|
t.Errorf("expected %d resource, but got %d", want, got)
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, resource := range resources {
|
||||||
|
resourceStatuses := tableOutput.dedupedStatusesForResource(resource)
|
||||||
|
if want, got := status.CurrentStatus, resourceStatuses[len(resourceStatuses)-1]; want != got {
|
||||||
|
t.Errorf("expected resource %q to have final status %s, but got %s", resource.name, want, got)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,10 +1,8 @@
|
|||||||
|
|
||||||
|
|
||||||
// Code generated by "mdtogo"; DO NOT EDIT.
|
// Code generated by "mdtogo"; DO NOT EDIT.
|
||||||
package commands
|
package commands
|
||||||
|
|
||||||
var EventsShort=`[Alpha] Poll the cluster until all provided resources have become Current and list the status change events.`
|
var EventsShort = `[Alpha] Poll the cluster until all provided resources have become Current and list the status change events.`
|
||||||
var EventsLong=`
|
var EventsLong = `
|
||||||
[Alpha] Poll the cluster for the state of all the provided resources until either they have all become
|
[Alpha] Poll the cluster for the state of all the provided resources until either they have all become
|
||||||
Current or the timeout is reached. The output will be status change events.
|
Current or the timeout is reached. The output will be status change events.
|
||||||
|
|
||||||
@@ -14,15 +12,15 @@ on StdIn.
|
|||||||
DIR:
|
DIR:
|
||||||
Path to local directory. If not provided, input is expected on StdIn.
|
Path to local directory. If not provided, input is expected on StdIn.
|
||||||
`
|
`
|
||||||
var EventsExamples=`
|
var EventsExamples = `
|
||||||
# Read resources from the filesystem and wait up to 1 minute for all of them to become Current
|
# Read resources from the filesystem and wait up to 1 minute for all of them to become Current
|
||||||
resource status events my-dir/
|
resource status events my-dir/
|
||||||
|
|
||||||
# Fetch all resources in the cluster and wait up to 5 minutes for all of them to become Current
|
# Fetch all resources in the cluster and wait up to 5 minutes for all of them to become Current
|
||||||
kubectl get all --all-namespaces -oyaml | resource status events --timeout=5m`
|
kubectl get all --all-namespaces -oyaml | resource status events --timeout=5m`
|
||||||
|
|
||||||
var FetchShort=`[Alpha] Fetch the state of the provided resources from the cluster and display status in a table.`
|
var FetchShort = `[Alpha] Fetch the state of the provided resources from the cluster and display status in a table.`
|
||||||
var FetchLong=`
|
var FetchLong = `
|
||||||
[Alpha] Fetches the state of all provided resources from the cluster and displays the status in
|
[Alpha] Fetches the state of all provided resources from the cluster and displays the status in
|
||||||
a table.
|
a table.
|
||||||
|
|
||||||
@@ -31,15 +29,15 @@ The list of resources are provided as manifests either on the filesystem or on S
|
|||||||
DIR:
|
DIR:
|
||||||
Path to local directory.
|
Path to local directory.
|
||||||
`
|
`
|
||||||
var FetchExamples=`
|
var FetchExamples = `
|
||||||
# Read resources from the filesystem and wait up to 1 minute for all of them to become Current
|
# Read resources from the filesystem and wait up to 1 minute for all of them to become Current
|
||||||
resource status fetch my-dir/
|
resource status fetch my-dir/
|
||||||
|
|
||||||
# Fetch all resources in the cluster and wait up to 5 minutes for all of them to become Current
|
# Fetch all resources in the cluster and wait up to 5 minutes for all of them to become Current
|
||||||
kubectl get all --all-namespaces -oyaml | resource status fetch`
|
kubectl get all --all-namespaces -oyaml | resource status fetch`
|
||||||
|
|
||||||
var WaitShort=`[Alpha] Poll the cluster until all provided resources have become Current and display progress in a table. `
|
var WaitShort = `[Alpha] Poll the cluster until all provided resources have become Current and display progress in a table. `
|
||||||
var WaitLong=`
|
var WaitLong = `
|
||||||
[Alpha] Poll the cluster for the state of all the provided resources until either they have all become
|
[Alpha] Poll the cluster for the state of all the provided resources until either they have all become
|
||||||
Current or the timeout is reached. The output will be presented as a table.
|
Current or the timeout is reached. The output will be presented as a table.
|
||||||
|
|
||||||
@@ -49,7 +47,7 @@ on StdIn.
|
|||||||
DIR:
|
DIR:
|
||||||
Path to local directory. If not provided, input is expected on StdIn.
|
Path to local directory. If not provided, input is expected on StdIn.
|
||||||
`
|
`
|
||||||
var WaitExamples=`
|
var WaitExamples = `
|
||||||
# Read resources from the filesystem and wait up to 1 minute for all of them to become Current
|
# Read resources from the filesystem and wait up to 1 minute for all of them to become Current
|
||||||
resource status wait my-dir/
|
resource status wait my-dir/
|
||||||
|
|
||||||
|
|||||||
@@ -196,14 +196,13 @@ By definition, an _exec_ plugin must be executable:
|
|||||||
chmod a+x $MY_PLUGIN_DIR/SillyConfigMapGenerator
|
chmod a+x $MY_PLUGIN_DIR/SillyConfigMapGenerator
|
||||||
```
|
```
|
||||||
|
|
||||||
## Download kustomize 3.0.0
|
## Install kustomize
|
||||||
|
|
||||||
|
Per the [instructions](../../INSTALL.md):
|
||||||
```
|
```
|
||||||
mkdir -p $DEMO/bin
|
curl -s "https://raw.githubusercontent.com/\
|
||||||
gh=https://github.com/kubernetes-sigs/kustomize/releases/download
|
kubernetes-sigs/kustomize/master/hack/install_kustomize.sh" | bash
|
||||||
url=$gh/v3.0.0/kustomize_3.0.0_linux_amd64
|
mv kustomize $DEMO/bin
|
||||||
curl -o $DEMO/bin/kustomize -L $url
|
|
||||||
chmod u+x $DEMO/bin/kustomize
|
|
||||||
```
|
```
|
||||||
|
|
||||||
## Review the layout
|
## Review the layout
|
||||||
|
|||||||
@@ -15,7 +15,7 @@ Resource configuration, and looks for invalid configuration.
|
|||||||
## Function invocation
|
## Function invocation
|
||||||
|
|
||||||
The function is invoked by authoring a [local Resource](local-resource)
|
The function is invoked by authoring a [local Resource](local-resource)
|
||||||
with `metadata.configFn` and running:
|
with `metadata.annotations.[config.kubernetes.io/function]` and running:
|
||||||
|
|
||||||
kustomize config run local-resource/
|
kustomize config run local-resource/
|
||||||
|
|
||||||
|
|||||||
@@ -4,9 +4,10 @@
|
|||||||
apiVersion: examples.config.kubernetes.io/v1beta1
|
apiVersion: examples.config.kubernetes.io/v1beta1
|
||||||
kind: Validator
|
kind: Validator
|
||||||
metadata:
|
metadata:
|
||||||
configFn:
|
annotations:
|
||||||
container:
|
config.kubernetes.io/function: |
|
||||||
image: gcr.io/kustomize-functions/example-tshirt:v0.1.0
|
container:
|
||||||
|
image: gcr.io/kustomize-functions/example-tshirt:v0.1.0
|
||||||
---
|
---
|
||||||
apiVersion: apps/v1
|
apiVersion: apps/v1
|
||||||
kind: Deployment
|
kind: Deployment
|
||||||
|
|||||||
@@ -25,7 +25,7 @@ function input, and writing the function output.
|
|||||||
## Function invocation
|
## Function invocation
|
||||||
|
|
||||||
The function is invoked by authoring a [local Resource](local-resource)
|
The function is invoked by authoring a [local Resource](local-resource)
|
||||||
with `metadata.configFn` and running:
|
with `metadata.annotations.[config.kubernetes.io/function]` and running:
|
||||||
|
|
||||||
kustomize config run local-resource/
|
kustomize config run local-resource/
|
||||||
|
|
||||||
|
|||||||
@@ -5,8 +5,9 @@ apiVersion: examples.config.kubernetes.io/v1beta1 # call `kustomize config run`
|
|||||||
kind: Nginx
|
kind: Nginx
|
||||||
metadata:
|
metadata:
|
||||||
name: demo
|
name: demo
|
||||||
configFn:
|
annotations:
|
||||||
container:
|
config.kubernetes.io/function: |
|
||||||
image: gcr.io/kustomize-functions/example-nginx:v0.1.0
|
container:
|
||||||
|
image: gcr.io/kustomize-functions/example-nginx:v0.1.0
|
||||||
spec:
|
spec:
|
||||||
replicas: 4
|
replicas: 4
|
||||||
|
|||||||
@@ -20,7 +20,7 @@ heavy lifting of implementing the function interface.
|
|||||||
## Function invocation
|
## Function invocation
|
||||||
|
|
||||||
The function is invoked by authoring a [local Resource](local-resource)
|
The function is invoked by authoring a [local Resource](local-resource)
|
||||||
with `metadata.configFn` and running:
|
with `metadata.annotations.[config.kubernetes.io/function]` and running:
|
||||||
|
|
||||||
kustomize config run local-resource/
|
kustomize config run local-resource/
|
||||||
|
|
||||||
|
|||||||
@@ -6,8 +6,9 @@ apiVersion: examples.config.kubernetes.io/v1beta1
|
|||||||
kind: CockroachDB
|
kind: CockroachDB
|
||||||
metadata:
|
metadata:
|
||||||
name: demo
|
name: demo
|
||||||
configFn:
|
annotations:
|
||||||
container:
|
config.kubernetes.io/function: |
|
||||||
image: gcr.io/kustomize-functions/example-cockroachdb:v0.1.0
|
container:
|
||||||
|
image: gcr.io/kustomize-functions/example-cockroachdb:v0.1.0
|
||||||
spec:
|
spec:
|
||||||
replicas: 3
|
replicas: 3
|
||||||
|
|||||||
@@ -18,7 +18,7 @@ the `API` struct definition in [main.go](image/main.go) for documentation.
|
|||||||
## Function invocation
|
## Function invocation
|
||||||
|
|
||||||
The function is invoked by authoring a [local Resource](local-resource)
|
The function is invoked by authoring a [local Resource](local-resource)
|
||||||
with `metadata.configFn` and running:
|
with `metadata.annotations.[config.kubernetes.io/function]` and running:
|
||||||
|
|
||||||
kustomize config run local-resource/
|
kustomize config run local-resource/
|
||||||
|
|
||||||
|
|||||||
@@ -4,9 +4,10 @@
|
|||||||
apiVersion: examples.config.kubernetes.io/v1beta1
|
apiVersion: examples.config.kubernetes.io/v1beta1
|
||||||
kind: Kubeval
|
kind: Kubeval
|
||||||
metadata:
|
metadata:
|
||||||
configFn:
|
annotations:
|
||||||
container:
|
config.kubernetes.io/function: |
|
||||||
image: gcr.io/kustomize-functions/example-validator-kubeval:v0.1.0
|
container:
|
||||||
|
image: gcr.io/kustomize-functions/example-validator-kubeval:v0.1.0
|
||||||
spec:
|
spec:
|
||||||
strict: true
|
strict: true
|
||||||
ignoreMissingSchemas: true
|
ignoreMissingSchemas: true
|
||||||
|
|||||||
@@ -15,7 +15,7 @@ Resource configuration, and looks for invalid configuration.
|
|||||||
## Function invocation
|
## Function invocation
|
||||||
|
|
||||||
The function is invoked by authoring a [local Resource](local-resource)
|
The function is invoked by authoring a [local Resource](local-resource)
|
||||||
with `metadata.configFn` and running:
|
with `metadata.annotations.[config.kubernetes.io/function]` and running:
|
||||||
|
|
||||||
kustomize config run local-resource/
|
kustomize config run local-resource/
|
||||||
|
|
||||||
|
|||||||
@@ -4,9 +4,10 @@
|
|||||||
apiVersion: examples.config.kubernetes.io/v1beta1
|
apiVersion: examples.config.kubernetes.io/v1beta1
|
||||||
kind: Validator
|
kind: Validator
|
||||||
metadata:
|
metadata:
|
||||||
configFn:
|
annotations:
|
||||||
container:
|
config.kubernetes.io/function: |
|
||||||
image: gcr.io/kustomize-functions/example-validator:v0.1.0
|
container:
|
||||||
|
image: gcr.io/kustomize-functions/example-validator:v0.1.0
|
||||||
---
|
---
|
||||||
apiVersion: apps/v1 # this should fail validation
|
apiVersion: apps/v1 # this should fail validation
|
||||||
kind: Deployment
|
kind: Deployment
|
||||||
|
|||||||
67
hack/pinUnpin.sh
Executable file
67
hack/pinUnpin.sh
Executable file
@@ -0,0 +1,67 @@
|
|||||||
|
#!/usr/bin/env bash
|
||||||
|
# Copyright 2019 The Kubernetes Authors.
|
||||||
|
# SPDX-License-Identifier: Apache-2.0
|
||||||
|
#
|
||||||
|
# In general, pin modules to a specific version of the
|
||||||
|
# kustomize API before a release of that module, and
|
||||||
|
# unpin the module after the module release so that
|
||||||
|
# development proceeds against the API's HEAD.
|
||||||
|
#
|
||||||
|
# E.g. for the kustomize CLI module, do this before
|
||||||
|
# releasing the CLI:
|
||||||
|
#
|
||||||
|
# ./hack/pinUnpin.sh pin kustomize v0.3.1
|
||||||
|
#
|
||||||
|
# where v0.3.1 is the most recently released version of
|
||||||
|
# the API, and do the following afterwards:
|
||||||
|
#
|
||||||
|
# ./hack/pinUnpin.sh unPin kustomize
|
||||||
|
|
||||||
|
set -o nounset
|
||||||
|
set -o pipefail
|
||||||
|
|
||||||
|
if [ "$#" -lt 2 ]; then
|
||||||
|
echo "usage:"
|
||||||
|
echo " ./hack/pinUnpin.sh pin kustomize v0.3.1"
|
||||||
|
echo " or "
|
||||||
|
echo " ./hack/pinUnpin.sh unPin kustomize"
|
||||||
|
exit 1
|
||||||
|
fi
|
||||||
|
|
||||||
|
operation=$1
|
||||||
|
if [[ ("$operation" != "pin") && ("$operation" != "unPin") ]]; then
|
||||||
|
echo "unknown operation $operation"
|
||||||
|
exit 1
|
||||||
|
fi
|
||||||
|
|
||||||
|
module=$2
|
||||||
|
if [ ! -d "$module" ]; then
|
||||||
|
echo "directory $module doesn't exist"
|
||||||
|
exit 1
|
||||||
|
fi
|
||||||
|
|
||||||
|
version="unnecessary"
|
||||||
|
if [ "$operation" == "pin" ]; then
|
||||||
|
if [ "$#" -le 2 ]; then
|
||||||
|
echo "Specify version to pin, e.g. '$0 $module pin v0.2.0'"
|
||||||
|
exit 1
|
||||||
|
fi
|
||||||
|
version=$3
|
||||||
|
fi
|
||||||
|
|
||||||
|
function unPin {
|
||||||
|
oldV=$(grep -m 1 sigs.k8s.io/kustomize/api go.mod | awk '{print $NF}')
|
||||||
|
go mod edit -replace=sigs.k8s.io/kustomize/api@${oldV}=../api
|
||||||
|
go mod tidy
|
||||||
|
}
|
||||||
|
|
||||||
|
function pin {
|
||||||
|
oldV=$(grep -m 1 sigs.k8s.io/kustomize/api go.mod | awk '{print $NF}')
|
||||||
|
go mod edit -dropreplace=sigs.k8s.io/kustomize/api@${oldV}
|
||||||
|
go mod edit -require=sigs.k8s.io/kustomize/api@$version
|
||||||
|
go mod tidy
|
||||||
|
}
|
||||||
|
|
||||||
|
pushd $module >& /dev/null
|
||||||
|
$operation
|
||||||
|
popd >& /dev/null
|
||||||
@@ -4,6 +4,7 @@
|
|||||||
package status
|
package status
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"fmt"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/pkg/errors"
|
"github.com/pkg/errors"
|
||||||
@@ -26,6 +27,11 @@ const (
|
|||||||
UnknownStatus Status = "Unknown"
|
UnknownStatus Status = "Unknown"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
var (
|
||||||
|
Statuses = []Status{InProgressStatus, FailedStatus, CurrentStatus, TerminatingStatus, UnknownStatus}
|
||||||
|
ConditionTypes = []ConditionType{ConditionFailed, ConditionInProgress}
|
||||||
|
)
|
||||||
|
|
||||||
// ConditionType defines the set of condition types allowed inside a Condition struct.
|
// ConditionType defines the set of condition types allowed inside a Condition struct.
|
||||||
type ConditionType string
|
type ConditionType string
|
||||||
|
|
||||||
@@ -42,6 +48,18 @@ func (s Status) String() string {
|
|||||||
return string(s)
|
return string(s)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// StatusFromString turns a string into a Status. Will panic if the provided string is
|
||||||
|
// not a valid status.
|
||||||
|
func FromStringOrDie(text string) Status {
|
||||||
|
s := Status(text)
|
||||||
|
for _, r := range Statuses {
|
||||||
|
if s == r {
|
||||||
|
return s
|
||||||
|
}
|
||||||
|
}
|
||||||
|
panic(fmt.Errorf("string has invalid status: %s", s))
|
||||||
|
}
|
||||||
|
|
||||||
// Result contains the results of a call to compute the status of
|
// Result contains the results of a call to compute the status of
|
||||||
// a resource.
|
// a resource.
|
||||||
type Result struct {
|
type Result struct {
|
||||||
|
|||||||
@@ -6,6 +6,7 @@ require (
|
|||||||
github.com/pkg/errors v0.8.1
|
github.com/pkg/errors v0.8.1
|
||||||
github.com/spf13/cobra v0.0.5
|
github.com/spf13/cobra v0.0.5
|
||||||
github.com/spf13/pflag v1.0.5
|
github.com/spf13/pflag v1.0.5
|
||||||
|
k8s.io/client-go v0.17.0
|
||||||
sigs.k8s.io/kustomize/api v0.3.1
|
sigs.k8s.io/kustomize/api v0.3.1
|
||||||
sigs.k8s.io/kustomize/cmd/config v0.0.2
|
sigs.k8s.io/kustomize/cmd/config v0.0.2
|
||||||
sigs.k8s.io/kustomize/cmd/kubectl v0.0.2
|
sigs.k8s.io/kustomize/cmd/kubectl v0.0.2
|
||||||
@@ -17,3 +18,5 @@ exclude (
|
|||||||
github.com/russross/blackfriday v2.0.0+incompatible
|
github.com/russross/blackfriday v2.0.0+incompatible
|
||||||
sigs.k8s.io/kustomize/api v0.2.0
|
sigs.k8s.io/kustomize/api v0.2.0
|
||||||
)
|
)
|
||||||
|
|
||||||
|
replace sigs.k8s.io/kustomize/api v0.3.1 => ../api
|
||||||
|
|||||||
@@ -207,15 +207,6 @@ github.com/googleapis/gnostic v0.0.0-20170729233727-0c5108395e2d h1:7XGaL1e6bYS1
|
|||||||
github.com/googleapis/gnostic v0.0.0-20170729233727-0c5108395e2d/go.mod h1:sJBsCZ4ayReDTBIg8b9dl28c5xFWyhBTVRp3pOg5EKY=
|
github.com/googleapis/gnostic v0.0.0-20170729233727-0c5108395e2d/go.mod h1:sJBsCZ4ayReDTBIg8b9dl28c5xFWyhBTVRp3pOg5EKY=
|
||||||
github.com/gophercloud/gophercloud v0.1.0 h1:P/nh25+rzXouhytV2pUHBb65fnds26Ghl8/391+sT5o=
|
github.com/gophercloud/gophercloud v0.1.0 h1:P/nh25+rzXouhytV2pUHBb65fnds26Ghl8/391+sT5o=
|
||||||
github.com/gophercloud/gophercloud v0.1.0/go.mod h1:vxM41WHh5uqHVBMZHzuwNOHh8XEoIEcSTewFxm1c5g8=
|
github.com/gophercloud/gophercloud v0.1.0/go.mod h1:vxM41WHh5uqHVBMZHzuwNOHh8XEoIEcSTewFxm1c5g8=
|
||||||
github.com/gorilla/context v0.0.0-20160226214623-1ea25387ff6f h1:9oNbS1z4rVpbnkHBdPZU4jo9bSmrLpII768arSyMFgk=
|
|
||||||
github.com/gorilla/context v0.0.0-20160226214623-1ea25387ff6f/go.mod h1:kBGZzfjB9CEq2AlWe17Uuf7NDRt0dE0s8S51q0aT7Yg=
|
|
||||||
github.com/gorilla/mux v1.6.0 h1:UykbtMB/w5No2LmE16gINgLj+r/vbziTgaoERQv6U+0=
|
|
||||||
github.com/gorilla/mux v1.6.0/go.mod h1:1lud6UwP+6orDFRuTfBEV8e9/aOM/c4fVVCaMa2zaAs=
|
|
||||||
github.com/gorilla/securecookie v0.0.0-20160422134519-667fe4e3466a h1:YH0IojQwndMQdeRWdw1aPT8bkbiWaYR3WD+Zf5e09DU=
|
|
||||||
github.com/gorilla/securecookie v0.0.0-20160422134519-667fe4e3466a/go.mod h1:ra0sb63/xPlUeL+yeDciTfxMRAA+MP+HVt/4epWDjd4=
|
|
||||||
github.com/gorilla/sessions v0.0.0-20160922145804-ca9ada445741 h1:OuuPl66BpF1q3OEkaPpp+VfzxrBBY62ATGdWqql/XX8=
|
|
||||||
github.com/gorilla/sessions v0.0.0-20160922145804-ca9ada445741/go.mod h1:+WVp8kdw6VhyKExm03PAMRn2ZxnPtm58pV0dBVPdhHE=
|
|
||||||
github.com/gorilla/websocket v1.2.0/go.mod h1:E7qHFY5m1UJ88s3WnNqhKjPHQ0heANvMoAMk2YaljkQ=
|
|
||||||
github.com/gorilla/websocket v1.4.0 h1:WDFjx/TMzVgy9VdMMQi2K2Emtwi2QcUQsztZ/zLaH/Q=
|
github.com/gorilla/websocket v1.4.0 h1:WDFjx/TMzVgy9VdMMQi2K2Emtwi2QcUQsztZ/zLaH/Q=
|
||||||
github.com/gorilla/websocket v1.4.0/go.mod h1:E7qHFY5m1UJ88s3WnNqhKjPHQ0heANvMoAMk2YaljkQ=
|
github.com/gorilla/websocket v1.4.0/go.mod h1:E7qHFY5m1UJ88s3WnNqhKjPHQ0heANvMoAMk2YaljkQ=
|
||||||
github.com/gostaticanalysis/analysisutil v0.0.0-20190318220348-4088753ea4d3 h1:JVnpOZS+qxli+rgVl98ILOXVNbW+kb5wcxeGx8ShUIw=
|
github.com/gostaticanalysis/analysisutil v0.0.0-20190318220348-4088753ea4d3 h1:JVnpOZS+qxli+rgVl98ILOXVNbW+kb5wcxeGx8ShUIw=
|
||||||
@@ -302,8 +293,6 @@ github.com/modern-go/reflect2 v0.0.0-20180320133207-05fbef0ca5da/go.mod h1:bx2lN
|
|||||||
github.com/modern-go/reflect2 v0.0.0-20180701023420-4b7aa43c6742/go.mod h1:bx2lNnkwVCuqBIxFjflWJWanXIb3RllmbCylyMrvgv0=
|
github.com/modern-go/reflect2 v0.0.0-20180701023420-4b7aa43c6742/go.mod h1:bx2lNnkwVCuqBIxFjflWJWanXIb3RllmbCylyMrvgv0=
|
||||||
github.com/modern-go/reflect2 v1.0.1 h1:9f412s+6RmYXLWZSEzVVgPGK7C2PphHj5RJrvfx9AWI=
|
github.com/modern-go/reflect2 v1.0.1 h1:9f412s+6RmYXLWZSEzVVgPGK7C2PphHj5RJrvfx9AWI=
|
||||||
github.com/modern-go/reflect2 v1.0.1/go.mod h1:bx2lNnkwVCuqBIxFjflWJWanXIb3RllmbCylyMrvgv0=
|
github.com/modern-go/reflect2 v1.0.1/go.mod h1:bx2lNnkwVCuqBIxFjflWJWanXIb3RllmbCylyMrvgv0=
|
||||||
github.com/monopole/mdrip v1.0.1 h1:LzwNWb8ge3+4iBZmxIE6VfUR5KIxhF7DKdf85t8Yvos=
|
|
||||||
github.com/monopole/mdrip v1.0.1/go.mod h1:/7E04hlzRG9Jrp6WILZfYYm/REoJWL2l+MlsCO1eH74=
|
|
||||||
github.com/mozilla/tls-observatory v0.0.0-20190404164649-a3c1b6cfecfd/go.mod h1:SrKMQvPiws7F7iqYp8/TX+IhxCYhzr6N/1yb8cwHsGk=
|
github.com/mozilla/tls-observatory v0.0.0-20190404164649-a3c1b6cfecfd/go.mod h1:SrKMQvPiws7F7iqYp8/TX+IhxCYhzr6N/1yb8cwHsGk=
|
||||||
github.com/munnerz/goautoneg v0.0.0-20120707110453-a547fc61f48d/go.mod h1:+n7T8mK8HuQTcFwEeznm/DIxMOiR9yIdICNftLE1DvQ=
|
github.com/munnerz/goautoneg v0.0.0-20120707110453-a547fc61f48d/go.mod h1:+n7T8mK8HuQTcFwEeznm/DIxMOiR9yIdICNftLE1DvQ=
|
||||||
github.com/mwitkow/go-conntrack v0.0.0-20161129095857-cc309e4a2223/go.mod h1:qRWi+5nqEBWmkhHvq77mSJWrCKwh8bxhgT7d/eI7P4U=
|
github.com/mwitkow/go-conntrack v0.0.0-20161129095857-cc309e4a2223/go.mod h1:qRWi+5nqEBWmkhHvq77mSJWrCKwh8bxhgT7d/eI7P4U=
|
||||||
@@ -361,8 +350,6 @@ github.com/shurcooL/go v0.0.0-20180423040247-9e1955d9fb6e h1:MZM7FHLqUHYI0Y/mQAt
|
|||||||
github.com/shurcooL/go v0.0.0-20180423040247-9e1955d9fb6e/go.mod h1:TDJrrUr11Vxrven61rcy3hJMUqaf/CLWYhHNPmT14Lk=
|
github.com/shurcooL/go v0.0.0-20180423040247-9e1955d9fb6e/go.mod h1:TDJrrUr11Vxrven61rcy3hJMUqaf/CLWYhHNPmT14Lk=
|
||||||
github.com/shurcooL/go-goon v0.0.0-20170922171312-37c2f522c041 h1:llrF3Fs4018ePo4+G/HV/uQUqEI1HMDjCeOf2V6puPc=
|
github.com/shurcooL/go-goon v0.0.0-20170922171312-37c2f522c041 h1:llrF3Fs4018ePo4+G/HV/uQUqEI1HMDjCeOf2V6puPc=
|
||||||
github.com/shurcooL/go-goon v0.0.0-20170922171312-37c2f522c041/go.mod h1:N5mDOmsrJOB+vfqUK+7DmDyjhSLIIBnXo9lvZJj3MWQ=
|
github.com/shurcooL/go-goon v0.0.0-20170922171312-37c2f522c041/go.mod h1:N5mDOmsrJOB+vfqUK+7DmDyjhSLIIBnXo9lvZJj3MWQ=
|
||||||
github.com/shurcooL/sanitized_anchor_name v1.0.0 h1:PdmoCO6wvbs+7yrJyMORt4/BmY5IYyJwS/kOiWx8mHo=
|
|
||||||
github.com/shurcooL/sanitized_anchor_name v1.0.0/go.mod h1:1NzhyTcUVG4SuEtjjoZeVRXNmyL/1OwPU0+IJeTBvfc=
|
|
||||||
github.com/sirupsen/logrus v1.2.0/go.mod h1:LxeOpSwHxABJmUn/MG1IvRgCAasNZTLOkJPxbbu5VWo=
|
github.com/sirupsen/logrus v1.2.0/go.mod h1:LxeOpSwHxABJmUn/MG1IvRgCAasNZTLOkJPxbbu5VWo=
|
||||||
github.com/sirupsen/logrus v1.4.2 h1:SPIRibHv4MatM3XXNO2BJeFLZwZ2LvZgfQ5+UNI2im4=
|
github.com/sirupsen/logrus v1.4.2 h1:SPIRibHv4MatM3XXNO2BJeFLZwZ2LvZgfQ5+UNI2im4=
|
||||||
github.com/sirupsen/logrus v1.4.2/go.mod h1:tLMulIdttU9McNUspp0xgXVQah82FyeX6MwdIuYE2rE=
|
github.com/sirupsen/logrus v1.4.2/go.mod h1:tLMulIdttU9McNUspp0xgXVQah82FyeX6MwdIuYE2rE=
|
||||||
@@ -540,8 +527,6 @@ gopkg.in/fsnotify.v1 v1.4.7/go.mod h1:Tz8NjZHkW78fSQdbUxIjBTcgA1z1m8ZHf0WmKUhAMy
|
|||||||
gopkg.in/inf.v0 v0.9.1 h1:73M5CoZyi3ZLMOyDlQh031Cx6N9NDJ2Vvfl76EDAgDc=
|
gopkg.in/inf.v0 v0.9.1 h1:73M5CoZyi3ZLMOyDlQh031Cx6N9NDJ2Vvfl76EDAgDc=
|
||||||
gopkg.in/inf.v0 v0.9.1/go.mod h1:cWUDdTG/fYaXco+Dcufb5Vnc6Gp2YChqWtbxRZE0mXw=
|
gopkg.in/inf.v0 v0.9.1/go.mod h1:cWUDdTG/fYaXco+Dcufb5Vnc6Gp2YChqWtbxRZE0mXw=
|
||||||
gopkg.in/resty.v1 v1.12.0/go.mod h1:mDo4pnntr5jdWRML875a/NmxYqAlA73dVijT2AXvQQo=
|
gopkg.in/resty.v1 v1.12.0/go.mod h1:mDo4pnntr5jdWRML875a/NmxYqAlA73dVijT2AXvQQo=
|
||||||
gopkg.in/russross/blackfriday.v2 v2.0.0 h1:+FlnIV8DSQnT7NZ43hcVKcdJdzZoeCmJj4Ql8gq5keA=
|
|
||||||
gopkg.in/russross/blackfriday.v2 v2.0.0/go.mod h1:6sSBNz/GtOm/pJTuh5UmBK2ZHfmnxGbl2NZg1UliSOI=
|
|
||||||
gopkg.in/tomb.v1 v1.0.0-20141024135613-dd632973f1e7 h1:uRGJdciOHaEIrze2W8Q3AKkepLTh2hOroT7a+7czfdQ=
|
gopkg.in/tomb.v1 v1.0.0-20141024135613-dd632973f1e7 h1:uRGJdciOHaEIrze2W8Q3AKkepLTh2hOroT7a+7czfdQ=
|
||||||
gopkg.in/tomb.v1 v1.0.0-20141024135613-dd632973f1e7/go.mod h1:dt/ZhP58zS4L8KSrWDmTeBkI65Dw0HsyUHuEVlX15mw=
|
gopkg.in/tomb.v1 v1.0.0-20141024135613-dd632973f1e7/go.mod h1:dt/ZhP58zS4L8KSrWDmTeBkI65Dw0HsyUHuEVlX15mw=
|
||||||
gopkg.in/yaml.v2 v2.0.0-20170812160011-eb3733d160e7/go.mod h1:JAlM8MvJe8wmxCU4Bli9HhUf9+ttbYbLASfIpnQbh74=
|
gopkg.in/yaml.v2 v2.0.0-20170812160011-eb3733d160e7/go.mod h1:JAlM8MvJe8wmxCU4Bli9HhUf9+ttbYbLASfIpnQbh74=
|
||||||
@@ -594,17 +579,12 @@ mvdan.cc/unparam v0.0.0-20190720180237-d51796306d8f h1:Cq7MalBHYACRd6EesksG1Q8Eo
|
|||||||
mvdan.cc/unparam v0.0.0-20190720180237-d51796306d8f/go.mod h1:4G1h5nDURzA3bwVMZIVpwbkw+04kSxk3rAtzlimaUJw=
|
mvdan.cc/unparam v0.0.0-20190720180237-d51796306d8f/go.mod h1:4G1h5nDURzA3bwVMZIVpwbkw+04kSxk3rAtzlimaUJw=
|
||||||
sigs.k8s.io/kustomize v2.0.3+incompatible h1:JUufWFNlI44MdtnjUqVnvh29rR37PQFzPbLXqhyOyX0=
|
sigs.k8s.io/kustomize v2.0.3+incompatible h1:JUufWFNlI44MdtnjUqVnvh29rR37PQFzPbLXqhyOyX0=
|
||||||
sigs.k8s.io/kustomize v2.0.3+incompatible/go.mod h1:MkjgH3RdOWrievjo6c9T245dYlB5QeXV4WCbnt/PEpU=
|
sigs.k8s.io/kustomize v2.0.3+incompatible/go.mod h1:MkjgH3RdOWrievjo6c9T245dYlB5QeXV4WCbnt/PEpU=
|
||||||
sigs.k8s.io/kustomize/api v0.3.0 h1:e7Erw2n8lT8+IWUukktozF0bgWwH2fFC+qsXP0gabg0=
|
|
||||||
sigs.k8s.io/kustomize/api v0.3.0/go.mod h1:4jaPCtRzxfQLFdYq4gYo40dBGW1hyPp/f4AuiZB5dAQ=
|
|
||||||
sigs.k8s.io/kustomize/api v0.3.1 h1:oqMIXvS6tFEUVuKIRUKDa05eC4Hh+cb9JYg8Zhp2d24=
|
|
||||||
sigs.k8s.io/kustomize/api v0.3.1/go.mod h1:A+ATnlHqzictQfQC1q3KB/T6MSr0UWQsrrLxMWkge2E=
|
|
||||||
sigs.k8s.io/kustomize/cmd/config v0.0.2 h1:FphfIoGJ0jGGJJXq9WoG5sqqEIuTeDGx58E5NWHV8Hc=
|
sigs.k8s.io/kustomize/cmd/config v0.0.2 h1:FphfIoGJ0jGGJJXq9WoG5sqqEIuTeDGx58E5NWHV8Hc=
|
||||||
sigs.k8s.io/kustomize/cmd/config v0.0.2/go.mod h1:c6IBoPpAAm5a2aD+0iH8IfeyCF5GPsY5Ws57Dwpcvg0=
|
sigs.k8s.io/kustomize/cmd/config v0.0.2/go.mod h1:c6IBoPpAAm5a2aD+0iH8IfeyCF5GPsY5Ws57Dwpcvg0=
|
||||||
sigs.k8s.io/kustomize/cmd/kubectl v0.0.2 h1:MxUAU5ie0tqx2MuDrUlcAL+Mgt8LVFcXc2scinSD8/w=
|
sigs.k8s.io/kustomize/cmd/kubectl v0.0.2 h1:MxUAU5ie0tqx2MuDrUlcAL+Mgt8LVFcXc2scinSD8/w=
|
||||||
sigs.k8s.io/kustomize/cmd/kubectl v0.0.2/go.mod h1:SbNCE1g937W1yvaQrZbvPNT3aDRdicdeW2qXLTa+YiM=
|
sigs.k8s.io/kustomize/cmd/kubectl v0.0.2/go.mod h1:SbNCE1g937W1yvaQrZbvPNT3aDRdicdeW2qXLTa+YiM=
|
||||||
sigs.k8s.io/kustomize/kyaml v0.0.2 h1:Rl/wMrnpZzZjsVeFIIOAb92Kz/UfLrTUEXjiHW6oS0o=
|
sigs.k8s.io/kustomize/kyaml v0.0.2 h1:Rl/wMrnpZzZjsVeFIIOAb92Kz/UfLrTUEXjiHW6oS0o=
|
||||||
sigs.k8s.io/kustomize/kyaml v0.0.2/go.mod h1:rywm/rcR5LmCBghz9956tE45OdUPChFoXVVs+WmhMTI=
|
sigs.k8s.io/kustomize/kyaml v0.0.2/go.mod h1:rywm/rcR5LmCBghz9956tE45OdUPChFoXVVs+WmhMTI=
|
||||||
sigs.k8s.io/kustomize/pluginator/v2 v2.0.0/go.mod h1:zrXhTv8BAKt0egmZX/8AtMOSFUSWM9YuoHvvqz8/eHE=
|
|
||||||
sigs.k8s.io/structured-merge-diff v0.0.0-20190525122527-15d366b2352e/go.mod h1:wWxsB5ozmmv/SG7nM11ayaAW51xMvak/t1r0CSlcokI=
|
sigs.k8s.io/structured-merge-diff v0.0.0-20190525122527-15d366b2352e/go.mod h1:wWxsB5ozmmv/SG7nM11ayaAW51xMvak/t1r0CSlcokI=
|
||||||
sigs.k8s.io/yaml v1.1.0 h1:4A07+ZFc2wgJwo8YNlQpr1rVlgUDlxXHhPJciaPY5gs=
|
sigs.k8s.io/yaml v1.1.0 h1:4A07+ZFc2wgJwo8YNlQpr1rVlgUDlxXHhPJciaPY5gs=
|
||||||
sigs.k8s.io/yaml v1.1.0/go.mod h1:UJmg0vDUVViEyp3mgSv9WPwZCDxu4rQW1olrI1uml+o=
|
sigs.k8s.io/yaml v1.1.0/go.mod h1:UJmg0vDUVViEyp3mgSv9WPwZCDxu4rQW1olrI1uml+o=
|
||||||
|
|||||||
@@ -6,35 +6,22 @@ package main
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"os"
|
"os"
|
||||||
"path/filepath"
|
|
||||||
|
|
||||||
"github.com/spf13/cobra"
|
|
||||||
"sigs.k8s.io/kustomize/cmd/config/complete"
|
"sigs.k8s.io/kustomize/cmd/config/complete"
|
||||||
"sigs.k8s.io/kustomize/kustomize/v3/internal/commands"
|
"sigs.k8s.io/kustomize/kustomize/v3/internal/commands"
|
||||||
|
|
||||||
|
// initialize auth
|
||||||
|
// This is here rather than in the libraries because of
|
||||||
|
// https://github.com/kubernetes-sigs/kustomize/issues/2060
|
||||||
|
_ "k8s.io/client-go/plugin/pkg/client/auth"
|
||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
cmd := commands.NewDefaultCommand()
|
cmd := commands.NewDefaultCommand()
|
||||||
completion(cmd)
|
complete.Complete(cmd).Complete("kustomize")
|
||||||
|
|
||||||
if err := cmd.Execute(); err != nil {
|
if err := cmd.Execute(); err != nil {
|
||||||
os.Exit(1)
|
os.Exit(1)
|
||||||
}
|
}
|
||||||
os.Exit(0)
|
os.Exit(0)
|
||||||
}
|
}
|
||||||
|
|
||||||
// completion performs shell completion if kustomize is being called to provide
|
|
||||||
// shell completion commands.
|
|
||||||
func completion(cmd *cobra.Command) {
|
|
||||||
// bash shell completion passes the command name as the first argument
|
|
||||||
// do this after configuring cmd so it has all the subcommands
|
|
||||||
if len(os.Args) > 1 {
|
|
||||||
// use the base name in case kustomize is called with an absolute path
|
|
||||||
name := filepath.Base(os.Args[1])
|
|
||||||
if name == "kustomize" {
|
|
||||||
// complete calls kustomize with itself as an argument
|
|
||||||
complete.Complete(cmd).Complete("kustomize")
|
|
||||||
os.Exit(0)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|||||||
@@ -1,7 +1,7 @@
|
|||||||
# Copyright 2019 The Kubernetes Authors.
|
# Copyright 2019 The Kubernetes Authors.
|
||||||
# SPDX-License-Identifier: Apache-2.0
|
# SPDX-License-Identifier: Apache-2.0
|
||||||
|
|
||||||
.PHONY: generate license fix vet fmt test lint tidy
|
.PHONY: generate license fix vet fmt test lint tidy openapi
|
||||||
|
|
||||||
GOPATH := $(shell go env GOPATH)
|
GOPATH := $(shell go env GOPATH)
|
||||||
|
|
||||||
@@ -32,3 +32,7 @@ test:
|
|||||||
|
|
||||||
vet:
|
vet:
|
||||||
go vet ./...
|
go vet ./...
|
||||||
|
|
||||||
|
openapi:
|
||||||
|
(which $(GOPATH)/bin/go-bindata || go get -v github.com/go-bindata/go-bindata)
|
||||||
|
go-bindata --pkg openapi -o openapi/swagger.go openapi/swagger.json
|
||||||
|
|||||||
@@ -6,11 +6,13 @@ package copyutil
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"bytes"
|
"bytes"
|
||||||
|
"fmt"
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
"os"
|
"os"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
|
"github.com/sergi/go-diff/diffmatchpatch"
|
||||||
"sigs.k8s.io/kustomize/kyaml/sets"
|
"sigs.k8s.io/kustomize/kyaml/sets"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -119,6 +121,9 @@ func Diff(sourceDir, destDir string) (sets.String, error) {
|
|||||||
return diff, err
|
return diff, err
|
||||||
}
|
}
|
||||||
if !bytes.Equal(b1, b2) {
|
if !bytes.Equal(b1, b2) {
|
||||||
|
dmp := diffmatchpatch.New()
|
||||||
|
diffs := dmp.DiffMain(string(b1), string(b2), false)
|
||||||
|
fmt.Println(dmp.DiffPrettyText(diffs))
|
||||||
diff.Insert(f)
|
diff.Insert(f)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -6,7 +6,9 @@ require (
|
|||||||
github.com/davecgh/go-spew v1.1.1
|
github.com/davecgh/go-spew v1.1.1
|
||||||
github.com/go-errors/errors v1.0.1
|
github.com/go-errors/errors v1.0.1
|
||||||
github.com/go-openapi/spec v0.19.5
|
github.com/go-openapi/spec v0.19.5
|
||||||
|
github.com/sergi/go-diff v1.1.0
|
||||||
github.com/stretchr/testify v1.4.0
|
github.com/stretchr/testify v1.4.0
|
||||||
github.com/xlab/treeprint v0.0.0-20181112141820-a009c3971eca
|
github.com/xlab/treeprint v0.0.0-20181112141820-a009c3971eca
|
||||||
|
gopkg.in/yaml.v2 v2.2.4
|
||||||
gopkg.in/yaml.v3 v3.0.0-20191026110619-0b21df46bc1d
|
gopkg.in/yaml.v3 v3.0.0-20191026110619-0b21df46bc1d
|
||||||
)
|
)
|
||||||
|
|||||||
@@ -27,6 +27,8 @@ github.com/mailru/easyjson v0.0.0-20190626092158-b2ccc519800e h1:hB2xlXdHp/pmPZq
|
|||||||
github.com/mailru/easyjson v0.0.0-20190626092158-b2ccc519800e/go.mod h1:C1wdFJiN94OJF2b5HbByQZoLdCWB1Yqtg26g4irojpc=
|
github.com/mailru/easyjson v0.0.0-20190626092158-b2ccc519800e/go.mod h1:C1wdFJiN94OJF2b5HbByQZoLdCWB1Yqtg26g4irojpc=
|
||||||
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
|
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
|
||||||
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
|
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
|
||||||
|
github.com/sergi/go-diff v1.1.0 h1:we8PVUC3FE2uYfodKH/nBHMSetSfHDR6scGdBi+erh0=
|
||||||
|
github.com/sergi/go-diff v1.1.0/go.mod h1:STckp+ISIX8hZLjrqAeVduY0gWCT9IjLuqbuNXdaHfM=
|
||||||
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
|
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
|
||||||
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
|
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
|
||||||
github.com/stretchr/testify v1.4.0 h1:2E4SXV/wtOkTonXsotYi4li6zVWxYlZuYNCXe9XRJyk=
|
github.com/stretchr/testify v1.4.0 h1:2E4SXV/wtOkTonXsotYi4li6zVWxYlZuYNCXe9XRJyk=
|
||||||
@@ -45,6 +47,8 @@ golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGm
|
|||||||
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
|
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
|
||||||
gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127 h1:qIbj1fsPNlZgppZ+VLlY7N33q108Sa+fhmuc+sWQYwY=
|
gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127 h1:qIbj1fsPNlZgppZ+VLlY7N33q108Sa+fhmuc+sWQYwY=
|
||||||
gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
|
gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
|
||||||
|
gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15 h1:YR8cESwS4TdDjEe65xsg0ogRM/Nc3DYOhEAlW+xobZo=
|
||||||
|
gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
|
||||||
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
|
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
|
||||||
gopkg.in/yaml.v2 v2.2.4 h1:/eiJrUcujPVeJ3xlSWaiNi3uSVmDGBK1pDHUHAnao1I=
|
gopkg.in/yaml.v2 v2.2.4 h1:/eiJrUcujPVeJ3xlSWaiNi3uSVmDGBK1pDHUHAnao1I=
|
||||||
gopkg.in/yaml.v2 v2.2.4/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
|
gopkg.in/yaml.v2 v2.2.4/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
|
||||||
|
|||||||
@@ -8,8 +8,10 @@ import (
|
|||||||
"fmt"
|
"fmt"
|
||||||
"os"
|
"os"
|
||||||
"os/exec"
|
"os/exec"
|
||||||
|
"path"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
|
"sigs.k8s.io/kustomize/kyaml/errors"
|
||||||
"sigs.k8s.io/kustomize/kyaml/kio"
|
"sigs.k8s.io/kustomize/kyaml/kio"
|
||||||
"sigs.k8s.io/kustomize/kyaml/kio/kioutil"
|
"sigs.k8s.io/kustomize/kyaml/kio/kioutil"
|
||||||
|
|
||||||
@@ -24,6 +26,106 @@ import (
|
|||||||
// non-zero.
|
// non-zero.
|
||||||
// The full set of environment variables from the parent process
|
// The full set of environment variables from the parent process
|
||||||
// are passed to the container.
|
// are passed to the container.
|
||||||
|
//
|
||||||
|
// Function Scoping:
|
||||||
|
// ContainerFilter applies the function only to Resources to which it is scoped.
|
||||||
|
//
|
||||||
|
// Resources are scoped to a function if any of the following are true:
|
||||||
|
// - the Resource were read from the same directory as the function config
|
||||||
|
// - the Resource were read from a subdirectory of the function config directory
|
||||||
|
// - the function config is in a directory named "functions" and
|
||||||
|
// they were read from a subdirectory of "functions" parent
|
||||||
|
// - the function config doesn't have a path annotation (considered globally scoped)
|
||||||
|
// - the ContainerFilter has GlobalScope == true
|
||||||
|
//
|
||||||
|
// In Scope Examples:
|
||||||
|
//
|
||||||
|
// Example 1: deployment.yaml and service.yaml in function.yaml scope
|
||||||
|
// same directory as the function config directory
|
||||||
|
// .
|
||||||
|
// ├── function.yaml
|
||||||
|
// ├── deployment.yaml
|
||||||
|
// └── service.yaml
|
||||||
|
//
|
||||||
|
// Example 2: apps/deployment.yaml and apps/service.yaml in function.yaml scope
|
||||||
|
// subdirectory of the function config directory
|
||||||
|
// .
|
||||||
|
// ├── function.yaml
|
||||||
|
// └── apps
|
||||||
|
// ├── deployment.yaml
|
||||||
|
// └── service.yaml
|
||||||
|
//
|
||||||
|
// Example 3: apps/deployment.yaml and apps/service.yaml in functions/function.yaml scope
|
||||||
|
// function config is in a directory named "functions"
|
||||||
|
// .
|
||||||
|
// ├── functions
|
||||||
|
// │ └── function.yaml
|
||||||
|
// └── apps
|
||||||
|
// ├── deployment.yaml
|
||||||
|
// └── service.yaml
|
||||||
|
//
|
||||||
|
// Out of Scope Examples:
|
||||||
|
//
|
||||||
|
// Example 1: apps/deployment.yaml and apps/service.yaml NOT in stuff/function.yaml scope
|
||||||
|
// .
|
||||||
|
// ├── stuff
|
||||||
|
// │ └── function.yaml
|
||||||
|
// └── apps
|
||||||
|
// ├── deployment.yaml
|
||||||
|
// └── service.yaml
|
||||||
|
//
|
||||||
|
// Example 2: apps/deployment.yaml and apps/service.yaml NOT in stuff/functions/function.yaml scope
|
||||||
|
// .
|
||||||
|
// ├── stuff
|
||||||
|
// │ └── functions
|
||||||
|
// │ └── function.yaml
|
||||||
|
// └── apps
|
||||||
|
// ├── deployment.yaml
|
||||||
|
// └── service.yaml
|
||||||
|
//
|
||||||
|
// Default Paths:
|
||||||
|
// Resources emitted by functions will have default path applied as annotations
|
||||||
|
// if none is present.
|
||||||
|
// The default path will be the function-dir/ (or parent directory in the case of "functions")
|
||||||
|
// + function-file-name/ + namespace/ + kind_name.yaml
|
||||||
|
//
|
||||||
|
// Example 1: Given a function in fn.yaml that produces a Deployment name foo and a Service named bar
|
||||||
|
// dir
|
||||||
|
// └── fn.yaml
|
||||||
|
//
|
||||||
|
// Would default newly generated Resources to:
|
||||||
|
//
|
||||||
|
// dir
|
||||||
|
// ├── fn.yaml
|
||||||
|
// └── fn
|
||||||
|
// ├── deployment_foo.yaml
|
||||||
|
// └── service_bar.yaml
|
||||||
|
//
|
||||||
|
// Example 2: Given a function in functions/fn.yaml that produces a Deployment name foo and a Service named bar
|
||||||
|
// dir
|
||||||
|
// └── fn.yaml
|
||||||
|
//
|
||||||
|
// Would default newly generated Resources to:
|
||||||
|
//
|
||||||
|
// dir
|
||||||
|
// ├── functions
|
||||||
|
// │ └── fn.yaml
|
||||||
|
// └── fn
|
||||||
|
// ├── deployment_foo.yaml
|
||||||
|
// └── service_bar.yaml
|
||||||
|
//
|
||||||
|
// Example 3: Given a function in fn.yaml that produces a Deployment name foo, namespace baz and a Service named bar namespace baz
|
||||||
|
// dir
|
||||||
|
// └── fn.yaml
|
||||||
|
//
|
||||||
|
// Would default newly generated Resources to:
|
||||||
|
//
|
||||||
|
// dir
|
||||||
|
// ├── fn.yaml
|
||||||
|
// └── fn
|
||||||
|
// └── baz
|
||||||
|
// ├── deployment_foo.yaml
|
||||||
|
// └── service_bar.yaml
|
||||||
type ContainerFilter struct {
|
type ContainerFilter struct {
|
||||||
|
|
||||||
// Image is the container image to use to create a container.
|
// Image is the container image to use to create a container.
|
||||||
@@ -40,6 +142,10 @@ type ContainerFilter struct {
|
|||||||
// Typically a Kubernetes style Resource Config.
|
// Typically a Kubernetes style Resource Config.
|
||||||
Config *yaml.RNode `yaml:"config,omitempty"`
|
Config *yaml.RNode `yaml:"config,omitempty"`
|
||||||
|
|
||||||
|
// GlobalScope will cause the function to be run against all input
|
||||||
|
// nodes instead of only nodes scoped under the function.
|
||||||
|
GlobalScope bool
|
||||||
|
|
||||||
// args may be specified by tests to override how a container is spawned
|
// args may be specified by tests to override how a container is spawned
|
||||||
args []string
|
args []string
|
||||||
|
|
||||||
@@ -65,8 +171,76 @@ func (s *StorageMount) String() string {
|
|||||||
return fmt.Sprintf("type=%s,src=%s,dst=%s:ro", s.MountType, s.Src, s.DstPath)
|
return fmt.Sprintf("type=%s,src=%s,dst=%s:ro", s.MountType, s.Src, s.DstPath)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// functionsDirectoryName is keyword directory name for functions scoped 1 directory higher
|
||||||
|
const functionsDirectoryName = "functions"
|
||||||
|
|
||||||
|
// getFunctionScope returns the path of the directory containing the function config,
|
||||||
|
// or its parent directory if the base directory is named "functions"
|
||||||
|
func (c *ContainerFilter) getFunctionScope() (string, error) {
|
||||||
|
m, err := c.Config.GetMeta()
|
||||||
|
if err != nil {
|
||||||
|
return "", errors.Wrap(err)
|
||||||
|
}
|
||||||
|
p, found := m.Annotations[kioutil.PathAnnotation]
|
||||||
|
if !found {
|
||||||
|
return "", nil
|
||||||
|
}
|
||||||
|
|
||||||
|
functionDir := path.Clean(path.Dir(p))
|
||||||
|
|
||||||
|
if path.Base(functionDir) == functionsDirectoryName {
|
||||||
|
// the scope of functions in a directory called "functions" is 1 level higher
|
||||||
|
// this is similar to how the golang "internal" directory scoping works
|
||||||
|
functionDir = path.Dir(functionDir)
|
||||||
|
}
|
||||||
|
return functionDir, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// scope partitions the input nodes into 2 slices. The first slice contains only Resources
|
||||||
|
// which are scoped under dir, and the second slice contains the Resources which are not.
|
||||||
|
func (c *ContainerFilter) scope(dir string, nodes []*yaml.RNode) ([]*yaml.RNode, []*yaml.RNode, error) {
|
||||||
|
// scope container filtered Resources to Resources under that directory
|
||||||
|
var input, saved []*yaml.RNode
|
||||||
|
if c.GlobalScope {
|
||||||
|
return nodes, nil, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
if dir == "" {
|
||||||
|
// global function
|
||||||
|
return nodes, nil, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// identify Resources read from directories under the function configuration
|
||||||
|
for i := range nodes {
|
||||||
|
m, err := nodes[i].GetMeta()
|
||||||
|
if err != nil {
|
||||||
|
return nil, nil, err
|
||||||
|
}
|
||||||
|
p, found := m.Annotations[kioutil.PathAnnotation]
|
||||||
|
if !found {
|
||||||
|
// this Resource isn't scoped under the function -- don't know where it came from
|
||||||
|
// consider it out of scope
|
||||||
|
saved = append(saved, nodes[i])
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
resourceDir := path.Clean(path.Dir(p))
|
||||||
|
if !strings.HasPrefix(resourceDir, dir) {
|
||||||
|
// this Resource doesn't fall under the function scope if it
|
||||||
|
// isn't in a subdirectory of where the function lives
|
||||||
|
saved = append(saved, nodes[i])
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
// this input is scoped under the function
|
||||||
|
input = append(input, nodes[i])
|
||||||
|
}
|
||||||
|
|
||||||
|
return input, saved, nil
|
||||||
|
}
|
||||||
|
|
||||||
// GrepFilter implements kio.GrepFilter
|
// GrepFilter implements kio.GrepFilter
|
||||||
func (c *ContainerFilter) Filter(input []*yaml.RNode) ([]*yaml.RNode, error) {
|
func (c *ContainerFilter) Filter(nodes []*yaml.RNode) ([]*yaml.RNode, error) {
|
||||||
// get the command to filter the Resources
|
// get the command to filter the Resources
|
||||||
cmd, err := c.getCommand()
|
cmd, err := c.getCommand()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -76,11 +250,23 @@ func (c *ContainerFilter) Filter(input []*yaml.RNode) ([]*yaml.RNode, error) {
|
|||||||
in := &bytes.Buffer{}
|
in := &bytes.Buffer{}
|
||||||
out := &bytes.Buffer{}
|
out := &bytes.Buffer{}
|
||||||
|
|
||||||
|
// only process Resources scoped to this function, save the others
|
||||||
|
functionDir, err := c.getFunctionScope()
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
input, saved, err := c.scope(functionDir, nodes)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
// write the input
|
// write the input
|
||||||
err = kio.ByteWriter{
|
err = kio.ByteWriter{
|
||||||
WrappingAPIVersion: kio.ResourceListAPIVersion,
|
WrappingAPIVersion: kio.ResourceListAPIVersion,
|
||||||
WrappingKind: kio.ResourceListKind,
|
WrappingKind: kio.ResourceListKind,
|
||||||
Writer: in, KeepReaderAnnotations: true, FunctionConfig: c.Config}.Write(input)
|
Writer: in,
|
||||||
|
KeepReaderAnnotations: true,
|
||||||
|
FunctionConfig: c.Config}.Write(input)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
@@ -98,7 +284,19 @@ func (c *ContainerFilter) Filter(input []*yaml.RNode) ([]*yaml.RNode, error) {
|
|||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
return r.Read()
|
output, err := r.Read()
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
// annotate any generated Resources with a path and index if they don't already have one
|
||||||
|
if err := kioutil.DefaultPathAnnotation(functionDir, output); err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
// emit both the Resources output from the function, and the out-of-scope Resources
|
||||||
|
// which were not provided to the function
|
||||||
|
return append(output, saved...), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// getArgs returns the command + args to run to spawn the container
|
// getArgs returns the command + args to run to spawn the container
|
||||||
@@ -139,7 +337,7 @@ func (c *ContainerFilter) getArgs() []string {
|
|||||||
return append(args, c.Image)
|
return append(args, c.Image)
|
||||||
}
|
}
|
||||||
|
|
||||||
// getCommand returns a command which will apply the GrepFilter using the container image
|
// getCommand returns a command which will apply the Filter using the container image
|
||||||
func (c *ContainerFilter) getCommand() (*exec.Cmd, error) {
|
func (c *ContainerFilter) getCommand() (*exec.Cmd, error) {
|
||||||
// encode the filter command API configuration
|
// encode the filter command API configuration
|
||||||
cfg := &bytes.Buffer{}
|
cfg := &bytes.Buffer{}
|
||||||
@@ -194,6 +392,13 @@ func (c *IsReconcilerFilter) Filter(inputs []*yaml.RNode) ([]*yaml.RNode, error)
|
|||||||
return out, nil
|
return out, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const (
|
||||||
|
FunctionAnnotationKey = "config.kubernetes.io/function"
|
||||||
|
oldFunctionAnnotationKey = "config.k8s.io/function"
|
||||||
|
)
|
||||||
|
|
||||||
|
var functionAnnotationKeys = []string{FunctionAnnotationKey, oldFunctionAnnotationKey}
|
||||||
|
|
||||||
// GetContainerName returns the container image for an API if one exists
|
// GetContainerName returns the container image for an API if one exists
|
||||||
func GetContainerName(n *yaml.RNode) (string, string) {
|
func GetContainerName(n *yaml.RNode) (string, string) {
|
||||||
meta, _ := n.GetMeta()
|
meta, _ := n.GetMeta()
|
||||||
@@ -201,11 +406,14 @@ func GetContainerName(n *yaml.RNode) (string, string) {
|
|||||||
// path to the function, this will be mounted into the container
|
// path to the function, this will be mounted into the container
|
||||||
path := meta.Annotations[kioutil.PathAnnotation]
|
path := meta.Annotations[kioutil.PathAnnotation]
|
||||||
|
|
||||||
functionAnnotation := meta.Annotations["config.k8s.io/function"]
|
// check previous keys for backwards compatibility
|
||||||
if functionAnnotation != "" {
|
for _, s := range functionAnnotationKeys {
|
||||||
annotationContent, _ := yaml.Parse(functionAnnotation)
|
functionAnnotation := meta.Annotations[s]
|
||||||
image, _ := annotationContent.Pipe(yaml.Lookup("container", "image"))
|
if functionAnnotation != "" {
|
||||||
return image.YNode().Value, path
|
annotationContent, _ := yaml.Parse(functionAnnotation)
|
||||||
|
image, _ := annotationContent.Pipe(yaml.Lookup("container", "image"))
|
||||||
|
return image.YNode().Value, path
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
container := meta.Annotations["config.kubernetes.io/container"]
|
container := meta.Annotations["config.kubernetes.io/container"]
|
||||||
|
|||||||
@@ -16,7 +16,7 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
func TestFilter_command(t *testing.T) {
|
func TestFilter_command(t *testing.T) {
|
||||||
cfg, err := yaml.Parse(`apiversion: apps/v1
|
cfg, err := yaml.Parse(`apiVersion: apps/v1
|
||||||
kind: Deployment
|
kind: Deployment
|
||||||
metadata:
|
metadata:
|
||||||
name: foo
|
name: foo
|
||||||
@@ -62,7 +62,7 @@ metadata:
|
|||||||
}
|
}
|
||||||
|
|
||||||
func TestFilter_command_StorageMount(t *testing.T) {
|
func TestFilter_command_StorageMount(t *testing.T) {
|
||||||
cfg, err := yaml.Parse(`apiversion: apps/v1
|
cfg, err := yaml.Parse(`apiVersion: apps/v1
|
||||||
kind: Deployment
|
kind: Deployment
|
||||||
metadata:
|
metadata:
|
||||||
name: foo
|
name: foo
|
||||||
@@ -103,7 +103,7 @@ metadata:
|
|||||||
}
|
}
|
||||||
|
|
||||||
func TestFilter_command_network(t *testing.T) {
|
func TestFilter_command_network(t *testing.T) {
|
||||||
cfg, err := yaml.Parse(`apiversion: apps/v1
|
cfg, err := yaml.Parse(`apiVersion: apps/v1
|
||||||
kind: Deployment
|
kind: Deployment
|
||||||
metadata:
|
metadata:
|
||||||
name: foo
|
name: foo
|
||||||
@@ -213,6 +213,7 @@ metadata:
|
|||||||
name: deployment-foo
|
name: deployment-foo
|
||||||
annotations:
|
annotations:
|
||||||
config.kubernetes.io/index: '0'
|
config.kubernetes.io/index: '0'
|
||||||
|
config.kubernetes.io/path: 'statefulset_deployment-foo.yaml'
|
||||||
---
|
---
|
||||||
apiVersion: v1
|
apiVersion: v1
|
||||||
kind: Service
|
kind: Service
|
||||||
@@ -220,11 +221,12 @@ metadata:
|
|||||||
name: service-foo
|
name: service-foo
|
||||||
annotations:
|
annotations:
|
||||||
config.kubernetes.io/index: '1'
|
config.kubernetes.io/index: '1'
|
||||||
|
config.kubernetes.io/path: 'service_service-foo.yaml'
|
||||||
`, b.String())
|
`, b.String())
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestFilter_Filter_noChange(t *testing.T) {
|
func TestFilter_Filter_noChange(t *testing.T) {
|
||||||
cfg, err := yaml.Parse(`apiversion: apps/v1
|
cfg, err := yaml.Parse(`apiVersion: apps/v1
|
||||||
kind: Deployment
|
kind: Deployment
|
||||||
metadata:
|
metadata:
|
||||||
name: foo
|
name: foo
|
||||||
@@ -234,7 +236,7 @@ metadata:
|
|||||||
}
|
}
|
||||||
|
|
||||||
input, err := (&kio.ByteReader{Reader: bytes.NewBufferString(`
|
input, err := (&kio.ByteReader{Reader: bytes.NewBufferString(`
|
||||||
apiversion: apps/v1
|
apiVersion: apps/v1
|
||||||
kind: Deployment
|
kind: Deployment
|
||||||
metadata:
|
metadata:
|
||||||
name: deployment-foo
|
name: deployment-foo
|
||||||
@@ -258,7 +260,7 @@ metadata:
|
|||||||
if !assert.Equal(t, `apiVersion: config.kubernetes.io/v1alpha1
|
if !assert.Equal(t, `apiVersion: config.kubernetes.io/v1alpha1
|
||||||
kind: ResourceList
|
kind: ResourceList
|
||||||
items:
|
items:
|
||||||
- apiversion: apps/v1
|
- apiVersion: apps/v1
|
||||||
kind: Deployment
|
kind: Deployment
|
||||||
metadata:
|
metadata:
|
||||||
name: deployment-foo
|
name: deployment-foo
|
||||||
@@ -270,7 +272,7 @@ items:
|
|||||||
name: service-foo
|
name: service-foo
|
||||||
annotations:
|
annotations:
|
||||||
config.kubernetes.io/index: '1'
|
config.kubernetes.io/index: '1'
|
||||||
functionConfig: {apiversion: apps/v1, kind: Deployment, metadata: {name: foo}}
|
functionConfig: {apiVersion: apps/v1, kind: Deployment, metadata: {name: foo}}
|
||||||
`, s) {
|
`, s) {
|
||||||
t.FailNow()
|
t.FailNow()
|
||||||
}
|
}
|
||||||
@@ -289,12 +291,13 @@ functionConfig: {apiversion: apps/v1, kind: Deployment, metadata: {name: foo}}
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
assert.Equal(t, `apiversion: apps/v1
|
assert.Equal(t, `apiVersion: apps/v1
|
||||||
kind: Deployment
|
kind: Deployment
|
||||||
metadata:
|
metadata:
|
||||||
name: deployment-foo
|
name: deployment-foo
|
||||||
annotations:
|
annotations:
|
||||||
config.kubernetes.io/index: '0'
|
config.kubernetes.io/index: '0'
|
||||||
|
config.kubernetes.io/path: 'deployment_deployment-foo.yaml'
|
||||||
---
|
---
|
||||||
apiVersion: v1
|
apiVersion: v1
|
||||||
kind: Service
|
kind: Service
|
||||||
@@ -302,6 +305,7 @@ metadata:
|
|||||||
name: service-foo
|
name: service-foo
|
||||||
annotations:
|
annotations:
|
||||||
config.kubernetes.io/index: '1'
|
config.kubernetes.io/index: '1'
|
||||||
|
config.kubernetes.io/path: 'service_service-foo.yaml'
|
||||||
`, b.String())
|
`, b.String())
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -333,12 +337,12 @@ metadata:
|
|||||||
c, _ = GetContainerName(n)
|
c, _ = GetContainerName(n)
|
||||||
assert.Equal(t, "gcr.io/foo/bar:something", c)
|
assert.Equal(t, "gcr.io/foo/bar:something", c)
|
||||||
|
|
||||||
// container from config.k8s.io/function annotation
|
// container from config.kubernetes.io/function annotation
|
||||||
n, err = yaml.Parse(`apiVersion: v1
|
n, err = yaml.Parse(`apiVersion: v1
|
||||||
kind: MyThing
|
kind: MyThing
|
||||||
metadata:
|
metadata:
|
||||||
annotations:
|
annotations:
|
||||||
config.k8s.io/function: |
|
config.kubernetes.io/function: |
|
||||||
container:
|
container:
|
||||||
image: gcr.io/foo/bar:something
|
image: gcr.io/foo/bar:something
|
||||||
`)
|
`)
|
||||||
@@ -359,3 +363,418 @@ metadata:
|
|||||||
c, _ = GetContainerName(n)
|
c, _ = GetContainerName(n)
|
||||||
assert.Equal(t, "", c)
|
assert.Equal(t, "", c)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestFilter_Filter_defaultNaming(t *testing.T) {
|
||||||
|
cfg, err := yaml.Parse(`apiVersion: apps/v1
|
||||||
|
kind: Deployment
|
||||||
|
metadata:
|
||||||
|
name: foo
|
||||||
|
annotations:
|
||||||
|
config.kubernetes.io/path: 'foo/bar.yaml'
|
||||||
|
`)
|
||||||
|
if !assert.NoError(t, err) {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
input, err := (&kio.ByteReader{Reader: bytes.NewBufferString(``)}).Read()
|
||||||
|
if !assert.NoError(t, err) {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
called := false
|
||||||
|
result, err := (&ContainerFilter{
|
||||||
|
Image: "example.com:version",
|
||||||
|
Config: cfg,
|
||||||
|
args: []string{"echo", `apiVersion: apps/v1
|
||||||
|
kind: Deployment
|
||||||
|
metadata:
|
||||||
|
name: deployment-foo
|
||||||
|
---
|
||||||
|
apiVersion: v1
|
||||||
|
kind: Service
|
||||||
|
metadata:
|
||||||
|
name: service-foo
|
||||||
|
`},
|
||||||
|
checkInput: func(s string) {
|
||||||
|
called = true
|
||||||
|
if !assert.Equal(t, `apiVersion: config.kubernetes.io/v1alpha1
|
||||||
|
kind: ResourceList
|
||||||
|
items: []
|
||||||
|
functionConfig: {apiVersion: apps/v1, kind: Deployment, metadata: {name: foo, annotations: {
|
||||||
|
config.kubernetes.io/path: 'foo/bar.yaml'}}}
|
||||||
|
`, s) {
|
||||||
|
t.FailNow()
|
||||||
|
}
|
||||||
|
},
|
||||||
|
}).Filter(input)
|
||||||
|
if !assert.NoError(t, err) {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if !assert.True(t, called) {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
b := &bytes.Buffer{}
|
||||||
|
err = kio.ByteWriter{Writer: b, KeepReaderAnnotations: true}.Write(result)
|
||||||
|
if !assert.NoError(t, err) {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
assert.Equal(t, `apiVersion: apps/v1
|
||||||
|
kind: Deployment
|
||||||
|
metadata:
|
||||||
|
name: deployment-foo
|
||||||
|
annotations:
|
||||||
|
config.kubernetes.io/index: '0'
|
||||||
|
config.kubernetes.io/path: 'foo/deployment_deployment-foo.yaml'
|
||||||
|
---
|
||||||
|
apiVersion: v1
|
||||||
|
kind: Service
|
||||||
|
metadata:
|
||||||
|
name: service-foo
|
||||||
|
annotations:
|
||||||
|
config.kubernetes.io/index: '1'
|
||||||
|
config.kubernetes.io/path: 'foo/service_service-foo.yaml'
|
||||||
|
`, b.String())
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestFilter_Filter_defaultNamingFunctions(t *testing.T) {
|
||||||
|
cfg, err := yaml.Parse(`apiVersion: apps/v1
|
||||||
|
kind: Deployment
|
||||||
|
metadata:
|
||||||
|
name: foo
|
||||||
|
annotations:
|
||||||
|
config.kubernetes.io/path: 'foo/functions/bar.yaml'
|
||||||
|
`)
|
||||||
|
if !assert.NoError(t, err) {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
input, err := (&kio.ByteReader{Reader: bytes.NewBufferString(``)}).Read()
|
||||||
|
if !assert.NoError(t, err) {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
called := false
|
||||||
|
result, err := (&ContainerFilter{
|
||||||
|
Image: "example.com:version",
|
||||||
|
Config: cfg,
|
||||||
|
args: []string{"echo", `apiVersion: apps/v1
|
||||||
|
kind: Deployment
|
||||||
|
metadata:
|
||||||
|
name: deployment-foo
|
||||||
|
---
|
||||||
|
apiVersion: v1
|
||||||
|
kind: Service
|
||||||
|
metadata:
|
||||||
|
name: service-foo
|
||||||
|
`},
|
||||||
|
checkInput: func(s string) {
|
||||||
|
called = true
|
||||||
|
if !assert.Equal(t, `apiVersion: config.kubernetes.io/v1alpha1
|
||||||
|
kind: ResourceList
|
||||||
|
items: []
|
||||||
|
functionConfig: {apiVersion: apps/v1, kind: Deployment, metadata: {name: foo, annotations: {
|
||||||
|
config.kubernetes.io/path: 'foo/functions/bar.yaml'}}}
|
||||||
|
`, s) {
|
||||||
|
t.FailNow()
|
||||||
|
}
|
||||||
|
},
|
||||||
|
}).Filter(input)
|
||||||
|
if !assert.NoError(t, err) {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if !assert.True(t, called) {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
b := &bytes.Buffer{}
|
||||||
|
err = kio.ByteWriter{Writer: b, KeepReaderAnnotations: true}.Write(result)
|
||||||
|
if !assert.NoError(t, err) {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
assert.Equal(t, `apiVersion: apps/v1
|
||||||
|
kind: Deployment
|
||||||
|
metadata:
|
||||||
|
name: deployment-foo
|
||||||
|
annotations:
|
||||||
|
config.kubernetes.io/index: '0'
|
||||||
|
config.kubernetes.io/path: 'foo/deployment_deployment-foo.yaml'
|
||||||
|
---
|
||||||
|
apiVersion: v1
|
||||||
|
kind: Service
|
||||||
|
metadata:
|
||||||
|
name: service-foo
|
||||||
|
annotations:
|
||||||
|
config.kubernetes.io/index: '1'
|
||||||
|
config.kubernetes.io/path: 'foo/service_service-foo.yaml'
|
||||||
|
`, b.String())
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestFilter_Filter_scopeMissingFromResource(t *testing.T) {
|
||||||
|
cfg, err := yaml.Parse(`apiVersion: apps/v1
|
||||||
|
kind: Deployment
|
||||||
|
metadata:
|
||||||
|
name: foo
|
||||||
|
annotations:
|
||||||
|
config.kubernetes.io/path: 'foo/bar.yaml'
|
||||||
|
`)
|
||||||
|
if !assert.NoError(t, err) {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
input, err := (&kio.ByteReader{Reader: bytes.NewBufferString(`
|
||||||
|
apiVersion: apps/v1
|
||||||
|
kind: Deployment
|
||||||
|
metadata:
|
||||||
|
name: deployment-foo
|
||||||
|
---
|
||||||
|
apiVersion: v1
|
||||||
|
kind: Service
|
||||||
|
metadata:
|
||||||
|
name: service-foo
|
||||||
|
`)}).Read()
|
||||||
|
if !assert.NoError(t, err) {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// no resources match the scope
|
||||||
|
called := false
|
||||||
|
result, err := (&ContainerFilter{
|
||||||
|
Image: "example.com:version",
|
||||||
|
Config: cfg,
|
||||||
|
args: []string{"sed", "s/Deployment/StatefulSet/g"},
|
||||||
|
checkInput: func(s string) {
|
||||||
|
called = true
|
||||||
|
if !assert.Equal(t, `apiVersion: config.kubernetes.io/v1alpha1
|
||||||
|
kind: ResourceList
|
||||||
|
items: []
|
||||||
|
functionConfig: {apiVersion: apps/v1, kind: Deployment, metadata: {name: foo, annotations: {
|
||||||
|
config.kubernetes.io/path: 'foo/bar.yaml'}}}
|
||||||
|
`, s) {
|
||||||
|
t.FailNow()
|
||||||
|
}
|
||||||
|
},
|
||||||
|
}).Filter(input)
|
||||||
|
if !assert.NoError(t, err) {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if !assert.True(t, called) {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
b := &bytes.Buffer{}
|
||||||
|
err = kio.ByteWriter{Writer: b, KeepReaderAnnotations: true}.Write(result)
|
||||||
|
if !assert.NoError(t, err) {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// Resources should be preserved -- paths shouldn't be set by container
|
||||||
|
assert.Equal(t, `apiVersion: apps/v1
|
||||||
|
kind: Deployment
|
||||||
|
metadata:
|
||||||
|
name: deployment-foo
|
||||||
|
annotations:
|
||||||
|
config.kubernetes.io/index: '0'
|
||||||
|
---
|
||||||
|
apiVersion: v1
|
||||||
|
kind: Service
|
||||||
|
metadata:
|
||||||
|
name: service-foo
|
||||||
|
annotations:
|
||||||
|
config.kubernetes.io/index: '1'
|
||||||
|
`, b.String())
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestFilter_Filter_scopeFunctionsDir(t *testing.T) {
|
||||||
|
// functions under "functions/" dir should be scoped to parent dir
|
||||||
|
cfg, err := yaml.Parse(`apiVersion: apps/v1
|
||||||
|
kind: Deployment
|
||||||
|
metadata:
|
||||||
|
name: foo
|
||||||
|
annotations:
|
||||||
|
config.kubernetes.io/path: 'foo/functions/bar.yaml'
|
||||||
|
`)
|
||||||
|
if !assert.NoError(t, err) {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
input, err := (&kio.ByteReader{Reader: bytes.NewBufferString(`
|
||||||
|
apiVersion: apps/v1
|
||||||
|
kind: Deployment
|
||||||
|
metadata:
|
||||||
|
name: deployment-foo
|
||||||
|
annotations:
|
||||||
|
config.kubernetes.io/path: 'foo/bar/d.yaml'
|
||||||
|
---
|
||||||
|
apiVersion: v1
|
||||||
|
kind: Service
|
||||||
|
metadata:
|
||||||
|
name: service-foo
|
||||||
|
annotations:
|
||||||
|
config.kubernetes.io/path: 'foo/bar/s.yaml'
|
||||||
|
`)}).Read()
|
||||||
|
if !assert.NoError(t, err) {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// no resources match the scope
|
||||||
|
called := false
|
||||||
|
result, err := (&ContainerFilter{
|
||||||
|
Image: "example.com:version",
|
||||||
|
Config: cfg,
|
||||||
|
args: []string{"sed", "s/Deployment/StatefulSet/g"},
|
||||||
|
checkInput: func(s string) {
|
||||||
|
called = true
|
||||||
|
if !assert.Equal(t, `apiVersion: config.kubernetes.io/v1alpha1
|
||||||
|
kind: ResourceList
|
||||||
|
items:
|
||||||
|
- apiVersion: apps/v1
|
||||||
|
kind: Deployment
|
||||||
|
metadata:
|
||||||
|
name: deployment-foo
|
||||||
|
annotations:
|
||||||
|
config.kubernetes.io/path: 'foo/bar/d.yaml'
|
||||||
|
config.kubernetes.io/index: '0'
|
||||||
|
- apiVersion: v1
|
||||||
|
kind: Service
|
||||||
|
metadata:
|
||||||
|
name: service-foo
|
||||||
|
annotations:
|
||||||
|
config.kubernetes.io/path: 'foo/bar/s.yaml'
|
||||||
|
config.kubernetes.io/index: '1'
|
||||||
|
functionConfig: {apiVersion: apps/v1, kind: Deployment, metadata: {name: foo, annotations: {
|
||||||
|
config.kubernetes.io/path: 'foo/functions/bar.yaml'}}}
|
||||||
|
`, s) {
|
||||||
|
t.FailNow()
|
||||||
|
}
|
||||||
|
},
|
||||||
|
}).Filter(input)
|
||||||
|
if !assert.NoError(t, err) {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if !assert.True(t, called) {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
b := &bytes.Buffer{}
|
||||||
|
err = kio.ByteWriter{Writer: b, KeepReaderAnnotations: true}.Write(result)
|
||||||
|
if !assert.NoError(t, err) {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// Resources should be modified
|
||||||
|
assert.Equal(t, `apiVersion: apps/v1
|
||||||
|
kind: StatefulSet
|
||||||
|
metadata:
|
||||||
|
name: deployment-foo
|
||||||
|
annotations:
|
||||||
|
config.kubernetes.io/path: 'foo/bar/d.yaml'
|
||||||
|
config.kubernetes.io/index: '0'
|
||||||
|
---
|
||||||
|
apiVersion: v1
|
||||||
|
kind: Service
|
||||||
|
metadata:
|
||||||
|
name: service-foo
|
||||||
|
annotations:
|
||||||
|
config.kubernetes.io/path: 'foo/bar/s.yaml'
|
||||||
|
config.kubernetes.io/index: '1'
|
||||||
|
`, b.String())
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestFilter_Filter_scopeDir(t *testing.T) {
|
||||||
|
// functions under "functions/" dir should be scoped to parent dir
|
||||||
|
cfg, err := yaml.Parse(`apiVersion: apps/v1
|
||||||
|
kind: Deployment
|
||||||
|
metadata:
|
||||||
|
name: foo
|
||||||
|
annotations:
|
||||||
|
config.kubernetes.io/path: 'foo/bar.yaml'
|
||||||
|
`)
|
||||||
|
if !assert.NoError(t, err) {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
input, err := (&kio.ByteReader{Reader: bytes.NewBufferString(`
|
||||||
|
apiVersion: apps/v1
|
||||||
|
kind: Deployment
|
||||||
|
metadata:
|
||||||
|
name: deployment-foo
|
||||||
|
annotations:
|
||||||
|
config.kubernetes.io/path: 'foo/bar/d.yaml'
|
||||||
|
---
|
||||||
|
apiVersion: v1
|
||||||
|
kind: Service
|
||||||
|
metadata:
|
||||||
|
name: service-foo
|
||||||
|
annotations:
|
||||||
|
config.kubernetes.io/path: 'foo/bar/s.yaml'
|
||||||
|
`)}).Read()
|
||||||
|
if !assert.NoError(t, err) {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// no resources match the scope
|
||||||
|
called := false
|
||||||
|
result, err := (&ContainerFilter{
|
||||||
|
Image: "example.com:version",
|
||||||
|
Config: cfg,
|
||||||
|
args: []string{"sed", "s/Deployment/StatefulSet/g"},
|
||||||
|
checkInput: func(s string) {
|
||||||
|
called = true
|
||||||
|
if !assert.Equal(t, `apiVersion: config.kubernetes.io/v1alpha1
|
||||||
|
kind: ResourceList
|
||||||
|
items:
|
||||||
|
- apiVersion: apps/v1
|
||||||
|
kind: Deployment
|
||||||
|
metadata:
|
||||||
|
name: deployment-foo
|
||||||
|
annotations:
|
||||||
|
config.kubernetes.io/path: 'foo/bar/d.yaml'
|
||||||
|
config.kubernetes.io/index: '0'
|
||||||
|
- apiVersion: v1
|
||||||
|
kind: Service
|
||||||
|
metadata:
|
||||||
|
name: service-foo
|
||||||
|
annotations:
|
||||||
|
config.kubernetes.io/path: 'foo/bar/s.yaml'
|
||||||
|
config.kubernetes.io/index: '1'
|
||||||
|
functionConfig: {apiVersion: apps/v1, kind: Deployment, metadata: {name: foo, annotations: {
|
||||||
|
config.kubernetes.io/path: 'foo/bar.yaml'}}}
|
||||||
|
`, s) {
|
||||||
|
t.FailNow()
|
||||||
|
}
|
||||||
|
},
|
||||||
|
}).Filter(input)
|
||||||
|
if !assert.NoError(t, err) {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if !assert.True(t, called) {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
b := &bytes.Buffer{}
|
||||||
|
err = kio.ByteWriter{Writer: b, KeepReaderAnnotations: true}.Write(result)
|
||||||
|
if !assert.NoError(t, err) {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// Resources should be preserved
|
||||||
|
assert.Equal(t, `apiVersion: apps/v1
|
||||||
|
kind: StatefulSet
|
||||||
|
metadata:
|
||||||
|
name: deployment-foo
|
||||||
|
annotations:
|
||||||
|
config.kubernetes.io/path: 'foo/bar/d.yaml'
|
||||||
|
config.kubernetes.io/index: '0'
|
||||||
|
---
|
||||||
|
apiVersion: v1
|
||||||
|
kind: Service
|
||||||
|
metadata:
|
||||||
|
name: service-foo
|
||||||
|
annotations:
|
||||||
|
config.kubernetes.io/path: 'foo/bar/s.yaml'
|
||||||
|
config.kubernetes.io/index: '1'
|
||||||
|
`, b.String())
|
||||||
|
}
|
||||||
|
|||||||
@@ -27,6 +27,7 @@ import (
|
|||||||
"sort"
|
"sort"
|
||||||
|
|
||||||
"sigs.k8s.io/kustomize/kyaml/kio"
|
"sigs.k8s.io/kustomize/kyaml/kio"
|
||||||
|
"sigs.k8s.io/kustomize/kyaml/openapi"
|
||||||
"sigs.k8s.io/kustomize/kyaml/yaml"
|
"sigs.k8s.io/kustomize/kyaml/yaml"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -54,7 +55,9 @@ func FormatFileOrDirectory(path string) error {
|
|||||||
}.Execute()
|
}.Execute()
|
||||||
}
|
}
|
||||||
|
|
||||||
type FormatFilter struct{}
|
type FormatFilter struct {
|
||||||
|
Process func(n *yaml.Node) error
|
||||||
|
}
|
||||||
|
|
||||||
var _ kio.Filter = FormatFilter{}
|
var _ kio.Filter = FormatFilter{}
|
||||||
|
|
||||||
@@ -75,7 +78,9 @@ func (f FormatFilter) Filter(slice []*yaml.RNode) ([]*yaml.RNode, error) {
|
|||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
kind, apiVersion := kindNode.YNode().Value, apiVersionNode.YNode().Value
|
kind, apiVersion := kindNode.YNode().Value, apiVersionNode.YNode().Value
|
||||||
err = (&formatter{apiVersion: apiVersion, kind: kind}).fmtNode(slice[i].YNode(), "")
|
s := openapi.SchemaForResourceType(yaml.TypeMeta{APIVersion: apiVersion, Kind: kind})
|
||||||
|
err = (&formatter{apiVersion: apiVersion, kind: kind, process: f.Process}).
|
||||||
|
fmtNode(slice[i].YNode(), "", s)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
@@ -86,10 +91,18 @@ func (f FormatFilter) Filter(slice []*yaml.RNode) ([]*yaml.RNode, error) {
|
|||||||
type formatter struct {
|
type formatter struct {
|
||||||
apiVersion string
|
apiVersion string
|
||||||
kind string
|
kind string
|
||||||
|
process func(n *yaml.Node) error
|
||||||
}
|
}
|
||||||
|
|
||||||
// fmtNode recursively formats the Document Contents.
|
// fmtNode recursively formats the Document Contents.
|
||||||
func (f *formatter) fmtNode(n *yaml.Node, path string) error {
|
// See: https://godoc.org/gopkg.in/yaml.v3#Node
|
||||||
|
func (f *formatter) fmtNode(n *yaml.Node, path string, schema *openapi.ResourceSchema) error {
|
||||||
|
if n.Kind == yaml.ScalarNode && schema != nil && schema.Schema != nil {
|
||||||
|
// ensure values that are interpreted as non-string values (e.g. "true")
|
||||||
|
// are properly quoted
|
||||||
|
yaml.FormatNonStringStyle(n, *schema.Schema)
|
||||||
|
}
|
||||||
|
|
||||||
// sort the order of mapping fields
|
// sort the order of mapping fields
|
||||||
if n.Kind == yaml.MappingNode {
|
if n.Kind == yaml.MappingNode {
|
||||||
sort.Sort(sortedMapContents(*n))
|
sort.Sort(sortedMapContents(*n))
|
||||||
@@ -104,12 +117,43 @@ func (f *formatter) fmtNode(n *yaml.Node, path string) error {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// format the Content
|
||||||
for i := range n.Content {
|
for i := range n.Content {
|
||||||
p := path
|
// MappingNode are structured as having their fields as Content,
|
||||||
if n.Kind == yaml.MappingNode && i%2 == 1 {
|
// with the field-key and field-value alternating. e.g. Even elements
|
||||||
p = fmt.Sprintf("%s.%s", path, n.Content[i-1].Value)
|
// are the keys and odd elements are the values
|
||||||
|
isFieldKey := n.Kind == yaml.MappingNode && i%2 == 0
|
||||||
|
isFieldValue := n.Kind == yaml.MappingNode && i%2 == 1
|
||||||
|
isElement := n.Kind == yaml.SequenceNode
|
||||||
|
|
||||||
|
// run the process callback on the node if it has been set
|
||||||
|
// don't process keys: their format should be fixed
|
||||||
|
if f.process != nil && !isFieldKey {
|
||||||
|
if err := f.process(n.Content[i]); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
}
|
}
|
||||||
err := f.fmtNode(n.Content[i], p)
|
|
||||||
|
// get the schema for this Node
|
||||||
|
p := path
|
||||||
|
var s *openapi.ResourceSchema
|
||||||
|
switch {
|
||||||
|
case isFieldValue:
|
||||||
|
// if the node is a field, lookup the schema using the field name
|
||||||
|
p = fmt.Sprintf("%s.%s", path, n.Content[i-1].Value)
|
||||||
|
if schema != nil {
|
||||||
|
s = schema.Field(n.Content[i-1].Value)
|
||||||
|
}
|
||||||
|
case isElement:
|
||||||
|
// if the node is a list element, lookup the schema for the array items
|
||||||
|
if schema != nil {
|
||||||
|
s = schema.Elements()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// format the node using the schema
|
||||||
|
err := f.fmtNode(n.Content[i], p, s)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
@@ -143,6 +187,7 @@ func (s sortedMapContents) Swap(i, j int) {
|
|||||||
s.Content[iFieldValueIndex], s.Content[jFieldValueIndex] = s.
|
s.Content[iFieldValueIndex], s.Content[jFieldValueIndex] = s.
|
||||||
Content[jFieldValueIndex], s.Content[iFieldValueIndex]
|
Content[jFieldValueIndex], s.Content[iFieldValueIndex]
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s sortedMapContents) Less(i, j int) bool {
|
func (s sortedMapContents) Less(i, j int) bool {
|
||||||
iFieldNameIndex := i * 2
|
iFieldNameIndex := i * 2
|
||||||
jFieldNameIndex := j * 2
|
jFieldNameIndex := j * 2
|
||||||
|
|||||||
@@ -13,10 +13,195 @@ import (
|
|||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
"github.com/stretchr/testify/assert"
|
"github.com/stretchr/testify/assert"
|
||||||
|
"sigs.k8s.io/kustomize/kyaml/kio"
|
||||||
. "sigs.k8s.io/kustomize/kyaml/kio/filters"
|
. "sigs.k8s.io/kustomize/kyaml/kio/filters"
|
||||||
"sigs.k8s.io/kustomize/kyaml/kio/filters/testyaml"
|
"sigs.k8s.io/kustomize/kyaml/kio/filters/testyaml"
|
||||||
|
"sigs.k8s.io/kustomize/kyaml/yaml"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
func TestFormatInput_FixYaml1_1Compatibility(t *testing.T) {
|
||||||
|
y := `
|
||||||
|
apiVersion: apps/v1
|
||||||
|
kind: Deployment
|
||||||
|
metadata:
|
||||||
|
name: foo
|
||||||
|
labels:
|
||||||
|
foo: on
|
||||||
|
foo2: hello1
|
||||||
|
annotations:
|
||||||
|
bar: 1
|
||||||
|
bar2: hello2
|
||||||
|
spec:
|
||||||
|
template:
|
||||||
|
spec:
|
||||||
|
containers:
|
||||||
|
- name: nginx
|
||||||
|
image: nginx:1.0.0
|
||||||
|
args:
|
||||||
|
- on
|
||||||
|
- 1
|
||||||
|
- hello
|
||||||
|
ports:
|
||||||
|
- name: http
|
||||||
|
targetPort: 80
|
||||||
|
containerPort: 80
|
||||||
|
`
|
||||||
|
|
||||||
|
// keep the style on values that parse as non-string types
|
||||||
|
expected := `apiVersion: apps/v1
|
||||||
|
kind: Deployment
|
||||||
|
metadata:
|
||||||
|
name: foo
|
||||||
|
labels:
|
||||||
|
foo: "on"
|
||||||
|
foo2: hello1
|
||||||
|
annotations:
|
||||||
|
bar: "1"
|
||||||
|
bar2: hello2
|
||||||
|
spec:
|
||||||
|
template:
|
||||||
|
spec:
|
||||||
|
containers:
|
||||||
|
- name: nginx
|
||||||
|
image: nginx:1.0.0
|
||||||
|
args:
|
||||||
|
- "on"
|
||||||
|
- "1"
|
||||||
|
- hello
|
||||||
|
ports:
|
||||||
|
- name: http
|
||||||
|
targetPort: 80
|
||||||
|
containerPort: 80
|
||||||
|
`
|
||||||
|
|
||||||
|
buff := &bytes.Buffer{}
|
||||||
|
err := kio.Pipeline{
|
||||||
|
Inputs: []kio.Reader{&kio.ByteReader{Reader: strings.NewReader(y)}},
|
||||||
|
Filters: []kio.Filter{FormatFilter{}},
|
||||||
|
Outputs: []kio.Writer{kio.ByteWriter{Writer: buff}},
|
||||||
|
}.Execute()
|
||||||
|
assert.NoError(t, err)
|
||||||
|
assert.Equal(t, expected, buff.String())
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestFormatInput_PostprocessStyle(t *testing.T) {
|
||||||
|
y := `
|
||||||
|
apiVersion: v1
|
||||||
|
kind: Foo
|
||||||
|
metadata:
|
||||||
|
name: foo
|
||||||
|
spec:
|
||||||
|
notBoolean: "true"
|
||||||
|
notBoolean2: "on"
|
||||||
|
isBoolean: on
|
||||||
|
isBoolean2: true
|
||||||
|
notInt: "12345"
|
||||||
|
isInt: 12345
|
||||||
|
isString1: hello world
|
||||||
|
isString2: "hello world"
|
||||||
|
`
|
||||||
|
|
||||||
|
// keep the style on values that parse as non-string types
|
||||||
|
expected := `apiVersion: v1
|
||||||
|
kind: Foo
|
||||||
|
metadata:
|
||||||
|
name: foo
|
||||||
|
spec:
|
||||||
|
isBoolean: on
|
||||||
|
isBoolean2: true
|
||||||
|
isInt: 12345
|
||||||
|
isString1: hello world
|
||||||
|
isString2: hello world
|
||||||
|
notBoolean: "true"
|
||||||
|
notBoolean2: "on"
|
||||||
|
notInt: "12345"
|
||||||
|
`
|
||||||
|
|
||||||
|
buff := &bytes.Buffer{}
|
||||||
|
err := kio.Pipeline{
|
||||||
|
Inputs: []kio.Reader{&kio.ByteReader{Reader: strings.NewReader(y)}},
|
||||||
|
Filters: []kio.Filter{FormatFilter{Process: func(n *yaml.Node) error {
|
||||||
|
if yaml.IsYaml1_1NonString(n) {
|
||||||
|
// don't change these styles, they are important for backwards compatibility
|
||||||
|
// e.g. "on" must remain quoted, on must remain unquoted
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
// style does not have semantic meaning
|
||||||
|
n.Style = 0
|
||||||
|
return nil
|
||||||
|
}}},
|
||||||
|
Outputs: []kio.Writer{kio.ByteWriter{Writer: buff}},
|
||||||
|
}.Execute()
|
||||||
|
assert.NoError(t, err)
|
||||||
|
assert.Equal(t, expected, buff.String())
|
||||||
|
|
||||||
|
y = `
|
||||||
|
apiVersion: v1
|
||||||
|
kind: Foo
|
||||||
|
metadata:
|
||||||
|
name: 'foo'
|
||||||
|
spec:
|
||||||
|
notBoolean: "true"
|
||||||
|
notBoolean2: "on"
|
||||||
|
notBoolean3: y is yes
|
||||||
|
isBoolean: on
|
||||||
|
isBoolean2: true
|
||||||
|
isBoolean3: y
|
||||||
|
notInt2: 1234 five
|
||||||
|
notInt3: one 2345
|
||||||
|
notInt: "12345"
|
||||||
|
isInt1: 12345
|
||||||
|
isInt2: -12345
|
||||||
|
isFloat1: 1.1234
|
||||||
|
isFloat2: 1.1234
|
||||||
|
isString1: hello world
|
||||||
|
isString2: "hello world"
|
||||||
|
isString3: 'hello world'
|
||||||
|
`
|
||||||
|
|
||||||
|
// keep the style on values that parse as non-string types
|
||||||
|
expected = `apiVersion: 'v1'
|
||||||
|
kind: 'Foo'
|
||||||
|
metadata:
|
||||||
|
name: 'foo'
|
||||||
|
spec:
|
||||||
|
isBoolean: on
|
||||||
|
isBoolean2: true
|
||||||
|
isBoolean3: y
|
||||||
|
isFloat1: 1.1234
|
||||||
|
isFloat2: 1.1234
|
||||||
|
isInt1: 12345
|
||||||
|
isInt2: -12345
|
||||||
|
isString1: 'hello world'
|
||||||
|
isString2: 'hello world'
|
||||||
|
isString3: 'hello world'
|
||||||
|
notBoolean: "true"
|
||||||
|
notBoolean2: "on"
|
||||||
|
notBoolean3: 'y is yes'
|
||||||
|
notInt: "12345"
|
||||||
|
notInt2: '1234 five'
|
||||||
|
notInt3: 'one 2345'
|
||||||
|
`
|
||||||
|
|
||||||
|
buff = &bytes.Buffer{}
|
||||||
|
err = kio.Pipeline{
|
||||||
|
Inputs: []kio.Reader{&kio.ByteReader{Reader: strings.NewReader(y)}},
|
||||||
|
Filters: []kio.Filter{FormatFilter{Process: func(n *yaml.Node) error {
|
||||||
|
if yaml.IsYaml1_1NonString(n) {
|
||||||
|
// don't change these styles, they are important for backwards compatibility
|
||||||
|
// e.g. "on" must remain quoted, on must remain unquoted
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
// style does not have semantic meaning
|
||||||
|
n.Style = yaml.SingleQuotedStyle
|
||||||
|
return nil
|
||||||
|
}}},
|
||||||
|
Outputs: []kio.Writer{kio.ByteWriter{Writer: buff}},
|
||||||
|
}.Execute()
|
||||||
|
assert.NoError(t, err)
|
||||||
|
assert.Equal(t, expected, buff.String())
|
||||||
|
}
|
||||||
|
|
||||||
func TestFormatInput_Style(t *testing.T) {
|
func TestFormatInput_Style(t *testing.T) {
|
||||||
y := `
|
y := `
|
||||||
apiVersion: v1
|
apiVersion: v1
|
||||||
|
|||||||
@@ -5,8 +5,10 @@ package kioutil
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"path"
|
||||||
"sort"
|
"sort"
|
||||||
"strconv"
|
"strconv"
|
||||||
|
"strings"
|
||||||
|
|
||||||
"sigs.k8s.io/kustomize/kyaml/errors"
|
"sigs.k8s.io/kustomize/kyaml/errors"
|
||||||
"sigs.k8s.io/kustomize/kyaml/yaml"
|
"sigs.k8s.io/kustomize/kyaml/yaml"
|
||||||
@@ -20,9 +22,6 @@ const (
|
|||||||
|
|
||||||
// PathAnnotation records the path to the file the Resource was read from
|
// PathAnnotation records the path to the file the Resource was read from
|
||||||
PathAnnotation AnnotationKey = "config.kubernetes.io/path"
|
PathAnnotation AnnotationKey = "config.kubernetes.io/path"
|
||||||
|
|
||||||
// PackageAnnotation records the name of the package the Resource was read from
|
|
||||||
PackageAnnotation AnnotationKey = "config.kubernetes.io/package"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
func GetFileAnnotations(rn *yaml.RNode) (string, string, error) {
|
func GetFileAnnotations(rn *yaml.RNode) (string, string, error) {
|
||||||
@@ -44,13 +43,101 @@ func ErrorIfMissingAnnotation(nodes []*yaml.RNode, keys ...AnnotationKey) error
|
|||||||
return errors.Wrap(err)
|
return errors.Wrap(err)
|
||||||
}
|
}
|
||||||
if val == nil {
|
if val == nil {
|
||||||
return errors.Errorf("missing package annotation %s", key)
|
return errors.Errorf("missing annotation %s", key)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// CreatePathAnnotationValue creates a default path annotation value for a Resource.
|
||||||
|
// The path prefix will be dir.
|
||||||
|
func CreatePathAnnotationValue(dir string, m yaml.ResourceMeta) string {
|
||||||
|
filename := fmt.Sprintf("%s_%s.yaml", strings.ToLower(m.Kind), m.Name)
|
||||||
|
return path.Join(dir, m.Namespace, filename)
|
||||||
|
}
|
||||||
|
|
||||||
|
// DefaultPathAndIndexAnnotation sets a default path or index value on any nodes missing the
|
||||||
|
// annotation
|
||||||
|
func DefaultPathAndIndexAnnotation(dir string, nodes []*yaml.RNode) error {
|
||||||
|
counts := map[string]int{}
|
||||||
|
|
||||||
|
// check each node for the path annotation
|
||||||
|
for i := range nodes {
|
||||||
|
m, err := nodes[i].GetMeta()
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
// calculate the max index in each file in case we are appending
|
||||||
|
if p, found := m.Annotations[PathAnnotation]; found {
|
||||||
|
// record the max indexes into each file
|
||||||
|
if i, found := m.Annotations[IndexAnnotation]; found {
|
||||||
|
index, _ := strconv.Atoi(i)
|
||||||
|
if index > counts[p] {
|
||||||
|
counts[p] = index
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// has the path annotation already -- do nothing
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
// set a path annotation on the Resource
|
||||||
|
path := CreatePathAnnotationValue(dir, m)
|
||||||
|
if err := nodes[i].PipeE(yaml.SetAnnotation(PathAnnotation, path)); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// set the index annotations
|
||||||
|
for i := range nodes {
|
||||||
|
m, err := nodes[i].GetMeta()
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
if _, found := m.Annotations[IndexAnnotation]; found {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
p := m.Annotations[PathAnnotation]
|
||||||
|
|
||||||
|
// set an index annotation on the Resource
|
||||||
|
c := counts[p]
|
||||||
|
counts[p] = c + 1
|
||||||
|
if err := nodes[i].PipeE(
|
||||||
|
yaml.SetAnnotation(IndexAnnotation, fmt.Sprintf("%d", c))); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// DefaultPathAnnotation sets a default path annotation on any Reources
|
||||||
|
// missing it.
|
||||||
|
func DefaultPathAnnotation(dir string, nodes []*yaml.RNode) error {
|
||||||
|
// check each node for the path annotation
|
||||||
|
for i := range nodes {
|
||||||
|
m, err := nodes[i].GetMeta()
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
if _, found := m.Annotations[PathAnnotation]; found {
|
||||||
|
// has the path annotation already -- do nothing
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
// set a path annotation on the Resource
|
||||||
|
path := CreatePathAnnotationValue(dir, m)
|
||||||
|
if err := nodes[i].PipeE(yaml.SetAnnotation(PathAnnotation, path)); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
// Map invokes fn for each element in nodes.
|
// Map invokes fn for each element in nodes.
|
||||||
func Map(nodes []*yaml.RNode, fn func(*yaml.RNode) (*yaml.RNode, error)) ([]*yaml.RNode, error) {
|
func Map(nodes []*yaml.RNode, fn func(*yaml.RNode) (*yaml.RNode, error)) ([]*yaml.RNode, error) {
|
||||||
var returnNodes []*yaml.RNode
|
var returnNodes []*yaml.RNode
|
||||||
@@ -136,7 +223,7 @@ func SortNodes(nodes []*yaml.RNode) error {
|
|||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
if iIndex != jIndex {
|
if iIndex != jIndex {
|
||||||
return iValue < jValue
|
return iIndex < jIndex
|
||||||
}
|
}
|
||||||
|
|
||||||
// elements are equal
|
// elements are equal
|
||||||
|
|||||||
332
kyaml/kio/kioutil/kioutil_test.go
Normal file
332
kyaml/kio/kioutil/kioutil_test.go
Normal file
@@ -0,0 +1,332 @@
|
|||||||
|
// Copyright 2019 The Kubernetes Authors.
|
||||||
|
// SPDX-License-Identifier: Apache-2.0
|
||||||
|
|
||||||
|
package kioutil_test
|
||||||
|
|
||||||
|
import (
|
||||||
|
"bytes"
|
||||||
|
"math/rand"
|
||||||
|
"strings"
|
||||||
|
"testing"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"github.com/stretchr/testify/assert"
|
||||||
|
"sigs.k8s.io/kustomize/kyaml/kio"
|
||||||
|
"sigs.k8s.io/kustomize/kyaml/kio/kioutil"
|
||||||
|
"sigs.k8s.io/kustomize/kyaml/yaml"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestSortNodes_moreThan10(t *testing.T) {
|
||||||
|
input := `
|
||||||
|
a: b
|
||||||
|
---
|
||||||
|
c: d
|
||||||
|
---
|
||||||
|
e: f
|
||||||
|
---
|
||||||
|
g: h
|
||||||
|
---
|
||||||
|
i: j
|
||||||
|
---
|
||||||
|
k: l
|
||||||
|
---
|
||||||
|
m: n
|
||||||
|
---
|
||||||
|
o: p
|
||||||
|
---
|
||||||
|
q: r
|
||||||
|
---
|
||||||
|
s: t
|
||||||
|
---
|
||||||
|
u: v
|
||||||
|
---
|
||||||
|
w: x
|
||||||
|
---
|
||||||
|
y: z
|
||||||
|
`
|
||||||
|
actual := &bytes.Buffer{}
|
||||||
|
rw := kio.ByteReadWriter{Reader: bytes.NewBufferString(input), Writer: actual}
|
||||||
|
nodes, err := rw.Read()
|
||||||
|
if !assert.NoError(t, err) {
|
||||||
|
t.Fail()
|
||||||
|
}
|
||||||
|
|
||||||
|
// randomize the list
|
||||||
|
rand.Seed(time.Now().UnixNano())
|
||||||
|
rand.Shuffle(len(nodes), func(i, j int) { nodes[i], nodes[j] = nodes[j], nodes[i] })
|
||||||
|
|
||||||
|
// sort them back into their original order
|
||||||
|
if !assert.NoError(t, kioutil.SortNodes(nodes)) {
|
||||||
|
t.Fail()
|
||||||
|
}
|
||||||
|
|
||||||
|
// check the sorted values
|
||||||
|
expected := strings.Split(input, "---")
|
||||||
|
for i := range nodes {
|
||||||
|
a := strings.TrimSpace(nodes[i].MustString())
|
||||||
|
b := strings.TrimSpace(expected[i])
|
||||||
|
if !assert.Contains(t, a, b) {
|
||||||
|
t.Fail()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if !assert.NoError(t, rw.Write(nodes)) {
|
||||||
|
t.Fail()
|
||||||
|
}
|
||||||
|
|
||||||
|
assert.Equal(t, strings.TrimSpace(input), strings.TrimSpace(actual.String()))
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestDefaultPathAnnotation(t *testing.T) {
|
||||||
|
var tests = []struct {
|
||||||
|
dir string
|
||||||
|
input string // input
|
||||||
|
expected string // expected result
|
||||||
|
name string
|
||||||
|
}{
|
||||||
|
{
|
||||||
|
`foo`,
|
||||||
|
`apiVersion: v1
|
||||||
|
kind: Bar
|
||||||
|
metadata:
|
||||||
|
name: a
|
||||||
|
namespace: b
|
||||||
|
`,
|
||||||
|
`apiVersion: v1
|
||||||
|
kind: Bar
|
||||||
|
metadata:
|
||||||
|
name: a
|
||||||
|
namespace: b
|
||||||
|
annotations:
|
||||||
|
config.kubernetes.io/path: 'foo/b/bar_a.yaml'
|
||||||
|
`, `with namespace`},
|
||||||
|
{
|
||||||
|
`foo`,
|
||||||
|
`apiVersion: v1
|
||||||
|
kind: Bar
|
||||||
|
metadata:
|
||||||
|
name: a
|
||||||
|
`,
|
||||||
|
`apiVersion: v1
|
||||||
|
kind: Bar
|
||||||
|
metadata:
|
||||||
|
name: a
|
||||||
|
annotations:
|
||||||
|
config.kubernetes.io/path: 'foo/bar_a.yaml'
|
||||||
|
`, `without namespace`},
|
||||||
|
|
||||||
|
{
|
||||||
|
``,
|
||||||
|
`apiVersion: v1
|
||||||
|
kind: Bar
|
||||||
|
metadata:
|
||||||
|
name: a
|
||||||
|
namespace: b
|
||||||
|
`,
|
||||||
|
`apiVersion: v1
|
||||||
|
kind: Bar
|
||||||
|
metadata:
|
||||||
|
name: a
|
||||||
|
namespace: b
|
||||||
|
annotations:
|
||||||
|
config.kubernetes.io/path: 'b/bar_a.yaml'
|
||||||
|
`, `without dir`},
|
||||||
|
{
|
||||||
|
``,
|
||||||
|
`apiVersion: v1
|
||||||
|
kind: Bar
|
||||||
|
metadata:
|
||||||
|
name: a
|
||||||
|
namespace: b
|
||||||
|
annotations:
|
||||||
|
config.kubernetes.io/path: 'a/b.yaml'
|
||||||
|
`,
|
||||||
|
`apiVersion: v1
|
||||||
|
kind: Bar
|
||||||
|
metadata:
|
||||||
|
name: a
|
||||||
|
namespace: b
|
||||||
|
annotations:
|
||||||
|
config.kubernetes.io/path: 'a/b.yaml'
|
||||||
|
`, `skip`},
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, s := range tests {
|
||||||
|
n := yaml.MustParse(s.input)
|
||||||
|
err := kioutil.DefaultPathAnnotation(s.dir, []*yaml.RNode{n})
|
||||||
|
if !assert.NoError(t, err, s.name) {
|
||||||
|
t.FailNow()
|
||||||
|
}
|
||||||
|
if !assert.Equal(t, s.expected, n.MustString(), s.name) {
|
||||||
|
t.FailNow()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestDefaultPathAndIndexAnnotation(t *testing.T) {
|
||||||
|
var tests = []struct {
|
||||||
|
dir string
|
||||||
|
input string // input
|
||||||
|
expected string // expected result
|
||||||
|
name string
|
||||||
|
}{
|
||||||
|
{
|
||||||
|
`foo`,
|
||||||
|
`apiVersion: v1
|
||||||
|
kind: Bar
|
||||||
|
metadata:
|
||||||
|
name: a
|
||||||
|
namespace: b
|
||||||
|
`,
|
||||||
|
`apiVersion: v1
|
||||||
|
kind: Bar
|
||||||
|
metadata:
|
||||||
|
name: a
|
||||||
|
namespace: b
|
||||||
|
annotations:
|
||||||
|
config.kubernetes.io/path: 'foo/b/bar_a.yaml'
|
||||||
|
config.kubernetes.io/index: '0'
|
||||||
|
`, `with namespace`},
|
||||||
|
{
|
||||||
|
`foo`,
|
||||||
|
`apiVersion: v1
|
||||||
|
kind: Bar
|
||||||
|
metadata:
|
||||||
|
name: a
|
||||||
|
`,
|
||||||
|
`apiVersion: v1
|
||||||
|
kind: Bar
|
||||||
|
metadata:
|
||||||
|
name: a
|
||||||
|
annotations:
|
||||||
|
config.kubernetes.io/path: 'foo/bar_a.yaml'
|
||||||
|
config.kubernetes.io/index: '0'
|
||||||
|
`, `without namespace`},
|
||||||
|
|
||||||
|
{
|
||||||
|
``,
|
||||||
|
`apiVersion: v1
|
||||||
|
kind: Bar
|
||||||
|
metadata:
|
||||||
|
name: a
|
||||||
|
namespace: b
|
||||||
|
`,
|
||||||
|
`apiVersion: v1
|
||||||
|
kind: Bar
|
||||||
|
metadata:
|
||||||
|
name: a
|
||||||
|
namespace: b
|
||||||
|
annotations:
|
||||||
|
config.kubernetes.io/path: 'b/bar_a.yaml'
|
||||||
|
config.kubernetes.io/index: '0'
|
||||||
|
`, `without dir`},
|
||||||
|
{
|
||||||
|
``,
|
||||||
|
`apiVersion: v1
|
||||||
|
kind: Bar
|
||||||
|
metadata:
|
||||||
|
name: a
|
||||||
|
namespace: b
|
||||||
|
annotations:
|
||||||
|
config.kubernetes.io/path: 'a/b.yaml'
|
||||||
|
config.kubernetes.io/index: '5'
|
||||||
|
`,
|
||||||
|
`apiVersion: v1
|
||||||
|
kind: Bar
|
||||||
|
metadata:
|
||||||
|
name: a
|
||||||
|
namespace: b
|
||||||
|
annotations:
|
||||||
|
config.kubernetes.io/path: 'a/b.yaml'
|
||||||
|
config.kubernetes.io/index: '5'
|
||||||
|
`, `skip`},
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, s := range tests {
|
||||||
|
out := &bytes.Buffer{}
|
||||||
|
r := kio.ByteReadWriter{
|
||||||
|
Reader: bytes.NewBufferString(s.input),
|
||||||
|
Writer: out,
|
||||||
|
KeepReaderAnnotations: true,
|
||||||
|
OmitReaderAnnotations: true,
|
||||||
|
}
|
||||||
|
n, err := r.Read()
|
||||||
|
if !assert.NoError(t, err, s.name) {
|
||||||
|
t.FailNow()
|
||||||
|
}
|
||||||
|
if !assert.NoError(t, kioutil.DefaultPathAndIndexAnnotation(s.dir, n), s.name) {
|
||||||
|
t.FailNow()
|
||||||
|
}
|
||||||
|
if !assert.NoError(t, r.Write(n), s.name) {
|
||||||
|
t.FailNow()
|
||||||
|
}
|
||||||
|
if !assert.Equal(t, s.expected, out.String(), s.name) {
|
||||||
|
t.FailNow()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestCreatePathAnnotationValue(t *testing.T) {
|
||||||
|
var tests = []struct {
|
||||||
|
dir string
|
||||||
|
meta yaml.ResourceMeta // input
|
||||||
|
expected string // expected result
|
||||||
|
name string
|
||||||
|
}{
|
||||||
|
{
|
||||||
|
`dir`,
|
||||||
|
yaml.ResourceMeta{Kind: "foo",
|
||||||
|
APIVersion: "apps/v1",
|
||||||
|
ObjectMeta: yaml.ObjectMeta{Name: "bar", Namespace: "baz"},
|
||||||
|
},
|
||||||
|
`dir/baz/foo_bar.yaml`, `with namespace`,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
``,
|
||||||
|
yaml.ResourceMeta{Kind: "foo",
|
||||||
|
APIVersion: "apps/v1",
|
||||||
|
ObjectMeta: yaml.ObjectMeta{Name: "bar", Namespace: "baz"},
|
||||||
|
},
|
||||||
|
`baz/foo_bar.yaml`, `without dir`,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
`dir`,
|
||||||
|
yaml.ResourceMeta{Kind: "foo",
|
||||||
|
APIVersion: "apps/v1",
|
||||||
|
ObjectMeta: yaml.ObjectMeta{Name: "bar"},
|
||||||
|
},
|
||||||
|
`dir/foo_bar.yaml`, `without namespace`,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
``,
|
||||||
|
yaml.ResourceMeta{Kind: "foo",
|
||||||
|
APIVersion: "apps/v1",
|
||||||
|
ObjectMeta: yaml.ObjectMeta{Name: "bar"},
|
||||||
|
},
|
||||||
|
`foo_bar.yaml`, `without namespace or dir`,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
``,
|
||||||
|
yaml.ResourceMeta{Kind: "foo",
|
||||||
|
APIVersion: "apps/v1",
|
||||||
|
ObjectMeta: yaml.ObjectMeta{},
|
||||||
|
},
|
||||||
|
`foo_.yaml`, `without namespace, dir or name`,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
``,
|
||||||
|
yaml.ResourceMeta{
|
||||||
|
APIVersion: "apps/v1",
|
||||||
|
ObjectMeta: yaml.ObjectMeta{},
|
||||||
|
},
|
||||||
|
`_.yaml`, `without any`,
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, s := range tests {
|
||||||
|
p := kioutil.CreatePathAnnotationValue(s.dir, s.meta)
|
||||||
|
if !assert.Equal(t, s.expected, p, s.name) {
|
||||||
|
t.FailNow()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -263,7 +263,6 @@ func (r *LocalPackageReader) initReaderAnnotations(path string, _ os.FileInfo) {
|
|||||||
r.SetAnnotations = map[string]string{}
|
r.SetAnnotations = map[string]string{}
|
||||||
}
|
}
|
||||||
if !r.OmitReaderAnnotations {
|
if !r.OmitReaderAnnotations {
|
||||||
r.SetAnnotations[kioutil.PackageAnnotation] = filepath.Dir(path)
|
|
||||||
r.SetAnnotations[kioutil.PathAnnotation] = path
|
r.SetAnnotations[kioutil.PathAnnotation] = path
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user