Skip to content

Commit

Permalink
make subresource proxy shard-aware
Browse files Browse the repository at this point in the history
Signed-off-by: David Festal <dfestal@redhat.com>
  • Loading branch information
davidfestal committed Apr 6, 2023
1 parent 5d93a10 commit 3a91224
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 15 deletions.
1 change: 1 addition & 0 deletions pkg/server/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -395,6 +395,7 @@ func NewConfig(opts kcpserveroptions.CompletedOptions) (*Config, error) {
apiHandler,
c.DynamicClusterClient,
c.KcpSharedInformerFactory,
c.CacheKcpSharedInformerFactory,
)
}

Expand Down
28 changes: 13 additions & 15 deletions pkg/tunneler/podsubresourceproxy_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,12 +48,22 @@ var (
errorCodecs = serializer.NewCodecFactory(errorScheme)
)

func init() {
errorScheme.AddUnversionedTypes(metav1.Unversioned,
&metav1.Status{},
)
}

// WithPodSubresourceProxying proxies the POD subresource requests using the syncer tunneler.
func (tn *tunneler) WithPodSubresourceProxying(apiHandler http.Handler, kcpclient dynamic.ClusterInterface, kcpInformer kcpinformers.SharedInformerFactory) http.Handler {
func (tn *tunneler) WithPodSubresourceProxying(apiHandler http.Handler, kcpclient dynamic.ClusterInterface, kcpInformer kcpinformers.SharedInformerFactory, globalKcpInformer kcpinformers.SharedInformerFactory) http.Handler {
syncTargetInformer, err := kcpInformer.ForResource(workloadv1alpha1.SchemeGroupVersion.WithResource("synctargets"))
if err != nil {
panic(err)
}
globalSyncTargetInformer, err := globalKcpInformer.ForResource(workloadv1alpha1.SchemeGroupVersion.WithResource("synctargets"))
if err != nil {
panic(err)
}

return &podSubresourceProxyHandler{
proxyFunc: tn.Proxy,
Expand All @@ -70,18 +80,14 @@ func (tn *tunneler) WithPodSubresourceProxying(apiHandler http.Handler, kcpclien
return pod, nil
},
getSyncTargetBySynctargetKey: func(ctx context.Context, synctargetKey string) (*workloadv1alpha1.SyncTarget, error) {
synctargets, err := syncTargetInformer.Informer().GetIndexer().ByIndex(indexers.SyncTargetsBySyncTargetKey, synctargetKey)
synctargets, err := indexers.ByIndexWithFallback[*workloadv1alpha1.SyncTarget](syncTargetInformer.Informer().GetIndexer(), globalSyncTargetInformer.Informer().GetIndexer(), indexers.SyncTargetsBySyncTargetKey, synctargetKey)
if err != nil {
return nil, err
}
if len(synctargets) != 1 {
return nil, fmt.Errorf("expected 1 synctarget for key %q, got %d", synctargetKey, len(synctargets))
}
synctarget, ok := synctargets[0].(*workloadv1alpha1.SyncTarget)
if !ok {
return nil, fmt.Errorf("expected synctarget to be of type %T, got %T", &workloadv1alpha1.SyncTarget{}, synctargets[0])
}
return synctarget, nil
return synctargets[0], nil
},
}
}
Expand Down Expand Up @@ -135,7 +141,6 @@ func (b *podSubresourceProxyHandler) ServeHTTP(w http.ResponseWriter, req *http.
apierrors.NewBadRequest(fmt.Sprintf("invalid subresource or not implemented %q", subresource)),
errorCodecs, schema.GroupVersion{}, w, req,
)
b.apiHandler.ServeHTTP(w, req)
return
}

Expand All @@ -148,15 +153,13 @@ func (b *podSubresourceProxyHandler) ServeHTTP(w http.ResponseWriter, req *http.
apierrors.NewNotFound(schema.GroupResource{Group: "", Resource: "pods"}, podName),
errorCodecs, schema.GroupVersion{}, w, req,
)
b.apiHandler.ServeHTTP(w, req)
return
}
if err != nil {
responsewriters.ErrorNegotiated(
apierrors.NewInternalError(err),
errorCodecs, schema.GroupVersion{}, w, req,
)
b.apiHandler.ServeHTTP(w, req)
return
}

Expand All @@ -175,7 +178,6 @@ func (b *podSubresourceProxyHandler) ServeHTTP(w http.ResponseWriter, req *http.
apierrors.NewBadRequest(fmt.Sprintf("pod %q is not upsynced", podName)),
errorCodecs, schema.GroupVersion{}, w, req,
)
b.apiHandler.ServeHTTP(w, req)
return
}

Expand All @@ -186,15 +188,13 @@ func (b *podSubresourceProxyHandler) ServeHTTP(w http.ResponseWriter, req *http.
apierrors.NewServiceUnavailable(fmt.Sprintf("subresource %q is not available right now for pod %q", subresource, podName)),
errorCodecs, schema.GroupVersion{}, w, req,
)
b.apiHandler.ServeHTTP(w, req)
return
}
if err != nil {
responsewriters.ErrorNegotiated(
apierrors.NewInternalError(err),
errorCodecs, schema.GroupVersion{}, w, req,
)
b.apiHandler.ServeHTTP(w, req)
return
}

Expand All @@ -216,7 +216,6 @@ func (b *podSubresourceProxyHandler) ServeHTTP(w http.ResponseWriter, req *http.
apierrors.NewInternalError(err),
errorCodecs, schema.GroupVersion{}, w, req,
)
b.apiHandler.ServeHTTP(w, req)
return
}

Expand All @@ -228,7 +227,6 @@ func (b *podSubresourceProxyHandler) ServeHTTP(w http.ResponseWriter, req *http.
apierrors.NewInternalError(err),
errorCodecs, schema.GroupVersion{}, w, req,
)
b.apiHandler.ServeHTTP(w, req)
return
}
// Set the URL path to the calculated
Expand Down

0 comments on commit 3a91224

Please sign in to comment.