Skip to content

Commit

Permalink
Quick and Dirty checked in
Browse files Browse the repository at this point in the history
  • Loading branch information
jbrette committed Jul 11, 2019
0 parents commit 29582b7
Show file tree
Hide file tree
Showing 29 changed files with 9,485 additions and 0 deletions.
38 changes: 38 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@

# Image URL to use all building/pushing image targets
COMPONENT ?= armada-operator
VERSION_V2 ?= 2.14.1
VERSION_V3 ?= 3.0.0
DHUBREPO ?= keleustes/${COMPONENT}-dev
DOCKER_NAMESPACE ?= keleustes
IMG_V2 ?= ${DHUBREPO}:v${VERSION_V2}
IMG_V3 ?= ${DHUBREPO}:v${VERSION_V3}

all: docker-build

setup:
GO111MODULE=on go mod vendor

clean:
rm -fr vendor
rm -fr cover.out
rm -fr build/_output
rm -fr config/crds

# Generate code
crd-yaml: setup
# go get sigs.k8s.io/controller-tools
GO111MODULE=on controller-gen crd paths=./pkg/apis/armada/... crd:trivialVersions=true output:crd:dir=./kubectl output:none

openapi-gen: setup
# go get k8s.io/kube-openapi
GO111MODULE=on go run vendor/k8s.io/kube-openapi/cmd/openapi-gen/openapi-gen.go -i "k8s.io/apimachinery/pkg/apis/meta/v1,github.com/keleustes/armada-operator/pkg/apis/armada/v1alpha1" -o pkg -p generated -O openapi_generated -r ./testdata/golden.report

swagger-gen: setup
GO111MODULE=on go run cmd/builder/main.go kubeval/swagger.json

kubeval-json: setup
REPO="keleustes/armada-operator"
schema=swagger/golden.json
prefix=https://raw.githubusercontent.com/${REPO}/master/${version}/
openapi2jsonschema -o "${version}" --prefix "${prefix}" "${schema}"
129 changes: 129 additions & 0 deletions cmd/builder/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,129 @@
/*
Copyright 2018 The Kubernetes Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package main

import (
"encoding/json"
"fmt"
"io/ioutil"
"log"
"os"
"strings"

"github.com/emicklei/go-restful"
"github.com/go-openapi/spec"
"k8s.io/kube-openapi/pkg/builder"
"k8s.io/kube-openapi/pkg/common"
"k8s.io/kube-openapi/pkg/util"
"github.com/keleustes/armada-crd/pkg/generated"
)

// TODO: Change this to output the generated swagger to stdout.
const defaultSwaggerFile = "generated.json"

func main() {
// Get the name of the generated swagger file from the args
// if it exists; otherwise use the default file name.
swaggerFilename := defaultSwaggerFile
if len(os.Args) > 1 {
swaggerFilename = os.Args[1]
}

// Generate the definition names from the map keys returned
// from GetOpenAPIDefinitions. Anonymous function returning empty
// Ref is not used.
var defNames []string
for name, _ := range generated.GetOpenAPIDefinitions(func(name string) spec.Ref {
return spec.Ref{}
}) {
defNames = append(defNames, name)
}

// Create a minimal builder config, then call the builder with the definition names.
config := createOpenAPIBuilderConfig()
config.GetDefinitions = generated.GetOpenAPIDefinitions
// Build the Paths using a simple WebService for the final spec
swagger, serr := builder.BuildOpenAPISpec(createWebServices(), config)
if serr != nil {
log.Fatalf("ERROR: %s", serr.Error())
}

// Marshal the swagger spec into JSON, then write it out.
specBytes, err := json.MarshalIndent(swagger, " ", " ")
if err != nil {
log.Fatalf("json marshal error: %s", err.Error())
}
err = ioutil.WriteFile(swaggerFilename, specBytes, 0644)
if err != nil {
log.Fatalf("stdout write error: %s", err.Error())
}
}

// CreateOpenAPIBuilderConfig hard-codes some values in the API builder
// config for testing.
func createOpenAPIBuilderConfig() *common.Config {
return &common.Config{
ProtocolList: []string{"https"},
IgnorePrefixes: []string{"/swaggerapi"},
Info: &spec.Info{
InfoProps: spec.InfoProps{
Title: "Integration Test",
Version: "1.0",
},
},
ResponseDefinitions: map[string]spec.Response{
"NotFound": spec.Response{
ResponseProps: spec.ResponseProps{
Description: "Entity not found.",
},
},
},
CommonResponses: map[int]spec.Response{
404: *spec.ResponseRef("#/responses/NotFound"),
},
}
}

// createWebServices hard-codes a simple WebService which only defines a GET path
// for testing.
func createWebServices() []*restful.WebService {
w := new(restful.WebService)
w.Route(buildRouteForType(w, "v1alpha1", "ArmadaChartGroup"))
return []*restful.WebService{w}
}

// Implements OpenAPICanonicalTypeNamer
var _ = util.OpenAPICanonicalTypeNamer(&typeNamer{})

type typeNamer struct {
pkg string
name string
}

func (t *typeNamer) OpenAPICanonicalTypeName() string {
return fmt.Sprintf("github.com/keleustes/armada-operator/pkg/apis/armada/%s.%s", t.pkg, t.name)
}

func buildRouteForType(ws *restful.WebService, pkg, name string) *restful.RouteBuilder {
namer := typeNamer{
pkg: pkg,
name: name,
}
return ws.GET(fmt.Sprintf("test/%s/%s", pkg, strings.ToLower(name))).
To(func(*restful.Request, *restful.Response) {}).
Writes(&namer)
}
18 changes: 18 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
module github.com/keleustes/armada-crd

go 1.12

require (
github.com/emicklei/go-restful v2.9.6+incompatible
github.com/go-openapi/spec v0.19.2
github.com/go-openapi/strfmt v0.19.2
github.com/go-openapi/validate v0.19.2
github.com/keleustes/armada-operator v0.1.3 // indirect
github.com/spf13/cobra v0.0.5 // indirect
k8s.io/api v0.0.0-20190703205437-39734b2a72fe
k8s.io/apiextensions-apiserver v0.0.0-20190704050600-357b4270afe4
k8s.io/apimachinery v0.0.0-20190703205208-4cfb76a8bf76
k8s.io/apiserver v0.0.0-20190707085829-7f6005dcdcb2
k8s.io/kube-openapi v0.0.0-20190228160746-b3a7cee44a30
sigs.k8s.io/controller-tools v0.1.11 // indirect
)
Loading

0 comments on commit 29582b7

Please sign in to comment.