forked from rancher/rancher
-
Notifications
You must be signed in to change notification settings - Fork 1
/
kontainerdriver_data.go
180 lines (157 loc) · 4.64 KB
/
kontainerdriver_data.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
package app
import (
"fmt"
"os"
"strings"
"github.com/rancher/rancher/pkg/controllers/management/drivers/kontainerdriver"
v3 "github.com/rancher/types/apis/management.cattle.io/v3"
"github.com/rancher/types/config"
"github.com/sirupsen/logrus"
"k8s.io/apimachinery/pkg/api/errors"
v1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)
func addKontainerDrivers(management *config.ManagementContext) error {
// create binary drop location if not exists
err := os.MkdirAll(kontainerdriver.DriverDir, 0777)
if err != nil {
return fmt.Errorf("error creating binary drop folder: %v", err)
}
creator := driverCreator{
driversLister: management.Management.KontainerDrivers("").Controller().Lister(),
drivers: management.Management.KontainerDrivers(""),
}
if err := cleanupImportDriver(creator); err != nil {
return err
}
if err := creator.add("rancherKubernetesEngine"); err != nil {
return err
}
if err := creator.add("googleKubernetesEngine"); err != nil {
return err
}
if err := creator.add("azureKubernetesService"); err != nil {
return err
}
if err := creator.add("amazonElasticContainerService"); err != nil {
return err
}
if err := creator.addCustomDriver(
"baiducloudcontainerengine",
"https://github.com/cnrancher/kontainer-engine-driver-baidu/releases/download/v0.2.0/kontainer-engine-driver-baidu-linux",
"4613e3be3ae5487b0e21dfa761b95de2144f80f98bf76847411e5fcada343d5e",
"https://cluster-driver.oss-cn-shenzhen.aliyuncs.com/baidu/ui/component.js",
false,
"*.aliyuncs.com", "*.baidubce.com",
); err != nil {
return err
}
if err := creator.addCustomDriver(
"aliyunkubernetescontainerservice",
"https://github.com/rancher/kontainer-engine-driver-aliyun/releases/download/v0.2.5/kontainer-engine-driver-aliyun-linux",
"31aa0a44450c5a5eb128dd0956292dfd91aab726d1a548f6d527a9212a27db9b",
"",
false,
"*.aliyuncs.com",
); err != nil {
return err
}
if err := creator.addCustomDriver(
"tencentkubernetesengine",
"https://github.com/rancher/kontainer-engine-driver-tencent/releases/download/v0.2.3/kontainer-engine-driver-tencent-linux",
"144f785473290ee2f63cf35da0c6bde12bc307878078500a47a0a8d04422ae53",
"",
false,
"*.tencentcloudapi.com", "*.qcloud.com",
); err != nil {
return err
}
if err := creator.addCustomDriver(
"huaweicontainercloudengine",
"https://github.com/rancher/kontainer-engine-driver-huawei/releases/download/v0.1.2/kontainer-engine-driver-huawei-linux",
"0b6c1dfaa477a60a3bd9f8a60a55fcafd883866c2c5c387aec75b95d6ba81d45",
"",
false,
"*.myhuaweicloud.com",
); err != nil {
return err
}
return nil
}
func cleanupImportDriver(creator driverCreator) error {
var err error
if _, err = creator.driversLister.Get("", "import"); err == nil {
err = creator.drivers.Delete("import", &v1.DeleteOptions{})
}
if !errors.IsNotFound(err) {
return err
}
return nil
}
type driverCreator struct {
driversLister v3.KontainerDriverLister
drivers v3.KontainerDriverInterface
}
func (c *driverCreator) add(name string) error {
logrus.Infof("adding kontainer driver %v", name)
driver, err := c.driversLister.Get("", name)
if err != nil {
if errors.IsNotFound(err) {
_, err = c.drivers.Create(&v3.KontainerDriver{
ObjectMeta: v1.ObjectMeta{
Name: strings.ToLower(name),
Namespace: "",
},
Spec: v3.KontainerDriverSpec{
URL: "",
BuiltIn: true,
Active: true,
},
Status: v3.KontainerDriverStatus{
DisplayName: name,
},
})
if err != nil && !errors.IsAlreadyExists(err) {
return fmt.Errorf("error creating driver: %v", err)
}
} else {
return fmt.Errorf("error getting driver: %v", err)
}
} else {
driver.Spec.URL = ""
_, err = c.drivers.Update(driver)
if err != nil {
return fmt.Errorf("error updating driver: %v", err)
}
}
return nil
}
func (c *driverCreator) addCustomDriver(name, url, checksum, uiURL string, active bool, domains ...string) error {
logrus.Infof("adding kontainer driver %v", name)
_, err := c.driversLister.Get("", name)
if err != nil {
if errors.IsNotFound(err) {
_, err = c.drivers.Create(&v3.KontainerDriver{
ObjectMeta: v1.ObjectMeta{
Name: strings.ToLower(name),
},
Spec: v3.KontainerDriverSpec{
URL: url,
BuiltIn: false,
Active: active,
Checksum: checksum,
UIURL: uiURL,
WhitelistDomains: domains,
},
Status: v3.KontainerDriverStatus{
DisplayName: name,
},
})
if err != nil && !errors.IsAlreadyExists(err) {
return fmt.Errorf("error creating driver: %v", err)
}
} else {
return fmt.Errorf("error getting driver: %v", err)
}
}
return nil
}