Skip to content

Commit bf1b566

Browse files
authored
chore: switch to openapiv3 as data format and remove self contained tests (#88)
* chore: switch to openapiv3 as data format and remove slef contained tests * tests: small test fixture fix * tests: small test fixture fix * chore: upgrade to go 1.25 and remove some replacements * tests: introduce basic e2e test suite for standard k8s cluster * chore: init logger * chore: running the linter...
1 parent 118ffa7 commit bf1b566

File tree

88 files changed

+1285
-3327
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

88 files changed

+1285
-3327
lines changed

.golangci.yaml

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
version: "2"
2+
formatters:
3+
enable:
4+
- gci
5+
- gofmt
6+
settings:
7+
gci:
8+
sections:
9+
- standard
10+
- default
11+
- prefix(k8s.io,sigs.k8s.io)
12+
- prefix(sigs.k8s.io/multicluster-runtime)
13+
- prefix(github.com/kcp-dev)
14+
- prefix(github.com/kcp-dev/multicluster-provider)
15+
- blank
16+
- dot
17+
custom-order: true
18+
gofmt:
19+
rewrite-rules:
20+
- pattern: 'interface{}'
21+
replacement: 'any'
22+
exclusions:
23+
generated: lax
24+
paths:
25+
- zz_generated.*\.go$
26+
- .*conversion.*\.go$
27+
- third_party$
28+
- builtin$
29+
- examples$
30+
31+
linters:
32+
exclusions:
33+
paths:
34+
- watcher_test\.go$

Taskfile.yml

Lines changed: 7 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ vars:
77
ENVTEST_VERSION: "release-0.20" # https://github.com/kubernetes-sigs/controller-runtime/releases
88
MOCKERY_VERSION: v2.52.3 # https://github.com/vektra/mockery/releases
99
CONTROLLER_GEN_VERSION: v0.18.0 # https://github.com/kubernetes-sigs/controller-tools/releases
10+
GOLANGCI_LINT_VERSION: v2.6.0
1011
tasks:
1112
## Setup
1213
setup:mockery:
@@ -21,39 +22,21 @@ tasks:
2122
internal: true
2223
cmds:
2324
- test -s {{.LOCAL_BIN}}/controller-gen || GOBIN=$(pwd)/{{.LOCAL_BIN}} go install sigs.k8s.io/controller-tools/cmd/controller-gen@{{.CONTROLLER_GEN_VERSION}}
24-
update:crd:
25-
desc: "Download the latest CRDs"
26-
cmds:
27-
- mkdir -p tests/gateway_test/testdata/crd
28-
- curl -sSLo tests/gateway_test/testdata/crd/core.platform-mesh.io_accounts.yaml https://raw.githubusercontent.com/platform-mesh/account-operator/main/config/crd/core.platform-mesh.io_accounts.yaml
29-
- echo "CRD updated successfully."
3025
setup:golangci-lint:
3126
internal: true
3227
cmds:
33-
- test -s {{.LOCAL_BIN}}/golangci-lint || GOBIN=$(pwd)/{{.LOCAL_BIN}} go install github.com/golangci/golangci-lint/cmd/golangci-lint@latest
28+
- test -s {{.LOCAL_BIN}}/golangci-lint || GOBIN=$(pwd)/{{.LOCAL_BIN}} go install github.com/golangci/golangci-lint/v2/cmd/golangci-lint@{{.GOLANGCI_LINT_VERSION}}
3429
setup:go-test-coverage:
3530
internal: true
3631
cmds:
3732
- test -s {{.LOCAL_BIN}}/go-test-coverage || GOBIN=$(pwd)/{{.LOCAL_BIN}} go install github.com/vladopajic/go-test-coverage/v2@latest
3833

3934
## Code Generation
40-
generate:crd:
41-
desc: "Generate CRD manifests from Go types"
42-
deps: [setup:controller-gen]
43-
cmds:
44-
- "{{.LOCAL_BIN}}/controller-gen crd:crdVersions=v1 paths=./common/apis/v1alpha1 output:crd:artifacts:config=config/crd"
45-
- echo "CRD manifests generated successfully in config/crd/"
46-
generate:deepcopy:
47-
desc: "Generate deepcopy methods for API types"
48-
deps: [setup:controller-gen]
49-
cmds:
50-
- "{{.LOCAL_BIN}}/controller-gen object paths=./common/apis/v1alpha1"
51-
- echo "Deepcopy methods generated successfully"
5235
generate:
5336
desc: "Generate all CRD-related files (manifests + deepcopy methods)"
54-
deps: [generate:crd, generate:deepcopy]
37+
deps: [setup:controller-gen]
5538
cmds:
56-
- echo "All CRD generation completed successfully!"
39+
- "{{.LOCAL_BIN}}/controller-gen object crd:crdVersions=v1 paths=./common/apis/v1alpha1 output:crd:artifacts:config=config/crd"
5740

5841
## Development
5942
mockery:
@@ -66,14 +49,14 @@ tasks:
6649
## Testing
6750
fmt:
6851
cmds:
69-
- go fmt ./...
52+
- "{{.LOCAL_BIN}}/golangci-lint fmt ./..."
7053
lint:
7154
deps: [setup:golangci-lint]
7255
cmds:
7356
- task: fmt
7457
- "{{.LOCAL_BIN}}/golangci-lint run --timeout 10m ./..."
7558
envtest:
76-
deps: [mockery]
59+
deps: [setup:envtest]
7760
env:
7861
KUBEBUILDER_ASSETS:
7962
sh: $(pwd)/{{.LOCAL_BIN}}/setup-envtest use {{.ENVTEST_K8S_VERSION}} --bin-dir $(pwd)/{{.LOCAL_BIN}} -p path
@@ -86,7 +69,7 @@ tasks:
8669
vars:
8770
ADDITIONAL_COMMAND_ARGS: -coverprofile=./cover.out -covermode=atomic -coverpkg=./...
8871
cover:
89-
deps: [ setup:envtest, update:crd, setup:go-test-coverage ]
72+
deps: [setup:envtest, setup:go-test-coverage ]
9073
cmds:
9174
- task: envtest
9275
vars:

cmd/gateway.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,15 @@ import (
77
"net/http"
88
"time"
99

10-
openmfpcontext "github.com/platform-mesh/golang-commons/context"
10+
pmcontext "github.com/platform-mesh/golang-commons/context"
1111
"github.com/platform-mesh/golang-commons/logger"
1212
"github.com/platform-mesh/golang-commons/sentry"
1313
"github.com/platform-mesh/golang-commons/traces"
14+
"github.com/platform-mesh/kubernetes-graphql-gateway/gateway/manager"
1415
"github.com/prometheus/client_golang/prometheus/promhttp"
1516
"github.com/spf13/cobra"
16-
ctrl "sigs.k8s.io/controller-runtime"
1717

18-
"github.com/platform-mesh/kubernetes-graphql-gateway/gateway/manager"
18+
ctrl "sigs.k8s.io/controller-runtime"
1919
)
2020

2121
var gatewayCmd = &cobra.Command{
@@ -25,7 +25,7 @@ var gatewayCmd = &cobra.Command{
2525
Run: func(_ *cobra.Command, _ []string) {
2626
log.Info().Str("LogLevel", log.GetLevel().String()).Msg("Starting the Gateway...")
2727

28-
ctx, _, shutdown := openmfpcontext.StartContext(log, appCfg, 1*time.Second)
28+
ctx, _, shutdown := pmcontext.StartContext(log, appCfg, 1*time.Second)
2929
defer shutdown()
3030

3131
if err := initializeSentry(ctx, log); err != nil {
@@ -68,7 +68,7 @@ func initializeSentry(ctx context.Context, log *logger.Logger) error {
6868
log.Fatal().Err(err).Msg("Sentry init failed")
6969
}
7070

71-
defer openmfpcontext.Recover(log)
71+
defer pmcontext.Recover(log)
7272
return nil
7373
}
7474

cmd/listener.go

Lines changed: 19 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,14 @@ import (
44
"context"
55
"crypto/tls"
66

7-
kcpapis "github.com/kcp-dev/kcp/sdk/apis/apis/v1alpha1"
8-
kcpcore "github.com/kcp-dev/kcp/sdk/apis/core/v1alpha1"
9-
kcptenancy "github.com/kcp-dev/kcp/sdk/apis/tenancy/v1alpha1"
7+
gatewayv1alpha1 "github.com/platform-mesh/kubernetes-graphql-gateway/common/apis/v1alpha1"
8+
"github.com/platform-mesh/kubernetes-graphql-gateway/listener/pkg/apischema"
9+
"github.com/platform-mesh/kubernetes-graphql-gateway/listener/pkg/workspacefile"
10+
"github.com/platform-mesh/kubernetes-graphql-gateway/listener/reconciler"
11+
"github.com/platform-mesh/kubernetes-graphql-gateway/listener/reconciler/clusteraccess"
12+
"github.com/platform-mesh/kubernetes-graphql-gateway/listener/reconciler/kcp"
1013
"github.com/spf13/cobra"
14+
1115
apiextensionsv1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1"
1216
"k8s.io/apimachinery/pkg/runtime"
1317
utilruntime "k8s.io/apimachinery/pkg/util/runtime"
@@ -17,26 +21,23 @@ import (
1721
"sigs.k8s.io/controller-runtime/pkg/healthz"
1822
"sigs.k8s.io/controller-runtime/pkg/metrics/filters"
1923
metricsserver "sigs.k8s.io/controller-runtime/pkg/metrics/server"
20-
"sigs.k8s.io/controller-runtime/pkg/webhook"
21-
22-
gatewayv1alpha1 "github.com/platform-mesh/kubernetes-graphql-gateway/common/apis/v1alpha1"
23-
"github.com/platform-mesh/kubernetes-graphql-gateway/listener/pkg/apischema"
24-
"github.com/platform-mesh/kubernetes-graphql-gateway/listener/pkg/workspacefile"
25-
"github.com/platform-mesh/kubernetes-graphql-gateway/listener/reconciler"
26-
"github.com/platform-mesh/kubernetes-graphql-gateway/listener/reconciler/clusteraccess"
27-
"github.com/platform-mesh/kubernetes-graphql-gateway/listener/reconciler/kcp"
28-
)
2924

30-
var (
31-
scheme = runtime.NewScheme()
32-
webhookServer webhook.Server
33-
metricsServerOptions metricsserver.Options
25+
kcpapis "github.com/kcp-dev/kcp/sdk/apis/apis/v1alpha1"
26+
kcpcore "github.com/kcp-dev/kcp/sdk/apis/core/v1alpha1"
27+
kcptenancy "github.com/kcp-dev/kcp/sdk/apis/tenancy/v1alpha1"
3428
)
3529

3630
var listenCmd = &cobra.Command{
3731
Use: "listener",
3832
Example: "KUBECONFIG=<path to kubeconfig file> go run . listener",
39-
PreRun: func(cmd *cobra.Command, args []string) {
33+
Run: func(cmd *cobra.Command, args []string) {
34+
log.Info().Str("LogLevel", log.GetLevel().String()).Msg("Starting the Listener...")
35+
36+
ctx := ctrl.SetupSignalHandler()
37+
restCfg := ctrl.GetConfigOrDie()
38+
39+
scheme := runtime.NewScheme()
40+
4041
utilruntime.Must(clientgoscheme.AddToScheme(scheme))
4142

4243
if appCfg.EnableKcp {
@@ -60,11 +61,7 @@ var listenCmd = &cobra.Command{
6061
tlsOpts = []func(c *tls.Config){disableHTTP2}
6162
}
6263

63-
webhookServer = webhook.NewServer(webhook.Options{
64-
TLSOpts: tlsOpts,
65-
})
66-
67-
metricsServerOptions = metricsserver.Options{
64+
metricsServerOptions := metricsserver.Options{
6865
BindAddress: defaultCfg.Metrics.BindAddress,
6966
SecureServing: defaultCfg.Metrics.Secure,
7067
TLSOpts: tlsOpts,
@@ -73,17 +70,10 @@ var listenCmd = &cobra.Command{
7370
if defaultCfg.Metrics.Secure {
7471
metricsServerOptions.FilterProvider = filters.WithAuthenticationAndAuthorization
7572
}
76-
},
77-
Run: func(cmd *cobra.Command, args []string) {
78-
log.Info().Str("LogLevel", log.GetLevel().String()).Msg("Starting the Listener...")
79-
80-
ctx := ctrl.SetupSignalHandler()
81-
restCfg := ctrl.GetConfigOrDie()
8273

8374
mgrOpts := ctrl.Options{
8475
Scheme: scheme,
8576
Metrics: metricsServerOptions,
86-
WebhookServer: webhookServer,
8777
HealthProbeBindAddress: defaultCfg.HealthProbeBindAddress,
8878
LeaderElection: defaultCfg.LeaderElection.Enabled,
8979
LeaderElectionID: "72231e1f.platform-mesh.io",

cmd/root.go

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,16 @@
11
package cmd
22

33
import (
4-
"github.com/spf13/cobra"
5-
"github.com/spf13/viper"
6-
7-
openmfpconfig "github.com/platform-mesh/golang-commons/config"
4+
pmconfig "github.com/platform-mesh/golang-commons/config"
85
"github.com/platform-mesh/golang-commons/logger"
96
"github.com/platform-mesh/kubernetes-graphql-gateway/common/config"
7+
"github.com/spf13/cobra"
8+
"github.com/spf13/viper"
109
)
1110

1211
var (
1312
appCfg config.Config
14-
defaultCfg *openmfpconfig.CommonServiceConfig
13+
defaultCfg *pmconfig.CommonServiceConfig
1514
v *viper.Viper
1615
log *logger.Logger
1716
)
@@ -25,7 +24,7 @@ func init() {
2524
rootCmd.AddCommand(listenCmd)
2625

2726
var err error
28-
v, defaultCfg, err = openmfpconfig.NewDefaultConfig(rootCmd)
27+
v, defaultCfg, err = pmconfig.NewDefaultConfig(rootCmd)
2928
if err != nil {
3029
panic(err)
3130
}
@@ -40,12 +39,12 @@ func init() {
4039
}
4140
})
4241

43-
err = openmfpconfig.BindConfigToFlags(v, gatewayCmd, &appCfg)
42+
err = pmconfig.BindConfigToFlags(v, gatewayCmd, &appCfg)
4443
if err != nil {
4544
panic(err)
4645
}
4746

48-
err = openmfpconfig.BindConfigToFlags(v, listenCmd, &appCfg)
47+
err = pmconfig.BindConfigToFlags(v, listenCmd, &appCfg)
4948
if err != nil {
5049
panic(err)
5150
}

common/auth/config.go

Lines changed: 15 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -6,16 +6,15 @@ import (
66
"errors"
77
"fmt"
88

9+
gatewayv1alpha1 "github.com/platform-mesh/kubernetes-graphql-gateway/common/apis/v1alpha1"
10+
911
authv1 "k8s.io/api/authentication/v1"
1012
corev1 "k8s.io/api/core/v1"
11-
1213
"k8s.io/apimachinery/pkg/types"
1314
"k8s.io/client-go/rest"
1415
"k8s.io/client-go/tools/clientcmd"
1516
"k8s.io/client-go/tools/clientcmd/api"
1617
"sigs.k8s.io/controller-runtime/pkg/client"
17-
18-
gatewayv1alpha1 "github.com/platform-mesh/kubernetes-graphql-gateway/common/apis/v1alpha1"
1918
)
2019

2120
// BuildConfig creates a rest.Config from cluster connection parameters
@@ -39,8 +38,8 @@ func BuildConfig(ctx context.Context, host string, auth *gatewayv1alpha1.AuthCon
3938
return nil, errors.Join(errors.New("failed to extract CA data"), err)
4039
}
4140
if caData != nil {
42-
config.TLSClientConfig.CAData = caData
43-
config.TLSClientConfig.Insecure = false // Use proper TLS verification when CA is provided
41+
config.CAData = caData
42+
config.Insecure = false // Use proper TLS verification when CA is provided
4443
}
4544
}
4645

@@ -74,8 +73,8 @@ func BuildConfigFromMetadata(host string, authType, token, kubeconfig, certData,
7473
if err != nil {
7574
return nil, fmt.Errorf("failed to decode CA data: %w", err)
7675
}
77-
config.TLSClientConfig.CAData = decodedCA
78-
config.TLSClientConfig.Insecure = false
76+
config.CAData = decodedCA
77+
config.Insecure = false
7978
}
8079

8180
// Handle authentication based on type
@@ -109,8 +108,8 @@ func BuildConfigFromMetadata(host string, authType, token, kubeconfig, certData,
109108
if err != nil {
110109
return nil, fmt.Errorf("failed to decode key data: %w", err)
111110
}
112-
config.TLSClientConfig.CertData = decodedCert
113-
config.TLSClientConfig.KeyData = decodedKey
111+
config.CertData = decodedCert
112+
config.KeyData = decodedKey
114113
}
115114
}
116115

@@ -247,16 +246,16 @@ func ConfigureAuthentication(ctx context.Context, config *rest.Config, auth *gat
247246
return errors.New("client certificate or key not found in secret")
248247
}
249248

250-
config.TLSClientConfig.CertData = certData
251-
config.TLSClientConfig.KeyData = keyData
249+
config.CertData = certData
250+
config.KeyData = keyData
252251
return nil
253252
}
254253

255254
if auth.ServiceAccount != nil {
256255
var expirationSeconds int64
257256
if auth.ServiceAccount.TokenExpiration != nil {
258257
// If TokenExpiration is provided, use its value
259-
expirationSeconds = int64(auth.ServiceAccount.TokenExpiration.Duration.Seconds())
258+
expirationSeconds = int64(auth.ServiceAccount.TokenExpiration.Seconds())
260259
} else {
261260
// If TokenExpiration is nil, use the desired default (3600 seconds = 1 hour)
262261
expirationSeconds = 3600
@@ -346,14 +345,14 @@ func ExtractAuthFromKubeconfig(config *rest.Config, authInfo *api.AuthInfo) erro
346345
}
347346

348347
if len(authInfo.ClientCertificateData) > 0 && len(authInfo.ClientKeyData) > 0 {
349-
config.TLSClientConfig.CertData = authInfo.ClientCertificateData
350-
config.TLSClientConfig.KeyData = authInfo.ClientKeyData
348+
config.CertData = authInfo.ClientCertificateData
349+
config.KeyData = authInfo.ClientKeyData
351350
return nil
352351
}
353352

354353
if authInfo.ClientCertificate != "" && authInfo.ClientKey != "" {
355-
config.TLSClientConfig.CertFile = authInfo.ClientCertificate
356-
config.TLSClientConfig.KeyFile = authInfo.ClientKey
354+
config.CertFile = authInfo.ClientCertificate
355+
config.KeyFile = authInfo.ClientKey
357356
return nil
358357
}
359358

0 commit comments

Comments
 (0)