add validation transformer

This commit is contained in:
Jingfang Liu
2019-05-29 13:49:31 -07:00
committed by jingfangliu
parent dd75392d98
commit b4dbac1b84
5 changed files with 135 additions and 5 deletions

View File

@@ -0,0 +1,35 @@
#!/bin/bash
# Validator transformer plugin by kubeval
# https://github.com/instrumenta/kubeval
#
# Assumes that kubeval is available from $PATH
#
# Reads Kubernetes resources from stdin
# Validates them and output in the stdout
#
# Example execution:
# cat <raw K8s yaml> | ./plugin/someteam.example.com/v1/Validator
if ! [ -x "$(command -v kubeval)" ]; then
echo "Error: kubeval is not installed."
exit 1
fi
temp_file=$(mktemp)
output_file=$(mktemp)
cat - > $temp_file
kubeval $temp_file > $output_file
if [ $? -eq 0 ]; then
cat $temp_file
rm $temp_file $output_file
exit 0
fi
cat $output_file
rm $temp_file $output_file
exit 1

View File

@@ -0,0 +1,79 @@
// +build notravis
// Copyright 2019 The Kubernetes Authors.
// SPDX-License-Identifier: Apache-2.0
package main_test
import (
"strings"
"testing"
"sigs.k8s.io/kustomize/pkg/kusttest"
"sigs.k8s.io/kustomize/plugin"
)
func TestValidatorHappy(t *testing.T) {
tc := plugin.NewEnvForTest(t).Set()
defer tc.Reset()
tc.BuildExecPlugin("someteam.example.com", "v1", "Validator")
th := kusttest_test.NewKustTestPluginHarness(t, "/app")
rm := th.LoadAndRunTransformer(`
apiVersion: someteam.example.com/v1
kind: Validator
metadata:
name: notImportantHere
`,
`apiVersion: v1
kind: ConfigMap
metadata:
annotations:
foo: bar
name: some-cm
data:
foo: bar
`)
th.AssertActualEqualsExpected(rm, `
apiVersion: v1
data:
foo: bar
kind: ConfigMap
metadata:
annotations:
foo: bar
name: some-cm
`)
}
func TestValidatorUnHappy(t *testing.T) {
tc := plugin.NewEnvForTest(t).Set()
defer tc.Reset()
tc.BuildExecPlugin("someteam.example.com", "v1", "Validator")
th := kusttest_test.NewKustTestPluginHarness(t, "/app")
err := th.ErrorFromLoadAndRunTransformer(`
apiVersion: someteam.example.com/v1
kind: Validator
metadata:
name: notImportantHere
`,
`apiVersion: v1
kind: ConfigMap
metadata:
annotations: {}
name: some-cm
data:
- foo: bar
`)
if err == nil {
t.Fatalf("expected an error")
}
if !strings.Contains(err.Error(),
"data: Invalid type. Expected: object, given: array") {
t.Fatalf("incorrect error %v", err)
}
}