Skip to content

Commit

Permalink
Switch to controller-gen to generate crd with complete openapi valida…
Browse files Browse the repository at this point in the history
…tion spec (argoproj#90)
  • Loading branch information
jessesuen authored May 31, 2019
1 parent 6719355 commit a6aa76d
Show file tree
Hide file tree
Showing 9 changed files with 14,042 additions and 400 deletions.
9 changes: 0 additions & 9 deletions Gopkg.lock

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

13 changes: 5 additions & 8 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,11 @@ endif
.PHONY: all
all: controller image

.PHONY: clientgen
clientgen:
.PHONY: codegen
codegen:
./hack/update-codegen.sh
./hack/update-openapigen.sh
go run ./hack/gen-crd-spec/main.go

.PHONY: controller
controller: clean-debug
Expand All @@ -68,13 +70,8 @@ lint:
test:
$(TEST_CMD) -v -covermode=count -coverprofile=coverage.out `go list ./...`

.PHONY: openapi-gen
openapi-gen: clientgen
./hack/update-openapigen.sh

.PHONY: manifests
manifests: openapi-gen
go run ./hack/gen-openapi-spec/main.go
manifests:
./hack/update-manifests.sh

# Cleans VSCode debug.test files from sub-dirs to prevent them from being included in packr boxes
Expand Down
100 changes: 100 additions & 0 deletions hack/gen-crd-spec/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
package main

import (
"encoding/json"
"io/ioutil"
"os"
"os/exec"
"strings"

"github.com/ghodss/yaml"
extensionsobj "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1beta1"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
)

const (
crdPath = "manifests/crds/rollout-crd.yaml"
)

func NewRolloutCustomResourceDefinition() *extensionsobj.CustomResourceDefinition {
crdYamlBytes, err := exec.Command(
"controller-gen",
"paths=./pkg/apis/rollouts/...",
"crd:trivialVersions=true",
"output:crd:stdout",
).Output()
checkErr(err)

// clean up stuff left by controller-gen
deleteFile("config/webhook/manifests.yaml")
deleteFile("config/webhook")
deleteFile("config")

var un unstructured.Unstructured
checkErr(yaml.Unmarshal(crdYamlBytes, &un.Object))

// We need to completely remove validation of problematic fields such as creationTimestamp,
// which get marshalled to `null`, but are typed as as a `string` during Open API validation
removeValidataion(&un, "metadata.creationTimestamp")
removeValidataion(&un, "spec.template.metadata.creationTimestamp")

crd := toCRD(&un)
crd.Spec.Scope = "Namespaced"
return crd
}

func deleteFile(path string) {
if _, err := os.Stat(path); os.IsNotExist(err) {
return
}
checkErr(os.Remove(path))
}

func removeValidataion(un *unstructured.Unstructured, path string) {
schemaPath := []string{"spec", "validation", "openAPIV3Schema"}
for _, part := range strings.Split(path, ".") {
schemaPath = append(schemaPath, "properties", part)
}
unstructured.RemoveNestedField(un.Object, schemaPath...)
}

func toCRD(un *unstructured.Unstructured) *extensionsobj.CustomResourceDefinition {
unBytes, err := json.Marshal(un)
checkErr(err)

var crd extensionsobj.CustomResourceDefinition
err = json.Unmarshal(unBytes, &crd)
checkErr(err)

return &crd
}

func checkErr(err error) {
if err != nil {
panic(err)
}
}

// Generate CRD spec for Rollout Resource
func main() {
crd := NewRolloutCustomResourceDefinition()

jsonBytes, err := json.Marshal(crd)
checkErr(err)

var r unstructured.Unstructured
err = json.Unmarshal(jsonBytes, &r.Object)
checkErr(err)

// clean up crd yaml before marshalling
unstructured.RemoveNestedField(r.Object, "status")
unstructured.RemoveNestedField(r.Object, "metadata", "creationTimestamp")
jsonBytes, err = json.MarshalIndent(r.Object, "", " ")
checkErr(err)

yamlBytes, err := yaml.JSONToYAML(jsonBytes)
checkErr(err)

err = ioutil.WriteFile(crdPath, yamlBytes, 0644)
checkErr(err)
}
93 changes: 0 additions & 93 deletions hack/gen-openapi-spec/main.go

This file was deleted.

Loading

0 comments on commit a6aa76d

Please sign in to comment.