Skip to content

Commit

Permalink
feat: add serializer to remove protobuf support (#51)
Browse files Browse the repository at this point in the history
Signed-off-by: Vishal Choudhary <[email protected]>
  • Loading branch information
vishal-chdhry authored Feb 5, 2024
1 parent 37729fb commit 86b7b27
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 1 deletion.
2 changes: 2 additions & 0 deletions pkg/api/install.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ func BuildPolicyReports(polr, cpolr rest.Storage) genericapiserver.APIGroupInfo
"clusterpolicyreports": cpolr,
}
apiGroupInfo.VersionedResourcesStorageMap[v1alpha2.SchemeGroupVersion.Version] = policyReportsResources
apiGroupInfo.NegotiatedSerializer = DefaultSubsetNegotiatedSerializer(Codecs)

return apiGroupInfo
}
Expand All @@ -63,6 +64,7 @@ func BuildEphemeralReports(ephr, cephr rest.Storage) genericapiserver.APIGroupIn
"clusterephemeralreports": cephr,
}
apiGroupInfo.VersionedResourcesStorageMap[reportsv1.SchemeGroupVersion.Version] = ephemeralReportsResources
apiGroupInfo.NegotiatedSerializer = DefaultSubsetNegotiatedSerializer(Codecs)

return apiGroupInfo
}
Expand Down
37 changes: 37 additions & 0 deletions pkg/api/serializer.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package api

import (
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/runtime/serializer"
)

type subsetNegotiatedSerializer struct {
accepts []func(info runtime.SerializerInfo) bool
runtime.NegotiatedSerializer
}

func (s subsetNegotiatedSerializer) SupportedMediaTypes() []runtime.SerializerInfo {
base := s.NegotiatedSerializer.SupportedMediaTypes()
var filtered []runtime.SerializerInfo
for _, info := range base {
for _, accept := range s.accepts {
if accept(info) {
filtered = append(filtered, info)
break
}
}
}
return filtered
}

func NoProtobuf(info runtime.SerializerInfo) bool {
return info.MediaType != runtime.ContentTypeProtobuf
}

func SubsetNegotiatedSerializer(codecs serializer.CodecFactory, accepts ...func(info runtime.SerializerInfo) bool) runtime.NegotiatedSerializer {
return subsetNegotiatedSerializer{accepts, codecs}
}

func DefaultSubsetNegotiatedSerializer(codecs serializer.CodecFactory) runtime.NegotiatedSerializer {
return SubsetNegotiatedSerializer(codecs, NoProtobuf)
}
3 changes: 2 additions & 1 deletion pkg/app/opts/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,8 @@ func (o Options) restConfig() (*rest.Config, error) {
return nil, fmt.Errorf("unable to construct lister client config: %v", err)
}

// config.ContentType = "application/vnd.kubernetes.protobuf"
// config.ContentType = "application/json"
// config.AcceptContentTypes = "application/json,application/vnd.kubernetes.protobuf"

err = rest.SetKubernetesDefaults(config)
if err != nil {
Expand Down

0 comments on commit 86b7b27

Please sign in to comment.