Skip to content

Commit 39bac9a

Browse files
authored
refactor: extract core pkg (#100)
## What type of PR is this? /kind refactor ## What this PR does / why we need it: Extract the `core` pkg: * `pkg/core/server` -> starter for CoreServer * `pkg/core/manager` -> domain layer for CoreServer * `pkg/core/handler` -> web layer for CoreServer
1 parent d975fd7 commit 39bac9a

File tree

13 files changed

+42
-29
lines changed

13 files changed

+42
-29
lines changed

.github/CODEOWNERS

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
# @global-owner1 and @global-owner2 will be requested for
77
# review when someone opens a pull request.
88
# * @global-owner1 @global-owner2
9-
* @elliotxx @panshuai-ps @adohe
9+
* @elliotxx @panshuai-ps @adohe @ffforest
1010

1111
# Order is important; the last matching pattern takes the most
1212
# precedence. When someone opens a pull request that only
@@ -24,13 +24,13 @@
2424
# explicit write access to the repository. In this example,
2525
# the octocats team in the octo-org organization owns all .txt files.
2626
# *.txt @octo-org/octocats
27-
*.go @elliotxx @panshuai-ps @adohe
27+
*.go @elliotxx @panshuai-ps @adohe @ffforest
2828

2929
# In this example, @doctocat owns any files in the build/logs
3030
# directory at the root of the repository and any of its
3131
# subdirectories.
3232
# /build/logs/ @doctocat
33-
/.github/ @elliotxx @panshuai-ps @adohe
33+
/.github/ @elliotxx @panshuai-ps @adohe @ffforest
3434

3535
# The `docs/*` pattern will match files like
3636
# `docs/getting-started.md` but not further nested files like
@@ -45,7 +45,7 @@
4545
# directory in the root of your repository and any of its
4646
# subdirectories.
4747
# /docs/ @doctocat
48-
/docs/ @elliotxx @panshuai-ps @adohe
48+
/docs/ @elliotxx @panshuai-ps @adohe @ffforest
4949

5050
# In this example, @octocat owns any file in the `/apps`
5151
# directory in the root of your repository except for the `/apps/github`

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ require (
2727
k8s.io/component-base v0.26.0
2828
k8s.io/klog/v2 v2.80.1
2929
k8s.io/kube-openapi v0.0.0-20230106171958-10e5f0effbd2
30+
k8s.io/metrics v0.26.0
3031
k8s.io/utils v0.0.0-20221107191617-1a15be271d1d
3132
sigs.k8s.io/structured-merge-diff/v4 v4.2.3
3233
sigs.k8s.io/yaml v1.3.0
@@ -124,7 +125,6 @@ require (
124125
k8s.io/apiextensions-apiserver v0.23.5 // indirect
125126
k8s.io/gengo v0.0.0-20220902162205-c0856e24416d // indirect
126127
k8s.io/kms v0.26.0 // indirect
127-
k8s.io/metrics v0.26.0 // indirect
128128
sigs.k8s.io/apiserver-network-proxy/konnectivity-client v0.0.35 // indirect
129129
sigs.k8s.io/json v0.0.0-20220713155537-f223a00ba0e2 // indirect
130130
)

pkg/apiserver/apiserver.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import (
2020
"fmt"
2121
"net/http"
2222

23+
"github.com/KusionStack/karbour/pkg/core/server"
2324
"github.com/KusionStack/karbour/pkg/registry"
2425
clusterstorage "github.com/KusionStack/karbour/pkg/registry/cluster"
2526
searchstorage "github.com/KusionStack/karbour/pkg/registry/search"
@@ -82,7 +83,7 @@ func (s *KarbourServer) InstallCoreServer(c *CompletedConfig) *KarbourServer {
8283
}
8384

8485
// Create the core server.
85-
s.mux = NewCoreServer(c)
86+
s.mux = server.NewCoreServer(&c.GenericConfig, c.ExtraConfig)
8687

8788
// Mount the core mux to NonGoRestfulMux of GenericAPIServer.
8889
s.GenericAPIServer.Handler.NonGoRestfulMux.HandlePrefix("/", s.mux)

pkg/handler/cluster/cluster.go renamed to pkg/core/handler/cluster/cluster.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ import (
1818
"encoding/json"
1919
"net/http"
2020

21-
"github.com/KusionStack/karbour/pkg/manager/cluster"
21+
"github.com/KusionStack/karbour/pkg/core/manager/cluster"
2222
"github.com/KusionStack/karbour/pkg/multicluster"
2323
"github.com/go-chi/chi/v5"
2424
"k8s.io/apiserver/pkg/server"

pkg/handler/config/config.go renamed to pkg/core/handler/config/config.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ import (
1818
"encoding/json"
1919
"net/http"
2020

21-
"github.com/KusionStack/karbour/pkg/manager/config"
21+
"github.com/KusionStack/karbour/pkg/core/manager/config"
2222
"github.com/KusionStack/karbour/pkg/util/ctxutil"
2323
)
2424

pkg/handler/resource/resource.go renamed to pkg/core/handler/resource/resource.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ import (
1919
"net/http"
2020

2121
"github.com/KusionStack/karbour/pkg/apis/search"
22-
"github.com/KusionStack/karbour/pkg/manager/resource"
22+
"github.com/KusionStack/karbour/pkg/core/manager/resource"
2323
"github.com/KusionStack/karbour/pkg/multicluster"
2424
"github.com/KusionStack/karbour/pkg/registry"
2525
searchstorage "github.com/KusionStack/karbour/pkg/registry/search"
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.

pkg/apiserver/router.go renamed to pkg/core/server/server.go

Lines changed: 32 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -12,25 +12,30 @@
1212
// See the License for the specific language governing permissions and
1313
// limitations under the License.
1414

15-
package apiserver
15+
package server
1616

1717
import (
1818
"fmt"
1919
"net/http"
2020
"strings"
2121

22-
clusterhandler "github.com/KusionStack/karbour/pkg/handler/cluster"
23-
confighandler "github.com/KusionStack/karbour/pkg/handler/config"
24-
resourcehandler "github.com/KusionStack/karbour/pkg/handler/resource"
25-
clustermanager "github.com/KusionStack/karbour/pkg/manager/cluster"
26-
"github.com/KusionStack/karbour/pkg/manager/config"
27-
resourcemanager "github.com/KusionStack/karbour/pkg/manager/resource"
22+
clusterhandler "github.com/KusionStack/karbour/pkg/core/handler/cluster"
23+
confighandler "github.com/KusionStack/karbour/pkg/core/handler/config"
24+
resourcehandler "github.com/KusionStack/karbour/pkg/core/handler/resource"
25+
clustermanager "github.com/KusionStack/karbour/pkg/core/manager/cluster"
26+
"github.com/KusionStack/karbour/pkg/core/manager/config"
27+
resourcemanager "github.com/KusionStack/karbour/pkg/core/manager/resource"
2828
appmiddleware "github.com/KusionStack/karbour/pkg/middleware"
29+
"github.com/KusionStack/karbour/pkg/registry"
2930
"github.com/go-chi/chi/v5"
3031
"github.com/go-chi/chi/v5/middleware"
32+
genericapiserver "k8s.io/apiserver/pkg/server"
3133
)
3234

33-
func NewCoreServer(c *CompletedConfig) *chi.Mux {
35+
func NewCoreServer(
36+
genericConfig *genericapiserver.CompletedConfig,
37+
extraConfig *registry.ExtraConfig,
38+
) *chi.Mux {
3439
router := chi.NewRouter()
3540

3641
// Set up middlewares
@@ -51,7 +56,7 @@ func NewCoreServer(c *CompletedConfig) *chi.Mux {
5156
})
5257

5358
router.Route("/api/v1", func(r chi.Router) {
54-
setupAPIV1(r, configMgr, clusterMgr, resourceMgr, c)
59+
setupAPIV1(r, configMgr, clusterMgr, resourceMgr, genericConfig, extraConfig)
5560
})
5661

5762
router.Get("/endpoints", func(w http.ResponseWriter, req *http.Request) {
@@ -63,7 +68,14 @@ func NewCoreServer(c *CompletedConfig) *chi.Mux {
6368
return router
6469
}
6570

66-
func setupAPIV1(r chi.Router, configMgr *config.Manager, clusterMgr *clustermanager.ClusterManager, resourceMgr *resourcemanager.ResourceManager, c *CompletedConfig) {
71+
func setupAPIV1(
72+
r chi.Router,
73+
configMgr *config.Manager,
74+
clusterMgr *clustermanager.ClusterManager,
75+
resourceMgr *resourcemanager.ResourceManager,
76+
genericConfig *genericapiserver.CompletedConfig,
77+
extraConfig *registry.ExtraConfig,
78+
) {
6779
r.Route("/config", func(r chi.Router) {
6880
r.Get("/", confighandler.Get(configMgr))
6981
// r.Delete("/", confighandler.Delete(configMgr))
@@ -73,23 +85,23 @@ func setupAPIV1(r chi.Router, configMgr *config.Manager, clusterMgr *clustermana
7385

7486
r.Route("/cluster", func(r chi.Router) {
7587
r.Route("/{clusterName}", func(r chi.Router) {
76-
r.Get("/", clusterhandler.Get(clusterMgr, &c.GenericConfig))
77-
r.Get("/yaml", clusterhandler.GetYAML(clusterMgr, &c.GenericConfig))
78-
r.Get("/detail", clusterhandler.GetDetail(clusterMgr, &c.GenericConfig))
79-
r.Get("/topology", clusterhandler.GetTopology(clusterMgr, &c.GenericConfig))
80-
r.Get("/namespace/{namespaceName}", clusterhandler.GetNamespace(clusterMgr, &c.GenericConfig))
81-
r.Get("/namespace/{namespaceName}/topology", clusterhandler.GetNamespaceTopology(clusterMgr, &c.GenericConfig))
88+
r.Get("/", clusterhandler.Get(clusterMgr, genericConfig))
89+
r.Get("/yaml", clusterhandler.GetYAML(clusterMgr, genericConfig))
90+
r.Get("/detail", clusterhandler.GetDetail(clusterMgr, genericConfig))
91+
r.Get("/topology", clusterhandler.GetTopology(clusterMgr, genericConfig))
92+
r.Get("/namespace/{namespaceName}", clusterhandler.GetNamespace(clusterMgr, genericConfig))
93+
r.Get("/namespace/{namespaceName}/topology", clusterhandler.GetNamespaceTopology(clusterMgr, genericConfig))
8294
})
8395
})
8496

8597
r.Route("/resource", func(r chi.Router) {
8698
r.Route("/search", func(r chi.Router) {
87-
r.Get("/", resourcehandler.SearchForResource(resourceMgr, c.ExtraConfig))
99+
r.Get("/", resourcehandler.SearchForResource(resourceMgr, extraConfig))
88100
})
89101
r.Route("/cluster/{clusterName}/{apiVersion}/namespace/{namespaceName}/{kind}/name/{resourceName}", func(r chi.Router) {
90-
r.Get("/", resourcehandler.Get(resourceMgr, &c.GenericConfig))
91-
r.Get("/yaml", resourcehandler.GetYAML(resourceMgr, &c.GenericConfig))
92-
r.Get("/topology", resourcehandler.GetTopology(resourceMgr, &c.GenericConfig))
102+
r.Get("/", resourcehandler.Get(resourceMgr, genericConfig))
103+
r.Get("/yaml", resourcehandler.GetYAML(resourceMgr, genericConfig))
104+
r.Get("/topology", resourcehandler.GetTopology(resourceMgr, genericConfig))
93105
})
94106
})
95107
}

0 commit comments

Comments
 (0)