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

Commit

Permalink
Do not advertise protobuf support
Browse files Browse the repository at this point in the history
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 <christopher.crone@docker.com>
  • Loading branch information
chris-crone authored and silvin-lubecki committed Nov 7, 2019
1 parent bbd07b4 commit 3a35484
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 0 deletions.
21 changes: 21 additions & 0 deletions internal/apiserver/apiserver.go
Expand Up @@ -2,6 +2,8 @@ package apiserver

import (
"net/http"
"reflect"
"unsafe"

"github.com/docker/compose-on-kubernetes/api/compose/v1alpha3"
"github.com/docker/compose-on-kubernetes/api/compose/v1beta1"
Expand Down Expand Up @@ -53,6 +55,25 @@ func init() {
if err := conversions.RegisterV1beta2Conversions(Scheme); err != nil {
panic(err)
}
// We do not support protobuf serialization as the `Stack` struct has
// fields with unsupported types (e.g.: map[string]*string). This causes
// issues like https://github.com/docker/compose-on-kubernetes/issues/150.
// The workaround is to remove protobuf from the advertised supported codecs.
removeProtobufMediaType(&Codecs)
}

// removeProtobufMediaType removes protobuf from the list of accepted media
// types for the given CodecFactory.
func removeProtobufMediaType(c *serializer.CodecFactory) {
codecsPtr := reflect.Indirect(reflect.ValueOf(c))
accepts := codecsPtr.FieldByName("accepts")
acceptsPtr := (*[]runtime.SerializerInfo)(unsafe.Pointer(accepts.UnsafeAddr()))
for i, v := range *acceptsPtr {
if v.MediaType == runtime.ContentTypeProtobuf {
*acceptsPtr = append((*acceptsPtr)[0:i], (*acceptsPtr)[i+1:]...)
break
}
}
}

// Config is the API server config
Expand Down
24 changes: 24 additions & 0 deletions internal/apiserver/apiserver_test.go
@@ -1 +1,25 @@
package apiserver

import (
"testing"

"github.com/stretchr/testify/assert"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/runtime/serializer"
)

func supportsProtobufCodec(c serializer.CodecFactory) bool {
for _, v := range c.SupportedMediaTypes() {
if v.MediaType == runtime.ContentTypeProtobuf {
return true
}
}
return false
}

func TestRemoveProtobuf(t *testing.T) {
codecs := serializer.NewCodecFactory(Scheme)
assert.True(t, supportsProtobufCodec(codecs))
removeProtobufMediaType(&codecs)
assert.False(t, supportsProtobufCodec(codecs))
}

0 comments on commit 3a35484

Please sign in to comment.