Skip to content

Commit

Permalink
Merge pull request #28 from garethr/schema-location-support
Browse files Browse the repository at this point in the history
Schema location support
  • Loading branch information
garethr authored Aug 19, 2017
2 parents a5543ed + 44d82dc commit 6494648
Show file tree
Hide file tree
Showing 8 changed files with 80 additions and 10 deletions.
20 changes: 20 additions & 0 deletions Dockerfile.offline
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
FROM golang:1.8-alpine as builder
RUN apk --no-cache add make git
RUN mkdir -p /go/src/github.com/garethr/kubeval/
COPY . /go/src/github.com/garethr/kubeval/
WORKDIR /go/src/github.com/garethr/kubeval/
RUN make linux

FROM alpine:latest as schemas
RUN apk --no-cache add git
RUN git clone https://github.com/garethr/kubernetes-json-schema.git
RUN git clone https://github.com/garethr/openshift-json-schema.git

FROM alpine:latest
RUN apk --no-cache add ca-certificates
COPY --from=builder /go/src/github.com/garethr/kubeval/bin/linux/amd64/kubeval .
COPY --from=schemas /kubernetes-json-schema /schemas/kubernetes-json-schema/master
COPY --from=schemas /openshift-json-schema /schemas/openshift-json-schema/master
ENV KUBEVAL_SCHEMA_LOCATION=file:///schemas
ENTRYPOINT ["/kubeval"]
CMD ["--help"]
8 changes: 7 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -74,9 +74,15 @@ docker:
docker build -t garethr/kubeval:$(TAG) .
docker tag garethr/kubeval:$(TAG) garethr/kubeval:latest

publish: docker
docker-offline:
docker build -f Dockerfile.offline -t garethr/kubeval:$(TAG)-offline .
docker tag garethr/kubeval:$(TAG)-offline garethr/kubeval:offline

publish: docker docker-offline
docker push garethr/kubeval:$(TAG)
docker push garethr/kubeval:latest
docker push garethr/kubeval:$(TAG)-offline
docker push garethr/kubeval:offline

vet:
go vet `glide novendor`
Expand Down
10 changes: 7 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -118,9 +118,13 @@ Validate a Kubernetes YAML file against the relevant schema
Usage:
kubeval <file> [file...] [flags]
Flags:
-v, --kubernetes-version string Version of Kubernetes to validate against (default "master")
--openshift Use OpenShift schemas instead of upstream Kubernetes
Flags:
-h, --help help for kubeval
-v, --kubernetes-version string Version of Kubernetes to validate against (default "master")
--openshift Use OpenShift schemas instead of upstream Kubernetes
--schema-location string Base URL used to download schemas. Can also be specified with the environment variable KUBEVAL_SCHEMA_LOCATION (default "https://raw.githubusercontent.com/garethr")
--version Display the kubeval version information and exit
```

The command has three important features:
Expand Down
5 changes: 5 additions & 0 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"runtime"

"github.com/spf13/cobra"
"github.com/spf13/viper"

"github.com/garethr/kubeval/kubeval"
"github.com/garethr/kubeval/log"
Expand Down Expand Up @@ -102,7 +103,11 @@ func Execute() {
}

func init() {
viper.SetEnvPrefix("KUBEVAL")
viper.AutomaticEnv()
RootCmd.Flags().StringVarP(&kubeval.Version, "kubernetes-version", "v", "master", "Version of Kubernetes to validate against")
RootCmd.Flags().StringVarP(&kubeval.SchemaLocation, "schema-location", "", kubeval.DefaultSchemaLocation, "Base URL used to download schemas. Can also be specified with the environment variable KUBEVAL_SCHEMA_LOCATION")
RootCmd.Flags().BoolVarP(&kubeval.OpenShift, "openshift", "", false, "Use OpenShift schemas instead of upstream Kubernetes")
RootCmd.Flags().BoolVarP(&Version, "version", "", false, "Display the kubeval version information and exit")
viper.BindPFlag("schema_location", RootCmd.Flags().Lookup("schema-location"))
}
6 changes: 4 additions & 2 deletions glide.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions glide.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,5 @@ import:
- package: github.com/fatih/color
version: ^1.5.0
- package: github.com/hashicorp/go-multierror
- package: github.com/spf13/viper
version: ^1.0.0
30 changes: 26 additions & 4 deletions kubeval/kubeval.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,10 @@ import (
"bytes"
"errors"
"fmt"
"strings"
"runtime"
"strings"

"github.com/spf13/viper"
"github.com/hashicorp/go-multierror"
"github.com/xeipuuv/gojsonschema"
"gopkg.in/yaml.v2"
Expand All @@ -16,6 +17,14 @@ import (
// for which we should load the schema
var Version string

// SchemaLocation represents what is the schema location,
/// where default value is maintener github project, but can be overriden
/// to either different github repo, or a local file
var SchemaLocation string

// DefaultSchemaLocation is the default value for
var DefaultSchemaLocation = "https://raw.githubusercontent.com/garethr"

// OpenShift represents whether to test against
// upstream Kubernetes of the OpenShift schemas
var OpenShift bool
Expand Down Expand Up @@ -69,7 +78,20 @@ func determineSchema(kind string) string {
normalisedVersion = "v" + normalisedVersion
}

return fmt.Sprintf("https://raw.githubusercontent.com/garethr/%s-json-schema/master/%s-standalone/%s.json", schemaType, normalisedVersion, strings.ToLower(kind))
// Check Viper for environment variable support first.
// Then check for an override in SchemaLocation
// Finally settle on the default value
baseURLFromEnv := viper.GetString("schema_location")
var baseURL string
if baseURLFromEnv != "" {
baseURL = baseURLFromEnv
} else if SchemaLocation == "" {
baseURL = DefaultSchemaLocation
} else {
baseURL = SchemaLocation
}

return fmt.Sprintf("%s/%s-json-schema/master/%s-standalone/%s.json", baseURL, schemaType, normalisedVersion, strings.ToLower(kind))
}

func determineKind(body interface{}) (string, error) {
Expand Down Expand Up @@ -113,7 +135,7 @@ func validateResource(data []byte, fileName string) (ValidationResult, error) {

results, err := gojsonschema.Validate(schemaLoader, documentLoader)
if err != nil {
return result, errors.New("Problem loading schema from the network")
return result, fmt.Errorf("Problem loading schema from the network at %s: %s", schema, err)
}

if results.Valid() {
Expand All @@ -132,7 +154,7 @@ func Validate(config []byte, fileName string) ([]ValidationResult, error) {
return nil, errors.New("The document " + fileName + " appears to be empty")
}

bits := bytes.Split(config, []byte("---" + detectLineBreak(config)))
bits := bytes.Split(config, []byte("---"+detectLineBreak(config)))

results := make([]ValidationResult, 0)
var errors *multierror.Error
Expand Down
9 changes: 9 additions & 0 deletions kubeval/kubeval_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,15 @@ func TestDetermineSchemaForVersions(t *testing.T) {
}
}

func TestDetermineSchemaForSchemaLocation(t *testing.T) {
SchemaLocation = "file:///home/me"
schema := determineSchema("sample")
expectedSchema := "file:///home/me/openshift-json-schema/master/v1.0-standalone/sample.json"
if schema != expectedSchema {
t.Errorf("Should be able to specify a schema location, expected %s, got %s instead ", expectedSchema, schema)
}
}

func TestDetermineKind(t *testing.T) {
_, err := determineKind("sample")
if err == nil {
Expand Down

0 comments on commit 6494648

Please sign in to comment.