Skip to content

Commit

Permalink
Make namespace portion of name a helm template variable instead of ha…
Browse files Browse the repository at this point in the history
…rdcoded (#15)
  • Loading branch information
brocwoodworthIBLX authored Oct 28, 2020
1 parent 27cd85c commit 234c5e1
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 21 deletions.
20 changes: 10 additions & 10 deletions example/cr/jsonschema-pb-cr.yaml
Original file line number Diff line number Diff line change
@@ -1,44 +1,44 @@
apiVersion: "tagging-v2.infoblox.com/v1"
kind: JsonSchema
metadata:
name: tagging-v2.pb.event
name: {{ .Release.Namespace }}.pb.event
spec:
name: tagging-v2.pb.event
name: {{ .Release.Namespace }}.pb.event
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"
kind: JsonSchema
metadata:
name: tagging-v2.pb.eventsubtype
name: {{ .Release.Namespace }}.pb.eventsubtype
spec:
name: tagging-v2.pb.eventsubtype
name: {{ .Release.Namespace }}.pb.eventsubtype
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"
kind: JsonSchema
metadata:
name: tagging-v2.pb.eventtype
name: {{ .Release.Namespace }}.pb.eventtype
spec:
name: tagging-v2.pb.eventtype
name: {{ .Release.Namespace }}.pb.eventtype
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"
kind: JsonSchema
metadata:
name: tagging-v2.pb.productname
name: {{ .Release.Namespace }}.pb.productname
spec:
name: tagging-v2.pb.productname
name: {{ .Release.Namespace }}.pb.productname
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"
kind: JsonSchema
metadata:
name: tagging-v2.pb.summary
name: {{ .Release.Namespace }}.pb.summary
spec:
name: tagging-v2.pb.summary
name: {{ .Release.Namespace }}.pb.summary
schema: >
{ "$schema": "http://json-schema.org/draft-04/schema#", "properties": { "id": { "$ref": "gorm.types.UUIDValue", "additionalProperties": true, "type": "object" }, "account_id": { "type": "string" }, "summarization_rule_id": { "$ref": "gorm.types.UUIDValue", "additionalProperties": true, "type": "object" }, "fingerprint": { "type": "string" }, "event_id": { "type": "string" }, "event_type": { "enum": [ "ACCOUNT", 0, "SYSTEM", 1, "PRODUCT", 2, "USER", 3 ], "oneOf": [ { "type": "string" }, { "type": "integer" } ] }, "event_subtype": { "type": "string" }, "event_application_id": { "type": "string" }, "event_location": { "type": "string" }, "event_severity": { "enum": [ "low", 0, "medium", 1, "high", 2 ], "oneOf": [ { "type": "string" }, { "type": "integer" } ] }, "event_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" }, "event_metadata": { "additionalProperties": { "type": "string" }, "type": "object" }, "event_occurredTime": { "type": "string", "format": "date-time" }, "event_generatedTime": { "type": "string", "format": "date-time" }, "event_status": { "enum": [ "RAISED", 0, "CLEARED", 1 ], "oneOf": [ { "type": "string" }, { "type": "integer" } ] }, "product_name": { "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" } }}
21 changes: 10 additions & 11 deletions schema_to_cr.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,9 @@ 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).")
namespacePtr := flag.String("namespace", "", "The namespace that the crd file exists in (example: notifications) (required)")

flag.Parse()
if *inputSchemaPtr == "" || *outputPathPtr == "" || *groupPtr == "" || *namespacePtr == "" {
if *inputSchemaPtr == "" || *outputPathPtr == "" || *groupPtr == "" {
flag.PrintDefaults()
os.Exit(1)
}
Expand All @@ -53,7 +52,7 @@ func main() {
os.Exit(1)
}

crOutput := createCrOutput(inputSchema, group, *namespacePtr)
crOutput := createCrOutput(inputSchema, group)
writeFiles(crOutput, outputPath, group)
}

Expand All @@ -77,7 +76,7 @@ func parseNamespaces(schemaDirectory string) []string {
return namespaces
}

func createCrOutput(inputSchema, group, namespace string) map[string]string {
func createCrOutput(inputSchema, group string) map[string]string {
crOutput := make(map[string]string)
namespaces := parseNamespaces(inputSchema)
for _, n := range namespaces {
Expand All @@ -91,35 +90,35 @@ func createCrOutput(inputSchema, group, namespace string) map[string]string {
namespaceOutput := ""
for _, f := range files {
filePath := namespaceDirectory + "/" + f.Name()
topic := namespace + "." + n + "." + strings.TrimSuffix(f.Name(), filepath.Ext(f.Name()))
schemaName := "{{ .Release.Namespace }}." + n + "." + strings.ToLower(strings.TrimSuffix(f.Name(), filepath.Ext(f.Name())))
if namespaceOutput != "" {
namespaceOutput = namespaceOutput + "---\n"
}
fmt.Printf("Creating custom resource for topic %v...\r\n", topic)
namespaceOutput = namespaceOutput + createCR(filePath, topic, group)
fmt.Printf("Creating custom resource for topic %v...\r\n", schemaName)
namespaceOutput = namespaceOutput + createCR(filePath, schemaName, group)
}
crOutput[n] = namespaceOutput
}
return crOutput
}

func createCR(inputFilePath, topic, group string) string {
func createCR(inputFilePath, schemaName, group string) string {
inputString, err := ioutil.ReadFile(inputFilePath)
if err != nil {
fmt.Printf("Error reading input file %v\r\n", inputFilePath)
os.Exit(1)
}
t, err := template.New("cr").Parse(cr_skeleton)
if err != nil {
fmt.Printf("Error processing template for schema %v", topic)
fmt.Printf("Error processing template for schema %v", schemaName)
}
var cr CR
cr.Name = strings.ToLower(topic)
cr.Name = schemaName
cr.Schema = string(strings.ReplaceAll(string(inputString), "\n", ""))
cr.Group = group
var tpl bytes.Buffer
if err := t.Execute(&tpl, cr); err != nil {
fmt.Printf("Error creating cr %v\r\n", topic)
fmt.Printf("Error creating cr %v\r\n", schemaName)
os.Exit(1)
}
return tpl.String()
Expand Down

0 comments on commit 234c5e1

Please sign in to comment.