1
0
mirror of https://github.com/rancher/norman.git synced 2025-06-28 16:27:25 +00:00
norman/store/crd/init.go

224 lines
5.8 KiB
Go
Raw Normal View History

package crd
import (
"context"
2018-06-19 18:26:12 +00:00
"fmt"
"strings"
"time"
2018-02-09 20:31:12 +00:00
"github.com/rancher/norman/store/proxy"
"github.com/rancher/norman/types"
"github.com/rancher/norman/types/convert"
"github.com/sirupsen/logrus"
2020-03-06 04:29:41 +00:00
"golang.org/x/sync/errgroup"
apiext "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1beta1"
2018-01-17 22:42:31 +00:00
"k8s.io/apiextensions-apiserver/pkg/client/clientset/clientset"
"k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/util/wait"
2018-06-19 18:26:12 +00:00
"k8s.io/client-go/rest"
)
type Factory struct {
2020-03-06 04:29:41 +00:00
eg errgroup.Group
2018-02-09 20:31:12 +00:00
ClientGetter proxy.ClientGetter
2018-01-17 22:42:31 +00:00
}
2018-06-19 18:26:12 +00:00
func NewFactoryFromClientGetter(clientGetter proxy.ClientGetter) *Factory {
return &Factory{
ClientGetter: clientGetter,
}
}
func NewFactoryFromClient(config rest.Config) (*Factory, error) {
getter, err := proxy.NewClientGetterFromConfig(config)
if err != nil {
return nil, err
}
return &Factory{
ClientGetter: getter,
}, nil
}
2020-03-06 04:29:41 +00:00
func (f *Factory) BatchWait() error {
return f.eg.Wait()
2018-06-19 18:26:12 +00:00
}
func (f *Factory) BatchCreateCRDs(ctx context.Context, storageContext types.StorageContext, schemas *types.Schemas, version *types.APIVersion, schemaIDs ...string) {
2020-03-06 04:29:41 +00:00
f.eg.Go(func() error {
2018-06-19 18:26:12 +00:00
var schemasToCreate []*types.Schema
for _, schemaID := range schemaIDs {
s := schemas.Schema(version, schemaID)
if s == nil {
2020-03-06 04:29:41 +00:00
return fmt.Errorf("can not find schema %s", schemaID)
2018-06-19 18:26:12 +00:00
}
schemasToCreate = append(schemasToCreate, s)
}
err := f.AssignStores(ctx, storageContext, schemasToCreate...)
if err != nil {
2020-03-06 04:29:41 +00:00
return fmt.Errorf("creating CRD store %v", err)
2018-06-19 18:26:12 +00:00
}
2020-03-06 04:29:41 +00:00
return nil
})
2018-06-19 18:26:12 +00:00
}
func (f *Factory) AssignStores(ctx context.Context, storageContext types.StorageContext, schemas ...*types.Schema) error {
schemaStatus, err := f.CreateCRDs(ctx, storageContext, schemas...)
2018-02-09 20:31:12 +00:00
if err != nil {
return err
2018-01-17 22:42:31 +00:00
}
2018-02-09 20:31:12 +00:00
for _, schema := range schemas {
crd, ok := schemaStatus[schema]
if !ok {
return fmt.Errorf("failed to create create/find CRD for %s", schema.ID)
}
2018-06-19 18:26:12 +00:00
schema.Store = proxy.NewProxyStore(ctx, f.ClientGetter,
2018-02-09 20:31:12 +00:00
storageContext,
[]string{"apis"},
crd.Spec.Group,
crd.Spec.Version,
crd.Status.AcceptedNames.Kind,
crd.Status.AcceptedNames.Plural)
2018-01-17 22:42:31 +00:00
}
2018-02-09 20:31:12 +00:00
return nil
}
2018-06-19 18:26:12 +00:00
func (f *Factory) CreateCRDs(ctx context.Context, storageContext types.StorageContext, schemas ...*types.Schema) (map[*types.Schema]*apiext.CustomResourceDefinition, error) {
schemaStatus := map[*types.Schema]*apiext.CustomResourceDefinition{}
2018-06-19 18:26:12 +00:00
apiClient, err := f.ClientGetter.APIExtClient(nil, storageContext)
2018-02-09 20:31:12 +00:00
if err != nil {
return nil, err
}
2020-03-26 20:07:32 +00:00
ready, err := f.getReadyCRDs(ctx, apiClient)
if err != nil {
return nil, err
}
for _, schema := range schemas {
2020-03-26 20:07:32 +00:00
crd, err := f.createCRD(ctx, apiClient, schema, ready)
if err != nil {
return nil, err
}
schemaStatus[schema] = crd
}
2020-03-26 20:07:32 +00:00
ready, err = f.getReadyCRDs(ctx, apiClient)
if err != nil {
return nil, err
}
for schema, crd := range schemaStatus {
if readyCrd, ok := ready[crd.Name]; ok {
schemaStatus[schema] = readyCrd
} else {
2018-06-19 18:26:12 +00:00
if err := f.waitCRD(ctx, apiClient, crd.Name, schema, schemaStatus); err != nil {
return nil, err
}
}
}
return schemaStatus, nil
}
2018-06-19 18:26:12 +00:00
func (f *Factory) waitCRD(ctx context.Context, apiClient clientset.Interface, crdName string, schema *types.Schema, schemaStatus map[*types.Schema]*apiext.CustomResourceDefinition) error {
logrus.Infof("Waiting for CRD %s to become available", crdName)
defer logrus.Infof("Done waiting for CRD %s to become available", crdName)
first := true
return wait.Poll(500*time.Millisecond, 60*time.Second, func() (bool, error) {
if !first {
logrus.Infof("Waiting for CRD %s to become available", crdName)
}
first = false
2020-03-26 20:07:32 +00:00
crd, err := apiClient.ApiextensionsV1beta1().CustomResourceDefinitions().Get(ctx, crdName, metav1.GetOptions{})
if err != nil {
return false, err
}
for _, cond := range crd.Status.Conditions {
switch cond.Type {
case apiext.Established:
if cond.Status == apiext.ConditionTrue {
schemaStatus[schema] = crd
return true, err
}
case apiext.NamesAccepted:
if cond.Status == apiext.ConditionFalse {
logrus.Infof("Name conflict on %s: %v\n", crdName, cond.Reason)
}
}
}
return false, ctx.Err()
})
}
2020-03-26 20:07:32 +00:00
func (f *Factory) createCRD(ctx context.Context, apiClient clientset.Interface, schema *types.Schema, ready map[string]*apiext.CustomResourceDefinition) (*apiext.CustomResourceDefinition, error) {
plural := strings.ToLower(schema.PluralName)
name := strings.ToLower(plural + "." + schema.Version.Group)
crd, ok := ready[name]
if ok {
return crd, nil
}
crd = &apiext.CustomResourceDefinition{
ObjectMeta: metav1.ObjectMeta{
Name: name,
},
Spec: apiext.CustomResourceDefinitionSpec{
Group: schema.Version.Group,
Version: schema.Version.Version,
Names: apiext.CustomResourceDefinitionNames{
Plural: plural,
Kind: convert.Capitalize(schema.ID),
},
},
}
if schema.Scope == types.NamespaceScope {
crd.Spec.Scope = apiext.NamespaceScoped
} else {
crd.Spec.Scope = apiext.ClusterScoped
}
logrus.Infof("Creating CRD %s", name)
2020-03-26 20:07:32 +00:00
crd2, err := apiClient.ApiextensionsV1beta1().CustomResourceDefinitions().Create(ctx, crd, metav1.CreateOptions{})
if errors.IsAlreadyExists(err) {
return crd, nil
}
2019-09-30 11:35:31 +00:00
return crd2, err
}
2020-03-26 20:07:32 +00:00
func (f *Factory) getReadyCRDs(ctx context.Context, apiClient clientset.Interface) (map[string]*apiext.CustomResourceDefinition, error) {
list, err := apiClient.ApiextensionsV1beta1().CustomResourceDefinitions().List(ctx, metav1.ListOptions{})
if err != nil {
return nil, err
}
result := map[string]*apiext.CustomResourceDefinition{}
for i, crd := range list.Items {
for _, cond := range crd.Status.Conditions {
switch cond.Type {
case apiext.Established:
if cond.Status == apiext.ConditionTrue {
result[crd.Name] = &list.Items[i]
}
}
}
}
return result, nil
}