Skip to content

Commit 3ee69e6

Browse files
authored
feat: integrate KCP support in manager and resolver components (#34)
* feat: integrate KCP support in manager and resolver components * fix: failing tests * fix: failing tests * chore: testcoverage
1 parent 89d77c7 commit 3ee69e6

File tree

4 files changed

+29
-9
lines changed

4 files changed

+29
-9
lines changed

Taskfile.yml

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -50,11 +50,11 @@ tasks:
5050
vars:
5151
ADDITIONAL_COMMAND_ARGS: -coverprofile=./cover.out -covermode=atomic -coverpkg=./...
5252
cover:
53-
deps:
54-
- task: setup:go-test-coverage
55-
- task: test
53+
deps: [setup:envtest]
5654
cmds:
57-
- "{{.LOCAL_BIN}}/go-test-coverage --threshold-file 50 --threshold-package 51 --threshold-total 56 --profile cover.out --config ./.testcoverage.yml"
55+
- task: envtest
56+
vars:
57+
ADDITIONAL_COMMAND_ARGS: -coverprofile=./cover.out -covermode=atomic -coverpkg=./...
5858
validate:
5959
cmds:
6060
- task: lint

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ require (
1212
github.com/graphql-go/graphql v0.8.1
1313
github.com/graphql-go/handler v0.2.4
1414
github.com/kcp-dev/kcp/sdk v0.26.1
15+
github.com/kcp-dev/logicalcluster/v3 v3.0.5
1516
github.com/mitchellh/mapstructure v1.5.0
1617
github.com/openmfp/golang-commons v0.124.0
1718
github.com/rs/zerolog v1.33.0
@@ -62,7 +63,6 @@ require (
6263
github.com/josharian/intern v1.0.0 // indirect
6364
github.com/json-iterator/go v1.1.12 // indirect
6465
github.com/kcp-dev/apimachinery/v2 v2.0.1-0.20240817110845-a9eb9752bfeb // indirect
65-
github.com/kcp-dev/logicalcluster/v3 v3.0.5 // indirect
6666
github.com/klauspost/compress v1.17.9 // indirect
6767
github.com/mailru/easyjson v0.7.7 // indirect
6868
github.com/mattn/go-colorable v0.1.13 // indirect

internal/manager/manager.go

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,14 +15,18 @@ import (
1515
"github.com/go-openapi/spec"
1616
"github.com/graphql-go/graphql"
1717
"github.com/graphql-go/handler"
18+
"github.com/kcp-dev/logicalcluster/v3"
1819
appConfig "github.com/openmfp/crd-gql-gateway/internal/config"
1920
"github.com/openmfp/crd-gql-gateway/internal/gateway"
2021
"github.com/openmfp/crd-gql-gateway/internal/resolver"
2122
"github.com/openmfp/golang-commons/logger"
23+
"k8s.io/apimachinery/pkg/api/meta"
2224
"k8s.io/client-go/kubernetes/scheme"
2325
"k8s.io/client-go/rest"
2426
"sigs.k8s.io/controller-runtime/pkg/cache"
2527
"sigs.k8s.io/controller-runtime/pkg/client"
28+
"sigs.k8s.io/controller-runtime/pkg/kcp"
29+
"sigs.k8s.io/controller-runtime/pkg/kontext"
2630
)
2731

2832
type Provider interface {
@@ -200,6 +204,7 @@ func (s *Service) ServeHTTP(w http.ResponseWriter, r *http.Request) {
200204
}
201205

202206
r = r.WithContext(context.WithValue(r.Context(), resolver.RuntimeClientKey{}, runtimeClient))
207+
r = r.WithContext(kontext.WithCluster(r.Context(), logicalcluster.Name(workspace)))
203208

204209
switch endpoint {
205210
case "graphql":
@@ -337,13 +342,27 @@ func setupK8sClients(ctx context.Context, cfg *rest.Config) (client.WithWatch, e
337342
return nil, fmt.Errorf("failed to sync cache")
338343
}
339344

340-
runtimeClient, err := client.NewWithWatch(cfg, client.Options{
345+
opts := client.Options{
341346
Scheme: scheme.Scheme,
342347
Cache: &client.CacheOptions{
343348
Reader: k8sCache,
344349
Unstructured: true,
345350
},
346-
})
351+
}
352+
353+
if cluster, ok := kontext.ClusterFrom(ctx); ok && !cluster.Empty() {
354+
httpClient, err := kcp.NewClusterAwareHTTPClient(cfg)
355+
if err != nil {
356+
return nil, err
357+
}
358+
359+
opts.HTTPClient = httpClient
360+
opts.MapperWithContext = func(ctx context.Context) (meta.RESTMapper, error) {
361+
return kcp.NewClusterAwareMapperProvider(cfg, httpClient)(ctx)
362+
}
363+
}
364+
365+
runtimeClient, err := client.NewWithWatch(cfg, opts)
347366

348367
return runtimeClient, err
349368
}

internal/resolver/subscription.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,13 @@ package resolver
33
import (
44
"context"
55
"errors"
6-
"github.com/graphql-go/graphql/language/ast"
7-
"k8s.io/apimachinery/pkg/watch"
86
"reflect"
97
"sort"
108
"strings"
119

10+
"github.com/graphql-go/graphql/language/ast"
11+
"k8s.io/apimachinery/pkg/watch"
12+
1213
"github.com/graphql-go/graphql"
1314
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
1415
"k8s.io/apimachinery/pkg/labels"

0 commit comments

Comments
 (0)