MCPcopy
hub / github.com/kubesphere/kubekey / Extension2Variables

Function Extension2Variables

pkg/variable/helper.go:422–454  ·  view source on GitHub ↗

Extension2Variables converts a runtime.RawExtension to a map[string]any variables structure. It first tries to unmarshal the extension as a map[string]any. If that fails, it tries to unmarshal as a []any (array). If that also fails, it tries to unmarshal as a string (useful for template strings enco

(ext runtime.RawExtension)

Source from the content-addressed store, hash-verified

420// If that also fails, it tries to unmarshal as a string (useful for template strings encoded as JSON).
421// If all attempts fail, it logs an error and returns the raw bytes as a string under the "raw" key.
422func Extension2Variables(ext runtime.RawExtension) map[string]any {
423 if len(ext.Raw) == 0 {
424 return make(map[string]any)
425 }
426
427 // Try to unmarshal as map[string]any
428 var data map[string]any
429 if err := json.Unmarshal(ext.Raw, &data); err == nil {
430 return data
431 }
432
433 // If failed, try to unmarshal as []any (array)
434 var arr []any
435 if err := json.Unmarshal(ext.Raw, &arr); err == nil {
436 return map[string]any{
437 "raw": arr,
438 }
439 }
440
441 // If still failed, try to unmarshal as string (e.g., a marshaled template string)
442 var str string
443 if err2 := json.Unmarshal(ext.Raw, &str); err2 == nil {
444 return map[string]any{
445 "raw": str,
446 }
447 }
448
449 // If still failed, log error and return raw bytes as string
450 klog.V(4).ErrorS(nil, "failed to unmarshal extension to variables", "raw", string(ext.Raw))
451 return map[string]any{
452 "raw": string(ext.Raw),
453 }
454}
455
456// Extension2String converts a runtime.RawExtension to a string, optionally parsing it as a template.
457// If the extension is empty, it returns nil. If the string is quoted, it unquotes it first.

Callers 15

TestFetchArgsParseFunction · 0.92
TestFetchArgsTemplateFunction · 0.92
ModuleFetchFunction · 0.92
newImageArgsFunction · 0.92
TestImageArgsParseFunction · 0.92
newCopyArgsFunction · 0.92
ModulePrometheusFunction · 0.92
TestPrometheusArgsParseFunction · 0.92
ModuleSetupFunction · 0.92
TestSetupArgsParseFunction · 0.92
ModuleDebugFunction · 0.92

Calls

no outgoing calls

Tested by 9

TestFetchArgsParseFunction · 0.74
TestFetchArgsTemplateFunction · 0.74
TestImageArgsParseFunction · 0.74
TestPrometheusArgsParseFunction · 0.74
TestSetupArgsParseFunction · 0.74
TestDebugArgsParseFunction · 0.74
TestHttpGetFileArgsParseFunction · 0.74