forked from rancher/rancher
-
Notifications
You must be signed in to change notification settings - Fork 1
/
driver.go
176 lines (133 loc) · 5.39 KB
/
driver.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
package clusterprovisioner
import (
"fmt"
"reflect"
"strings"
"github.com/rancher/kontainer-engine/service"
"github.com/rancher/rancher/pkg/clusterprovisioninglogger"
"github.com/rancher/rke/services"
v3 "github.com/rancher/types/apis/management.cattle.io/v3"
"github.com/sirupsen/logrus"
apierrors "k8s.io/apimachinery/pkg/api/errors"
"k8s.io/apimachinery/pkg/runtime"
)
const DriverNameField = "driverName"
func (p *Provisioner) driverCreate(cluster *v3.Cluster, spec v3.ClusterSpec) (api string, token string, cert string, err error) {
ctx, logger := clusterprovisioninglogger.NewLogger(p.Clusters, cluster, v3.ClusterConditionProvisioned)
defer logger.Close()
spec = cleanRKE(spec)
if newCluster, err := p.Clusters.Update(cluster); err == nil {
cluster = newCluster
}
kontainerDriver, err := p.getKontainerDriver(spec)
if err != nil {
return "", "", "", err
}
return p.engineService.Create(ctx, cluster.Name, kontainerDriver, spec)
}
func (p *Provisioner) getKontainerDriver(spec v3.ClusterSpec) (*v3.KontainerDriver, error) {
if spec.GenericEngineConfig != nil {
return p.KontainerDriverLister.Get("", (*spec.GenericEngineConfig)[DriverNameField].(string))
}
if spec.RancherKubernetesEngineConfig != nil {
return p.KontainerDriverLister.Get("", service.RancherKubernetesEngineDriverName)
}
if spec.ImportedConfig != nil {
return p.KontainerDriverLister.Get("", "import")
}
return nil, fmt.Errorf("no kontainer driver for cluster %v", spec.DisplayName)
}
func (p *Provisioner) driverUpdate(cluster *v3.Cluster, spec v3.ClusterSpec) (api string, token string, cert string, err error) {
ctx, logger := clusterprovisioninglogger.NewLogger(p.Clusters, cluster, v3.ClusterConditionUpdated)
defer logger.Close()
spec = cleanRKE(spec)
applied := cleanRKE(cluster.Status.AppliedSpec)
if spec.RancherKubernetesEngineConfig != nil && cluster.Status.APIEndpoint != "" && cluster.Status.ServiceAccountToken != "" &&
reflect.DeepEqual(applied.RancherKubernetesEngineConfig, spec.RancherKubernetesEngineConfig) {
return cluster.Status.APIEndpoint, cluster.Status.ServiceAccountToken, cluster.Status.CACert, nil
}
if spec.RancherKubernetesEngineConfig != nil && spec.RancherKubernetesEngineConfig.Services.Etcd.Snapshot == nil &&
applied.RancherKubernetesEngineConfig != nil && applied.RancherKubernetesEngineConfig.Services.Etcd.Snapshot == nil {
_false := false
cluster.Spec.RancherKubernetesEngineConfig.Services.Etcd.Snapshot = &_false
}
if newCluster, err := p.Clusters.Update(cluster); err == nil {
cluster = newCluster
}
kontainerDriver, err := p.getKontainerDriver(spec)
if err != nil {
return "", "", "", err
}
return p.engineService.Update(ctx, cluster.Name, kontainerDriver, spec)
}
func (p *Provisioner) driverRemove(cluster *v3.Cluster, forceRemove bool) error {
ctx, logger := clusterprovisioninglogger.NewLogger(p.Clusters, cluster, v3.ClusterConditionProvisioned)
defer logger.Close()
spec := cleanRKE(cluster.Spec)
_, err := v3.ClusterConditionUpdated.Do(cluster, func() (runtime.Object, error) {
if newCluster, err := p.Clusters.Update(cluster); err == nil {
cluster = newCluster
}
kontainerDriver, err := p.getKontainerDriver(spec)
if err != nil {
if apierrors.IsNotFound(err) {
logrus.Warnf("Could not find kontainer driver for cluster removal [%v]", err)
return nil, nil
}
return nil, err
}
return cluster, p.engineService.Remove(ctx, cluster.Name, kontainerDriver, spec, forceRemove)
})
return err
}
func (p *Provisioner) driverRestore(cluster *v3.Cluster, spec v3.ClusterSpec) (string, string, string, error) {
ctx, logger := clusterprovisioninglogger.NewLogger(p.Clusters, cluster, v3.ClusterConditionUpdated)
defer logger.Close()
spec = cleanRKE(spec)
newCluster, err := p.Clusters.Update(cluster)
if err != nil {
return "", "", "", err
}
cluster = newCluster
kontainerDriver, err := p.getKontainerDriver(spec)
if err != nil {
return "", "", "", err
}
snapshot := strings.Split(spec.RancherKubernetesEngineConfig.Restore.SnapshotName, ":")[1]
return p.engineService.ETCDRestore(ctx, cluster.Name, kontainerDriver, spec, snapshot)
}
func (p *Provisioner) generateServiceAccount(cluster *v3.Cluster, spec v3.ClusterSpec) (string, error) {
ctx, logger := clusterprovisioninglogger.NewLogger(p.Clusters, cluster, v3.ClusterConditionUpdated)
defer logger.Close()
spec = cleanRKE(spec)
kontainerDriver, err := p.getKontainerDriver(spec)
if err != nil {
return "", err
}
return p.engineService.GenerateServiceAccount(ctx, cluster.Name, kontainerDriver, spec)
}
func (p *Provisioner) removeLegacyServiceAccount(cluster *v3.Cluster, spec v3.ClusterSpec) error {
ctx, logger := clusterprovisioninglogger.NewLogger(p.Clusters, cluster, v3.ClusterConditionUpdated)
defer logger.Close()
spec = cleanRKE(spec)
kontainerDriver, err := p.getKontainerDriver(spec)
if err != nil {
return err
}
return p.engineService.RemoveLegacyServiceAccount(ctx, cluster.Name, kontainerDriver, spec)
}
func cleanRKE(spec v3.ClusterSpec) v3.ClusterSpec {
if spec.RancherKubernetesEngineConfig == nil {
return spec
}
result := spec.DeepCopy()
var filteredNodes []v3.RKEConfigNode
for _, node := range spec.RancherKubernetesEngineConfig.Nodes {
if len(node.Role) == 1 && node.Role[0] == services.WorkerRole {
continue
}
filteredNodes = append(filteredNodes, node)
}
result.RancherKubernetesEngineConfig.Nodes = filteredNodes
return *result
}