jsonnet-libs / k8s

Code generator for Jsonnet Kubernetes libraries.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Strimzi CRD error `got -: {`

primeroz opened this issue · comments

Hi,

I am trying to generate libsonnet for strimzi kafka-operator 0.23.0

i added a libs/strimzi/config.jsonnet like

local config = import 'jsonnet/config.jsonnet';

config.new(
  name='strimzi',
  specs=[
    {
      output: '0.23.0',
      openapi: 'http://localhost:8001/openapi/v2',
      prefix: '^io\\.strimzi\\..*',
      crds: ['https://github.com/strimzi/strimzi-kafka-operator/releases/download/0.23.0/strimzi-crds-0.23.0.yaml'],
      localName: 'strimzi',
    },
  ]
)

when run make run INPUT_DIR=libs/strimzi i get

140:11-12 Expected one of :, ::, :::, +:, +::, +:::, got: -: {  

I did check the swagger output and i can't see any -: so not sure what is causing this

when looking in the rendered dir

tree gen/github.com/jsonnet-libs/strimzi-lib
gen/github.com/jsonnet-libs/strimzi-lib
├── 0.23.0/
│   ├── _gen/
│   │   └── kafka/
│   │       ├── v1beta1/
│   │       │   ├── kafkaUser.libsonnet
│   │       │   └── main.libsonnet
│   │       └── v1beta2/
│   │           └── kafkaTopic.libsonnet
│   └── gen.libsonnet
├── docs/
│   ├── stylesheets/
│   │   └── extra.css
│   └── README.md
├── .github/
│   └── workflows/
│       └── main.yml
├── LICENSE
├── mkdocs.yml
├── README.md
└── requirements.txt

so it would seem it breaks after v1beta2/kafkaTopic

It failed on this object, the -XX field name trips up the generator.

...
    '#jvmOptions':: d.obj(help="**Currently not supported** JVM Options for pods."),
    jvmOptions: {
      '#with-XX':: d.fn(help="A map of -XX options to the JVM.", args=[d.arg(name="-XX", type=d.T.any)]),
      with-XX(-XX): { spec+: { jvmOptions+: { -XX: -XX } } },
      '#with-Xms':: d.fn(help="-Xms option to to the JVM.", args=[d.arg(name="-Xms", type=d.T.string)]),
      with-Xms(-Xms): { spec+: { jvmOptions+: { -Xms: -Xms } } },
      '#with-Xmx':: d.fn(help="-Xmx option to to the JVM.", args=[d.arg(name="-Xmx", type=d.T.string)]),
      with-Xmx(-Xmx): { spec+: { jvmOptions+: { -Xmx: -Xmx } } },
      '#withGcLoggingEnabled':: d.fn(help="Specifies whether the Garbage Collection logging is enabled. The default is false.", args=[d.arg(name="gcLoggingEnabled", type=d.T.boolean)]),
      withGcLoggingEnabled(gcLoggingEnabled): { spec+: { jvmOptions+: { gcLoggingEnabled: gcLoggingEnabled } } },
      '#withJavaSystemProperties':: d.fn(help="A map of additional system properties which will be passed using the `-D` option to the JVM.", args=[d.arg(name="javaSystemProperties", type=d.T.array)]),
      withJavaSystemProperties(javaSystemProperties): { spec+: { jvmOptions+: { javaSystemProperties: if std.isArray(v=javaSystemProperties) then javaSystemProperties else [javaSystemProperties] } } },
      '#withJavaSystemPropertiesMixin':: d.fn(help="A map of additional system properties which will be passed using the `-D` option to the JVM.\n\n**Note:** This function appends passed data to existing values", args=[d.arg(name="javaSystemProperties", type=d.T.array)]),
      withJavaSystemPropertiesMixin(javaSystemProperties): { spec+: { jvmOptions+: { javaSystemProperties+: if std.isArray(v=javaSystemProperties) then javaSystemProperties else [javaSystemProperties] } } }
    },
...

I think we need to escape that somehow, I'd start searching here: https://github.com/jsonnet-libs/k8s/blob/master/pkg/builder/objects.go#L43

thank you, i will have a look at the escaping.

BTW how did you find the objects on which k8s was failing ? is there a trick or did you literally just search for anything with - ?

140:11-12 Expected one of :, ::, :::, +:, +::, +:::, got: -: {

140 is the line number here, the current master also prints the jsonnet that it fails upon, this allowed me to quickly find the cause.

Hi thanks for your pointers, i got something working and i will do a PR later on once i have done a bit more testing.

in particular i want to check that the rendering of all files for the k8s-alpha and istio and the other configs that are already part of the config is not change

FYI this is the diff

diff --git a/pkg/builder/objects.go b/pkg/builder/objects.go
index 726705a..9558b4b 100644
--- a/pkg/builder/objects.go
+++ b/pkg/builder/objects.go
@@ -45,7 +45,7 @@ func escapeKey(s string) string {
 	case "local", "error":
 		return fmt.Sprintf(`'%s'`, s)
 	default:
-		if strings.HasPrefix(s, "#") {
+		if strings.HasPrefix(s, "#") || strings.HasPrefix(s, "-") {
 			return fmt.Sprintf(`'%s'`, s)
 		}
 		return s
diff --git a/pkg/model/modifiers.go b/pkg/model/modifiers.go
index 69bc7e6..f3b8d3a 100644
--- a/pkg/model/modifiers.go
+++ b/pkg/model/modifiers.go
@@ -58,6 +58,14 @@ type Object struct {
 	Fields modifiers `json:"fields"`
 }
 
+func sanitizePrefix(s string) string {
+
+	s = strings.TrimPrefix(s, "-")
+	s = strings.TrimPrefix(s, "+")
+
+	return s
+}
+
 // modsForProps generates Modifiers for a (nested) map of swagger properties
 // (object fields)
 func modsForProps(props map[string]*swagger.Schema, ctx string) map[string]interface{} {
@@ -97,7 +105,7 @@ func newModifier(name string, p *swagger.Schema, ctx string) (string, interface{
 			Target: strings.TrimPrefix(ctx+"."+name, "."),
 			Type:   p.Type,
 		}
-		return fmt.Sprintf("with%s", strings.Title(name)), fn
+		return fmt.Sprintf("with%s", strings.Title(sanitizePrefix(name))), fn
 	}
 }
 
@@ -109,7 +117,7 @@ func fnArg(name string) string {
 	if name == "local" {
 		return "Local"
 	}
-	return name
+	return sanitizePrefix(name)
 }
 
 // CamelLower returns the string with the word lowercased.

I did something very similar in the linked PR, review welcome, I might have missed something.

Rendering look good. thankyou !