1
0
mirror of https://github.com/rancher/norman.git synced 2025-04-29 11:54:44 +00:00
norman/example/main.go

67 lines
1.3 KiB
Go
Raw Normal View History

2017-11-11 04:44:02 +00:00
package main
import (
2018-02-09 20:31:12 +00:00
"context"
2017-11-11 04:44:02 +00:00
"fmt"
"net/http"
"os"
2017-11-28 21:28:25 +00:00
"github.com/rancher/norman/api"
"github.com/rancher/norman/store/crd"
2018-02-09 20:31:12 +00:00
"github.com/rancher/norman/store/proxy"
2017-11-11 04:44:02 +00:00
"github.com/rancher/norman/types"
2017-11-29 21:27:02 +00:00
"github.com/rancher/norman/types/factory"
2017-11-28 21:28:25 +00:00
"k8s.io/client-go/tools/clientcmd"
2017-11-11 04:44:02 +00:00
)
type Foo struct {
types.Resource
Name string `json:"name"`
Foo string `json:"foo"`
SubThing Baz `json:"subThing"`
}
type Baz struct {
Name string `json:"name"`
}
var (
version = types.APIVersion{
Version: "v1",
2017-12-20 04:39:57 +00:00
Group: "example.core.cattle.io",
2017-11-11 04:44:02 +00:00
Path: "/example/v1",
}
2017-11-29 21:27:02 +00:00
Schemas = factory.Schemas(&version)
2017-11-11 04:44:02 +00:00
)
func main() {
2017-11-28 21:28:25 +00:00
kubeConfig, err := clientcmd.BuildConfigFromFlags("", os.Getenv("KUBECONFIG"))
if err != nil {
2017-11-11 04:44:02 +00:00
panic(err)
}
2018-02-09 20:31:12 +00:00
client, err := proxy.NewClientGetterFromConfig(*kubeConfig)
2017-11-11 04:44:02 +00:00
if err != nil {
panic(err)
}
2018-02-09 20:31:12 +00:00
crdFactory := crd.Factory{
ClientGetter: client,
}
2017-11-28 21:28:25 +00:00
Schemas.MustImportAndCustomize(&version, Foo{}, func(schema *types.Schema) {
2021-09-21 21:02:30 +00:00
if err := crdFactory.AssignStores(context.Background(), types.DefaultStorageContext, nil, schema); err != nil {
2018-02-09 20:31:12 +00:00
panic(err)
}
2017-11-28 21:28:25 +00:00
})
server := api.NewAPIServer()
if err := server.AddSchemas(Schemas); err != nil {
panic(err)
}
2017-11-11 04:44:02 +00:00
fmt.Println("Listening on 0.0.0.0:1234")
http.ListenAndServe("0.0.0.0:1234", server)
}