mirror of
https://github.com/kubernetes-sigs/kustomize.git
synced 2026-06-29 17:41:13 +00:00
release cmd/config 0.0.6
Merge remote-tracking branch 'upstream/master' into release-cmd/config-v0.0
This commit is contained in:
@@ -78,7 +78,10 @@ func NewConfigCommand(name string) *cobra.Command {
|
||||
root.AddCommand(commands.CountCommand(name))
|
||||
root.AddCommand(commands.RunFnCommand(name))
|
||||
root.AddCommand(commands.SetCommand(name))
|
||||
root.AddCommand(commands.ListSettersCommand(name))
|
||||
root.AddCommand(commands.CreateSetterCommand(name))
|
||||
root.AddCommand(commands.SinkCommand(name))
|
||||
root.AddCommand(commands.SourceCommand(name))
|
||||
|
||||
root.AddCommand(&cobra.Command{
|
||||
Use: "docs-merge",
|
||||
@@ -92,8 +95,13 @@ func NewConfigCommand(name string) *cobra.Command {
|
||||
})
|
||||
root.AddCommand(&cobra.Command{
|
||||
Use: "docs-fn",
|
||||
Short: "[Alpha] Documentation for writing containerized functions run by run.",
|
||||
Long: api.ConfigFnLong,
|
||||
Short: "[Alpha] Documentation for developing and invoking Configuration Functions.",
|
||||
Long: api.FunctionsImplLong,
|
||||
})
|
||||
root.AddCommand(&cobra.Command{
|
||||
Use: "docs-fn-spec",
|
||||
Short: "[Alpha] Documentation for Configuration Functions Specification.",
|
||||
Long: api.FunctionsSpecLong,
|
||||
})
|
||||
root.AddCommand(&cobra.Command{
|
||||
Use: "docs-io-annotations",
|
||||
|
||||
@@ -1,281 +0,0 @@
|
||||
# Configuration Functions API Semantics
|
||||
|
||||
Configuration Functions are functions packaged as executables in containers which enable
|
||||
**shift-left practices**. They configure applications and infrastructure through
|
||||
Kubernetes style Resource Configuration, but run locally pre-commit.
|
||||
|
||||
Configuration functions enable shift-left practices (client-side) through:
|
||||
|
||||
- Pre-commit / delivery validation and linting of configuration
|
||||
- e.g. Fail if any containers don't have PodSecurityPolicy or CPU / Memory limits
|
||||
- Implementation of abstractions as client actuated APIs (e.g. templating)
|
||||
- e.g. Create a client-side *"CRD"* for generating configuration checked into git
|
||||
- Aspect Orient configuration / Injection of cross-cutting configuration
|
||||
- e.g. T-Shirt size containers by annotating Resources with `small`, `medium`, `large`
|
||||
and inject the cpu and memory resources into containers accordingly.
|
||||
- e.g. Inject `init` and `side-car` containers into Resources based off of Resource
|
||||
Type, annotations, etc.
|
||||
|
||||
Performing these on the client rather than the server enables:
|
||||
|
||||
- Configuration to be reviewed prior to being sent to the API server
|
||||
- Configuration to be validated as part of the CD pipeline
|
||||
- Configuration for Resources to validated holistically rather than individually
|
||||
per-Resource -- e.g. ensure the `Service.selector` and `Deployment.spec.template` labels
|
||||
match.
|
||||
- MutatingWebHooks are scoped to a single Resource instance at a time.
|
||||
- Low-level tweaks to the output of high-level abstractions -- e.g. add an `init container`
|
||||
to a client *"CRD"* Resource after it was generated.
|
||||
- Composition and layering of multiple functions together
|
||||
- Compose generation, injection, validation together
|
||||
|
||||
Configuration Functions are implemented as executable programs published in containers which:
|
||||
|
||||
- Accept as input (stdin):
|
||||
- A list of Resource Configuration
|
||||
- A Function Configuration (to configure the function itself)
|
||||
- Emit as output (stdout + exit):
|
||||
- A list of Resource Configuration
|
||||
- An exit code for success / failure
|
||||
|
||||
### Function Specification
|
||||
|
||||
- Functions **SHOULD** be published as container images containing a `CMD` invoking an executable.
|
||||
- Functions **MUST** accept input on STDIN a `ResourceList` containing the Resources and
|
||||
`functionConfig`.
|
||||
- Functions **MUST** emit output on STDOUT a `ResourceList` containing the modified
|
||||
Resources.
|
||||
- Functions **MUST** exit non-0 on failure, and exit 0 on success.
|
||||
- Functions **MAY** emit output on STDERR with error messaging.
|
||||
- Functions performing validation **SHOULD** exit failure and emit error messaging
|
||||
on a validation failure.
|
||||
- Functions generating Resources **SHOULD** retain non-conflicting changes on the
|
||||
generated Resources -- e.g. 1. the function generates a Deployment, but doesn't
|
||||
specify `cpu`, 2. the user sets the `cpu` on the generated Resource, 3. the
|
||||
function should keep the `cpu` when regenerating the Resource a second time.
|
||||
- Functions **SHOULD** be usable outside `kustomize config run` -- e.g. though pipeline
|
||||
mechanisms such as Tekton.
|
||||
|
||||
#### Input Format
|
||||
|
||||
Functions must accept on STDIN:
|
||||
|
||||
`ResourceList`:
|
||||
- contains `items` field, same as `List.items`
|
||||
- contains `functionConfig` field -- a single item with the configuration for the function itself
|
||||
|
||||
Example `ResourceList` Input:
|
||||
|
||||
apiVersion: config.kubernetes.io/v1alpha1
|
||||
kind: ResourceList
|
||||
functionConfig:
|
||||
apiVersion: example.com/v1beta1
|
||||
kind: Nginx
|
||||
metadata:
|
||||
name: my-instance
|
||||
annotations:
|
||||
config.kubernetes.io/local-config: "true"
|
||||
spec:
|
||||
replicas: 5
|
||||
items:
|
||||
- apiVersion: apps/v1
|
||||
kind: Deployment
|
||||
metadata:
|
||||
name: my-instance
|
||||
spec:
|
||||
replicas: 3
|
||||
...
|
||||
- apiVersion: v1
|
||||
kind: Service
|
||||
metadata:
|
||||
name: my-instance
|
||||
spec:
|
||||
...
|
||||
|
||||
#### Output Format
|
||||
|
||||
Functions must emit on STDOUT:
|
||||
|
||||
`ResourceList`:
|
||||
- contains `items` field, same as `List.items`
|
||||
|
||||
Example `ResourceList` Output:
|
||||
|
||||
apiVersion: config.kubernetes.io/v1alpha1
|
||||
kind: ResourceList
|
||||
items:
|
||||
- apiVersion: apps/v1
|
||||
kind: Deployment
|
||||
metadata:
|
||||
name: my-instance
|
||||
spec:
|
||||
replicas: 5
|
||||
...
|
||||
- apiVersion: v1
|
||||
kind: Service
|
||||
metadata:
|
||||
name: my-instance
|
||||
spec:
|
||||
...
|
||||
|
||||
#### Container Environment
|
||||
|
||||
When run by `kustomize config run`, functions are run in containers with the
|
||||
following environment:
|
||||
|
||||
- Network: `none`
|
||||
- User: `nobody`
|
||||
- Security Options: `no-new-privileges`
|
||||
- Volumes: the volume containing the `functionConfig` yaml is mounted under `/local` as `ro`
|
||||
|
||||
### Example Function Implementation
|
||||
|
||||
Following is an example for implementing an nginx abstraction using a config
|
||||
function.
|
||||
|
||||
#### `nginx-template.sh`
|
||||
|
||||
`nginx-template.sh` is a simple bash script which uses a *heredoc* as a templating solution
|
||||
for generating Resources from the functionConfig input fields.
|
||||
|
||||
The script wraps itself using `config run wrap -- $0` which will:
|
||||
|
||||
1. Parse the `ResourceList.functionConfig` (provided to the container stdin) into env vars
|
||||
2. Merge the stdout into the original list of Resources
|
||||
3. Defaults filenames for newly generated Resources (if they are not set as annotations)
|
||||
to `config/NAME_KIND.yaml`
|
||||
4. Format the output
|
||||
|
||||
#!/bin/bash
|
||||
# script must run wrapped by `kustomize config run wrap`
|
||||
# for parsing input the functionConfig into env vars
|
||||
if [ -z ${WRAPPED} ]; then
|
||||
export WRAPPED=true
|
||||
config run wrap -- $0
|
||||
exit $?
|
||||
fi
|
||||
|
||||
cat <<End-of-message
|
||||
apiVersion: v1
|
||||
kind: Service
|
||||
metadata:
|
||||
name: ${NAME}
|
||||
labels:
|
||||
app: nginx
|
||||
instance: ${NAME}
|
||||
spec:
|
||||
ports:
|
||||
- port: 80
|
||||
targetPort: 80
|
||||
name: http
|
||||
selector:
|
||||
app: nginx
|
||||
instance: ${NAME}
|
||||
---
|
||||
apiVersion: apps/v1
|
||||
kind: Deployment
|
||||
metadata:
|
||||
name: ${NAME}
|
||||
labels:
|
||||
app: nginx
|
||||
instance: ${NAME}
|
||||
spec:
|
||||
replicas: ${REPLICAS}
|
||||
selector:
|
||||
matchLabels:
|
||||
app: nginx
|
||||
instance: ${NAME}
|
||||
template:
|
||||
metadata:
|
||||
labels:
|
||||
app: nginx
|
||||
instance: ${NAME}
|
||||
spec:
|
||||
containers:
|
||||
- name: nginx
|
||||
image: nginx:1.7.9
|
||||
ports:
|
||||
- containerPort: 80
|
||||
End-of-message
|
||||
|
||||
#### `Dockerfile`
|
||||
|
||||
`Dockerfile` installs `kustomize config` and copies the script into the container image.
|
||||
|
||||
FROM golang:1.13-stretch
|
||||
RUN go get sigs.k8s.io/kustomize/cmd/config
|
||||
RUN mv /go/bin/config /usr/bin/config
|
||||
COPY nginx-template.sh /usr/bin/nginx-template.sh
|
||||
CMD ["nginx-template.sh]
|
||||
|
||||
### Example Function Usage
|
||||
|
||||
Following is an example of running the `kustomize config run` using the preceding API.
|
||||
|
||||
#### `nginx.yaml` (Input)
|
||||
|
||||
`dir/nginx.yaml` contains a reference to the Function. The contents of `nginx.yaml`
|
||||
are passed to the Function through the `ResourceList.functionConfig` field.
|
||||
|
||||
apiVersion: example.com/v1beta1
|
||||
kind: Nginx
|
||||
metadata:
|
||||
name: my-instance
|
||||
annotations:
|
||||
config.kubernetes.io/local-config: "true"
|
||||
configFn:
|
||||
container:
|
||||
image: gcr.io/example-functions/nginx-template:v1.0.0
|
||||
spec:
|
||||
replicas: 5
|
||||
|
||||
- `configFn.container.image`: the image to use for this API
|
||||
- `annotations[config.kubernetes.io/local-config]`: mark this as not a Resource that should
|
||||
be applied
|
||||
|
||||
#### `kustomize config run dir/` (Output)
|
||||
|
||||
`dir/my-instance_deployment.yaml` contains the Deployment:
|
||||
|
||||
apiVersion: apps/v1
|
||||
kind: Deployment
|
||||
metadata:
|
||||
name: my-instance
|
||||
labels:
|
||||
app: nginx
|
||||
instance: my-instance
|
||||
spec:
|
||||
replicas: 5
|
||||
selector:
|
||||
matchLabels:
|
||||
app: nginx
|
||||
instance: my-instance
|
||||
template:
|
||||
metadata:
|
||||
labels:
|
||||
app: nginx
|
||||
instance: my-instance
|
||||
spec:
|
||||
containers:
|
||||
- name: nginx
|
||||
image: nginx:1.7.9
|
||||
ports:
|
||||
- containerPort: 80
|
||||
|
||||
`dir/my-instance_service.yaml` contains the Service:
|
||||
|
||||
apiVersion: v1
|
||||
kind: Service
|
||||
metadata:
|
||||
name: my-instance
|
||||
labels:
|
||||
app: nginx
|
||||
instance: my-instance
|
||||
spec:
|
||||
ports:
|
||||
- port: 80
|
||||
targetPort: 80
|
||||
name: http
|
||||
selector:
|
||||
app: nginx
|
||||
instance: my-instance
|
||||
181
cmd/config/docs/api-conventions/functions-impl.md
Normal file
181
cmd/config/docs/api-conventions/functions-impl.md
Normal file
@@ -0,0 +1,181 @@
|
||||
# Running Configuration Functions using kustomize CLI
|
||||
|
||||
Configuration functions can be implemented using any toolchain and invoked using any
|
||||
container workflow orchestrator including Tekton, Cloud Build, or run directly using `docker run`.
|
||||
|
||||
Run `config help docs-fn-spec` to see the Configuration Functions Specification.
|
||||
|
||||
`kustomize config run` is an example orchestrator for invoking Configuration Functions. This
|
||||
document describes how to implement and invoke an example function.
|
||||
|
||||
## Example Function Implementation
|
||||
|
||||
Following is an example for implementing an nginx abstraction using a configuration
|
||||
function.
|
||||
|
||||
### `nginx-template.sh`
|
||||
|
||||
`nginx-template.sh` is a simple bash script which uses a _heredoc_ as a templating solution
|
||||
for generating Resources from the functionConfig input fields.
|
||||
|
||||
The script wraps itself using `config run wrap -- $0` which will:
|
||||
|
||||
1. Parse the `ResourceList.functionConfig` (provided to the container stdin) into env vars
|
||||
2. Merge the stdout into the original list of Resources
|
||||
3. Defaults filenames for newly generated Resources (if they are not set as annotations)
|
||||
to `config/NAME_KIND.yaml`
|
||||
4. Format the output
|
||||
|
||||
```bash
|
||||
#!/bin/bash
|
||||
# script must run wrapped by "kustomize config run wrap"
|
||||
# for parsing input the functionConfig into env vars
|
||||
if [ -z ${WRAPPED} ]; then
|
||||
export WRAPPED=true
|
||||
config run wrap -- $0
|
||||
exit $?
|
||||
fi
|
||||
|
||||
cat <<End-of-message
|
||||
apiVersion: v1
|
||||
kind: Service
|
||||
metadata:
|
||||
name: ${NAME}
|
||||
labels:
|
||||
app: nginx
|
||||
instance: ${NAME}
|
||||
spec:
|
||||
ports:
|
||||
- port: 80
|
||||
targetPort: 80
|
||||
name: http
|
||||
selector:
|
||||
app: nginx
|
||||
instance: ${NAME}
|
||||
---
|
||||
apiVersion: apps/v1
|
||||
kind: Deployment
|
||||
metadata:
|
||||
name: ${NAME}
|
||||
labels:
|
||||
app: nginx
|
||||
instance: ${NAME}
|
||||
spec:
|
||||
replicas: ${REPLICAS}
|
||||
selector:
|
||||
matchLabels:
|
||||
app: nginx
|
||||
instance: ${NAME}
|
||||
template:
|
||||
metadata:
|
||||
labels:
|
||||
app: nginx
|
||||
instance: ${NAME}
|
||||
spec:
|
||||
containers:
|
||||
- name: nginx
|
||||
image: nginx:1.7.9
|
||||
ports:
|
||||
- containerPort: 80
|
||||
End-of-message
|
||||
```
|
||||
|
||||
### Dockerfile
|
||||
|
||||
`Dockerfile` installs `kustomize config` and copies the script into the container image.
|
||||
|
||||
```
|
||||
FROM golang:1.13-stretch
|
||||
RUN go get sigs.k8s.io/kustomize/cmd/config
|
||||
RUN mv /go/bin/config /usr/bin/config
|
||||
COPY nginx-template.sh /usr/bin/nginx-template.sh
|
||||
CMD ["nginx-template.sh]
|
||||
```
|
||||
|
||||
## Example Function Usage
|
||||
|
||||
Following is an example of running the `kustomize config run` using the preceding API.
|
||||
|
||||
When run by `kustomize config run`, functions are run in containers with the
|
||||
following environment:
|
||||
|
||||
- Network: `none`
|
||||
- User: `nobody`
|
||||
- Security Options: `no-new-privileges`
|
||||
- Volumes: the volume containing the `functionConfig` yaml is mounted under `/local` as `ro`
|
||||
|
||||
### Input
|
||||
|
||||
`dir/nginx.yaml` contains a reference to the Function. The contents of `nginx.yaml`
|
||||
are passed to the Function through the `ResourceList.functionConfig` field.
|
||||
|
||||
```yaml
|
||||
apiVersion: example.com/v1beta1
|
||||
kind: Nginx
|
||||
metadata:
|
||||
name: my-instance
|
||||
annotations:
|
||||
config.kubernetes.io/local-config: "true"
|
||||
config.k8s.io/function: |
|
||||
container:
|
||||
image: gcr.io/example-functions/nginx-template:v1.0.0
|
||||
spec:
|
||||
replicas: 5
|
||||
```
|
||||
|
||||
- `annotations[config.k8s.io/function].container.image`: the image to use for this API
|
||||
- `annotations[config.kubernetes.io/local-config]`: mark this as not a Resource that should
|
||||
be applied
|
||||
|
||||
### Output
|
||||
|
||||
The function is invoked using by runing `kustomize config run dir/`.
|
||||
|
||||
`dir/my-instance_deployment.yaml` contains the Deployment:
|
||||
|
||||
```yaml
|
||||
apiVersion: apps/v1
|
||||
kind: Deployment
|
||||
metadata:
|
||||
name: my-instance
|
||||
labels:
|
||||
app: nginx
|
||||
instance: my-instance
|
||||
spec:
|
||||
replicas: 5
|
||||
selector:
|
||||
matchLabels:
|
||||
app: nginx
|
||||
instance: my-instance
|
||||
template:
|
||||
metadata:
|
||||
labels:
|
||||
app: nginx
|
||||
instance: my-instance
|
||||
spec:
|
||||
containers:
|
||||
- name: nginx
|
||||
image: nginx:1.7.9
|
||||
ports:
|
||||
- containerPort: 80
|
||||
```
|
||||
|
||||
`dir/my-instance_service.yaml` contains the Service:
|
||||
|
||||
```yaml
|
||||
apiVersion: v1
|
||||
kind: Service
|
||||
metadata:
|
||||
name: my-instance
|
||||
labels:
|
||||
app: nginx
|
||||
instance: my-instance
|
||||
spec:
|
||||
ports:
|
||||
- port: 80
|
||||
targetPort: 80
|
||||
name: http
|
||||
selector:
|
||||
app: nginx
|
||||
instance: my-instance
|
||||
```
|
||||
186
cmd/config/docs/api-conventions/functions-spec.md
Normal file
186
cmd/config/docs/api-conventions/functions-spec.md
Normal file
@@ -0,0 +1,186 @@
|
||||
# Configuration Functions Specification
|
||||
|
||||
This document specifies a standard for client-side functions that operate on
|
||||
Kubernetes declarative configurations. This standard enables creating
|
||||
small, interoperable, and language-independent executable programs packaged as
|
||||
containers that can be chained together as part of a configuration management pipeline.
|
||||
The end result of such a pipeline are fully rendered configurations that can then be
|
||||
applied to a control plane (e.g. Using ‘kubectl apply’ for Kubernetes control plane).
|
||||
As such, although this document references Kubernetes Resource Model and API conventions,
|
||||
it is completely decoupled from Kuberentes API machinery and does not depend on any
|
||||
in-cluster components.
|
||||
|
||||
This document references terms described in [Kubernetes API Conventions][1].
|
||||
|
||||
The key words "MUST", "MUST NOT", "REQUIRED", "SHALL", "SHALL NOT", "SHOULD",
|
||||
"SHOULD NOT", "RECOMMENDED", "MAY", and "OPTIONAL" in this document are to be
|
||||
interpreted as described in [RFC 2119][2].
|
||||
|
||||
## Use Cases
|
||||
|
||||
_Configuration functions_ enable shift-left practices (client-side) through:
|
||||
|
||||
- Pre-commit / delivery validation and linting of configuration
|
||||
- e.g. Fail if any containers don't have PodSecurityPolicy or CPU / Memory limits
|
||||
- Implementation of abstractions as client actuated APIs (e.g. templating)
|
||||
- e.g. Create a client-side _"CRD"_ for generating configuration checked into git
|
||||
- Aspect Orient configuration / Injection of cross-cutting configuration
|
||||
- e.g. T-Shirt size containers by annotating Resources with `small`, `medium`, `large`
|
||||
and inject the cpu and memory resources into containers accordingly.
|
||||
- e.g. Inject `init` and `side-car` containers into Resources based off of Resource
|
||||
Type, annotations, etc.
|
||||
|
||||
Performing these on the client rather than the server enables:
|
||||
|
||||
- Configuration to be reviewed prior to being sent to the API server
|
||||
- Configuration to be validated as part of the CI?CD pipeline
|
||||
- Configuration for Resources to validated holistically rather than individually
|
||||
per-Resource
|
||||
- e.g. ensure the `Service.selector` and `Deployment.spec.template` labels
|
||||
match.
|
||||
- e.g. MutatingWebHooks are scoped to a single Resource instance at a time.
|
||||
- Low-level tweaks to the output of high-level abstractions
|
||||
- e.g. add an `init container` to a client _"CRD"_ Resource after it was generated.
|
||||
- Composition and layering of multiple functions together
|
||||
- Compose generation, injection, validation together
|
||||
|
||||
## Spec
|
||||
|
||||
### Input Type
|
||||
|
||||
A function MUST accept as input a single [Kubernetes List type][3].
|
||||
The `items` field in the input will contain a sequence of [Object types][3].
|
||||
A function MAY not support [Simple types][3] and List types.
|
||||
|
||||
An example using `v1/ConfigMapList` as input:
|
||||
|
||||
```yaml
|
||||
apiVersion: v1
|
||||
kind: ConfigMapList
|
||||
items:
|
||||
- apiVersion: v1
|
||||
kind: ConfigMap
|
||||
metadata:
|
||||
name: config1
|
||||
data:
|
||||
p1: v1
|
||||
p2: v2
|
||||
- apiVersion: v1
|
||||
kind: ConfigMap
|
||||
metadata:
|
||||
name: config2
|
||||
```
|
||||
|
||||
An example using `v1/List` as input:
|
||||
|
||||
```yaml
|
||||
apiVersion: v1
|
||||
kind: List
|
||||
items:
|
||||
spec:
|
||||
- apiVersion: foo-corp.com/v1
|
||||
kind: FulfillmentCenter
|
||||
metadata:
|
||||
name: staging
|
||||
address: "100 Main St."
|
||||
- apiVersion: rbac.authorization.k8s.io/v1
|
||||
kind: ClusterRole
|
||||
metadata:
|
||||
name: namespace-reader
|
||||
rules:
|
||||
- resources:
|
||||
- namespaces
|
||||
apiGroups:
|
||||
- ""
|
||||
verbs:
|
||||
- get
|
||||
- watch
|
||||
- list
|
||||
```
|
||||
|
||||
In addition, a function MUST accept as input a List of kind `ResourceList` where the
|
||||
`functionConfig` field, if present, will contain the invocation-specific configuration passed to the function
|
||||
by the orchestrator.
|
||||
Functions MAY consider this field optional so that they can be triggered in an ad-hoc fashion.
|
||||
|
||||
An example using `config.kubernetes.io/v1beta1/ResourceList` as input:
|
||||
|
||||
```yaml
|
||||
apiVersion: config.kubernetes.io/v1beta1
|
||||
kind: ResourceList
|
||||
functionConfig:
|
||||
apiVersion: foo-corp.com/v1
|
||||
kind: FulfillmentCenter
|
||||
metadata:
|
||||
name: staging
|
||||
metadata:
|
||||
annotations:
|
||||
config.k8s.io/function: |
|
||||
container:
|
||||
image: gcr.io/example/foo:v1.0.0
|
||||
spec:
|
||||
address: "100 Main St."
|
||||
items:
|
||||
- apiVersion: rbac.authorization.k8s.io/v1
|
||||
kind: ClusterRole
|
||||
metadata:
|
||||
name: namespace-reader
|
||||
rules:
|
||||
- resources:
|
||||
- namespaces
|
||||
apiGroups:
|
||||
- ""
|
||||
verbs:
|
||||
- get
|
||||
- watch
|
||||
- list
|
||||
```
|
||||
|
||||
Here `FulfillmentCenter` kind with name `staging` is passed as the invocation-specific configuration
|
||||
to the function.
|
||||
|
||||
### Output Type
|
||||
|
||||
A function’s output MUST be the same as the input specification above
|
||||
-- i.e. `ResourceList` or `List`.
|
||||
This is necessary to enable chaining two or more functions together in a pipeline.
|
||||
The serialization format of the output SHOULD match that of its input on each invocation
|
||||
-- e.g. if the input was a `ResourceList`, the output should also be a `ResourceList`.
|
||||
|
||||
### Serialization Format
|
||||
|
||||
A function MUST support YAML as a serialization format for the input and output.
|
||||
A function MUST use utf8 encoding (as YAML is a superset of JSON, JSON will also be supported
|
||||
by any conforming function).
|
||||
|
||||
### Operations
|
||||
|
||||
A function MAY Create, Update, or Delete any number of items in the `items` field and output the
|
||||
resultant list.
|
||||
|
||||
A function MAY modify annotations with prefix `config.kubernetes.io`, but must be careful about
|
||||
doing so since they’re used for orchestration purposes and will likely impact subsequent functions
|
||||
in the pipeline.
|
||||
|
||||
A function SHOULD preserve comments when input serialization format is YAML.
|
||||
This allows for human authoring of configuration to coexist with changes made by functions.
|
||||
|
||||
### Containerization
|
||||
|
||||
A function MUST be implemented as a container.
|
||||
|
||||
A function container MUST be capable of running as a non-root user if it does not require
|
||||
access to host filesystem or makes network calls.
|
||||
|
||||
### stdin/stdout/stderr and Exit Codes
|
||||
|
||||
A function MUST accept input from stdin and emit output to stdout.
|
||||
|
||||
Any error messages MUST be emitted to stderr.
|
||||
|
||||
An exit code of zero indicates function execution was successful.
|
||||
A non-zero exit code indicates a failure.
|
||||
|
||||
[1]: https://github.com/kubernetes/community/blob/master/contributors/devel/sig-architecture/api-conventions.md
|
||||
[2]: https://tools.ietf.org/html/rfc2119
|
||||
[3]: https://github.com/kubernetes/community/blob/master/contributors/devel/sig-architecture/api-conventions.md#types-kinds
|
||||
23
cmd/config/docs/commands/list-setters.md
Normal file
23
cmd/config/docs/commands/list-setters.md
Normal file
@@ -0,0 +1,23 @@
|
||||
## set
|
||||
|
||||
[Alpha] List setters for Resources.
|
||||
|
||||
### Synopsis
|
||||
|
||||
List setters for Resources.
|
||||
|
||||
DIR
|
||||
|
||||
A directory containing Resource configuration.
|
||||
|
||||
NAME
|
||||
|
||||
Optional. The name of the setter to display.
|
||||
|
||||
### Examples
|
||||
|
||||
Show setters:
|
||||
|
||||
$ config set DIR/
|
||||
NAME DESCRIPTION VALUE TYPE COUNT SETBY
|
||||
name-prefix '' PREFIX string 2
|
||||
@@ -22,8 +22,8 @@ order they appear in the file).
|
||||
|
||||
#### Config Functions:
|
||||
|
||||
Config functions are specified as Kubernetes types containing a metadata.configFn.container.image
|
||||
field. This field tells run how to invoke the container.
|
||||
Config functions are specified as Kubernetes types containing a metadata.annotations.[config.kubernetes.io/function]
|
||||
field specifying an image for the container to run. This image tells run how to invoke the container.
|
||||
|
||||
Example config function:
|
||||
|
||||
@@ -31,17 +31,17 @@ order they appear in the file).
|
||||
apiVersion: fn.example.com/v1beta1
|
||||
kind: ExampleFunctionKind
|
||||
metadata:
|
||||
configFn:
|
||||
container:
|
||||
# function is invoked as a container running this image
|
||||
image: gcr.io/example/examplefunction:v1.0.1
|
||||
annotations:
|
||||
config.kubernetes.io/function: |
|
||||
container:
|
||||
# function is invoked as a container running this image
|
||||
image: gcr.io/example/examplefunction:v1.0.1
|
||||
config.kubernetes.io/local-config: "true" # tools should ignore this
|
||||
spec:
|
||||
configField: configValue
|
||||
|
||||
In the preceding example, 'kustomize config run example/' would identify the function by
|
||||
the metadata.configFn field. It would then write all Resources in the directory to
|
||||
the metadata.annotations.[config.kubernetes.io/function] field. It would then write all Resources in the directory to
|
||||
a container stdin (running the gcr.io/example/examplefunction:v1.0.1 image). It
|
||||
would then write the container stdout back to example/, replacing the directory
|
||||
file contents.
|
||||
|
||||
@@ -59,19 +59,19 @@ To create a custom setter for a field see: `kustomize help config create-setter`
|
||||
List setters: Show the possible setters
|
||||
|
||||
$ config set DIR/
|
||||
NAME DESCRIPTION VALUE TYPE COUNT OWNER
|
||||
NAME DESCRIPTION VALUE TYPE COUNT SETBY
|
||||
name-prefix '' PREFIX string 2
|
||||
|
||||
Perform substitution: set a new value, owner and description
|
||||
Perform set: set a new value, owner and description
|
||||
|
||||
$ kustomize config set DIR/ name-prefix "test" --description "test environment" --set-by "dev"
|
||||
performed 2 substitutions
|
||||
set 2 values
|
||||
|
||||
Show substitutions: Show the new values
|
||||
List setters: Show the new values
|
||||
|
||||
$ config set dir
|
||||
NAME DESCRIPTION VALUE TYPE COUNT SUBSTITUTED OWNER
|
||||
prefix 'test environment' test string 2 true dev
|
||||
$ config set DIR/
|
||||
NAME DESCRIPTION VALUE TYPE COUNT SETBY
|
||||
name-prefix 'test environment' test string 2 dev
|
||||
|
||||
New Resource YAML:
|
||||
|
||||
|
||||
18
cmd/config/docs/commands/sink.md
Normal file
18
cmd/config/docs/commands/sink.md
Normal file
@@ -0,0 +1,18 @@
|
||||
## sink
|
||||
|
||||
[Alpha] Implement a Sink by writing input to a local directory.
|
||||
|
||||
### Synopsis
|
||||
|
||||
[Alpha] Implement a Sink by writing input to a local directory.
|
||||
|
||||
kustomize config sink DIR
|
||||
|
||||
DIR:
|
||||
Path to local directory.
|
||||
|
||||
`sink` writes its input to a directory
|
||||
|
||||
### Examples
|
||||
|
||||
kustomize config source DIR/ | your-function | kustomize config sink DIR/
|
||||
21
cmd/config/docs/commands/source.md
Normal file
21
cmd/config/docs/commands/source.md
Normal file
@@ -0,0 +1,21 @@
|
||||
## source
|
||||
|
||||
[Alpha] Implement a Source by reading a local directory.
|
||||
|
||||
### Synopsis
|
||||
|
||||
[Alpha] Implement a Source by reading a local directory.
|
||||
|
||||
kustomize config source DIR
|
||||
|
||||
DIR:
|
||||
Path to local directory.
|
||||
|
||||
`source` emits configuration to act as input to a function
|
||||
|
||||
### Examples
|
||||
|
||||
# emity configuration directory as input source to a function
|
||||
kustomize config source DIR/
|
||||
|
||||
kustomize config source DIR/ | your-function | kustomize config sink DIR/
|
||||
@@ -9,7 +9,6 @@ require (
|
||||
github.com/spf13/cobra v0.0.5
|
||||
github.com/spf13/pflag v1.0.5
|
||||
github.com/stretchr/testify v1.4.0
|
||||
gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15 // indirect
|
||||
k8s.io/apimachinery v0.17.0
|
||||
sigs.k8s.io/kustomize/kyaml v0.0.5
|
||||
)
|
||||
|
||||
@@ -101,6 +101,8 @@ github.com/posener/complete/v2 v2.0.1-alpha.12/go.mod h1://JlL91cS2JV7rOl6LVHrRq
|
||||
github.com/posener/script v1.0.4 h1:nSuXW5ZdmFnQIueLB2s0qvs4oNsUloM1Zydzh75v42w=
|
||||
github.com/posener/script v1.0.4/go.mod h1:Rg3ijooqulo05aGLyGsHoLmIOUzHUVK19WVgrYBPU/E=
|
||||
github.com/russross/blackfriday v1.5.2/go.mod h1:JO/DiYxRf+HjHt06OyowR9PTA263kcR/rfWxYHBV53g=
|
||||
github.com/sergi/go-diff v1.1.0 h1:we8PVUC3FE2uYfodKH/nBHMSetSfHDR6scGdBi+erh0=
|
||||
github.com/sergi/go-diff v1.1.0/go.mod h1:STckp+ISIX8hZLjrqAeVduY0gWCT9IjLuqbuNXdaHfM=
|
||||
github.com/spf13/afero v1.1.2/go.mod h1:j4pytiNVoe2o6bmDsKpLACNPDBIoEAkihy7loJ1B0CQ=
|
||||
github.com/spf13/cast v1.3.0/go.mod h1:Qx5cxh0v+4UWYiBimWS+eyWzqEqokIECu5etghLkUJE=
|
||||
github.com/spf13/cobra v0.0.5 h1:f0B+LkLX6DtmRH1isoNA9VTtNUK9K8xYd28JNNfOv/s=
|
||||
|
||||
@@ -121,6 +121,13 @@ func (r *CatRunner) runE(c *cobra.Command, args []string) error {
|
||||
out = o
|
||||
}
|
||||
|
||||
// remove this annotation explicitly, the ByteWriter won't clear it by
|
||||
// default because it doesn't set it
|
||||
clear := []string{"config.kubernetes.io/path"}
|
||||
if r.KeepAnnotations {
|
||||
clear = nil
|
||||
}
|
||||
|
||||
var outputs []kio.Writer
|
||||
outputs = append(outputs, kio.ByteWriter{
|
||||
Writer: out,
|
||||
@@ -129,6 +136,7 @@ func (r *CatRunner) runE(c *cobra.Command, args []string) error {
|
||||
WrappingAPIVersion: r.WrapApiVersion,
|
||||
FunctionConfig: functionConfig,
|
||||
Style: yaml.GetStyle(r.Styles...),
|
||||
ClearAnnotations: clear,
|
||||
})
|
||||
|
||||
return handleError(c, kio.Pipeline{Inputs: inputs, Filters: fltr, Outputs: outputs}.Execute())
|
||||
|
||||
@@ -90,8 +90,6 @@ metadata:
|
||||
name: foo
|
||||
annotations:
|
||||
app: nginx2
|
||||
config.kubernetes.io/package: '.'
|
||||
config.kubernetes.io/path: 'f1.yaml'
|
||||
spec:
|
||||
replicas: 1
|
||||
---
|
||||
@@ -100,8 +98,6 @@ metadata:
|
||||
name: foo
|
||||
annotations:
|
||||
app: nginx
|
||||
config.kubernetes.io/package: '.'
|
||||
config.kubernetes.io/path: 'f1.yaml'
|
||||
spec:
|
||||
selector:
|
||||
app: nginx
|
||||
@@ -114,8 +110,6 @@ metadata:
|
||||
app: nginx
|
||||
annotations:
|
||||
app: nginx
|
||||
config.kubernetes.io/package: '.'
|
||||
config.kubernetes.io/path: 'f2.yaml'
|
||||
spec:
|
||||
replicas: 3
|
||||
`, b.String()) {
|
||||
@@ -196,8 +190,6 @@ metadata:
|
||||
name: foo
|
||||
annotations:
|
||||
app: nginx2
|
||||
config.kubernetes.io/package: '.'
|
||||
config.kubernetes.io/path: 'f1.yaml'
|
||||
spec:
|
||||
replicas: 1
|
||||
---
|
||||
@@ -206,8 +198,6 @@ metadata:
|
||||
name: foo
|
||||
annotations:
|
||||
app: nginx
|
||||
config.kubernetes.io/package: '.'
|
||||
config.kubernetes.io/path: 'f1.yaml'
|
||||
spec:
|
||||
selector:
|
||||
app: nginx
|
||||
@@ -218,8 +208,6 @@ metadata:
|
||||
name: foo
|
||||
annotations:
|
||||
config.kubernetes.io/local-config: "true"
|
||||
config.kubernetes.io/package: '.'
|
||||
config.kubernetes.io/path: 'f2.yaml'
|
||||
configFn:
|
||||
container:
|
||||
image: gcr.io/example/image:version
|
||||
@@ -233,8 +221,6 @@ metadata:
|
||||
name: bar
|
||||
annotations:
|
||||
app: nginx
|
||||
config.kubernetes.io/package: '.'
|
||||
config.kubernetes.io/path: 'f2.yaml'
|
||||
spec:
|
||||
replicas: 3
|
||||
`, b.String()) {
|
||||
@@ -314,8 +300,6 @@ metadata:
|
||||
name: foo
|
||||
annotations:
|
||||
config.kubernetes.io/local-config: "true"
|
||||
config.kubernetes.io/package: '.'
|
||||
config.kubernetes.io/path: 'f2.yaml'
|
||||
configFn:
|
||||
container:
|
||||
image: gcr.io/example/reconciler:v1
|
||||
@@ -414,8 +398,6 @@ metadata:
|
||||
name: foo
|
||||
annotations:
|
||||
app: nginx2
|
||||
config.kubernetes.io/package: '.'
|
||||
config.kubernetes.io/path: 'f1.yaml'
|
||||
spec:
|
||||
replicas: 1
|
||||
---
|
||||
@@ -424,8 +406,6 @@ metadata:
|
||||
name: foo
|
||||
annotations:
|
||||
app: nginx
|
||||
config.kubernetes.io/package: '.'
|
||||
config.kubernetes.io/path: 'f1.yaml'
|
||||
spec:
|
||||
selector:
|
||||
app: nginx
|
||||
@@ -438,8 +418,6 @@ metadata:
|
||||
app: nginx
|
||||
annotations:
|
||||
app: nginx
|
||||
config.kubernetes.io/package: '.'
|
||||
config.kubernetes.io/path: 'f2.yaml'
|
||||
spec:
|
||||
replicas: 3
|
||||
`, string(actual)) {
|
||||
@@ -536,8 +514,6 @@ metadata:
|
||||
name: foo
|
||||
annotations:
|
||||
app: nginx2
|
||||
config.kubernetes.io/package: '.'
|
||||
config.kubernetes.io/path: 'f1.yaml'
|
||||
spec:
|
||||
replicas: 1
|
||||
---
|
||||
@@ -546,8 +522,6 @@ metadata:
|
||||
name: foo
|
||||
annotations:
|
||||
app: nginx
|
||||
config.kubernetes.io/package: '.'
|
||||
config.kubernetes.io/path: 'f1.yaml'
|
||||
spec:
|
||||
selector:
|
||||
app: nginx
|
||||
@@ -560,8 +534,6 @@ metadata:
|
||||
app: nginx
|
||||
annotations:
|
||||
app: nginx
|
||||
config.kubernetes.io/package: '.'
|
||||
config.kubernetes.io/path: 'f2.yaml'
|
||||
spec:
|
||||
replicas: 3
|
||||
`, string(actual)) {
|
||||
|
||||
47
cmd/config/internal/commands/cmdlistsetters.go
Normal file
47
cmd/config/internal/commands/cmdlistsetters.go
Normal file
@@ -0,0 +1,47 @@
|
||||
// Copyright 2019 The Kubernetes Authors.
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
package commands
|
||||
|
||||
import (
|
||||
"github.com/spf13/cobra"
|
||||
"sigs.k8s.io/kustomize/cmd/config/internal/generateddocs/commands"
|
||||
"sigs.k8s.io/kustomize/kyaml/setters"
|
||||
)
|
||||
|
||||
// NewListSettersRunner returns a command runner.
|
||||
func NewListSettersRunner(parent string) *ListSettersRunner {
|
||||
r := &ListSettersRunner{}
|
||||
c := &cobra.Command{
|
||||
Use: "list-setters DIR [NAME]",
|
||||
Args: cobra.RangeArgs(1, 2),
|
||||
Short: commands.ListSettersShort,
|
||||
Long: commands.ListSettersLong,
|
||||
Example: commands.ListSettersExamples,
|
||||
PreRunE: r.preRunE,
|
||||
RunE: r.runE,
|
||||
}
|
||||
fixDocs(parent, c)
|
||||
r.Command = c
|
||||
return r
|
||||
}
|
||||
|
||||
func ListSettersCommand(parent string) *cobra.Command {
|
||||
return NewListSettersRunner(parent).Command
|
||||
}
|
||||
|
||||
type ListSettersRunner struct {
|
||||
Command *cobra.Command
|
||||
Lookup setters.LookupSetters
|
||||
}
|
||||
|
||||
func (r *ListSettersRunner) preRunE(c *cobra.Command, args []string) error {
|
||||
if len(args) > 1 {
|
||||
r.Lookup.Name = args[1]
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (r *ListSettersRunner) runE(c *cobra.Command, args []string) error {
|
||||
return handleError(c, lookup(r.Lookup, c, args))
|
||||
}
|
||||
@@ -5,6 +5,7 @@ package commands
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
|
||||
"github.com/olekukonko/tablewriter"
|
||||
"github.com/spf13/cobra"
|
||||
@@ -63,14 +64,14 @@ func (r *SetRunner) runE(c *cobra.Command, args []string) error {
|
||||
return handleError(c, r.perform(c, args))
|
||||
}
|
||||
|
||||
return handleError(c, r.lookup(c, args))
|
||||
return handleError(c, lookup(r.Lookup, c, args))
|
||||
}
|
||||
|
||||
func (r *SetRunner) lookup(c *cobra.Command, args []string) error {
|
||||
func lookup(l setters.LookupSetters, c *cobra.Command, args []string) error {
|
||||
// lookup the setters
|
||||
err := kio.Pipeline{
|
||||
Inputs: []kio.Reader{&kio.LocalPackageReader{PackagePath: args[0]}},
|
||||
Filters: []kio.Filter{&r.Lookup},
|
||||
Filters: []kio.Filter{&l},
|
||||
}.Execute()
|
||||
if err != nil {
|
||||
return err
|
||||
@@ -86,8 +87,8 @@ func (r *SetRunner) lookup(c *cobra.Command, args []string) error {
|
||||
table.SetHeader([]string{
|
||||
"NAME", "DESCRIPTION", "VALUE", "TYPE", "COUNT", "SETBY",
|
||||
})
|
||||
for i := range r.Lookup.SetterCounts {
|
||||
s := r.Lookup.SetterCounts[i]
|
||||
for i := range l.SetterCounts {
|
||||
s := l.SetterCounts[i]
|
||||
v := s.Value
|
||||
if s.Value == "" {
|
||||
v = s.Value
|
||||
@@ -102,6 +103,11 @@ func (r *SetRunner) lookup(c *cobra.Command, args []string) error {
|
||||
})
|
||||
}
|
||||
table.Render()
|
||||
|
||||
if len(l.SetterCounts) == 0 {
|
||||
// exit non-0 if no matching setters are found
|
||||
os.Exit(1)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
|
||||
@@ -76,7 +76,6 @@ metadata:
|
||||
annotations:
|
||||
app: nginx2
|
||||
config.kubernetes.io/index: '0'
|
||||
config.kubernetes.io/package: '.'
|
||||
config.kubernetes.io/path: 'f1.yaml'
|
||||
spec:
|
||||
replicas: 1
|
||||
@@ -87,7 +86,6 @@ metadata:
|
||||
annotations:
|
||||
app: nginx
|
||||
config.kubernetes.io/index: '1'
|
||||
config.kubernetes.io/package: '.'
|
||||
config.kubernetes.io/path: 'f1.yaml'
|
||||
spec:
|
||||
selector:
|
||||
|
||||
48
cmd/config/internal/commands/sink.go
Normal file
48
cmd/config/internal/commands/sink.go
Normal file
@@ -0,0 +1,48 @@
|
||||
// Copyright 2019 The Kubernetes Authors.
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
package commands
|
||||
|
||||
import (
|
||||
"github.com/spf13/cobra"
|
||||
"sigs.k8s.io/kustomize/cmd/config/internal/generateddocs/commands"
|
||||
"sigs.k8s.io/kustomize/kyaml/kio"
|
||||
)
|
||||
|
||||
// GetSinkRunner returns a command for Sink.
|
||||
func GetSinkRunner(name string) *SinkRunner {
|
||||
r := &SinkRunner{}
|
||||
c := &cobra.Command{
|
||||
Use: "sink DIR",
|
||||
Short: commands.SinkShort,
|
||||
Long: commands.SinkLong,
|
||||
Example: commands.SinkExamples,
|
||||
RunE: r.runE,
|
||||
Args: cobra.ExactArgs(1),
|
||||
}
|
||||
fixDocs(name, c)
|
||||
r.Command = c
|
||||
return r
|
||||
}
|
||||
|
||||
func SinkCommand(name string) *cobra.Command {
|
||||
return GetSinkRunner(name).Command
|
||||
}
|
||||
|
||||
// SinkRunner contains the run function
|
||||
type SinkRunner struct {
|
||||
Command *cobra.Command
|
||||
}
|
||||
|
||||
func (r *SinkRunner) runE(c *cobra.Command, args []string) error {
|
||||
err := kio.Pipeline{
|
||||
Inputs: []kio.Reader{&kio.ByteReader{Reader: c.InOrStdin()}},
|
||||
Outputs: []kio.Writer{
|
||||
&kio.LocalPackageWriter{
|
||||
PackagePath: args[0],
|
||||
ClearAnnotations: []string{"config.kubernetes.io/path"},
|
||||
},
|
||||
},
|
||||
}.Execute()
|
||||
return handleError(c, err)
|
||||
}
|
||||
140
cmd/config/internal/commands/sink_test.go
Normal file
140
cmd/config/internal/commands/sink_test.go
Normal file
@@ -0,0 +1,140 @@
|
||||
// Copyright 2019 The Kubernetes Authors.
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
package commands_test
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"io/ioutil"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
"sigs.k8s.io/kustomize/cmd/config/internal/commands"
|
||||
)
|
||||
|
||||
func TestSinkCommand(t *testing.T) {
|
||||
d, err := ioutil.TempDir("", "kustomize-source-test")
|
||||
if !assert.NoError(t, err) {
|
||||
t.FailNow()
|
||||
}
|
||||
defer os.RemoveAll(d)
|
||||
|
||||
// fmt the files
|
||||
b := &bytes.Buffer{}
|
||||
r := commands.GetSinkRunner("")
|
||||
r.Command.SetIn(bytes.NewBufferString(`apiVersion: config.kubernetes.io/v1alpha1
|
||||
kind: ResourceList
|
||||
items:
|
||||
- kind: Deployment
|
||||
metadata:
|
||||
labels:
|
||||
app: nginx2
|
||||
name: foo
|
||||
annotations:
|
||||
app: nginx2
|
||||
config.kubernetes.io/index: '0'
|
||||
config.kubernetes.io/path: 'f1.yaml'
|
||||
spec:
|
||||
replicas: 1
|
||||
- kind: Service
|
||||
metadata:
|
||||
name: foo
|
||||
annotations:
|
||||
app: nginx
|
||||
config.kubernetes.io/index: '1'
|
||||
config.kubernetes.io/path: 'f1.yaml'
|
||||
spec:
|
||||
selector:
|
||||
app: nginx
|
||||
- apiVersion: v1
|
||||
kind: Abstraction
|
||||
metadata:
|
||||
name: foo
|
||||
annotations:
|
||||
config.kubernetes.io/function: |
|
||||
container:
|
||||
image: gcr.io/example/reconciler:v1
|
||||
config.kubernetes.io/local-config: "true"
|
||||
config.kubernetes.io/index: '0'
|
||||
config.kubernetes.io/path: 'f2.yaml'
|
||||
spec:
|
||||
replicas: 3
|
||||
- apiVersion: apps/v1
|
||||
kind: Deployment
|
||||
metadata:
|
||||
labels:
|
||||
app: nginx
|
||||
name: bar
|
||||
annotations:
|
||||
app: nginx
|
||||
config.kubernetes.io/index: '1'
|
||||
config.kubernetes.io/path: 'f2.yaml'
|
||||
spec:
|
||||
replicas: 3
|
||||
`))
|
||||
r.Command.SetArgs([]string{d})
|
||||
r.Command.SetOut(b)
|
||||
if !assert.NoError(t, r.Command.Execute()) {
|
||||
t.FailNow()
|
||||
}
|
||||
|
||||
actual, err := ioutil.ReadFile(filepath.Join(d, "f1.yaml"))
|
||||
if !assert.NoError(t, err) {
|
||||
t.FailNow()
|
||||
}
|
||||
expected := `kind: Deployment
|
||||
metadata:
|
||||
labels:
|
||||
app: nginx2
|
||||
name: foo
|
||||
annotations:
|
||||
app: nginx2
|
||||
spec:
|
||||
replicas: 1
|
||||
---
|
||||
kind: Service
|
||||
metadata:
|
||||
name: foo
|
||||
annotations:
|
||||
app: nginx
|
||||
spec:
|
||||
selector:
|
||||
app: nginx
|
||||
`
|
||||
if !assert.Equal(t, expected, string(actual)) {
|
||||
t.FailNow()
|
||||
}
|
||||
|
||||
actual, err = ioutil.ReadFile(filepath.Join(d, "f2.yaml"))
|
||||
if !assert.NoError(t, err) {
|
||||
t.FailNow()
|
||||
}
|
||||
expected = `apiVersion: v1
|
||||
kind: Abstraction
|
||||
metadata:
|
||||
name: foo
|
||||
annotations:
|
||||
config.kubernetes.io/function: |
|
||||
container:
|
||||
image: gcr.io/example/reconciler:v1
|
||||
config.kubernetes.io/local-config: "true"
|
||||
spec:
|
||||
replicas: 3
|
||||
---
|
||||
apiVersion: apps/v1
|
||||
kind: Deployment
|
||||
metadata:
|
||||
labels:
|
||||
app: nginx
|
||||
name: bar
|
||||
annotations:
|
||||
app: nginx
|
||||
spec:
|
||||
replicas: 3
|
||||
`
|
||||
if !assert.Equal(t, expected, string(actual)) {
|
||||
t.FailNow()
|
||||
}
|
||||
}
|
||||
77
cmd/config/internal/commands/source.go
Normal file
77
cmd/config/internal/commands/source.go
Normal file
@@ -0,0 +1,77 @@
|
||||
// Copyright 2019 The Kubernetes Authors.
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
package commands
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"github.com/spf13/cobra"
|
||||
"sigs.k8s.io/kustomize/cmd/config/internal/generateddocs/commands"
|
||||
"sigs.k8s.io/kustomize/kyaml/kio"
|
||||
"sigs.k8s.io/kustomize/kyaml/yaml"
|
||||
)
|
||||
|
||||
// GetSourceRunner returns a command for Source.
|
||||
func GetSourceRunner(name string) *SourceRunner {
|
||||
r := &SourceRunner{}
|
||||
c := &cobra.Command{
|
||||
Use: "source DIR",
|
||||
Short: commands.SourceShort,
|
||||
Long: commands.SourceLong,
|
||||
Example: commands.SourceExamples,
|
||||
RunE: r.runE,
|
||||
Args: cobra.ExactArgs(1),
|
||||
}
|
||||
fixDocs(name, c)
|
||||
c.Flags().StringVar(&r.WrapKind, "wrap-kind", kio.ResourceListKind,
|
||||
"output using this format.")
|
||||
c.Flags().StringVar(&r.WrapApiVersion, "wrap-version", kio.ResourceListAPIVersion,
|
||||
"output using this format.")
|
||||
c.Flags().StringVar(&r.FunctionConfig, "function-config", "",
|
||||
"path to function config.")
|
||||
r.Command = c
|
||||
_ = c.MarkFlagFilename("function-config", "yaml", "json", "yml")
|
||||
return r
|
||||
}
|
||||
|
||||
func SourceCommand(name string) *cobra.Command {
|
||||
return GetSourceRunner(name).Command
|
||||
}
|
||||
|
||||
// SourceRunner contains the run function
|
||||
type SourceRunner struct {
|
||||
WrapKind string
|
||||
WrapApiVersion string
|
||||
FunctionConfig string
|
||||
Command *cobra.Command
|
||||
}
|
||||
|
||||
func (r *SourceRunner) runE(c *cobra.Command, args []string) error {
|
||||
// if there is a function-config specified, emit it
|
||||
var functionConfig *yaml.RNode
|
||||
if r.FunctionConfig != "" {
|
||||
configs, err := kio.LocalPackageReader{PackagePath: r.FunctionConfig}.Read()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if len(configs) != 1 {
|
||||
return fmt.Errorf("expected exactly 1 functionConfig, found %d", len(configs))
|
||||
}
|
||||
functionConfig = configs[0]
|
||||
}
|
||||
|
||||
var outputs []kio.Writer
|
||||
outputs = append(outputs, kio.ByteWriter{
|
||||
Writer: c.OutOrStdout(),
|
||||
KeepReaderAnnotations: true,
|
||||
WrappingKind: r.WrapKind,
|
||||
WrappingAPIVersion: r.WrapApiVersion,
|
||||
FunctionConfig: functionConfig,
|
||||
})
|
||||
|
||||
err := kio.Pipeline{
|
||||
Inputs: []kio.Reader{kio.LocalPackageReader{PackagePath: args[0]}},
|
||||
Outputs: outputs}.Execute()
|
||||
return handleError(c, err)
|
||||
}
|
||||
136
cmd/config/internal/commands/source_test.go
Normal file
136
cmd/config/internal/commands/source_test.go
Normal file
@@ -0,0 +1,136 @@
|
||||
// Copyright 2019 The Kubernetes Authors.
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
package commands_test
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"io/ioutil"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
"sigs.k8s.io/kustomize/cmd/config/internal/commands"
|
||||
)
|
||||
|
||||
func TestSourceCommand(t *testing.T) {
|
||||
d, err := ioutil.TempDir("", "kustomize-source-test")
|
||||
if !assert.NoError(t, err) {
|
||||
return
|
||||
}
|
||||
defer os.RemoveAll(d)
|
||||
|
||||
err = ioutil.WriteFile(filepath.Join(d, "f1.yaml"), []byte(`
|
||||
kind: Deployment
|
||||
metadata:
|
||||
labels:
|
||||
app: nginx2
|
||||
name: foo
|
||||
annotations:
|
||||
app: nginx2
|
||||
spec:
|
||||
replicas: 1
|
||||
---
|
||||
kind: Service
|
||||
metadata:
|
||||
name: foo
|
||||
annotations:
|
||||
app: nginx
|
||||
spec:
|
||||
selector:
|
||||
app: nginx
|
||||
`), 0600)
|
||||
if !assert.NoError(t, err) {
|
||||
return
|
||||
}
|
||||
err = ioutil.WriteFile(filepath.Join(d, "f2.yaml"), []byte(`
|
||||
apiVersion: v1
|
||||
kind: Abstraction
|
||||
metadata:
|
||||
name: foo
|
||||
annotations:
|
||||
config.kubernetes.io/function: |
|
||||
container:
|
||||
image: gcr.io/example/reconciler:v1
|
||||
config.kubernetes.io/local-config: "true"
|
||||
spec:
|
||||
replicas: 3
|
||||
---
|
||||
apiVersion: apps/v1
|
||||
kind: Deployment
|
||||
metadata:
|
||||
labels:
|
||||
app: nginx
|
||||
name: bar
|
||||
annotations:
|
||||
app: nginx
|
||||
spec:
|
||||
replicas: 3
|
||||
`), 0600)
|
||||
if !assert.NoError(t, err) {
|
||||
return
|
||||
}
|
||||
|
||||
// fmt the files
|
||||
b := &bytes.Buffer{}
|
||||
r := commands.GetSourceRunner("")
|
||||
r.Command.SetArgs([]string{d})
|
||||
r.Command.SetOut(b)
|
||||
if !assert.NoError(t, r.Command.Execute()) {
|
||||
return
|
||||
}
|
||||
|
||||
if !assert.Equal(t, `apiVersion: config.kubernetes.io/v1alpha1
|
||||
kind: ResourceList
|
||||
items:
|
||||
- kind: Deployment
|
||||
metadata:
|
||||
labels:
|
||||
app: nginx2
|
||||
name: foo
|
||||
annotations:
|
||||
app: nginx2
|
||||
config.kubernetes.io/index: '0'
|
||||
config.kubernetes.io/path: 'f1.yaml'
|
||||
spec:
|
||||
replicas: 1
|
||||
- kind: Service
|
||||
metadata:
|
||||
name: foo
|
||||
annotations:
|
||||
app: nginx
|
||||
config.kubernetes.io/index: '1'
|
||||
config.kubernetes.io/path: 'f1.yaml'
|
||||
spec:
|
||||
selector:
|
||||
app: nginx
|
||||
- apiVersion: v1
|
||||
kind: Abstraction
|
||||
metadata:
|
||||
name: foo
|
||||
annotations:
|
||||
config.kubernetes.io/function: |
|
||||
container:
|
||||
image: gcr.io/example/reconciler:v1
|
||||
config.kubernetes.io/local-config: "true"
|
||||
config.kubernetes.io/index: '0'
|
||||
config.kubernetes.io/path: 'f2.yaml'
|
||||
spec:
|
||||
replicas: 3
|
||||
- apiVersion: apps/v1
|
||||
kind: Deployment
|
||||
metadata:
|
||||
labels:
|
||||
app: nginx
|
||||
name: bar
|
||||
annotations:
|
||||
app: nginx
|
||||
config.kubernetes.io/index: '1'
|
||||
config.kubernetes.io/path: 'f2.yaml'
|
||||
spec:
|
||||
replicas: 3
|
||||
`, b.String()) {
|
||||
return
|
||||
}
|
||||
}
|
||||
@@ -104,7 +104,6 @@ metadata:
|
||||
namespace: default
|
||||
annotations:
|
||||
app: nginx2
|
||||
config.kubernetes.io/package: .
|
||||
config.kubernetes.io/path: f1.yaml
|
||||
spec:
|
||||
replicas: 1
|
||||
@@ -118,7 +117,6 @@ metadata:
|
||||
namespace: default
|
||||
annotations:
|
||||
app: nginx2
|
||||
config.kubernetes.io/package: .
|
||||
config.kubernetes.io/path: f1.yaml
|
||||
spec:
|
||||
replicas: 1
|
||||
@@ -132,7 +130,6 @@ metadata:
|
||||
namespace: default
|
||||
annotations:
|
||||
app: nginx2
|
||||
config.kubernetes.io/package: .
|
||||
config.kubernetes.io/path: f1.yaml
|
||||
spec:
|
||||
replicas: 1
|
||||
@@ -146,7 +143,6 @@ metadata:
|
||||
namespace: default2
|
||||
annotations:
|
||||
app: nginx2
|
||||
config.kubernetes.io/package: .
|
||||
config.kubernetes.io/path: f1.yaml
|
||||
spec:
|
||||
replicas: 1
|
||||
@@ -160,7 +156,6 @@ metadata:
|
||||
namespace: default
|
||||
annotations:
|
||||
app: nginx3
|
||||
config.kubernetes.io/package: .
|
||||
config.kubernetes.io/path: f1.yaml
|
||||
spec:
|
||||
replicas: 1
|
||||
@@ -171,8 +166,7 @@ metadata:
|
||||
app: nginx
|
||||
annotations:
|
||||
app: nginx
|
||||
config.kubernetes.io/package: bar-package
|
||||
config.kubernetes.io/path: f2.yaml
|
||||
config.kubernetes.io/path: bar-package/f2.yaml
|
||||
name: bar
|
||||
spec:
|
||||
replicas: 3
|
||||
@@ -183,7 +177,6 @@ metadata:
|
||||
namespace: default
|
||||
annotations:
|
||||
app: nginx
|
||||
config.kubernetes.io/package: .
|
||||
config.kubernetes.io/path: f1.yaml
|
||||
spec:
|
||||
selector:
|
||||
|
||||
@@ -4,288 +4,6 @@
|
||||
// Code generated by "mdtogo"; DO NOT EDIT.
|
||||
package api
|
||||
|
||||
var ConfigFnLong = `# Configuration Functions API Semantics
|
||||
|
||||
Configuration Functions are functions packaged as executables in containers which enable
|
||||
**shift-left practices**. They configure applications and infrastructure through
|
||||
Kubernetes style Resource Configuration, but run locally pre-commit.
|
||||
|
||||
Configuration functions enable shift-left practices (client-side) through:
|
||||
|
||||
- Pre-commit / delivery validation and linting of configuration
|
||||
- e.g. Fail if any containers don't have PodSecurityPolicy or CPU / Memory limits
|
||||
- Implementation of abstractions as client actuated APIs (e.g. templating)
|
||||
- e.g. Create a client-side *"CRD"* for generating configuration checked into git
|
||||
- Aspect Orient configuration / Injection of cross-cutting configuration
|
||||
- e.g. T-Shirt size containers by annotating Resources with ` + "`" + `small` + "`" + `, ` + "`" + `medium` + "`" + `, ` + "`" + `large` + "`" + `
|
||||
and inject the cpu and memory resources into containers accordingly.
|
||||
- e.g. Inject ` + "`" + `init` + "`" + ` and ` + "`" + `side-car` + "`" + ` containers into Resources based off of Resource
|
||||
Type, annotations, etc.
|
||||
|
||||
Performing these on the client rather than the server enables:
|
||||
|
||||
- Configuration to be reviewed prior to being sent to the API server
|
||||
- Configuration to be validated as part of the CD pipeline
|
||||
- Configuration for Resources to validated holistically rather than individually
|
||||
per-Resource -- e.g. ensure the ` + "`" + `Service.selector` + "`" + ` and ` + "`" + `Deployment.spec.template` + "`" + ` labels
|
||||
match.
|
||||
- MutatingWebHooks are scoped to a single Resource instance at a time.
|
||||
- Low-level tweaks to the output of high-level abstractions -- e.g. add an ` + "`" + `init container` + "`" + `
|
||||
to a client *"CRD"* Resource after it was generated.
|
||||
- Composition and layering of multiple functions together
|
||||
- Compose generation, injection, validation together
|
||||
|
||||
Configuration Functions are implemented as executable programs published in containers which:
|
||||
|
||||
- Accept as input (stdin):
|
||||
- A list of Resource Configuration
|
||||
- A Function Configuration (to configure the function itself)
|
||||
- Emit as output (stdout + exit):
|
||||
- A list of Resource Configuration
|
||||
- An exit code for success / failure
|
||||
|
||||
### Function Specification
|
||||
|
||||
- Functions **SHOULD** be published as container images containing a ` + "`" + `CMD` + "`" + ` invoking an executable.
|
||||
- Functions **MUST** accept input on STDIN a ` + "`" + `ResourceList` + "`" + ` containing the Resources and
|
||||
` + "`" + `functionConfig` + "`" + `.
|
||||
- Functions **MUST** emit output on STDOUT a ` + "`" + `ResourceList` + "`" + ` containing the modified
|
||||
Resources.
|
||||
- Functions **MUST** exit non-0 on failure, and exit 0 on success.
|
||||
- Functions **MAY** emit output on STDERR with error messaging.
|
||||
- Functions performing validation **SHOULD** exit failure and emit error messaging
|
||||
on a validation failure.
|
||||
- Functions generating Resources **SHOULD** retain non-conflicting changes on the
|
||||
generated Resources -- e.g. 1. the function generates a Deployment, but doesn't
|
||||
specify ` + "`" + `cpu` + "`" + `, 2. the user sets the ` + "`" + `cpu` + "`" + ` on the generated Resource, 3. the
|
||||
function should keep the ` + "`" + `cpu` + "`" + ` when regenerating the Resource a second time.
|
||||
- Functions **SHOULD** be usable outside ` + "`" + `kustomize config run` + "`" + ` -- e.g. though pipeline
|
||||
mechanisms such as Tekton.
|
||||
|
||||
#### Input Format
|
||||
|
||||
Functions must accept on STDIN:
|
||||
|
||||
` + "`" + `ResourceList` + "`" + `:
|
||||
- contains ` + "`" + `items` + "`" + ` field, same as ` + "`" + `List.items` + "`" + `
|
||||
- contains ` + "`" + `functionConfig` + "`" + ` field -- a single item with the configuration for the function itself
|
||||
|
||||
Example ` + "`" + `ResourceList` + "`" + ` Input:
|
||||
|
||||
apiVersion: config.kubernetes.io/v1alpha1
|
||||
kind: ResourceList
|
||||
functionConfig:
|
||||
apiVersion: example.com/v1beta1
|
||||
kind: Nginx
|
||||
metadata:
|
||||
name: my-instance
|
||||
annotations:
|
||||
config.kubernetes.io/local-config: "true"
|
||||
spec:
|
||||
replicas: 5
|
||||
items:
|
||||
- apiVersion: apps/v1
|
||||
kind: Deployment
|
||||
metadata:
|
||||
name: my-instance
|
||||
spec:
|
||||
replicas: 3
|
||||
...
|
||||
- apiVersion: v1
|
||||
kind: Service
|
||||
metadata:
|
||||
name: my-instance
|
||||
spec:
|
||||
...
|
||||
|
||||
#### Output Format
|
||||
|
||||
Functions must emit on STDOUT:
|
||||
|
||||
` + "`" + `ResourceList` + "`" + `:
|
||||
- contains ` + "`" + `items` + "`" + ` field, same as ` + "`" + `List.items` + "`" + `
|
||||
|
||||
Example ` + "`" + `ResourceList` + "`" + ` Output:
|
||||
|
||||
apiVersion: config.kubernetes.io/v1alpha1
|
||||
kind: ResourceList
|
||||
items:
|
||||
- apiVersion: apps/v1
|
||||
kind: Deployment
|
||||
metadata:
|
||||
name: my-instance
|
||||
spec:
|
||||
replicas: 5
|
||||
...
|
||||
- apiVersion: v1
|
||||
kind: Service
|
||||
metadata:
|
||||
name: my-instance
|
||||
spec:
|
||||
...
|
||||
|
||||
#### Container Environment
|
||||
|
||||
When run by ` + "`" + `kustomize config run` + "`" + `, functions are run in containers with the
|
||||
following environment:
|
||||
|
||||
- Network: ` + "`" + `none` + "`" + `
|
||||
- User: ` + "`" + `nobody` + "`" + `
|
||||
- Security Options: ` + "`" + `no-new-privileges` + "`" + `
|
||||
- Volumes: the volume containing the ` + "`" + `functionConfig` + "`" + ` yaml is mounted under ` + "`" + `/local` + "`" + ` as ` + "`" + `ro` + "`" + `
|
||||
|
||||
### Example Function Implementation
|
||||
|
||||
Following is an example for implementing an nginx abstraction using a config
|
||||
function.
|
||||
|
||||
#### ` + "`" + `nginx-template.sh` + "`" + `
|
||||
|
||||
` + "`" + `nginx-template.sh` + "`" + ` is a simple bash script which uses a *heredoc* as a templating solution
|
||||
for generating Resources from the functionConfig input fields.
|
||||
|
||||
The script wraps itself using ` + "`" + `config run wrap -- $0` + "`" + ` which will:
|
||||
|
||||
1. Parse the ` + "`" + `ResourceList.functionConfig` + "`" + ` (provided to the container stdin) into env vars
|
||||
2. Merge the stdout into the original list of Resources
|
||||
3. Defaults filenames for newly generated Resources (if they are not set as annotations)
|
||||
to ` + "`" + `config/NAME_KIND.yaml` + "`" + `
|
||||
4. Format the output
|
||||
|
||||
#!/bin/bash
|
||||
# script must run wrapped by ` + "`" + `kustomize config run wrap` + "`" + `
|
||||
# for parsing input the functionConfig into env vars
|
||||
if [ -z ${WRAPPED} ]; then
|
||||
export WRAPPED=true
|
||||
config run wrap -- $0
|
||||
exit $?
|
||||
fi
|
||||
|
||||
cat <<End-of-message
|
||||
apiVersion: v1
|
||||
kind: Service
|
||||
metadata:
|
||||
name: ${NAME}
|
||||
labels:
|
||||
app: nginx
|
||||
instance: ${NAME}
|
||||
spec:
|
||||
ports:
|
||||
- port: 80
|
||||
targetPort: 80
|
||||
name: http
|
||||
selector:
|
||||
app: nginx
|
||||
instance: ${NAME}
|
||||
---
|
||||
apiVersion: apps/v1
|
||||
kind: Deployment
|
||||
metadata:
|
||||
name: ${NAME}
|
||||
labels:
|
||||
app: nginx
|
||||
instance: ${NAME}
|
||||
spec:
|
||||
replicas: ${REPLICAS}
|
||||
selector:
|
||||
matchLabels:
|
||||
app: nginx
|
||||
instance: ${NAME}
|
||||
template:
|
||||
metadata:
|
||||
labels:
|
||||
app: nginx
|
||||
instance: ${NAME}
|
||||
spec:
|
||||
containers:
|
||||
- name: nginx
|
||||
image: nginx:1.7.9
|
||||
ports:
|
||||
- containerPort: 80
|
||||
End-of-message
|
||||
|
||||
#### ` + "`" + `Dockerfile` + "`" + `
|
||||
|
||||
` + "`" + `Dockerfile` + "`" + ` installs ` + "`" + `kustomize config` + "`" + ` and copies the script into the container image.
|
||||
|
||||
FROM golang:1.13-stretch
|
||||
RUN go get sigs.k8s.io/kustomize/cmd/config
|
||||
RUN mv /go/bin/config /usr/bin/config
|
||||
COPY nginx-template.sh /usr/bin/nginx-template.sh
|
||||
CMD ["nginx-template.sh]
|
||||
|
||||
### Example Function Usage
|
||||
|
||||
Following is an example of running the ` + "`" + `kustomize config run` + "`" + ` using the preceding API.
|
||||
|
||||
#### ` + "`" + `nginx.yaml` + "`" + ` (Input)
|
||||
|
||||
` + "`" + `dir/nginx.yaml` + "`" + ` contains a reference to the Function. The contents of ` + "`" + `nginx.yaml` + "`" + `
|
||||
are passed to the Function through the ` + "`" + `ResourceList.functionConfig` + "`" + ` field.
|
||||
|
||||
apiVersion: example.com/v1beta1
|
||||
kind: Nginx
|
||||
metadata:
|
||||
name: my-instance
|
||||
annotations:
|
||||
config.kubernetes.io/local-config: "true"
|
||||
configFn:
|
||||
container:
|
||||
image: gcr.io/example-functions/nginx-template:v1.0.0
|
||||
spec:
|
||||
replicas: 5
|
||||
|
||||
- ` + "`" + `configFn.container.image` + "`" + `: the image to use for this API
|
||||
- ` + "`" + `annotations[config.kubernetes.io/local-config]` + "`" + `: mark this as not a Resource that should
|
||||
be applied
|
||||
|
||||
#### ` + "`" + `kustomize config run dir/` + "`" + ` (Output)
|
||||
|
||||
` + "`" + `dir/my-instance_deployment.yaml` + "`" + ` contains the Deployment:
|
||||
|
||||
apiVersion: apps/v1
|
||||
kind: Deployment
|
||||
metadata:
|
||||
name: my-instance
|
||||
labels:
|
||||
app: nginx
|
||||
instance: my-instance
|
||||
spec:
|
||||
replicas: 5
|
||||
selector:
|
||||
matchLabels:
|
||||
app: nginx
|
||||
instance: my-instance
|
||||
template:
|
||||
metadata:
|
||||
labels:
|
||||
app: nginx
|
||||
instance: my-instance
|
||||
spec:
|
||||
containers:
|
||||
- name: nginx
|
||||
image: nginx:1.7.9
|
||||
ports:
|
||||
- containerPort: 80
|
||||
|
||||
` + "`" + `dir/my-instance_service.yaml` + "`" + ` contains the Service:
|
||||
|
||||
apiVersion: v1
|
||||
kind: Service
|
||||
metadata:
|
||||
name: my-instance
|
||||
labels:
|
||||
app: nginx
|
||||
instance: my-instance
|
||||
spec:
|
||||
ports:
|
||||
- port: 80
|
||||
targetPort: 80
|
||||
name: http
|
||||
selector:
|
||||
app: nginx
|
||||
instance: my-instance`
|
||||
|
||||
var ConfigIoLong = `# Configuration IO API Semantics
|
||||
|
||||
Resource Configuration may be read / written from / to sources such as directories,
|
||||
@@ -346,6 +64,355 @@ Example:
|
||||
annotations:
|
||||
config.kubernetes.io/local-config: "true"`
|
||||
|
||||
var FunctionsImplShort = `Following is an example for implementing an nginx abstraction using a configuration`
|
||||
var FunctionsImplLong = `# Running Configuration Functions using kustomize CLI
|
||||
|
||||
Configuration functions can be implemented using any toolchain and invoked using any
|
||||
container workflow orchestrator including Tekton, Cloud Build, or run directly using ` + "`" + `docker run` + "`" + `.
|
||||
|
||||
Run ` + "`" + `config help docs-fn-spec` + "`" + ` to see the Configuration Functions Specification.
|
||||
|
||||
` + "`" + `kustomize config run` + "`" + ` is an example orchestrator for invoking Configuration Functions. This
|
||||
document describes how to implement and invoke an example function.
|
||||
|
||||
function.
|
||||
|
||||
### ` + "`" + `nginx-template.sh` + "`" + `
|
||||
|
||||
` + "`" + `nginx-template.sh` + "`" + ` is a simple bash script which uses a _heredoc_ as a templating solution
|
||||
for generating Resources from the functionConfig input fields.
|
||||
|
||||
The script wraps itself using ` + "`" + `config run wrap -- $0` + "`" + ` which will:
|
||||
|
||||
1. Parse the ` + "`" + `ResourceList.functionConfig` + "`" + ` (provided to the container stdin) into env vars
|
||||
2. Merge the stdout into the original list of Resources
|
||||
3. Defaults filenames for newly generated Resources (if they are not set as annotations)
|
||||
to ` + "`" + `config/NAME_KIND.yaml` + "`" + `
|
||||
4. Format the output
|
||||
|
||||
#!/bin/bash
|
||||
# script must run wrapped by "kustomize config run wrap"
|
||||
# for parsing input the functionConfig into env vars
|
||||
if [ -z ${WRAPPED} ]; then
|
||||
export WRAPPED=true
|
||||
config run wrap -- $0
|
||||
exit $?
|
||||
fi
|
||||
|
||||
cat <<End-of-message
|
||||
apiVersion: v1
|
||||
kind: Service
|
||||
metadata:
|
||||
name: ${NAME}
|
||||
labels:
|
||||
app: nginx
|
||||
instance: ${NAME}
|
||||
spec:
|
||||
ports:
|
||||
- port: 80
|
||||
targetPort: 80
|
||||
name: http
|
||||
selector:
|
||||
app: nginx
|
||||
instance: ${NAME}
|
||||
---
|
||||
apiVersion: apps/v1
|
||||
kind: Deployment
|
||||
metadata:
|
||||
name: ${NAME}
|
||||
labels:
|
||||
app: nginx
|
||||
instance: ${NAME}
|
||||
spec:
|
||||
replicas: ${REPLICAS}
|
||||
selector:
|
||||
matchLabels:
|
||||
app: nginx
|
||||
instance: ${NAME}
|
||||
template:
|
||||
metadata:
|
||||
labels:
|
||||
app: nginx
|
||||
instance: ${NAME}
|
||||
spec:
|
||||
containers:
|
||||
- name: nginx
|
||||
image: nginx:1.7.9
|
||||
ports:
|
||||
- containerPort: 80
|
||||
End-of-message
|
||||
|
||||
### Dockerfile
|
||||
|
||||
` + "`" + `Dockerfile` + "`" + ` installs ` + "`" + `kustomize config` + "`" + ` and copies the script into the container image.
|
||||
|
||||
FROM golang:1.13-stretch
|
||||
RUN go get sigs.k8s.io/kustomize/cmd/config
|
||||
RUN mv /go/bin/config /usr/bin/config
|
||||
COPY nginx-template.sh /usr/bin/nginx-template.sh
|
||||
CMD ["nginx-template.sh]
|
||||
|
||||
## Example Function Usage
|
||||
|
||||
Following is an example of running the ` + "`" + `kustomize config run` + "`" + ` using the preceding API.
|
||||
|
||||
When run by ` + "`" + `kustomize config run` + "`" + `, functions are run in containers with the
|
||||
following environment:
|
||||
|
||||
- Network: ` + "`" + `none` + "`" + `
|
||||
- User: ` + "`" + `nobody` + "`" + `
|
||||
- Security Options: ` + "`" + `no-new-privileges` + "`" + `
|
||||
- Volumes: the volume containing the ` + "`" + `functionConfig` + "`" + ` yaml is mounted under ` + "`" + `/local` + "`" + ` as ` + "`" + `ro` + "`" + `
|
||||
|
||||
### Input
|
||||
|
||||
` + "`" + `dir/nginx.yaml` + "`" + ` contains a reference to the Function. The contents of ` + "`" + `nginx.yaml` + "`" + `
|
||||
are passed to the Function through the ` + "`" + `ResourceList.functionConfig` + "`" + ` field.
|
||||
|
||||
apiVersion: example.com/v1beta1
|
||||
kind: Nginx
|
||||
metadata:
|
||||
name: my-instance
|
||||
annotations:
|
||||
config.kubernetes.io/local-config: "true"
|
||||
config.k8s.io/function: |
|
||||
container:
|
||||
image: gcr.io/example-functions/nginx-template:v1.0.0
|
||||
spec:
|
||||
replicas: 5
|
||||
|
||||
- ` + "`" + `annotations[config.k8s.io/function].container.image` + "`" + `: the image to use for this API
|
||||
- ` + "`" + `annotations[config.kubernetes.io/local-config]` + "`" + `: mark this as not a Resource that should
|
||||
be applied
|
||||
|
||||
### Output
|
||||
|
||||
The function is invoked using by runing ` + "`" + `kustomize config run dir/` + "`" + `.
|
||||
|
||||
` + "`" + `dir/my-instance_deployment.yaml` + "`" + ` contains the Deployment:
|
||||
|
||||
apiVersion: apps/v1
|
||||
kind: Deployment
|
||||
metadata:
|
||||
name: my-instance
|
||||
labels:
|
||||
app: nginx
|
||||
instance: my-instance
|
||||
spec:
|
||||
replicas: 5
|
||||
selector:
|
||||
matchLabels:
|
||||
app: nginx
|
||||
instance: my-instance
|
||||
template:
|
||||
metadata:
|
||||
labels:
|
||||
app: nginx
|
||||
instance: my-instance
|
||||
spec:
|
||||
containers:
|
||||
- name: nginx
|
||||
image: nginx:1.7.9
|
||||
ports:
|
||||
- containerPort: 80
|
||||
|
||||
` + "`" + `dir/my-instance_service.yaml` + "`" + ` contains the Service:
|
||||
|
||||
apiVersion: v1
|
||||
kind: Service
|
||||
metadata:
|
||||
name: my-instance
|
||||
labels:
|
||||
app: nginx
|
||||
instance: my-instance
|
||||
spec:
|
||||
ports:
|
||||
- port: 80
|
||||
targetPort: 80
|
||||
name: http
|
||||
selector:
|
||||
app: nginx
|
||||
instance: my-instance`
|
||||
|
||||
var FunctionsSpecShort = `_Configuration functions_ enable shift-left practices (client-side) through:`
|
||||
var FunctionsSpecLong = `# Configuration Functions Specification
|
||||
|
||||
This document specifies a standard for client-side functions that operate on
|
||||
Kubernetes declarative configurations. This standard enables creating
|
||||
small, interoperable, and language-independent executable programs packaged as
|
||||
containers that can be chained together as part of a configuration management pipeline.
|
||||
The end result of such a pipeline are fully rendered configurations that can then be
|
||||
applied to a control plane (e.g. Using ‘kubectl apply’ for Kubernetes control plane).
|
||||
As such, although this document references Kubernetes Resource Model and API conventions,
|
||||
it is completely decoupled from Kuberentes API machinery and does not depend on any
|
||||
in-cluster components.
|
||||
|
||||
This document references terms described in [Kubernetes API Conventions][1].
|
||||
|
||||
The key words "MUST", "MUST NOT", "REQUIRED", "SHALL", "SHALL NOT", "SHOULD",
|
||||
"SHOULD NOT", "RECOMMENDED", "MAY", and "OPTIONAL" in this document are to be
|
||||
interpreted as described in [RFC 2119][2].
|
||||
|
||||
|
||||
- Pre-commit / delivery validation and linting of configuration
|
||||
- e.g. Fail if any containers don't have PodSecurityPolicy or CPU / Memory limits
|
||||
- Implementation of abstractions as client actuated APIs (e.g. templating)
|
||||
- e.g. Create a client-side _"CRD"_ for generating configuration checked into git
|
||||
- Aspect Orient configuration / Injection of cross-cutting configuration
|
||||
- e.g. T-Shirt size containers by annotating Resources with ` + "`" + `small` + "`" + `, ` + "`" + `medium` + "`" + `, ` + "`" + `large` + "`" + `
|
||||
and inject the cpu and memory resources into containers accordingly.
|
||||
- e.g. Inject ` + "`" + `init` + "`" + ` and ` + "`" + `side-car` + "`" + ` containers into Resources based off of Resource
|
||||
Type, annotations, etc.
|
||||
|
||||
Performing these on the client rather than the server enables:
|
||||
|
||||
- Configuration to be reviewed prior to being sent to the API server
|
||||
- Configuration to be validated as part of the CI?CD pipeline
|
||||
- Configuration for Resources to validated holistically rather than individually
|
||||
per-Resource
|
||||
- e.g. ensure the ` + "`" + `Service.selector` + "`" + ` and ` + "`" + `Deployment.spec.template` + "`" + ` labels
|
||||
match.
|
||||
- e.g. MutatingWebHooks are scoped to a single Resource instance at a time.
|
||||
- Low-level tweaks to the output of high-level abstractions
|
||||
- e.g. add an ` + "`" + `init container` + "`" + ` to a client _"CRD"_ Resource after it was generated.
|
||||
- Composition and layering of multiple functions together
|
||||
- Compose generation, injection, validation together
|
||||
|
||||
## Spec
|
||||
|
||||
### Input Type
|
||||
|
||||
A function MUST accept as input a single [Kubernetes List type][3].
|
||||
The ` + "`" + `items` + "`" + ` field in the input will contain a sequence of [Object types][3].
|
||||
A function MAY not support [Simple types][3] and List types.
|
||||
|
||||
An example using ` + "`" + `v1/ConfigMapList` + "`" + ` as input:
|
||||
|
||||
apiVersion: v1
|
||||
kind: ConfigMapList
|
||||
items:
|
||||
- apiVersion: v1
|
||||
kind: ConfigMap
|
||||
metadata:
|
||||
name: config1
|
||||
data:
|
||||
p1: v1
|
||||
p2: v2
|
||||
- apiVersion: v1
|
||||
kind: ConfigMap
|
||||
metadata:
|
||||
name: config2
|
||||
|
||||
An example using ` + "`" + `v1/List` + "`" + ` as input:
|
||||
|
||||
apiVersion: v1
|
||||
kind: List
|
||||
items:
|
||||
spec:
|
||||
- apiVersion: foo-corp.com/v1
|
||||
kind: FulfillmentCenter
|
||||
metadata:
|
||||
name: staging
|
||||
address: "100 Main St."
|
||||
- apiVersion: rbac.authorization.k8s.io/v1
|
||||
kind: ClusterRole
|
||||
metadata:
|
||||
name: namespace-reader
|
||||
rules:
|
||||
- resources:
|
||||
- namespaces
|
||||
apiGroups:
|
||||
- ""
|
||||
verbs:
|
||||
- get
|
||||
- watch
|
||||
- list
|
||||
|
||||
In addition, a function MUST accept as input a List of kind ` + "`" + `ResourceList` + "`" + ` where the
|
||||
` + "`" + `functionConfig` + "`" + ` field, if present, will contain the invocation-specific configuration passed to the function
|
||||
by the orchestrator.
|
||||
Functions MAY consider this field optional so that they can be triggered in an ad-hoc fashion.
|
||||
|
||||
An example using ` + "`" + `config.kubernetes.io/v1beta1/ResourceList` + "`" + ` as input:
|
||||
|
||||
apiVersion: config.kubernetes.io/v1beta1
|
||||
kind: ResourceList
|
||||
functionConfig:
|
||||
apiVersion: foo-corp.com/v1
|
||||
kind: FulfillmentCenter
|
||||
metadata:
|
||||
name: staging
|
||||
metadata:
|
||||
annotations:
|
||||
config.k8s.io/function: |
|
||||
container:
|
||||
image: gcr.io/example/foo:v1.0.0
|
||||
spec:
|
||||
address: "100 Main St."
|
||||
items:
|
||||
- apiVersion: rbac.authorization.k8s.io/v1
|
||||
kind: ClusterRole
|
||||
metadata:
|
||||
name: namespace-reader
|
||||
rules:
|
||||
- resources:
|
||||
- namespaces
|
||||
apiGroups:
|
||||
- ""
|
||||
verbs:
|
||||
- get
|
||||
- watch
|
||||
- list
|
||||
|
||||
Here ` + "`" + `FulfillmentCenter` + "`" + ` kind with name ` + "`" + `staging` + "`" + ` is passed as the invocation-specific configuration
|
||||
to the function.
|
||||
|
||||
### Output Type
|
||||
|
||||
A function’s output MUST be the same as the input specification above
|
||||
-- i.e. ` + "`" + `ResourceList` + "`" + ` or ` + "`" + `List` + "`" + `.
|
||||
This is necessary to enable chaining two or more functions together in a pipeline.
|
||||
The serialization format of the output SHOULD match that of its input on each invocation
|
||||
-- e.g. if the input was a ` + "`" + `ResourceList` + "`" + `, the output should also be a ` + "`" + `ResourceList` + "`" + `.
|
||||
|
||||
### Serialization Format
|
||||
|
||||
A function MUST support YAML as a serialization format for the input and output.
|
||||
A function MUST use utf8 encoding (as YAML is a superset of JSON, JSON will also be supported
|
||||
by any conforming function).
|
||||
|
||||
### Operations
|
||||
|
||||
A function MAY Create, Update, or Delete any number of items in the ` + "`" + `items` + "`" + ` field and output the
|
||||
resultant list.
|
||||
|
||||
A function MAY modify annotations with prefix ` + "`" + `config.kubernetes.io` + "`" + `, but must be careful about
|
||||
doing so since they’re used for orchestration purposes and will likely impact subsequent functions
|
||||
in the pipeline.
|
||||
|
||||
A function SHOULD preserve comments when input serialization format is YAML.
|
||||
This allows for human authoring of configuration to coexist with changes made by functions.
|
||||
|
||||
### Containerization
|
||||
|
||||
A function MUST be implemented as a container.
|
||||
|
||||
A function container MUST be capable of running as a non-root user if it does not require
|
||||
access to host filesystem or makes network calls.
|
||||
|
||||
### stdin/stdout/stderr and Exit Codes
|
||||
|
||||
A function MUST accept input from stdin and emit output to stdout.
|
||||
|
||||
Any error messages MUST be emitted to stderr.
|
||||
|
||||
An exit code of zero indicates function execution was successful.
|
||||
A non-zero exit code indicates a failure.
|
||||
|
||||
[1]: https://github.com/kubernetes/community/blob/master/contributors/devel/sig-architecture/api-conventions.md
|
||||
[2]: https://tools.ietf.org/html/rfc2119
|
||||
[3]: https://github.com/kubernetes/community/blob/master/contributors/devel/sig-architecture/api-conventions.md#types-kinds`
|
||||
|
||||
var Merge2Long = `# Merge (2-way)
|
||||
|
||||
2-way merges fields from a source to a destination, overriding the destination fields
|
||||
|
||||
@@ -142,6 +142,25 @@ var GrepExamples = `
|
||||
# look for Resources matching a specific container image
|
||||
kustomize config grep "spec.template.spec.containers[name=nginx].image=nginx:1\.7\.9" my-dir/ | kustomize config tree`
|
||||
|
||||
var ListSettersShort = `[Alpha] List setters for Resources.`
|
||||
var ListSettersLong = `
|
||||
List setters for Resources.
|
||||
|
||||
DIR
|
||||
|
||||
A directory containing Resource configuration.
|
||||
|
||||
NAME
|
||||
|
||||
Optional. The name of the setter to display.
|
||||
`
|
||||
var ListSettersExamples = `
|
||||
Show setters:
|
||||
|
||||
$ config set DIR/
|
||||
NAME DESCRIPTION VALUE TYPE COUNT SETBY
|
||||
name-prefix '' PREFIX string 2`
|
||||
|
||||
var MergeShort = `[Alpha] Merge Resource configuration files`
|
||||
var MergeLong = `
|
||||
[Alpha] Merge Resource configuration files
|
||||
@@ -201,8 +220,8 @@ order they appear in the file).
|
||||
|
||||
#### Config Functions:
|
||||
|
||||
Config functions are specified as Kubernetes types containing a metadata.configFn.container.image
|
||||
field. This field tells run how to invoke the container.
|
||||
Config functions are specified as Kubernetes types containing a metadata.annotations.[config.kubernetes.io/function]
|
||||
field specifying an image for the container to run. This image tells run how to invoke the container.
|
||||
|
||||
Example config function:
|
||||
|
||||
@@ -210,17 +229,17 @@ order they appear in the file).
|
||||
apiVersion: fn.example.com/v1beta1
|
||||
kind: ExampleFunctionKind
|
||||
metadata:
|
||||
configFn:
|
||||
container:
|
||||
# function is invoked as a container running this image
|
||||
image: gcr.io/example/examplefunction:v1.0.1
|
||||
annotations:
|
||||
config.kubernetes.io/function: |
|
||||
container:
|
||||
# function is invoked as a container running this image
|
||||
image: gcr.io/example/examplefunction:v1.0.1
|
||||
config.kubernetes.io/local-config: "true" # tools should ignore this
|
||||
spec:
|
||||
configField: configValue
|
||||
|
||||
In the preceding example, 'kustomize config run example/' would identify the function by
|
||||
the metadata.configFn field. It would then write all Resources in the directory to
|
||||
the metadata.annotations.[config.kubernetes.io/function] field. It would then write all Resources in the directory to
|
||||
a container stdin (running the gcr.io/example/examplefunction:v1.0.1 image). It
|
||||
would then write the container stdout back to example/, replacing the directory
|
||||
file contents.
|
||||
@@ -286,19 +305,19 @@ var SetExamples = `
|
||||
List setters: Show the possible setters
|
||||
|
||||
$ config set DIR/
|
||||
NAME DESCRIPTION VALUE TYPE COUNT OWNER
|
||||
NAME DESCRIPTION VALUE TYPE COUNT SETBY
|
||||
name-prefix '' PREFIX string 2
|
||||
|
||||
Perform substitution: set a new value, owner and description
|
||||
Perform set: set a new value, owner and description
|
||||
|
||||
$ kustomize config set DIR/ name-prefix "test" --description "test environment" --set-by "dev"
|
||||
performed 2 substitutions
|
||||
set 2 values
|
||||
|
||||
Show substitutions: Show the new values
|
||||
List setters: Show the new values
|
||||
|
||||
$ config set dir
|
||||
NAME DESCRIPTION VALUE TYPE COUNT SUBSTITUTED OWNER
|
||||
prefix 'test environment' test string 2 true dev
|
||||
$ config set DIR/
|
||||
NAME DESCRIPTION VALUE TYPE COUNT SETBY
|
||||
name-prefix 'test environment' test string 2 dev
|
||||
|
||||
New Resource YAML:
|
||||
|
||||
@@ -313,6 +332,37 @@ var SetExamples = `
|
||||
name: test-app2 # {"description":"test environment","type":"string","x-kustomize":{"setBy":"dev","partialFieldSetters":[{"name":"name-prefix","value":"test"}]}}
|
||||
...`
|
||||
|
||||
var SinkShort = `[Alpha] Implement a Sink by writing input to a local directory.`
|
||||
var SinkLong = `
|
||||
[Alpha] Implement a Sink by writing input to a local directory.
|
||||
|
||||
kustomize config sink DIR
|
||||
|
||||
DIR:
|
||||
Path to local directory.
|
||||
|
||||
` + "`" + `sink` + "`" + ` writes its input to a directory
|
||||
`
|
||||
var SinkExamples = `
|
||||
kustomize config source DIR/ | your-function | kustomize config sink DIR/`
|
||||
|
||||
var SourceShort = `[Alpha] Implement a Source by reading a local directory.`
|
||||
var SourceLong = `
|
||||
[Alpha] Implement a Source by reading a local directory.
|
||||
|
||||
kustomize config source DIR
|
||||
|
||||
DIR:
|
||||
Path to local directory.
|
||||
|
||||
` + "`" + `source` + "`" + ` emits configuration to act as input to a function
|
||||
`
|
||||
var SourceExamples = `
|
||||
# emity configuration directory as input source to a function
|
||||
kustomize config source DIR/
|
||||
|
||||
kustomize config source DIR/ | your-function | kustomize config sink DIR/`
|
||||
|
||||
var TreeShort = `[Alpha] Display Resource structure from a directory or stdin.`
|
||||
var TreeLong = `
|
||||
[Alpha] Display Resource structure from a directory or stdin.
|
||||
|
||||
@@ -9,6 +9,7 @@ package main
|
||||
import (
|
||||
"os"
|
||||
|
||||
"sigs.k8s.io/kustomize/cmd/config/complete"
|
||||
"sigs.k8s.io/kustomize/cmd/config/configcobra"
|
||||
"sigs.k8s.io/kustomize/kyaml/commandutil"
|
||||
)
|
||||
@@ -16,7 +17,10 @@ import (
|
||||
func main() {
|
||||
// enable the config commands
|
||||
os.Setenv(commandutil.EnableAlphaCommmandsEnvName, "true")
|
||||
if err := configcobra.NewConfigCommand("").Execute(); err != nil {
|
||||
cmd := configcobra.NewConfigCommand("")
|
||||
complete.Complete(cmd).Complete("config")
|
||||
|
||||
if err := cmd.Execute(); err != nil {
|
||||
os.Exit(1)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -4,6 +4,8 @@ go 1.13
|
||||
|
||||
require (
|
||||
github.com/spf13/cobra v0.0.5
|
||||
k8s.io/api v0.17.0
|
||||
k8s.io/apimachinery v0.17.0
|
||||
k8s.io/cli-runtime v0.17.0
|
||||
k8s.io/client-go v0.17.0
|
||||
k8s.io/component-base v0.17.0 // indirect
|
||||
|
||||
@@ -85,6 +85,7 @@ github.com/go-openapi/jsonreference v0.19.3/go.mod h1:rjx6GuL8TTa9VaixXglHmQmIL9
|
||||
github.com/go-openapi/spec v0.0.0-20160808142527-6aced65f8501/go.mod h1:J8+jY1nAiCcj+friV/PDoE1/3eeccG9LYBs0tYvLOWc=
|
||||
github.com/go-openapi/spec v0.19.3 h1:0XRyw8kguri6Yw4SxhsQA/atC88yqrk0+G4YhI2wabc=
|
||||
github.com/go-openapi/spec v0.19.3/go.mod h1:FpwSN1ksY1eteniUU7X0N/BgJ7a4WvBFVA8Lj9mJglo=
|
||||
github.com/go-openapi/spec v0.19.5 h1:Xm0Ao53uqnk9QE/LlYV5DEU09UAgpliA85QoT9LzqPw=
|
||||
github.com/go-openapi/spec v0.19.5/go.mod h1:Hm2Jr4jv8G1ciIAo+frC/Ft+rR2kQDh8JHKHb3gWUSk=
|
||||
github.com/go-openapi/swag v0.0.0-20160704191624-1d0bd113de87/go.mod h1:DXUve3Dpr1UfpPtxFw+EFuQ41HhCWZfha5jSVRG7C7I=
|
||||
github.com/go-openapi/swag v0.19.2 h1:jvO6bCMBEilGwMfHhrd61zIID4oIFdwb76V17SM88dE=
|
||||
@@ -212,6 +213,7 @@ github.com/prometheus/procfs v0.0.2/go.mod h1:TjEm7ze935MbeOT/UhFTIMYKhuLP4wbCsT
|
||||
github.com/remyoudompheng/bigfft v0.0.0-20170806203942-52369c62f446/go.mod h1:uYEyJGbgTkfkS4+E/PavXkNJcbFIpEtjt2B0KDQ5+9M=
|
||||
github.com/russross/blackfriday v1.5.2 h1:HyvC0ARfnZBqnXwABFeSZHpKvJHJJfPz81GNueLj0oo=
|
||||
github.com/russross/blackfriday v1.5.2/go.mod h1:JO/DiYxRf+HjHt06OyowR9PTA263kcR/rfWxYHBV53g=
|
||||
github.com/sergi/go-diff v1.1.0/go.mod h1:STckp+ISIX8hZLjrqAeVduY0gWCT9IjLuqbuNXdaHfM=
|
||||
github.com/sirupsen/logrus v1.2.0/go.mod h1:LxeOpSwHxABJmUn/MG1IvRgCAasNZTLOkJPxbbu5VWo=
|
||||
github.com/sirupsen/logrus v1.4.2 h1:SPIRibHv4MatM3XXNO2BJeFLZwZ2LvZgfQ5+UNI2im4=
|
||||
github.com/sirupsen/logrus v1.4.2/go.mod h1:tLMulIdttU9McNUspp0xgXVQah82FyeX6MwdIuYE2rE=
|
||||
@@ -330,6 +332,8 @@ gopkg.in/alecthomas/kingpin.v2 v2.2.6/go.mod h1:FMv+mEhP44yOT+4EoQTLFTRgOQ1FBLks
|
||||
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
|
||||
gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127 h1:qIbj1fsPNlZgppZ+VLlY7N33q108Sa+fhmuc+sWQYwY=
|
||||
gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
|
||||
gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15 h1:YR8cESwS4TdDjEe65xsg0ogRM/Nc3DYOhEAlW+xobZo=
|
||||
gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
|
||||
gopkg.in/fsnotify.v1 v1.4.7 h1:xOHLXZwVvI9hhs+cLKq5+I5onOuwQLhQwiu63xxlHs4=
|
||||
gopkg.in/fsnotify.v1 v1.4.7/go.mod h1:Tz8NjZHkW78fSQdbUxIjBTcgA1z1m8ZHf0WmKUhAMys=
|
||||
gopkg.in/inf.v0 v0.9.1 h1:73M5CoZyi3ZLMOyDlQh031Cx6N9NDJ2Vvfl76EDAgDc=
|
||||
|
||||
@@ -17,9 +17,6 @@ import (
|
||||
cmdutil "k8s.io/kubectl/pkg/cmd/util"
|
||||
"k8s.io/kubectl/pkg/util/i18n"
|
||||
"sigs.k8s.io/kustomize/kyaml/commandutil"
|
||||
|
||||
// initialize auth
|
||||
_ "k8s.io/client-go/plugin/pkg/client/auth"
|
||||
)
|
||||
|
||||
// GetCommand returns a command from kubectl to install
|
||||
|
||||
59
cmd/kubectl/kubectlcobra/grouping.go
Normal file
59
cmd/kubectl/kubectlcobra/grouping.go
Normal file
@@ -0,0 +1,59 @@
|
||||
// Copyright 2020 The Kubernetes Authors.
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
// package kubectlcobra contains cobra commands from kubectl
|
||||
package kubectlcobra
|
||||
|
||||
import (
|
||||
"k8s.io/apimachinery/pkg/api/meta"
|
||||
"k8s.io/apimachinery/pkg/runtime"
|
||||
"k8s.io/cli-runtime/pkg/resource"
|
||||
)
|
||||
|
||||
const GroupingLabel = "kustomize.k8s.io/group-id"
|
||||
|
||||
// isGroupingObject returns true if the passed object has the
|
||||
// grouping label.
|
||||
// TODO(seans3): Check type is ConfigMap.
|
||||
func isGroupingObject(obj runtime.Object) bool {
|
||||
if obj == nil {
|
||||
return false
|
||||
}
|
||||
accessor, err := meta.Accessor(obj)
|
||||
if err == nil {
|
||||
labels := accessor.GetLabels()
|
||||
_, exists := labels[GroupingLabel]
|
||||
if exists {
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
// findGroupingObject returns the "Grouping" object (ConfigMap with
|
||||
// grouping label) if it exists, and a boolean describing if it was found.
|
||||
func findGroupingObject(infos []*resource.Info) (*resource.Info, bool) {
|
||||
for _, info := range infos {
|
||||
if info != nil && isGroupingObject(info.Object) {
|
||||
return info, true
|
||||
}
|
||||
}
|
||||
return nil, false
|
||||
}
|
||||
|
||||
// sortGroupingObject reorders the infos slice to place the grouping
|
||||
// object in the first position. Returns true if grouping object found,
|
||||
// false otherwise.
|
||||
func sortGroupingObject(infos []*resource.Info) bool {
|
||||
for i, info := range infos {
|
||||
if info != nil && isGroupingObject(info.Object) {
|
||||
// If the grouping object is not already in the first position,
|
||||
// swap the grouping object with the first object.
|
||||
if i > 0 {
|
||||
infos[0], infos[i] = infos[i], infos[0]
|
||||
}
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
217
cmd/kubectl/kubectlcobra/grouping_test.go
Normal file
217
cmd/kubectl/kubectlcobra/grouping_test.go
Normal file
@@ -0,0 +1,217 @@
|
||||
// Copyright 2020 The Kubernetes Authors.
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
// package kubectlcobra contains cobra commands from kubectl
|
||||
package kubectlcobra
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
corev1 "k8s.io/api/core/v1"
|
||||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
||||
"k8s.io/apimachinery/pkg/runtime"
|
||||
"k8s.io/cli-runtime/pkg/resource"
|
||||
)
|
||||
|
||||
var testNamespace = "test-grouping-namespace"
|
||||
var groupingObjName = "test-grouping-obj"
|
||||
var pod1Name = "pod-1"
|
||||
var pod2Name = "pod-2"
|
||||
var pod3Name = "pod-3"
|
||||
|
||||
var groupingObj = corev1.ConfigMap{
|
||||
ObjectMeta: metav1.ObjectMeta{
|
||||
Namespace: testNamespace,
|
||||
Name: groupingObjName,
|
||||
Labels: map[string]string{
|
||||
GroupingLabel: "true",
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
var groupingInfo = &resource.Info{
|
||||
Namespace: testNamespace,
|
||||
Name: groupingObjName,
|
||||
Object: &groupingObj,
|
||||
}
|
||||
|
||||
var pod1 = corev1.Pod{
|
||||
ObjectMeta: metav1.ObjectMeta{
|
||||
Namespace: testNamespace,
|
||||
Name: pod1Name,
|
||||
},
|
||||
}
|
||||
|
||||
var pod1Info = &resource.Info{
|
||||
Namespace: testNamespace,
|
||||
Name: pod1Name,
|
||||
Object: &pod1,
|
||||
}
|
||||
|
||||
var pod2 = corev1.Pod{
|
||||
ObjectMeta: metav1.ObjectMeta{
|
||||
Namespace: testNamespace,
|
||||
Name: pod2Name,
|
||||
},
|
||||
}
|
||||
|
||||
var pod2Info = &resource.Info{
|
||||
Namespace: testNamespace,
|
||||
Name: pod2Name,
|
||||
Object: &pod2,
|
||||
}
|
||||
|
||||
var pod3 = corev1.Pod{
|
||||
ObjectMeta: metav1.ObjectMeta{
|
||||
Namespace: testNamespace,
|
||||
Name: pod3Name,
|
||||
},
|
||||
}
|
||||
|
||||
var pod3Info = &resource.Info{
|
||||
Namespace: testNamespace,
|
||||
Name: pod3Name,
|
||||
Object: &pod3,
|
||||
}
|
||||
|
||||
func TestIsGroupingObject(t *testing.T) {
|
||||
tests := []struct {
|
||||
obj runtime.Object
|
||||
isGrouping bool
|
||||
}{
|
||||
{
|
||||
obj: nil,
|
||||
isGrouping: false,
|
||||
},
|
||||
{
|
||||
obj: &groupingObj,
|
||||
isGrouping: true,
|
||||
},
|
||||
{
|
||||
obj: &pod2,
|
||||
isGrouping: false,
|
||||
},
|
||||
}
|
||||
|
||||
for _, test := range tests {
|
||||
grouping := isGroupingObject(test.obj)
|
||||
if test.isGrouping && !grouping {
|
||||
t.Errorf("Grouping object not identified: %#v", test.obj)
|
||||
}
|
||||
if !test.isGrouping && grouping {
|
||||
t.Errorf("Non-grouping object identifed as grouping obj: %#v", test.obj)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestFindGroupingObject(t *testing.T) {
|
||||
tests := []struct {
|
||||
infos []*resource.Info
|
||||
exists bool
|
||||
name string
|
||||
}{
|
||||
{
|
||||
infos: []*resource.Info{},
|
||||
exists: false,
|
||||
name: "",
|
||||
},
|
||||
{
|
||||
infos: []*resource.Info{nil},
|
||||
exists: false,
|
||||
name: "",
|
||||
},
|
||||
{
|
||||
infos: []*resource.Info{groupingInfo},
|
||||
exists: true,
|
||||
name: groupingObjName,
|
||||
},
|
||||
{
|
||||
infos: []*resource.Info{pod1Info},
|
||||
exists: false,
|
||||
name: "",
|
||||
},
|
||||
{
|
||||
infos: []*resource.Info{pod1Info, pod2Info, pod3Info},
|
||||
exists: false,
|
||||
name: "",
|
||||
},
|
||||
{
|
||||
infos: []*resource.Info{pod1Info, pod2Info, groupingInfo, pod3Info},
|
||||
exists: true,
|
||||
name: groupingObjName,
|
||||
},
|
||||
}
|
||||
|
||||
for _, test := range tests {
|
||||
groupingObj, found := findGroupingObject(test.infos)
|
||||
if test.exists && !found {
|
||||
t.Errorf("Should have found grouping object")
|
||||
}
|
||||
if !test.exists && found {
|
||||
t.Errorf("Grouping object found, but it does not exist: %#v", groupingObj)
|
||||
}
|
||||
if test.exists && found && test.name != groupingObj.Name {
|
||||
t.Errorf("Grouping object name does not match: %s/%s", test.name, groupingObj.Name)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestSortGroupingObject(t *testing.T) {
|
||||
tests := []struct {
|
||||
infos []*resource.Info
|
||||
sorted bool
|
||||
}{
|
||||
{
|
||||
infos: []*resource.Info{},
|
||||
sorted: false,
|
||||
},
|
||||
{
|
||||
infos: []*resource.Info{groupingInfo},
|
||||
sorted: true,
|
||||
},
|
||||
{
|
||||
infos: []*resource.Info{pod1Info},
|
||||
sorted: false,
|
||||
},
|
||||
{
|
||||
infos: []*resource.Info{pod1Info, pod2Info},
|
||||
sorted: false,
|
||||
},
|
||||
{
|
||||
infos: []*resource.Info{groupingInfo, pod1Info},
|
||||
sorted: true,
|
||||
},
|
||||
{
|
||||
infos: []*resource.Info{pod1Info, groupingInfo},
|
||||
sorted: true,
|
||||
},
|
||||
{
|
||||
infos: []*resource.Info{pod1Info, pod2Info, groupingInfo, pod3Info},
|
||||
sorted: true,
|
||||
},
|
||||
{
|
||||
infos: []*resource.Info{pod1Info, pod2Info, pod3Info, groupingInfo},
|
||||
sorted: true,
|
||||
},
|
||||
{
|
||||
infos: []*resource.Info{groupingInfo, pod1Info, pod2Info, pod3Info},
|
||||
sorted: true,
|
||||
},
|
||||
}
|
||||
|
||||
for _, test := range tests {
|
||||
wasSorted := sortGroupingObject(test.infos)
|
||||
if wasSorted && !test.sorted {
|
||||
t.Errorf("Grouping object was sorted, but it shouldn't have been")
|
||||
}
|
||||
if !wasSorted && test.sorted {
|
||||
t.Errorf("Grouping object was NOT sorted, but it should have been")
|
||||
}
|
||||
if wasSorted {
|
||||
first := test.infos[0]
|
||||
if !isGroupingObject(first.Object) {
|
||||
t.Errorf("Grouping object was not sorted into first position")
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
83
cmd/kubectl/kubectlcobra/inventory.go
Normal file
83
cmd/kubectl/kubectlcobra/inventory.go
Normal file
@@ -0,0 +1,83 @@
|
||||
// Copyright 2020 The Kubernetes Authors.
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
// package kubectlcobra contains cobra commands from kubectl
|
||||
package kubectlcobra
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"strings"
|
||||
|
||||
"k8s.io/apimachinery/pkg/runtime/schema"
|
||||
)
|
||||
|
||||
// Separates inventory fields. This string is allowable as a
|
||||
// ConfigMap key, but it is not allowed as a character in
|
||||
// resource name.
|
||||
const fieldSeparator = "_"
|
||||
|
||||
// Inventory organizes and stores the indentifying information
|
||||
// for an object. This struct (as a string) is stored in a
|
||||
// grouping object to keep track of sets of applied objects.
|
||||
type Inventory struct {
|
||||
Namespace string
|
||||
Name string
|
||||
GroupKind schema.GroupKind
|
||||
}
|
||||
|
||||
// createInventory returns a pointer to an Inventory struct filled
|
||||
// with the passed values. This function validates the passed fields
|
||||
// and returns an error for bad parameters.
|
||||
func createInventory(namespace string,
|
||||
name string, gk schema.GroupKind) (*Inventory, error) {
|
||||
|
||||
// Namespace can be empty, but name cannot.
|
||||
name = strings.TrimSpace(name)
|
||||
if name == "" {
|
||||
return nil, fmt.Errorf("Empty name for inventory object")
|
||||
}
|
||||
if gk.Empty() {
|
||||
return nil, fmt.Errorf("Empty GroupKind for inventory object")
|
||||
}
|
||||
|
||||
return &Inventory{
|
||||
Namespace: strings.TrimSpace(namespace),
|
||||
Name: name,
|
||||
GroupKind: gk,
|
||||
}, nil
|
||||
}
|
||||
|
||||
// parseInventory takes a string, splits it into its five fields,
|
||||
// and returns a pointer to an Inventory struct storing the
|
||||
// five fields. Example inventory string:
|
||||
//
|
||||
// test-namespace/test-name/apps/v1/ReplicaSet
|
||||
//
|
||||
// Returns an error if unable to parse and create the Inventory
|
||||
// struct.
|
||||
func parseInventory(inv string) (*Inventory, error) {
|
||||
parts := strings.Split(inv, fieldSeparator)
|
||||
if len(parts) == 4 {
|
||||
gk := schema.GroupKind{
|
||||
Group: strings.TrimSpace(parts[2]),
|
||||
Kind: strings.TrimSpace(parts[3]),
|
||||
}
|
||||
return createInventory(parts[0], parts[1], gk)
|
||||
}
|
||||
return nil, fmt.Errorf("Unable to decode inventory: %s\n", inv)
|
||||
}
|
||||
|
||||
// Equals returns true if the Inventory structs are identical;
|
||||
// false otherwise.
|
||||
func (i *Inventory) Equals(other *Inventory) bool {
|
||||
return i.String() == other.String()
|
||||
}
|
||||
|
||||
// String create a string version of the Inventory struct.
|
||||
func (i *Inventory) String() string {
|
||||
return fmt.Sprintf("%s%s%s%s%s%s%s",
|
||||
i.Namespace, fieldSeparator,
|
||||
i.Name, fieldSeparator,
|
||||
i.GroupKind.Group, fieldSeparator,
|
||||
i.GroupKind.Kind)
|
||||
}
|
||||
218
cmd/kubectl/kubectlcobra/inventory_test.go
Normal file
218
cmd/kubectl/kubectlcobra/inventory_test.go
Normal file
@@ -0,0 +1,218 @@
|
||||
// Copyright 2020 The Kubernetes Authors.
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
// package kubectlcobra contains cobra commands from kubectl
|
||||
package kubectlcobra
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"k8s.io/apimachinery/pkg/runtime/schema"
|
||||
)
|
||||
|
||||
func TestCreateInventory(t *testing.T) {
|
||||
tests := []struct {
|
||||
namespace string
|
||||
name string
|
||||
gk schema.GroupKind
|
||||
expected string
|
||||
isError bool
|
||||
}{
|
||||
{
|
||||
namespace: " \n",
|
||||
name: " test-name\t",
|
||||
gk: schema.GroupKind{
|
||||
Group: "apps",
|
||||
Kind: "ReplicaSet",
|
||||
},
|
||||
expected: "_test-name_apps_ReplicaSet",
|
||||
isError: false,
|
||||
},
|
||||
{
|
||||
namespace: "test-namespace ",
|
||||
name: " test-name\t",
|
||||
gk: schema.GroupKind{
|
||||
Group: "apps",
|
||||
Kind: "ReplicaSet",
|
||||
},
|
||||
expected: "test-namespace_test-name_apps_ReplicaSet",
|
||||
isError: false,
|
||||
},
|
||||
// Error with empty name.
|
||||
{
|
||||
namespace: "test-namespace ",
|
||||
name: " \t",
|
||||
gk: schema.GroupKind{
|
||||
Group: "apps",
|
||||
Kind: "ReplicaSet",
|
||||
},
|
||||
expected: "",
|
||||
isError: true,
|
||||
},
|
||||
// Error with empty GroupKind.
|
||||
{
|
||||
namespace: "test-namespace",
|
||||
name: "test-name",
|
||||
gk: schema.GroupKind{},
|
||||
expected: "",
|
||||
isError: true,
|
||||
},
|
||||
}
|
||||
|
||||
for _, test := range tests {
|
||||
inv, err := createInventory(test.namespace, test.name, test.gk)
|
||||
if !test.isError {
|
||||
if err != nil {
|
||||
t.Errorf("Error creating inventory when it should have worked.")
|
||||
} else if test.expected != inv.String() {
|
||||
t.Errorf("Expected inventory (%s) != created inventory(%s)\n", test.expected, inv.String())
|
||||
}
|
||||
}
|
||||
if test.isError && err == nil {
|
||||
t.Errorf("Should have returned an error in createInventory()")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestInventoryEqual(t *testing.T) {
|
||||
tests := []struct {
|
||||
inventory1 *Inventory
|
||||
inventory2 *Inventory
|
||||
isEqual bool
|
||||
}{
|
||||
// Two equal inventories without a namespace
|
||||
{
|
||||
inventory1: &Inventory{
|
||||
Name: "test-inv",
|
||||
GroupKind: schema.GroupKind{
|
||||
Group: "apps",
|
||||
Kind: "Deployment",
|
||||
},
|
||||
},
|
||||
inventory2: &Inventory{
|
||||
Name: "test-inv",
|
||||
GroupKind: schema.GroupKind{
|
||||
Group: "apps",
|
||||
Kind: "Deployment",
|
||||
},
|
||||
},
|
||||
isEqual: true,
|
||||
},
|
||||
// Two equal inventories with a namespace
|
||||
{
|
||||
inventory1: &Inventory{
|
||||
Namespace: "test-namespace",
|
||||
Name: "test-inv",
|
||||
GroupKind: schema.GroupKind{
|
||||
Group: "apps",
|
||||
Kind: "Deployment",
|
||||
},
|
||||
},
|
||||
inventory2: &Inventory{
|
||||
Namespace: "test-namespace",
|
||||
Name: "test-inv",
|
||||
GroupKind: schema.GroupKind{
|
||||
Group: "apps",
|
||||
Kind: "Deployment",
|
||||
},
|
||||
},
|
||||
isEqual: true,
|
||||
},
|
||||
// One inventory with a namespace, one without -- not equal.
|
||||
{
|
||||
inventory1: &Inventory{
|
||||
Name: "test-inv",
|
||||
GroupKind: schema.GroupKind{
|
||||
Group: "apps",
|
||||
Kind: "Deployment",
|
||||
},
|
||||
},
|
||||
inventory2: &Inventory{
|
||||
Namespace: "test-namespace",
|
||||
Name: "test-inv",
|
||||
GroupKind: schema.GroupKind{
|
||||
Group: "apps",
|
||||
Kind: "Deployment",
|
||||
},
|
||||
},
|
||||
isEqual: false,
|
||||
},
|
||||
// One inventory with a Deployment, one with a ReplicaSet -- not equal.
|
||||
{
|
||||
inventory1: &Inventory{
|
||||
Name: "test-inv",
|
||||
GroupKind: schema.GroupKind{
|
||||
Group: "apps",
|
||||
Kind: "Deployment",
|
||||
},
|
||||
},
|
||||
inventory2: &Inventory{
|
||||
Name: "test-inv",
|
||||
GroupKind: schema.GroupKind{
|
||||
Group: "apps",
|
||||
Kind: "ReplicaSet",
|
||||
},
|
||||
},
|
||||
isEqual: false,
|
||||
},
|
||||
}
|
||||
|
||||
for _, test := range tests {
|
||||
actual := test.inventory1.Equals(test.inventory2)
|
||||
if test.isEqual && !actual {
|
||||
t.Errorf("Expected inventories equal, but actual is not: (%s)/(%s)\n", test.inventory1, test.inventory2)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestParseInventory(t *testing.T) {
|
||||
tests := []struct {
|
||||
invStr string
|
||||
inventory *Inventory
|
||||
isError bool
|
||||
}{
|
||||
{
|
||||
invStr: "_test-name_apps_ReplicaSet\t",
|
||||
inventory: &Inventory{
|
||||
Name: "test-name",
|
||||
GroupKind: schema.GroupKind{
|
||||
Group: "apps",
|
||||
Kind: "ReplicaSet",
|
||||
},
|
||||
},
|
||||
isError: false,
|
||||
},
|
||||
{
|
||||
invStr: "test-namespace_test-name_apps_Deployment",
|
||||
inventory: &Inventory{
|
||||
Namespace: "test-namespace",
|
||||
Name: "test-name",
|
||||
GroupKind: schema.GroupKind{
|
||||
Group: "apps",
|
||||
Kind: "Deployment",
|
||||
},
|
||||
},
|
||||
isError: false,
|
||||
},
|
||||
// Not enough fields -- error
|
||||
{
|
||||
invStr: "_test-name_apps",
|
||||
inventory: &Inventory{},
|
||||
isError: true,
|
||||
},
|
||||
}
|
||||
|
||||
for _, test := range tests {
|
||||
actual, err := parseInventory(test.invStr)
|
||||
if !test.isError {
|
||||
if err != nil {
|
||||
t.Errorf("Error parsing inventory when it should have worked.")
|
||||
} else if !test.inventory.Equals(actual) {
|
||||
t.Errorf("Expected inventory (%s) != parsed inventory (%s)\n", test.inventory, actual)
|
||||
}
|
||||
}
|
||||
if test.isError && err == nil {
|
||||
t.Errorf("Should have returned an error in parseInventory()")
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -8,6 +8,10 @@ import (
|
||||
|
||||
"sigs.k8s.io/kustomize/cmd/kubectl/kubectlcobra"
|
||||
"sigs.k8s.io/kustomize/kyaml/commandutil"
|
||||
|
||||
// This is here rather than in the libraries because of
|
||||
// https://github.com/kubernetes-sigs/kustomize/issues/2060
|
||||
_ "k8s.io/client-go/plugin/pkg/client/auth"
|
||||
)
|
||||
|
||||
func main() {
|
||||
|
||||
@@ -15,7 +15,7 @@ linters:
|
||||
- dogsled
|
||||
- dupl
|
||||
- errcheck
|
||||
- funlen
|
||||
# - funlen
|
||||
# - gochecknoinits
|
||||
- goconst
|
||||
- gocritic
|
||||
|
||||
@@ -3,13 +3,15 @@ module sigs.k8s.io/kustomize/cmd/resource
|
||||
go 1.12
|
||||
|
||||
require (
|
||||
github.com/acarl005/stripansi v0.0.0-20180116102854-5a71ef0e047d
|
||||
github.com/pkg/errors v0.8.1
|
||||
github.com/spf13/cobra v0.0.5
|
||||
github.com/stretchr/testify v1.4.0
|
||||
golang.org/x/net v0.0.0-20191004110552-13f9640d40b9 // indirect
|
||||
k8s.io/api v0.0.0-20190918155943-95b840bb6a1f
|
||||
k8s.io/apimachinery v0.0.0-20190913080033-27d36303b655
|
||||
k8s.io/client-go v0.0.0-20190918160344-1fbdaa4c8d90
|
||||
sigs.k8s.io/controller-runtime v0.4.0
|
||||
sigs.k8s.io/kustomize/cmd/mdtogo v0.0.0-20191222005333-3900166fdf4f // indirect
|
||||
sigs.k8s.io/kustomize/kstatus v0.0.0-20191204200457-7c1b477ff62d
|
||||
sigs.k8s.io/kustomize/kyaml v0.0.0-20191202204815-0a19a5dbd9b8
|
||||
)
|
||||
|
||||
@@ -3,21 +3,31 @@ cloud.google.com/go v0.34.0/go.mod h1:aQUYkXzVsufM+DwF1aE+0xfcU+56JwCaLick0ClmMT
|
||||
cloud.google.com/go v0.38.0 h1:ROfEUZz+Gh5pa62DJWXSaonyu3StP6EA6lPEXPI6mCo=
|
||||
cloud.google.com/go v0.38.0/go.mod h1:990N+gfupTy94rShfmMCWGDn0LpTmnzTp2qbd1dvSRU=
|
||||
github.com/Azure/go-ansiterm v0.0.0-20170929234023-d6e3b3328b78/go.mod h1:LmzpDX56iTiv29bbRTIsUNlaFfuhWRQBWjQdVyAevI8=
|
||||
github.com/Azure/go-autorest/autorest v0.9.0 h1:MRvx8gncNaXJqOoLmhNjUAKh33JJF8LyxPhomEtOsjs=
|
||||
github.com/Azure/go-autorest/autorest v0.9.0/go.mod h1:xyHB1BMZT0cuDHU7I0+g046+BFDTQ8rEZB0s4Yfa6bI=
|
||||
github.com/Azure/go-autorest/autorest/adal v0.5.0 h1:q2gDruN08/guU9vAjuPWff0+QIrpH6ediguzdAzXAUU=
|
||||
github.com/Azure/go-autorest/autorest/adal v0.5.0/go.mod h1:8Z9fGy2MpX0PvDjB1pEgQTmVqjGhiHBW7RJJEciWzS0=
|
||||
github.com/Azure/go-autorest/autorest/date v0.1.0 h1:YGrhWfrgtFs84+h0o46rJrlmsZtyZRg470CqAXTZaGM=
|
||||
github.com/Azure/go-autorest/autorest/date v0.1.0/go.mod h1:plvfp3oPSKwf2DNjlBjWF/7vwR+cUD/ELuzDCXwHUVA=
|
||||
github.com/Azure/go-autorest/autorest/mocks v0.1.0/go.mod h1:OTyCOPRA2IgIlWxVYxBee2F5Gr4kF2zd2J5cFRaIDN0=
|
||||
github.com/Azure/go-autorest/autorest/mocks v0.2.0 h1:Ww5g4zThfD/6cLb4z6xxgeyDa7QDkizMkJKe0ysZXp0=
|
||||
github.com/Azure/go-autorest/autorest/mocks v0.2.0/go.mod h1:OTyCOPRA2IgIlWxVYxBee2F5Gr4kF2zd2J5cFRaIDN0=
|
||||
github.com/Azure/go-autorest/logger v0.1.0 h1:ruG4BSDXONFRrZZJ2GUXDiUyVpayPmb1GnWeHDdaNKY=
|
||||
github.com/Azure/go-autorest/logger v0.1.0/go.mod h1:oExouG+K6PryycPJfVSxi/koC6LSNgds39diKLz7Vrc=
|
||||
github.com/Azure/go-autorest/tracing v0.5.0 h1:TRn4WjSnkcSy5AEG3pnbtFSwNtwzjr4VYyQflFE619k=
|
||||
github.com/Azure/go-autorest/tracing v0.5.0/go.mod h1:r/s2XiOKccPW3HrqB+W0TQzfbtp2fGCgRFtBroKn4Dk=
|
||||
github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU=
|
||||
github.com/BurntSushi/xgb v0.0.0-20160522181843-27f122750802/go.mod h1:IVnqGOEym/WlBOVXweHU+Q+/VP0lqqI8lqeDx9IjBqo=
|
||||
github.com/NYTimes/gziphandler v0.0.0-20170623195520-56545f4a5d46/go.mod h1:3wb06e3pkSAbeQ52E9H9iFoQsEEwGN64994WTCIhntQ=
|
||||
github.com/PuerkitoBio/purell v1.0.0/go.mod h1:c11w/QuzBsJSee3cPx9rAFu61PvFxuPbtSwDGJws/X0=
|
||||
github.com/PuerkitoBio/purell v1.1.0/go.mod h1:c11w/QuzBsJSee3cPx9rAFu61PvFxuPbtSwDGJws/X0=
|
||||
github.com/PuerkitoBio/purell v1.1.1 h1:WEQqlqaGbrPkxLJWfBwQmfEAE1Z7ONdDLqrN38tNFfI=
|
||||
github.com/PuerkitoBio/purell v1.1.1/go.mod h1:c11w/QuzBsJSee3cPx9rAFu61PvFxuPbtSwDGJws/X0=
|
||||
github.com/PuerkitoBio/urlesc v0.0.0-20160726150825-5bd2802263f2/go.mod h1:uGdkoq3SwY9Y+13GIhn11/XLaGBb4BfwItxLd5jeuXE=
|
||||
github.com/PuerkitoBio/urlesc v0.0.0-20170810143723-de5bf2ad4578 h1:d+Bc7a5rLufV/sSk/8dngufqelfh6jnri85riMAaF/M=
|
||||
github.com/PuerkitoBio/urlesc v0.0.0-20170810143723-de5bf2ad4578/go.mod h1:uGdkoq3SwY9Y+13GIhn11/XLaGBb4BfwItxLd5jeuXE=
|
||||
github.com/acarl005/stripansi v0.0.0-20180116102854-5a71ef0e047d h1:licZJFw2RwpHMqeKTCYkitsPqHNxTmd4SNR5r94FGM8=
|
||||
github.com/acarl005/stripansi v0.0.0-20180116102854-5a71ef0e047d/go.mod h1:asat636LX7Bqt5lYEZ27JNDcqxfjdBQuJ/MM4CN/Lzo=
|
||||
github.com/armon/consul-api v0.0.0-20180202201655-eb2c6b5be1b6/go.mod h1:grANhF5doyWs3UAsr3K4I6qtAmlQcZDesFNEHPZAzj8=
|
||||
github.com/asaskevich/govalidator v0.0.0-20180720115003-f9ffefc3facf/go.mod h1:lB+ZfQJz7igIIfQNfa7Ml4HSf2uFQQRzpGGRXenZAgY=
|
||||
github.com/asaskevich/govalidator v0.0.0-20190424111038-f61b66f89f4a/go.mod h1:lB+ZfQJz7igIIfQNfa7Ml4HSf2uFQQRzpGGRXenZAgY=
|
||||
@@ -39,6 +49,7 @@ github.com/davecgh/go-spew v0.0.0-20151105211317-5215b55f46b2/go.mod h1:J7Y8YcW2
|
||||
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
||||
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
|
||||
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
||||
github.com/dgrijalva/jwt-go v3.2.0+incompatible h1:7qlOGliEKZXTDg6OTjfoBKDXWrumCAMpl/TFQ4/5kLM=
|
||||
github.com/dgrijalva/jwt-go v3.2.0+incompatible/go.mod h1:E3ru+11k8xSBh+hMPgOLZmtrrCbhqsmaPHjLKYnJCaQ=
|
||||
github.com/docker/docker v0.7.3-0.20190327010347-be7ac8be2ae0/go.mod h1:eEKB0N0r5NX/I1kEveEz05bcu8tLC/8azJZsviup8Sk=
|
||||
github.com/docker/go-units v0.3.3/go.mod h1:fgPhTUdO+D/Jk86RDLlptpiXQzgHJF7gydDDbaIK4Dk=
|
||||
@@ -74,10 +85,12 @@ github.com/go-openapi/jsonpointer v0.0.0-20160704185906-46af16f9f7b1/go.mod h1:+
|
||||
github.com/go-openapi/jsonpointer v0.17.0/go.mod h1:cOnomiV+CVVwFLk0A/MExoFMjwdsUdVpsRhURCKh+3M=
|
||||
github.com/go-openapi/jsonpointer v0.18.0/go.mod h1:cOnomiV+CVVwFLk0A/MExoFMjwdsUdVpsRhURCKh+3M=
|
||||
github.com/go-openapi/jsonpointer v0.19.2/go.mod h1:3akKfEdA7DF1sugOqz1dVQHBcuDBPKZGEoHC/NkiQRg=
|
||||
github.com/go-openapi/jsonpointer v0.19.3 h1:gihV7YNZK1iK6Tgwwsxo2rJbD1GTbdm72325Bq8FI3w=
|
||||
github.com/go-openapi/jsonpointer v0.19.3/go.mod h1:Pl9vOtqEWErmShwVjC8pYs9cog34VGT37dQOVbmoatg=
|
||||
github.com/go-openapi/jsonreference v0.0.0-20160704190145-13c6e3589ad9/go.mod h1:W3Z9FmVs9qj+KR4zFKmDPGiLdk1D9Rlm7cyMvf57TTg=
|
||||
github.com/go-openapi/jsonreference v0.17.0/go.mod h1:g4xxGn04lDIRh0GJb5QlpE3HfopLOL6uZrK/VgnsK9I=
|
||||
github.com/go-openapi/jsonreference v0.18.0/go.mod h1:g4xxGn04lDIRh0GJb5QlpE3HfopLOL6uZrK/VgnsK9I=
|
||||
github.com/go-openapi/jsonreference v0.19.2 h1:o20suLFB4Ri0tuzpWtyHlh7E7HnkqTNLq6aR6WVNS1w=
|
||||
github.com/go-openapi/jsonreference v0.19.2/go.mod h1:jMjeRr2HHw6nAVajTXJ4eiUwohSTlpa0o73RUL1owJc=
|
||||
github.com/go-openapi/loads v0.17.0/go.mod h1:72tmFy5wsWx89uEVddd0RjRWPZm92WRLhf7AC+0+OOU=
|
||||
github.com/go-openapi/loads v0.18.0/go.mod h1:72tmFy5wsWx89uEVddd0RjRWPZm92WRLhf7AC+0+OOU=
|
||||
@@ -89,6 +102,7 @@ github.com/go-openapi/spec v0.0.0-20160808142527-6aced65f8501/go.mod h1:J8+jY1nA
|
||||
github.com/go-openapi/spec v0.17.0/go.mod h1:XkF/MOi14NmjsfZ8VtAKf8pIlbZzyoTvZsdfssdxcBI=
|
||||
github.com/go-openapi/spec v0.18.0/go.mod h1:XkF/MOi14NmjsfZ8VtAKf8pIlbZzyoTvZsdfssdxcBI=
|
||||
github.com/go-openapi/spec v0.19.2/go.mod h1:sCxk3jxKgioEJikev4fgkNmwS+3kuYdJtcsZsD5zxMY=
|
||||
github.com/go-openapi/spec v0.19.5 h1:Xm0Ao53uqnk9QE/LlYV5DEU09UAgpliA85QoT9LzqPw=
|
||||
github.com/go-openapi/spec v0.19.5/go.mod h1:Hm2Jr4jv8G1ciIAo+frC/Ft+rR2kQDh8JHKHb3gWUSk=
|
||||
github.com/go-openapi/strfmt v0.17.0/go.mod h1:P82hnJI0CXkErkXi8IKjPbNBM6lV6+5pLP5l494TcyU=
|
||||
github.com/go-openapi/strfmt v0.18.0/go.mod h1:P82hnJI0CXkErkXi8IKjPbNBM6lV6+5pLP5l494TcyU=
|
||||
@@ -97,6 +111,7 @@ github.com/go-openapi/swag v0.0.0-20160704191624-1d0bd113de87/go.mod h1:DXUve3Dp
|
||||
github.com/go-openapi/swag v0.17.0/go.mod h1:AByQ+nYG6gQg71GINrmuDXCPWdL640yX49/kXLo40Tg=
|
||||
github.com/go-openapi/swag v0.18.0/go.mod h1:AByQ+nYG6gQg71GINrmuDXCPWdL640yX49/kXLo40Tg=
|
||||
github.com/go-openapi/swag v0.19.2/go.mod h1:POnQmlKehdgb5mhVOsnJFsivZCEZ/vjK9gh66Z9tfKk=
|
||||
github.com/go-openapi/swag v0.19.5 h1:lTz6Ys4CmqqCQmZPBlbQENR1/GucA2bzYTE12Pw4tFY=
|
||||
github.com/go-openapi/swag v0.19.5/go.mod h1:POnQmlKehdgb5mhVOsnJFsivZCEZ/vjK9gh66Z9tfKk=
|
||||
github.com/go-openapi/validate v0.18.0/go.mod h1:Uh4HdOzKt19xGIGm1qHf/ofbX1YQ4Y+MYsct2VUrAJ4=
|
||||
github.com/go-openapi/validate v0.19.2/go.mod h1:1tRCw7m3jtI8eNWEEliiAqUIcBztB2KDnRCRMUi7GTA=
|
||||
@@ -132,6 +147,7 @@ github.com/googleapis/gax-go/v2 v2.0.4/go.mod h1:0Wqv26UfaUD9n4G6kQubkQ+KchISgw+
|
||||
github.com/googleapis/gnostic v0.0.0-20170729233727-0c5108395e2d/go.mod h1:sJBsCZ4ayReDTBIg8b9dl28c5xFWyhBTVRp3pOg5EKY=
|
||||
github.com/googleapis/gnostic v0.3.1 h1:WeAefnSUHlBb0iJKwxFDZdbfGwkd7xRNuV+IpXMJhYk=
|
||||
github.com/googleapis/gnostic v0.3.1/go.mod h1:on+2t9HRStVgn95RSsFWFz+6Q0Snyqv1awfrALZdbtU=
|
||||
github.com/gophercloud/gophercloud v0.1.0 h1:P/nh25+rzXouhytV2pUHBb65fnds26Ghl8/391+sT5o=
|
||||
github.com/gophercloud/gophercloud v0.1.0/go.mod h1:vxM41WHh5uqHVBMZHzuwNOHh8XEoIEcSTewFxm1c5g8=
|
||||
github.com/gorilla/websocket v1.4.0/go.mod h1:E7qHFY5m1UJ88s3WnNqhKjPHQ0heANvMoAMk2YaljkQ=
|
||||
github.com/gregjones/httpcache v0.0.0-20170728041850-787624de3eb7/go.mod h1:FecbI9+v66THATjSRHfNgh1IVFe/9kFxbXtjV0ctIMA=
|
||||
@@ -170,6 +186,7 @@ github.com/mailru/easyjson v0.0.0-20160728113105-d5b7844b561a/go.mod h1:C1wdFJiN
|
||||
github.com/mailru/easyjson v0.0.0-20180823135443-60711f1a8329/go.mod h1:C1wdFJiN94OJF2b5HbByQZoLdCWB1Yqtg26g4irojpc=
|
||||
github.com/mailru/easyjson v0.0.0-20190312143242-1de009706dbe/go.mod h1:C1wdFJiN94OJF2b5HbByQZoLdCWB1Yqtg26g4irojpc=
|
||||
github.com/mailru/easyjson v0.0.0-20190614124828-94de47d64c63/go.mod h1:C1wdFJiN94OJF2b5HbByQZoLdCWB1Yqtg26g4irojpc=
|
||||
github.com/mailru/easyjson v0.0.0-20190626092158-b2ccc519800e h1:hB2xlXdHp/pmPZq0y3QnmWAArdw9PqbmotexnWx/FU8=
|
||||
github.com/mailru/easyjson v0.0.0-20190626092158-b2ccc519800e/go.mod h1:C1wdFJiN94OJF2b5HbByQZoLdCWB1Yqtg26g4irojpc=
|
||||
github.com/matttproud/golang_protobuf_extensions v1.0.1 h1:4hp9jkHxhMHkqkrB3Ix0jegS5sx/RkqARlsWZ6pIwiU=
|
||||
github.com/matttproud/golang_protobuf_extensions v1.0.1/go.mod h1:D8He9yQNgCq6Z5Ld7szi9bcBfOoFv/3dc6xSMkL2PC0=
|
||||
@@ -215,6 +232,7 @@ github.com/prometheus/procfs v0.0.0-20181204211112-1dc9a6cbc91a h1:9a8MnZMP0X2nL
|
||||
github.com/prometheus/procfs v0.0.0-20181204211112-1dc9a6cbc91a/go.mod h1:c3At6R/oaqEKCNdg8wHV1ftS6bRYblBhIjjI8uT2IGk=
|
||||
github.com/remyoudompheng/bigfft v0.0.0-20170806203942-52369c62f446/go.mod h1:uYEyJGbgTkfkS4+E/PavXkNJcbFIpEtjt2B0KDQ5+9M=
|
||||
github.com/russross/blackfriday v1.5.2/go.mod h1:JO/DiYxRf+HjHt06OyowR9PTA263kcR/rfWxYHBV53g=
|
||||
github.com/sergi/go-diff v1.1.0/go.mod h1:STckp+ISIX8hZLjrqAeVduY0gWCT9IjLuqbuNXdaHfM=
|
||||
github.com/sirupsen/logrus v1.4.2/go.mod h1:tLMulIdttU9McNUspp0xgXVQah82FyeX6MwdIuYE2rE=
|
||||
github.com/soheilhy/cmux v0.1.3/go.mod h1:IM3LyeVVIOuxMH7sFAkER9+bJ4dT7Ms6E4xg4kGIyLM=
|
||||
github.com/spf13/afero v1.1.2/go.mod h1:j4pytiNVoe2o6bmDsKpLACNPDBIoEAkihy7loJ1B0CQ=
|
||||
@@ -349,8 +367,10 @@ google.golang.org/appengine v1.5.0/go.mod h1:xpcJRLb0r/rnEns0DIKYYv+WjYCduHsrkT7
|
||||
google.golang.org/genproto v0.0.0-20180817151627-c66870c02cf8/go.mod h1:JiN7NxoALGmiZfu7CAH4rXhgtRTLTxftemlI0sWmxmc=
|
||||
google.golang.org/genproto v0.0.0-20190307195333-5fe7a883aa19/go.mod h1:VzzqZJRnGkLBvHegQrXjBqPurQTc5/KpmUdxsrq26oE=
|
||||
google.golang.org/genproto v0.0.0-20190418145605-e7d98fc518a7/go.mod h1:VzzqZJRnGkLBvHegQrXjBqPurQTc5/KpmUdxsrq26oE=
|
||||
google.golang.org/genproto v0.0.0-20190502173448-54afdca5d873 h1:nfPFGzJkUDX6uBmpN/pSw7MbOAWegH5QDQuoXFHedLg=
|
||||
google.golang.org/genproto v0.0.0-20190502173448-54afdca5d873/go.mod h1:VzzqZJRnGkLBvHegQrXjBqPurQTc5/KpmUdxsrq26oE=
|
||||
google.golang.org/grpc v1.19.0/go.mod h1:mqu4LbDTu4XGKhr4mRzUsmM4RtVoemTSY81AxZiDr8c=
|
||||
google.golang.org/grpc v1.23.0 h1:AzbTB6ux+okLTzP8Ru1Xs41C303zdcfEht7MQnYJt5A=
|
||||
google.golang.org/grpc v1.23.0/go.mod h1:Y5yQAOtifL1yxbo5wqy6BxZv8vAUGQwXBOALyacEbxg=
|
||||
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
|
||||
gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
|
||||
@@ -407,9 +427,6 @@ modernc.org/strutil v1.0.0/go.mod h1:lstksw84oURvj9y3tn8lGvRxyRC1S2+g5uuIzNfIOBs
|
||||
modernc.org/xc v1.0.0/go.mod h1:mRNCo0bvLjGhHO9WsyuKVU4q0ceiDDDoEeWDJHrNx8I=
|
||||
sigs.k8s.io/controller-runtime v0.4.0 h1:wATM6/m+3w8lj8FXNaO6Fs/rq/vqoOjO1Q116Z9NPsg=
|
||||
sigs.k8s.io/controller-runtime v0.4.0/go.mod h1:ApC79lpY3PHW9xj/w9pj+lYkLgwAAUZwfXkME1Lajns=
|
||||
sigs.k8s.io/kustomize v2.0.3+incompatible h1:JUufWFNlI44MdtnjUqVnvh29rR37PQFzPbLXqhyOyX0=
|
||||
sigs.k8s.io/kustomize/cmd/mdtogo v0.0.0-20191222005333-3900166fdf4f h1:Wdh26pJ0THtsuSB1DCkaLc1Ssv2NDddB7E7vCSdTHdg=
|
||||
sigs.k8s.io/kustomize/cmd/mdtogo v0.0.0-20191222005333-3900166fdf4f/go.mod h1:arffnBwv6VTLUY3hxATxJ2fwNMWy92GSXm6UXEjFddQ=
|
||||
sigs.k8s.io/structured-merge-diff v0.0.0-20190525122527-15d366b2352e/go.mod h1:wWxsB5ozmmv/SG7nM11ayaAW51xMvak/t1r0CSlcokI=
|
||||
sigs.k8s.io/structured-merge-diff v0.0.0-20190817042607-6149e4549fca/go.mod h1:IIgPezJWb76P0hotTxzDbWsMYB8APh18qZnxkomBpxA=
|
||||
sigs.k8s.io/testing_frameworks v0.1.2 h1:vK0+tvjF0BZ/RYFeZ1E6BYBwHJJXhjuZ3TdsEKH+UQM=
|
||||
|
||||
@@ -10,7 +10,9 @@ import (
|
||||
|
||||
"sigs.k8s.io/kustomize/cmd/resource/status"
|
||||
|
||||
_ "k8s.io/client-go/plugin/pkg/client/auth/gcp"
|
||||
// This is here rather than in the libraries because of
|
||||
// https://github.com/kubernetes-sigs/kustomize/issues/2060
|
||||
_ "k8s.io/client-go/plugin/pkg/client/auth"
|
||||
)
|
||||
|
||||
var root = &cobra.Command{
|
||||
|
||||
@@ -16,7 +16,9 @@ import (
|
||||
|
||||
// GetEventsRunner returns a command EventsRunner.
|
||||
func GetEventsRunner() *EventsRunner {
|
||||
r := &EventsRunner{}
|
||||
r := &EventsRunner{
|
||||
createClientFunc: createClient,
|
||||
}
|
||||
c := &cobra.Command{
|
||||
Use: "events DIR...",
|
||||
Short: commands.EventsShort,
|
||||
@@ -46,13 +48,15 @@ type EventsRunner struct {
|
||||
Interval time.Duration
|
||||
Timeout time.Duration
|
||||
Command *cobra.Command
|
||||
|
||||
createClientFunc createClientFunc
|
||||
}
|
||||
|
||||
func (r *EventsRunner) runE(c *cobra.Command, args []string) error {
|
||||
ctx := context.Background()
|
||||
|
||||
// Create a client and use it to set up a new resolver.
|
||||
client, err := getClient()
|
||||
client, err := r.createClientFunc()
|
||||
if err != nil {
|
||||
return errors.Wrap(err, "error creating client")
|
||||
}
|
||||
|
||||
298
cmd/resource/status/cmd/events_test.go
Normal file
298
cmd/resource/status/cmd/events_test.go
Normal file
@@ -0,0 +1,298 @@
|
||||
package cmd
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"fmt"
|
||||
"reflect"
|
||||
"regexp"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
"sigs.k8s.io/kustomize/kstatus/status"
|
||||
"sigs.k8s.io/kustomize/kstatus/wait"
|
||||
)
|
||||
|
||||
func TestEventsNoResources(t *testing.T) {
|
||||
inBuffer := &bytes.Buffer{}
|
||||
outBuffer := &bytes.Buffer{}
|
||||
|
||||
fakeClient := &FakeClient{}
|
||||
|
||||
r := GetEventsRunner()
|
||||
r.createClientFunc = newClientFunc(fakeClient)
|
||||
r.Command.SetArgs([]string{})
|
||||
r.Command.SetIn(inBuffer)
|
||||
r.Command.SetOut(outBuffer)
|
||||
|
||||
err := r.Command.Execute()
|
||||
if !assert.NoError(t, err) {
|
||||
return
|
||||
}
|
||||
|
||||
eventOutput := parseEventOutput(t, outBuffer.String())
|
||||
|
||||
if want, got := 1, len(eventOutput.events); want != got {
|
||||
t.Errorf("expected %d events, but got %d", want, got)
|
||||
}
|
||||
|
||||
event := eventOutput.events[0]
|
||||
if want, got := status.CurrentStatus, event.aggStatus; want != got {
|
||||
t.Errorf("expected agg status %s, but got %s", want, got)
|
||||
}
|
||||
}
|
||||
|
||||
func TestEventsMultipleUpdates(t *testing.T) {
|
||||
inBuffer := &bytes.Buffer{}
|
||||
outBuffer := &bytes.Buffer{}
|
||||
|
||||
_, err := fmt.Fprint(inBuffer, `
|
||||
apiVersion: apps/v1
|
||||
kind: Deployment
|
||||
metadata:
|
||||
name: bar
|
||||
namespace: default
|
||||
`)
|
||||
if !assert.NoError(t, err) {
|
||||
return
|
||||
}
|
||||
|
||||
fakeClient := &FakeClient{
|
||||
resourceCallbackMap: map[string]ResourceGetCallback{
|
||||
"Deployment": createDeploymentStatusFunc(),
|
||||
},
|
||||
}
|
||||
|
||||
r := GetEventsRunner()
|
||||
r.createClientFunc = newClientFunc(fakeClient)
|
||||
r.Command.SetArgs([]string{})
|
||||
r.Command.SetIn(inBuffer)
|
||||
r.Command.SetOut(outBuffer)
|
||||
|
||||
err = r.Command.Execute()
|
||||
if !assert.NoError(t, err) {
|
||||
return
|
||||
}
|
||||
|
||||
eventOutput := parseEventOutput(t, outBuffer.String())
|
||||
|
||||
aggStatuses := eventOutput.allAggStatuses()
|
||||
expectedAggStatuses := []status.Status{
|
||||
status.InProgressStatus,
|
||||
status.InProgressStatus,
|
||||
status.InProgressStatus,
|
||||
status.InProgressStatus,
|
||||
status.CurrentStatus,
|
||||
status.CurrentStatus,
|
||||
}
|
||||
if !reflect.DeepEqual(aggStatuses, expectedAggStatuses) {
|
||||
t.Errorf("expected agg statuses to be %s, but got %s", joinStatuses(expectedAggStatuses),
|
||||
joinStatuses(aggStatuses))
|
||||
}
|
||||
|
||||
resources := eventOutput.allResources()
|
||||
if want, got := 1, len(resources); want != got {
|
||||
t.Errorf("expected %d resource, but got %d", want, got)
|
||||
}
|
||||
|
||||
resource := resources[0]
|
||||
resourceStatuses := eventOutput.statusesForResource(resource)
|
||||
expectedResourceStatuses := []status.Status{
|
||||
status.InProgressStatus,
|
||||
status.InProgressStatus,
|
||||
status.InProgressStatus,
|
||||
status.InProgressStatus,
|
||||
status.CurrentStatus,
|
||||
}
|
||||
if !reflect.DeepEqual(resourceStatuses, expectedResourceStatuses) {
|
||||
t.Errorf("expected statuses to be %s, but got %s", joinStatuses(expectedResourceStatuses),
|
||||
joinStatuses(resourceStatuses))
|
||||
}
|
||||
}
|
||||
|
||||
func TestEventsMultipleResources(t *testing.T) {
|
||||
inBuffer := &bytes.Buffer{}
|
||||
outBuffer := &bytes.Buffer{}
|
||||
|
||||
_, err := fmt.Fprint(inBuffer, `
|
||||
apiVersion: v1
|
||||
kind: List
|
||||
items:
|
||||
- apiVersion: v1
|
||||
kind: Pod
|
||||
metadata:
|
||||
name: foo
|
||||
namespace: default
|
||||
- apiVersion: v1
|
||||
kind: Service
|
||||
metadata:
|
||||
name: bar
|
||||
namespace: default
|
||||
`)
|
||||
if !assert.NoError(t, err) {
|
||||
return
|
||||
}
|
||||
|
||||
fakeClient := &FakeClient{
|
||||
resourceCallbackMap: map[string]ResourceGetCallback{
|
||||
"Pod": createPodStatusFunc(),
|
||||
"Service": createServiceStatusFunc(),
|
||||
},
|
||||
}
|
||||
|
||||
r := GetEventsRunner()
|
||||
r.createClientFunc = newClientFunc(fakeClient)
|
||||
r.Command.SetArgs([]string{})
|
||||
r.Command.SetIn(inBuffer)
|
||||
r.Command.SetOut(outBuffer)
|
||||
|
||||
err = r.Command.Execute()
|
||||
if !assert.NoError(t, err) {
|
||||
return
|
||||
}
|
||||
|
||||
eventOutput := parseEventOutput(t, outBuffer.String())
|
||||
|
||||
aggStatuses := eventOutput.allAggStatuses()
|
||||
expectedAggStatuses := []status.Status{
|
||||
status.UnknownStatus,
|
||||
status.CurrentStatus,
|
||||
status.CurrentStatus,
|
||||
}
|
||||
if !reflect.DeepEqual(aggStatuses, expectedAggStatuses) {
|
||||
t.Errorf("expected agg statuses to be %s, but got %s", joinStatuses(expectedAggStatuses),
|
||||
joinStatuses(aggStatuses))
|
||||
}
|
||||
|
||||
resources := eventOutput.allResources()
|
||||
if want, got := 2, len(resources); got != want {
|
||||
t.Errorf("expected %d resource, but got %d", want, got)
|
||||
}
|
||||
|
||||
for _, resource := range resources {
|
||||
resourceStatuses := eventOutput.statusesForResource(resource)
|
||||
if want, got := status.CurrentStatus, resourceStatuses[len(resourceStatuses)-1]; want != got {
|
||||
t.Errorf("expected resource %q to have final status %s, but got %s", resource.name, want, got)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
type EventOutput struct {
|
||||
events []EventOutputLine
|
||||
unknownLines []string
|
||||
}
|
||||
|
||||
func (e *EventOutput) allAggStatuses() []status.Status {
|
||||
var aggStatuses []status.Status
|
||||
for _, event := range e.events {
|
||||
aggStatuses = append(aggStatuses, event.aggStatus)
|
||||
}
|
||||
return aggStatuses
|
||||
}
|
||||
|
||||
func (e *EventOutput) allResources() []ResourceIdentifier {
|
||||
var resources []ResourceIdentifier
|
||||
seenResources := make(map[ResourceIdentifier]bool)
|
||||
for _, event := range e.events {
|
||||
if !event.isResourceUpdateEvent() {
|
||||
continue
|
||||
}
|
||||
r := event.identifier
|
||||
if _, found := seenResources[r]; !found {
|
||||
resources = append(resources, r)
|
||||
seenResources[r] = true
|
||||
}
|
||||
}
|
||||
return resources
|
||||
}
|
||||
|
||||
func (e *EventOutput) statusesForResource(resource ResourceIdentifier) []status.Status {
|
||||
var statuses []status.Status
|
||||
for _, event := range e.events {
|
||||
if !event.isResourceUpdateEvent() {
|
||||
continue
|
||||
}
|
||||
if event.identifier.Equals(resource) {
|
||||
statuses = append(statuses, event.status)
|
||||
}
|
||||
}
|
||||
return statuses
|
||||
}
|
||||
|
||||
type EventOutputLine struct {
|
||||
eventType string
|
||||
aggStatus status.Status
|
||||
identifier ResourceIdentifier
|
||||
status status.Status
|
||||
message string
|
||||
}
|
||||
|
||||
func (e *EventOutputLine) isResourceUpdateEvent() bool {
|
||||
return e.eventType == string(wait.ResourceUpdate)
|
||||
}
|
||||
|
||||
var (
|
||||
eventRegex = regexp.MustCompile(`^\s*` +
|
||||
`(?P<eventType>\S+)\s+` +
|
||||
`(?P<aggStatus>\S+)\s+` +
|
||||
`((?P<resourceType>\S+)\s+` +
|
||||
`(?P<namespace>\S+)\s+` +
|
||||
`(?P<name>\S+)\s+` +
|
||||
`(?P<status>\S+)\s+` +
|
||||
`(?P<message>.*\S)){0,1}` +
|
||||
`\s*$`)
|
||||
eventHeaderRegex = regexp.MustCompile(`^\s*` +
|
||||
`EVENT TYPE\s+` +
|
||||
`AGG STATUS\s+` +
|
||||
`TYPE\s+` +
|
||||
`NAMESPACE\s+` +
|
||||
`NAME\s+` +
|
||||
`STATUS\s+` +
|
||||
`MESSAGE` +
|
||||
`\s*$`)
|
||||
)
|
||||
|
||||
func parseEventOutput(_ *testing.T, output string) EventOutput {
|
||||
var eventOutput EventOutput
|
||||
lines := strings.Split(output, "\n")
|
||||
for _, line := range lines {
|
||||
if len(line) == 0 {
|
||||
continue // Ignore empty lines
|
||||
}
|
||||
match := eventHeaderRegex.FindStringSubmatch(line)
|
||||
if match != nil {
|
||||
continue // Ignore headers
|
||||
}
|
||||
match = eventRegex.FindStringSubmatch(line)
|
||||
if match == nil {
|
||||
eventOutput.unknownLines = append(eventOutput.unknownLines, line)
|
||||
continue
|
||||
}
|
||||
|
||||
eventOutputLine := EventOutputLine{
|
||||
eventType: match[1],
|
||||
aggStatus: status.FromStringOrDie(match[2]),
|
||||
}
|
||||
|
||||
if eventOutputLine.eventType == string(wait.ResourceUpdate) {
|
||||
resourceType := match[4]
|
||||
parts := strings.Split(resourceType, "/")
|
||||
var identifier ResourceIdentifier
|
||||
if len(parts) == 2 {
|
||||
identifier.apiVersion = parts[0]
|
||||
identifier.kind = parts[1]
|
||||
} else {
|
||||
identifier.apiVersion = strings.Join(parts[:2], "/")
|
||||
identifier.kind = parts[2]
|
||||
}
|
||||
identifier.namespace = match[5]
|
||||
identifier.name = match[6]
|
||||
eventOutputLine.identifier = identifier
|
||||
eventOutputLine.status = status.FromStringOrDie(match[7])
|
||||
eventOutputLine.message = match[8]
|
||||
}
|
||||
|
||||
eventOutput.events = append(eventOutput.events, eventOutputLine)
|
||||
}
|
||||
return eventOutput
|
||||
}
|
||||
@@ -17,7 +17,9 @@ import (
|
||||
|
||||
// GetFetchRunner returns a command FetchRunner.
|
||||
func GetFetchRunner() *FetchRunner {
|
||||
r := &FetchRunner{}
|
||||
r := &FetchRunner{
|
||||
createClientFunc: createClient,
|
||||
}
|
||||
c := &cobra.Command{
|
||||
Use: "fetch DIR...",
|
||||
Short: commands.FetchShort,
|
||||
@@ -41,18 +43,20 @@ func FetchCommand() *cobra.Command {
|
||||
type FetchRunner struct {
|
||||
IncludeSubpackages bool
|
||||
Command *cobra.Command
|
||||
|
||||
createClientFunc createClientFunc
|
||||
}
|
||||
|
||||
func (r *FetchRunner) runE(c *cobra.Command, args []string) error {
|
||||
ctx := context.Background()
|
||||
|
||||
// Create a new client and use it to set up a resolver.
|
||||
client, err := getClient()
|
||||
k8sClient, err := r.createClientFunc()
|
||||
if err != nil {
|
||||
return errors.Wrap(err, "error creating client")
|
||||
return errors.Wrap(err, "error creating k8sClient")
|
||||
}
|
||||
|
||||
resolver := wait.NewResolver(client, time.Minute)
|
||||
resolver := wait.NewResolver(k8sClient, time.Minute)
|
||||
|
||||
// Set up a CaptureIdentifierFilter and run all inputs through the
|
||||
// filter with the pipeline to capture the inventory of resources
|
||||
|
||||
230
cmd/resource/status/cmd/fetch_test.go
Normal file
230
cmd/resource/status/cmd/fetch_test.go
Normal file
@@ -0,0 +1,230 @@
|
||||
package cmd
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"fmt"
|
||||
"io/ioutil"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"github.com/acarl005/stripansi"
|
||||
"github.com/stretchr/testify/assert"
|
||||
appsv1 "k8s.io/api/apps/v1"
|
||||
v1 "k8s.io/api/core/v1"
|
||||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
||||
"sigs.k8s.io/controller-runtime/pkg/client/fake"
|
||||
"sigs.k8s.io/kustomize/kstatus/status"
|
||||
)
|
||||
|
||||
func TestEmptyManifest(t *testing.T) {
|
||||
inBuffer := &bytes.Buffer{}
|
||||
outBuffer := &bytes.Buffer{}
|
||||
|
||||
fakeClient := fake.NewFakeClientWithScheme(scheme)
|
||||
|
||||
r := GetFetchRunner()
|
||||
r.createClientFunc = newClientFunc(fakeClient)
|
||||
r.Command.SetArgs([]string{})
|
||||
r.Command.SetIn(inBuffer)
|
||||
r.Command.SetOut(outBuffer)
|
||||
|
||||
err := r.Command.Execute()
|
||||
if !assert.NoError(t, err) {
|
||||
return
|
||||
}
|
||||
|
||||
output := outBuffer.String()
|
||||
lines := strings.Split(output, "\n")
|
||||
|
||||
if want, got := 2, len(lines); want != got {
|
||||
t.Errorf("Expected %d lines, but got %d", want, got)
|
||||
}
|
||||
}
|
||||
|
||||
func TestFetchStatusFromManifestStdIn(t *testing.T) {
|
||||
inBuffer := &bytes.Buffer{}
|
||||
outBuffer := &bytes.Buffer{}
|
||||
|
||||
_, err := fmt.Fprint(inBuffer, `
|
||||
apiVersion: apps/v1
|
||||
kind: Deployment
|
||||
metadata:
|
||||
name: bar
|
||||
namespace: default
|
||||
`)
|
||||
if !assert.NoError(t, err) {
|
||||
return
|
||||
}
|
||||
|
||||
deployment := createDeployment("bar", "default", 42, appsv1.DeploymentStatus{
|
||||
ObservedGeneration: 1,
|
||||
})
|
||||
|
||||
fakeClient := fake.NewFakeClientWithScheme(scheme, deployment)
|
||||
|
||||
r := GetFetchRunner()
|
||||
r.createClientFunc = newClientFunc(fakeClient)
|
||||
r.Command.SetArgs([]string{})
|
||||
r.Command.SetIn(inBuffer)
|
||||
r.Command.SetOut(outBuffer)
|
||||
|
||||
err = r.Command.Execute()
|
||||
if !assert.NoError(t, err) {
|
||||
return
|
||||
}
|
||||
cleanOutput := stripansi.Strip(outBuffer.String())
|
||||
tableOutput := parseTableOutput(t, cleanOutput)
|
||||
|
||||
expectedResource := ResourceIdentifier{
|
||||
apiVersion: "apps/v1",
|
||||
kind: "Deployment",
|
||||
namespace: "default",
|
||||
name: "bar",
|
||||
}
|
||||
expectedStatus := status.InProgressStatus
|
||||
expectedMessage := "Deployment generation is 2, but latest observed generation is 1"
|
||||
|
||||
verifyOutputContains(t, tableOutput, expectedResource, expectedStatus, expectedMessage)
|
||||
}
|
||||
|
||||
//nolint:funlen
|
||||
func TestFetchStatusFromManifestsFiles(t *testing.T) {
|
||||
d, err := ioutil.TempDir("", "status-fetch-test")
|
||||
if !assert.NoError(t, err) {
|
||||
return
|
||||
}
|
||||
defer os.RemoveAll(d)
|
||||
|
||||
err = ioutil.WriteFile(filepath.Join(d, "dep.yaml"), []byte(`
|
||||
apiVersion: apps/v1
|
||||
kind: Deployment
|
||||
metadata:
|
||||
name: foo
|
||||
namespace: default
|
||||
`), 0600)
|
||||
if !assert.NoError(t, err) {
|
||||
return
|
||||
}
|
||||
err = ioutil.WriteFile(filepath.Join(d, "svc.yaml"), []byte(`
|
||||
apiVersion: v1
|
||||
kind: Service
|
||||
metadata:
|
||||
name: foo
|
||||
namespace: default
|
||||
`), 0600)
|
||||
if !assert.NoError(t, err) {
|
||||
return
|
||||
}
|
||||
|
||||
replicas := int32(42)
|
||||
deployment := createDeployment("foo", "default", replicas, appsv1.DeploymentStatus{
|
||||
ObservedGeneration: 2,
|
||||
Replicas: replicas,
|
||||
ReadyReplicas: replicas,
|
||||
AvailableReplicas: replicas,
|
||||
UpdatedReplicas: replicas,
|
||||
Conditions: []appsv1.DeploymentCondition{
|
||||
{
|
||||
Type: appsv1.DeploymentAvailable,
|
||||
Status: v1.ConditionTrue,
|
||||
},
|
||||
},
|
||||
})
|
||||
service := createService("foo", "default")
|
||||
|
||||
fakeClient := fake.NewFakeClientWithScheme(scheme, deployment, service)
|
||||
|
||||
outBuffer := &bytes.Buffer{}
|
||||
|
||||
r := GetFetchRunner()
|
||||
r.createClientFunc = newClientFunc(fakeClient)
|
||||
r.Command.SetArgs([]string{d})
|
||||
r.Command.SetOut(outBuffer)
|
||||
|
||||
err = r.Command.Execute()
|
||||
if !assert.NoError(t, err) {
|
||||
return
|
||||
}
|
||||
|
||||
cleanOutput := stripansi.Strip(outBuffer.String())
|
||||
tableOutput := parseTableOutput(t, cleanOutput)
|
||||
|
||||
expectedDeploymentResource := ResourceIdentifier{
|
||||
apiVersion: "apps/v1",
|
||||
kind: "Deployment",
|
||||
namespace: "default",
|
||||
name: "foo",
|
||||
}
|
||||
expectedDeploymentStatus := status.CurrentStatus
|
||||
expectedDeploymentMessage := "Deployment is available. Replicas: 42"
|
||||
verifyOutputContains(t, tableOutput, expectedDeploymentResource, expectedDeploymentStatus, expectedDeploymentMessage)
|
||||
|
||||
expectedServiceResource := ResourceIdentifier{
|
||||
apiVersion: "v1",
|
||||
kind: "Service",
|
||||
namespace: "default",
|
||||
name: "foo",
|
||||
}
|
||||
expectedServiceStatus := status.CurrentStatus
|
||||
expectedServiceMessage := "Service is ready"
|
||||
|
||||
verifyOutputContains(t, tableOutput, expectedServiceResource, expectedServiceStatus, expectedServiceMessage)
|
||||
}
|
||||
|
||||
func createDeployment(name, namespace string, replicas int32, status appsv1.DeploymentStatus) *appsv1.Deployment {
|
||||
return &appsv1.Deployment{
|
||||
TypeMeta: metav1.TypeMeta{
|
||||
APIVersion: "apps/v1",
|
||||
Kind: "Deployment",
|
||||
},
|
||||
ObjectMeta: metav1.ObjectMeta{
|
||||
Name: name,
|
||||
Namespace: namespace,
|
||||
Generation: 2,
|
||||
},
|
||||
Spec: appsv1.DeploymentSpec{
|
||||
Replicas: &replicas,
|
||||
},
|
||||
Status: status,
|
||||
}
|
||||
}
|
||||
|
||||
func createService(name, namespace string) *v1.Service {
|
||||
return &v1.Service{
|
||||
TypeMeta: metav1.TypeMeta{
|
||||
APIVersion: "v1",
|
||||
Kind: "Service",
|
||||
},
|
||||
ObjectMeta: metav1.ObjectMeta{
|
||||
Name: name,
|
||||
Namespace: namespace,
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
func verifyOutputContains(t *testing.T, tableOutput TableOutput, resource ResourceIdentifier, status status.Status, message string) {
|
||||
if len(tableOutput.Frames) == 0 {
|
||||
t.Fatalf("expected match for resource %s, but output had no frames", resource.name)
|
||||
}
|
||||
firstFrame := tableOutput.Frames[0]
|
||||
var foundResource ResourceOutput
|
||||
match := false
|
||||
for _, resourceOutput := range firstFrame.Resources {
|
||||
if resourceOutput.identifier.Equals(resource) {
|
||||
foundResource = resourceOutput
|
||||
match = true
|
||||
break
|
||||
}
|
||||
}
|
||||
if !match {
|
||||
t.Errorf("expected match for resource %s, but didn't find it", resource.name)
|
||||
}
|
||||
if want, got := status, foundResource.status; want != got {
|
||||
t.Errorf("expected status %s for resource %s, but got %s", want, resource.name, got)
|
||||
}
|
||||
if want, got := message, foundResource.message; !strings.HasPrefix(want, got) {
|
||||
t.Errorf("expected message %s for resource %s, but got %s", want, resource.name, got)
|
||||
}
|
||||
}
|
||||
245
cmd/resource/status/cmd/helpers_test.go
Normal file
245
cmd/resource/status/cmd/helpers_test.go
Normal file
@@ -0,0 +1,245 @@
|
||||
package cmd
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"regexp"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
|
||||
"k8s.io/apimachinery/pkg/runtime"
|
||||
"sigs.k8s.io/controller-runtime/pkg/client"
|
||||
"sigs.k8s.io/kustomize/kstatus/status"
|
||||
)
|
||||
|
||||
type TableOutput struct {
|
||||
Frames []TableOutputFrame
|
||||
UnknownRows []string
|
||||
}
|
||||
|
||||
func (t *TableOutput) allAggStatuses() []status.Status {
|
||||
var statuses []status.Status
|
||||
for _, frame := range t.Frames {
|
||||
if frame.AggregateStatus != "" {
|
||||
statuses = append(statuses, frame.AggregateStatus)
|
||||
}
|
||||
}
|
||||
return statuses
|
||||
}
|
||||
|
||||
func (t *TableOutput) dedupedAggStatuses() []status.Status {
|
||||
var dedupedStatuses []status.Status
|
||||
statuses := t.allAggStatuses()
|
||||
var previousStatus status.Status
|
||||
for _, s := range statuses {
|
||||
if s != previousStatus {
|
||||
dedupedStatuses = append(dedupedStatuses, s)
|
||||
previousStatus = s
|
||||
}
|
||||
}
|
||||
return dedupedStatuses
|
||||
}
|
||||
|
||||
func (t *TableOutput) resources() []ResourceIdentifier {
|
||||
seenResources := make(map[ResourceIdentifier]bool)
|
||||
var resources []ResourceIdentifier
|
||||
for _, frame := range t.Frames {
|
||||
for _, resource := range frame.Resources {
|
||||
r := resource.identifier
|
||||
_, found := seenResources[r]
|
||||
if !found {
|
||||
seenResources[r] = true
|
||||
resources = append(resources, r)
|
||||
}
|
||||
}
|
||||
}
|
||||
return resources
|
||||
}
|
||||
|
||||
func (t *TableOutput) dedupedStatusesForResource(resource ResourceIdentifier) []status.Status {
|
||||
var dedupedStatuses []status.Status
|
||||
var previousStatus status.Status
|
||||
for _, frame := range t.Frames {
|
||||
for _, r := range frame.Resources {
|
||||
if r.identifier.Equals(resource) {
|
||||
if r.status != previousStatus {
|
||||
previousStatus = r.status
|
||||
dedupedStatuses = append(dedupedStatuses, r.status)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return dedupedStatuses
|
||||
}
|
||||
|
||||
type TableOutputFrame struct {
|
||||
AggregateStatus status.Status
|
||||
Resources []ResourceOutput
|
||||
}
|
||||
|
||||
type ResourceIdentifier struct {
|
||||
apiVersion string
|
||||
kind string
|
||||
name string
|
||||
namespace string
|
||||
}
|
||||
|
||||
func (r ResourceIdentifier) Equals(identifier ResourceIdentifier) bool {
|
||||
return r.apiVersion == identifier.apiVersion &&
|
||||
r.kind == identifier.kind &&
|
||||
r.namespace == identifier.namespace &&
|
||||
r.name == identifier.name
|
||||
}
|
||||
|
||||
type ResourceOutput struct {
|
||||
identifier ResourceIdentifier
|
||||
status status.Status
|
||||
message string
|
||||
}
|
||||
|
||||
var (
|
||||
headerRegex = regexp.MustCompile(`^\s*TYPE\s+NAMESPACE\s+NAME\s+STATUS\s+MESSAGE\s*$`)
|
||||
resourceRegex = regexp.MustCompile(`^(?P<resourceType>\S+)\s+(?P<namespace>\S+)\s+(?P<name>\S+)\s+(?P<status>\S+)\s+(?P<message>.*\S)\s*$`)
|
||||
aggStatusRegex = regexp.MustCompile(`^\s*AggregateStatus: (?P<aggregateStatus>\S+)\s*$`)
|
||||
)
|
||||
|
||||
func parseTableOutput(_ *testing.T, output string) TableOutput {
|
||||
tableOutput := TableOutput{}
|
||||
|
||||
lines := strings.Split(output, "\n")
|
||||
|
||||
hasAggStatus := false
|
||||
var currentFrame TableOutputFrame
|
||||
for i, line := range lines {
|
||||
if len(line) == 0 {
|
||||
continue // We don't care about empty lines.
|
||||
}
|
||||
|
||||
// Check for lines with aggregate status. They are not always present, but if they are,
|
||||
// they always start a new frame of output.
|
||||
match := aggStatusRegex.FindStringSubmatch(line)
|
||||
if match != nil {
|
||||
hasAggStatus = true
|
||||
if i != 0 {
|
||||
tableOutput.Frames = append(tableOutput.Frames, currentFrame)
|
||||
}
|
||||
currentFrame = TableOutputFrame{
|
||||
AggregateStatus: status.FromStringOrDie(match[1]),
|
||||
}
|
||||
continue
|
||||
}
|
||||
|
||||
match = headerRegex.FindStringSubmatch(line)
|
||||
if match != nil {
|
||||
if !hasAggStatus {
|
||||
if i != 0 {
|
||||
tableOutput.Frames = append(tableOutput.Frames, currentFrame)
|
||||
}
|
||||
currentFrame = TableOutputFrame{}
|
||||
}
|
||||
continue
|
||||
}
|
||||
|
||||
match = resourceRegex.FindStringSubmatch(line)
|
||||
if match != nil {
|
||||
var identifier ResourceIdentifier
|
||||
resourceType := match[1]
|
||||
parts := strings.Split(resourceType, "/")
|
||||
if len(parts) == 2 {
|
||||
identifier.apiVersion = parts[0]
|
||||
identifier.kind = parts[1]
|
||||
} else {
|
||||
identifier.apiVersion = strings.Join(parts[:2], "/")
|
||||
identifier.kind = parts[2]
|
||||
}
|
||||
identifier.namespace = match[2]
|
||||
identifier.name = match[3]
|
||||
|
||||
res := ResourceOutput{
|
||||
identifier: identifier,
|
||||
}
|
||||
res.status = status.FromStringOrDie(match[4])
|
||||
res.message = match[5]
|
||||
currentFrame.Resources = append(currentFrame.Resources, res)
|
||||
continue
|
||||
}
|
||||
tableOutput.UnknownRows = append(tableOutput.UnknownRows, line)
|
||||
}
|
||||
tableOutput.Frames = append(tableOutput.Frames, currentFrame)
|
||||
return tableOutput
|
||||
}
|
||||
|
||||
func createDeploymentStatusFunc() func(*unstructured.Unstructured) error {
|
||||
metadataMap := map[string]interface{}{
|
||||
"generation": int64(2),
|
||||
}
|
||||
specMap := map[string]interface{}{
|
||||
"replicas": int64(2),
|
||||
}
|
||||
statusMap := map[string]interface{}{
|
||||
"observedGeneration": int64(2),
|
||||
"replicas": int64(4),
|
||||
"updatedReplicas": int64(4),
|
||||
"readyReplicas": int64(4),
|
||||
}
|
||||
var conditions = make([]interface{}, 0)
|
||||
conditions = append(conditions, map[string]interface{}{
|
||||
"type": "Available",
|
||||
"status": "True",
|
||||
})
|
||||
callbackCount := int64(0)
|
||||
return func(deployment *unstructured.Unstructured) error {
|
||||
_ = unstructured.SetNestedMap(deployment.Object, metadataMap, "metadata")
|
||||
_ = unstructured.SetNestedMap(deployment.Object, specMap, "spec")
|
||||
statusMap["availableReplicas"] = callbackCount
|
||||
_ = unstructured.SetNestedMap(deployment.Object, statusMap, "status")
|
||||
_ = unstructured.SetNestedSlice(deployment.Object, conditions, "status", "conditions")
|
||||
callbackCount++
|
||||
return nil
|
||||
}
|
||||
}
|
||||
|
||||
func createServiceStatusFunc() func(*unstructured.Unstructured) error {
|
||||
return func(*unstructured.Unstructured) error {
|
||||
return nil
|
||||
}
|
||||
}
|
||||
|
||||
func createPodStatusFunc() func(*unstructured.Unstructured) error {
|
||||
statusMap := map[string]interface{}{
|
||||
"phase": "Succeeded",
|
||||
}
|
||||
return func(pod *unstructured.Unstructured) error {
|
||||
_ = unstructured.SetNestedMap(pod.Object, statusMap, "status")
|
||||
return nil
|
||||
}
|
||||
}
|
||||
|
||||
type ResourceGetCallback func(resource *unstructured.Unstructured) error
|
||||
|
||||
type FakeClient struct {
|
||||
resourceCallbackMap map[string]ResourceGetCallback
|
||||
}
|
||||
|
||||
func (f *FakeClient) Get(_ context.Context, _ client.ObjectKey, obj runtime.Object) error {
|
||||
kind := obj.GetObjectKind().GroupVersionKind().Kind
|
||||
callbackFunc, found := f.resourceCallbackMap[kind]
|
||||
if !found {
|
||||
return fmt.Errorf("no callback func found for kind %s", kind)
|
||||
}
|
||||
u := obj.(*unstructured.Unstructured)
|
||||
return callbackFunc(u)
|
||||
}
|
||||
|
||||
func (f *FakeClient) List(ctx context.Context, list runtime.Object, opts ...client.ListOption) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func joinStatuses(statuses []status.Status) string {
|
||||
var stringStatuses []string
|
||||
for _, s := range statuses {
|
||||
stringStatuses = append(stringStatuses, s.String())
|
||||
}
|
||||
return strings.Join(stringStatuses, ",")
|
||||
}
|
||||
@@ -179,10 +179,13 @@ func (s *TablePrinter) printTable(data StatusData, deleteUp bool) {
|
||||
}
|
||||
|
||||
func (s *TablePrinter) printTableRow(rowData []RowData) {
|
||||
for _, row := range rowData {
|
||||
for i, row := range rowData {
|
||||
setColor(s.out, row.color)
|
||||
format := fmt.Sprintf("%%-%ds ", row.width)
|
||||
format := fmt.Sprintf("%%-%ds", row.width)
|
||||
printOrDie(s.out, format, trimString(row.content, row.width))
|
||||
if i != len(rowData)-1 {
|
||||
printOrDie(s.out, " ")
|
||||
}
|
||||
setColor(s.out, WHITE)
|
||||
}
|
||||
printOrDie(s.out, "\n")
|
||||
|
||||
@@ -22,9 +22,11 @@ func init() {
|
||||
_ = clientgoscheme.AddToScheme(scheme)
|
||||
}
|
||||
|
||||
// getClient returns a client for talking to a Kubernetes cluster. The client
|
||||
type createClientFunc func() (client.Reader, error)
|
||||
|
||||
// createClient returns a client for talking to a Kubernetes cluster. The client
|
||||
// is from controller-runtime.
|
||||
func getClient() (client.Client, error) {
|
||||
func createClient() (client.Reader, error) {
|
||||
config := ctrl.GetConfigOrDie()
|
||||
mapper, err := apiutil.NewDiscoveryRESTMapper(config)
|
||||
if err != nil {
|
||||
@@ -33,6 +35,12 @@ func getClient() (client.Client, error) {
|
||||
return client.New(config, client.Options{Scheme: scheme, Mapper: mapper})
|
||||
}
|
||||
|
||||
func newClientFunc(c client.Reader) func() (client.Reader, error) {
|
||||
return func() (client.Reader, error) {
|
||||
return c, nil
|
||||
}
|
||||
}
|
||||
|
||||
// CaptureIdentifiersFilter implements the Filter interface in the kio package. It
|
||||
// captures the identifiers for all resources passed through the pipeline.
|
||||
type CaptureIdentifiersFilter struct {
|
||||
|
||||
@@ -18,7 +18,9 @@ import (
|
||||
|
||||
// GetWaitRunner return a command WaitRunner.
|
||||
func GetWaitRunner() *WaitRunner {
|
||||
r := &WaitRunner{}
|
||||
r := &WaitRunner{
|
||||
createClientFunc: createClient,
|
||||
}
|
||||
c := &cobra.Command{
|
||||
Use: "wait DIR...",
|
||||
Short: commands.WaitShort,
|
||||
@@ -48,6 +50,8 @@ type WaitRunner struct {
|
||||
Interval time.Duration
|
||||
Timeout time.Duration
|
||||
Command *cobra.Command
|
||||
|
||||
createClientFunc createClientFunc
|
||||
}
|
||||
|
||||
// runE implements the logic of the command and will call the Wait command in the wait
|
||||
@@ -55,7 +59,7 @@ type WaitRunner struct {
|
||||
// TablePrinter to display the information.
|
||||
func (r *WaitRunner) runE(c *cobra.Command, args []string) error {
|
||||
ctx := context.Background()
|
||||
client, err := getClient()
|
||||
client, err := r.createClientFunc()
|
||||
if err != nil {
|
||||
return errors.Wrap(err, "error creating client")
|
||||
}
|
||||
|
||||
176
cmd/resource/status/cmd/wait_test.go
Normal file
176
cmd/resource/status/cmd/wait_test.go
Normal file
@@ -0,0 +1,176 @@
|
||||
package cmd
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"fmt"
|
||||
"reflect"
|
||||
"testing"
|
||||
|
||||
"github.com/acarl005/stripansi"
|
||||
"github.com/stretchr/testify/assert"
|
||||
"sigs.k8s.io/kustomize/kstatus/status"
|
||||
)
|
||||
|
||||
func TestWaitNoResources(t *testing.T) {
|
||||
inBuffer := &bytes.Buffer{}
|
||||
outBuffer := &bytes.Buffer{}
|
||||
|
||||
fakeClient := &FakeClient{}
|
||||
|
||||
r := GetWaitRunner()
|
||||
r.createClientFunc = newClientFunc(fakeClient)
|
||||
r.Command.SetArgs([]string{})
|
||||
r.Command.SetIn(inBuffer)
|
||||
r.Command.SetOut(outBuffer)
|
||||
|
||||
err := r.Command.Execute()
|
||||
if !assert.NoError(t, err) {
|
||||
return
|
||||
}
|
||||
cleanOutput := stripansi.Strip(outBuffer.String())
|
||||
tableOutput := parseTableOutput(t, cleanOutput)
|
||||
|
||||
if want, got := 2, len(tableOutput.Frames); want != got {
|
||||
t.Errorf("expected %d frames, but found %d", want, got)
|
||||
}
|
||||
|
||||
aggStatuses := tableOutput.allAggStatuses()
|
||||
expectedAggStatuses := []status.Status{
|
||||
status.CurrentStatus,
|
||||
status.CurrentStatus,
|
||||
}
|
||||
if !reflect.DeepEqual(aggStatuses, expectedAggStatuses) {
|
||||
t.Errorf("expected agg statuses to be %s, but got %s", joinStatuses(expectedAggStatuses),
|
||||
joinStatuses(aggStatuses))
|
||||
}
|
||||
|
||||
resources := tableOutput.resources()
|
||||
if want, got := 0, len(resources); want != got {
|
||||
t.Errorf("expected %d resources, but found %d", want, got)
|
||||
}
|
||||
}
|
||||
|
||||
func TestWaitMultipleUpdates(t *testing.T) {
|
||||
inBuffer := &bytes.Buffer{}
|
||||
outBuffer := &bytes.Buffer{}
|
||||
|
||||
_, err := fmt.Fprint(inBuffer, `
|
||||
apiVersion: apps/v1
|
||||
kind: Deployment
|
||||
metadata:
|
||||
name: bar
|
||||
namespace: default
|
||||
`)
|
||||
if !assert.NoError(t, err) {
|
||||
return
|
||||
}
|
||||
|
||||
fakeClient := &FakeClient{
|
||||
resourceCallbackMap: map[string]ResourceGetCallback{
|
||||
"Deployment": createDeploymentStatusFunc(),
|
||||
},
|
||||
}
|
||||
|
||||
r := GetWaitRunner()
|
||||
r.createClientFunc = newClientFunc(fakeClient)
|
||||
r.Command.SetArgs([]string{})
|
||||
r.Command.SetIn(inBuffer)
|
||||
r.Command.SetOut(outBuffer)
|
||||
|
||||
err = r.Command.Execute()
|
||||
if !assert.NoError(t, err) {
|
||||
return
|
||||
}
|
||||
|
||||
cleanOutput := stripansi.Strip(outBuffer.String())
|
||||
tableOutput := parseTableOutput(t, cleanOutput)
|
||||
|
||||
aggStatuses := tableOutput.dedupedAggStatuses()
|
||||
expectedStatuses := []status.Status{
|
||||
status.UnknownStatus,
|
||||
status.InProgressStatus,
|
||||
status.CurrentStatus,
|
||||
}
|
||||
if !reflect.DeepEqual(aggStatuses, expectedStatuses) {
|
||||
t.Errorf("expected deduped agg statuses to be %s, but got %s", joinStatuses(expectedStatuses),
|
||||
joinStatuses(aggStatuses))
|
||||
}
|
||||
|
||||
resources := tableOutput.resources()
|
||||
if want, got := 1, len(resources); got != want {
|
||||
t.Errorf("expected %d resource, but got %d", want, got)
|
||||
}
|
||||
|
||||
resource := resources[0]
|
||||
resourceStatuses := tableOutput.dedupedStatusesForResource(resource)
|
||||
expectedResourceStatuses := []status.Status{
|
||||
status.InProgressStatus,
|
||||
status.CurrentStatus,
|
||||
}
|
||||
if !reflect.DeepEqual(expectedResourceStatuses, resourceStatuses) {
|
||||
t.Errorf("expected resource %q to have statuses %s, but got %s", resource.name,
|
||||
joinStatuses(expectedResourceStatuses), joinStatuses(resourceStatuses))
|
||||
}
|
||||
}
|
||||
|
||||
func TestWaitMultipleResources(t *testing.T) {
|
||||
inBuffer := &bytes.Buffer{}
|
||||
outBuffer := &bytes.Buffer{}
|
||||
|
||||
_, err := fmt.Fprint(inBuffer, `
|
||||
apiVersion: v1
|
||||
kind: List
|
||||
items:
|
||||
- apiVersion: v1
|
||||
kind: Pod
|
||||
metadata:
|
||||
name: foo
|
||||
namespace: default
|
||||
- apiVersion: v1
|
||||
kind: Service
|
||||
metadata:
|
||||
name: bar
|
||||
namespace: default
|
||||
`)
|
||||
if !assert.NoError(t, err) {
|
||||
return
|
||||
}
|
||||
|
||||
fakeClient := &FakeClient{
|
||||
resourceCallbackMap: map[string]ResourceGetCallback{
|
||||
"Pod": createPodStatusFunc(),
|
||||
"Service": createServiceStatusFunc(),
|
||||
},
|
||||
}
|
||||
|
||||
r := GetWaitRunner()
|
||||
r.createClientFunc = newClientFunc(fakeClient)
|
||||
r.Command.SetArgs([]string{})
|
||||
r.Command.SetIn(inBuffer)
|
||||
r.Command.SetOut(outBuffer)
|
||||
|
||||
err = r.Command.Execute()
|
||||
if !assert.NoError(t, err) {
|
||||
return
|
||||
}
|
||||
|
||||
cleanOutput := stripansi.Strip(outBuffer.String())
|
||||
tableOutput := parseTableOutput(t, cleanOutput)
|
||||
|
||||
aggStatuses := tableOutput.dedupedAggStatuses()
|
||||
if want, got := status.CurrentStatus, aggStatuses[len(aggStatuses)-1]; want != got {
|
||||
t.Errorf("expected final agg statuses to be %s, but got %s", want, got)
|
||||
}
|
||||
|
||||
resources := tableOutput.resources()
|
||||
if want, got := 2, len(resources); got != want {
|
||||
t.Errorf("expected %d resource, but got %d", want, got)
|
||||
}
|
||||
|
||||
for _, resource := range resources {
|
||||
resourceStatuses := tableOutput.dedupedStatusesForResource(resource)
|
||||
if want, got := status.CurrentStatus, resourceStatuses[len(resourceStatuses)-1]; want != got {
|
||||
t.Errorf("expected resource %q to have final status %s, but got %s", resource.name, want, got)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,10 +1,8 @@
|
||||
|
||||
|
||||
// Code generated by "mdtogo"; DO NOT EDIT.
|
||||
package commands
|
||||
|
||||
var EventsShort=`[Alpha] Poll the cluster until all provided resources have become Current and list the status change events.`
|
||||
var EventsLong=`
|
||||
var EventsShort = `[Alpha] Poll the cluster until all provided resources have become Current and list the status change events.`
|
||||
var EventsLong = `
|
||||
[Alpha] Poll the cluster for the state of all the provided resources until either they have all become
|
||||
Current or the timeout is reached. The output will be status change events.
|
||||
|
||||
@@ -14,15 +12,15 @@ on StdIn.
|
||||
DIR:
|
||||
Path to local directory. If not provided, input is expected on StdIn.
|
||||
`
|
||||
var EventsExamples=`
|
||||
var EventsExamples = `
|
||||
# Read resources from the filesystem and wait up to 1 minute for all of them to become Current
|
||||
resource status events my-dir/
|
||||
|
||||
# Fetch all resources in the cluster and wait up to 5 minutes for all of them to become Current
|
||||
kubectl get all --all-namespaces -oyaml | resource status events --timeout=5m`
|
||||
|
||||
var FetchShort=`[Alpha] Fetch the state of the provided resources from the cluster and display status in a table.`
|
||||
var FetchLong=`
|
||||
var FetchShort = `[Alpha] Fetch the state of the provided resources from the cluster and display status in a table.`
|
||||
var FetchLong = `
|
||||
[Alpha] Fetches the state of all provided resources from the cluster and displays the status in
|
||||
a table.
|
||||
|
||||
@@ -31,15 +29,15 @@ The list of resources are provided as manifests either on the filesystem or on S
|
||||
DIR:
|
||||
Path to local directory.
|
||||
`
|
||||
var FetchExamples=`
|
||||
var FetchExamples = `
|
||||
# Read resources from the filesystem and wait up to 1 minute for all of them to become Current
|
||||
resource status fetch my-dir/
|
||||
|
||||
# Fetch all resources in the cluster and wait up to 5 minutes for all of them to become Current
|
||||
kubectl get all --all-namespaces -oyaml | resource status fetch`
|
||||
|
||||
var WaitShort=`[Alpha] Poll the cluster until all provided resources have become Current and display progress in a table. `
|
||||
var WaitLong=`
|
||||
var WaitShort = `[Alpha] Poll the cluster until all provided resources have become Current and display progress in a table. `
|
||||
var WaitLong = `
|
||||
[Alpha] Poll the cluster for the state of all the provided resources until either they have all become
|
||||
Current or the timeout is reached. The output will be presented as a table.
|
||||
|
||||
@@ -49,7 +47,7 @@ on StdIn.
|
||||
DIR:
|
||||
Path to local directory. If not provided, input is expected on StdIn.
|
||||
`
|
||||
var WaitExamples=`
|
||||
var WaitExamples = `
|
||||
# Read resources from the filesystem and wait up to 1 minute for all of them to become Current
|
||||
resource status wait my-dir/
|
||||
|
||||
|
||||
Reference in New Issue
Block a user