Skip to content
This repository was archived by the owner on Feb 14, 2022. It is now read-only.

Commit 3a35484

Browse files
chris-cronesilvin-lubecki
authored andcommitted
Do not advertise protobuf support
This fixes #150. As the `Stack` struct does not support being serialized to protobuf, we must not advertise that we support protobuf. Signed-off-by: Christopher Crone <[email protected]>
1 parent bbd07b4 commit 3a35484

File tree

2 files changed

+45
-0
lines changed

2 files changed

+45
-0
lines changed

internal/apiserver/apiserver.go

+21
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ package apiserver
22

33
import (
44
"net/http"
5+
"reflect"
6+
"unsafe"
57

68
"github.com/docker/compose-on-kubernetes/api/compose/v1alpha3"
79
"github.com/docker/compose-on-kubernetes/api/compose/v1beta1"
@@ -53,6 +55,25 @@ func init() {
5355
if err := conversions.RegisterV1beta2Conversions(Scheme); err != nil {
5456
panic(err)
5557
}
58+
// We do not support protobuf serialization as the `Stack` struct has
59+
// fields with unsupported types (e.g.: map[string]*string). This causes
60+
// issues like https://github.com/docker/compose-on-kubernetes/issues/150.
61+
// The workaround is to remove protobuf from the advertised supported codecs.
62+
removeProtobufMediaType(&Codecs)
63+
}
64+
65+
// removeProtobufMediaType removes protobuf from the list of accepted media
66+
// types for the given CodecFactory.
67+
func removeProtobufMediaType(c *serializer.CodecFactory) {
68+
codecsPtr := reflect.Indirect(reflect.ValueOf(c))
69+
accepts := codecsPtr.FieldByName("accepts")
70+
acceptsPtr := (*[]runtime.SerializerInfo)(unsafe.Pointer(accepts.UnsafeAddr()))
71+
for i, v := range *acceptsPtr {
72+
if v.MediaType == runtime.ContentTypeProtobuf {
73+
*acceptsPtr = append((*acceptsPtr)[0:i], (*acceptsPtr)[i+1:]...)
74+
break
75+
}
76+
}
5677
}
5778

5879
// Config is the API server config

internal/apiserver/apiserver_test.go

+24
Original file line numberDiff line numberDiff line change
@@ -1 +1,25 @@
11
package apiserver
2+
3+
import (
4+
"testing"
5+
6+
"github.com/stretchr/testify/assert"
7+
"k8s.io/apimachinery/pkg/runtime"
8+
"k8s.io/apimachinery/pkg/runtime/serializer"
9+
)
10+
11+
func supportsProtobufCodec(c serializer.CodecFactory) bool {
12+
for _, v := range c.SupportedMediaTypes() {
13+
if v.MediaType == runtime.ContentTypeProtobuf {
14+
return true
15+
}
16+
}
17+
return false
18+
}
19+
20+
func TestRemoveProtobuf(t *testing.T) {
21+
codecs := serializer.NewCodecFactory(Scheme)
22+
assert.True(t, supportsProtobufCodec(codecs))
23+
removeProtobufMediaType(&codecs)
24+
assert.False(t, supportsProtobufCodec(codecs))
25+
}

0 commit comments

Comments
 (0)