Skip to content

Commit

Permalink
Add e2e test for kustomize overlays pruning
Browse files Browse the repository at this point in the history
Signed-off-by: Stefan Prodan <stefan.prodan@gmail.com>
  • Loading branch information
stefanprodan committed Aug 31, 2021
1 parent cda56e7 commit 1e970e6
Show file tree
Hide file tree
Showing 14 changed files with 80 additions and 72 deletions.
14 changes: 10 additions & 4 deletions .github/workflows/e2e.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -38,19 +38,25 @@ jobs:
fi
- name: Build
run: make build
- name: Apply test
- name: Test apply --kustomize
run: |
./bin/kustomizer apply -i kustomizer-test -k testdata/kustomize/ --wait --prune
kubectl -n kustomizer-test get tests 2>&1 | grep custom-resource-test1
- name: Test apply --filename
run: |
./bin/kustomizer apply -i kustomizer-demo -f testdata/plain/ --wait --prune
kubectl -n kustomizer-demo get svc frontend 2>&1 | grep frontend
- name: Prune test
kubectl -n kustomizer-test get svc frontend 2>&1 | grep frontend
- name: Test apply --prune
run: |
rm -rf testdata/plain/frontend
./bin/kustomizer apply -i kustomizer-demo -f testdata/plain/ --wait --prune
kubectl -n kustomizer-demo get svc frontend 2>&1 | grep NotFound
- name: Delete test
- name: Test delete --wait
run: |
./bin/kustomizer delete -i kustomizer-demo --wait
kubectl get ns kustomizer-demo 2>&1 | grep NotFound
./bin/kustomizer delete -i kustomizer-test --wait
kubectl get crd tests.testing.kustomizer.dev 2>&1 | grep NotFound
- name: Debug failure
if: failure()
run: |
Expand Down
17 changes: 10 additions & 7 deletions cmd/kustomizer/apply.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,13 +44,16 @@ type applyFlags struct {
var applyArgs applyFlags

func init() {
applyCmd.Flags().StringSliceVarP(&applyArgs.filename, "filename", "f", nil, "path to Kubernetes manifest(s)")
applyCmd.Flags().StringVarP(&applyArgs.kustomize, "kustomize", "k", "", "process a kustomization directory (can't be used together with -f)")
applyCmd.Flags().BoolVar(&applyArgs.wait, "wait", false, "wait for the applied Kubernetes objects to become ready")
applyCmd.Flags().BoolVar(&applyArgs.force, "force", false, "recreate objects that contain immutable fields changes")
applyCmd.Flags().BoolVar(&applyArgs.prune, "prune", false, "delete stale objects")
applyCmd.Flags().StringVarP(&applyArgs.inventoryName, "inventory-name", "i", "", "inventory configmap name")
applyCmd.Flags().StringVar(&applyArgs.inventoryNamespace, "inventory-namespace", "default", "inventory configmap namespace")
applyCmd.Flags().StringSliceVarP(&applyArgs.filename, "filename", "f", nil,
"Path to Kubernetes manifest(s). If a directory is specified, then all manifests in the directory tree will be processed recursively.")
applyCmd.Flags().StringVarP(&applyArgs.kustomize, "kustomize", "k", "",
"Path to a directory that contains a kustomization.yaml.")
applyCmd.Flags().BoolVar(&applyArgs.wait, "wait", false, "Wait for the applied Kubernetes objects to become ready.")
applyCmd.Flags().BoolVar(&applyArgs.force, "force", false, "Recreate objects that contain immutable fields changes.")
applyCmd.Flags().BoolVar(&applyArgs.prune, "prune", false, "Delete stale objects from the cluster.")
applyCmd.Flags().StringVarP(&applyArgs.inventoryName, "inventory-name", "i", "", "The name of the inventory configmap.")
applyCmd.Flags().StringVar(&applyArgs.inventoryNamespace, "inventory-namespace", "default",
"The namespace of the inventory configmap. The namespace must exist on the target cluster.")

rootCmd.AddCommand(applyCmd)
}
Expand Down
13 changes: 8 additions & 5 deletions cmd/kustomizer/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,9 +50,12 @@ type buildFlags struct {
var buildArgs buildFlags

func init() {
buildCmd.Flags().StringSliceVarP(&buildArgs.filename, "filename", "f", nil, "path to Kubernetes manifest(s)")
buildCmd.Flags().StringVarP(&buildArgs.kustomize, "kustomize", "k", "", "process a kustomization directory (can't be used together with -f)")
buildCmd.Flags().StringVarP(&buildArgs.output, "output", "o", "yaml", "output can be yaml or json")
buildCmd.Flags().StringSliceVarP(&buildArgs.filename, "filename", "f", nil,
"Path to Kubernetes manifest(s). If a directory is specified, then all manifests in the directory tree will be processed recursively.")
buildCmd.Flags().StringVarP(&buildArgs.kustomize, "kustomize", "k", "",
"Path to a directory that contains a kustomization.yaml.")
buildCmd.Flags().StringVarP(&buildArgs.output, "output", "o", "yaml",
"Write manifests to stdout in YAML or JSON format.")

rootCmd.AddCommand(buildCmd)
}
Expand Down Expand Up @@ -103,7 +106,7 @@ func buildManifests(kustomizePath string, filePaths []string) ([]*unstructured.U
}

if len(filePaths) > 0 {
manifests, err := scan(filePaths)
manifests, err := scanForManifests(filePaths)
if err != nil {
return nil, err
}
Expand All @@ -126,7 +129,7 @@ func buildManifests(kustomizePath string, filePaths []string) ([]*unstructured.U
return objects, nil
}

func scan(paths []string) ([]string, error) {
func scanForManifests(paths []string) ([]string, error) {
var manifests []string

for _, in := range paths {
Expand Down
8 changes: 4 additions & 4 deletions cmd/kustomizer/delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ import (

var deleteCmd = &cobra.Command{
Use: "delete",
Short: "Delete the Kubernetes objects in the inventory",
Short: "Delete the Kubernetes objects in the inventory.",
RunE: deleteCmdRun,
}

Expand All @@ -40,9 +40,9 @@ type deleteFlags struct {
var deleteArgs deleteFlags

func init() {
deleteCmd.Flags().StringVarP(&deleteArgs.inventoryName, "inventory-name", "i", "", "inventory configmap name")
deleteCmd.Flags().StringVar(&deleteArgs.inventoryNamespace, "inventory-namespace", "default", "inventory configmap namespace")
deleteCmd.Flags().BoolVar(&deleteArgs.wait, "wait", true, "wait for the deleted Kubernetes objects to be terminated")
deleteCmd.Flags().StringVarP(&deleteArgs.inventoryName, "inventory-name", "i", "", "The name of the inventory configmap.")
deleteCmd.Flags().StringVar(&deleteArgs.inventoryNamespace, "inventory-namespace", "default", "The namespace of the inventory configmap.")
deleteCmd.Flags().BoolVar(&deleteArgs.wait, "wait", true, "Wait for the deleted Kubernetes objects to be terminated.")

rootCmd.AddCommand(deleteCmd)
}
Expand Down
8 changes: 5 additions & 3 deletions cmd/kustomizer/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,9 +52,11 @@ var (

func init() {
rootCmd.PersistentFlags().StringVarP(&rootArgs.kubeconfig, "kubeconfig", "", "",
"absolute path to the kubeconfig file")
rootCmd.PersistentFlags().StringVarP(&rootArgs.kubecontext, "context", "", "", "kubernetes context to use")
rootCmd.PersistentFlags().DurationVar(&rootArgs.timeout, "timeout", time.Minute, "timeout for this operation")
"Absolute path to the kubeconfig file.")
rootCmd.PersistentFlags().StringVarP(&rootArgs.kubecontext, "context", "", "",
"The Kubernetes context to use.")
rootCmd.PersistentFlags().DurationVar(&rootArgs.timeout, "timeout", time.Minute,
"The length of time to wait before giving up on the current operation.")

rootCmd.DisableAutoGenTag = true
}
Expand Down
7 changes: 0 additions & 7 deletions testdata/crds/resources/cr.yaml

This file was deleted.

16 changes: 16 additions & 0 deletions testdata/kustomize/cr.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
apiVersion: testing.kustomizer.dev/v1
kind: Test
metadata:
name: custom-resource-test1
spec:
type: integration
valuesFrom: test-config
---
apiVersion: testing.kustomizer.dev/v1
kind: Test
metadata:
name: custom-resource-test2
spec:
type: unit
valuesFrom: test-config
---
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
apiVersion: apiextensions.k8s.io/v1beta1
kind: CustomResourceDefinition
metadata:
name: tests.testing.k8s.io
name: tests.testing.kustomizer.dev
spec:
group: testing.k8s.io
group: testing.kustomizer.dev
version: v1
versions:
- name: v1
Expand Down Expand Up @@ -31,3 +31,6 @@ spec:
enum:
- unit
- integration
valuesFrom:
description: ConfigMap reference
type: string
14 changes: 14 additions & 0 deletions testdata/kustomize/kustomization.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
apiVersion: kustomize.config.k8s.io/v1beta1
kind: Kustomization
namespace: kustomizer-test
configurations:
- kustomizeconfig.yaml
resources:
- namespace.yaml
- crd.yaml
- cr.yaml
configMapGenerator:
- name: test-config
files:
- test.conf

6 changes: 6 additions & 0 deletions testdata/kustomize/kustomizeconfig.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
nameReference:
- kind: ConfigMap
version: v1
fieldSpecs:
- path: spec/valuesFrom
kind: Test
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
apiVersion: v1
kind: Namespace
metadata:
name: test2
name: kustomizer-test
1 change: 1 addition & 0 deletions testdata/kustomize/test.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
test=test2
6 changes: 0 additions & 6 deletions testdata/sops/kustomization.yaml

This file was deleted.

33 changes: 0 additions & 33 deletions testdata/sops/secret.yaml

This file was deleted.

0 comments on commit 1e970e6

Please sign in to comment.