Compare commits

...

95 Commits

Author SHA1 Message Date
k8s-ci-robot
8f701a0041 Merge pull request #565 from zoncoen/name-suffix-3
Update docs, examples, comments, and test data for nameSuffix
2018-11-29 08:13:42 -08:00
k8s-ci-robot
593f9231ae Merge pull request #583 from monopole/fix560
Ignore empty fields targeted by transformations.
2018-11-29 08:07:01 -08:00
zoncoen
59df8a0dda update docs, examples, comments 2018-11-29 22:29:52 +09:00
Jeffrey Regan
6b93973bad Fix #560 (kinda/sorta) 2018-11-28 16:46:10 -08:00
k8s-ci-robot
df3ec571fb Merge pull request #582 from monopole/preFix560
Add test for mutatefield
2018-11-28 16:38:31 -08:00
k8s-ci-robot
f03fad7a96 Merge pull request #563 from Liujingfang1/crdexample
update crd example by using configurations file list
2018-11-28 14:59:22 -08:00
Jeff Regan
f714e9faf3 another tweak 2018-11-28 14:55:48 -08:00
Jeff Regan
3e1a3d83da Minor tweaks 2018-11-28 14:53:33 -08:00
Jeffrey Regan
8ba2ea9ca7 Add test for mutatefield 2018-11-28 11:01:50 -08:00
zoncoen
7dc8ef1028 update build command testdata 2018-11-26 14:00:26 +09:00
Jeff Regan
ef51cceff5 Merge pull request #579 from monopole/deleteHashicorpCloner
Delete hashicorp cloner.
2018-11-22 17:11:12 -08:00
jregan
a40c2502de Delete hashicorp cloner. 2018-11-22 16:58:59 -08:00
Jeff Regan
0201f9cba8 Merge pull request #578 from monopole/turnOnSimpleCloner
Turn off hashicorp cloner, turn on simple cloner.
2018-11-22 16:56:56 -08:00
jregan
7c1277f24c Turn off hashicorp cloner. 2018-11-22 16:48:24 -08:00
Jeff Regan
29f03dfb55 Merge pull request #577 from monopole/fixCloner
Deal with branch spec in simpleGitCloner.
2018-11-22 16:47:51 -08:00
jregan
02d2d38c21 Deal with branch spec in simpleGitCloner. 2018-11-22 16:42:28 -08:00
k8s-ci-robot
6757efe290 Merge pull request #547 from Liujingfang1/base_var
POC: allow accessing labels and annotations in vars
2018-11-22 15:32:36 -08:00
Jeff Regan
5990af8ced Merge pull request #576 from monopole/removeStrayComment
Remove stray comment.
2018-11-22 10:39:39 -08:00
jregan
6cddc25f0e Remove stray comment. 2018-11-22 10:39:18 -08:00
Jeff Regan
8bd773b536 Merge pull request #574 from monopole/simpleGitCloner
Introduce simple git cloner.
2018-11-22 10:33:43 -08:00
jregan
d9ba209543 Introduce simple git cloner. 2018-11-22 10:24:35 -08:00
Jeff Regan
c51646e3db Merge pull request #573 from monopole/enforceRelocatability
Enforce relocatable kustomizations.
2018-11-22 09:15:08 -08:00
jregan
4f9d00c021 Enforce relocatabile kustomizations. 2018-11-22 09:07:05 -08:00
Jeff Regan
0042c4be54 Merge pull request #571 from monopole/fileRename
Rename gitloader to gitcloner.
2018-11-22 08:46:41 -08:00
jregan
910eb322e0 Rename gitloader to gitcloner. 2018-11-22 08:41:59 -08:00
Jeff Regan
064b768176 Merge pull request #570 from monopole/hotYoga
Add test coverage to gitloader.
2018-11-22 08:37:20 -08:00
jregan
4daa655516 Add test coverage to gitloader. 2018-11-22 08:27:25 -08:00
Jeff Regan
d6910e9788 Merge pull request #569 from monopole/fsRemoveAll
Add removeAll to fakeFs
2018-11-22 06:59:21 -08:00
jregan
eed16afb00 Add removeAll to fakeFs 2018-11-22 06:53:44 -08:00
Jingfang Liu
6ec77b27da update crd example by using configurations file list 2018-11-20 10:31:44 -08:00
k8s-ci-robot
621ed52bab Merge pull request #556 from zoncoen/name-suffix-2
Add nameSuffix field to kustomization.yaml
2018-11-20 08:50:22 -08:00
zoncoen
b8c2ed20d1 fix the command usage 2018-11-20 17:09:40 +09:00
k8s-ci-robot
19ad9c2d46 Merge pull request #544 from Liujingfang1/config
Remove -t flag in build and add configurations field in kustomization file
2018-11-19 16:04:44 -08:00
k8s-ci-robot
41cc210fa0 Merge pull request #546 from Liujingfang1/var
update docs for vars
2018-11-19 10:29:59 -08:00
zoncoen
3488b542ac add edit command option for editing name suffix 2018-11-19 12:09:54 +09:00
zoncoen
04a030bcf0 enable nameSuffix field of kustomization.yaml 2018-11-19 12:09:47 +09:00
Jingfang Liu
25415c5501 Remove -t flag in build and add configurations field in kustomization.yaml 2018-11-16 13:44:18 -08:00
Jingfang Liu
a094be45d9 update vendor_kustomize.sh with run-in-gopath.sh (#545) 2018-11-16 13:27:20 -08:00
k8s-ci-robot
fdb8a7d74a Merge pull request #550 from zoncoen/name-suffix-1
Enable namePrefixTransformer to append name suffix
2018-11-16 09:07:44 -08:00
zoncoen
d481dbad62 combine transformers 2018-11-15 18:49:36 +09:00
zoncoen
c1e7f1b957 fix the order of YAMLs
If suffix is empty string, ResId.String() retuns name with "noSuffix".
2018-11-15 18:29:11 +09:00
zoncoen
93094c78eb add transformer for appending suffix 2018-11-14 12:46:58 +09:00
zoncoen
a14609f730 add suffix field to ResId 2018-11-14 12:44:33 +09:00
zoncoen
a8984578e4 refactor test code for readability 2018-11-14 12:36:28 +09:00
Jingfang Liu
51e9fec65d allow accessing labels and annotations in vars 2018-11-13 15:56:06 -08:00
k8s-ci-robot
38b7f42f9e Merge pull request #543 from giannello/patch-1
Add StorageClass to the list of ordered objects
2018-11-13 11:38:12 -08:00
Jingfang Liu
e574948577 update docs for vars 2018-11-13 11:32:10 -08:00
Giuseppe Iannello
ebf1efe07e Add StorageClass to the list of ordered objects
StorageClasses can be configured as `default`, so that PVCs can use them without an explicit reference.
This change adds StorageClasses close to the beginning of the compiled output.
2018-11-13 14:51:48 +01:00
Jingfang Liu
83bc67c8ad remove glog dependency from kustomize code (#542) 2018-11-12 11:35:00 -08:00
k8s-ci-robot
1648eceb47 Merge pull request #541 from mooncak/fix_typos
Fix typos: expectd->expected, cluser->cluster
2018-11-12 08:23:30 -08:00
mooncake
538aaaf217 Fix typos: expectd->expected, cluser->cluster
Signed-off-by: mooncake <xcoder@tenxcloud.com>
2018-11-11 00:14:25 +08:00
k8s-ci-robot
5b35443533 Merge pull request #537 from Liujingfang1/vendor
update vendor_kustomize.sh
2018-11-07 19:21:03 -08:00
k8s-ci-robot
e089a56e05 Merge pull request #536 from Liujingfang1/mergenil
Update TransformerConfig.Merge function to handle nil
2018-11-07 19:19:33 -08:00
k8s-ci-robot
5c4a778e6a Merge pull request #535 from monopole/deleteSomeDeps
Simplify some code and add a de-looping TODO.
2018-11-07 14:28:11 -08:00
Jingfang Liu
e0ec8028eb Update TransformerConfig.Merge function to handle nil 2018-11-07 14:05:40 -08:00
Jeff Regan
578ff2e45c Merge pull request #533 from Liujingfang1/config
fix incorrect path in default namereference configs
2018-11-07 14:02:10 -08:00
Jeffrey Regan
d04877a9e7 Simplify some code and add TODOs. 2018-11-07 13:58:14 -08:00
Jingfang Liu
727b5ebd7f update vendor_kustomize.sh 2018-11-07 13:45:38 -08:00
Jeff Regan
af1e1e6942 Merge pull request #534 from monopole/typoRepair
Add/fix some documentation and vars names.
2018-11-07 12:42:59 -08:00
Jeffrey Regan
d05bb6b199 Add/fix some documentation and vars names. 2018-11-07 12:36:25 -08:00
Jeff Regan
ba953484bf Merge pull request #522 from Liujingfang1/name
make sure the objects loaded have name and kind
2018-11-07 12:18:50 -08:00
Jeff Regan
fdf78b1d7d Merge pull request #501 from Liujingfang1/generatoroptions
Add example for generatorOptions
2018-11-07 10:55:30 -08:00
Jeff Regan
95fed47c1c Update generatorOptions.md 2018-11-07 10:53:46 -08:00
Jingfang Liu
4cf916e6f4 fix incorrect path in default namereference configs 2018-11-07 10:04:49 -08:00
k8s-ci-robot
23bf326d93 Merge pull request #530 from twz123/patch-1
Update default var reference link
2018-11-05 11:12:04 -08:00
k8s-ci-robot
bcd4d185a7 Merge pull request #529 from pst/patch-1
Fix typo in namereference path for cronjobs
2018-11-05 11:10:38 -08:00
Tom Wieczorek
57a5fa593c Update default var reference link
It has moved recently.
2018-11-05 18:15:04 +01:00
Philipp Strube
421ca3fb3c Fix typo in namereference path for cronjobs
Fix namereference for CronJob `path: spec/jobTemplate/spec/template/spec/containers/env/valueFrom/configMapKeyRef/name`
2018-11-05 14:07:32 +01:00
k8s-ci-robot
29945c2c7a Merge pull request #528 from laverya/fallback-to-string-sort-in-gvk-comparison-if-indices-equal
add fallback for GVK comparison
2018-11-02 13:07:56 -07:00
Andrew Lavery
9d82d54c5b add fallback for GVK comparison
only return comparison of 'Kind' indices if they do not match
otherwise fall back to GVK string comparison
this reduces output instability
2018-11-01 12:22:21 -07:00
Jingfang Liu
4827d9984f Add example for generatorOptions 2018-10-30 13:53:26 -07:00
Jeff Regan
d718fe3ee1 Merge pull request #525 from monopole/renameDisableHash
Rename disableHash to disableNameSuffixHash
2018-10-30 11:42:57 -07:00
Jeffrey Regan
a8fbe35ecf Rename disableHash to disableNameSuffixHash 2018-10-30 11:36:00 -07:00
Jingfang Liu
5947f696ff make sure the objects loaded have name and kind 2018-10-29 14:59:56 -07:00
k8s-ci-robot
40e0bbeec2 Merge pull request #502 from Liujingfang1/yml
add support .yml extension for kusotmization file
2018-10-29 12:33:16 -07:00
Jingfang Liu
ecbf3c5f51 add support .yml extension for kusotmization file 2018-10-29 12:21:00 -07:00
k8s-ci-robot
dfa952f0d5 Merge pull request #520 from monopole/loadingWithHistory
Consult history in fileloader.
2018-10-29 11:27:11 -07:00
jregan
793577d044 Consult history in fileloader.
Fixes #366

To reproduce #366, add

```
bases:
- .
```

to `examples/helloWorld/kustomization.yaml`, attempt to build it, and enjoy the stack overflow.

This PR fixes this by adding history to file loaders,
allowing one to avoid cycles in overlay->base
relationships.  To make entry points clearer, this PR
exposes only two public ways to make a fresh
(no-history) loader

 * rooted at `/`
 * rooted at the process's current working directory.

When making a new loader from an existing loader,
retaining history along an overlay trace, the only
allowed use is to go deeper into a file hierarchy, or
go up and over to a never before visited sibling. This
fix can probably be defeated by devious symbolic links.
2018-10-29 11:10:21 -07:00
Jeff Regan
1224dc0c87 Merge pull request #517 from monopole/testCleanup
Improve test coverage.
2018-10-28 14:17:44 -07:00
jregan
885c1952a4 Improve test coverage. 2018-10-28 13:52:25 -07:00
k8s-ci-robot
383b3e798b Merge pull request #516 from kubernetes-sigs/revert-488-prefix
Revert "Skip adding nameprefix to namespace"
2018-10-26 16:07:54 -07:00
Jingfang Liu
1020167e22 Revert "Skip adding nameprefix to namespace" 2018-10-26 14:42:27 -07:00
k8s-ci-robot
3c242f58da Merge pull request #515 from kubernetes-sigs/revert-500-namespace
Revert "create namespace resource if not found"
2018-10-26 14:25:03 -07:00
Jingfang Liu
f8a18ce662 Revert "create namespace resource if not found" 2018-10-26 14:01:53 -07:00
k8s-ci-robot
6a917c5f36 Merge pull request #508 from ryane/fix-507
kustomize build loads transformer configs when path argument omitted
2018-10-26 11:10:14 -07:00
k8s-ci-robot
7af1f206aa Merge pull request #513 from sethpollack/patch-1
fix spelling
2018-10-26 10:48:29 -07:00
k8s-ci-robot
0714abfe79 Merge pull request #511 from BenTheElder/export-factory
make k8sdeps.NewFactory() importable
2018-10-26 10:43:55 -07:00
Seth Pollack
6037734641 fix spelling 2018-10-25 22:28:22 -04:00
Benjamin Elder
76ba38cec5 make k8sdeps.NewFactory() importable 2018-10-25 17:21:58 -07:00
k8s-ci-robot
5c918dc56a Merge pull request #510 from ryane/fix-509
fix goreleaser version injection
2018-10-25 16:52:10 -07:00
k8s-ci-robot
292ed0e605 Merge pull request #503 from BenTheElder/prune-vendor
Prune vendor
2018-10-25 16:27:29 -07:00
ryane
e97960c2f0 fix goreleaser version injection
version command and vars moved into misc package
2018-10-25 10:47:10 -04:00
ryane
9f73341271 kustomize build loads transformer configs when path argument omitted 2018-10-25 09:56:46 -04:00
Benjamin Elder
163515c5a0 dep ensure 2018-10-24 17:24:58 -07:00
Benjamin Elder
41845522f6 enable dep pruning and gitignore macOS DS_Store 2018-10-24 17:15:07 -07:00
6860 changed files with 2548 additions and 3355217 deletions

3
.gitignore vendored
View File

@@ -14,3 +14,6 @@ kustomize
# We use sed -i.bak when doing in-line replace, because it works better cross-platform
.bak
# macOS
*.DS_store

253
Gopkg.lock generated
View File

@@ -2,165 +2,109 @@
[[projects]]
digest = "1:8e47871087b94913898333f37af26732faaab30cdb41571136cf7aec9921dae7"
digest = "1:d8ebbd207f3d3266d4423ce4860c9f3794956306ded6c7ba312ecc69cdfbf04c"
name = "github.com/PuerkitoBio/purell"
packages = ["."]
pruneopts = ""
pruneopts = "NUT"
revision = "0bcb03f4b4d0a9428594752bd2a3b9aa0a9d4bd4"
version = "v1.1.0"
[[projects]]
branch = "master"
digest = "1:331a419049c2be691e5ba1d24342fc77c7e767a80c666a18fd8a9f7b82419c1c"
digest = "1:8098cd40cd09879efbf12e33bcd51ead4a66006ac802cd563a66c4f3373b9727"
name = "github.com/PuerkitoBio/urlesc"
packages = ["."]
pruneopts = ""
pruneopts = "NUT"
revision = "de5bf2ad457846296e2031421a34e2568e304e35"
[[projects]]
digest = "1:4905d4dce09d1a9e93fe2b9733f92bee5f149dd229727bbc074465b24fb489cd"
name = "github.com/aws/aws-sdk-go"
packages = [
"aws",
"aws/awserr",
"aws/awsutil",
"aws/client",
"aws/client/metadata",
"aws/corehandlers",
"aws/credentials",
"aws/credentials/ec2rolecreds",
"aws/credentials/endpointcreds",
"aws/credentials/stscreds",
"aws/csm",
"aws/defaults",
"aws/ec2metadata",
"aws/endpoints",
"aws/request",
"aws/session",
"aws/signer/v4",
"internal/sdkio",
"internal/sdkrand",
"internal/sdkuri",
"internal/shareddefaults",
"private/protocol",
"private/protocol/eventstream",
"private/protocol/eventstream/eventstreamapi",
"private/protocol/query",
"private/protocol/query/queryutil",
"private/protocol/rest",
"private/protocol/restxml",
"private/protocol/xml/xmlutil",
"service/s3",
"service/sts",
]
pruneopts = ""
revision = "fde4ded7becdeae4d26bf1212916aabba79349b4"
version = "v1.14.12"
[[projects]]
branch = "master"
digest = "1:98e84060475ed245c3b355042afd43a74aa7d32efe50658f4f995977916f9fc3"
name = "github.com/bgentry/go-netrc"
packages = ["netrc"]
pruneopts = ""
revision = "9fd32a8b3d3d3f9d43c341bfe098430e07609480"
[[projects]]
digest = "1:56c130d885a4aacae1dd9c7b71cfe39912c7ebc1ff7d2b46083c8812996dc43b"
digest = "1:a2c1d0e43bd3baaa071d1b9ed72c27d78169b2b269f71c105ac4ba34b1be4a39"
name = "github.com/davecgh/go-spew"
packages = ["spew"]
pruneopts = ""
pruneopts = "NUT"
revision = "346938d642f2ec3594ed81d874461961cd0faa76"
version = "v1.1.0"
[[projects]]
digest = "1:971e9ba63a417c5f1f83ab358677bc59e96ff04285f26c6646ff089fb60b15e8"
digest = "1:f8e6f07329067bc182633dcb19a3df53ce5d454b551e1b5a1cac2163748648d9"
name = "github.com/emicklei/go-restful"
packages = [
".",
"log",
]
pruneopts = ""
pruneopts = "NUT"
revision = "3658237ded108b4134956c1b3050349d93e7b895"
version = "v2.7.1"
[[projects]]
digest = "1:dcefbadf4534c5ecac8573698fba6e6e601157bfa8f96aafe29df31ae582ef2a"
digest = "1:ad32dc29f37281bacb5dcedff17c9461dc1739dc8a5f63a71ab491c6e92edf8d"
name = "github.com/evanphx/json-patch"
packages = ["."]
pruneopts = ""
pruneopts = "NUT"
revision = "afac545df32f2287a079e2dfb7ba2745a643747e"
version = "v3.0.0"
[[projects]]
digest = "1:b13707423743d41665fd23f0c36b2f37bb49c30e94adb813319c44188a51ba22"
digest = "1:81466b4218bf6adddac2572a30ac733a9255919bc2f470b4827a317bd4ee1756"
name = "github.com/ghodss/yaml"
packages = ["."]
pruneopts = ""
pruneopts = "NUT"
revision = "0ca9ea5df5451ffdf184b4428c902747c2c11cd7"
version = "v1.0.0"
[[projects]]
digest = "1:858b7fe7b0f4bc7ef9953926828f2816ea52d01a88d72d1c45bc8c108f23c356"
name = "github.com/go-ini/ini"
packages = ["."]
pruneopts = ""
revision = "358ee7663966325963d4e8b2e1fbd570c5195153"
version = "v1.38.1"
[[projects]]
branch = "master"
digest = "1:e116a4866bffeec941056a1fcfd37e520fad1ee60e4e3579719f19a43c392e10"
digest = "1:260f7ebefc63024c8dfe2c9f1a2935a89fa4213637a1f522f592f80c001cc441"
name = "github.com/go-openapi/jsonpointer"
packages = ["."]
pruneopts = ""
pruneopts = "NUT"
revision = "3a0015ad55fa9873f41605d3e8f28cd279c32ab2"
[[projects]]
branch = "master"
digest = "1:3830527ef0f4f9b268d9286661c0f52f9115f8aefd9f45ee7352516f93489ac9"
digest = "1:98abd61947ff5c7c6fcfec5473d02a4821ed3a2dd99a4fbfdb7925b0dd745546"
name = "github.com/go-openapi/jsonreference"
packages = ["."]
pruneopts = ""
pruneopts = "NUT"
revision = "3fb327e6747da3043567ee86abd02bb6376b6be2"
[[projects]]
branch = "master"
digest = "1:238a056875c4b053b4b29984765ee335bf8c539fdf17e527fd9b7aa72521c8dd"
digest = "1:e95b560c49fb849a61957a5fb3346ce23b3f67426e00e01179e5396cabc9a12c"
name = "github.com/go-openapi/spec"
packages = ["."]
pruneopts = ""
pruneopts = "NUT"
revision = "bcff419492eeeb01f76e77d2ebc714dc97b607f5"
[[projects]]
branch = "master"
digest = "1:7b067ca8b94982960860d18c42e29f15bbd0e8d9ae8145a83a218296e75393cf"
digest = "1:a610c604eb06f0be4b0fc667388b7a221155d77d7f9089f70ac142a4a9daf014"
name = "github.com/go-openapi/swag"
packages = ["."]
pruneopts = ""
pruneopts = "NUT"
revision = "811b1089cde9dad18d4d0c2d09fbdbf28dbd27a5"
[[projects]]
digest = "1:0a3f6a0c68ab8f3d455f8892295503b179e571b7fefe47cc6c556405d1f83411"
digest = "1:1b3dd24f14a5280710fc7a3aa2480b6e4d20fdfc905841de9a3aa2aa2f1d4ee9"
name = "github.com/gogo/protobuf"
packages = [
"proto",
"sortkeys",
]
pruneopts = ""
pruneopts = "NUT"
revision = "1adfc126b41513cc696b209667c8656ea7aac67c"
version = "v1.0.0"
[[projects]]
branch = "master"
digest = "1:107b233e45174dbab5b1324201d092ea9448e58243ab9f039e4c0f332e121e3a"
digest = "1:e2b86e41f3d669fc36b50d31d32d22c8ac656c75aa5ea89717ce7177e134ff2a"
name = "github.com/golang/glog"
packages = ["."]
pruneopts = ""
pruneopts = "NUT"
revision = "23def4e6c14b4da8ac2ed8007337bc5eb5007998"
[[projects]]
digest = "1:f958a1c137db276e52f0b50efee41a1a389dcdded59a69711f3e872757dab34b"
digest = "1:03e14cff610a8a58b774e36bd337fa979482be86aab01be81fb8bbd6d0f07fc8"
name = "github.com/golang/protobuf"
packages = [
"proto",
@@ -169,172 +113,101 @@
"ptypes/duration",
"ptypes/timestamp",
]
pruneopts = ""
pruneopts = "NUT"
revision = "b4deda0973fb4c70b50d226b1af49f3da59f5265"
version = "v1.1.0"
[[projects]]
branch = "master"
digest = "1:754f77e9c839b24778a4b64422236d38515301d2baeb63113aa3edc42e6af692"
digest = "1:52c5834e2bebac9030c97cc0798ac11c3aa8a39f098aeb419f142533da6cd3cc"
name = "github.com/google/gofuzz"
packages = ["."]
pruneopts = ""
pruneopts = "NUT"
revision = "24818f796faf91cd76ec7bddd72458fbced7a6c1"
[[projects]]
digest = "1:2a131706ff80636629ab6373f2944569b8252ecc018cda8040931b05d32e3c16"
digest = "1:3d7c1446fc5c710351b246c0dc6700fae843ca27f5294d0bd9f68bab2a810c44"
name = "github.com/googleapis/gnostic"
packages = [
"OpenAPIv2",
"compiler",
"extensions",
]
pruneopts = ""
pruneopts = "NUT"
revision = "ee43cbb60db7bd22502942cccbc39059117352ab"
version = "v0.1.0"
[[projects]]
branch = "master"
digest = "1:f5d25fd7bdda08e39e01193ef94a1ebf7547b1b931bcdec785d08050598f306c"
name = "github.com/hashicorp/go-cleanhttp"
packages = ["."]
pruneopts = ""
revision = "d5fe4b57a186c716b0e00b8c301cbd9b4182694d"
[[projects]]
branch = "master"
digest = "1:fd15b3f6aac9d0fe68c6e38922282e0d2e88cd77b927ac3dd842e363645522c0"
name = "github.com/hashicorp/go-getter"
packages = [
".",
"helper/url",
]
pruneopts = ""
revision = "4bda8fa99001c61db3cad96b421d4c12a81f256d"
[[projects]]
branch = "master"
digest = "1:2cf6c60c74eacadd31652674364af55c8d54a86b8ea193548f1c37f8c9af8f9c"
name = "github.com/hashicorp/go-safetemp"
packages = ["."]
pruneopts = ""
revision = "b1a1dbde6fdc11e3ae79efd9039009e22d4ae240"
[[projects]]
branch = "master"
digest = "1:139bdc2c89779b8ff8b1150be28f889b0ed964e6da96f32cbc9035bd4642881c"
name = "github.com/hashicorp/go-version"
packages = ["."]
pruneopts = ""
revision = "270f2f71b1ee587f3b609f00f422b76a6b28f348"
[[projects]]
digest = "1:870d441fe217b8e689d7949fef6e43efbc787e50f200cb1e70dbca9204a1d6be"
digest = "1:406338ad39ab2e37b7f4452906442a3dbf0eb3379dd1f06aafb5c07e769a5fbb"
name = "github.com/inconshreveable/mousetrap"
packages = ["."]
pruneopts = ""
pruneopts = "NUT"
revision = "76626ae9c91c4f2a10f34cad8ce83ea42c93bb75"
version = "v1.0"
[[projects]]
digest = "1:6f49eae0c1e5dab1dafafee34b207aeb7a42303105960944828c2079b92fc88e"
name = "github.com/jmespath/go-jmespath"
packages = ["."]
pruneopts = ""
revision = "0b12d6b5"
[[projects]]
digest = "1:9eab2325abbed0ebcee9d44bb3660a69d5d10e42d5ac4a0e77f7a6ea22bfce88"
digest = "1:42c47ace7ccb114261ef7e0d418d274921514ab50a3bf6bdb9e51c3dde8ce13d"
name = "github.com/json-iterator/go"
packages = ["."]
pruneopts = ""
pruneopts = "NUT"
revision = "ca39e5af3ece67bbcda3d0f4f56a8e24d9f2dad4"
version = "1.1.3"
[[projects]]
branch = "master"
digest = "1:d9e483f4b9e306facf126bd90b02d512bd22ea4471e1568867e32221a8abbb16"
digest = "1:ada518b8c338e10e0afa443d84671476d3bd1d926e13713938088e8ddbee1a3e"
name = "github.com/mailru/easyjson"
packages = [
"buffer",
"jlexer",
"jwriter",
]
pruneopts = ""
pruneopts = "NUT"
revision = "3fdea8d05856a0c8df22ed4bc71b3219245e4485"
[[projects]]
branch = "master"
digest = "1:83854f6b1d2ce047b69657e3a87ba7602f4c5505e8bdfd02ab857db8e983bde1"
name = "github.com/mitchellh/go-homedir"
packages = ["."]
pruneopts = ""
revision = "58046073cbffe2f25d425fe1331102f55cf719de"
[[projects]]
branch = "master"
digest = "1:51c98e2c9a8d0a724a69f46421876af14e12132cb02f1d0e144785d752247162"
name = "github.com/mitchellh/go-testing-interface"
packages = ["."]
pruneopts = ""
revision = "a61a99592b77c9ba629d254a693acffaeb4b7e28"
[[projects]]
digest = "1:0c0ff2a89c1bb0d01887e1dac043ad7efbf3ec77482ef058ac423d13497e16fd"
digest = "1:2f42fa12d6911c7b7659738758631bec870b7e9b4c6be5444f963cdcfccc191f"
name = "github.com/modern-go/concurrent"
packages = ["."]
pruneopts = ""
pruneopts = "NUT"
revision = "bacd9c7ef1dd9b15be4a9909b8ac7a4e313eec94"
version = "1.0.3"
[[projects]]
digest = "1:420f9231f816eeca3ff5aab070caac3ed7f27e4d37ded96ce9de3d7a7a2e31ad"
digest = "1:314a5881fab303a80d6d2e35a77000f2224bb50f09ef63a9aa4c1f9eaef985d8"
name = "github.com/modern-go/reflect2"
packages = ["."]
pruneopts = ""
pruneopts = "NUT"
revision = "1df9eeb2bb81f327b96228865c5687bc2194af3f"
version = "1.0.0"
[[projects]]
digest = "1:7365acd48986e205ccb8652cc746f09c8b7876030d53710ea6ef7d0bd0dcd7ca"
digest = "1:5cf3f025cbee5951a4ee961de067c8a89fc95a5adabead774f82822efabab121"
name = "github.com/pkg/errors"
packages = ["."]
pruneopts = ""
pruneopts = "NUT"
revision = "645ef00459ed84a119197bfb8d8205042c6df63d"
version = "v0.8.0"
[[projects]]
digest = "1:74c32990510c9f188556aa17600313e867d1d06f5a9db244056a95d144ec34ce"
digest = "1:0f156dbd01b40676bdcbc64e51535c09b50f83c9cca5faef3090f82f18bda3c2"
name = "github.com/spf13/cobra"
packages = ["."]
pruneopts = ""
pruneopts = "NUT"
revision = "a1f051bc3eba734da4772d60e2d677f47cf93ef4"
version = "v0.0.2"
[[projects]]
digest = "1:8e243c568f36b09031ec18dff5f7d2769dcf5ca4d624ea511c8e3197dc3d352d"
digest = "1:15e5c398fbd9d2c439b635a08ac161b13d04f0c2aa587fe256b65dc0c3efe8b7"
name = "github.com/spf13/pflag"
packages = ["."]
pruneopts = ""
pruneopts = "NUT"
revision = "583c0c0531f06d5278b7d917446061adc344b5cd"
version = "v1.0.1"
[[projects]]
digest = "1:ee723e6a1962a196eeba1b24f82af61a4f60f8821d7aa96d48e787f8337bcffc"
name = "github.com/ulikunitz/xz"
packages = [
".",
"internal/hash",
"internal/xlog",
"lzma",
]
pruneopts = ""
revision = "0c6b41e72360850ca4f98dc341fd999726ea007f"
version = "v0.5.4"
[[projects]]
branch = "master"
digest = "1:35171304d8332a0cfac5f3bd9222467f036732ddde75c65278b16f65216e03ed"
digest = "1:d1a6ebe75268a41b6fbb1d43947cf8688d8580423b7484fa5ae608beef6df24d"
name = "golang.org/x/net"
packages = [
"http2",
@@ -342,11 +215,11 @@
"idna",
"lex/httplex",
]
pruneopts = ""
pruneopts = "NUT"
revision = "1c05540f6879653db88113bc4a2b70aec4bd491f"
[[projects]]
digest = "1:5acd3512b047305d49e8763eef7ba423901e85d5dd2fd1e71778a0ea8de10bd4"
digest = "1:e33513a825fcd765e97b5de639a2f7547542d1a8245df0cef18e1fd390b778a9"
name = "golang.org/x/text"
packages = [
"collate",
@@ -365,29 +238,29 @@
"unicode/rangetable",
"width",
]
pruneopts = ""
pruneopts = "NUT"
revision = "f21a4dfb5e38f5895301dc265a8def02365cc3d0"
version = "v0.3.0"
[[projects]]
digest = "1:75fb3fcfc73a8c723efde7777b40e8e8ff9babf30d8c56160d01beffea8a95a6"
digest = "1:2d1fbdc6777e5408cabeb02bf336305e724b925ff4546ded0fa8715a7267922a"
name = "gopkg.in/inf.v0"
packages = ["."]
pruneopts = ""
pruneopts = "NUT"
revision = "d2d2541c53f18d2a059457998ce2876cc8e67cbf"
version = "v0.9.1"
[[projects]]
digest = "1:f0620375dd1f6251d9973b5f2596228cc8042e887cd7f827e4220bc1ce8c30e2"
digest = "1:7c95b35057a0ff2e19f707173cc1a947fa43a6eb5c4d300d196ece0334046082"
name = "gopkg.in/yaml.v2"
packages = ["."]
pruneopts = ""
pruneopts = "NUT"
revision = "5420a8b6744d3b0345ab293f6fcba19c978f1183"
version = "v2.2.1"
[[projects]]
branch = "master"
digest = "1:663df6da5560210fc39194a0a2c4fceba09ead717c330f1174bb15597cf18ce8"
digest = "1:d895c7c24a0dd1ed2ecd061fd88dfea9e1e84d6f280ed859942a2d1aabee10ec"
name = "k8s.io/api"
packages = [
"admissionregistration/v1alpha1",
@@ -419,12 +292,12 @@
"storage/v1alpha1",
"storage/v1beta1",
]
pruneopts = ""
pruneopts = "NUT"
revision = "53d615ae3f440f957cb9989d989d597f047262d9"
[[projects]]
branch = "master"
digest = "1:bcb2285bb525712de7903a5d254c2789df65c8b58d2cfac5a26d950ad94c2079"
digest = "1:dff69dd9d9fc681ae077ce5a409aca3c24894d09102ab0395ca7972f6ec01811"
name = "k8s.io/apimachinery"
packages = [
"pkg/api/equality",
@@ -465,26 +338,26 @@
"third_party/forked/golang/json",
"third_party/forked/golang/reflect",
]
pruneopts = ""
pruneopts = "NUT"
revision = "13b73596e4b63e03203e86f6d9c7bcc1b937c62f"
[[projects]]
digest = "1:071cc2f032b701b9dba26568e040940f26931a49e3a3985f3375f17f7f6d9c5f"
digest = "1:ae9ced9ef7b8eb2794a4f80bc3af9d2bc38ec7d60337367bad9a655c1d641458"
name = "k8s.io/client-go"
packages = ["kubernetes/scheme"]
pruneopts = ""
pruneopts = "NUT"
revision = "23781f4d6632d88e869066eaebb743857aa1ef9b"
version = "v7.0.0"
[[projects]]
branch = "master"
digest = "1:386c5d69077ce740614e8309ddf107dde91a5db25d3d779143f452fb4fbdfd1e"
digest = "1:f4fb3421360af5c51070bfe0c1c7467f8809fa70e278e129f068f5106b5c8a65"
name = "k8s.io/kube-openapi"
packages = [
"pkg/common",
"pkg/util/proto",
]
pruneopts = ""
pruneopts = "NUT"
revision = "b3f03f55328800731ce03a164b80973014ecd455"
[solve-meta]
@@ -493,8 +366,6 @@
input-imports = [
"github.com/evanphx/json-patch",
"github.com/ghodss/yaml",
"github.com/golang/glog",
"github.com/hashicorp/go-getter",
"github.com/pkg/errors",
"github.com/spf13/cobra",
"gopkg.in/yaml.v2",

View File

@@ -1,4 +1,3 @@
# Gopkg.toml example
#
# Refer to https://github.com/golang/dep/blob/master/docs/Gopkg.toml.md
@@ -20,6 +19,11 @@
# name = "github.com/x/y"
# version = "2.4.0"
# prune out unused content from vendor
[prune]
go-tests = true
non-go = true
unused-packages = true
[[constraint]]
name = "github.com/evanphx/json-patch"
@@ -29,10 +33,6 @@
name = "github.com/ghodss/yaml"
version = "1.0.0"
[[constraint]]
branch = "master"
name = "github.com/golang/glog"
[[constraint]]
name = "github.com/spf13/cobra"
version = "0.0.2"
@@ -56,11 +56,3 @@
[[override]]
branch = "master"
name = "github.com/go-openapi/spec"
[[constraint]]
branch = "master"
name = "github.com/hashicorp/go-getter"
[[constraint]]
name = "github.com/krishicks/yaml-patch"
version = "0.0.10"

View File

@@ -4,7 +4,7 @@ project_name: kustomize
builds:
- main: ./kustomize.go
binary: kustomize
ldflags: -s -X sigs.k8s.io/kustomize/pkg/commands.kustomizeVersion={{.Version}} -X sigs.k8s.io/kustomize/pkg/commands.gitCommit={{.Commit}} -X sigs.k8s.io/kustomize/pkg/commands.buildDate={{.Date}}
ldflags: -s -X sigs.k8s.io/kustomize/pkg/commands/misc.kustomizeVersion={{.Version}} -X sigs.k8s.io/kustomize/pkg/commands/misc.gitCommit={{.Commit}} -X sigs.k8s.io/kustomize/pkg/commands/misc.buildDate={{.Date}}
goos:
- darwin
- linux

281
build/vendor_kustomize.diff Executable file
View File

@@ -0,0 +1,281 @@
commit 1b893558aa83ac6491e5ba416b493170a9045fec
Author: Jingfang Liu <jingfangliu@google.com>
Date: Mon Nov 12 10:26:12 2018 -0800
last change
diff --git a/staging/src/k8s.io/cli-runtime/artifacts/kustomization/configMap.yaml b/staging/src/k8s.io/cli-runtime/artifacts/kustomization/configMap.yaml
new file mode 100644
index 0000000000..0008853094
--- /dev/null
+++ b/staging/src/k8s.io/cli-runtime/artifacts/kustomization/configMap.yaml
@@ -0,0 +1,8 @@
+
+apiVersion: v1
+kind: ConfigMap
+metadata:
+ name: the-map
+data:
+ altGreeting: "Good Morning!"
+ enableRisky: "false"
diff --git a/staging/src/k8s.io/cli-runtime/artifacts/kustomization/deployment.yaml b/staging/src/k8s.io/cli-runtime/artifacts/kustomization/deployment.yaml
new file mode 100644
index 0000000000..6e79409080
--- /dev/null
+++ b/staging/src/k8s.io/cli-runtime/artifacts/kustomization/deployment.yaml
@@ -0,0 +1,30 @@
+apiVersion: apps/v1
+kind: Deployment
+metadata:
+ name: the-deployment
+spec:
+ replicas: 3
+ template:
+ metadata:
+ labels:
+ deployment: hello
+ spec:
+ containers:
+ - name: the-container
+ image: monopole/hello:1
+ command: ["/hello",
+ "--port=8080",
+ "--enableRiskyFeature=$(ENABLE_RISKY)"]
+ ports:
+ - containerPort: 8080
+ env:
+ - name: ALT_GREETING
+ valueFrom:
+ configMapKeyRef:
+ name: the-map
+ key: altGreeting
+ - name: ENABLE_RISKY
+ valueFrom:
+ configMapKeyRef:
+ name: the-map
+ key: enableRisky
diff --git a/staging/src/k8s.io/cli-runtime/artifacts/kustomization/kustomization.yaml b/staging/src/k8s.io/cli-runtime/artifacts/kustomization/kustomization.yaml
new file mode 100644
index 0000000000..6e1e3202d5
--- /dev/null
+++ b/staging/src/k8s.io/cli-runtime/artifacts/kustomization/kustomization.yaml
@@ -0,0 +1,5 @@
+nameprefix: test-
+ resources:
+- deployment.yaml
+- service.yaml
+- configMap.yaml
diff --git a/staging/src/k8s.io/cli-runtime/artifacts/kustomization/service.yaml b/staging/src/k8s.io/cli-runtime/artifacts/kustomization/service.yaml
new file mode 100644
index 0000000000..2942cdb7df
--- /dev/null
+++ b/staging/src/k8s.io/cli-runtime/artifacts/kustomization/service.yaml
@@ -0,0 +1,13 @@
+kind: Service
+apiVersion: v1
+metadata:
+ name: the-service
+spec:
+ selector:
+ deployment: hello
+ type: LoadBalancer
+ ports:
+ - protocol: TCP
+ port: 8666
+ targetPort: 8080
+
\ No newline at end of file
diff --git a/staging/src/k8s.io/cli-runtime/pkg/genericclioptions/resource/BUILD b/staging/src/k8s.io/cli-runtime/pkg/genericclioptions/resource/BUILD
index 22b34de008..b91d1c0130 100644
--- a/staging/src/k8s.io/cli-runtime/pkg/genericclioptions/resource/BUILD
+++ b/staging/src/k8s.io/cli-runtime/pkg/genericclioptions/resource/BUILD
@@ -35,12 +35,15 @@ go_library(
"//staging/src/k8s.io/apimachinery/pkg/util/sets:go_default_library",
"//staging/src/k8s.io/apimachinery/pkg/util/yaml:go_default_library",
"//staging/src/k8s.io/apimachinery/pkg/watch:go_default_library",
+ "//staging/src/k8s.io/cli-runtime/pkg/kustomize/k8sdeps:go_default_library",
"//staging/src/k8s.io/client-go/discovery:go_default_library",
"//staging/src/k8s.io/client-go/kubernetes/scheme:go_default_library",
"//staging/src/k8s.io/client-go/rest:go_default_library",
"//staging/src/k8s.io/client-go/restmapper:go_default_library",
"//vendor/golang.org/x/text/encoding/unicode:go_default_library",
"//vendor/golang.org/x/text/transform:go_default_library",
+ "//vendor/sigs.k8s.io/kustomize/pkg/commands/build:go_default_library",
+ "//vendor/sigs.k8s.io/kustomize/pkg/fs:go_default_library",
],
)
diff --git a/staging/src/k8s.io/cli-runtime/pkg/genericclioptions/resource/builder_test.go b/staging/src/k8s.io/cli-runtime/pkg/genericclioptions/resource/builder_test.go
index 7fd526b33c..801f13f772 100644
--- a/staging/src/k8s.io/cli-runtime/pkg/genericclioptions/resource/builder_test.go
+++ b/staging/src/k8s.io/cli-runtime/pkg/genericclioptions/resource/builder_test.go
@@ -465,27 +465,48 @@ func TestPathBuilderWithMultipleInvalid(t *testing.T) {
}
func TestDirectoryBuilder(t *testing.T) {
- b := newDefaultBuilder().
- FilenameParam(false, &FilenameOptions{Recursive: false, Filenames: []string{"../../../artifacts/guestbook"}}).
- NamespaceParam("test").DefaultNamespace()
+ tests := []struct {
+ directories []string
+ singleItem bool
+ number int
+ expectedNames []string
+ }{
+ {[]string{"../../../artifacts/guestbook"}, false, 3, []string{"redis-master"}},
+ {[]string{"../../../artifacts/kustomization"}, true, 3, []string{"test-the-deployment"}},
+ {[]string{"../../../artifacts/guestbook", "../../../artifacts/kustomization"}, false, 6, []string{"redis-master", "test-the-deployment"}},
+ }
- test := &testVisitor{}
- singleItemImplied := false
+ for _, tt := range tests {
+ b := newDefaultBuilder().
+ FilenameParam(false, &FilenameOptions{Recursive: false, Filenames: tt.directories}).
+ NamespaceParam("test").DefaultNamespace()
- err := b.Do().IntoSingleItemImplied(&singleItemImplied).Visit(test.Handle)
- if err != nil || singleItemImplied || len(test.Infos) < 3 {
- t.Fatalf("unexpected response: %v %t %#v", err, singleItemImplied, test.Infos)
- }
+ test := &testVisitor{}
+ singleItemImplied := false
- found := false
- for _, info := range test.Infos {
- if info.Name == "redis-master" && info.Namespace == "test" && info.Object != nil {
- found = true
- break
+ err := b.Do().IntoSingleItemImplied(&singleItemImplied).Visit(test.Handle)
+ if err != nil || singleItemImplied != tt.singleItem || len(test.Infos) < tt.number {
+ t.Fatalf("unexpected response: %v %t %#v", err, singleItemImplied, test.Infos)
+ }
+
+ contained := func(name string) bool {
+ for _, info := range test.Infos {
+ if info.Name == name && info.Namespace == "test" && info.Object != nil {
+ return true
+ }
+ }
+ return false
+ }
+
+ allFound := true
+ for _, name := range tt.expectedNames {
+ if !contained(name) {
+ allFound = false
+ }
+ }
+ if !allFound {
+ t.Errorf("unexpected responses: %#v", test.Infos)
}
- }
- if !found {
- t.Errorf("unexpected responses: %#v", test.Infos)
}
}
diff --git a/staging/src/k8s.io/cli-runtime/pkg/genericclioptions/resource/visitor.go b/staging/src/k8s.io/cli-runtime/pkg/genericclioptions/resource/visitor.go
index 32c1a691a5..d7a37e1cde 100644
--- a/staging/src/k8s.io/cli-runtime/pkg/genericclioptions/resource/visitor.go
+++ b/staging/src/k8s.io/cli-runtime/pkg/genericclioptions/resource/visitor.go
@@ -20,10 +20,12 @@ import (
"bytes"
"fmt"
"io"
+ "io/ioutil"
"net/http"
"net/url"
"os"
"path/filepath"
+ "strings"
"time"
"golang.org/x/text/encoding/unicode"
@@ -38,6 +40,9 @@ import (
utilerrors "k8s.io/apimachinery/pkg/util/errors"
"k8s.io/apimachinery/pkg/util/yaml"
"k8s.io/apimachinery/pkg/watch"
+ "k8s.io/cli-runtime/pkg/kustomize/k8sdeps"
+ "sigs.k8s.io/kustomize/pkg/commands/build"
+ "sigs.k8s.io/kustomize/pkg/fs"
)
const (
@@ -452,7 +457,10 @@ func ExpandPathsToFileVisitors(mapper *mapper, paths string, recursive bool, ext
if err != nil {
return err
}
-
+ if isKustomizationDir(path) {
+ visitors = append(visitors, NewKustomizationVisitor(mapper, path, schema))
+ return filepath.SkipDir
+ }
if fi.IsDir() {
if path != paths && !recursive {
return filepath.SkipDir
@@ -463,7 +471,10 @@ func ExpandPathsToFileVisitors(mapper *mapper, paths string, recursive bool, ext
if path != paths && ignoreFile(path, extensions) {
return nil
}
-
+ if strings.HasSuffix(path, "kustomization.yaml") {
+ visitors = append(visitors, NewKustomizationVisitor(mapper, filepath.Dir(path), schema))
+ return nil
+ }
visitor := &FileVisitor{
Path: path,
StreamVisitor: NewStreamVisitor(nil, mapper, path, schema),
@@ -479,6 +490,14 @@ func ExpandPathsToFileVisitors(mapper *mapper, paths string, recursive bool, ext
return visitors, nil
}
+func isKustomizationDir(path string) bool {
+ if _, err := os.Stat(filepath.Join(path, "kustomization.yaml")); err == nil {
+ return true
+ }
+ return false
+}
+
+
// FileVisitor is wrapping around a StreamVisitor, to handle open/close files
type FileVisitor struct {
Path string
@@ -507,6 +526,37 @@ func (v *FileVisitor) Visit(fn VisitorFunc) error {
return v.StreamVisitor.Visit(fn)
}
+// KustomizationVisitor prorvides the output of kustomization build
+type KustomizationVisitor struct {
+ Path string
+ *StreamVisitor
+}
+
+// Visit in a KustomizationVisitor build the kustomization output
+func (v *KustomizationVisitor) Visit(fn VisitorFunc) error {
+ fSys := fs.MakeRealFS()
+ f := k8sdeps.NewFactory()
+ var out bytes.Buffer
+ cmd := build.NewCmdBuild(&out, fSys, f.ResmapF, f.TransformerF)
+ cmd.SetArgs([]string{v.Path})
+ // we want to silence usage, error output, and any future output from cobra
+ // we will get error output as a golang error from execute
+ cmd.SetOutput(ioutil.Discard)
+ _, err := cmd.ExecuteC()
+ if err != nil {
+ return err
+ }
+ v.StreamVisitor.Reader = bytes.NewReader(out.Bytes())
+ return v.StreamVisitor.Visit(fn)
+}
+
+func NewKustomizationVisitor(mapper *mapper, path string, schema ContentValidator) *KustomizationVisitor {
+ return &KustomizationVisitor{
+ Path: path,
+ StreamVisitor: NewStreamVisitor(nil, mapper, path, schema),
+ }
+}
+
// StreamVisitor reads objects from an io.Reader and walks them. A stream visitor can only be
// visited once.
// TODO: depends on objects being in JSON format before being passed to decode - need to implement

View File

@@ -17,6 +17,8 @@
set -e
set -x
DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" >/dev/null && pwd )"
# vendor_kustomize.sh creates the change in kubernetes repo for vendoring kustomize
function setUpWorkspace {
@@ -26,27 +28,27 @@ function setUpWorkspace {
}
function cloneK8s {
mkdir -p $GOPATH/src/k8s.io
cd $GOPATH/src/k8s.io
mkdir -p $KPATH/src/k8s.io
cd $KPATH/src/k8s.io
git clone git@github.com:kubernetes/kubernetes.git
}
function godepRestore {
cd $GOPATH/src/k8s.io/kubernetes
cd $KPATH/src/k8s.io/kubernetes
# restore dependencies
hack/godep-restore.sh
hack/run-in-gopath.sh hack/godep-restore.sh
}
function getKustomizeDeps {
# get Kustomize and Kustomize dependencies
godep get sigs.k8s.io/kustomize/pkg/commands
godep get github.com/bgentry/go-netrc/netrc
godep get github.com/hashicorp/go-cleanhttp
godep get github.com/hashicorp/go-getter
godep get github.com/hashicorp/go-safetemp
godep get github.com/hashicorp/go-version
hack/run-in-gopath.sh godep get sigs.k8s.io/kustomize/pkg/commands
hack/run-in-gopath.sh godep get github.com/bgentry/go-netrc/netrc
hack/run-in-gopath.sh godep get github.com/hashicorp/go-cleanhttp
hack/run-in-gopath.sh godep get github.com/hashicorp/go-getter
hack/run-in-gopath.sh godep get github.com/hashicorp/go-safetemp
hack/run-in-gopath.sh godep get github.com/hashicorp/go-version
# The hashes below passed bin/pre-commit.sh with kustomize HEAD at time of merger.
DEPS=(
@@ -61,7 +63,7 @@ function getKustomizeDeps {
)
function foo {
cd $GOPATH/src/github.com/$1
cd $KPATH/src/k8s.io/kubernetes/_output/local/go/src/github.com/$1
git checkout $2
}
for i in "${DEPS[@]}"; do
@@ -70,58 +72,35 @@ function getKustomizeDeps {
}
function updateK8s {
# Copy k8sdeps from Kustomize to kubectl
mkdir -p $GOPATH/src/k8s.io/kubernetes/pkg/kubectl/kustomize
cp -r $GOPATH/src/sigs.k8s.io/kustomize/internal/k8sdeps \
$GOPATH/src/k8s.io/kubernetes/pkg/kubectl/kustomize/k8sdeps
# Copy k8sdeps from Kustomize to cli-runtime in staging
mkdir -p $KPATH/src/k8s.io/kubernetes/staging/src/k8s.io/cli-runtime/pkg/kustomize
cp -r $KPATH/src/k8s.io/kubernetes/_output/local/go/src/sigs.k8s.io/kustomize/k8sdeps \
$KPATH/src/k8s.io/kubernetes/staging/src/k8s.io/cli-runtime/pkg/kustomize/k8sdeps
# Change import path of k8sdeps
find $GOPATH/src/k8s.io/kubernetes/pkg/kubectl/kustomize/k8sdeps \
find $KPATH/src/k8s.io/kubernetes/staging/src/k8s.io/cli-runtime/pkg/kustomize/k8sdeps \
-type f -name "*.go" | \
xargs sed -i \
's!sigs.k8s.io/kustomize/internal/k8sdeps!k8s.io/kubernetes/pkg/kubectl/kustomize/k8sdeps!'
's!sigs.k8s.io/kustomize/k8sdeps!k8s.io/cli-runtime/pkg/kustomize/k8sdeps!'
# Add kustomize command to kubectl
cat > $GOPATH/kubectl.diff << EOF
diff --git a/pkg/kubectl/cmd/cmd.go b/pkg/kubectl/cmd/cmd.go
index 43a541ecc9..2d23bfd27d 100644
--- a/pkg/kubectl/cmd/cmd.go
+++ b/pkg/kubectl/cmd/cmd.go
@@ -74,6 +74,8 @@ import (
"k8s.io/kubernetes/pkg/kubectl/util/templates"
"k8s.io/cli-runtime/pkg/genericclioptions"
+ "k8s.io/kubernetes/pkg/kubectl/kustomize/k8sdeps"
+ "sigs.k8s.io/kustomize/pkg/commands"
)
const (
@@ -505,6 +507,7 @@ func NewKubectlCommand(in io.Reader, out, err io.Writer) *cobra.Command {
replace.NewCmdReplace(f, ioStreams),
wait.NewCmdWait(f, ioStreams),
convert.NewCmdConvert(f, ioStreams),
+ templates.NormalizeAll(commands.NewDefaultCommand(k8sdeps.NewFactory())),
},
},
{
EOF
cp $DIR/vendor_kustomize.diff $KPATH/vendor_kustomize.diff
cd $GOPATH/src/k8s.io/kubernetes
git apply --ignore-space-change --ignore-whitespace $GOPATH/kubectl.diff
git apply --ignore-space-change --ignore-whitespace $KPATH/vendor_kustomize.diff
}
function godepSave {
# Save all dependencies into k8s.io/kubernetes/vendor by running
# hack/godep-save.sh
./hack/godep-save.sh
hack/run-in-gopath.sh hack/godep-save.sh
}
function verify {
# make sure in k8s.io/kubernetes/vendor/sigs.k8s.io/kustomize
# there is no internal package
test 0 == $(ls $GOPATH/src/k8s.io/kubernetes/vendor/sigs.k8s.io/kustomize | grep “internal” | wc -l)
test 0 == $(ls $KPATH/src/k8s.io/kubernetes/vendor/sigs.k8s.io/kustomize | grep “internal” | wc -l)
# Make sure it compiles.
test 0 == $(bazel build cmd/kubectl:kubectl)
@@ -130,10 +109,6 @@ function verify {
echo "The change for vendoring kustomize is ready in $GOPATH/src/k8s.io/kubernetes.\n Next step, open a PR for it.\n"
}
function updateDocs {
./hack/update-generated-docs.sh
}
setUpWorkspace
cloneK8s
godepRestore
@@ -141,4 +116,3 @@ getKustomizeDeps
updateK8s
godepSave
verify
updateDocs

View File

@@ -150,7 +150,7 @@ Here's an [example](kustomization.yaml).
A kustomization contains fields falling into these categories:
* _Customization operators_ for modifying operands, e.g.
_namePrefix_, _commonLabels_, _patches_, etc.
_namePrefix_, _nameSuffix_, _commonLabels_, _patches_, etc.
* _Customization operands_:
* [resources] - completely specified k8s API objects,

View File

@@ -40,6 +40,13 @@ namespace: my-namespace
# "wordpress" becomes "alices-wordpress".
namePrefix: alices-
# Value of this field is appended to the
# names of all resources, e.g. a deployment named
# "wordpress" becomes "wordpress-v2".
# The suffix is appended before content hash
# if resource type is ConfigMap or Secret.
nameSuffix: -v2
# Labels to add to all resources and selectors.
commonLabels:
someName: someValue
@@ -102,6 +109,24 @@ secretGenerator:
envCommand: printf \"DB_USERNAME=admin\nDB_PASSWORD=somepw\"
type: Opaque
# generatorOptions modify behavior of all ConfigMap and Secret generators
generatorOptions:
# labels to add to all generated resources
labels:
kustomize.generated.resources: somevalue
# annotations to add to all generated resources
annotations:
kustomize.generated.resource: somevalue
# timeoutSeconds specifies the timeout for commands
timeoutSeconds: 30
# shell and arguments to use as a context for commands used in resource
# generation. Default at time of writing: ["sh", "-c"]
shell: ["sh", "-c"]
# disableNameSuffixHash is true disables the default behavior of adding a
# suffix to the names of generated resources that is a hash of
# the resource contents.
disableNameSuffixHash: true
# Each entry in this list should resolve to a directory
# containing a kustomization file, else the
# customization fails.
@@ -187,9 +212,9 @@ patchesJson6902:
# transformation for any objects in those types.
#
# Typical use case: A CRD object refers to a ConfigMap object.
# In kustomization, the ConfigMap object name may change by adding namePrefix or hashing
# In kustomization, the ConfigMap object name may change by adding namePrefix, nameSuffix, or hashing
# The name reference for this ConfigMap object in CRD object need to be
# updated with namePrefix or hashing in the same way.
# updated with namePrefix, nameSuffix, or hashing in the same way.
crds:
- crds/typeA.yaml
- crds/typeB.yaml
@@ -216,7 +241,7 @@ crds:
# the image field of container number 2 inside of a pod can currently not be done.
#
# Not every location of a variable is supported. To see a complete list of locations
# see the file [refvars.go](https://github.com/kubernetes-sigs/kustomize/blob/master/pkg/transformers/refvars.go#L20).
# see the file [varreference.go](https://github.com/kubernetes-sigs/kustomize/blob/master/pkg/transformers/config/defaultconfig/varreference.go#L20).
#
# An example of a situation where you'd not use vars is when you'd like to set a
# pod's `serviceAccountName`. In that case you would just reference the ServiceAccount

View File

@@ -29,11 +29,13 @@ go get sigs.k8s.io/kustomize
* [configGenerations](configGeneration.md) -
Rolling update when ConfigMapGenerator changes
* [generatorOptions](generatorOptions.md) - Modifying behavior of all ConfigMap and Secret generators.
* [breakfast](breakfast.md) - Customize breakfast for
Alice and Bob.
* [container args](wordpress/README.md) - Injecting k8s runtime data into container arguments (e.g. to point wordpress to a SQL service).
* [vars](wordpress/README.md) - Injecting k8s runtime data into container arguments (e.g. to point wordpress to a SQL service) by vars.
* [image tags](imageTags.md) - Updating image tags without applying a patch.

View File

@@ -194,6 +194,7 @@ cat <<EOF >$OVERLAYS/development/kustomization.yaml
bases:
- ../../base
namePrefix: dev-
nameSuffix: -v1
configMapGenerator:
- name: my-configmap
behavior: merge
@@ -215,11 +216,12 @@ kustomize build $OVERLAYS/development
The name of the generated `ConfigMap` is visible in this
output.
The name should be something like `dev-my-configmap-b5m75ck895`:
The name should be something like `dev-my-configmap-v1-2gccmccgd5`:
* `"dev-"` comes from the `namePrefix` field,
* `"my-configmap"` comes from the `configMapGenerator/name` field,
* `"-b5m75ck895"` comes from a deterministic hash that `kustomize`
* `"-v1"` comes from the `nameSuffix` field,
* `"-2gccmccgd5"` comes from a deterministic hash that `kustomize`
computes from the contents of the configMap.
The hash suffix is critical. If the configMap content
@@ -291,7 +293,7 @@ kustomize build $OVERLAYS/production
```
A CICD process could apply this directly to
the cluser using:
the cluster using:
> ```
> kustomize build $OVERLAYS/production | kubectl apply -f -

View File

@@ -60,6 +60,7 @@ mkdir -p $OVERLAYS/staging
cat <<'EOF' >$OVERLAYS/staging/kustomization.yaml
namePrefix: staging-
nameSuffix: -v1
commonLabels:
variant: staging
org: acmeCorporation
@@ -150,13 +151,17 @@ The configMap name is prefixed by _staging-_, per the
`namePrefix` field in
`$OVERLAYS/staging/kustomization.yaml`.
The configMap name is suffixed by _-v1_, per the
`nameSuffix` field in
`$OVERLAYS/staging/kustomization.yaml`.
The suffix to the configMap name is generated from a
hash of the maps content - in this case the name suffix
is _hhhhkfmgmk_:
is _k25m8k5k5m_:
<!-- @grepStagingHash @test -->
```
kustomize build $OVERLAYS/staging | grep hhhhkfmgmk
kustomize build $OVERLAYS/staging | grep k25m8k5k5m
```
Now modify the map patch, to change the greeting
@@ -183,20 +188,20 @@ kustomize build $OVERLAYS/staging |\
```
Confirm that the change in configMap content resulted
in three new names ending in _khk45ktkd9_ - one in the
in three new names ending in _cd7kdh48fd_ - one in the
configMap name itself, and two in the deployment that
uses the map:
<!-- @countHashes @test -->
```
test 3 == \
$(kustomize build $OVERLAYS/staging | grep khk45ktkd9 | wc -l); \
$(kustomize build $OVERLAYS/staging | grep cd7kdh48fd | wc -l); \
echo $?
```
Applying these resources to the cluster will result in
a rolling update of the deployments pods, retargetting
them from the _hhhhkfmgmk_ maps to the _khk45ktkd9_
them from the _k25m8k5k5m_ maps to the _cd7kdh48fd_
maps. The system will later garbage collect the
unused maps.

View File

@@ -0,0 +1,62 @@
# Generator Options
Kustomize provides options to modify the behavior of ConfigMap and Secret generators. These options include
- disable appending a content hash suffix to the names of generated resources
- adding labels to generated resources
- adding annotations to generated resources
- changing shell and arguments for getting data from commands
- changing timeout for executing commands
This demo shows how to use these options. First create a workspace.
```
DEMO_HOME=$(mkdir -d)
```
Create a kustomization and add a ConfigMap generator to it.
<!-- @createCMGenerator @test -->
```
cat > $DEMO_HOME/kustomization.yaml << EOF
configMapGenerator:
- name: my-configmap
literals:
- foo=bar
- baz=qux
EOF
```
Add following generatorOptions
<!-- @addGeneratorOptions @test -->
```
cat >> $DEMO_HOME/kustomization.yaml << EOF
generatorOptions:
disableNameSuffixHash: true
labels:
kustomize.generated.resource: somevalue
annotations:
annotations.only.for.generated: othervalue
EOF
```
Run `kustomize build` and make sure that the generated ConfigMap
- doesn't have name suffix
<!-- @verify @test -->
```
test 1 == \
$(kustomize build $DEMO_HOME | grep "name: my-configmap$" | wc -l); \
echo $?
```
- has label `kustomize.generated.resource: somevalue`
```
test 1 == \
$(kustomize build $DEMO_HOME | grep -A 1 "labels" | grep "kustomize.generated.resource" | wc -l); \
echo $?
```
- has annotation `annotations.only.for.generated: othervalue`
```
test 1 == \
$(kustomize build $DEMO_HOME | grep -A 1 "annotations" | grep "annotations.only.for.generated" | wc -l); \
echo $?
```

View File

@@ -2,7 +2,7 @@
Kustomize computes the resources by applying a series of transformers:
- namespace transformer
- prefix transformer
- prefix/suffix transformer
- label transformer
- annotation transformer
- name reference transformer
@@ -22,8 +22,8 @@ create: false
```
If `create` is set to true, it indicates the transformer to create the path if it is not found in the resources. This is most useful for label and annotation transformers, where the path for labels or annotations may not be set before the transformation.
## prefix transformer
Name prefix transformer adds prefix to the `metadata/name` field for all resources with following configuration:
## prefix/suffix transformer
Name prefix suffix transformer adds prefix and suffix to the `metadata/name` field for all resources with following configuration:
```
namePrefix:
- path: metadata/name
@@ -89,8 +89,8 @@ nameReference:
## cusotmizing transformer configurations
Kustomize has a default set of configurations. They can be saved to local directory through `kustomize config save -d`. kusotmize allows modifying those configuration files and using them in `kustomize build` through `-t`. This tutorial shows how to customize those configurations to
- [support a crd type](crd/README.md)
Kustomize has a default set of configurations. They can be saved to local directory through `kustomize config save -d`. Kustomize allows modifying those configuration files and using them in kustomization.yaml file. This tutorial shows how to customize those configurations to
- [support a CRD type](crd/README.md)
- disabling adding commonLabels to fields in some kind of resources
- add extra fields for variable substitution
- add extra fields for name reference

View File

@@ -1,39 +1,57 @@
## Transformer Configurations - CRD
## Supporting Custom Resources (defined by a CRD)
This tutorial shows how to add transformer configurations to support a CRD type.
This tutorial shows how to add transformer configurations to support a custom resource.
### Get Default Config
Get the default transformer configurations by
Create a workspace by
<!-- @createws @test -->
```
DEMO_HOME=$(mktemp -d)
```
### Get the native config as a starting point
Get the default transformer configurations using this command:
<!-- @saveConfig @test -->
```
kustomize config save -d ~/.kustomize/config
kustomize config save -d $DEMO_HOME/kustomizeconfig
```
The default configurations are save in directory `~/.kustomize/config` as several files
The default configurations are saved
in the directory `$DEMO_HOME/kusotmizeconfig` as several files
> ```
> commonannotations.yaml commonlabels.yaml nameprefix.yaml namereference.yaml namespace.yaml varreference.yaml
> commonannotations.yaml
> commonlabels.yaml
> nameprefix.yaml
> namereference.yaml
> namespace.yaml
> varreference.yaml
> ```
### Add Config for a CRD
All transformers will be involved for a CRD type. The default configurations already include some common fieldSpec for all types:
These files contain the field specifications for native resources
that transformation directives like `namePrefix`, `commonLabels`, etc.
need to do their work.
These default configurations already include some common
field specifictions for all types:
- nameprefix is added to `.metadata.name`
- namespace is added to `.metadata.namespace`
- labels is added to `.metadata.labels`
- annotations is added to `.metadata.annotations`
Thus those fieldSpec don't need to be added to support a CRD type.
Consider a CRD type `MyKind` with fields
### Adding a custom resource
Consider a CRD of kind `MyKind` with fields
- `.spec.secretRef.name` reference a Secret
- `.spec.beeRef.name` reference an instance of CRD `Bee`
- `.spec.containers.command` as the list of container commands
- `.spec.selectors` as the label selectors
Add following file to configure the transformers for the above fields
Add the following file to configure the transformers for the above fields
<!-- @addConfig @test -->
```
cat > ~/.kustomize/config/mykind.yaml << EOF
cat > $DEMO_HOME/kustomizeconfig/mykind.yaml << EOF
commonLabels:
- path: spec/selectors
@@ -60,31 +78,12 @@ EOF
```
### Apply config
Create a kustomization with a `MyKind` instance.
<!-- @createKustomization @test -->
Create a file with some resources that
includes an instance of `MyKind`:
<!-- @createResource @test -->
```
DEMO_HOME=$(mktemp -d)
cat > $DEMO_HOME/kustomization.yaml << EOF
resources:
- resources.yaml
namePrefix: test-
commonLabels:
foo: bar
vars:
- name: BEE_ACTION
objref:
kind: Bee
name: bee
apiVersion: v1beta1
fieldref:
fieldpath: spec.action
EOF
cat > $DEMO_HOME/resources.yaml << EOF
apiVersion: v1
kind: Secret
@@ -118,54 +117,60 @@ spec:
EOF
```
Run `kustomize build` with customized transformer configurations and verify that
the namereference is correctly resolved.
Create a kustomization referring to it:
<!-- @createKustomization @test -->
```
cat > $DEMO_HOME/kustomization.yaml << EOF
resources:
- resources.yaml
namePrefix: test-
commonLabels:
foo: bar
vars:
- name: BEE_ACTION
objref:
kind: Bee
name: bee
apiVersion: v1beta1
fieldref:
fieldpath: spec.action
EOF
```
Use the customized transformer configurations by specifying them
in the kustomization file:
<!-- @addTransformerConfigs @test -->
```
cat >> $DEMO_HOME/kustomization.yaml << EOF
configurations:
- kustomizeconfig/mykind.yaml
- kustomizeconfig/commonannotations.yaml
- kustomizeconfig/commonlabels.yaml
- kustomizeconfig/nameprefix.yaml
- kustomizeconfig/namereference.yaml
- kustomizeconfig/namespace.yaml
- kustomizeconfig/varreference.yaml
EOF
```
Run `kustomize build` and verify that the namereference is correctly resolved.
<!-- @build @test -->
```
test 2 == \
$(kustomize build $DEMO_HOME -t ~/.kustomize/config | grep -A 2 ".*Ref" | grep "test-" | wc -l); \
$(kustomize build $DEMO_HOME | grep -A 2 ".*Ref" | grep "test-" | wc -l); \
echo $?
```
Run `kustomize build` with customized transformer configurations and verify that
the vars correctly resolved.
Run `kustomize build` and verify that the vars correctly resolved.
<!-- @verify @test -->
```
test 0 == \
$(kustomize build $DEMO_HOME -t ~/.kustomize/config | grep "BEE_ACTION" | wc -l); \
$(kustomize build $DEMO_HOME | grep "BEE_ACTION" | wc -l); \
echo $?
```
To understand this better, compare the output using default transformer configurations.
<!-- @compareOutput -->
```
diff \
<(kustomize build $DEMO_HOME) \
<(kustomize build $DEMO_HOME -t ~/.kustomize/config ) |\
more
```
The difference output should look something like
> ```
> 20,21c20,21
> < action: $(BEE_ACTION)
> < name: bee
> ---
> > action: fly
> > name: test-bee
> 25c25
> < - $(BEE_ACTION)
> ---
> > - fly
> 28c28,30
> < name: crdsecret
> ---
> > name: test-crdsecret
> > selectors:
> > foo: bar
> ```

View File

@@ -1,6 +1,6 @@
# Demo: Injecting k8s runtime data into containers
In this tutorial, you will learn how to use `kustomize` to declare a variable reference and substitute it in container's command.
In this tutorial, you will learn how to use `kustomize` to declare a variable reference and substitute it in container's command. Note that, the substitution is not for arbitrary fields, it is only applicable to container env, args and command.
To run WordPress, it's necessary to

View File

@@ -129,9 +129,9 @@ func TestConstructConfigMap(t *testing.T) {
}
fSys := fs.MakeFakeFS()
fSys.WriteFile("configmap/app.env", []byte("DB_USERNAME=admin\nDB_PASSWORD=somepw\n"))
fSys.WriteFile("configmap/app-init.ini", []byte("FOO=bar\nBAR=baz\n"))
f := NewConfigMapFactory(fSys, loader.NewFileLoader(fSys))
fSys.WriteFile("/configmap/app.env", []byte("DB_USERNAME=admin\nDB_PASSWORD=somepw\n"))
fSys.WriteFile("/configmap/app-init.ini", []byte("FOO=bar\nBAR=baz\n"))
f := NewConfigMapFactory(fSys, loader.NewFileLoaderAtRoot(fSys))
for _, tc := range testCases {
cm, err := f.MakeConfigMap(&tc.input, tc.options)
if err != nil {

View File

@@ -18,9 +18,9 @@ limitations under the License.
package k8sdeps
import (
"sigs.k8s.io/kustomize/internal/k8sdeps/kunstruct"
"sigs.k8s.io/kustomize/internal/k8sdeps/transformer"
"sigs.k8s.io/kustomize/internal/k8sdeps/validator"
"sigs.k8s.io/kustomize/k8sdeps/kunstruct"
"sigs.k8s.io/kustomize/k8sdeps/transformer"
"sigs.k8s.io/kustomize/k8sdeps/validator"
"sigs.k8s.io/kustomize/pkg/factory"
)

View File

@@ -18,32 +18,33 @@ package kunstruct
import (
"bytes"
"fmt"
"io"
"strings"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"k8s.io/apimachinery/pkg/util/yaml"
"sigs.k8s.io/kustomize/internal/k8sdeps/configmapandsecret"
"sigs.k8s.io/kustomize/k8sdeps/configmapandsecret"
"sigs.k8s.io/kustomize/pkg/fs"
"sigs.k8s.io/kustomize/pkg/ifc"
"sigs.k8s.io/kustomize/pkg/types"
)
// KunstructurerFactoryImpl hides construction using apimachinery types.
type KunstructurerFactoryImpl struct {
cmfactory *configmapandsecret.ConfigMapFactory
secfactory *configmapandsecret.SecretFactory
// KunstructuredFactoryImpl hides construction using apimachinery types.
type KunstructuredFactoryImpl struct {
cmFactory *configmapandsecret.ConfigMapFactory
secretFactory *configmapandsecret.SecretFactory
}
var _ ifc.KunstructuredFactory = &KunstructurerFactoryImpl{}
var _ ifc.KunstructuredFactory = &KunstructuredFactoryImpl{}
// NewKunstructuredFactoryImpl returns a factory.
func NewKunstructuredFactoryImpl() ifc.KunstructuredFactory {
return &KunstructurerFactoryImpl{}
return &KunstructuredFactoryImpl{}
}
// SliceFromBytes returns a slice of Kunstructured.
func (kf *KunstructurerFactoryImpl) SliceFromBytes(
func (kf *KunstructuredFactoryImpl) SliceFromBytes(
in []byte) ([]ifc.Kunstructured, error) {
decoder := yaml.NewYAMLOrJSONDecoder(bytes.NewReader(in), 1024)
var result []ifc.Kunstructured
@@ -52,6 +53,10 @@ func (kf *KunstructurerFactoryImpl) SliceFromBytes(
var out unstructured.Unstructured
err = decoder.Decode(&out)
if err == nil {
err = kf.validate(out)
if err != nil {
return nil, err
}
result = append(result, &UnstructAdapter{Unstructured: out})
}
}
@@ -66,14 +71,14 @@ func isEmptyYamlError(err error) bool {
}
// FromMap returns an instance of Kunstructured.
func (kf *KunstructurerFactoryImpl) FromMap(
func (kf *KunstructuredFactoryImpl) FromMap(
m map[string]interface{}) ifc.Kunstructured {
return &UnstructAdapter{Unstructured: unstructured.Unstructured{Object: m}}
}
// MakeConfigMap returns an instance of Kunstructured for ConfigMap
func (kf *KunstructurerFactoryImpl) MakeConfigMap(args *types.ConfigMapArgs, options *types.GeneratorOptions) (ifc.Kunstructured, error) {
cm, err := kf.cmfactory.MakeConfigMap(args, options)
func (kf *KunstructuredFactoryImpl) MakeConfigMap(args *types.ConfigMapArgs, options *types.GeneratorOptions) (ifc.Kunstructured, error) {
cm, err := kf.cmFactory.MakeConfigMap(args, options)
if err != nil {
return nil, err
}
@@ -81,8 +86,8 @@ func (kf *KunstructurerFactoryImpl) MakeConfigMap(args *types.ConfigMapArgs, opt
}
// MakeSecret returns an instance of Kunstructured for Secret
func (kf *KunstructurerFactoryImpl) MakeSecret(args *types.SecretArgs, options *types.GeneratorOptions) (ifc.Kunstructured, error) {
sec, err := kf.secfactory.MakeSecret(args, options)
func (kf *KunstructuredFactoryImpl) MakeSecret(args *types.SecretArgs, options *types.GeneratorOptions) (ifc.Kunstructured, error) {
sec, err := kf.secretFactory.MakeSecret(args, options)
if err != nil {
return nil, err
}
@@ -90,7 +95,18 @@ func (kf *KunstructurerFactoryImpl) MakeSecret(args *types.SecretArgs, options *
}
// Set sets loader, filesystem and workdirectory
func (kf *KunstructurerFactoryImpl) Set(fs fs.FileSystem, ldr ifc.Loader) {
kf.cmfactory = configmapandsecret.NewConfigMapFactory(fs, ldr)
kf.secfactory = configmapandsecret.NewSecretFactory(fs, ldr.Root())
func (kf *KunstructuredFactoryImpl) Set(fs fs.FileSystem, ldr ifc.Loader) {
kf.cmFactory = configmapandsecret.NewConfigMapFactory(fs, ldr)
kf.secretFactory = configmapandsecret.NewSecretFactory(fs, ldr.Root())
}
// validate validates that u has kind and name
func (kf *KunstructuredFactoryImpl) validate(u unstructured.Unstructured) error {
if u.GetName() == "" {
return fmt.Errorf("Missing metadata.name in object %v", u)
}
if u.GetKind() == "" {
return fmt.Errorf("Missing kind in object %v", u)
}
return nil
}

View File

@@ -100,6 +100,18 @@ WOOOOOOOOOOOOOOOOOOOOOOOOT: woot
expectedOut: []ifc.Kunstructured{},
expectedErr: true,
},
{
name: "Missing .metadata.name in object",
input: []byte(`
apiVersion: v1
kind: Namespace
metadata:
annotations:
foo: bar
`),
expectedOut: nil,
expectedErr: true,
},
}
for _, test := range tests {

View File

@@ -0,0 +1,71 @@
/*
Copyright 2018 The Kubernetes Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
// Package kunstruct provides unstructured from api machinery and factory for creating unstructured
package kunstruct
import (
"fmt"
"strings"
)
func parseFields(path string) ([]string, error) {
if !strings.Contains(path, "[") {
return strings.Split(path, "."), nil
}
var fields []string
start := 0
insideParentheses := false
for i := range path {
switch path[i] {
case '.':
if !insideParentheses {
fields = append(fields, path[start:i])
start = i + 1
}
case '[':
if !insideParentheses {
if i == start {
start = i + 1
} else {
fields = append(fields, path[start:i])
start = i + 1
}
insideParentheses = true
} else {
return nil, fmt.Errorf("nested parentheses are not allowed: %s", path)
}
case ']':
if insideParentheses {
fields = append(fields, path[start:i])
start = i + 1
insideParentheses = false
} else {
return nil, fmt.Errorf("Invalid field path %s", path)
}
}
}
if start < len(path)-1 {
fields = append(fields, path[start:])
}
for i, f := range fields {
if strings.HasPrefix(f, "\"") || strings.HasPrefix(f, "'") {
fields[i] = strings.Trim(f, "\"'")
}
}
return fields, nil
}

View File

@@ -20,7 +20,6 @@ package kunstruct
import (
"encoding/json"
"fmt"
"strings"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
@@ -80,8 +79,12 @@ func (fs *UnstructAdapter) SetMap(m map[string]interface{}) {
// GetFieldValue returns value at the given fieldpath.
func (fs *UnstructAdapter) GetFieldValue(path string) (string, error) {
fields, err := parseFields(path)
if err != nil {
return "", err
}
s, found, err := unstructured.NestedString(
fs.UnstructuredContent(), strings.Split(path, ".")...)
fs.UnstructuredContent(), fields...)
if found || err != nil {
return s, err
}

View File

@@ -18,8 +18,8 @@ limitations under the License.
package transformer
import (
"sigs.k8s.io/kustomize/internal/k8sdeps/transformer/hash"
"sigs.k8s.io/kustomize/internal/k8sdeps/transformer/patch"
"sigs.k8s.io/kustomize/k8sdeps/transformer/hash"
"sigs.k8s.io/kustomize/k8sdeps/transformer/patch"
"sigs.k8s.io/kustomize/pkg/resource"
"sigs.k8s.io/kustomize/pkg/transformers"
)

View File

@@ -20,7 +20,6 @@ import (
"fmt"
"sigs.k8s.io/kustomize/pkg/resmap"
"sigs.k8s.io/kustomize/pkg/resource"
"sigs.k8s.io/kustomize/pkg/transformers"
)
@@ -33,25 +32,16 @@ func NewNameHashTransformer() transformers.Transformer {
return &nameHashTransformer{}
}
// Transform appends hash to configmaps and secrets.
// Transform appends hash to generated resources.
func (o *nameHashTransformer) Transform(m resmap.ResMap) error {
for _, res := range m {
if res.IsGenerated() {
err := o.appendHash(res)
h, err := NewKustHash().Hash(res.Map())
if err != nil {
return err
}
res.SetName(fmt.Sprintf("%s-%s", res.GetName(), h))
}
}
return nil
}
func (o *nameHashTransformer) appendHash(res *resource.Resource) error {
h, err := NewKustHash().Hash(res.Map())
if err != nil {
return err
}
nameWithHash := fmt.Sprintf("%s-%s", res.GetName(), h)
res.SetName(nameWithHash)
return nil
}

View File

@@ -20,7 +20,7 @@ import (
"reflect"
"testing"
"sigs.k8s.io/kustomize/internal/k8sdeps/kunstruct"
"sigs.k8s.io/kustomize/k8sdeps/kunstruct"
"sigs.k8s.io/kustomize/pkg/ifc"
"sigs.k8s.io/kustomize/pkg/resid"
"sigs.k8s.io/kustomize/pkg/resmap"

View File

@@ -21,7 +21,7 @@ import (
"strings"
"testing"
"sigs.k8s.io/kustomize/internal/k8sdeps/kunstruct"
"sigs.k8s.io/kustomize/k8sdeps/kunstruct"
"sigs.k8s.io/kustomize/pkg/gvk"
"sigs.k8s.io/kustomize/pkg/resid"
"sigs.k8s.io/kustomize/pkg/resmap"

View File

@@ -19,14 +19,11 @@ package main
import (
"os"
"github.com/golang/glog"
"sigs.k8s.io/kustomize/internal/k8sdeps"
"sigs.k8s.io/kustomize/k8sdeps"
"sigs.k8s.io/kustomize/pkg/commands"
)
func main() {
defer glog.Flush()
if err := commands.NewDefaultCommand(k8sdeps.NewFactory()).Execute(); err != nil {
os.Exit(1)
}

View File

@@ -18,7 +18,6 @@ package build
import (
"io"
"strings"
"github.com/pkg/errors"
"github.com/spf13/cobra"
@@ -28,13 +27,11 @@ import (
"sigs.k8s.io/kustomize/pkg/loader"
"sigs.k8s.io/kustomize/pkg/resmap"
"sigs.k8s.io/kustomize/pkg/target"
"sigs.k8s.io/kustomize/pkg/transformers/config"
)
type buildOptions struct {
kustomizationPath string
outputPath string
transformerconfigPaths []string
kustomizationPath string
outputPath string
}
var examples = `
@@ -50,11 +47,6 @@ url examples:
sigs.k8s.io/kustomize//examples/multibases?ref=v1.0.6
github.com/Liujingfang1/mysql
github.com/Liujingfang1/kustomize//examples/helloWorld?ref=repoUrl2
Advanced usage:
Use different transformer configurations by passing files to kustomize
build somedir -t someconfigdir
build somedir -t some-transformer-configfile,another-transformer-configfile
`
// NewCmdBuild creates a new build command.
@@ -63,7 +55,6 @@ func NewCmdBuild(
rf *resmap.Factory,
ptf transformer.Factory) *cobra.Command {
var o buildOptions
var p string
cmd := &cobra.Command{
Use: "build [path]",
@@ -71,7 +62,7 @@ func NewCmdBuild(
Example: examples,
SilenceUsage: true,
RunE: func(cmd *cobra.Command, args []string) error {
err := o.Validate(args, p, fs)
err := o.Validate(args)
if err != nil {
return err
}
@@ -82,36 +73,18 @@ func NewCmdBuild(
&o.outputPath,
"output", "o", "",
"If specified, write the build output to this path.")
cmd.Flags().StringVarP(
&p,
"transformer-config", "t", "",
"If specified, use the transformer configs load from these files.")
return cmd
}
// Validate validates build command.
func (o *buildOptions) Validate(args []string, p string, fs fs.FileSystem) error {
func (o *buildOptions) Validate(args []string) error {
if len(args) > 1 {
return errors.New("specify one path to " + constants.KustomizationFileName)
}
if len(args) == 0 {
o.kustomizationPath = "./"
return nil
}
o.kustomizationPath = args[0]
if p == "" {
return nil
}
if fs.IsDir(p) {
paths, err := fs.Glob(p + "/*")
if err != nil {
return err
}
o.transformerconfigPaths = paths
} else {
o.transformerconfigPaths = strings.Split(p, ",")
o.kustomizationPath = args[0]
}
return nil
@@ -120,21 +93,13 @@ func (o *buildOptions) Validate(args []string, p string, fs fs.FileSystem) error
// RunBuild runs build command.
func (o *buildOptions) RunBuild(
out io.Writer, fSys fs.FileSystem,
rf *resmap.Factory,
ptf transformer.Factory) error {
rootLoader, err := loader.NewLoader(o.kustomizationPath, "", fSys)
rf *resmap.Factory, ptf transformer.Factory) error {
ldr, err := loader.NewLoader(o.kustomizationPath, fSys)
if err != nil {
return err
}
tc, err := makeTransformerconfig(fSys, o.transformerconfigPaths)
if err != nil {
return err
}
defer rootLoader.Cleanup()
kt, err := target.NewKustTarget(
rootLoader, fSys,
rf,
ptf, tc)
defer ldr.Cleanup()
kt, err := target.NewKustTarget(ldr, fSys, rf, ptf)
if err != nil {
return err
}
@@ -153,18 +118,3 @@ func (o *buildOptions) RunBuild(
_, err = out.Write(res)
return err
}
// makeTransformerConfig returns a complete TransformerConfig object from either files
// or the default configs
func makeTransformerconfig(
fSys fs.FileSystem, paths []string) (*config.TransformerConfig, error) {
if paths == nil || len(paths) == 0 {
return config.NewFactory(nil).DefaultConfig(), nil
}
ldr, err := loader.NewLoader(".", "", fSys)
if err != nil {
return nil, errors.Wrap(
err, "cannot create transformer configuration loader")
}
return config.NewFactory(ldr).FromFiles(paths)
}

View File

@@ -26,7 +26,7 @@ import (
"testing"
"github.com/ghodss/yaml"
"sigs.k8s.io/kustomize/internal/k8sdeps"
"sigs.k8s.io/kustomize/k8sdeps"
"sigs.k8s.io/kustomize/pkg/commands/kustfile"
"sigs.k8s.io/kustomize/pkg/constants"
"sigs.k8s.io/kustomize/pkg/fs"
@@ -56,7 +56,7 @@ func TestBuildValidate(t *testing.T) {
}
for _, mycase := range cases {
opts := buildOptions{}
e := opts.Validate(mycase.args, "", nil)
e := opts.Validate(mycase.args)
if len(mycase.erMsg) > 0 {
if e == nil {
t.Errorf("%s: Expected an error %v", mycase.name, mycase.erMsg)

View File

@@ -2,3 +2,4 @@ resources:
- serviceaccount.yaml
- rolebinding.yaml
namePrefix: base-
nameSuffix: -suffix

View File

@@ -2,6 +2,7 @@ bases:
- ../../base/
namePrefix: a-
nameSuffix: -suffixA
resources:
- serviceaccount.yaml

View File

@@ -2,3 +2,4 @@ bases:
- ../../base/
namePrefix: b-
nameSuffix: -suffixB

View File

@@ -1,4 +1,4 @@
description: multibases with name reference
args: []
filename: testdata/testcase-multibases-conflict/combined
expectedError: detected conflicts when resolving name references serviceaccount
expectedError: Multiple matches for name noGroup_v1_ServiceAccount

View File

@@ -2,3 +2,4 @@ resources:
- serviceaccount.yaml
- rolebinding.yaml
namePrefix: base-
nameSuffix: -suffix

View File

@@ -1,33 +1,33 @@
apiVersion: v1
kind: ServiceAccount
metadata:
name: a-base-serviceaccount
name: a-base-serviceaccount-suffix-suffixA
---
apiVersion: v1
kind: ServiceAccount
metadata:
name: b-base-serviceaccount
name: b-base-serviceaccount-suffix-suffixB
---
apiVersion: rbac.authorization.k8s.io/v1beta1
kind: RoleBinding
metadata:
name: a-base-rolebinding
name: a-base-rolebinding-suffix-suffixA
roleRef:
apiGroup: rbac.authorization.k8s.io
kind: Role
name: role
subjects:
- kind: ServiceAccount
name: a-base-serviceaccount
name: a-base-serviceaccount-suffix-suffixA
---
apiVersion: rbac.authorization.k8s.io/v1beta1
kind: RoleBinding
metadata:
name: b-base-rolebinding
name: b-base-rolebinding-suffix-suffixB
roleRef:
apiGroup: rbac.authorization.k8s.io
kind: Role
name: role
subjects:
- kind: ServiceAccount
name: b-base-serviceaccount
name: b-base-serviceaccount-suffix-suffixB

View File

@@ -2,3 +2,4 @@ bases:
- ../../base/
namePrefix: a-
nameSuffix: -suffixA

View File

@@ -2,3 +2,4 @@ bases:
- ../../base/
namePrefix: b-
nameSuffix: -suffixB

View File

@@ -77,16 +77,10 @@ metadata:
apiVersion: v1
kind: Service
metadata:
annotations:
prometheus.io/path: _status/vars
prometheus.io/port: "8080"
prometheus.io/scrape: "true"
service.alpha.kubernetes.io/tolerate-unready-endpoints: "true"
labels:
app: cockroachdb
name: dev-base-cockroachdb
name: dev-base-cockroachdb-public
spec:
clusterIP: None
ports:
- name: grpc
port: 26257
@@ -100,10 +94,16 @@ spec:
apiVersion: v1
kind: Service
metadata:
annotations:
prometheus.io/path: _status/vars
prometheus.io/port: "8080"
prometheus.io/scrape: "true"
service.alpha.kubernetes.io/tolerate-unready-endpoints: "true"
labels:
app: cockroachdb
name: dev-base-cockroachdb-public
name: dev-base-cockroachdb
spec:
clusterIP: None
ports:
- name: grpc
port: 26257

View File

@@ -67,7 +67,7 @@ func newCmdAddConfigMap(fSys fs.FileSystem, kf ifc.KunstructuredFactory) *cobra.
}
// Add the flagsAndArgs map to the kustomization file.
kf.Set(fSys, loader.NewFileLoader(fSys))
kf.Set(fSys, loader.NewFileLoaderAtCwd(fSys))
err = addConfigMap(kustomization, flagsAndArgs, kf)
if err != nil {
return err

View File

@@ -36,6 +36,9 @@ func NewCmdEdit(fsys fs.FileSystem, v ifc.Validator, kf ifc.KunstructuredFactory
# Sets the nameprefix field
kustomize edit set nameprefix <prefix-value>
# Sets the namesuffix field
kustomize edit set namesuffix <suffix-value>
`,
Args: cobra.MinimumNArgs(1),
}

View File

@@ -31,12 +31,16 @@ func NewCmdSet(fsys fs.FileSystem, v ifc.Validator) *cobra.Command {
Example: `
# Sets the nameprefix field
kustomize edit set nameprefix <prefix-value>
# Sets the namesuffix field
kustomize edit set namesuffix <suffix-value>
`,
Args: cobra.MinimumNArgs(1),
}
c.AddCommand(
newCmdSetNamePrefix(fsys),
newCmdSetNameSuffix(fsys),
newCmdSetNamespace(fsys, v),
newCmdSetImageTag(fsys),
)

View File

@@ -0,0 +1,86 @@
/*
Copyright 2018 The Kubernetes Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package set
import (
"errors"
"github.com/spf13/cobra"
"sigs.k8s.io/kustomize/pkg/commands/kustfile"
"sigs.k8s.io/kustomize/pkg/fs"
)
type setNameSuffixOptions struct {
suffix string
}
// newCmdSetNameSuffix sets the value of the nameSuffix field in the kustomization.
func newCmdSetNameSuffix(fsys fs.FileSystem) *cobra.Command {
var o setNameSuffixOptions
cmd := &cobra.Command{
Use: "namesuffix",
Short: "Sets the value of the nameSuffix field in the kustomization file.",
Example: `
The command
set namesuffix -- -acme
will add the field "nameSuffix: -acme" to the kustomization file if it doesn't exist,
and overwrite the value with "-acme" if the field does exist.
`,
RunE: func(cmd *cobra.Command, args []string) error {
err := o.Validate(args)
if err != nil {
return err
}
err = o.Complete(cmd, args)
if err != nil {
return err
}
return o.RunSetNameSuffix(fsys)
},
}
return cmd
}
// Validate validates setNameSuffix command.
func (o *setNameSuffixOptions) Validate(args []string) error {
if len(args) != 1 {
return errors.New("must specify exactly one suffix value")
}
// TODO: add further validation on the value.
o.suffix = args[0]
return nil
}
// Complete completes setNameSuffix command.
func (o *setNameSuffixOptions) Complete(cmd *cobra.Command, args []string) error {
return nil
}
// RunSetNameSuffix runs setNameSuffix command (does real work).
func (o *setNameSuffixOptions) RunSetNameSuffix(fSys fs.FileSystem) error {
mf, err := kustfile.NewKustomizationFile(fSys)
if err != nil {
return err
}
m, err := mf.Read()
if err != nil {
return err
}
m.NameSuffix = o.suffix
return mf.Write(m)
}

View File

@@ -0,0 +1,60 @@
/*
Copyright 2018 The Kubernetes Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package set
import (
"strings"
"testing"
"sigs.k8s.io/kustomize/pkg/fs"
)
const (
goodSuffixValue = "-acme"
)
func TestSetNameSuffixHappyPath(t *testing.T) {
fakeFS := fs.MakeFakeFS()
fakeFS.WriteTestKustomization()
cmd := newCmdSetNameSuffix(fakeFS)
args := []string{goodSuffixValue}
err := cmd.RunE(cmd, args)
if err != nil {
t.Errorf("unexpected cmd error: %v", err)
}
content, err := fakeFS.ReadTestKustomization()
if err != nil {
t.Errorf("unexpected read error: %v", err)
}
if !strings.Contains(string(content), goodSuffixValue) {
t.Errorf("expected suffix value in kustomization file")
}
}
func TestSetNameSuffixNoArgs(t *testing.T) {
fakeFS := fs.MakeFakeFS()
cmd := newCmdSetNameSuffix(fakeFS)
err := cmd.Execute()
if err == nil {
t.Errorf("expected error: %v", err)
}
if err.Error() != "must specify exactly one suffix value" {
t.Errorf("incorrect error: %v", err.Error())
}
}

View File

@@ -22,7 +22,6 @@ import (
"fmt"
"io"
"log"
"path"
"reflect"
"regexp"
"strings"
@@ -55,6 +54,7 @@ func determineFieldOrder() []string {
"Resources",
"Bases",
"NamePrefix",
"NameSuffix",
"Namespace",
"Crds",
"CommonLabels",
@@ -67,6 +67,7 @@ func determineFieldOrder() []string {
"GeneratorOptions",
"Vars",
"ImageTags",
"Configurations",
}
// Add deprecated fields here.
@@ -118,7 +119,7 @@ type kustomizationFile struct {
// NewKustomizationFile returns a new instance.
func NewKustomizationFile(fSys fs.FileSystem) (*kustomizationFile, error) { // nolint
mf := &kustomizationFile{path: constants.KustomizationFileName, fSys: fSys}
mf := &kustomizationFile{fSys: fSys}
err := mf.validate()
if err != nil {
return nil, err
@@ -127,19 +128,16 @@ func NewKustomizationFile(fSys fs.FileSystem) (*kustomizationFile, error) { // n
}
func (mf *kustomizationFile) validate() error {
if !mf.fSys.Exists(mf.path) {
return fmt.Errorf("Missing kustomization file '%s'.\n", mf.path)
}
if mf.fSys.IsDir(mf.path) {
mf.path = path.Join(mf.path, constants.KustomizationFileName)
if !mf.fSys.Exists(mf.path) {
return fmt.Errorf("Missing kustomization file '%s'.\n", mf.path)
}
if mf.fSys.Exists(constants.KustomizationFileName) {
mf.path = constants.KustomizationFileName
} else if mf.fSys.Exists(constants.SecondaryKustomizationFileName) {
mf.path = constants.SecondaryKustomizationFileName
} else {
if !strings.HasSuffix(mf.path, constants.KustomizationFileName) {
return fmt.Errorf("Kustomization file path (%s) should have %s suffix\n",
mf.path, constants.KustomizationFileSuffix)
}
return fmt.Errorf("Missing kustomization file '%s'.\n", constants.KustomizationFileName)
}
if mf.fSys.IsDir(mf.path) {
return fmt.Errorf("%s should be a file", mf.path)
}
return nil
}

View File

@@ -21,6 +21,7 @@ import (
"strings"
"testing"
"sigs.k8s.io/kustomize/pkg/constants"
"sigs.k8s.io/kustomize/pkg/fs"
"sigs.k8s.io/kustomize/pkg/types"
)
@@ -32,6 +33,7 @@ func TestFieldOrder(t *testing.T) {
"Resources",
"Bases",
"NamePrefix",
"NameSuffix",
"Namespace",
"Crds",
"CommonLabels",
@@ -43,6 +45,7 @@ func TestFieldOrder(t *testing.T) {
"GeneratorOptions",
"Vars",
"ImageTags",
"Configurations",
}
actual := determineFieldOrder()
if len(expected) != len(actual) {
@@ -84,6 +87,7 @@ func TestWriteAndRead(t *testing.T) {
func TestDeprecationOfPatches(t *testing.T) {
hasDeprecatedFields := []byte(`
namePrefix: acme
nameSuffix: emca
patches:
- alice
patchesStrategicMerge:
@@ -102,6 +106,9 @@ patchesStrategicMerge:
if k.NamePrefix != "acme" {
t.Fatalf("Unexpected name prefix")
}
if k.NameSuffix != "emca" {
t.Fatalf("Unexpected name suffix")
}
if len(k.Patches) > 0 {
t.Fatalf("Expected nothing in Patches.")
}
@@ -141,6 +148,25 @@ func TestNewNotExist(t *testing.T) {
}
}
func TestSecondarySuffix(t *testing.T) {
kcontent := `
configMapGenerator:
- literals:
- foo=bar
- baz=qux
name: my-configmap
`
fakeFS := fs.MakeFakeFS()
fakeFS.WriteFile(constants.SecondaryKustomizationFileName, []byte(kcontent))
k, err := NewKustomizationFile(fakeFS)
if err != nil {
t.Fatalf("Unexpected Error: %v", err)
}
if k.path != constants.SecondaryKustomizationFileName {
t.Fatalf("Load incorrect file path %s", k.path)
}
}
func TestPreserveComments(t *testing.T) {
kustomizationContentWithComments := []byte(
`# shem qing some comments
@@ -221,7 +247,7 @@ patchesStrategicMerge:
- pod.yaml
# generator options
generatorOptions:
disableHash: true
disableNameSuffixHash: true
`)
expected := []byte(`
@@ -259,7 +285,7 @@ patchesStrategicMerge:
- pod.yaml
# generator options
generatorOptions:
disableHash: true
disableNameSuffixHash: true
`)
fSys := fs.MakeFakeFS()
fSys.WriteTestKustomizationWith(kustomizationContentWithComments)

View File

@@ -20,5 +20,12 @@ package constants
// KustomizationFileSuffix is expected suffix for KustomizationFileName.
const KustomizationFileSuffix = ".yaml"
// SecondaryKustomizationFileSuffix is the second expected suffix when KustomizationFileSuffix is not found
const SecondaryKustomizationFileSuffix = ".yml"
// KustomizationFileName is the Well-Known File Name for a kustomize configuration file.
const KustomizationFileName = "kustomization" + KustomizationFileSuffix
// SecondaryKustomizationFileName is the secondary File Name for a kustomize configuration file when
// KustomizationFileName is not found
const SecondaryKustomizationFileName = "kustomization" + SecondaryKustomizationFileSuffix

View File

@@ -19,24 +19,29 @@ package fs
import (
"fmt"
"path/filepath"
"sigs.k8s.io/kustomize/pkg/constants"
"sort"
"strings"
"sigs.k8s.io/kustomize/pkg/constants"
)
var _ FileSystem = &FakeFS{}
var _ FileSystem = &fakeFs{}
// FakeFS implements FileSystem using a fake in-memory filesystem.
type FakeFS struct {
// fakeFs implements FileSystem using a fake in-memory filesystem.
type fakeFs struct {
m map[string]*FakeFile
}
// MakeFakeFS returns an instance of FakeFS with no files in it.
func MakeFakeFS() *FakeFS {
return &FakeFS{m: map[string]*FakeFile{}}
// MakeFakeFS returns an instance of fakeFs with no files in it.
func MakeFakeFS() *fakeFs {
result := &fakeFs{m: map[string]*FakeFile{}}
result.Mkdir("/")
return result
}
// kustomizationContent is used in tests.
const kustomizationContent = `namePrefix: some-prefix
nameSuffix: some-suffix
# Labels to add to all objects and selectors.
# These labels would also be used to form the selector for apply --prune
# Named differently than “labels” to avoid confusion with metadata for this object
@@ -54,7 +59,7 @@ secretGenerator: []
`
// Create assures a fake file appears in the in-memory file system.
func (fs *FakeFS) Create(name string) (File, error) {
func (fs *fakeFs) Create(name string) (File, error) {
f := &FakeFile{}
f.open = true
fs.m[name] = f
@@ -62,18 +67,33 @@ func (fs *FakeFS) Create(name string) (File, error) {
}
// Mkdir assures a fake directory appears in the in-memory file system.
func (fs *FakeFS) Mkdir(name string) error {
func (fs *fakeFs) Mkdir(name string) error {
fs.m[name] = makeDir(name)
return nil
}
// MkdirAll delegates to Mkdir
func (fs *FakeFS) MkdirAll(name string) error {
func (fs *fakeFs) MkdirAll(name string) error {
return fs.Mkdir(name)
}
// RemoveAll presumably does rm -r on a path.
// There's no error.
func (fs *fakeFs) RemoveAll(name string) error {
var toRemove []string
for k := range fs.m {
if strings.HasPrefix(k, name) {
toRemove = append(toRemove, k)
}
}
for _, k := range toRemove {
delete(fs.m, k)
}
return nil
}
// Open returns a fake file in the open state.
func (fs *FakeFS) Open(name string) (File, error) {
func (fs *fakeFs) Open(name string) (File, error) {
if _, found := fs.m[name]; !found {
return nil, fmt.Errorf("file %q cannot be opened", name)
}
@@ -81,13 +101,13 @@ func (fs *FakeFS) Open(name string) (File, error) {
}
// Exists returns true if file is known.
func (fs *FakeFS) Exists(name string) bool {
func (fs *fakeFs) Exists(name string) bool {
_, found := fs.m[name]
return found
}
// Glob returns the list of matching files
func (fs *FakeFS) Glob(pattern string) ([]string, error) {
func (fs *fakeFs) Glob(pattern string) ([]string, error) {
var result []string
for p := range fs.m {
if fs.pathMatch(p, pattern) {
@@ -99,28 +119,36 @@ func (fs *FakeFS) Glob(pattern string) ([]string, error) {
}
// IsDir returns true if the file exists and is a directory.
func (fs *FakeFS) IsDir(name string) bool {
func (fs *fakeFs) IsDir(name string) bool {
f, found := fs.m[name]
if !found {
return false
if found && f.dir {
return true
}
return f.dir
if !strings.HasSuffix(name, "/") {
name = name + "/"
}
for k := range fs.m {
if strings.HasPrefix(k, name) {
return true
}
}
return false
}
// ReadFile always returns an empty bytes and error depending on content of m.
func (fs *FakeFS) ReadFile(name string) ([]byte, error) {
func (fs *fakeFs) ReadFile(name string) ([]byte, error) {
if ff, found := fs.m[name]; found {
return ff.content, nil
}
return nil, fmt.Errorf("cannot read file %q", name)
}
func (fs *FakeFS) ReadTestKustomization() ([]byte, error) {
func (fs *fakeFs) ReadTestKustomization() ([]byte, error) {
return fs.ReadFile(constants.KustomizationFileName)
}
// WriteFile always succeeds and does nothing.
func (fs *FakeFS) WriteFile(name string, c []byte) error {
func (fs *fakeFs) WriteFile(name string, c []byte) error {
ff := &FakeFile{}
ff.Write(c)
fs.m[name] = ff
@@ -128,16 +156,16 @@ func (fs *FakeFS) WriteFile(name string, c []byte) error {
}
// WriteTestKustomization writes a standard test file.
func (fs *FakeFS) WriteTestKustomization() {
func (fs *fakeFs) WriteTestKustomization() {
fs.WriteTestKustomizationWith([]byte(kustomizationContent))
}
// WriteTestKustomizationWith writes a standard test file.
func (fs *FakeFS) WriteTestKustomizationWith(bytes []byte) {
func (fs *fakeFs) WriteTestKustomizationWith(bytes []byte) {
fs.WriteFile(constants.KustomizationFileName, bytes)
}
func (fs *FakeFS) pathMatch(path, pattern string) bool {
func (fs *fakeFs) pathMatch(path, pattern string) bool {
match, _ := filepath.Match(pattern, path)
return match
}

View File

@@ -27,6 +27,10 @@ func TestExists(t *testing.T) {
if x.Exists("foo") {
t.Fatalf("expected no foo")
}
x.Mkdir("/")
if !x.IsDir("/") {
t.Fatalf("expected dir at /")
}
}
func TestIsDir(t *testing.T) {
@@ -36,14 +40,62 @@ func TestIsDir(t *testing.T) {
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
if !x.Exists(expectedName) {
t.Fatalf(expectedName + " should exist")
}
shouldExist(t, x, expectedName)
if !x.IsDir(expectedName) {
t.Fatalf(expectedName + " should be a dir")
}
}
func shouldExist(t *testing.T, fs FileSystem, name string) {
if !fs.Exists(name) {
t.Fatalf(name + " should exist")
}
}
func shouldNotExist(t *testing.T, fs FileSystem, name string) {
if fs.Exists(name) {
t.Fatalf(name + " should not exist")
}
}
func TestRemoveAll(t *testing.T) {
x := MakeFakeFS()
x.WriteFile("/foo/project/file.yaml", []byte("Unused"))
x.WriteFile("/foo/project/subdir/file.yaml", []byte("Unused"))
x.WriteFile("/foo/apple/subdir/file.yaml", []byte("Unused"))
shouldExist(t, x, "/foo/project/file.yaml")
shouldExist(t, x, "/foo/project/subdir/file.yaml")
shouldExist(t, x, "/foo/apple/subdir/file.yaml")
err := x.RemoveAll("/foo/project")
if err != nil {
t.Fatalf("Unexpected error: %v", err)
}
shouldNotExist(t, x, "/foo/project/file.yaml")
shouldNotExist(t, x, "/foo/project/subdir/file.yaml")
shouldExist(t, x, "/foo/apple/subdir/file.yaml")
}
func TestIsDirDeeper(t *testing.T) {
x := MakeFakeFS()
x.WriteFile("/foo/project/file.yaml", []byte("Unused"))
x.WriteFile("/foo/project/subdir/file.yaml", []byte("Unused"))
if !x.IsDir("/") {
t.Fatalf("/ should be a dir")
}
if !x.IsDir("/foo") {
t.Fatalf("/foo should be a dir")
}
if !x.IsDir("/foo/project") {
t.Fatalf("/foo/project should be a dir")
}
if x.IsDir("/fo") {
t.Fatalf("/fo should not be a dir")
}
if x.IsDir("/x") {
t.Fatalf("/x should not be a dir")
}
}
func TestCreate(t *testing.T) {
x := MakeFakeFS()
f, err := x.Create("foo")
@@ -53,9 +105,7 @@ func TestCreate(t *testing.T) {
if err != nil {
t.Fatalf("unexpected error")
}
if !x.Exists("foo") {
t.Fatalf("expected foo to exist")
}
shouldExist(t, x, "foo")
}
func TestReadFile(t *testing.T) {

View File

@@ -27,6 +27,7 @@ type FileSystem interface {
Create(name string) (File, error)
Mkdir(name string) error
MkdirAll(name string) error
RemoveAll(name string) error
Open(name string) (File, error)
IsDir(name string) bool
Exists(name string) bool

View File

@@ -45,6 +45,11 @@ func (realFS) MkdirAll(name string) error {
return os.MkdirAll(name, 0777|os.ModeDir)
}
// RemoveAll delegates to os.RemoveAll.
func (realFS) RemoveAll(name string) error {
return os.RemoveAll(name)
}
// Open delegates to os.Open.
func (realFS) Open(name string) (File, error) { return os.Open(name) }

View File

@@ -70,6 +70,7 @@ func (x Gvk) Equals(o Gvk) bool {
// In some cases order just specified to provide determinism.
var order = []string{
"Namespace",
"StorageClass",
"CustomResourceDefinition",
"ServiceAccount",
"Role",
@@ -97,7 +98,9 @@ func (x Gvk) IsLessThan(o Gvk) bool {
indexI, foundI := typeOrders[x.Kind]
indexJ, foundJ := typeOrders[o.Kind]
if foundI && foundJ {
return indexI < indexJ
if indexI != indexJ {
return indexI < indexJ
}
}
if foundI && !foundJ {
return true

View File

@@ -48,6 +48,16 @@ var lessThanTests = []struct {
Gvk{Group: "a", Version: "b", Kind: "ClusterRole"}},
{Gvk{Group: "a", Version: "b", Kind: "a"},
Gvk{Group: "a", Version: "b", Kind: "b"}},
{Gvk{Group: "a", Version: "b", Kind: "Namespace"},
Gvk{Group: "a", Version: "c", Kind: "Namespace"}},
{Gvk{Group: "a", Version: "c", Kind: "Namespace"},
Gvk{Group: "b", Version: "c", Kind: "Namespace"}},
{Gvk{Group: "b", Version: "c", Kind: "Namespace"},
Gvk{Group: "a", Version: "c", Kind: "ClusterRole"}},
{Gvk{Group: "a", Version: "c", Kind: "Namespace"},
Gvk{Group: "a", Version: "b", Kind: "ClusterRole"}},
{Gvk{Group: "a", Version: "d", Kind: "Namespace"},
Gvk{Group: "b", Version: "c", Kind: "Namespace"}},
}
func TestIsLessThan1(t *testing.T) {
@@ -55,6 +65,9 @@ func TestIsLessThan1(t *testing.T) {
if !hey.x1.IsLessThan(hey.x2) {
t.Fatalf("%v should be less than %v", hey.x1, hey.x2)
}
if hey.x2.IsLessThan(hey.x1) {
t.Fatalf("%v should not be less than %v", hey.x2, hey.x1)
}
}
}

View File

@@ -18,6 +18,7 @@ limitations under the License.
package loadertest
import (
"log"
"sigs.k8s.io/kustomize/pkg/fs"
"sigs.k8s.io/kustomize/pkg/ifc"
"sigs.k8s.io/kustomize/pkg/loader"
@@ -29,15 +30,17 @@ type FakeLoader struct {
delegate ifc.Loader
}
// NewFakeLoader returns a Loader that delegates calls, and encapsulates
// a fake file system that the Loader reads from. "initialDir" parameter
// must be an full, absolute directory (trailing slash doesn't matter).
// NewFakeLoader returns a Loader that uses a fake filesystem.
// The argument should be an absolute file path.
func NewFakeLoader(initialDir string) FakeLoader {
// Create fake filesystem and inject it into initial Loader.
fakefs := fs.MakeFakeFS()
rootLoader := loader.NewFileLoader(fakefs)
ldr, _ := rootLoader.New(initialDir)
return FakeLoader{fs: fakefs, delegate: ldr}
fSys := fs.MakeFakeFS()
fSys.Mkdir(initialDir)
ldr, err := loader.NewLoader(initialDir, fSys)
if err != nil {
log.Fatalf("Unable to make loader: %v", err)
}
return FakeLoader{fs: fSys, delegate: ldr}
}
// AddFile adds a fake file to the file system.

View File

@@ -18,93 +18,207 @@ package loader
import (
"fmt"
"os"
"log"
"path/filepath"
"strings"
"sigs.k8s.io/kustomize/pkg/fs"
"sigs.k8s.io/kustomize/pkg/ifc"
)
const currentDir = "."
// TODO: 2018/Nov/20 remove this before next release.
// Leave only the true path. Retaining only for
// quick revert.
const enforceRelocatable = true
// fileLoader loads files from a file system.
// fileLoader loads files, returning an array of bytes.
// It also enforces two kustomization requirements:
//
// 1) relocatable
//
// A kustomization and the resources, bases,
// patches, etc. that it depends on should be
// relocatable, so all path specifications
// must be relative, not absolute. The paths
// are taken relative to the location of the
// kusttomization file.
//
// 2) acyclic
//
// There should be no cycles in overlay to base
// relationships, including no cycles between
// git repositories.
//
// The loader has a notion of a current working directory
// (CWD), called 'root', that is independent of the CWD
// of the process. When `Load` is called with a file path
// argument, the load is done relative to this root,
// not relative to the process CWD.
//
// The loader offers a `New` method returning a new loader
// with a new root. The new root can be one of two things,
// a remote git repo URL, or a directory specified relative
// to the current root. In the former case, the remote
// repository is locally cloned, and the new loader is
// rooted on a path in that clone.
//
// Crucially, a root history is used to so that New fails
// if its argument either matches or is a parent of the
// current or any previously used root.
//
// This disallows:
//
// * A base that is a git repository that, in turn,
// specifies a base repository seen previously
// in the loading process (a cycle).
//
// * An overlay depending on a base positioned at or
// above it. I.e. '../foo' is OK, but '.', '..',
// '../..', etc. are disallowed. Allowing such a
// base has no advantages and encourage cycles,
// particularly if some future change were to
// introduce globbing to file specifications in
// the kustomization file.
//
type fileLoader struct {
root string
// Previously visited directories, tracked to
// avoid cycles. The last entry is the current root.
roots []string
// File system utilities.
fSys fs.FileSystem
// Used to clone repositories.
cloner gitCloner
// Used to clean up, as needed.
cleaner func() error
}
// NewFileLoader returns a new fileLoader.
func NewFileLoader(fSys fs.FileSystem) *fileLoader {
return newFileLoaderAtRoot("", fSys)
// NewFileLoaderAtCwd returns a loader that loads from ".".
func NewFileLoaderAtCwd(fSys fs.FileSystem) *fileLoader {
return newLoaderOrDie(fSys, ".")
}
// newFileLoaderAtRoot returns a new fileLoader with given root.
func newFileLoaderAtRoot(root string, fs fs.FileSystem) *fileLoader {
return &fileLoader{root: root, fSys: fs}
// NewFileLoaderAtRoot returns a loader that loads from "/".
func NewFileLoaderAtRoot(fSys fs.FileSystem) *fileLoader {
return newLoaderOrDie(fSys, "/")
}
// Root returns the root location for this Loader.
// Root returns the absolute path that is prepended to any
// relative paths used in Load.
func (l *fileLoader) Root() string {
return l.root
return l.roots[len(l.roots)-1]
}
// Returns a new Loader rooted at newRoot. "newRoot" MUST be
// a directory (not a file). The directory can have a trailing
// slash or not.
// Example: "/home/seans/project" or "/home/seans/project/"
// NOT "/home/seans/project/file.yaml".
func (l *fileLoader) New(newRoot string) (ifc.Loader, error) {
return NewLoader(newRoot, l.root, l.fSys)
}
// IsAbsPath return true if the location calculated with the root
// and location params a full file path.
func (l *fileLoader) IsAbsPath(root string, location string) bool {
fullFilePath, err := l.fullLocation(root, location)
func newLoaderOrDie(fSys fs.FileSystem, path string) *fileLoader {
l, err := newFileLoaderAt(
path, fSys, []string{}, simpleGitCloner)
if err != nil {
return false
log.Fatalf("unable to make loader at '%s'; %v", path, err)
}
return filepath.IsAbs(fullFilePath)
return l
}
// fullLocation returns some notion of a full path.
// If location is a full file path, then ignore root. If location is relative, then
// join the root path with the location path. Either root or location can be empty,
// but not both. Special case for ".": Expands to current working directory.
// Example: "/home/seans/project", "subdir/bar" -> "/home/seans/project/subdir/bar".
func (l *fileLoader) fullLocation(root string, location string) (string, error) {
// First, validate the parameters
if len(root) == 0 && len(location) == 0 {
return "", fmt.Errorf("unable to calculate full location: root and location empty")
// newFileLoaderAt returns a new fileLoader with given root.
func newFileLoaderAt(
root string, fSys fs.FileSystem,
roots []string, cloner gitCloner) (*fileLoader, error) {
if root == "" {
return nil, fmt.Errorf(
"loader root cannot be empty")
}
// Special case current directory, expanding to full file path.
if location == currentDir {
currentDir, err := os.Getwd()
if err != nil {
return "", err
root, err := filepath.Abs(root)
if err != nil {
return nil, fmt.Errorf(
"absolute path error in '%s' : %v", root, err)
}
if !fSys.IsDir(root) {
return nil, fmt.Errorf("absolute root dir '%s' does not exist", root)
}
return &fileLoader{
roots: append(roots, root),
fSys: fSys,
cloner: cloner,
cleaner: func() error { return nil },
}, nil
}
// New returns a new Loader, rooted relative to current loader,
// or rooted in a temp directory holding a git repo clone.
func (l *fileLoader) New(root string) (ifc.Loader, error) {
if root == "" {
return nil, fmt.Errorf("new root cannot be empty")
}
if isRepoUrl(root) {
if err := l.seenBefore(root); err != nil {
return nil, err
}
location = currentDir
return newGitLoader(root, l.fSys, l.roots, l.cloner)
}
// Assume the location is a full file path. If not, then join root with location.
fullLocation := location
if !filepath.IsAbs(location) {
fullLocation = filepath.Join(root, location)
if enforceRelocatable && filepath.IsAbs(root) {
return nil, fmt.Errorf("new root '%s' cannot be absolute", root)
}
return fullLocation, nil
}
// Load returns the bytes from reading a file at fullFilePath.
// Implements the Loader interface.
func (l *fileLoader) Load(location string) ([]byte, error) {
fullLocation, err := l.fullLocation(l.root, location)
// Get absolute path to squeeze out "..", ".", etc.
// to facilitate the seenBefore test.
absRoot, err := filepath.Abs(filepath.Join(l.Root(), root))
if err != nil {
fmt.Printf("Trouble in fulllocation: %v\n", err)
return nil, fmt.Errorf(
"problem joining '%s' to '%s': %v", l.Root(), root, err)
}
if err := l.seenBefore(absRoot); err != nil {
return nil, err
}
return l.fSys.ReadFile(fullLocation)
return newFileLoaderAt(absRoot, l.fSys, l.roots, l.cloner)
}
// Cleanup does nothing
func (l *fileLoader) Cleanup() error {
// newGitLoader returns a new Loader pinned to a temporary
// directory holding a cloned git repo.
func newGitLoader(
root string, fSys fs.FileSystem,
roots []string, cloner gitCloner) (ifc.Loader, error) {
tmpDirForRepo, pathInRepo, err := cloner(root)
if err != nil {
return nil, err
}
trueRoot := filepath.Join(tmpDirForRepo, pathInRepo)
if !fSys.IsDir(trueRoot) {
return nil, fmt.Errorf(
"something wrong cloning '%s'; unable to find '%s'",
root, trueRoot)
}
return &fileLoader{
roots: append(roots, root, trueRoot),
fSys: fSys,
cloner: cloner,
cleaner: func() error { return fSys.RemoveAll(tmpDirForRepo) },
}, nil
}
// seenBefore tests whether the current or any previously
// visited root begins with the given path.
func (l *fileLoader) seenBefore(path string) error {
for _, r := range l.roots {
if strings.HasPrefix(r, path) {
return fmt.Errorf(
"cycle detected: new root '%s' contains previous root '%s'",
path, r)
}
}
return nil
}
// Load returns content of file at the given relative path.
func (l *fileLoader) Load(path string) ([]byte, error) {
if filepath.IsAbs(path) {
if enforceRelocatable {
return nil, fmt.Errorf(
"must use relative path; '%s' is absolute", path)
}
} else {
path = filepath.Join(l.Root(), path)
}
return l.fSys.ReadFile(path)
}
// Cleanup runs the cleaner.
func (l *fileLoader) Cleanup() error {
return l.cleaner()
}

View File

@@ -17,102 +17,184 @@ limitations under the License.
package loader
import (
"path/filepath"
"reflect"
"strings"
"testing"
"sigs.k8s.io/kustomize/pkg/fs"
)
func TestLoader_Root(t *testing.T) {
type testData struct {
path string
expectedContent string
}
// Initialize the fake file system and the root loader.
fakefs := fs.MakeFakeFS()
fakefs.WriteFile("/home/seans/project/file.yaml", []byte("Unused"))
fakefs.WriteFile("/home/seans/project/subdir/file.yaml", []byte("Unused"))
fakefs.WriteFile("/home/seans/project2/file.yaml", []byte("Unused"))
rootLoader := NewFileLoader(fakefs)
var testCases = []testData{
{
path: "foo/project/fileA.yaml",
expectedContent: "fileA content",
},
{
path: "foo/project/subdir1/fileB.yaml",
expectedContent: "fileB content",
},
{
path: "foo/project/subdir2/fileC.yaml",
expectedContent: "fileC content",
},
{
path: "foo/project/fileD.yaml",
expectedContent: "fileD content",
},
}
_, err := rootLoader.New("")
func MakeFakeFs(td []testData) fs.FileSystem {
fSys := fs.MakeFakeFS()
for _, x := range td {
fSys.WriteFile("/"+x.path, []byte(x.expectedContent))
}
return fSys
}
func TestLoaderLoad(t *testing.T) {
l1 := NewFileLoaderAtRoot(MakeFakeFs(testCases))
if "/" != l1.Root() {
t.Fatalf("incorrect root: '%s'\n", l1.Root())
}
for _, x := range testCases {
b, err := l1.Load(x.path)
if err != nil {
t.Fatalf("unexpected load error %v", err)
}
if !reflect.DeepEqual([]byte(x.expectedContent), b) {
t.Fatalf("in load expected %s, but got %s", x.expectedContent, b)
}
}
l2, err := l1.New("foo/project")
if err != nil {
t.Fatalf("unexpected err: %v\n", err)
}
if "/foo/project" != l2.Root() {
t.Fatalf("incorrect root: %s\n", l2.Root())
}
for _, x := range testCases {
b, err := l2.Load(strings.TrimPrefix(x.path, "foo/project/"))
if err != nil {
t.Fatalf("unexpected load error %v", err)
}
if !reflect.DeepEqual([]byte(x.expectedContent), b) {
t.Fatalf("in load expected %s, but got %s", x.expectedContent, b)
}
}
l2, err = l1.New("foo/project/") // Assure trailing slash stripped
if err != nil {
t.Fatalf("unexpected err: %v\n", err)
}
if "/foo/project" != l2.Root() {
t.Fatalf("incorrect root: %s\n", l2.Root())
}
}
func TestLoaderNewSubDir(t *testing.T) {
l1, err := NewFileLoaderAtRoot(MakeFakeFs(testCases)).New("foo/project")
if err != nil {
t.Fatalf("unexpected err: %v\n", err)
}
l2, err := l1.New("subdir1")
if err != nil {
t.Fatalf("unexpected err: %v\n", err)
}
if "/foo/project/subdir1" != l2.Root() {
t.Fatalf("incorrect root: %s\n", l2.Root())
}
x := testCases[1]
b, err := l2.Load("fileB.yaml")
if err != nil {
t.Fatalf("unexpected load error %v", err)
}
if !reflect.DeepEqual([]byte(x.expectedContent), b) {
t.Fatalf("in load expected %s, but got %s", x.expectedContent, b)
}
}
func TestLoaderBadRelative(t *testing.T) {
l1, err := NewFileLoaderAtRoot(MakeFakeFs(testCases)).New("foo/project/subdir1")
if err != nil {
t.Fatalf("unexpected err: %v\n", err)
}
if "/foo/project/subdir1" != l1.Root() {
t.Fatalf("incorrect root: %s\n", l1.Root())
}
// Cannot cd into a file.
l2, err := l1.New("fileB.yaml")
if err == nil {
t.Fatalf("expected err, but got root %s", l2.Root())
}
// It's not okay to stay at the same place.
l2, err = l1.New(".")
if err == nil {
t.Fatalf("expected err, but got root %s", l2.Root())
}
// It's not okay to go up and back down into same place.
l2, err = l1.New("../subdir1")
if err == nil {
t.Fatalf("expected err, but got root %s", l2.Root())
}
// It's not okay to go up via a relative path.
l2, err = l1.New("..")
if err == nil {
t.Fatalf("expected err, but got root %s", l2.Root())
}
// It's not okay to go up via an absolute path.
l2, err = l1.New("/foo/project")
if err == nil {
t.Fatalf("expected err, but got root %s", l2.Root())
}
// It's not okay to go to the root.
l2, err = l1.New("/")
if err == nil {
t.Fatalf("expected err, but got root %s", l2.Root())
}
// It's okay to go up and down to a sibling.
l2, err = l1.New("../subdir2")
if err != nil {
t.Fatalf("unexpected new error %v", err)
}
if "/foo/project/subdir2" != l2.Root() {
t.Fatalf("incorrect root: %s\n", l2.Root())
}
x := testCases[2]
b, err := l2.Load("fileC.yaml")
if err != nil {
t.Fatalf("unexpected load error %v", err)
}
if !reflect.DeepEqual([]byte(x.expectedContent), b) {
t.Fatalf("in load expected %s, but got %s", x.expectedContent, b)
}
// It's not OK to go over to a previously visited directory.
// Must disallow going back and forth in a cycle.
l1, err = l2.New("../subdir1")
if err == nil {
t.Fatalf("expected err, but got root %s", l1.Root())
}
}
func TestLoaderMisc(t *testing.T) {
l := NewFileLoaderAtRoot(MakeFakeFs(testCases))
_, err := l.New("")
if err == nil {
t.Fatalf("Expected error for empty root location not returned")
}
_, err = rootLoader.New("https://google.com/project")
_, err = l.New("https://google.com/project")
if err == nil {
t.Fatalf("Expected error")
}
// Test with trailing slash in directory.
loader, err := rootLoader.New("/home/seans/project/")
if err != nil {
t.Fatalf("Unexpected in New(): %v\n", err)
}
if "/home/seans/project" != loader.Root() {
t.Fatalf("Incorrect Loader Root: %s\n", loader.Root())
}
subLoader, err := loader.New("subdir")
if err != nil {
t.Fatalf("Unexpected in New(): %v\n", err)
}
if "/home/seans/project/subdir" != subLoader.Root() {
t.Fatalf("Incorrect Loader Root: %s\n", subLoader.Root())
}
// Test without trailing slash in directory.
anotherLoader, err := loader.New("/home/seans/project2")
if err != nil {
t.Fatalf("Unexpected in New(): %v\n", err)
}
if "/home/seans/project2" != anotherLoader.Root() {
t.Fatalf("Incorrect Loader Root: %s\n", anotherLoader.Root())
}
// Current directory should be expanded to a full absolute file path.
currentDirLoader, err := loader.New(".")
if err != nil {
t.Fatalf("Unexpected in New(): %v\n", err)
}
if !filepath.IsAbs(currentDirLoader.Root()) {
t.Fatalf("Incorrect Loader Root: %s\n", currentDirLoader.Root())
}
}
func TestLoader_Load(t *testing.T) {
// Initialize the fake file system and the root loader.
fakefs := fs.MakeFakeFS()
fakefs.WriteFile("/home/seans/project/file.yaml", []byte("This is a yaml file"))
fakefs.WriteFile("/home/seans/project/subdir/file.yaml", []byte("Subdirectory file content"))
fakefs.WriteFile("/home/seans/project2/file.yaml", []byte("This is another yaml file"))
rootLoader := NewFileLoader(fakefs)
loader, err := rootLoader.New("/home/seans/project")
if err != nil {
t.Fatalf("Unexpected in New(): %v\n", err)
}
fileBytes, err := loader.Load("file.yaml") // Load relative to root location
if err != nil {
t.Fatalf("Unexpected error in Load(): %v", err)
}
if !reflect.DeepEqual([]byte("This is a yaml file"), fileBytes) {
t.Fatalf("Load failed. Expected %s, but got %s", "This is a yaml file", fileBytes)
}
fileBytes, err = loader.Load("subdir/file.yaml")
if err != nil {
t.Fatalf("Unexpected error in Load(): %v", err)
}
if !reflect.DeepEqual([]byte("Subdirectory file content"), fileBytes) {
t.Fatalf("Load failed. Expected %s, but got %s", "Subdirectory file content", fileBytes)
}
fileBytes, err = loader.Load("/home/seans/project2/file.yaml")
if err != nil {
t.Fatalf("Unexpected error in Load(): %v", err)
}
if !reflect.DeepEqual([]byte("This is another yaml file"), fileBytes) {
t.Fatalf("Load failed. Expected %s, but got %s", "This is another yaml file", fileBytes)
}
}

133
pkg/loader/gitcloner.go Normal file
View File

@@ -0,0 +1,133 @@
/*
Copyright 2018 The Kubernetes Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package loader
import (
"bytes"
"io/ioutil"
"os/exec"
"path/filepath"
"strings"
"github.com/pkg/errors"
)
// gitCloner is a function that can clone a git repo.
type gitCloner func(url string) (
// Directory where the repo is cloned to.
checkoutDir string,
// Relative path in the checkoutDir to location
// of kustomization file.
pathInCoDir string,
// Any error encountered when cloning.
err error)
// isRepoUrl checks if a string is likely a github repo Url.
func isRepoUrl(arg string) bool {
arg = strings.ToLower(arg)
return !filepath.IsAbs(arg) &&
(strings.HasPrefix(arg, "git::") ||
strings.HasPrefix(arg, "gh:") ||
strings.HasPrefix(arg, "github.com") ||
strings.HasPrefix(arg, "git@github.com:") ||
strings.Index(arg, "github.com/") > -1)
}
func makeTmpDir() (string, error) {
return ioutil.TempDir("", "kustomize-")
}
func simpleGitCloner(spec string) (
checkoutDir string, pathInCoDir string, err error) {
gitProgram, err := exec.LookPath("git")
if err != nil {
return "", "", errors.Wrap(err, "no 'git' program on path")
}
checkoutDir, err = makeTmpDir()
if err != nil {
return
}
repo, pathInCoDir, gitRef, err := parseGithubUrl(spec)
if err != nil {
return
}
cmd := exec.Command(
gitProgram,
"clone",
"https://github.com/"+repo+".git",
checkoutDir)
var out bytes.Buffer
cmd.Stdout = &out
err = cmd.Run()
if err != nil {
return "", "",
errors.Wrapf(err, "trouble cloning %s", spec)
}
if gitRef == "" {
return
}
cmd = exec.Command(gitProgram, "checkout", gitRef)
cmd.Dir = checkoutDir
err = cmd.Run()
if err != nil {
return "", "",
errors.Wrapf(err, "trouble checking out href %s", gitRef)
}
return checkoutDir, pathInCoDir, nil
}
const refQuery = "?ref="
// From strings like git@github.com:someOrg/someRepo.git or
// https://github.com/someOrg/someRepo?ref=someHash, extract
// the parts.
func parseGithubUrl(n string) (
repo string, path string, gitRef string, err error) {
for _, p := range []string{
// Order matters here.
"git::", "gh:", "https://", "http://",
"git@", "github.com:", "github.com/", "gitlab.com/"} {
if strings.ToLower(n[:len(p)]) == p {
n = n[len(p):]
}
}
if strings.HasSuffix(n, ".git") {
n = n[0 : len(n)-len(".git")]
}
i := strings.Index(n, string(filepath.Separator))
if i < 1 {
return "", "", "", errors.New("no separator")
}
j := strings.Index(n[i+1:], string(filepath.Separator))
if j >= 0 {
j += i + 1
repo = n[:j]
path, gitRef = peelQuery(n[j+1:])
} else {
path = ""
repo, gitRef = peelQuery(n)
}
return
}
func peelQuery(arg string) (string, string) {
j := strings.Index(arg, refQuery)
if j >= 0 {
return arg[:j], arg[j+len(refQuery):]
}
return arg, ""
}

View File

@@ -0,0 +1,241 @@
/*
Copyright 2018 The Kubernetes Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package loader
import (
"fmt"
"path/filepath"
"strings"
"testing"
"sigs.k8s.io/kustomize/pkg/constants"
"sigs.k8s.io/kustomize/pkg/fs"
)
func TestIsRepoURL(t *testing.T) {
testcases := []struct {
input string
expected bool
}{
{
input: "https://github.com/org/repo",
expected: true,
},
{
input: "github.com/org/repo",
expected: true,
},
{
input: "git@github.com:org/repo",
expected: true,
},
{
input: "gh:org/repo",
expected: true,
},
{
input: "git::https://gitlab.com/org/repo",
expected: true,
},
{
input: "/github.com/org/repo",
expected: false,
},
{
input: "/abs/path/to/file",
expected: false,
},
{
input: "../relative",
expected: false,
},
{
input: "foo",
expected: false,
},
{
input: ".",
expected: false,
},
{
input: "",
expected: false,
},
}
for _, tc := range testcases {
actual := isRepoUrl(tc.input)
if actual != tc.expected {
t.Errorf("unexpected error: unexpected result %t for input %s", actual, tc.input)
}
}
}
func splitOnNthSlash(v string, n int) (string, string) {
left := ""
for i := 0; i < n; i++ {
k := strings.Index(v, "/")
if k < 0 {
break
}
left = left + v[:k+1]
v = v[k+1:]
}
return left[:len(left)-1], v
}
func TestSplit(t *testing.T) {
path := "a/b/c/d/e/f/g"
if left, right := splitOnNthSlash(path, 2); left != "a/b" || right != "c/d/e/f/g" {
t.Fatalf("got left='%s', right='%s'", left, right)
}
if left, right := splitOnNthSlash(path, 3); left != "a/b/c" || right != "d/e/f/g" {
t.Fatalf("got left='%s', right='%s'", left, right)
}
if left, right := splitOnNthSlash(path, 6); left != "a/b/c/d/e/f" || right != "g" {
t.Fatalf("got left='%s', right='%s'", left, right)
}
}
// makeFakeGitCloner returns a cloner that ignores the
// URL argument and returns a path in a fake file system
// that should already hold the 'repo' contents.
func makeFakeGitCloner(t *testing.T, fSys fs.FileSystem, coRoot string) gitCloner {
if !fSys.IsDir(coRoot) {
t.Fatalf("expecting a directory at '%s'", coRoot)
}
return func(url string) (
checkoutDir string, pathInCoDir string, err error) {
_, path := splitOnNthSlash(url, 3)
if !fSys.IsDir(coRoot + "/" + path) {
t.Fatalf("expecting a directory at '%s'/'%s'",
coRoot, path)
}
return coRoot, path, nil
}
}
func TestGitLoader(t *testing.T) {
rootUrl := "github.com/someOrg/someRepo"
pathInRepo := "foo/base"
url := rootUrl + "/" + pathInRepo
if !isRepoUrl(url) {
t.Fatalf("'%s' should be accepted as a repo url", url)
}
coRoot := "/tmp"
fSys := fs.MakeFakeFS()
fSys.MkdirAll(coRoot)
fSys.MkdirAll(coRoot + "/" + pathInRepo)
fSys.WriteFile(
coRoot+"/"+pathInRepo+"/"+constants.KustomizationFileName,
[]byte(`
whatever
`))
l, err := newGitLoader(
url, fSys, []string{},
makeFakeGitCloner(t, fSys, coRoot))
if err != nil {
t.Fatalf("unexpected err: %v\n", err)
}
if coRoot+"/"+pathInRepo != l.Root() {
t.Fatalf("expected root '%s', got '%s'\n",
coRoot+"/"+pathInRepo, l.Root())
}
if _, err = l.New(url); err == nil {
t.Fatalf("expected cycle error")
}
if _, err = l.New(rootUrl + "/" + "foo"); err == nil {
t.Fatalf("expected cycle error")
}
pathInRepo = "foo/overlay"
fSys.MkdirAll(coRoot + "/" + pathInRepo)
url = rootUrl + "/" + pathInRepo
if !isRepoUrl(url) {
t.Fatalf("'%s' should be accepted as a repo url", url)
}
l2, err := l.New(url)
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
if coRoot+"/"+pathInRepo != l2.Root() {
t.Fatalf("expected root '%s', got '%s'\n",
coRoot+"/"+pathInRepo, l2.Root())
}
}
var repoNames = []string{"someOrg/someRepo", "kubernetes/website"}
var paths = []string{"README.md", "foo/krusty.txt", ""}
var hrefArgs = []string{"someBranch", ""}
var extractFmts = []string{
"gh:%s",
"GH:%s",
"gitHub.com/%s",
"https://github.com/%s",
"hTTps://github.com/%s",
"git::https://gitlab.com/%s",
"github.com:%s",
}
func TestParseGithubUrl(t *testing.T) {
for _, repoName := range repoNames {
for _, pathName := range paths {
for _, extractFmt := range extractFmts {
for _, hrefArg := range hrefArgs {
spec := repoName
if len(pathName) > 0 {
spec = filepath.Join(spec, pathName)
}
input := fmt.Sprintf(extractFmt, spec)
if hrefArg != "" {
input = input + refQuery + hrefArg
}
if !isRepoUrl(input) {
t.Errorf("Should smell like github arg: %s\n", input)
continue
}
repo, path, gitRef, err := parseGithubUrl(input)
if err != nil {
t.Errorf("problem %v", err)
}
if repo != repoName {
t.Errorf("\n"+
" from %s\n"+
" actual Repo %s\n"+
"expected Repo %s\n", input, repo, repoName)
}
if path != pathName {
t.Errorf("\n"+
" from %s\n"+
" actual Path %s\n"+
"expected Path %s\n", input, path, pathName)
}
if gitRef != hrefArg {
t.Errorf("\n"+
" from %s\n"+
" actual Href %s\n"+
"expected Href %s\n", input, gitRef, hrefArg)
}
}
}
}
}
}

View File

@@ -1,108 +0,0 @@
/*
Copyright 2018 The Kubernetes Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package loader
import (
"io/ioutil"
"os"
"path/filepath"
"sigs.k8s.io/kustomize/pkg/ifc"
"strings"
"github.com/hashicorp/go-getter"
"sigs.k8s.io/kustomize/pkg/fs"
)
// githubLoader loads files from a checkout github repo
type githubLoader struct {
repo string
// targetDir will hold the local repo
targetDir string
// checkoutDir is for the whole repository
checkoutDir string
loader *fileLoader
}
// Root returns the root location for this Loader.
func (l *githubLoader) Root() string {
return l.targetDir
}
// New delegates to fileLoader.New
func (l *githubLoader) New(newRoot string) (ifc.Loader, error) {
return l.loader.New(newRoot)
}
// Load delegates to fileLoader.Load
func (l *githubLoader) Load(location string) ([]byte, error) {
return l.loader.Load(location)
}
// Cleanup removes the checked out repo
func (l *githubLoader) Cleanup() error {
return os.RemoveAll(l.checkoutDir)
}
// newGithubLoader returns a new fileLoader with given github Url.
func newGithubLoader(repoUrl string, fs fs.FileSystem) (*githubLoader, error) {
dir, err := ioutil.TempDir("", "kustomize-")
if err != nil {
return nil, err
}
repodir := filepath.Join(dir, "repo")
src, subdir := getter.SourceDirSubdir(repoUrl)
err = checkout(src, repodir)
if err != nil {
return nil, err
}
target := filepath.Join(repodir, subdir)
l := newFileLoaderAtRoot(target, fs)
return &githubLoader{
repo: repoUrl,
targetDir: target,
checkoutDir: repodir,
loader: l,
}, nil
}
// isRepoUrl checks if a string is a repo Url
func isRepoUrl(s string) bool {
if strings.HasPrefix(s, "https://") {
return true
}
if strings.HasPrefix(s, "git::") {
return true
}
host := strings.SplitN(s, "/", 2)[0]
return strings.Contains(host, ".com") || strings.Contains(host, ".org")
}
// Checkout clones a github repo with specified commit/tag/branch
func checkout(url, dir string) error {
pwd, err := os.Getwd()
if err != nil {
return err
}
client := &getter.Client{
Src: url,
Dst: dir,
Pwd: pwd,
Mode: getter.ClientModeDir,
}
return client.Get()
}

View File

@@ -1,60 +0,0 @@
/*
Copyright 2018 The Kubernetes Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package loader
import (
"testing"
)
func TestIsRepoURL(t *testing.T) {
testcases := []struct {
input string
expected bool
}{
{
input: "https://github.com/org/repo",
expected: true,
},
{
input: "github.com/org/repo",
expected: true,
},
{
input: "/github.com/org/repo",
expected: false,
},
{
input: "/abs/path/to/file",
expected: false,
},
{
input: "../relative",
expected: false,
},
{
input: "git::https://gitlab.com/org/repo",
expected: true,
},
}
for _, tc := range testcases {
actual := isRepoUrl(tc.input)
if actual != tc.expected {
t.Errorf("unexpected error: unexpected result %t for input %s", actual, tc.input)
}
}
}

View File

@@ -18,52 +18,16 @@ limitations under the License.
package loader
import (
"fmt"
"path/filepath"
"sigs.k8s.io/kustomize/pkg/ifc"
"sigs.k8s.io/kustomize/pkg/fs"
"sigs.k8s.io/kustomize/pkg/ifc"
)
// NewLoader returns a Loader given a target
// The target can be a local disk directory or a github Url
func NewLoader(target, r string, fSys fs.FileSystem) (ifc.Loader, error) {
if !isValidLoaderPath(target, r) {
return nil, fmt.Errorf("Not valid path: root='%s', loc='%s'\n", r, target)
// NewLoader returns a Loader.
func NewLoader(root string, fSys fs.FileSystem) (ifc.Loader, error) {
if isRepoUrl(root) {
return newGitLoader(
root, fSys, []string{}, simpleGitCloner)
}
if !isLocalTarget(target, fSys) && isRepoUrl(target) {
return newGithubLoader(target, fSys)
}
l := newFileLoaderAtRoot(r, fSys)
if isRootLoaderPath(r) {
absPath, err := filepath.Abs(target)
if err != nil {
return nil, err
}
target = absPath
}
if !l.IsAbsPath(l.root, target) {
return nil, fmt.Errorf("Not abs path: l.root='%s', loc='%s'\n", l.root, target)
}
root, err := l.fullLocation(l.root, target)
if err != nil {
return nil, err
}
return newFileLoaderAtRoot(root, l.fSys), nil
}
func isValidLoaderPath(target, root string) bool {
return target != "" || root != ""
}
func isRootLoaderPath(root string) bool {
return root == ""
}
// isLocalTarget checks if a file exists in the filesystem
func isLocalTarget(s string, fs fs.FileSystem) bool {
return fs.Exists(s)
return newFileLoaderAt(
root, fSys, []string{}, simpleGitCloner)
}

View File

@@ -25,7 +25,7 @@ type Json6902 struct {
// applied to. It must refer to a Kubernetes resource under the
// purview of this kustomization. Target should use the
// raw name of the object (the name specified in its YAML,
// before addition of a namePrefix).
// before addition of a namePrefix and a nameSuffix).
Target *Target `json:"target" yaml:"target"`
// relative file path for a json patch file inside a kustomization

View File

@@ -22,7 +22,7 @@ import (
"testing"
"gopkg.in/yaml.v2"
"sigs.k8s.io/kustomize/internal/k8sdeps/kunstruct"
"sigs.k8s.io/kustomize/k8sdeps/kunstruct"
"sigs.k8s.io/kustomize/pkg/gvk"
"sigs.k8s.io/kustomize/pkg/internal/loadertest"
"sigs.k8s.io/kustomize/pkg/patch"
@@ -82,7 +82,7 @@ func TestNewPatchJson6902FactoryJSON(t *testing.T) {
target:
kind: Deployment
name: some-name
path: /testpath/patch.json
path: patch.json
`)
p := patch.Json6902{}
err = yaml.Unmarshal(jsonPatch, &p)
@@ -120,7 +120,7 @@ func TestNewPatchJson6902FactoryYAML(t *testing.T) {
target:
name: some-name
kind: Deployment
path: /testpath/patch.yaml
path: patch.yaml
`)
p := patch.Json6902{}
err = yaml.Unmarshal(jsonPatch, &p)
@@ -162,12 +162,12 @@ func TestNewPatchJson6902FactoryMulti(t *testing.T) {
- target:
kind: foo
name: some-name
path: /testpath/patch.json
path: patch.json
- target:
kind: foo
name: some-name
path: /testpath/patch.yaml
path: patch.yaml
`)
var p []patch.Json6902
err = yaml.Unmarshal(jsonPatches, &p)
@@ -276,12 +276,12 @@ func TestNewPatchJson6902FactoryMultiConflict(t *testing.T) {
- target:
kind: foo
name: some-name
path: /testpath/patch.json
path: patch.json
- target:
kind: foo
name: some-name
path: /testpath/patch.yaml
path: patch.yaml
`)
var p []patch.Json6902
err = yaml.Unmarshal(jsonPatches, &p)

View File

@@ -21,7 +21,7 @@ import (
"testing"
"github.com/evanphx/json-patch"
"sigs.k8s.io/kustomize/internal/k8sdeps/kunstruct"
"sigs.k8s.io/kustomize/k8sdeps/kunstruct"
"sigs.k8s.io/kustomize/pkg/gvk"
"sigs.k8s.io/kustomize/pkg/resid"
"sigs.k8s.io/kustomize/pkg/resmap"

View File

@@ -17,6 +17,7 @@ limitations under the License.
package resid
import (
"fmt"
"strings"
"sigs.k8s.io/kustomize/pkg/gvk"
@@ -32,22 +33,46 @@ type ResId struct {
// an untransformed resource has no prefix, fully transformed resource has an arbitrary number of prefixes
// concatenated together.
prefix string
// nameSuffix of the resource
// an untransformed resource has no suffix, fully transformed resource has an arbitrary number of suffixes
// concatenated together.
suffix string
// namespace the resource belongs to
// an untransformed resource has no namespace, fully transformed resource has the namespace from
// the top most overlay
namespace string
}
// NewResIdWithPrefixSuffixNamespace creates new resource identifier with a prefix, suffix and a namespace
func NewResIdWithPrefixSuffixNamespace(k gvk.Gvk, n, p, s, ns string) ResId {
return ResId{gvKind: k, name: n, prefix: p, suffix: s, namespace: ns}
}
// NewResIdWithPrefixNamespace creates new resource identifier with a prefix and a namespace
func NewResIdWithPrefixNamespace(k gvk.Gvk, n, p, ns string) ResId {
return ResId{gvKind: k, name: n, prefix: p, namespace: ns}
}
// NewResIdWithSuffixNamespace creates new resource identifier with a suffix and a namespace
func NewResIdWithSuffixNamespace(k gvk.Gvk, n, s, ns string) ResId {
return ResId{gvKind: k, name: n, suffix: s, namespace: ns}
}
// NewResIdWithPrefixSuffix creates new resource identifier with a prefix and suffix
func NewResIdWithPrefixSuffix(k gvk.Gvk, n, p, s string) ResId {
return ResId{gvKind: k, name: n, prefix: p, suffix: s}
}
// NewResIdWithPrefix creates new resource identifier with a prefix
func NewResIdWithPrefix(k gvk.Gvk, n, p string) ResId {
return ResId{gvKind: k, name: n, prefix: p}
}
// NewResIdWithSuffix creates new resource identifier with a suffix
func NewResIdWithSuffix(k gvk.Gvk, n, s string) ResId {
return ResId{gvKind: k, name: n, suffix: s}
}
// NewResId creates new resource identifier
func NewResId(k gvk.Gvk, n string) ResId {
return ResId{gvKind: k, name: n}
@@ -62,6 +87,7 @@ const (
noNamespace = "noNamespace"
noPrefix = "noPrefix"
noName = "noName"
noSuffix = "noSuffix"
separator = "|"
)
@@ -79,9 +105,13 @@ func (n ResId) String() string {
if nm == "" {
nm = noName
}
s := n.suffix
if s == "" {
s = noSuffix
}
return strings.Join(
[]string{n.gvKind.String(), ns, p, nm}, separator)
[]string{n.gvKind.String(), ns, p, nm, s}, separator)
}
// GvknString of ResId based on GVK and name
@@ -90,7 +120,7 @@ func (n ResId) GvknString() string {
}
// GvknEquals return if two ResId have the same Group/Version/Kind and name
// The comparison excludes prefix
// The comparison excludes prefix and suffix
func (n ResId) GvknEquals(id ResId) bool {
return n.gvKind.Equals(id.gvKind) && n.name == id.name
}
@@ -105,24 +135,44 @@ func (n ResId) Name() string {
return n.name
}
// NameWithPrefixSuffix returns resource name with prefix and suffix.
func (n ResId) NameWithPrefixSuffix() string {
prefix := strings.Join(n.prefixList(), "")
suffix := strings.Join(n.suffixList(), "")
return fmt.Sprintf("%s%s%s", prefix, n.name, suffix)
}
// Prefix returns name prefix.
func (n ResId) Prefix() string {
return n.prefix
}
// Suffix returns name suffix.
func (n ResId) Suffix() string {
return n.suffix
}
// Namespace returns resource namespace.
func (n ResId) Namespace() string {
return n.namespace
}
// CopyWithNewPrefix make a new copy from current ResId and append a new prefix
func (n ResId) CopyWithNewPrefix(p string) ResId {
return ResId{gvKind: n.gvKind, name: n.name, prefix: n.concatPrefix(p), namespace: n.namespace}
// CopyWithNewPrefixSuffix make a new copy from current ResId and append a new prefix and suffix
func (n ResId) CopyWithNewPrefixSuffix(p, s string) ResId {
prefix := n.prefix
if p != "" {
prefix = n.concatPrefix(p)
}
suffix := n.suffix
if s != "" {
suffix = n.concatSuffix(s)
}
return ResId{gvKind: n.gvKind, name: n.name, prefix: prefix, suffix: suffix, namespace: n.namespace}
}
// CopyWithNewNamespace make a new copy from current ResId and set a new namespace
func (n ResId) CopyWithNewNamespace(ns string) ResId {
return ResId{gvKind: n.gvKind, name: n.name, prefix: n.prefix, namespace: ns}
return ResId{gvKind: n.gvKind, name: n.name, prefix: n.prefix, suffix: n.suffix, namespace: ns}
}
// HasSameLeftmostPrefix check if two ResIds have the same
@@ -133,6 +183,14 @@ func (n ResId) HasSameLeftmostPrefix(id ResId) bool {
return prefixes1[0] == prefixes2[0]
}
// HasSameRightmostSuffix check if two ResIds have the same
// right most suffix.
func (n ResId) HasSameRightmostSuffix(id ResId) bool {
suffixes1 := n.suffixList()
suffixes2 := id.suffixList()
return suffixes1[len(suffixes1)-1] == suffixes2[len(suffixes2)-1]
}
func (n ResId) concatPrefix(p string) string {
if p == "" {
return n.prefix
@@ -143,6 +201,20 @@ func (n ResId) concatPrefix(p string) string {
return p + ":" + n.prefix
}
func (n ResId) concatSuffix(s string) string {
if s == "" {
return n.suffix
}
if n.suffix == "" {
return s
}
return n.suffix + ":" + s
}
func (n ResId) prefixList() []string {
return strings.Split(n.prefix, ":")
}
func (n ResId) suffixList() []string {
return strings.Split(n.suffix, ":")
}

View File

@@ -11,27 +11,30 @@ var stringTests = []struct {
s string
}{
{ResId{gvKind: gvk.Gvk{Group: "g", Version: "v", Kind: "k"},
name: "nm", prefix: "p", namespace: "ns"},
"g_v_k|ns|p|nm"},
name: "nm", prefix: "p", suffix: "s", namespace: "ns"},
"g_v_k|ns|p|nm|s"},
{ResId{gvKind: gvk.Gvk{Version: "v", Kind: "k"},
name: "nm", prefix: "p", namespace: "ns"},
"noGroup_v_k|ns|p|nm"},
name: "nm", prefix: "p", suffix: "s", namespace: "ns"},
"noGroup_v_k|ns|p|nm|s"},
{ResId{gvKind: gvk.Gvk{Kind: "k"},
name: "nm", prefix: "p", namespace: "ns"},
"noGroup_noVersion_k|ns|p|nm"},
name: "nm", prefix: "p", suffix: "s", namespace: "ns"},
"noGroup_noVersion_k|ns|p|nm|s"},
{ResId{gvKind: gvk.Gvk{},
name: "nm", prefix: "p", namespace: "ns"},
"noGroup_noVersion_noKind|ns|p|nm"},
name: "nm", prefix: "p", suffix: "s", namespace: "ns"},
"noGroup_noVersion_noKind|ns|p|nm|s"},
{ResId{gvKind: gvk.Gvk{},
name: "nm", prefix: "p"},
"noGroup_noVersion_noKind|noNamespace|p|nm"},
name: "nm", prefix: "p", suffix: "s"},
"noGroup_noVersion_noKind|noNamespace|p|nm|s"},
{ResId{gvKind: gvk.Gvk{},
name: "nm"},
"noGroup_noVersion_noKind|noNamespace|noPrefix|nm"},
name: "nm", suffix: "s"},
"noGroup_noVersion_noKind|noNamespace|noPrefix|nm|s"},
{ResId{gvKind: gvk.Gvk{},
suffix: "s"},
"noGroup_noVersion_noKind|noNamespace|noPrefix|noName|s"},
{ResId{gvKind: gvk.Gvk{}},
"noGroup_noVersion_noKind|noNamespace|noPrefix|noName"},
"noGroup_noVersion_noKind|noNamespace|noPrefix|noName|noSuffix"},
{ResId{},
"noGroup_noVersion_noKind|noNamespace|noPrefix|noName"},
"noGroup_noVersion_noKind|noNamespace|noPrefix|noName|noSuffix"},
}
func TestString(t *testing.T) {
@@ -47,23 +50,26 @@ var gvknStringTests = []struct {
s string
}{
{ResId{gvKind: gvk.Gvk{Group: "g", Version: "v", Kind: "k"},
name: "nm", prefix: "p", namespace: "ns"},
name: "nm", prefix: "p", suffix: "s", namespace: "ns"},
"g_v_k|nm"},
{ResId{gvKind: gvk.Gvk{Version: "v", Kind: "k"},
name: "nm", prefix: "p", namespace: "ns"},
name: "nm", prefix: "p", suffix: "s", namespace: "ns"},
"noGroup_v_k|nm"},
{ResId{gvKind: gvk.Gvk{Kind: "k"},
name: "nm", prefix: "p", namespace: "ns"},
name: "nm", prefix: "p", suffix: "s", namespace: "ns"},
"noGroup_noVersion_k|nm"},
{ResId{gvKind: gvk.Gvk{},
name: "nm", prefix: "p", namespace: "ns"},
name: "nm", prefix: "p", suffix: "s", namespace: "ns"},
"noGroup_noVersion_noKind|nm"},
{ResId{gvKind: gvk.Gvk{},
name: "nm", prefix: "p"},
name: "nm", prefix: "p", suffix: "s"},
"noGroup_noVersion_noKind|nm"},
{ResId{gvKind: gvk.Gvk{},
name: "nm"},
name: "nm", suffix: "s"},
"noGroup_noVersion_noKind|nm"},
{ResId{gvKind: gvk.Gvk{},
suffix: "s"},
"noGroup_noVersion_noKind|"},
{ResId{gvKind: gvk.Gvk{}},
"noGroup_noVersion_noKind|"},
{ResId{},
@@ -83,19 +89,19 @@ var GvknEqualsTest = []struct {
x2 ResId
}{
{ResId{gvKind: gvk.Gvk{Group: "g", Version: "v", Kind: "k"},
name: "nm", prefix: "AA", namespace: "X"},
name: "nm", prefix: "AA", suffix: "aa", namespace: "X"},
ResId{gvKind: gvk.Gvk{Group: "g", Version: "v", Kind: "k"},
name: "nm", prefix: "BB", namespace: "Z"}},
name: "nm", prefix: "BB", suffix: "bb", namespace: "Z"}},
{ResId{gvKind: gvk.Gvk{Version: "v", Kind: "k"},
name: "nm", prefix: "AA", namespace: "X"},
name: "nm", prefix: "AA", suffix: "aa", namespace: "X"},
ResId{gvKind: gvk.Gvk{Version: "v", Kind: "k"},
name: "nm", prefix: "BB", namespace: "Z"}},
name: "nm", prefix: "BB", suffix: "bb", namespace: "Z"}},
{ResId{gvKind: gvk.Gvk{Kind: "k"},
name: "nm", prefix: "AA", namespace: "X"},
name: "nm", prefix: "AA", suffix: "aa", namespace: "X"},
ResId{gvKind: gvk.Gvk{Kind: "k"},
name: "nm", prefix: "BB", namespace: "Z"}},
{ResId{name: "nm", prefix: "AA", namespace: "X"},
ResId{name: "nm", prefix: "BB", namespace: "Z"}},
name: "nm", prefix: "BB", suffix: "bb", namespace: "Z"}},
{ResId{name: "nm", prefix: "AA", suffix: "aa", namespace: "X"},
ResId{name: "nm", prefix: "BB", suffix: "bb", namespace: "Z"}},
}
func TestEquals(t *testing.T) {

View File

@@ -53,8 +53,8 @@ metadata:
---
`
l := loadertest.NewFakeLoader("/home/seans/project")
if ferr := l.AddFile("/home/seans/project/deployment.yaml", []byte(resourceStr)); ferr != nil {
l := loadertest.NewFakeLoader("/whatever/project")
if ferr := l.AddFile("/whatever/project/deployment.yaml", []byte(resourceStr)); ferr != nil {
t.Fatalf("Error adding fake file: %v\n", ferr)
}
expected := ResMap{resid.NewResId(deploy, "dply1"): rf.FromMap(
@@ -85,7 +85,7 @@ metadata:
}
m, _ := rmF.FromFiles(
l, []string{"/home/seans/project/deployment.yaml"})
l, []string{"deployment.yaml"})
if len(m) != 3 {
t.Fatalf("%#v should contain 3 appResource, but got %d", m, len(m))
}
@@ -145,7 +145,7 @@ func TestNewFromConfigMaps(t *testing.T) {
expected ResMap
}
l := loadertest.NewFakeLoader("/home/seans/project/")
l := loadertest.NewFakeLoader("/whatever/project/")
testCases := []testCase{
{
description: "construct config map from env",
@@ -157,7 +157,7 @@ func TestNewFromConfigMaps(t *testing.T) {
},
},
},
filepath: "/home/seans/project/app.env",
filepath: "/whatever/project/app.env",
content: "DB_USERNAME=admin\nDB_PASSWORD=somepw",
expected: ResMap{
resid.NewResId(cmap, "envConfigMap"): rf.FromMap(
@@ -183,7 +183,7 @@ func TestNewFromConfigMaps(t *testing.T) {
},
},
},
filepath: "/home/seans/project/app-init.ini",
filepath: "/whatever/project/app-init.ini",
content: "FOO=bar\nBAR=baz\n",
expected: ResMap{
resid.NewResId(cmap, "fileConfigMap"): rf.FromMap(
@@ -270,7 +270,7 @@ func TestNewResMapFromSecretArgs(t *testing.T) {
}
fakeFs := fs.MakeFakeFS()
fakeFs.Mkdir(".")
rmF.Set(fakeFs, loader.NewFileLoader(fakeFs))
rmF.Set(fakeFs, loader.NewFileLoaderAtRoot(fakeFs))
actual, err := rmF.NewResMapFromSecretArgs(secrets, nil)
if err != nil {
@@ -326,7 +326,7 @@ func TestSecretTimeout(t *testing.T) {
}
fakeFs := fs.MakeFakeFS()
fakeFs.Mkdir(".")
rmF.Set(fakeFs, loader.NewFileLoader(fakeFs))
rmF.Set(fakeFs, loader.NewFileLoaderAtRoot(fakeFs))
_, err := rmF.NewResMapFromSecretArgs(secrets, nil)
if err == nil {

View File

@@ -20,11 +20,11 @@ package resmap
import (
"bytes"
"fmt"
"log"
"reflect"
"sort"
"github.com/ghodss/yaml"
"github.com/golang/glog"
"sigs.k8s.io/kustomize/pkg/ifc"
"sigs.k8s.io/kustomize/pkg/resid"
"sigs.k8s.io/kustomize/pkg/resource"
@@ -121,16 +121,17 @@ func (m ResMap) DeepCopy(rf *resource.Factory) ResMap {
return mcopy
}
// FilterBy returns a ResMap containing ResIds with the same namespace and nameprefix
// with the inputId
// If inputId is a cluster level resource, return the original resmap
// FilterBy returns a subset ResMap containing ResIds with
// the same namespace and leftmost name prefix and rightmost name
// as the inputId. If inputId is a cluster level resource, this
// returns the original ResMap.
func (m ResMap) FilterBy(inputId resid.ResId) ResMap {
if inputId.Gvk().IsClusterKind() {
return m
}
result := ResMap{}
for id, res := range m {
if id.Namespace() == inputId.Namespace() && id.HasSameLeftmostPrefix(inputId) {
if id.Namespace() == inputId.Namespace() && id.HasSameLeftmostPrefix(inputId) && id.HasSameRightmostSuffix(inputId) {
result[id] = res
}
}
@@ -180,17 +181,17 @@ func MergeWithOverride(maps ...ResMap) (ResMap, error) {
id = matchedId[0]
switch r.Behavior() {
case ifc.BehaviorReplace:
glog.V(4).Infof(
log.Printf(
"Replace %v with %v", result[id].Map(), r.Map())
r.Replace(result[id])
result[id] = r
result[id].SetBehavior(ifc.BehaviorCreate)
case ifc.BehaviorMerge:
glog.V(4).Infof(
log.Printf(
"Merging %v with %v", result[id].Map(), r.Map())
r.Merge(result[id])
result[id] = r
glog.V(4).Infof(
log.Printf(
"Merged object is %v", result[id].Map())
result[id].SetBehavior(ifc.BehaviorCreate)
default:

View File

@@ -20,7 +20,7 @@ import (
"reflect"
"testing"
"sigs.k8s.io/kustomize/internal/k8sdeps/kunstruct"
"sigs.k8s.io/kustomize/k8sdeps/kunstruct"
"sigs.k8s.io/kustomize/pkg/gvk"
"sigs.k8s.io/kustomize/pkg/ifc"
"sigs.k8s.io/kustomize/pkg/resid"
@@ -119,53 +119,147 @@ func TestDemandOneMatchForId(t *testing.T) {
}
func TestFilterBy(t *testing.T) {
rm := ResMap{resid.NewResIdWithPrefixNamespace(cmap, "cm1", "prefix1", "ns1"): rf.FromMap(
map[string]interface{}{
"apiVersion": "v1",
"kind": "ConfigMap",
"metadata": map[string]interface{}{
"name": "cm1",
tests := map[string]struct {
resMap ResMap
filter resid.ResId
expected ResMap
}{
"different namespace": {
resMap: ResMap{resid.NewResIdWithPrefixSuffixNamespace(cmap, "config-map", "prefix", "suffix", "namespace1"): rf.FromMap(
map[string]interface{}{
"apiVersion": "v1",
"kind": "ConfigMap",
"metadata": map[string]interface{}{
"name": "config-map",
},
}),
},
}),
resid.NewResIdWithPrefixNamespace(cmap, "cm2", "prefix1", "ns1"): rf.FromMap(
map[string]interface{}{
"apiVersion": "v1",
"kind": "ConfigMap",
"metadata": map[string]interface{}{
"name": "cm2",
},
}),
}
rm1 := ResMap{
resid.NewResIdWithPrefixNamespace(cmap, "cm3", "prefix1", "ns2"): rf.FromMap(
map[string]interface{}{
"apiVersion": "v1",
"kind": "ConfigMap",
"metadata": map[string]interface{}{
"name": "cm2",
},
}),
filter: resid.NewResIdWithPrefixSuffixNamespace(cmap, "config-map", "prefix", "suffix", "namespace2"),
expected: ResMap{},
},
"different prefix": {
resMap: ResMap{resid.NewResIdWithPrefixSuffixNamespace(cmap, "config-map", "prefix1", "suffix", "namespace"): rf.FromMap(
map[string]interface{}{
"apiVersion": "v1",
"kind": "ConfigMap",
"metadata": map[string]interface{}{
"name": "config-map",
},
}),
},
filter: resid.NewResIdWithPrefixSuffixNamespace(cmap, "config-map", "prefix2", "suffix", "namespace"),
expected: ResMap{},
},
"different suffix": {
resMap: ResMap{resid.NewResIdWithPrefixSuffixNamespace(cmap, "config-map", "prefix", "suffix1", "namespace"): rf.FromMap(
map[string]interface{}{
"apiVersion": "v1",
"kind": "ConfigMap",
"metadata": map[string]interface{}{
"name": "config-map",
},
}),
},
filter: resid.NewResIdWithPrefixSuffixNamespace(cmap, "config-map", "prefix", "suffix2", "namespace"),
expected: ResMap{},
},
"same namespace, same prefix": {
resMap: ResMap{resid.NewResIdWithPrefixNamespace(cmap, "config-map1", "prefix", "namespace"): rf.FromMap(
map[string]interface{}{
"apiVersion": "v1",
"kind": "ConfigMap",
"metadata": map[string]interface{}{
"name": "config-map1",
},
}),
},
filter: resid.NewResIdWithPrefixNamespace(cmap, "config-map2", "prefix", "namespace"),
expected: ResMap{resid.NewResIdWithPrefixNamespace(cmap, "config-map1", "prefix", "namespace"): rf.FromMap(
map[string]interface{}{
"apiVersion": "v1",
"kind": "ConfigMap",
"metadata": map[string]interface{}{
"name": "config-map1",
},
}),
},
},
"same namespace, same suffix": {
resMap: ResMap{resid.NewResIdWithSuffixNamespace(cmap, "config-map1", "suffix", "namespace"): rf.FromMap(
map[string]interface{}{
"apiVersion": "v1",
"kind": "ConfigMap",
"metadata": map[string]interface{}{
"name": "config-map1",
},
}),
},
filter: resid.NewResIdWithSuffixNamespace(cmap, "config-map2", "suffix", "namespace"),
expected: ResMap{resid.NewResIdWithSuffixNamespace(cmap, "config-map1", "suffix", "namespace"): rf.FromMap(
map[string]interface{}{
"apiVersion": "v1",
"kind": "ConfigMap",
"metadata": map[string]interface{}{
"name": "config-map1",
},
}),
},
},
"same namespace, same prefix, same suffix": {
resMap: ResMap{resid.NewResIdWithPrefixSuffixNamespace(cmap, "config-map1", "prefix", "suffix", "namespace"): rf.FromMap(
map[string]interface{}{
"apiVersion": "v1",
"kind": "ConfigMap",
"metadata": map[string]interface{}{
"name": "config-map",
},
}),
},
filter: resid.NewResIdWithPrefixSuffixNamespace(cmap, "config-map2", "prefix", "suffix", "namespace"),
expected: ResMap{resid.NewResIdWithPrefixSuffixNamespace(cmap, "config-map1", "prefix", "suffix", "namespace"): rf.FromMap(
map[string]interface{}{
"apiVersion": "v1",
"kind": "ConfigMap",
"metadata": map[string]interface{}{
"name": "config-map",
},
}),
},
},
"filter by cluster-level Gvk": {
resMap: ResMap{resid.NewResIdWithPrefixNamespace(cmap, "config-map", "prefix", "namespace"): rf.FromMap(
map[string]interface{}{
"apiVersion": "v1",
"kind": "ConfigMap",
"metadata": map[string]interface{}{
"name": "config-map",
},
}),
},
filter: resid.NewResId(gvk.Gvk{Kind: "ClusterRoleBinding"}, "cluster-role-binding"),
expected: ResMap{resid.NewResIdWithPrefixNamespace(cmap, "config-map", "prefix", "namespace"): rf.FromMap(
map[string]interface{}{
"apiVersion": "v1",
"kind": "ConfigMap",
"metadata": map[string]interface{}{
"name": "config-map",
},
}),
},
},
}
for k, v := range rm {
rm1[k] = v
}
empty := rm1.FilterBy(resid.NewResIdWithPrefixNamespace(cmap, "cm4", "prefix1", "ns3"))
if len(empty) != 0 {
t.Fatalf("Expected empty filtered map but got %v", empty)
}
ns1map := rm1.FilterBy(resid.NewResIdWithPrefixNamespace(cmap, "cm4", "prefix1", "ns1"))
if !reflect.DeepEqual(rm, ns1map) {
t.Fatalf("Expected %v but got back %v", rm, ns1map)
}
clmap := rm1.FilterBy(resid.NewResId(gvk.Gvk{Kind: "ClusterRoleBinding"}, "crb"))
if !reflect.DeepEqual(rm1, clmap) {
t.Fatalf("Expected %v but got back %v", rm1, clmap)
for name, test := range tests {
test := test
t.Run(name, func(t *testing.T) {
got := test.resMap.FilterBy(test.filter)
if !reflect.DeepEqual(test.expected, got) {
t.Fatalf("Expected %v but got back %v", test.expected, got)
}
})
}
}
func TestDeepCopy(t *testing.T) {
rm1 := ResMap{
resid.NewResId(cmap, "cm1"): rf.FromMap(

View File

@@ -26,14 +26,14 @@ import (
func TestSliceFromPatches(t *testing.T) {
patchGood1 := patch.StrategicMerge("/foo/patch1.yaml")
patchGood1 := patch.StrategicMerge("patch1.yaml")
patch1 := `
apiVersion: apps/v1
kind: Deployment
metadata:
name: pooh
`
patchGood2 := patch.StrategicMerge("/foo/patch2.yaml")
patchGood2 := patch.StrategicMerge("patch2.yaml")
patch2 := `
apiVersion: v1
kind: ConfigMap
@@ -45,14 +45,14 @@ metadata:
---
---
`
patchBad := patch.StrategicMerge("/foo/patch3.yaml")
patchBad := patch.StrategicMerge("patch3.yaml")
patch3 := `
WOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOT: woot
`
l := loadertest.NewFakeLoader("/foo")
l.AddFile(string(patchGood1), []byte(patch1))
l.AddFile(string(patchGood2), []byte(patch2))
l.AddFile(string(patchBad), []byte(patch3))
l := loadertest.NewFakeLoader("/")
l.AddFile("/"+string(patchGood1), []byte(patch1))
l.AddFile("/"+string(patchGood2), []byte(patch2))
l.AddFile("/"+string(patchBad), []byte(patch3))
tests := []struct {
name string

View File

@@ -19,7 +19,7 @@ package resource
import (
"testing"
"sigs.k8s.io/kustomize/internal/k8sdeps/kunstruct"
"sigs.k8s.io/kustomize/k8sdeps/kunstruct"
"sigs.k8s.io/kustomize/pkg/gvk"
"sigs.k8s.io/kustomize/pkg/resid"
)

View File

@@ -21,17 +21,18 @@ import (
"bytes"
"encoding/json"
"fmt"
"sigs.k8s.io/kustomize/pkg/ifc"
"sigs.k8s.io/kustomize/pkg/resid"
"log"
"strings"
"github.com/ghodss/yaml"
"github.com/golang/glog"
"github.com/pkg/errors"
"sigs.k8s.io/kustomize/pkg/constants"
"sigs.k8s.io/kustomize/pkg/fs"
"sigs.k8s.io/kustomize/pkg/ifc"
"sigs.k8s.io/kustomize/pkg/ifc/transformer"
interror "sigs.k8s.io/kustomize/pkg/internal/error"
patchtransformer "sigs.k8s.io/kustomize/pkg/patch/transformer"
"sigs.k8s.io/kustomize/pkg/resid"
"sigs.k8s.io/kustomize/pkg/resmap"
"sigs.k8s.io/kustomize/pkg/resource"
"sigs.k8s.io/kustomize/pkg/transformers"
@@ -44,18 +45,17 @@ type KustTarget struct {
kustomization *types.Kustomization
ldr ifc.Loader
fSys fs.FileSystem
rf *resmap.Factory
tcfg *config.TransformerConfig
ptf transformer.Factory
rFactory *resmap.Factory
tConfig *config.TransformerConfig
tFactory transformer.Factory
}
// NewKustTarget returns a new instance of KustTarget primed with a Loader.
func NewKustTarget(
ldr ifc.Loader, fSys fs.FileSystem,
rf *resmap.Factory,
ptf transformer.Factory,
tcfg *config.TransformerConfig) (*KustTarget, error) {
content, err := ldr.Load(constants.KustomizationFileName)
rFactory *resmap.Factory,
tFactory transformer.Factory) (*KustTarget, error) {
content, err := loadKustFile(ldr)
if err != nil {
return nil, err
}
@@ -66,13 +66,17 @@ func NewKustTarget(
return nil, err
}
k.DealWithDeprecatedFields()
tConfig, err := makeTransformerConfig(ldr, k.Configurations)
if err != nil {
return nil, err
}
return &KustTarget{
kustomization: &k,
ldr: ldr,
fSys: fSys,
rf: rf,
tcfg: tcfg,
ptf: ptf,
rFactory: rFactory,
tConfig: tConfig,
tFactory: tFactory,
}, nil
}
@@ -81,12 +85,20 @@ func unmarshal(y []byte, o interface{}) error {
if err != nil {
return err
}
dec := json.NewDecoder(bytes.NewReader(j))
dec.DisallowUnknownFields()
return dec.Decode(o)
}
// makeTransformerConfig returns a complete TransformerConfig object from either files
// or the default configs
func makeTransformerConfig(ldr ifc.Loader, paths []string) (*config.TransformerConfig, error) {
if paths == nil || len(paths) == 0 {
return config.NewFactory(nil).DefaultConfig(), nil
}
return config.NewFactory(ldr).FromFiles(paths)
}
// MakeCustomizedResMap creates a ResMap per kustomization instructions.
// The Resources in the returned ResMap are fully customized.
func (kt *KustTarget) MakeCustomizedResMap() (resmap.ResMap, error) {
@@ -99,15 +111,19 @@ func (kt *KustTarget) MakeCustomizedResMap() (resmap.ResMap, error) {
// resolveRefsToGeneratedResources fixes all name references.
func (kt *KustTarget) resolveRefsToGeneratedResources(m resmap.ResMap) (resmap.ResMap, error) {
if kt.kustomization.GeneratorOptions == nil || !kt.kustomization.GeneratorOptions.DisableHash {
err := kt.ptf.MakeHashTransformer().Transform(m)
if kt.kustomization.GeneratorOptions == nil ||
!kt.kustomization.GeneratorOptions.DisableNameSuffixHash {
// This effects only generated resources.
// It changes only the Name field in the
// resource held in the ResMap's value, not
// the Name in the key in the ResMap.
err := kt.tFactory.MakeHashTransformer().Transform(m)
if err != nil {
return nil, err
}
}
var r []transformers.Transformer
t, err := transformers.NewNameReferenceTransformer(kt.tcfg.NameReference)
t, err := transformers.NewNameReferenceTransformer(kt.tConfig.NameReference)
if err != nil {
return nil, err
}
@@ -117,7 +133,7 @@ func (kt *KustTarget) resolveRefsToGeneratedResources(m resmap.ResMap) (resmap.R
if err != nil {
return nil, err
}
t = transformers.NewRefVarTransformer(refVars, kt.tcfg.VarReference)
t = transformers.NewRefVarTransformer(refVars, kt.tConfig.VarReference)
r = append(r, t)
err = transformers.NewMultiTransformer(r).Transform(m)
@@ -135,7 +151,7 @@ func (kt *KustTarget) loadCustomizedResMap() (resmap.ResMap, error) {
errs.Append(errors.Wrap(err, "loadResMapFromBasesAndResources"))
}
crdTc, err := config.NewFactory(kt.ldr).LoadCRDs(kt.kustomization.Crds)
kt.tcfg = kt.tcfg.Merge(crdTc)
kt.tConfig = kt.tConfig.Merge(crdTc)
if err != nil {
errs.Append(errors.Wrap(err, "LoadCRDs"))
}
@@ -148,12 +164,11 @@ func (kt *KustTarget) loadCustomizedResMap() (resmap.ResMap, error) {
return nil, err
}
patches, err := kt.rf.RF().SliceFromPatches(
patches, err := kt.rFactory.RF().SliceFromPatches(
kt.ldr, kt.kustomization.PatchesStrategicMerge)
if err != nil {
errs.Append(errors.Wrap(err, "SliceFromPatches"))
}
if len(errs.Get()) > 0 {
return nil, errs
}
@@ -185,12 +200,14 @@ func (kt *KustTarget) loadCustomizedResMap() (resmap.ResMap, error) {
func (kt *KustTarget) generateConfigMapsAndSecrets(
errs *interror.KustomizationErrors) (resmap.ResMap, error) {
kt.rf.Set(kt.fSys, kt.ldr)
cms, err := kt.rf.NewResMapFromConfigMapArgs(kt.kustomization.ConfigMapGenerator, kt.kustomization.GeneratorOptions)
kt.rFactory.Set(kt.fSys, kt.ldr)
cms, err := kt.rFactory.NewResMapFromConfigMapArgs(
kt.kustomization.ConfigMapGenerator, kt.kustomization.GeneratorOptions)
if err != nil {
errs.Append(errors.Wrap(err, "NewResMapFromConfigMapArgs"))
}
secrets, err := kt.rf.NewResMapFromSecretArgs(kt.kustomization.SecretGenerator, kt.kustomization.GeneratorOptions)
secrets, err := kt.rFactory.NewResMapFromSecretArgs(
kt.kustomization.SecretGenerator, kt.kustomization.GeneratorOptions)
if err != nil {
errs.Append(errors.Wrap(err, "NewResMapFromSecretArgs"))
}
@@ -200,7 +217,7 @@ func (kt *KustTarget) generateConfigMapsAndSecrets(
// Gets Bases and Resources as advertised.
func (kt *KustTarget) loadResMapFromBasesAndResources() (resmap.ResMap, error) {
bases, errs := kt.loadCustomizedBases()
resources, err := kt.rf.FromFiles(
resources, err := kt.rFactory.FromFiles(
kt.ldr, kt.kustomization.Resources)
if err != nil {
errs.Append(errors.Wrap(err, "rawResources failed to read Resources"))
@@ -223,7 +240,8 @@ func (kt *KustTarget) loadCustomizedBases() (resmap.ResMap, *interror.Kustomizat
continue
}
target, err := NewKustTarget(
ldr, kt.fSys, kt.rf, kt.ptf, kt.tcfg)
ldr, kt.fSys,
kt.rFactory, kt.tFactory)
if err != nil {
errs.Append(errors.Wrap(err, "couldn't make target for "+path))
continue
@@ -253,7 +271,7 @@ func (kt *KustTarget) loadBasesAsFlatList() ([]*KustTarget, error) {
continue
}
target, err := NewKustTarget(
ldr, kt.fSys, kt.rf, kt.ptf, kt.tcfg)
ldr, kt.fSys, kt.rFactory, kt.tFactory)
if err != nil {
errs.Append(err)
continue
@@ -269,27 +287,30 @@ func (kt *KustTarget) loadBasesAsFlatList() ([]*KustTarget, error) {
// newTransformer makes a Transformer that does everything except resolve generated names.
func (kt *KustTarget) newTransformer(patches []*resource.Resource) (transformers.Transformer, error) {
var r []transformers.Transformer
t, err := kt.ptf.MakePatchTransformer(patches, kt.rf.RF())
t, err := kt.tFactory.MakePatchTransformer(patches, kt.rFactory.RF())
if err != nil {
return nil, err
}
r = append(r, t)
r = append(r, transformers.NewNamespaceTransformer(
string(kt.kustomization.Namespace), kt.tcfg.NameSpace, kt.rf.RF()))
t, err = transformers.NewNamePrefixTransformer(
string(kt.kustomization.NamePrefix), kt.tcfg.NamePrefix)
string(kt.kustomization.Namespace), kt.tConfig.NameSpace))
t, err = transformers.NewNamePrefixSuffixTransformer(
string(kt.kustomization.NamePrefix),
string(kt.kustomization.NameSuffix),
kt.tConfig.NamePrefix,
)
if err != nil {
return nil, err
}
r = append(r, t)
t, err = transformers.NewLabelsMapTransformer(
kt.kustomization.CommonLabels, kt.tcfg.CommonLabels)
kt.kustomization.CommonLabels, kt.tConfig.CommonLabels)
if err != nil {
return nil, err
}
r = append(r, t)
t, err = transformers.NewAnnotationsMapTransformer(
kt.kustomization.CommonAnnotations, kt.tcfg.CommonAnnotations)
kt.kustomization.CommonAnnotations, kt.tConfig.CommonAnnotations)
if err != nil {
return nil, err
}
@@ -312,7 +333,7 @@ func (kt *KustTarget) resolveRefVars(m resmap.ResMap) (map[string]string, error)
}
result[v.Name] = s
} else {
glog.Infof("couldn't resolve v: %v", v)
log.Printf("couldn't resolve v: %v", v)
}
}
return result, nil
@@ -347,3 +368,16 @@ func (kt *KustTarget) getAllVars() ([]types.Var, error) {
}
return result, nil
}
func loadKustFile(ldr ifc.Loader) ([]byte, error) {
for _, kf := range []string{constants.KustomizationFileName, constants.SecondaryKustomizationFileName} {
content, err := ldr.Load(kf)
if err == nil {
return content, nil
}
if !strings.Contains(err.Error(), "no such file or directory") {
return nil, err
}
}
return nil, fmt.Errorf("no kustomization.yaml file under %s", ldr.Root())
}

View File

@@ -22,8 +22,8 @@ import (
"strings"
"testing"
"sigs.k8s.io/kustomize/internal/k8sdeps/kunstruct"
"sigs.k8s.io/kustomize/internal/k8sdeps/transformer"
"sigs.k8s.io/kustomize/k8sdeps/kunstruct"
"sigs.k8s.io/kustomize/k8sdeps/transformer"
"sigs.k8s.io/kustomize/pkg/constants"
"sigs.k8s.io/kustomize/pkg/fs"
"sigs.k8s.io/kustomize/pkg/gvk"
@@ -32,13 +32,13 @@ import (
"sigs.k8s.io/kustomize/pkg/resid"
"sigs.k8s.io/kustomize/pkg/resmap"
"sigs.k8s.io/kustomize/pkg/resource"
"sigs.k8s.io/kustomize/pkg/transformers/config"
"sigs.k8s.io/kustomize/pkg/types"
)
const (
kustomizationContent1 = `
namePrefix: foo-
nameSuffix: -bar
namespace: ns1
commonLabels:
app: nginx
@@ -96,8 +96,7 @@ func makeKustTarget(t *testing.T, l ifc.Loader) *KustTarget {
fakeFs := fs.MakeFakeFS()
fakeFs.Mkdir("/")
kt, err := NewKustTarget(
l, fakeFs, rf, transformer.NewFactoryImpl(),
config.NewFactory(l).DefaultConfig())
l, fakeFs, rf, transformer.NewFactoryImpl())
if err != nil {
t.Fatalf("Unexpected construction error %v", err)
}
@@ -132,12 +131,12 @@ var ns = gvk.Gvk{Version: "v1", Kind: "Namespace"}
func TestResources1(t *testing.T) {
expected := resmap.ResMap{
resid.NewResIdWithPrefixNamespace(deploy, "dply1", "foo-", "ns1"): rf.RF().FromMap(
resid.NewResIdWithPrefixSuffixNamespace(deploy, "dply1", "foo-", "-bar", "ns1"): rf.RF().FromMap(
map[string]interface{}{
"apiVersion": "apps/v1",
"kind": "Deployment",
"metadata": map[string]interface{}{
"name": "foo-dply1",
"name": "foo-dply1-bar",
"namespace": "ns1",
"labels": map[string]interface{}{
"app": "nginx",
@@ -165,12 +164,12 @@ func TestResources1(t *testing.T) {
},
},
}),
resid.NewResIdWithPrefixNamespace(cmap, "literalConfigMap", "foo-", "ns1"): rf.RF().FromMap(
resid.NewResIdWithPrefixSuffixNamespace(cmap, "literalConfigMap", "foo-", "-bar", "ns1"): rf.RF().FromMap(
map[string]interface{}{
"apiVersion": "v1",
"kind": "ConfigMap",
"metadata": map[string]interface{}{
"name": "foo-literalConfigMap-mc92bgcbh5",
"name": "foo-literalConfigMap-bar-8d2dkb8k24",
"namespace": "ns1",
"labels": map[string]interface{}{
"app": "nginx",
@@ -184,12 +183,12 @@ func TestResources1(t *testing.T) {
"DB_PASSWORD": "somepw",
},
}).SetBehavior(ifc.BehaviorCreate),
resid.NewResIdWithPrefixNamespace(secret, "secret", "foo-", "ns1"): rf.RF().FromMap(
resid.NewResIdWithPrefixSuffixNamespace(secret, "secret", "foo-", "-bar", "ns1"): rf.RF().FromMap(
map[string]interface{}{
"apiVersion": "v1",
"kind": "Secret",
"metadata": map[string]interface{}{
"name": "foo-secret-877fcfhgt5",
"name": "foo-secret-bar-9btc7bt4kb",
"namespace": "ns1",
"labels": map[string]interface{}{
"app": "nginx",
@@ -204,12 +203,12 @@ func TestResources1(t *testing.T) {
"DB_PASSWORD": base64.StdEncoding.EncodeToString([]byte("somepw")),
},
}).SetBehavior(ifc.BehaviorCreate),
resid.NewResIdWithPrefixNamespace(ns, "ns1", "", ""): rf.RF().FromMap(
resid.NewResIdWithPrefixSuffixNamespace(ns, "ns1", "foo-", "-bar", ""): rf.RF().FromMap(
map[string]interface{}{
"apiVersion": "v1",
"kind": "Namespace",
"metadata": map[string]interface{}{
"name": "ns1",
"name": "foo-ns1-bar",
"labels": map[string]interface{}{
"app": "nginx",
},
@@ -261,17 +260,17 @@ func TestSecretTimeout(t *testing.T) {
}
}
func TestDisableHash(t *testing.T) {
func TestDisableNameSuffixHash(t *testing.T) {
kt := makeKustTarget(t, makeLoader1(t))
kt.kustomization.GeneratorOptions = &types.GeneratorOptions{DisableHash: true}
kt.kustomization.GeneratorOptions = &types.GeneratorOptions{DisableNameSuffixHash: true}
actual, err := kt.MakeCustomizedResMap()
if err != nil {
t.Fatalf("unexpected Resources error %v", err)
}
for id, r := range actual {
if !strings.HasSuffix(r.GetName(), id.Name()) {
t.Fatalf("unexpected hash was added to %s: %s", id.Name(), r.GetName())
if r.GetName() != id.NameWithPrefixSuffix() {
t.Errorf("unexpected hash was added to %s: %s", id.NameWithPrefixSuffix(), r.GetName())
}
}
}

View File

@@ -108,7 +108,7 @@ nameReference:
kind: Job
- path: spec/jobTemplate/spec/template/spec/volumes/configMap/name
kind: CronJob
- path: spec/jobTemplate/spec/template/spec/containers/env/valueFrom/configmapKeyRef/name
- path: spec/jobTemplate/spec/template/spec/containers/env/valueFrom/configMapKeyRef/name
kind: CronJob
- path: spec/jobTemplate/spec/template/spec/initContainers/env/valueFrom/configMapKeyRef/name
kind: CronJob
@@ -198,7 +198,7 @@ nameReference:
kind: Job
- path: spec/template/spec/imagePullSecrets/name
kind: Job
- path: spec/jobTemplate/spec/template/sepc/volumes/secret/secretName
- path: spec/jobTemplate/spec/template/spec/volumes/secret/secretName
kind: CronJob
- path: spec/jobTemplate/spec/template/spec/containers/env/valueFrom/secretKeyRef/name
kind: CronJob

View File

@@ -38,8 +38,8 @@ namePrefix:
kind: SomeKind
`
fakeFS := fs.MakeFakeFS()
fakeFS.WriteFile("transformerconfig/test/config.yaml", []byte(transformerConfig))
ldr := loader.NewFileLoader(fakeFS)
fakeFS.WriteFile("/transformerconfig/test/config.yaml", []byte(transformerConfig))
ldr := loader.NewFileLoaderAtRoot(fakeFS)
expected := &TransformerConfig{
NamePrefix: []FieldSpec{
{
@@ -55,7 +55,7 @@ func TestMakeTransformerConfigFromFiles(t *testing.T) {
ldr, expected, _ := makeFakeLoaderAndOutput()
tcfg, err := NewFactory(ldr).FromFiles([]string{"transformerconfig/test/config.yaml"})
if err != nil {
t.Fatalf("unexpected error %v", err)
t.Fatalf("unexpected error: %v", err)
}
if !reflect.DeepEqual(tcfg, expected) {

View File

@@ -182,9 +182,11 @@ func TestLoadCRDs(t *testing.T) {
NameReference: nbrs,
}
actualTc, _ := NewFactory(makeLoader(t)).LoadCRDs(
[]string{"/testpath/crd.json"})
actualTc, err := NewFactory(makeLoader(t)).LoadCRDs(
[]string{"crd.json"})
if err != nil {
t.Fatalf("unexpected error:%v", err)
}
if !reflect.DeepEqual(actualTc, expectedTc) {
t.Fatalf("expected\n %v\n but got\n %v\n", expectedTc, actualTc)
}

View File

@@ -25,6 +25,7 @@ import (
// TransformerConfig holds the data needed to perform transformations.
type TransformerConfig struct {
NamePrefix fsSlice `json:"namePrefix,omitempty" yaml:"namePrefix,omitempty"`
NameSuffix fsSlice `json:"nameSuffix,omitempty" yaml:"nameSuffix,omitempty"`
NameSpace fsSlice `json:"namespace,omitempty" yaml:"namespace,omitempty"`
CommonLabels fsSlice `json:"commonLabels,omitempty" yaml:"commonLabels,omitempty"`
CommonAnnotations fsSlice `json:"commonAnnotations,omitempty" yaml:"commonAnnotations,omitempty"`
@@ -47,6 +48,11 @@ func (t *TransformerConfig) AddPrefixFieldSpec(fs FieldSpec) {
t.NamePrefix = append(t.NamePrefix, fs)
}
// AddSuffixFieldSpec adds a FieldSpec to NameSuffix
func (t *TransformerConfig) AddSuffixFieldSpec(fs FieldSpec) {
t.NameSuffix = append([]FieldSpec{fs}, t.NameSuffix...)
}
// AddLabelFieldSpec adds a FieldSpec to CommonLabels
func (t *TransformerConfig) AddLabelFieldSpec(fs FieldSpec) {
t.CommonLabels = append(t.CommonLabels, fs)
@@ -64,8 +70,12 @@ func (t *TransformerConfig) AddNamereferenceFieldSpec(nbrs NameBackReferences) {
// Merge merges two TransformerConfigs objects into a new TransformerConfig object
func (t *TransformerConfig) Merge(input *TransformerConfig) *TransformerConfig {
if input == nil {
return t
}
merged := &TransformerConfig{}
merged.NamePrefix = append(t.NamePrefix, input.NamePrefix...)
merged.NameSuffix = append(input.NameSuffix, t.NameSuffix...)
merged.NameSpace = append(t.NameSpace, input.NameSpace...)
merged.CommonAnnotations = append(t.CommonAnnotations, input.CommonAnnotations...)
merged.CommonLabels = append(t.CommonLabels, input.CommonLabels...)

View File

@@ -61,6 +61,10 @@ func TestAddFieldSpecs(t *testing.T) {
if len(cfg.NamePrefix) != 1 {
t.Fatalf("failed to add nameprefix FieldSpec")
}
cfg.AddSuffixFieldSpec(fieldSpec)
if len(cfg.NameSuffix) != 1 {
t.Fatalf("failed to add namesuffix FieldSpec")
}
cfg.AddLabelFieldSpec(fieldSpec)
if len(cfg.CommonLabels) != 1 {
t.Fatalf("failed to add nameprefix FieldSpec")
@@ -117,10 +121,12 @@ func TestMerge(t *testing.T) {
cfga := &TransformerConfig{}
cfga.AddNamereferenceFieldSpec(nameReference[0])
cfga.AddPrefixFieldSpec(fieldSpecs[0])
cfga.AddSuffixFieldSpec(fieldSpecs[0])
cfgb := &TransformerConfig{}
cfgb.AddNamereferenceFieldSpec(nameReference[1])
cfgb.AddPrefixFieldSpec(fieldSpecs[1])
cfga.AddSuffixFieldSpec(fieldSpecs[1])
actual := cfga.Merge(cfgb)
@@ -128,6 +134,10 @@ func TestMerge(t *testing.T) {
t.Fatal("merge failed for namePrefix FieldSpec")
}
if len(actual.NameSuffix) != 2 {
t.Fatal("merge failed for nameSuffix FieldSpec")
}
if len(actual.NameReference) != 1 {
t.Fatal("merge failed for namereference FieldSpec")
}
@@ -137,8 +147,15 @@ func TestMerge(t *testing.T) {
expected.AddNamereferenceFieldSpec(nameReference[1])
expected.AddPrefixFieldSpec(fieldSpecs[0])
expected.AddPrefixFieldSpec(fieldSpecs[1])
expected.AddSuffixFieldSpec(fieldSpecs[0])
expected.AddSuffixFieldSpec(fieldSpecs[1])
if !reflect.DeepEqual(actual, expected) {
t.Fatalf("expected: %v\n but got: %v\n", expected, actual)
}
actual = cfga.Merge(nil)
if !reflect.DeepEqual(actual, cfga) {
t.Fatalf("expected: %v\n but got: %v\n", cfga, actual)
}
}

View File

@@ -20,7 +20,7 @@ import (
"reflect"
"testing"
"sigs.k8s.io/kustomize/internal/k8sdeps/kunstruct"
"sigs.k8s.io/kustomize/k8sdeps/kunstruct"
"sigs.k8s.io/kustomize/pkg/gvk"
"sigs.k8s.io/kustomize/pkg/resid"
"sigs.k8s.io/kustomize/pkg/resmap"

View File

@@ -77,7 +77,7 @@ func (o *mapTransformer) Transform(m resmap.ResMap) error {
func (o *mapTransformer) addMap(in interface{}) (interface{}, error) {
m, ok := in.(map[string]interface{})
if !ok {
return nil, fmt.Errorf("%#v is expectd to be %T", in, m)
return nil, fmt.Errorf("%#v is expected to be %T", in, m)
}
for k, v := range o.m {
m[k] = v

View File

@@ -20,7 +20,7 @@ import (
"reflect"
"testing"
"sigs.k8s.io/kustomize/internal/k8sdeps/kunstruct"
"sigs.k8s.io/kustomize/k8sdeps/kunstruct"
"sigs.k8s.io/kustomize/pkg/gvk"
"sigs.k8s.io/kustomize/pkg/resid"
"sigs.k8s.io/kustomize/pkg/resmap"

View File

@@ -18,11 +18,17 @@ package transformers
import (
"fmt"
"log"
"strings"
)
type mutateFunc func(interface{}) (interface{}, error)
func mutateField(m map[string]interface{}, pathToField []string, createIfNotPresent bool, fns ...mutateFunc) error {
func mutateField(
m map[string]interface{},
pathToField []string,
createIfNotPresent bool,
fns ...mutateFunc) error {
if len(pathToField) == 0 {
return nil
}
@@ -49,6 +55,11 @@ func mutateField(m map[string]interface{}, pathToField []string, createIfNotPres
v := m[pathToField[0]]
newPathToField := pathToField[1:]
switch typedV := v.(type) {
case nil:
log.Printf(
"nil value at `%s` ignored in mutation attempt",
strings.Join(pathToField, "."))
return nil
case map[string]interface{}:
return mutateField(typedV, newPathToField, createIfNotPresent, fns...)
case []interface{}:
@@ -56,7 +67,7 @@ func mutateField(m map[string]interface{}, pathToField []string, createIfNotPres
item := typedV[i]
typedItem, ok := item.(map[string]interface{})
if !ok {
return fmt.Errorf("%#v is expectd to be %T", item, typedItem)
return fmt.Errorf("%#v is expected to be %T", item, typedItem)
}
err := mutateField(typedItem, newPathToField, createIfNotPresent, fns...)
if err != nil {

View File

@@ -0,0 +1,168 @@
/*
Copyright 2018 The Kubernetes Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package transformers
import (
"fmt"
"sigs.k8s.io/kustomize/k8sdeps/kunstruct"
"sigs.k8s.io/kustomize/pkg/ifc"
"testing"
)
type noopMutator struct {
wasCalled bool
errorToReturn error
}
var errExpected = fmt.Errorf("oops")
const originalValue = "tomato"
const newValue = "notThe" + originalValue
func (m *noopMutator) mutate(in interface{}) (interface{}, error) {
m.wasCalled = true
return newValue, m.errorToReturn
}
func makeTestDeployment() ifc.Kunstructured {
factory := kunstruct.NewKunstructuredFactoryImpl()
return factory.FromMap(
map[string]interface{}{
"group": "apps",
"apiVersion": "v1",
"kind": "Deployment",
"metadata": map[string]interface{}{
"name": originalValue,
},
"spec": map[string]interface{}{
"template": map[string]interface{}{
"env": []interface{}{
map[string]interface{}{
"name": "HELLO",
"value": "hi there",
},
map[string]interface{}{
"name": "GOODBYE",
"value": "adios!",
},
},
"metadata": map[string]interface{}{
"labels": map[string]interface{}{
"vegetable": originalValue,
},
},
"spec": map[string]interface{}{
"containers": []interface{}{
map[string]interface{}{
"name": "tangerine",
"image": originalValue,
},
},
},
},
},
})
}
func getFieldValue(t *testing.T, obj ifc.Kunstructured, fieldName string) string {
v, err := obj.GetFieldValue(fieldName)
if err != nil {
t.Fatalf("unexpected field error: %v", err)
}
return v
}
func TestNoPath(t *testing.T) {
obj := makeTestDeployment()
m := &noopMutator{}
err := mutateField(
obj.Map(), []string{}, false, m.mutate)
if m.wasCalled {
t.Fatalf("mutator should not have been called.")
}
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
}
func TestHappyPath(t *testing.T) {
obj := makeTestDeployment()
v := getFieldValue(t, obj, "metadata.name")
if v != originalValue {
t.Fatalf("unexpected original value: %v", v)
}
v = getFieldValue(t, obj, "spec.template.metadata.labels.vegetable")
if v != originalValue {
t.Fatalf("unexpected original value: %v", v)
}
m := &noopMutator{}
err := mutateField(
obj.Map(), []string{"metadata", "name"}, false, m.mutate)
if !m.wasCalled {
t.Fatalf("mutator should have been called.")
}
if err != nil {
t.Fatalf("unexpected mutate error: %v", err)
}
v = getFieldValue(t, obj, "metadata.name")
if v != newValue {
t.Fatalf("unexpected new value: %v", v)
}
m = &noopMutator{}
err = mutateField(
obj.Map(), []string{"spec", "template", "metadata", "labels", "vegetable"}, false, m.mutate)
if !m.wasCalled {
t.Fatalf("mutator should have been called.")
}
if err != nil {
t.Fatalf("unexpected mutate error: %v", err)
}
v = getFieldValue(t, obj, "spec.template.metadata.labels.vegetable")
if v != newValue {
t.Fatalf("unexpected new value: %v", v)
}
}
func TestWithError(t *testing.T) {
obj := makeTestDeployment()
m := noopMutator{errorToReturn: errExpected}
err := mutateField(
obj.Map(), []string{"metadata", "name"}, false, m.mutate)
if !m.wasCalled {
t.Fatalf("mutator was not called!")
}
if err != errExpected {
t.Fatalf("unexpected error: %v", err)
}
}
func TestWithNil(t *testing.T) {
obj := makeTestDeployment()
foo := obj.Map()["spec"]
foo = foo.(map[string]interface{})["template"]
foo = foo.(map[string]interface{})["metadata"]
foo.(map[string]interface{})["labels"] = nil
m := &noopMutator{}
err := mutateField(
obj.Map(), []string{"spec", "template", "metadata", "labels", "vegetable"}, false, m.mutate)
if err != nil {
t.Fatalf("Unexpected error: %v", err)
}
}

View File

@@ -21,7 +21,6 @@ import (
"fmt"
"sigs.k8s.io/kustomize/pkg/gvk"
"sigs.k8s.io/kustomize/pkg/resid"
"sigs.k8s.io/kustomize/pkg/resmap"
"sigs.k8s.io/kustomize/pkg/transformers/config"
)
@@ -42,22 +41,26 @@ func NewNameReferenceTransformer(
return &nameReferenceTransformer{backRefs: br}, nil
}
// Transform does the fields update according to fieldSpecs.
// Transform does the field update according to fieldSpecs.
// The old name is in the key in the map and the new name is in the object
// associated with the key. e.g. if <k, v> is one of the key-value pair in the map,
// then the old name is k.Name and the new name is v.GetName()
func (o *nameReferenceTransformer) Transform(m resmap.ResMap) error {
// TODO: Too much looping.
// Even more hidden loops in FilterBy,
// updateNameReference and FindByGVKN.
for id := range m {
objMap := m[id].Map()
for _, backRef := range o.backRefs {
for _, path := range backRef.FieldSpecs {
if !id.Gvk().IsSelected(&path.Gvk) {
continue
}
err := mutateField(objMap, path.PathSlice(), path.CreateIfNotPresent,
o.updateNameReference(backRef.Gvk, m.FilterBy(id)))
if err != nil {
return err
for _, fSpec := range backRef.FieldSpecs {
if id.Gvk().IsSelected(&fSpec.Gvk) {
err := mutateField(
m[id].Map(), fSpec.PathSlice(),
fSpec.CreateIfNotPresent,
o.updateNameReference(
backRef.Gvk, m.FilterBy(id)))
if err != nil {
return err
}
}
}
}
@@ -66,33 +69,26 @@ func (o *nameReferenceTransformer) Transform(m resmap.ResMap) error {
}
func (o *nameReferenceTransformer) updateNameReference(
k gvk.Gvk, m resmap.ResMap) func(in interface{}) (interface{}, error) {
backRef gvk.Gvk, m resmap.ResMap) func(in interface{}) (interface{}, error) {
return func(in interface{}) (interface{}, error) {
s, ok := in.(string)
if !ok {
return nil, fmt.Errorf("%#v is expectd to be %T", in, s)
return nil, fmt.Errorf("%#v is expected to be %T", in, s)
}
for id, res := range m {
if !id.Gvk().IsSelected(&k) {
continue
}
if id.Name() == s {
err := o.detectConflict(id, m, s)
if err != nil {
return nil, err
if id.Gvk().IsSelected(&backRef) && id.Name() == s {
matchedIds := m.FindByGVKN(id)
// If there's more than one match, there's no way
// to know which one to pick, so emit error.
if len(matchedIds) > 1 {
return nil, fmt.Errorf(
"Multiple matches for name %s:\n %v", id, matchedIds)
}
// Return transformed name of the object,
// complete with prefixes, hashes, etc.
return res.GetName(), nil
}
}
return in, nil
}
}
func (o *nameReferenceTransformer) detectConflict(id resid.ResId, m resmap.ResMap, name string) error {
matchedIds := m.FindByGVKN(id)
if len(matchedIds) > 1 {
return fmt.Errorf("detected conflicts when resolving name references %s:\n%v", name, matchedIds)
}
return nil
}

View File

@@ -20,7 +20,7 @@ import (
"reflect"
"testing"
"sigs.k8s.io/kustomize/internal/k8sdeps/kunstruct"
"sigs.k8s.io/kustomize/k8sdeps/kunstruct"
"sigs.k8s.io/kustomize/pkg/resid"
"sigs.k8s.io/kustomize/pkg/resmap"
"sigs.k8s.io/kustomize/pkg/resource"

View File

@@ -18,9 +18,7 @@ package transformers
import (
"sigs.k8s.io/kustomize/pkg/gvk"
"sigs.k8s.io/kustomize/pkg/resid"
"sigs.k8s.io/kustomize/pkg/resmap"
"sigs.k8s.io/kustomize/pkg/resource"
"sigs.k8s.io/kustomize/pkg/transformers/config"
)
@@ -28,13 +26,12 @@ type namespaceTransformer struct {
namespace string
fieldSpecsToUse []config.FieldSpec
fieldSpecsToSkip []config.FieldSpec
factory *resource.Factory
}
var _ Transformer = &namespaceTransformer{}
// NewNamespaceTransformer construct a namespaceTransformer.
func NewNamespaceTransformer(ns string, cf []config.FieldSpec, f *resource.Factory) Transformer {
func NewNamespaceTransformer(ns string, cf []config.FieldSpec) Transformer {
if len(ns) == 0 {
return NewNoOpTransformer()
}
@@ -46,13 +43,11 @@ func NewNamespaceTransformer(ns string, cf []config.FieldSpec, f *resource.Facto
namespace: ns,
fieldSpecsToUse: cf,
fieldSpecsToSkip: skip,
factory: f,
}
}
// Transform adds the namespace.
func (o *namespaceTransformer) Transform(m resmap.ResMap) error {
o.createNamespaceIfNotFound(m)
mf := resmap.ResMap{}
for id := range m {
@@ -124,22 +119,3 @@ func (o *namespaceTransformer) updateClusterRoleBinding(m resmap.ResMap) {
objMap["subjects"] = subjects
}
}
func (o *namespaceTransformer) createNamespaceIfNotFound(m resmap.ResMap) {
id := resid.NewResId(gvk.Gvk{
Version: "v1",
Kind: "Namespace",
}, o.namespace)
ids := m.FindByGVKN(id)
if len(ids) == 0 {
m[id] = o.factory.FromMap(
map[string]interface{}{
"apiVersion": "v1",
"kind": "Namespace",
"metadata": map[string]interface{}{
"name": o.namespace,
},
},
)
}
}

View File

@@ -20,7 +20,7 @@ import (
"reflect"
"testing"
"sigs.k8s.io/kustomize/internal/k8sdeps/kunstruct"
"sigs.k8s.io/kustomize/k8sdeps/kunstruct"
"sigs.k8s.io/kustomize/pkg/resid"
"sigs.k8s.io/kustomize/pkg/resmap"
"sigs.k8s.io/kustomize/pkg/resource"
@@ -47,12 +47,12 @@ func TestNamespaceRun(t *testing.T) {
"namespace": "foo",
},
}),
resid.NewResId(ns, "test"): rf.FromMap(
resid.NewResId(ns, "ns1"): rf.FromMap(
map[string]interface{}{
"apiVersion": "v1",
"kind": "Namespace",
"metadata": map[string]interface{}{
"name": "test",
"name": "ns1",
},
}),
resid.NewResId(sa, "default"): rf.FromMap(
@@ -108,12 +108,12 @@ func TestNamespaceRun(t *testing.T) {
}),
}
expected := resmap.ResMap{
resid.NewResIdWithPrefixNamespace(ns, "test", "", ""): rf.FromMap(
resid.NewResIdWithPrefixNamespace(ns, "ns1", "", ""): rf.FromMap(
map[string]interface{}{
"apiVersion": "v1",
"kind": "Namespace",
"metadata": map[string]interface{}{
"name": "test",
"name": "ns1",
},
}),
resid.NewResIdWithPrefixNamespace(cmap, "cm1", "", "test"): rf.FromMap(
@@ -187,7 +187,7 @@ func TestNamespaceRun(t *testing.T) {
}),
}
nst := NewNamespaceTransformer("test", defaultTransformerConfig.NameSpace, rf)
nst := NewNamespaceTransformer("test", defaultTransformerConfig.NameSpace)
err := nst.Transform(m)
if err != nil {
t.Fatalf("unexpected error: %v", err)
@@ -243,7 +243,7 @@ func TestNamespaceRunForClusterLevelKind(t *testing.T) {
expected := m.DeepCopy(rf)
nst := NewNamespaceTransformer("ns1", defaultTransformerConfig.NameSpace, rf)
nst := NewNamespaceTransformer("test", defaultTransformerConfig.NameSpace)
err := nst.Transform(m)
if err != nil {
@@ -254,30 +254,3 @@ func TestNamespaceRunForClusterLevelKind(t *testing.T) {
t.Fatalf("actual doesn't match expected: %v", err)
}
}
func TestNamespaceNotFound(t *testing.T) {
rf := resource.NewFactory(
kunstruct.NewKunstructuredFactoryImpl())
m := resmap.ResMap{}
expected := resmap.ResMap{
resid.NewResId(ns, "test"): rf.FromMap(
map[string]interface{}{
"apiVersion": "v1",
"kind": "Namespace",
"metadata": map[string]interface{}{
"name": "test",
},
}),
}
nst := NewNamespaceTransformer("test", defaultTransformerConfig.NameSpace, rf)
err := nst.Transform(m)
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
if !reflect.DeepEqual(m, expected) {
err = expected.ErrorIfNotEqual(m)
t.Fatalf("actual doesn't match expected: %v", err)
}
}

View File

@@ -1,99 +0,0 @@
/*
Copyright 2018 The Kubernetes Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package transformers
import (
"errors"
"fmt"
"sigs.k8s.io/kustomize/pkg/gvk"
"sigs.k8s.io/kustomize/pkg/resmap"
"sigs.k8s.io/kustomize/pkg/transformers/config"
)
// namePrefixTransformer contains the prefix and the FieldSpecs
// for each field needing a name prefix.
type namePrefixTransformer struct {
prefix string
fieldSpecsToUse []config.FieldSpec
fieldSpecsToSkip []config.FieldSpec
}
var _ Transformer = &namePrefixTransformer{}
var prefixFieldSpecsToSkip = []config.FieldSpec{
{
Gvk: gvk.Gvk{Kind: "CustomResourceDefinition"},
},
{
Gvk: gvk.Gvk{Kind: "Namespace"},
},
}
// NewNamePrefixTransformer construct a namePrefixTransformer.
func NewNamePrefixTransformer(np string, pc []config.FieldSpec) (Transformer, error) {
if len(np) == 0 {
return NewNoOpTransformer(), nil
}
if pc == nil {
return nil, errors.New("fieldSpecs is not expected to be nil")
}
return &namePrefixTransformer{fieldSpecsToUse: pc, prefix: np, fieldSpecsToSkip: prefixFieldSpecsToSkip}, nil
}
// Transform prepends the name prefix.
func (o *namePrefixTransformer) Transform(m resmap.ResMap) error {
mf := resmap.ResMap{}
for id := range m {
found := false
for _, path := range o.fieldSpecsToSkip {
if id.Gvk().IsSelected(&path.Gvk) {
found = true
break
}
}
if !found {
mf[id] = m[id]
delete(m, id)
}
}
for id := range mf {
objMap := mf[id].Map()
for _, path := range o.fieldSpecsToUse {
if !id.Gvk().IsSelected(&path.Gvk) {
continue
}
err := mutateField(objMap, path.PathSlice(), path.CreateIfNotPresent, o.addPrefix)
if err != nil {
return err
}
newId := id.CopyWithNewPrefix(o.prefix)
m[newId] = mf[id]
}
}
return nil
}
func (o *namePrefixTransformer) addPrefix(in interface{}) (interface{}, error) {
s, ok := in.(string)
if !ok {
return nil, fmt.Errorf("%#v is expectd to be %T", in, s)
}
return o.prefix + s, nil
}

View File

@@ -0,0 +1,108 @@
/*
Copyright 2018 The Kubernetes Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package transformers
import (
"errors"
"fmt"
"log"
"sigs.k8s.io/kustomize/pkg/gvk"
"sigs.k8s.io/kustomize/pkg/resmap"
"sigs.k8s.io/kustomize/pkg/transformers/config"
)
// namePrefixSuffixTransformer contains the prefix, suffix, and the FieldSpecs
// for each field needing a name prefix and suffix.
type namePrefixSuffixTransformer struct {
prefix string
suffix string
fieldSpecsToUse []config.FieldSpec
fieldSpecsToSkip []config.FieldSpec
}
var _ Transformer = &namePrefixSuffixTransformer{}
var prefixSuffixFieldSpecsToSkip = []config.FieldSpec{
{
Gvk: gvk.Gvk{Kind: "CustomResourceDefinition"},
},
}
// deprecateNamePrefixSuffixFieldSpec will be moved into prefixSuffixFieldSpecsToSkip in next release
var deprecateNamePrefixSuffixFieldSpec = config.FieldSpec{
Gvk: gvk.Gvk{Kind: "Namespace"},
}
// NewNamePrefixSuffixTransformer construct a namePrefixSuffixTransformer.
func NewNamePrefixSuffixTransformer(np, ns string, pc []config.FieldSpec) (Transformer, error) {
if len(np) == 0 && len(ns) == 0 {
return NewNoOpTransformer(), nil
}
if pc == nil {
return nil, errors.New("fieldSpecs is not expected to be nil")
}
return &namePrefixSuffixTransformer{fieldSpecsToUse: pc, prefix: np, suffix: ns, fieldSpecsToSkip: prefixSuffixFieldSpecsToSkip}, nil
}
// Transform prepends the name prefix and appends the name suffix.
func (o *namePrefixSuffixTransformer) Transform(m resmap.ResMap) error {
// Fill map "mf" with entries subject to name modification, and
// delete these entries from "m", so that for now m retains only
// the entries whose names will not be modified.
mf := resmap.ResMap{}
for id := range m {
found := false
for _, path := range o.fieldSpecsToSkip {
if id.Gvk().IsSelected(&path.Gvk) {
found = true
break
}
}
if !found {
mf[id] = m[id]
delete(m, id)
}
}
for id := range mf {
if id.Gvk().IsSelected(&deprecateNamePrefixSuffixFieldSpec.Gvk) {
log.Println("Adding nameprefix and namesuffix to Namespace resource will be deprecated in next release.")
}
objMap := mf[id].Map()
for _, path := range o.fieldSpecsToUse {
if !id.Gvk().IsSelected(&path.Gvk) {
continue
}
err := mutateField(objMap, path.PathSlice(), path.CreateIfNotPresent, o.addPrefixSuffix)
if err != nil {
return err
}
newId := id.CopyWithNewPrefixSuffix(o.prefix, o.suffix)
m[newId] = mf[id]
}
}
return nil
}
func (o *namePrefixSuffixTransformer) addPrefixSuffix(in interface{}) (interface{}, error) {
s, ok := in.(string)
if !ok {
return nil, fmt.Errorf("%#v is expected to be %T", in, s)
}
return fmt.Sprintf("%s%s%s", o.prefix, s, o.suffix), nil
}

View File

@@ -20,13 +20,13 @@ import (
"reflect"
"testing"
"sigs.k8s.io/kustomize/internal/k8sdeps/kunstruct"
"sigs.k8s.io/kustomize/k8sdeps/kunstruct"
"sigs.k8s.io/kustomize/pkg/resid"
"sigs.k8s.io/kustomize/pkg/resmap"
"sigs.k8s.io/kustomize/pkg/resource"
)
func TestPrefixNameRun(t *testing.T) {
func TestPrefixSuffixNameRun(t *testing.T) {
rf := resource.NewFactory(
kunstruct.NewKunstructuredFactoryImpl())
m := resmap.ResMap{
@@ -54,30 +54,22 @@ func TestPrefixNameRun(t *testing.T) {
"name": "crd",
},
}),
resid.NewResId(ns, "ns"): rf.FromMap(
map[string]interface{}{
"apiVersion": "v1",
"kind": "Namespace",
"metadata": map[string]interface{}{
"name": "ns",
},
}),
}
expected := resmap.ResMap{
resid.NewResIdWithPrefix(cmap, "cm1", "someprefix-"): rf.FromMap(
resid.NewResIdWithPrefixSuffix(cmap, "cm1", "someprefix-", "-somesuffix"): rf.FromMap(
map[string]interface{}{
"apiVersion": "v1",
"kind": "ConfigMap",
"metadata": map[string]interface{}{
"name": "someprefix-cm1",
"name": "someprefix-cm1-somesuffix",
},
}),
resid.NewResIdWithPrefix(cmap, "cm2", "someprefix-"): rf.FromMap(
resid.NewResIdWithPrefixSuffix(cmap, "cm2", "someprefix-", "-somesuffix"): rf.FromMap(
map[string]interface{}{
"apiVersion": "v1",
"kind": "ConfigMap",
"metadata": map[string]interface{}{
"name": "someprefix-cm2",
"name": "someprefix-cm2-somesuffix",
},
}),
resid.NewResId(crd, "crd"): rf.FromMap(
@@ -88,22 +80,14 @@ func TestPrefixNameRun(t *testing.T) {
"name": "crd",
},
}),
resid.NewResId(ns, "ns"): rf.FromMap(
map[string]interface{}{
"apiVersion": "v1",
"kind": "Namespace",
"metadata": map[string]interface{}{
"name": "ns",
},
}),
}
npt, err := NewNamePrefixTransformer(
"someprefix-", defaultTransformerConfig.NamePrefix)
npst, err := NewNamePrefixSuffixTransformer(
"someprefix-", "-somesuffix", defaultTransformerConfig.NamePrefix)
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
err = npt.Transform(m)
err = npst.Transform(m)
if err != nil {
t.Fatalf("unexpected error: %v", err)
}

Some files were not shown because too many files have changed in this diff Show More