2019-08-04 17:41:32 +00:00
|
|
|
package server
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"net/http"
|
|
|
|
|
2019-09-09 21:28:55 +00:00
|
|
|
"github.com/rancher/wrangler/pkg/generic"
|
|
|
|
schema2 "k8s.io/apimachinery/pkg/runtime/schema"
|
|
|
|
|
|
|
|
"github.com/rancher/naok/pkg/clustercache"
|
|
|
|
|
|
|
|
"github.com/rancher/wrangler-api/pkg/generated/controllers/core"
|
|
|
|
|
2019-08-04 17:41:32 +00:00
|
|
|
"github.com/rancher/naok/pkg/accesscontrol"
|
|
|
|
"github.com/rancher/naok/pkg/client"
|
2019-08-14 18:08:34 +00:00
|
|
|
"github.com/rancher/naok/pkg/controllers/schema"
|
|
|
|
"github.com/rancher/naok/pkg/resources"
|
|
|
|
"github.com/rancher/naok/pkg/server/publicapi"
|
2019-08-04 17:41:32 +00:00
|
|
|
"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"
|
|
|
|
)
|
|
|
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2019-09-09 21:28:55 +00:00
|
|
|
core, err := core.NewFactoryFromConfig(restConfig)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2019-08-04 17:41:32 +00:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
cf, err := client.NewFactory(restConfig)
|
|
|
|
if err != nil {
|
2019-08-14 18:08:34 +00:00
|
|
|
return err
|
2019-08-04 17:41:32 +00:00
|
|
|
}
|
|
|
|
|
2019-09-09 21:28:55 +00:00
|
|
|
ccache := clustercache.NewClusterCache(ctx, cf.DynamicClient())
|
|
|
|
|
2019-08-14 18:08:34 +00:00
|
|
|
sf := resources.SchemaFactory(cf,
|
2019-08-14 20:13:26 +00:00
|
|
|
accesscontrol.NewAccessStore(rbac.Rbac().V1()),
|
2019-09-09 21:28:55 +00:00
|
|
|
k8s,
|
|
|
|
ccache,
|
|
|
|
core.Core().V1().ConfigMap(),
|
|
|
|
core.Core().V1().Secret())
|
2019-08-13 23:36:03 +00:00
|
|
|
|
2019-09-09 21:28:55 +00:00
|
|
|
sync := schema.Register(ctx,
|
2019-08-04 17:41:32 +00:00
|
|
|
k8s.Discovery(),
|
|
|
|
crd.Apiextensions().V1beta1().CustomResourceDefinition(),
|
|
|
|
api.Apiregistration().V1().APIService(),
|
2019-09-09 21:28:55 +00:00
|
|
|
ccache,
|
2019-08-13 23:36:03 +00:00
|
|
|
sf)
|
2019-08-04 17:41:32 +00:00
|
|
|
|
2019-08-14 18:08:34 +00:00
|
|
|
handler, err := publicapi.NewHandler(restConfig, sf)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2019-09-09 21:28:55 +00:00
|
|
|
for _, controllers := range []controllers{api, crd, rbac} {
|
|
|
|
for gvk, controller := range controllers.Controllers() {
|
|
|
|
ccache.AddController(gvk, controller.Informer())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-08-14 18:08:34 +00:00
|
|
|
if err := start.All(ctx, 5, api, crd, rbac); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2019-09-09 21:28:55 +00:00
|
|
|
if err := sync(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2019-08-14 18:08:34 +00:00
|
|
|
logrus.Infof("listening on %s", cfg.ListenAddress)
|
|
|
|
return http.ListenAndServe(cfg.ListenAddress, handler)
|
2019-08-04 17:41:32 +00:00
|
|
|
}
|
2019-09-09 21:28:55 +00:00
|
|
|
|
|
|
|
type controllers interface {
|
|
|
|
Controllers() map[schema2.GroupVersionKind]*generic.Controller
|
|
|
|
}
|