forked from rancher/rancher
-
Notifications
You must be signed in to change notification settings - Fork 0
/
kontainerdriver_data.go
156 lines (137 loc) · 3.95 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
package app
import (
"fmt"
"os"
"strings"
"github.com/rancher/rancher/pkg/controllers/management/drivers/kontainerdriver"
"github.com/rancher/types/apis/management.cattle.io/v3"
"github.com/rancher/types/config"
"github.com/sirupsen/logrus"
"k8s.io/apimachinery/pkg/api/errors"
"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 := creator.add("import"); 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(
"aliyunkubernetescontainerservice",
"https://github.com/rancher/kontainer-engine-driver-aliyun/releases/download/v0.2.4/kontainer-engine-driver-aliyun-linux",
"61e8d1a69dae4c9bee7a1618399422300b95436ca747e520329cfc1a724c4180",
"",
false,
"*.aliyuncs.com",
); err != nil {
return err
}
if err := creator.addCustomDriver(
"tencentkubernetesengine",
"https://github.com/rancher/kontainer-engine-driver-tencent/releases/download/v0.2.2/kontainer-engine-driver-tencent-linux",
"923bde3bcc2201e236b0e6ebcf83ca540dd12d23b5aa4804f12dd37f9beca6c6",
"",
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.1/kontainer-engine-driver-huawei-linux",
"8114c33cf166fa8447d3289db5330d38fbe87e09d4130d3d9eb6ba4dd8904a98",
"",
false,
"*.myhuaweicloud.com",
); err != nil {
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
}