forked from rancher/rancher
-
Notifications
You must be signed in to change notification settings - Fork 0
/
globaldns_provider.go
203 lines (173 loc) · 7.25 KB
/
globaldns_provider.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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
package globaldns
import (
"bytes"
"context"
"fmt"
"text/template"
"github.com/rancher/rancher/pkg/namespace"
"github.com/rancher/types/apis/management.cattle.io/v3"
"github.com/rancher/types/config"
"github.com/sirupsen/logrus"
"k8s.io/api/core/v1"
"k8s.io/api/extensions/v1beta1"
k8srbacV1 "k8s.io/api/rbac/v1beta1"
k8serrors "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/client-go/kubernetes/scheme"
corev1 "k8s.io/client-go/kubernetes/typed/core/v1"
extv1beta1 "k8s.io/client-go/kubernetes/typed/extensions/v1beta1"
rbacv1 "k8s.io/client-go/kubernetes/typed/rbac/v1beta1"
)
const (
GlobaldnsProviderLauncher = "mgmt-global-dns-provider-launcher"
)
type ProviderLauncher struct {
GlobalDNSproviders v3.GlobalDNSProviderInterface
GlobalDNSproviderLister v3.GlobalDNSProviderLister
Deployments extv1beta1.DeploymentInterface
ServiceAccounts corev1.ServiceAccountInterface
ClusterRoles rbacv1.ClusterRoleInterface
ClusterRoleBindings rbacv1.ClusterRoleBindingInterface
}
func newGlobalDNSProviderLauncher(ctx context.Context, mgmt *config.ManagementContext) *ProviderLauncher {
n := &ProviderLauncher{
GlobalDNSproviders: mgmt.Management.GlobalDNSProviders(namespace.GlobalNamespace),
GlobalDNSproviderLister: mgmt.Management.GlobalDNSProviders(namespace.GlobalNamespace).Controller().Lister(),
Deployments: mgmt.K8sClient.ExtensionsV1beta1().Deployments(namespace.GlobalNamespace),
ServiceAccounts: mgmt.K8sClient.CoreV1().ServiceAccounts(namespace.GlobalNamespace),
ClusterRoles: mgmt.K8sClient.RbacV1beta1().ClusterRoles(),
ClusterRoleBindings: mgmt.K8sClient.RbacV1beta1().ClusterRoleBindings(),
}
return n
}
//sync is called periodically and on real updates
func (n *ProviderLauncher) sync(key string, obj *v3.GlobalDNSProvider) (runtime.Object, error) {
if obj == nil || obj.DeletionTimestamp != nil {
return nil, nil
}
//check if provider already running for this GlobalDNSProvider.
if n.isProviderAlreadyRunning(obj) {
logrus.Infof("GlobaldnsProviderLauncher: Found a running external-dns deployment for this provider %v , skip creating a new one", obj.Name)
return nil, nil
}
//create svcAcct and rbac entities if not created already
err := n.createExternalDNSServiceAccount()
if err != nil {
return nil, err
}
err = n.createExternalDNSClusterRole()
if err != nil {
return nil, err
}
err = n.createExternalDNSClusterRoleBinding()
if err != nil {
return nil, err
}
//handle external-dns deployment
if obj.Spec.Route53ProviderConfig != nil {
return n.handleRoute53Provider(obj)
}
return nil, nil
}
func (n *ProviderLauncher) handleRoute53Provider(obj *v3.GlobalDNSProvider) (runtime.Object, error) {
//create external-dns route53 provider
data := map[string]interface{}{
"awsAccessKey": obj.Spec.Route53ProviderConfig.AccessKey,
"awsSecretKey": obj.Spec.Route53ProviderConfig.SecretKey,
"route53Domain": obj.Spec.Route53ProviderConfig.RootDomain,
"deploymentName": obj.Name,
}
route53Template := template.Must(template.New("route53_template").Parse(Route53DeploymentTemplate))
var output bytes.Buffer
if err := route53Template.Execute(&output, data); err != nil {
return nil, fmt.Errorf("GlobaldnsProviderLauncher: Error parsing the external-dns/route53 deployment template: %v", err)
}
decode := scheme.Codecs.UniversalDeserializer().Decode
deployObj, _, err := decode(output.Bytes(), nil, nil)
if err != nil {
return nil, fmt.Errorf("GlobaldnsProviderLauncher: Error decoding external-dns/route53 deployment template to Kubernetes Object: %v", err)
}
deployment := deployObj.(*v1beta1.Deployment)
//set ownerRef to the dnsProvider CR
controller := true
ownerRef := []metav1.OwnerReference{{
Name: obj.Name,
APIVersion: "v3",
UID: obj.UID,
Kind: obj.Kind,
Controller: &controller,
}}
deployment.ObjectMeta.OwnerReferences = ownerRef
deploymentCreated, err := n.Deployments.Create(deployment)
if err != nil {
return nil, fmt.Errorf("GlobaldnsProviderLauncher: Error creating external-dns deployment for Route53 provider: %v ", err)
}
logrus.Infof("GlobaldnsProviderLauncher: Created Route53 external-dns provider deployment %v", deploymentCreated.Name)
return nil, nil
}
func (n *ProviderLauncher) isProviderAlreadyRunning(obj *v3.GlobalDNSProvider) bool {
existingDep, err := n.Deployments.Get("route53", metav1.GetOptions{})
if (err != nil && k8serrors.IsNotFound(err)) || existingDep == nil {
return false
} else if err != nil && !k8serrors.IsNotFound(err) {
logrus.Errorf("GlobaldnsProviderLauncher: Error listing external-dns %v Deployment %v", obj.Name, err)
return false
}
return true
}
func (n *ProviderLauncher) createExternalDNSServiceAccount() error {
decode := scheme.Codecs.UniversalDeserializer().Decode
//create svcAcct and bindings
_, err := n.ServiceAccounts.Get("external-dns", metav1.GetOptions{})
if err != nil && !k8serrors.IsNotFound(err) {
return fmt.Errorf("GlobaldnsProviderLauncher: Error listing external-dns ServiceAccount %v", err)
} else if k8serrors.IsNotFound(err) {
svcAcctObj, _, err := decode([]byte(ExternalDNSServiceAcct), nil, nil)
if err != nil {
return fmt.Errorf("GlobaldnsProviderLauncher: Error decoding external-dns ServiceAccount to Kubernetes Object: %v", err)
}
svcAcct := svcAcctObj.(*v1.ServiceAccount)
_, err = n.ServiceAccounts.Create(svcAcct)
if err != nil {
return fmt.Errorf("GlobaldnsProviderLauncher: Error creating external-dns ServiceAccount: %v ", err)
}
}
return nil
}
func (n *ProviderLauncher) createExternalDNSClusterRole() error {
decode := scheme.Codecs.UniversalDeserializer().Decode
_, err := n.ClusterRoles.Get("external-dns", metav1.GetOptions{})
if err != nil && !k8serrors.IsNotFound(err) {
return fmt.Errorf("GlobaldnsProviderLauncher: Error listing external-dns ClusterRole %v", err)
} else if k8serrors.IsNotFound(err) {
crObj, _, err := decode([]byte(ExternalDNSClusterRole), nil, nil)
if err != nil {
return fmt.Errorf("GlobaldnsProviderLauncher: Error decoding external-dns ClusterRole to Kubernetes Object: %v", err)
}
cr := crObj.(*k8srbacV1.ClusterRole)
_, err = n.ClusterRoles.Create(cr)
if err != nil {
return fmt.Errorf("GlobaldnsProviderLauncher: Error creating external-dns ClusterRole: %v ", err)
}
}
return nil
}
func (n *ProviderLauncher) createExternalDNSClusterRoleBinding() error {
decode := scheme.Codecs.UniversalDeserializer().Decode
_, err := n.ClusterRoleBindings.Get("external-dns-viewer", metav1.GetOptions{})
if err != nil && !k8serrors.IsNotFound(err) {
return fmt.Errorf("GlobaldnsProviderLauncher: Error listing external-dns-viewer ClusterRoleBinding %v", err)
} else if k8serrors.IsNotFound(err) {
crBindingObj, _, err := decode([]byte(ExternalDNSClusterRoleBinding), nil, nil)
if err != nil {
return fmt.Errorf("GlobaldnsProviderLauncher; Error decoding external-dns-viewer ClusterRoleBinding to Kubernetes Object: %v", err)
}
crBinding := crBindingObj.(*k8srbacV1.ClusterRoleBinding)
_, err = n.ClusterRoleBindings.Create(crBinding)
if err != nil {
return fmt.Errorf("GlobaldnsProviderLauncher: Error creating external-dns-viewer ClusterRoleBinding: %v ", err)
}
}
return nil
}