Skip to content

Commit b6c91c0

Browse files
authored
Chore: harmonize listener and gateway (#118)
* one config to rule them all * single gitignore * single taskfile On-behalf-of: @SAP [email protected] Signed-off-by: Artem Shcherbatiuk <[email protected]>
1 parent 84a83a8 commit b6c91c0

File tree

16 files changed

+118
-282
lines changed

16 files changed

+118
-282
lines changed

.gitignore

Lines changed: 23 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,11 @@
1-
# If you prefer the allow list template instead of the deny list, see community template:
2-
# https://github.com/github/gitignore/blob/main/community/Golang/Go.AllowList.gitignore
3-
#
41
# Binaries for programs and plugins
52
*.exe
63
*.exe~
74
*.dll
85
*.so
96
*.dylib
10-
.vscode
11-
.idea
12-
.secret
13-
.env
7+
bin/*
8+
Dockerfile.cross
149
bin
1510

1611
# Test binary, built with `go test -c`
@@ -20,14 +15,29 @@ bin
2015
*.out
2116
coverage.html
2217

23-
# Dependency directories (remove the comment below to include it)
24-
# vendor/
18+
# Kubernetes Generated files - skip generated files, except for vendored files
19+
!vendor/**/zz_generated.*
20+
21+
# editor and IDE paraphernalia
22+
.idea
23+
.vscode
24+
*.swp
25+
*.swo
26+
*~
27+
28+
# MacOS
29+
**/.DS_Store
30+
31+
# KCP local files
32+
**/.kcp/
33+
.kcp
34+
35+
# Secrets and environment files
36+
.secret
37+
.env
2538

2639
# Go workspace file
2740
go.work
2841

2942
# binary files
30-
main
31-
32-
# kcp
33-
.kcp
43+
main

Dockerfile

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,10 @@ WORKDIR /app
33
COPY . .
44
RUN CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -ldflags '-w -s' main.go
55

6-
FROM alpine
76

7+
FROM scratch
88
WORKDIR /app
9-
10-
119
COPY --from=builder /app/main .
12-
1310
USER 1001:1001
14-
1511
ENTRYPOINT ["./main"]
16-
CMD ["gateway"]
12+
CMD ["listener"]

Taskfile.yml

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,9 @@ version: '3'
33
dotenv: ['.taskenv', '.secret/.env', '{{.HOME}}/.env' ]
44
vars:
55
LOCAL_BIN: bin
6-
ENVTEST_K8S_VERSION: "1.29.0"
7-
ENVTEST_VERSION: release-0.17
8-
MOCKERY_VERSION: v2.52.3
6+
ENVTEST_K8S_VERSION: "1.30.0" # to get latest version run $(pwd)/bin/setup-envtest list
7+
ENVTEST_VERSION: "release-0.20" # https://github.com/kubernetes-sigs/controller-runtime/releases
8+
MOCKERY_VERSION: v2.52.3 # https://github.com/vektra/mockery/releases
99
tasks:
1010
## Setup
1111
setup:mockery:
@@ -15,7 +15,7 @@ tasks:
1515
setup:envtest:
1616
internal: true
1717
cmds:
18-
- test -s {{.LOCAL_BIN}}/setup-envtest|| GOBIN=$(pwd)/{{.LOCAL_BIN}} go install sigs.k8s.io/controller-runtime/tools/setup-envtest@{{.ENVTEST_VERSION}}
18+
- test -s {{.LOCAL_BIN}}/setup-envtest || GOBIN=$(pwd)/{{.LOCAL_BIN}} go install sigs.k8s.io/controller-runtime/tools/setup-envtest@{{.ENVTEST_VERSION}}
1919
setup:golangci-lint:
2020
internal: true
2121
cmds:
@@ -30,9 +30,6 @@ tasks:
3030
deps: [ setup:mockery ]
3131
cmds:
3232
- "{{.LOCAL_BIN}}/mockery"
33-
build:
34-
cmds:
35-
- go build ./...
3633
docker:
3734
cmds:
3835
- docker build -t ghcr.io/openmfp/kubernetes-graphql-gateway .
@@ -59,11 +56,12 @@ tasks:
5956
vars:
6057
ADDITIONAL_COMMAND_ARGS: -coverprofile=./cover.out -covermode=atomic -coverpkg=./...
6158
cover:
62-
deps: [setup:envtest]
59+
deps: [setup:envtest, setup:go-test-coverage]
6360
cmds:
6461
- task: envtest
6562
vars:
6663
ADDITIONAL_COMMAND_ARGS: -coverprofile=./cover.out -covermode=atomic -coverpkg=./...
64+
- "{{.LOCAL_BIN}}/go-test-coverage --profile cover.out --config ./.testcoverage.yml"
6765
validate:
6866
cmds:
6967
- task: mockery

cmd/gateway.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ import (
1414

1515
"github.com/openmfp/golang-commons/logger"
1616

17-
appCfg "github.com/openmfp/kubernetes-graphql-gateway/gateway/config"
17+
appConfig "github.com/openmfp/kubernetes-graphql-gateway/common/config"
1818
"github.com/openmfp/kubernetes-graphql-gateway/gateway/manager"
1919
)
2020

@@ -25,7 +25,7 @@ var gatewayCmd = &cobra.Command{
2525
RunE: func(cmd *cobra.Command, args []string) error {
2626
start := time.Now()
2727

28-
appCfg, err := appCfg.NewFromEnv()
28+
appCfg, err := appConfig.NewFromEnv()
2929
if err != nil {
3030
log.Fatal().Err(err).Msg("Error getting app restCfg, exiting")
3131
}

cmd/listener.go

Lines changed: 12 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,8 @@ import (
2222
metricsserver "sigs.k8s.io/controller-runtime/pkg/metrics/server"
2323
"sigs.k8s.io/controller-runtime/pkg/webhook"
2424

25-
"github.com/openmfp/kubernetes-graphql-gateway/listener/flags"
25+
"github.com/openmfp/kubernetes-graphql-gateway/common/config"
2626
"github.com/openmfp/kubernetes-graphql-gateway/listener/kcp"
27-
// +kubebuilder:scaffold:imports
2827
)
2928

3029
func init() {
@@ -36,7 +35,7 @@ var (
3635
setupLog = ctrl.Log.WithName("setup")
3736
webhookServer webhook.Server
3837
metricsServerOptions metricsserver.Options
39-
opFlags *flags.Flags
38+
appCfg *config.Config
4039
)
4140

4241
var listenCmd = &cobra.Command{
@@ -49,15 +48,14 @@ var listenCmd = &cobra.Command{
4948
utilruntime.Must(kcpcore.AddToScheme(scheme))
5049
utilruntime.Must(kcptenancy.AddToScheme(scheme))
5150
utilruntime.Must(apiextensionsv1.AddToScheme(scheme))
52-
// +kubebuilder:scaffold:scheme
5351

5452
opts := zap.Options{
5553
Development: true,
5654
}
5755
ctrl.SetLogger(zap.New(zap.UseFlagOptions(&opts)))
5856

5957
var err error
60-
opFlags, err = flags.NewFromEnv()
58+
appCfg, err = config.NewFromEnv()
6159
if err != nil {
6260
setupLog.Error(err, "failed to get operator flags from env, exiting...")
6361
os.Exit(1)
@@ -69,7 +67,7 @@ var listenCmd = &cobra.Command{
6967
}
7068

7169
var tlsOpts []func(*tls.Config)
72-
if !opFlags.EnableHTTP2 {
70+
if !appCfg.EnableHTTP2 {
7371
tlsOpts = []func(c *tls.Config){disableHTTP2}
7472
}
7573

@@ -78,12 +76,12 @@ var listenCmd = &cobra.Command{
7876
})
7977

8078
metricsServerOptions = metricsserver.Options{
81-
BindAddress: opFlags.MetricsAddr,
82-
SecureServing: opFlags.SecureMetrics,
79+
BindAddress: appCfg.MetricsAddr,
80+
SecureServing: appCfg.SecureMetrics,
8381
TLSOpts: tlsOpts,
8482
}
8583

86-
if opFlags.SecureMetrics {
84+
if appCfg.SecureMetrics {
8785
metricsServerOptions.FilterProvider = filters.WithAuthenticationAndAuthorization
8886
}
8987
},
@@ -94,12 +92,12 @@ var listenCmd = &cobra.Command{
9492
Scheme: scheme,
9593
Metrics: metricsServerOptions,
9694
WebhookServer: webhookServer,
97-
HealthProbeBindAddress: opFlags.ProbeAddr,
98-
LeaderElection: opFlags.EnableLeaderElection,
95+
HealthProbeBindAddress: appCfg.ProbeAddr,
96+
LeaderElection: appCfg.EnableLeaderElection,
9997
LeaderElectionID: "72231e1f.openmfp.io",
10098
}
10199

102-
newMgrFunc := kcp.ManagerFactory(opFlags)
100+
newMgrFunc := kcp.ManagerFactory(appCfg)
103101

104102
mgr, err := newMgrFunc(cfg, mgrOpts)
105103
if err != nil {
@@ -110,10 +108,10 @@ var listenCmd = &cobra.Command{
110108
reconcilerOpts := kcp.ReconcilerOpts{
111109
Scheme: scheme,
112110
Config: cfg,
113-
OpenAPIDefinitionsPath: opFlags.OpenApiDefinitionsPath,
111+
OpenAPIDefinitionsPath: appCfg.OpenApiDefinitionsPath,
114112
}
115113

116-
newReconcilerFunc := kcp.ReconcilerFactory(opFlags)
114+
newReconcilerFunc := kcp.ReconcilerFactory(appCfg)
117115

118116
reconciler, err := newReconcilerFunc(reconcilerOpts)
119117
if err != nil {
@@ -125,7 +123,6 @@ var listenCmd = &cobra.Command{
125123
setupLog.Error(err, "unable to create controller")
126124
os.Exit(1)
127125
}
128-
// +kubebuilder:scaffold:builder
129126

130127
if err := mgr.AddHealthzCheck("healthz", healthz.Ping); err != nil {
131128
setupLog.Error(err, "unable to set up health check")

common/config/config.go

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
package config
2+
3+
import (
4+
"github.com/vrischmann/envconfig"
5+
)
6+
7+
type Config struct {
8+
Listener
9+
Gateway
10+
11+
OpenApiDefinitionsPath string `envconfig:"default=./bin/definitions"`
12+
EnableKcp bool `envconfig:"default=true,optional"`
13+
LocalDevelopment bool `envconfig:"default=false,optional"`
14+
}
15+
16+
type Gateway struct {
17+
Port string `envconfig:"default=8080,optional"`
18+
LogLevel string `envconfig:"default=INFO,optional"`
19+
UserNameClaim string `envconfig:"default=email,optional"`
20+
ShouldImpersonate bool `envconfig:"default=true,optional"`
21+
22+
HandlerCfg struct {
23+
Pretty bool `envconfig:"default=true,optional"`
24+
Playground bool `envconfig:"default=true,optional"`
25+
GraphiQL bool `envconfig:"default=true,optional"`
26+
}
27+
28+
Cors struct {
29+
Enabled bool `envconfig:"default=false,optional"`
30+
AllowedOrigins []string `envconfig:"default=*,optional"`
31+
AllowedHeaders []string `envconfig:"default=*,optional"`
32+
}
33+
}
34+
35+
type Listener struct {
36+
MetricsAddr string `envconfig:"default=0,optional"`
37+
EnableLeaderElection bool `envconfig:"default=false,optional"`
38+
ProbeAddr string `envconfig:"default=:8081,optional"`
39+
SecureMetrics bool `envconfig:"default=true,optional"`
40+
EnableHTTP2 bool `envconfig:"default=false,optional"`
41+
}
42+
43+
// NewFromEnv creates a Gateway from environment values
44+
func NewFromEnv() (*Config, error) {
45+
cfg := &Config{}
46+
err := envconfig.Init(cfg)
47+
return cfg, err
48+
}

gateway/config/config.go

Lines changed: 0 additions & 39 deletions
This file was deleted.

gateway/manager/manager.go

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,14 +16,16 @@ import (
1616
"github.com/graphql-go/graphql"
1717
"github.com/graphql-go/handler"
1818
"github.com/kcp-dev/logicalcluster/v3"
19-
"github.com/openmfp/golang-commons/logger"
20-
appConfig "github.com/openmfp/kubernetes-graphql-gateway/gateway/config"
21-
"github.com/openmfp/kubernetes-graphql-gateway/gateway/resolver"
22-
"github.com/openmfp/kubernetes-graphql-gateway/gateway/schema"
2319
"k8s.io/client-go/rest"
2420
"sigs.k8s.io/controller-runtime/pkg/client"
2521
"sigs.k8s.io/controller-runtime/pkg/kcp"
2622
"sigs.k8s.io/controller-runtime/pkg/kontext"
23+
24+
"github.com/openmfp/golang-commons/logger"
25+
26+
appConfig "github.com/openmfp/kubernetes-graphql-gateway/common/config"
27+
"github.com/openmfp/kubernetes-graphql-gateway/gateway/resolver"
28+
"github.com/openmfp/kubernetes-graphql-gateway/gateway/schema"
2729
)
2830

2931
type Provider interface {
@@ -37,7 +39,7 @@ type FileWatcher interface {
3739
}
3840

3941
type Service struct {
40-
appCfg appConfig.Config
42+
appCfg *appConfig.Config
4143
handlers map[string]*graphqlHandler
4244
log *logger.Logger
4345
mu sync.RWMutex
@@ -51,7 +53,7 @@ type graphqlHandler struct {
5153
handler http.Handler
5254
}
5355

54-
func NewManager(log *logger.Logger, cfg *rest.Config, appCfg appConfig.Config) (*Service, error) {
56+
func NewManager(log *logger.Logger, cfg *rest.Config, appCfg *appConfig.Config) (*Service, error) {
5557
watcher, err := fsnotify.NewWatcher()
5658
if err != nil {
5759
return nil, err

listener/.gitignore

Lines changed: 0 additions & 30 deletions
This file was deleted.

0 commit comments

Comments
 (0)