From 1d912697043ad9733318af52ac067043269d758c Mon Sep 17 00:00:00 2001 From: Broc Woodworth Date: Wed, 28 Oct 2020 17:18:23 -0700 Subject: [PATCH] Crd optional (#16) * make CRD generation optional * change example file --- example/cr/jsonschema-crd.yaml | 4 ++-- example/cr/jsonschema-pb-cr.yaml | 10 +++++----- schema_to_cr.go | 10 +++++++--- 3 files changed, 14 insertions(+), 10 deletions(-) diff --git a/example/cr/jsonschema-crd.yaml b/example/cr/jsonschema-crd.yaml index 1db7e56..3299cc0 100644 --- a/example/cr/jsonschema-crd.yaml +++ b/example/cr/jsonschema-crd.yaml @@ -1,9 +1,9 @@ apiVersion: apiextensions.k8s.io/v1 kind: CustomResourceDefinition metadata: - name: jsonschemas.tagging-v2.infoblox.com + name: jsonschemas.schemaregistry.infoblox.com spec: - group: tagging-v2.infoblox.com + group: schemaregistry.infoblox.com versions: - name: v1 served: true diff --git a/example/cr/jsonschema-pb-cr.yaml b/example/cr/jsonschema-pb-cr.yaml index 56428bc..01b3ccf 100644 --- a/example/cr/jsonschema-pb-cr.yaml +++ b/example/cr/jsonschema-pb-cr.yaml @@ -1,4 +1,4 @@ -apiVersion: "tagging-v2.infoblox.com/v1" +apiVersion: "schemaregistry.infoblox.com/v1" kind: JsonSchema metadata: name: {{ .Release.Namespace }}.pb.event @@ -7,7 +7,7 @@ spec: schema: > { "$schema": "http://json-schema.org/draft-04/schema#", "properties": { "type": { "enum": [ "ACCOUNT", 0, "SYSTEM", 1, "PRODUCT", 2, "USER", 3 ], "oneOf": [ { "type": "string" }, { "type": "integer" } ] }, "subtype": { "type": "string" }, "account_id": { "type": "string" }, "application_id": { "type": "string" }, "location": { "type": "string" }, "severity": { "type": "string" }, "TTL": { "properties": { "seconds": { "oneOf": [ { "type": "integer" }, { "type": "string" } ], "description": "Signed seconds of the span of time. Must be from -315,576,000,000\n to +315,576,000,000 inclusive. Note: these bounds are computed from:\n 60 sec/min * 60 min/hr * 24 hr/day * 365.25 days/year * 10000 years" }, "nanos": { "type": "integer", "description": "Signed fractions of a second at nanosecond resolution of the span\n of time. Durations less than one second are represented with a 0\n `seconds` field and a positive or negative `nanos` field. For durations\n of one second or more, a non-zero value for the `nanos` field must be\n of the same sign as the `seconds` field. Must be from -999,999,999\n to +999,999,999 inclusive." } }, "additionalProperties": true, "type": "object" }, "metadata": { "additionalProperties": { "type": "string" }, "type": "object" }, "occurredTime": { "type": "string", "format": "date-time" }, "generatedTime": { "type": "string", "format": "date-time" }, "id": { "type": "string" }, "status": { "enum": [ "RAISED", 0, "CLEARED", 1, "REMINDER", 2 ], "oneOf": [ { "type": "string" }, { "type": "integer" } ] }, "product_name": { "type": "string" } }, "additionalProperties": true, "type": "object"} --- -apiVersion: "tagging-v2.infoblox.com/v1" +apiVersion: "schemaregistry.infoblox.com/v1" kind: JsonSchema metadata: name: {{ .Release.Namespace }}.pb.eventsubtype @@ -16,7 +16,7 @@ spec: schema: > { "$schema": "http://json-schema.org/draft-04/schema#", "properties": { "id": { "$ref": "gorm.types.UUIDValue", "additionalProperties": true, "type": "object" }, "name": { "type": "string" }, "product_name": { "properties": { "id": { "$ref": "gorm.types.UUIDValue", "additionalProperties": true, "type": "object" }, "name": { "type": "string" }, "description": { "type": "string" }, "allowed": { "type": "boolean" } }, "additionalProperties": true, "type": "object" }, "event_type": { "properties": { "id": { "$ref": "gorm.types.UUIDValue", "additionalProperties": true, "type": "object" }, "name": { "type": "string" }, "description": { "type": "string" } }, "additionalProperties": true, "type": "object" }, "allowed": { "type": "boolean" }, "description": { "type": "string" } }, "additionalProperties": true, "type": "object", "definitions": { "gorm.types.UUIDValue": { "$schema": "http://json-schema.org/draft-04/schema#", "properties": { "value": { "type": "string" } }, "additionalProperties": true, "type": "object", "id": "gorm.types.UUIDValue" } }} --- -apiVersion: "tagging-v2.infoblox.com/v1" +apiVersion: "schemaregistry.infoblox.com/v1" kind: JsonSchema metadata: name: {{ .Release.Namespace }}.pb.eventtype @@ -25,7 +25,7 @@ spec: schema: > { "$schema": "http://json-schema.org/draft-04/schema#", "properties": { "id": { "properties": { "value": { "type": "string" } }, "additionalProperties": true, "type": "object" }, "name": { "type": "string" }, "description": { "type": "string" } }, "additionalProperties": true, "type": "object"} --- -apiVersion: "tagging-v2.infoblox.com/v1" +apiVersion: "schemaregistry.infoblox.com/v1" kind: JsonSchema metadata: name: {{ .Release.Namespace }}.pb.productname @@ -34,7 +34,7 @@ spec: schema: > { "$schema": "http://json-schema.org/draft-04/schema#", "properties": { "id": { "properties": { "value": { "type": "string" } }, "additionalProperties": true, "type": "object" }, "name": { "type": "string" }, "description": { "type": "string" }, "allowed": { "type": "boolean" } }, "additionalProperties": true, "type": "object"} --- -apiVersion: "tagging-v2.infoblox.com/v1" +apiVersion: "schemaregistry.infoblox.com/v1" kind: JsonSchema metadata: name: {{ .Release.Namespace }}.pb.summary diff --git a/schema_to_cr.go b/schema_to_cr.go index 466c04c..e5165ac 100644 --- a/schema_to_cr.go +++ b/schema_to_cr.go @@ -31,7 +31,8 @@ const crd_skeleton = "apiVersion: apiextensions.k8s.io/v1\nkind: CustomResourceD func main() { inputSchemaPtr := flag.String("inputschema", "", "The directory containing the schema files. tool will automatically import all schema files within subdirectories (required).") outputPathPtr := flag.String("outputpath", "", "The path to the directory where the result CRs will go (required).") - groupPtr := flag.String("group", "", "The string of the group for the created crd file (example: notifications.infoblox.com) (required).") + groupPtr := flag.String("group", "", "The string of the group for the created CR and CRD files (example: schemaregistry.infoblox.com) (required).") + makeCrdPtr := flag.Bool("makecrd", false, "Boolean option to choose whether to generate a new CRD file (optional; default false)") flag.Parse() if *inputSchemaPtr == "" || *outputPathPtr == "" || *groupPtr == "" { @@ -53,7 +54,7 @@ func main() { } crOutput := createCrOutput(inputSchema, group) - writeFiles(crOutput, outputPath, group) + writeFiles(crOutput, outputPath, group, *makeCrdPtr) } func parseNamespaces(schemaDirectory string) []string { @@ -124,7 +125,7 @@ func createCR(inputFilePath, schemaName, group string) string { return tpl.String() } -func writeFiles(crOutput map[string]string, outputPath, group string) { +func writeFiles(crOutput map[string]string, outputPath, group string, makeCrd bool) { for namespace, output := range crOutput { fo1, err := os.Create(outputPath + "/jsonschema-" + namespace + "-cr.yaml") if err != nil { @@ -136,6 +137,9 @@ func writeFiles(crOutput map[string]string, outputPath, group string) { fmt.Printf("Error writing to jsonschema-%v-cr.yaml file\r\n", namespace) } } + if !makeCrd { + return + } fo2, err := os.Create(outputPath + "/jsonschema-crd.yaml") if err != nil { fmt.Printf("Error creating crd.yaml file\r\n")