You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

convert.go 2.9 kB

2 years ago
2 years ago
2 years ago
2 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. package tool
  2. import (
  3. "PCM/adaptor/PCM-CORE/model"
  4. "bytes"
  5. "encoding/json"
  6. "github.com/robfig/cron/v3"
  7. "io"
  8. "k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
  9. "k8s.io/apimachinery/pkg/runtime"
  10. syaml "k8s.io/apimachinery/pkg/runtime/serializer/yaml"
  11. kyaml "k8s.io/apimachinery/pkg/util/yaml"
  12. "mime/multipart"
  13. "sigs.k8s.io/yaml"
  14. "strconv"
  15. "strings"
  16. )
  17. // Convert 通过JSON赋值
  18. func Convert(source interface{}, target interface{}) {
  19. jsonByte, _ := json.Marshal(source)
  20. json.Unmarshal(jsonByte, &target)
  21. }
  22. // Int64ToString int64转string
  23. func Int64ToString(value int64) string {
  24. return strconv.FormatInt(value, 10)
  25. }
  26. // EntryIdToString EntryID转string
  27. func EntryIdToString(id cron.EntryID) string {
  28. return strconv.Itoa(int(id))
  29. }
  30. func StringToInt(value string) int {
  31. intValue, _ := strconv.Atoi(value)
  32. return intValue
  33. }
  34. func RunTimeToSeconds(runTime string) int {
  35. time := strings.Split(runTime, ":")
  36. day, _ := strconv.Atoi(time[0])
  37. hour, _ := strconv.Atoi(time[1])
  38. seconds, _ := strconv.Atoi(time[2])
  39. return day*3600 + hour*60 + seconds
  40. }
  41. func Yaml2struct(fileHeader *multipart.FileHeader, req interface{}) error {
  42. file, err := fileHeader.Open()
  43. if err != nil {
  44. return err
  45. }
  46. fileByte, err := io.ReadAll(file)
  47. if err != nil {
  48. return err
  49. }
  50. err = yaml.Unmarshal(fileByte, &req)
  51. if err != nil {
  52. return err
  53. }
  54. return nil
  55. }
  56. func K8sUnstructured(dataString string, target interface{}) {
  57. unstructuredList := unstructured.UnstructuredList{}
  58. json.Unmarshal([]byte(dataString), &unstructuredList)
  59. runtime.DefaultUnstructuredConverter.FromUnstructured(unstructuredList.UnstructuredContent(), target)
  60. }
  61. func UnMarshalK8sStruct(yamlString string, taskId int64) model.Cloud {
  62. var cloud model.Cloud
  63. d := kyaml.NewYAMLOrJSONDecoder(bytes.NewBufferString(yamlString), 4096)
  64. var err error
  65. for {
  66. var rawObj runtime.RawExtension
  67. err = d.Decode(&rawObj)
  68. if err == io.EOF {
  69. break
  70. }
  71. if err != nil {
  72. }
  73. obj := &unstructured.Unstructured{}
  74. syaml.NewDecodingSerializer(unstructured.UnstructuredJSONScheme).Decode(rawObj.Raw, nil, obj)
  75. if err != nil {
  76. }
  77. unstructuredMap, err := runtime.DefaultUnstructuredConverter.ToUnstructured(obj)
  78. if err != nil {
  79. }
  80. unstructureObj := &unstructured.Unstructured{Object: unstructuredMap}
  81. cloud = model.Cloud{
  82. TaskId: taskId,
  83. ApiVersion: unstructureObj.GetAPIVersion(),
  84. Name: unstructureObj.GetName(),
  85. Kind: unstructureObj.GetKind(),
  86. Namespace: unstructureObj.GetNamespace(),
  87. Status: "Saved",
  88. ServiceName: "kubeNative",
  89. }
  90. }
  91. return cloud
  92. }
  93. // removeDuplication_map 去重数组
  94. func RemoveDuplication_map(arr []string) []string {
  95. set := make(map[string]struct{}, len(arr))
  96. j := 0
  97. for _, v := range arr {
  98. _, ok := set[v]
  99. if ok {
  100. continue
  101. }
  102. set[v] = struct{}{}
  103. arr[j] = v
  104. j++
  105. }
  106. return arr[:j]
  107. }

PCM is positioned as Software stack over Cloud, aiming to build the standards and ecology of heterogeneous cloud collaboration for JCC in a non intrusive and autonomous peer-to-peer manner.