This repository has been archived by the owner on May 16, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
crd_storage_client.go
109 lines (98 loc) · 2.76 KB
/
crd_storage_client.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
package crd
import (
"fmt"
"time"
corev1 "k8s.io/api/core/v1"
"k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1beta1"
apiexts "k8s.io/apiextensions-apiserver/pkg/client/clientset/clientset"
apierrors "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/client-go/kubernetes"
"k8s.io/client-go/rest"
"github.com/solo-io/gloo-storage"
crdclientset "github.com/solo-io/gloo-storage/crd/client/clientset/versioned"
crdv1 "github.com/solo-io/gloo-storage/crd/solo.io/v1"
"github.com/solo-io/gloo/pkg/log"
)
type Client struct {
v1 *v1client
}
func NewStorage(cfg *rest.Config, namespace string, syncFrequency time.Duration) (storage.Interface, error) {
if namespace == "" {
namespace = GlooDefaultNamespace
}
crdClient, err := crdclientset.NewForConfig(cfg)
if err != nil {
return nil, err
}
apiextClient, err := apiexts.NewForConfig(cfg)
if err != nil {
return nil, err
}
kubeClient, err := kubernetes.NewForConfig(cfg)
if err != nil {
return nil, err
}
return &Client{
v1: &v1client{
upstreams: &upstreamsClient{
crds: crdClient,
namespace: namespace,
syncFrequency: syncFrequency,
},
virtualHosts: &virtualHostsClient{
crds: crdClient,
namespace: namespace,
syncFrequency: syncFrequency,
},
apiexts: apiextClient,
kubeclient: kubeClient,
namespace: namespace,
},
}, nil
}
func (c *Client) V1() storage.V1 {
return c.v1
}
type v1client struct {
apiexts apiexts.Interface
kubeclient kubernetes.Interface
upstreams *upstreamsClient
virtualHosts *virtualHostsClient
namespace string
}
func (c *v1client) Register() error {
// create namespace if it doesnt exist
if _, err := c.kubeclient.CoreV1().Namespaces().Create(&corev1.Namespace{
ObjectMeta: metav1.ObjectMeta{
Name: c.namespace,
},
}); err != nil && !apierrors.IsAlreadyExists(err) {
return fmt.Errorf("failed to create namespace %v: %v", c.namespace, err)
}
for _, crd := range crdv1.KnownCRDs {
toRegister := &v1beta1.CustomResourceDefinition{
ObjectMeta: metav1.ObjectMeta{Name: crd.FullName()},
Spec: v1beta1.CustomResourceDefinitionSpec{
Group: crd.Group,
Version: crd.Version,
Scope: v1beta1.NamespaceScoped,
Names: v1beta1.CustomResourceDefinitionNames{
Plural: crd.Plural,
Kind: crd.Kind,
},
},
}
log.Debugf("registering crd %v", crd)
if _, err := c.apiexts.ApiextensionsV1beta1().CustomResourceDefinitions().Create(toRegister); err != nil && !apierrors.IsAlreadyExists(err) {
return fmt.Errorf("failed to create crd: %v", err)
}
}
return nil
}
func (c *v1client) Upstreams() storage.Upstreams {
return c.upstreams
}
func (c *v1client) VirtualHosts() storage.VirtualHosts {
return c.virtualHosts
}