1
0
mirror of https://github.com/rancher/steve.git synced 2025-04-28 19:24:42 +00:00
steve/pkg/server/server.go

98 lines
2.3 KiB
Go
Raw Normal View History

2019-08-04 17:41:32 +00:00
package server
import (
"context"
"net/http"
2019-08-13 23:36:03 +00:00
"github.com/rancher/naok/pkg/resources"
"github.com/rancher/naok/pkg/controllers/schema"
2019-08-04 17:41:32 +00:00
"github.com/rancher/naok/pkg/accesscontrol"
"github.com/rancher/naok/pkg/client"
"github.com/rancher/wrangler-api/pkg/generated/controllers/apiextensions.k8s.io"
"github.com/rancher/wrangler-api/pkg/generated/controllers/apiregistration.k8s.io"
rbaccontroller "github.com/rancher/wrangler-api/pkg/generated/controllers/rbac"
"github.com/rancher/wrangler/pkg/kubeconfig"
"github.com/rancher/wrangler/pkg/start"
"github.com/sirupsen/logrus"
"k8s.io/client-go/kubernetes"
"k8s.io/client-go/rest"
)
type Config struct {
2019-08-08 05:42:15 +00:00
Kubeconfig string
Namespace string
ListenAddress string
2019-08-04 17:41:32 +00:00
}
func Run(ctx context.Context, cfg Config) error {
restConfig, err := kubeconfig.GetNonInteractiveClientConfig(cfg.Kubeconfig).ClientConfig()
if err != nil {
return err
}
rbac, err := rbaccontroller.NewFactoryFromConfig(restConfig)
if err != nil {
return err
}
k8s, err := kubernetes.NewForConfig(restConfig)
if err != nil {
return err
}
api, err := apiregistration.NewFactoryFromConfig(restConfig)
if err != nil {
return err
}
crd, err := apiextensions.NewFactoryFromConfig(restConfig)
if err != nil {
return err
}
2019-08-08 05:42:15 +00:00
starter, err := startAPI(ctx, cfg.ListenAddress, restConfig, k8s, crd, api, rbac)
2019-08-04 17:41:32 +00:00
if err != nil {
return err
}
if err := start.All(ctx, 5, api, crd, rbac); err != nil {
return err
}
if err := starter(); err != nil {
return err
}
<-ctx.Done()
return nil
}
2019-08-08 05:42:15 +00:00
func startAPI(ctx context.Context, listenAddress string, restConfig *rest.Config, k8s *kubernetes.Clientset, crd *apiextensions.Factory,
2019-08-04 17:41:32 +00:00
api *apiregistration.Factory, rbac *rbaccontroller.Factory) (func() error, error) {
cf, err := client.NewFactory(restConfig)
if err != nil {
return nil, err
}
2019-08-13 23:36:03 +00:00
as := accesscontrol.NewAccessStore(rbac.Rbac().V1())
sf := resources.SchemaFactory(cf, as)
schema.Register(ctx,
2019-08-04 17:41:32 +00:00
k8s.Discovery(),
crd.Apiextensions().V1beta1().CustomResourceDefinition(),
api.Apiregistration().V1().APIService(),
2019-08-13 23:36:03 +00:00
sf)
2019-08-04 17:41:32 +00:00
return func() error {
2019-08-13 23:36:03 +00:00
handler, err := newAPIServer(restConfig, sf)
2019-08-08 05:42:15 +00:00
if err != nil {
return err
}
logrus.Infof("listening on %s", listenAddress)
return http.ListenAndServe(listenAddress, handler)
2019-08-04 17:41:32 +00:00
}, nil
}