1
1
package config
2
2
3
3
import (
4
- "errors"
5
4
"fmt"
6
5
"gopkg.in/yaml.v3"
7
6
"k8s.io/client-go/kubernetes"
8
7
"k8s.io/client-go/tools/clientcmd"
9
- "log"
10
8
"os"
11
- "regexp"
12
9
"strings"
13
10
"sys-api/models"
14
- "sys-api/pkg/imp/cloudstack"
15
- "sys-api/pkg/repository"
16
- "sys-api/pkg/subsystems/cs"
17
11
)
18
12
19
13
func Setup () error {
@@ -36,153 +30,46 @@ func Setup() error {
36
30
return makeError (err )
37
31
}
38
32
39
- csClient := cloudstack .NewAsyncClient (
40
- models .Config .CS .URL ,
41
- models .Config .CS .ApiKey ,
42
- models .Config .CS .Secret ,
43
- true ,
44
- )
45
-
46
- // Load Kubernetes clusters from cloudstack
47
- listClusterParams := csClient .Kubernetes .NewListKubernetesClustersParams ()
48
- listClusterParams .SetListall (true )
49
- clusters , err := csClient .Kubernetes .ListKubernetesClusters (listClusterParams )
33
+ models .Config .K8s .Clients = make (map [string ]kubernetes.Clientset )
34
+
35
+ // Load config from models.Config.K8s.ConfigDir
36
+ // Filename without an extension is used as the cluster name
37
+ files , err := os .ReadDir (models .Config .K8s .ConfigDir )
50
38
if err != nil {
51
39
return makeError (err )
52
40
}
53
41
54
- fetchConfig := func (name string , publicUrl string ) string {
55
- clusterIdx := - 1
56
- for idx , cluster := range clusters .KubernetesClusters {
57
- if cluster .Name == name {
58
- clusterIdx = idx
59
- break
60
- }
61
- }
62
-
63
- if clusterIdx == - 1 {
64
- fmt .Println ("cluster" , name , "not found" )
65
- return ""
66
- }
67
-
68
- params := csClient .Kubernetes .NewGetKubernetesClusterConfigParams ()
69
- params .SetId (clusters .KubernetesClusters [clusterIdx ].Id )
70
-
71
- k8sConfig , err := csClient .Kubernetes .GetKubernetesClusterConfig (params )
72
- if err != nil {
73
- log .Println ("failed to fetch config for cluster" , name , ". details:" , err )
74
- return ""
75
- }
76
-
77
- // use regex to replace the private ip in config.ConffigData 172.31.1.* with the public ip
78
- regex := regexp .MustCompile (`https://172.31.1.[0-9]+:6443` )
79
-
80
- k8sConfig .ClusterConfig .Configdata = regex .ReplaceAllString (k8sConfig .ClusterConfig .Configdata , publicUrl )
81
-
82
- return k8sConfig .ClusterConfig .Configdata
83
- }
84
-
85
- models .Config .K8s .Clients = make (map [string ]kubernetes.Clientset )
86
-
87
- for _ , cluster := range models .Config .K8s .Clusters {
88
- // get the public ip of the cluster
89
- publicURL := cluster .URL
90
-
91
- // get the config data from cloudstack
92
- configData := fetchConfig (cluster .Name , publicURL )
93
- if configData == "" {
42
+ for _ , file := range files {
43
+ if file .IsDir () {
94
44
continue
95
45
}
96
46
97
- // create the k8s client
98
- client , err := createK8sClient ([] byte ( configData ))
47
+ clusterName := strings . TrimSuffix ( file . Name (), filepath [ strings . LastIndex ( filepath , "." ):])
48
+ configData , err := os . ReadFile ( models . Config . K8s . ConfigDir + "/" + file . Name ( ))
99
49
if err != nil {
100
- fmt .Println (makeError (errors . New ( "failed to connect to k8s cluster " + cluster . Name + ". details: " + err . Error ()) ))
50
+ fmt .Println (makeError (err ))
101
51
continue
102
52
}
103
53
104
- if client == nil {
105
- fmt .Println (makeError (errors .New ("failed to connect to k8s cluster " + cluster .Name + ", client is nil" )))
106
- continue
107
- }
108
-
109
- models .Config .K8s .Clients [cluster .Name ] = * client
110
- }
111
-
112
- clusterNames := make ([]string , len (models .Config .K8s .Clients ))
113
- i := 0
114
- for name := range models .Config .K8s .Clients {
115
- clusterNames [i ] = name
116
- i ++
117
- }
118
-
119
- if len (clusterNames ) > 0 {
120
- fmt .Println ("successfully connected to k8s clusters:" , strings .Join (clusterNames , ", " ))
121
- } else {
122
- fmt .Println ("failed to connect to any k8s clusters" )
123
- }
124
-
125
- return nil
126
- }
127
-
128
- func SyncCloudStackHosts () error {
129
- // Register hosts
130
- hosts , err := cs .NewClient (cs.ClientConfig {
131
- URL : models .Config .CS .URL ,
132
- ApiKey : models .Config .CS .ApiKey ,
133
- Secret : models .Config .CS .Secret ,
134
- }).ListHosts ()
135
-
136
- if err != nil {
137
- return fmt .Errorf ("failed to fetch hosts from cloudstack. details: %s" , err )
138
- }
139
-
140
- if len (hosts ) == 0 {
141
- return fmt .Errorf ("no hosts found in cloudstack. this is likely due to no hosts being available" )
142
- }
143
-
144
- zones := make (map [string ]models.Zone )
145
- for _ , host := range hosts {
146
- convertedName := convertCloudStackZone (host .Zone )
147
- if convertedName == nil {
148
- log .Printf ("zone %s not found in cloudstack zone name conversion map. this is likely a unoffical zone\n " , host .Zone )
54
+ client , err := createK8sClient (configData )
55
+ if err != nil {
56
+ fmt .Println (makeError (err ))
149
57
continue
150
58
}
151
59
152
- newHost := models .NewHost (host .Name , host .DisplayName , * convertedName , host .IP , host .Port , host .Enabled )
153
- if err = repository .NewClient ().RegisterHost (newHost ); err != nil {
154
- return err
155
- }
60
+ models .Config .K8s .Clients [clusterName ] = * client
156
61
157
- if _ , exists := zones [host .Zone ]; ! exists {
158
- zones [host .Zone ] = models.Zone {Name : * convertedName }
159
- }
62
+ fmt .Println ("Successfully connected to k8s cluster:" , clusterName )
160
63
}
161
64
162
- // Register zones
163
- for _ , zone := range zones {
164
- if err := repository .NewClient ().RegisterZone (& zone ); err != nil {
165
- return err
166
- }
65
+ if len (models .Config .K8s .Clients ) == 0 {
66
+ fmt .Println ("No k8s clusters found. Please check your config file and ensure that the k8s config directory is correct" )
167
67
}
168
68
169
69
return nil
170
70
}
171
71
172
- // convertCloudStackZone converts a cloudstack zone
173
- // It is a temporary solution since CloudStack does not use the same zone names as other systems
174
- func convertCloudStackZone (zoneName string ) * string {
175
- switch zoneName {
176
- case "Flemingsberg" :
177
- s := "se-flem"
178
- return & s
179
- case "Kista" :
180
- s := "se-kista"
181
- return & s
182
- }
183
- return nil
184
- }
185
-
72
+ // createK8sClient creates a k8s client from the text content of a kubeconfig file
186
73
func createK8sClient (configData []byte ) (* kubernetes.Clientset , error ) {
187
74
makeError := func (err error ) error {
188
75
return fmt .Errorf ("failed to create k8s client. details: %s" , err )
0 commit comments