Skip to content

Commit 6a29c60

Browse files
authored
fix: crd default value (#83)
Signed-off-by: peefy <[email protected]>
1 parent cdaa48b commit 6a29c60

File tree

4 files changed

+80
-24
lines changed

4 files changed

+80
-24
lines changed

pkg/kube_resource/generator/testdata/array_branch/models/operator_victoriametrics_com_v1beta1_vm_agent.k

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -878,8 +878,8 @@ schema OperatorVictoriametricsComV1beta1VMAgentSpecClaimTemplatesItems0SpecResou
878878

879879

880880
check:
881-
all _, n in limits { regex.match(str(n), r"^(\+|-)?(([0-9]+(\.[0-9]*)?)|(\.[0-9]+))(([KMGTPE]i)|[numkMGTPE]|([eE](\+|-)?(([0-9]+(\.[0-9]*)?)|(\.[0-9]+))))?$") } if limits
882-
all _, n in requests { regex.match(str(n), r"^(\+|-)?(([0-9]+(\.[0-9]*)?)|(\.[0-9]+))(([KMGTPE]i)|[numkMGTPE]|([eE](\+|-)?(([0-9]+(\.[0-9]*)?)|(\.[0-9]+))))?$") } if requests
881+
all _, limits in limits {regex.match(str(limits), r"^(\+|-)?(([0-9]+(\.[0-9]*)?)|(\.[0-9]+))(([KMGTPE]i)|[numkMGTPE]|([eE](\+|-)?(([0-9]+(\.[0-9]*)?)|(\.[0-9]+))))?$") if limits } if limits
882+
all _, requests in requests {regex.match(str(requests), r"^(\+|-)?(([0-9]+(\.[0-9]*)?)|(\.[0-9]+))(([KMGTPE]i)|[numkMGTPE]|([eE](\+|-)?(([0-9]+(\.[0-9]*)?)|(\.[0-9]+))))?$") if requests } if requests
883883

884884

885885
schema OperatorVictoriametricsComV1beta1VMAgentSpecClaimTemplatesItems0SpecSelector:
@@ -957,8 +957,8 @@ schema OperatorVictoriametricsComV1beta1VMAgentSpecClaimTemplatesItems0Status:
957957

958958

959959
check:
960-
all _, n in allocatedResources { regex.match(str(n), r"^(\+|-)?(([0-9]+(\.[0-9]*)?)|(\.[0-9]+))(([KMGTPE]i)|[numkMGTPE]|([eE](\+|-)?(([0-9]+(\.[0-9]*)?)|(\.[0-9]+))))?$") } if allocatedResources
961-
all _, n in capacity { regex.match(str(n), r"^(\+|-)?(([0-9]+(\.[0-9]*)?)|(\.[0-9]+))(([KMGTPE]i)|[numkMGTPE]|([eE](\+|-)?(([0-9]+(\.[0-9]*)?)|(\.[0-9]+))))?$") } if capacity
960+
all _, allocatedResources in allocatedResources {regex.match(str(allocatedResources), r"^(\+|-)?(([0-9]+(\.[0-9]*)?)|(\.[0-9]+))(([KMGTPE]i)|[numkMGTPE]|([eE](\+|-)?(([0-9]+(\.[0-9]*)?)|(\.[0-9]+))))?$") if allocatedResources } if allocatedResources
961+
all _, capacity in capacity {regex.match(str(capacity), r"^(\+|-)?(([0-9]+(\.[0-9]*)?)|(\.[0-9]+))(([KMGTPE]i)|[numkMGTPE]|([eE](\+|-)?(([0-9]+(\.[0-9]*)?)|(\.[0-9]+))))?$") if capacity } if capacity
962962

963963

964964
schema OperatorVictoriametricsComV1beta1VMAgentSpecClaimTemplatesItems0StatusConditionsItems0:
@@ -2475,8 +2475,8 @@ schema OperatorVictoriametricsComV1beta1VMAgentSpecResources:
24752475

24762476

24772477
check:
2478-
all _, n in limits { regex.match(str(n), r"^(\+|-)?(([0-9]+(\.[0-9]*)?)|(\.[0-9]+))(([KMGTPE]i)|[numkMGTPE]|([eE](\+|-)?(([0-9]+(\.[0-9]*)?)|(\.[0-9]+))))?$") } if limits
2479-
all _, n in requests { regex.match(str(n), r"^(\+|-)?(([0-9]+(\.[0-9]*)?)|(\.[0-9]+))(([KMGTPE]i)|[numkMGTPE]|([eE](\+|-)?(([0-9]+(\.[0-9]*)?)|(\.[0-9]+))))?$") } if requests
2478+
all _, limits in limits {regex.match(str(limits), r"^(\+|-)?(([0-9]+(\.[0-9]*)?)|(\.[0-9]+))(([KMGTPE]i)|[numkMGTPE]|([eE](\+|-)?(([0-9]+(\.[0-9]*)?)|(\.[0-9]+))))?$") if limits } if limits
2479+
all _, requests in requests {regex.match(str(requests), r"^(\+|-)?(([0-9]+(\.[0-9]*)?)|(\.[0-9]+))(([KMGTPE]i)|[numkMGTPE]|([eE](\+|-)?(([0-9]+(\.[0-9]*)?)|(\.[0-9]+))))?$") if requests } if requests
24802480

24812481

24822482
schema OperatorVictoriametricsComV1beta1VMAgentSpecRollingUpdate:
@@ -2876,8 +2876,8 @@ schema OperatorVictoriametricsComV1beta1VMAgentSpecStatefulStorageVolumeClaimTem
28762876

28772877

28782878
check:
2879-
all _, n in limits { regex.match(str(n), r"^(\+|-)?(([0-9]+(\.[0-9]*)?)|(\.[0-9]+))(([KMGTPE]i)|[numkMGTPE]|([eE](\+|-)?(([0-9]+(\.[0-9]*)?)|(\.[0-9]+))))?$") } if limits
2880-
all _, n in requests { regex.match(str(n), r"^(\+|-)?(([0-9]+(\.[0-9]*)?)|(\.[0-9]+))(([KMGTPE]i)|[numkMGTPE]|([eE](\+|-)?(([0-9]+(\.[0-9]*)?)|(\.[0-9]+))))?$") } if requests
2879+
all _, limits in limits {regex.match(str(limits), r"^(\+|-)?(([0-9]+(\.[0-9]*)?)|(\.[0-9]+))(([KMGTPE]i)|[numkMGTPE]|([eE](\+|-)?(([0-9]+(\.[0-9]*)?)|(\.[0-9]+))))?$") if limits } if limits
2880+
all _, requests in requests {regex.match(str(requests), r"^(\+|-)?(([0-9]+(\.[0-9]*)?)|(\.[0-9]+))(([KMGTPE]i)|[numkMGTPE]|([eE](\+|-)?(([0-9]+(\.[0-9]*)?)|(\.[0-9]+))))?$") if requests } if requests
28812881

28822882

28832883
schema OperatorVictoriametricsComV1beta1VMAgentSpecStatefulStorageVolumeClaimTemplateSpecSelector:
@@ -2955,8 +2955,8 @@ schema OperatorVictoriametricsComV1beta1VMAgentSpecStatefulStorageVolumeClaimTem
29552955

29562956

29572957
check:
2958-
all _, n in allocatedResources { regex.match(str(n), r"^(\+|-)?(([0-9]+(\.[0-9]*)?)|(\.[0-9]+))(([KMGTPE]i)|[numkMGTPE]|([eE](\+|-)?(([0-9]+(\.[0-9]*)?)|(\.[0-9]+))))?$") } if allocatedResources
2959-
all _, n in capacity { regex.match(str(n), r"^(\+|-)?(([0-9]+(\.[0-9]*)?)|(\.[0-9]+))(([KMGTPE]i)|[numkMGTPE]|([eE](\+|-)?(([0-9]+(\.[0-9]*)?)|(\.[0-9]+))))?$") } if capacity
2958+
all _, allocatedResources in allocatedResources {regex.match(str(allocatedResources), r"^(\+|-)?(([0-9]+(\.[0-9]*)?)|(\.[0-9]+))(([KMGTPE]i)|[numkMGTPE]|([eE](\+|-)?(([0-9]+(\.[0-9]*)?)|(\.[0-9]+))))?$") if allocatedResources } if allocatedResources
2959+
all _, capacity in capacity {regex.match(str(capacity), r"^(\+|-)?(([0-9]+(\.[0-9]*)?)|(\.[0-9]+))(([KMGTPE]i)|[numkMGTPE]|([eE](\+|-)?(([0-9]+(\.[0-9]*)?)|(\.[0-9]+))))?$") if capacity } if capacity
29602960

29612961

29622962
schema OperatorVictoriametricsComV1beta1VMAgentSpecStatefulStorageVolumeClaimTemplateStatusConditionsItems0:

pkg/swagger/generator/language.go

Lines changed: 48 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
package generator
1616

1717
import (
18+
"encoding/json"
1819
"fmt"
1920
"log"
2021
"path"
@@ -164,14 +165,47 @@ func (l *LanguageOpts) ToKclValue(data interface{}) string {
164165
return fmt.Sprintf("[%s]", content)
165166
case reflect.String:
166167
return fmt.Sprintf("\"%s\"", data)
168+
case reflect.Int,
169+
reflect.Int8,
170+
reflect.Int16,
171+
reflect.Int32,
172+
reflect.Int64,
173+
reflect.Uint,
174+
reflect.Uint8,
175+
reflect.Uint16,
176+
reflect.Uint32,
177+
reflect.Uint64:
178+
return fmt.Sprintf("%v", data)
179+
case reflect.Float32, reflect.Float64:
180+
return fmt.Sprintf("%v", data)
167181
case reflect.Bool:
168182
if data.(bool) {
169183
return "True"
170184
}
171185
return "False"
172186
default:
173-
//TODO(xiarui.xr): support user defined struct
174-
return fmt.Sprintf("%v", data)
187+
// Reflect value
188+
if dataValue, ok := data.(reflect.Value); ok {
189+
return l.ToKclValue(dataValue.Interface())
190+
} else if dataSlice, ok := data.(yaml.MapSlice); ok {
191+
// If is a MapSlice
192+
var dictContents []string
193+
for _, v := range dataSlice {
194+
k := v.Key
195+
v := v.Value
196+
dictContents = append(dictContents, fmt.Sprintf("%s: %s", l.ToKclValue(k), l.ToKclValue(v)))
197+
}
198+
content := strings.Join(dictContents, ", ")
199+
return fmt.Sprintf("{%s}", content)
200+
} else {
201+
// User defined struct
202+
valueString, err := ToKCLValueString(data)
203+
if err != nil {
204+
log.Fatal(err)
205+
return "None"
206+
}
207+
return valueString
208+
}
175209
}
176210
}
177211

@@ -326,3 +360,15 @@ func KclLangOpts() *LanguageOpts {
326360
opts.Init()
327361
return opts
328362
}
363+
364+
func ToKCLValueString(value interface{}) (string, error) {
365+
jsonString, err := json.Marshal(value)
366+
if err != nil {
367+
return "", err
368+
}
369+
// In KCL, `true`, `false` and `null` are denoted by `True`, `False` and `None`.
370+
result := strings.Replace(string(jsonString), ": true", ": True", -1)
371+
result = strings.Replace(result, ": false", ": False", -1)
372+
result = strings.Replace(result, ": null", ": None", -1)
373+
return result, nil
374+
}

pkg/swagger/generator/model.go

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1513,31 +1513,42 @@ func RecoverMapValueOrder(oldValue interface{}) interface{} {
15131513
keys := value.MapKeys()
15141514
var newValue yaml.MapSlice = make([]yaml.MapItem, len(keys))
15151515

1516-
for _, key := range keys {
1516+
for i, key := range keys {
15171517
k := key.Interface()
15181518
v := value.MapIndex(key).Interface()
1519-
15201519
mapV := reflect.ValueOf(v)
15211520
switch mapV.Kind() {
15221521
case reflect.Map:
1522+
hasXOrder := false
15231523
var order int64
15241524
var innerValue interface{}
15251525
mapIter := mapV.MapRange()
15261526
for mapIter.Next() {
15271527
kk := mapIter.Key().String()
15281528
if kk == xOrder {
1529+
hasXOrder = true
15291530
order = int64(mapIter.Value().Interface().(float64))
15301531
}
15311532
if kk == "value" {
15321533
innerValue = mapIter.Value().Interface()
15331534
}
15341535
}
1535-
newValue[order] = yaml.MapItem{
1536-
Key: k,
1537-
Value: RecoverMapValueOrder(innerValue),
1536+
if hasXOrder {
1537+
newValue[order] = yaml.MapItem{
1538+
Key: k,
1539+
Value: RecoverMapValueOrder(innerValue),
1540+
}
1541+
} else {
1542+
newValue[i] = yaml.MapItem{
1543+
Key: k,
1544+
Value: RecoverMapValueOrder(v),
1545+
}
15381546
}
15391547
default:
1540-
log.Fatalf("unexpected ordered map value: %s", v)
1548+
newValue[i] = yaml.MapItem{
1549+
Key: k,
1550+
Value: RecoverMapValueOrder(v),
1551+
}
15411552
}
15421553
}
15431554
return newValue

pkg/swagger/generator/spec.go

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@ package generator
1717
import (
1818
"errors"
1919
"fmt"
20-
"io/ioutil"
2120
"log"
2221
"os"
2322
"path/filepath"
@@ -186,11 +185,11 @@ func WithXOrder(specPath string, addXOrderFunc func(yamlDoc interface{}) interfa
186185
panic(err)
187186
}
188187

189-
tmpFile, err := ioutil.TempFile("", filepath.Base(specPath))
188+
tmpFile, err := os.CreateTemp("", filepath.Base(specPath))
190189
if err != nil {
191190
panic(err)
192191
}
193-
if err := ioutil.WriteFile(tmpFile.Name(), out, 0); err != nil {
192+
if err := os.WriteFile(tmpFile.Name(), out, 0); err != nil {
194193
panic(err)
195194
}
196195
return tmpFile.Name()
@@ -250,17 +249,17 @@ func AddXOrderOnDefaultExample(yamlDoc interface{}) interface{} {
250249
break
251250
}
252251
}
253-
// add default / example x-order
252+
// Add default / example x-order
254253
var addXOrder func(interface{})
255254
addXOrder = func(element interface{}) {
256-
// 假设element是某个definition,即,是一个schema,首先找到其中的default、example选项
255+
// Assuming element is a certain definition, that is, a schema, first find the default and example options in it
257256
if defaultValue, ok := lookForSlice(element, "default"); ok {
258257
addXOrder2MapValue(defaultValue)
259258
}
260259
if exampleValue, ok := lookForSlice(element, "example"); ok {
261260
addXOrder2MapValue(exampleValue)
262261
}
263-
// look for the properties and add addXOrder on each property
262+
// Look for the properties and add addXOrder on each property
264263
if props, ok := lookForMapSlice(element, "properties"); ok {
265264
for _, prop := range props {
266265
if pSlice, ok := prop.Value.(yaml.MapSlice); ok {

0 commit comments

Comments
 (0)