generated from kubernetes/kubernetes-template-project
-
Notifications
You must be signed in to change notification settings - Fork 950
/
port_parser.go
99 lines (87 loc) · 3 KB
/
port_parser.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
/*
Copyright 2023 The Fluid Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package vineyard
import (
"context"
"fmt"
"github.com/fluid-cloudnative/fluid/api/v1alpha1"
"github.com/fluid-cloudnative/fluid/pkg/common"
"github.com/fluid-cloudnative/fluid/pkg/utils/kubeclient"
"github.com/pkg/errors"
corev1 "k8s.io/api/core/v1"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/yaml"
)
// GetReservedPorts defines restoration logic for VineyardRuntime
func GetReservedPorts(client client.Client) (ports []int, err error) {
var datasets v1alpha1.DatasetList
err = client.List(context.TODO(), &datasets)
if err != nil {
return nil, errors.Wrap(err, "can't list datasets when GetReservedPorts")
}
for _, dataset := range datasets.Items {
if len(dataset.Status.Runtimes) != 0 {
// Assume there is only one runtime with category "Accelerate"
accelerateRuntime := dataset.Status.Runtimes[0]
if accelerateRuntime.Type != common.VineyardRuntime {
continue
}
configMapName := fmt.Sprintf("%s-%s-values", accelerateRuntime.Name, accelerateRuntime.Type)
configMap, err := kubeclient.GetConfigmapByName(client, configMapName, accelerateRuntime.Namespace)
if err != nil {
return nil, errors.Wrap(err, "GetConfigMapByName when GetReservedPorts")
}
if configMap == nil {
continue
}
reservedPorts, err := parsePortsFromConfigMap(configMap)
if err != nil {
return nil, errors.Wrap(err, "parsePortsFromConfigMap when GetReservedPorts")
}
ports = append(ports, reservedPorts...)
}
}
return ports, nil
}
// parsePortsFromConfigMap extracts port usage information given a configMap
func parsePortsFromConfigMap(configMap *corev1.ConfigMap) (ports []int, err error) {
var value Vineyard
if v, ok := configMap.Data["data"]; ok {
if err := yaml.Unmarshal([]byte(v), &value); err != nil {
return nil, err
}
if value.Master.HostNetwork && value.Master.Ports != nil {
clientPort, ok := value.Master.Ports[MasterClientName]
if ok {
fmt.Println("clientPort:", clientPort)
ports = append(ports, clientPort)
}
peerPort, ok := value.Master.Ports[MasterPeerName]
if ok {
fmt.Println("peerPort:", peerPort)
ports = append(ports, peerPort)
}
}
if value.Worker.HostNetwork && value.Worker.Ports != nil {
rpcPort, ok := value.Worker.Ports[WorkerRPCName]
if ok {
ports = append(ports, rpcPort)
}
exporterPort, ok := value.Worker.Ports[WorkerExporterName]
if ok {
ports = append(ports, exporterPort)
}
}
}
return ports, nil
}