-
Notifications
You must be signed in to change notification settings - Fork 0
/
protos.go
146 lines (126 loc) · 3.24 KB
/
protos.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
package main
import (
_ "embed"
"encoding/json"
"fmt"
"strings"
"github.com/emcfarlane/starlarkproto"
"go.starlark.net/starlark"
"go.starlark.net/starlarkstruct"
"google.golang.org/protobuf/encoding/protojson"
"google.golang.org/protobuf/proto"
"google.golang.org/protobuf/reflect/protodesc"
"google.golang.org/protobuf/reflect/protoreflect"
"google.golang.org/protobuf/reflect/protoregistry"
"google.golang.org/protobuf/types/descriptorpb"
"google.golang.org/protobuf/types/dynamicpb"
"sigs.k8s.io/yaml"
)
type k8Object struct {
kind string
apiVersion string
}
var (
//go:embed protos.star
protoStar []byte
//go:embed protos.pb
protoPB []byte
globals = starlark.StringDict{
"struct": starlark.NewBuiltin("struct", starlarkstruct.Make),
"proto": starlarkproto.NewModule(protoregistry.GlobalFiles),
}
kindLooup = map[k8Object]protoreflect.FullName{}
)
func genAPIVersion(md protoreflect.FileDescriptor) string {
return strings.ReplaceAll(
strings.TrimPrefix(
strings.TrimPrefix(
string(md.FullName()),
"k8s.io.api.",
),
"k8s.io.apimachinery.pkg.",
),
".", "/",
)
}
func parseAPIVersion(apiVersion string) string {
if strings.Count(apiVersion, "/") == 0 {
return "core/" + apiVersion
}
return apiVersion
}
func init() {
var descSet descriptorpb.FileDescriptorSet
if err := proto.Unmarshal(protoPB, &descSet); err != nil {
panic(err)
}
for _, file := range descSet.File {
fd, err := protodesc.NewFile(file, protoregistry.GlobalFiles)
if err != nil {
panic(err)
}
if err := protoregistry.GlobalFiles.RegisterFile(fd); err != nil {
panic(err)
}
apiVersion := genAPIVersion(fd)
name := strings.ReplaceAll(apiVersion, "/", "_")
globals[name] = starlarkproto.NewDescriptor(fd)
//fmt.Printf("%s = proto.file(%q)\n", name, fd.Path())
for i := 0; i < fd.Messages().Len(); i++ {
md := fd.Messages().Get(i)
k8 := k8Object{
kind: string(md.Name()),
apiVersion: apiVersion,
}
kindLooup[k8] = md.FullName()
}
}
}
func yamlToProtos(src string) ([]proto.Message, error) {
var msgs []proto.Message
for _, part := range strings.Split(src, "---\n") {
jb, err := yaml.YAMLToJSON([]byte(part))
if err != nil {
return nil, err
}
fmt.Println(string(jb))
var m map[string]interface{}
if err := json.Unmarshal(jb, &m); err != nil {
return nil, err
}
kind, ok := m["kind"].(string)
if !ok {
return nil, fmt.Errorf("kind invalid")
}
apiVersion, ok := m["apiVersion"].(string)
if !ok {
return nil, fmt.Errorf("apiVersion invalid")
}
apiVersion = parseAPIVersion(apiVersion)
delete(m, "kind")
delete(m, "apiVersion")
jb, err = json.Marshal(m)
if err != nil {
return nil, err
}
typ, ok := kindLooup[k8Object{
kind: kind,
apiVersion: apiVersion,
}]
if !ok {
return nil, fmt.Errorf("kind %s not found", kind)
}
desc, err := protoregistry.GlobalFiles.FindDescriptorByName(typ)
if err != nil {
return nil, fmt.Errorf("type %s not found: %w", typ, err)
}
md := desc.(protoreflect.MessageDescriptor)
msg := dynamicpb.NewMessage(md)
if err := protojson.Unmarshal(jb, msg); err != nil {
return nil, err
}
msgs = append(msgs, msg)
//fmt.Printf("kind: %s, apiVersion: %s\n", kind, apiVersion)
}
return msgs, nil
}