2017-11-10 21:44:02 -07:00
|
|
|
package server
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
|
|
|
|
"github.com/pkg/errors"
|
|
|
|
"github.com/rancher/norman/api"
|
|
|
|
"github.com/rancher/norman/store/crd"
|
|
|
|
"github.com/rancher/norman/types"
|
|
|
|
"k8s.io/apiextensions-apiserver/pkg/client/clientset/clientset"
|
|
|
|
"k8s.io/client-go/dynamic"
|
|
|
|
"k8s.io/client-go/rest"
|
|
|
|
"k8s.io/client-go/tools/clientcmd"
|
|
|
|
)
|
|
|
|
|
2017-11-21 13:46:30 -07:00
|
|
|
func NewAPIServer(ctx context.Context, kubeConfig string, schemas *types.Schemas) (*api.Server, error) {
|
2017-11-10 21:44:02 -07:00
|
|
|
config, err := clientcmd.BuildConfigFromFlags("", kubeConfig)
|
|
|
|
if err != nil {
|
|
|
|
return nil, errors.Wrap(err, "failed to build kubeConfig")
|
|
|
|
}
|
|
|
|
return NewAPIServerFromConfig(ctx, config, schemas)
|
|
|
|
}
|
|
|
|
|
2017-11-21 13:46:30 -07:00
|
|
|
func NewClients(kubeConfig string) (rest.Interface, clientset.Interface, error) {
|
2017-11-10 21:44:02 -07:00
|
|
|
config, err := clientcmd.BuildConfigFromFlags("", kubeConfig)
|
|
|
|
if err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
return NewClientsFromConfig(config)
|
|
|
|
}
|
|
|
|
|
2017-11-21 13:46:30 -07:00
|
|
|
func NewClientsFromConfig(config *rest.Config) (rest.Interface, clientset.Interface, error) {
|
2017-11-10 21:44:02 -07:00
|
|
|
dynamicConfig := *config
|
|
|
|
if dynamicConfig.NegotiatedSerializer == nil {
|
|
|
|
configConfig := dynamic.ContentConfig()
|
|
|
|
dynamicConfig.NegotiatedSerializer = configConfig.NegotiatedSerializer
|
|
|
|
}
|
|
|
|
|
|
|
|
k8sClient, err := rest.UnversionedRESTClientFor(&dynamicConfig)
|
|
|
|
if err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
apiExtClient, err := clientset.NewForConfig(&dynamicConfig)
|
|
|
|
if err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return k8sClient, apiExtClient, nil
|
|
|
|
}
|
|
|
|
|
2017-11-21 13:46:30 -07:00
|
|
|
func NewAPIServerFromConfig(ctx context.Context, config *rest.Config, schemas *types.Schemas) (*api.Server, error) {
|
2017-11-10 21:44:02 -07:00
|
|
|
k8sClient, apiExtClient, err := NewClientsFromConfig(config)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return NewAPIServerFromClients(ctx, k8sClient, apiExtClient, schemas)
|
|
|
|
}
|
|
|
|
|
2017-11-21 13:46:30 -07:00
|
|
|
func NewAPIServerFromClients(ctx context.Context, k8sClient rest.Interface, apiExtClient clientset.Interface, schemas *types.Schemas) (*api.Server, error) {
|
2017-11-10 21:44:02 -07:00
|
|
|
store := crd.NewCRDStore(apiExtClient, k8sClient)
|
|
|
|
if err := store.AddSchemas(ctx, schemas); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
server := api.NewAPIServer()
|
2017-11-21 13:46:30 -07:00
|
|
|
return server, server.AddSchemas(schemas)
|
2017-11-10 21:44:02 -07:00
|
|
|
}
|