Skip to content

Commit

Permalink
refactor: update code
Browse files Browse the repository at this point in the history
  • Loading branch information
colin404 committed Aug 7, 2024
1 parent 618f80f commit 73a3c2a
Show file tree
Hide file tree
Showing 23 changed files with 6,731 additions and 156 deletions.
2 changes: 2 additions & 0 deletions cmd/onex-apiserver/apiserver.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ import (
"github.com/superproj/onex/pkg/apis/apps/v1beta1"
"github.com/superproj/onex/pkg/generated/clientset/versioned"
"github.com/superproj/onex/pkg/generated/informers"
generatedopenapi "github.com/superproj/onex/pkg/generated/openapi"
)

func main() {
Expand All @@ -45,6 +46,7 @@ func main() {
// Add custom admission plugins.
app.WithAdmissionPlugin(minerset.PluginName, minerset.Register),
// Add custom admission plugins initializer.
app.WithGetOpenAPIDefinitions(generatedopenapi.GetOpenAPIDefinitions),
app.WithAdmissionInitializers(func(c *genericapiserver.RecommendedConfig) ([]admission.PluginInitializer, error) {
client, err := versioned.NewForConfig(c.LoopbackClientConfig)
if err != nil {
Expand Down
2 changes: 1 addition & 1 deletion cmd/onex-apiserver/app/options/completion.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (
apiserveroptions "k8s.io/apiserver/pkg/server/options"

controlplaneoptions "github.com/superproj/onex/internal/controlplane/apiserver/options"
"github.com/superproj/onex/internal/controlplane/storage"
"github.com/superproj/onex/pkg/apiserver/storage"
)

// completedOptions is a private wrapper that enforces a call of Complete() before Run can be invoked.
Expand Down
4 changes: 3 additions & 1 deletion cmd/onex-apiserver/app/options/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,11 @@ import (

genericapiserver "k8s.io/apiserver/pkg/server"
cliflag "k8s.io/component-base/cli/flag"
"k8s.io/kube-openapi/pkg/common"

"github.com/superproj/onex/internal/controlplane"
controlplaneoptions "github.com/superproj/onex/internal/controlplane/apiserver/options"
"github.com/superproj/onex/internal/controlplane/storage"
"github.com/superproj/onex/pkg/apiserver/storage"
)

const defaultEtcdPathPrefix = "/registry/onex.io"
Expand All @@ -38,6 +39,7 @@ type Extra struct {
ExternalRESTStorageProviders []storage.RESTStorageProvider
ExternalVersionedInformers controlplane.ExternalSharedInformerFactory
ExternalPostStartHooks map[string]genericapiserver.PostStartHookFunc
GetOpenAPIDefinitions common.GetOpenAPIDefinitions
}

// NewServerRunOptions returns a new ServerRunOptions.
Expand Down
13 changes: 10 additions & 3 deletions cmd/onex-apiserver/app/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,14 +43,14 @@ import (
"k8s.io/klog/v2"
aggregatorapiserver "k8s.io/kube-aggregator/pkg/apiserver"
aggregatorscheme "k8s.io/kube-aggregator/pkg/apiserver/scheme"
"k8s.io/kube-openapi/pkg/common"
"k8s.io/kubernetes/pkg/api/legacyscheme"
"k8s.io/kubernetes/pkg/features"

"github.com/superproj/onex/cmd/onex-apiserver/app/options"
"github.com/superproj/onex/internal/controlplane"
controlplaneapiserver "github.com/superproj/onex/internal/controlplane/apiserver"
"github.com/superproj/onex/internal/controlplane/storage"
generatedopenapi "github.com/superproj/onex/pkg/generated/openapi"
"github.com/superproj/onex/pkg/apiserver/storage"
"github.com/superproj/onex/pkg/version"
)

Expand Down Expand Up @@ -92,6 +92,13 @@ func WithAdmissionPlugin(name string, registerFunc RegisterFunc) Option {
}
}

// WithOpenAPIDefinitions sets the OpenAPI definitions for the server.
func WithGetOpenAPIDefinitions(getOpenAPIDefinitions common.GetOpenAPIDefinitions) Option {
return func(s *options.ServerRunOptions) {
s.GetOpenAPIDefinitions = getOpenAPIDefinitions
}
}

// WithAdmissionInitializers returns an Option function that sets the external admission initializers in the ServerRunOptions.
func WithAdmissionInitializers(initializer func(c *genericapiserver.RecommendedConfig) ([]admission.PluginInitializer, error)) Option {
return func(s *options.ServerRunOptions) {
Expand Down Expand Up @@ -277,7 +284,7 @@ func CreateOneXAPIServerConfig(opts options.CompletedOptions) (
genericConfig, _, kubeSharedInformers, storageFactory, err := controlplaneapiserver.BuildGenericConfig(
opts.CompletedOptions,
[]*runtime.Scheme{legacyscheme.Scheme, extensionsapiserver.Scheme, aggregatorscheme.Scheme},
generatedopenapi.GetOpenAPIDefinitions,
opts.GetOpenAPIDefinitions,
)
if err != nil {
return nil, nil, err
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
// this file is https://github.com/superproj/onex.
//

package app
package main

// UPDATEME: When add new api group.
import (
Expand Down
6 changes: 1 addition & 5 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ require (
google.golang.org/grpc v1.58.3
google.golang.org/protobuf v1.34.2
gorm.io/driver/mysql v1.5.1-0.20230509030346-3715c134c25b
gorm.io/driver/postgres v1.4.5
gorm.io/gen v0.3.23
gorm.io/gorm v1.25.1-0.20230505075827-e61b98d69677
k8s.io/api v0.30.2
Expand Down Expand Up @@ -199,7 +200,6 @@ require (
golang.org/x/exp v0.0.0-20230905200255-921286631fa9 // indirect
gopkg.in/square/go-jose.v2 v2.6.0 // indirect
gorm.io/datatypes v1.1.1-0.20230130040222-c43177d3cf8c // indirect
gorm.io/driver/postgres v1.4.5 // indirect
gorm.io/driver/sqlserver v1.4.1 // indirect
gorm.io/hints v1.1.0 // indirect
gorm.io/plugin/dbresolver v1.3.0 // indirect
Expand Down Expand Up @@ -313,12 +313,8 @@ replace (
go.opentelemetry.io/otel/sdk => go.opentelemetry.io/otel/sdk v1.19.0
go.opentelemetry.io/otel/trace => go.opentelemetry.io/otel/trace v1.19.0
go.opentelemetry.io/proto/otlp => go.opentelemetry.io/proto/otlp v1.0.0
k8s.io/api => /home/colin/workspace/golang/src/k8s.io/api
k8s.io/apiextensions-apiserver => k8s.io/apiextensions-apiserver v0.30.2
k8s.io/apimachinery => /home/colin/workspace/golang/src/k8s.io/apimachinery
k8s.io/apiserver => k8s.io/apiserver v0.30.2
k8s.io/cli-runtime => k8s.io/cli-runtime v0.30.2
k8s.io/client-go => /home/colin/workspace/golang/src/k8s.io/client-go
k8s.io/cloud-provider => k8s.io/cloud-provider v0.30.2
k8s.io/cluster-bootstrap => k8s.io/cluster-bootstrap v0.30.2
k8s.io/code-generator => k8s.io/code-generator v0.30.2
Expand Down
6 changes: 6 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -2374,12 +2374,18 @@ honnef.co/go/tools v0.0.1-2019.2.3/go.mod h1:a3bituU0lyd329TUQxRnasdCoJDkEUEAqEt
honnef.co/go/tools v0.0.1-2020.1.3/go.mod h1:X/FiERA/W4tHapMX5mGpAtMSVEeEUOyHaw9vFzvIQ3k=
honnef.co/go/tools v0.0.1-2020.1.4/go.mod h1:X/FiERA/W4tHapMX5mGpAtMSVEeEUOyHaw9vFzvIQ3k=
honnef.co/go/tools v0.1.3/go.mod h1:NgwopIslSNH47DimFoV78dnkksY2EFtX0ajyb3K/las=
k8s.io/api v0.30.2 h1:+ZhRj+28QT4UOH+BKznu4CBgPWgkXO7XAvMcMl0qKvI=
k8s.io/api v0.30.2/go.mod h1:ULg5g9JvOev2dG0u2hig4Z7tQ2hHIuS+m8MNZ+X6EmI=
k8s.io/apiextensions-apiserver v0.30.2 h1:l7Eue2t6QiLHErfn2vwK4KgF4NeDgjQkCXtEbOocKIE=
k8s.io/apiextensions-apiserver v0.30.2/go.mod h1:lsJFLYyK40iguuinsb3nt+Sj6CmodSI4ACDLep1rgjw=
k8s.io/apimachinery v0.30.2 h1:fEMcnBj6qkzzPGSVsAZtQThU62SmQ4ZymlXRC5yFSCg=
k8s.io/apimachinery v0.30.2/go.mod h1:iexa2somDaxdnj7bha06bhb43Zpa6eWH8N8dbqVjTUc=
k8s.io/apiserver v0.30.2 h1:ACouHiYl1yFI2VFI3YGM+lvxgy6ir4yK2oLOsLI1/tw=
k8s.io/apiserver v0.30.2/go.mod h1:BOTdFBIch9Sv0ypSEcUR6ew/NUFGocRFNl72Ra7wTm8=
k8s.io/cli-runtime v0.30.2 h1:ooM40eEJusbgHNEqnHziN9ZpLN5U4WcQGsdLKVxpkKE=
k8s.io/cli-runtime v0.30.2/go.mod h1:Y4g/2XezFyTATQUbvV5WaChoUGhojv/jZAtdp5Zkm0A=
k8s.io/client-go v0.30.2 h1:sBIVJdojUNPDU/jObC+18tXWcTJVcwyqS9diGdWHk50=
k8s.io/client-go v0.30.2/go.mod h1:JglKSWULm9xlJLx4KCkfLLQ7XwtlbflV6uFFSHTMgVs=
k8s.io/cloud-provider v0.30.2 h1:yov6r02v7sMUNNvzEz51LtL2krn2c1wsC+dy/8BxKQI=
k8s.io/cloud-provider v0.30.2/go.mod h1:w69t2dSjDtI9BYK6SEqj6HmMKIojEk08fXRoUzjFN2I=
k8s.io/code-generator v0.30.2 h1:ZY1+aGkqZVwKIyGsOzquaeZ5rSfE6wZHur8z3jQAaiw=
Expand Down
4 changes: 2 additions & 2 deletions internal/apiserver/admission/plugin/minerset/admission.go
Original file line number Diff line number Diff line change
Expand Up @@ -140,9 +140,9 @@ func addMinerSetSelector(ms *apps.MinerSet) {
ms.Spec.Template.ObjectMeta.Labels[apps.LabelMinerSet] = ms.Name
}

// SetInternalInformerFactory gets Lister from SharedInformerFactory.
// SetExternalInformerFactory gets Lister from SharedInformerFactory.
// The lister knows how to lists MinerSets.
func (p *Plugin) SetInternalInformerFactory(f informers.SharedInformerFactory) {
func (p *Plugin) SetExternalInformerFactory(f informers.SharedInformerFactory) {
p.lister = f.Apps().V1beta1().MinerSets().Lister()
p.SetReadyFunc(f.Apps().V1beta1().MinerSets().Informer().HasSynced)
}
Expand Down
4 changes: 2 additions & 2 deletions internal/apiserver/registry/apps/rest/storage_apps.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,13 @@ import (
serverstorage "k8s.io/apiserver/pkg/server/storage"
"k8s.io/kubernetes/pkg/api/legacyscheme"

"github.com/superproj/onex/internal/controlplane/storage"
serializerutil "github.com/superproj/onex/internal/pkg/util/serializer"
chainstore "github.com/superproj/onex/internal/apiserver/registry/apps/chain/storage"
minerstore "github.com/superproj/onex/internal/apiserver/registry/apps/miner/storage"
minersetstore "github.com/superproj/onex/internal/apiserver/registry/apps/minerset/storage"
serializerutil "github.com/superproj/onex/internal/pkg/util/serializer"
"github.com/superproj/onex/pkg/apis/apps"
"github.com/superproj/onex/pkg/apis/apps/v1beta1"
"github.com/superproj/onex/pkg/apiserver/storage"
)

// RESTStorageProvider is a struct for apps REST storage.
Expand Down
2 changes: 1 addition & 1 deletion internal/controlplane/apiserver/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ import (
"github.com/superproj/onex/internal/controlplane"
"github.com/superproj/onex/internal/controlplane/admission/initializer"
controlplaneoptions "github.com/superproj/onex/internal/controlplane/apiserver/options"
"github.com/superproj/onex/internal/controlplane/storage"
"github.com/superproj/onex/pkg/apiserver/storage"
"github.com/superproj/onex/pkg/generated/clientset/versioned"
"github.com/superproj/onex/pkg/generated/informers"
"github.com/superproj/onex/pkg/version"
Expand Down
28 changes: 1 addition & 27 deletions internal/controlplane/instance.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
package controlplane

import (
"context"
"fmt"
"net/http"
"reflect"
Expand All @@ -16,7 +15,6 @@ import (
coordinationv1 "k8s.io/api/coordination/v1"
apiv1 "k8s.io/api/core/v1"
flowcontrolv1 "k8s.io/api/flowcontrol/v1"
apierrors "k8s.io/apimachinery/pkg/api/errors"
"k8s.io/apimachinery/pkg/runtime/schema"
apiserverfeatures "k8s.io/apiserver/pkg/features"
peerreconcilers "k8s.io/apiserver/pkg/reconcilers"
Expand All @@ -30,11 +28,10 @@ import (
"k8s.io/kubernetes/pkg/routes"

"github.com/superproj/onex/internal/controlplane/controller/systemnamespaces"
"github.com/superproj/onex/internal/controlplane/storage"
"github.com/superproj/onex/internal/pkg/config/minerprofile"
coordinationrest "github.com/superproj/onex/internal/registry/coordination/rest"
corerest "github.com/superproj/onex/internal/registry/core/rest"
flowcontrolrest "github.com/superproj/onex/internal/registry/flowcontrol/rest"
"github.com/superproj/onex/pkg/apiserver/storage"
"github.com/superproj/onex/pkg/generated/clientset/versioned"
"github.com/superproj/onex/pkg/generated/informers"
)
Expand Down Expand Up @@ -198,29 +195,6 @@ func (c completedConfig) New(delegationTarget genericapiserver.DelegationTarget)
},
)

m.GenericAPIServer.AddPostStartHookOrDie(
"initialize-instance-config-client",
func(ctx genericapiserver.PostStartHookContext) error {
client, err := versioned.NewForConfig(ctx.LoopbackClientConfig)
if err != nil {
return err
}

if err := minerprofile.Init(context.Background(), client); err != nil {
// When returning 'NotFound' error, we should not report an error, otherwise we can not
// create 'MinerTypesConfigMapName' configmap via onex-apiserver
if apierrors.IsNotFound(err) {
return nil
}

klog.ErrorS(err, "Failed to init miner type cache")
return err
}

return nil
},
)

if utilfeature.DefaultFeatureGate.Enabled(apiserverfeatures.APIServerIdentity) {
// put some post start hook here
// refer to: https://github.com/kubernetes/kubernetes/blob/v1.29.3/pkg/controlplane/instance.go#L515
Expand Down
1 change: 1 addition & 0 deletions internal/registry/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
registry to apiserver chassis
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ import (
"k8s.io/kubernetes/pkg/apis/coordination"
leasestore "k8s.io/kubernetes/pkg/registry/coordination/lease/storage"

"github.com/superproj/onex/internal/controlplane/storage"
serializerutil "github.com/superproj/onex/internal/pkg/util/serializer"
"github.com/superproj/onex/pkg/apiserver/storage"
// leasestore "github.com/superproj/onex/internal/registry/coordination/lease/storage".
)

Expand Down
3 changes: 2 additions & 1 deletion internal/toyblc/toyblc.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
"github.com/superproj/onex/internal/toyblc/miner"
"github.com/superproj/onex/internal/toyblc/ws"
"github.com/superproj/onex/pkg/log"
genericmw "github.com/superproj/onex/pkg/middleware/gin"
genericoptions "github.com/superproj/onex/pkg/options"
)

Expand Down Expand Up @@ -50,7 +51,7 @@ func (c completedConfig) New() (*ToyBLC, error) {
bs, ss := blc.NewBlockSet(c.Address), ws.NewSockets()

// gin.Recovery() 中间件,用来捕获任何 panic,并恢复
mws := []gin.HandlerFunc{gin.Recovery(), mw.NoCache, mw.Cors, mw.Secure, mw.TraceID()}
mws := []gin.HandlerFunc{gin.Recovery(), genericmw.NoCache, genericmw.Cors, genericmw.Secure, mw.TraceID()}

// 设置 Gin 模式
gin.SetMode(gin.ReleaseMode)
Expand Down
Loading

0 comments on commit 73a3c2a

Please sign in to comment.