/
forge.go
127 lines (112 loc) · 4.35 KB
/
forge.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
// Copyright 2019-2023 The Liqo 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 forge
import (
"fmt"
"strconv"
"strings"
v1 "k8s.io/api/core/v1"
discoveryv1alpha1 "github.com/liqotech/liqo/apis/discovery/v1alpha1"
sharingv1alpha1 "github.com/liqotech/liqo/apis/sharing/v1alpha1"
"github.com/liqotech/liqo/pkg/utils/pod"
"github.com/liqotech/liqo/pkg/virtualKubelet"
vk "github.com/liqotech/liqo/pkg/vkMachinery"
)
func getDefaultStorageClass(storageClasses []sharingv1alpha1.StorageType) sharingv1alpha1.StorageType {
for _, storageClass := range storageClasses {
if storageClass.Default {
return storageClass
}
}
return storageClasses[0]
}
func forgeVKContainers(
vkImage string, homeCluster, remoteCluster *discoveryv1alpha1.ClusterIdentity,
nodeName, vkNamespace string, opts *VirtualKubeletOpts,
resourceOffer *sharingv1alpha1.ResourceOffer) []v1.Container {
command := []string{
"/usr/bin/virtual-kubelet",
}
args := []string{
stringifyArgument("--foreign-cluster-id", remoteCluster.ClusterID),
stringifyArgument("--foreign-cluster-name", remoteCluster.ClusterName),
stringifyArgument("--nodename", nodeName),
stringifyArgument("--node-ip", "$(POD_IP)"),
stringifyArgument("--tenant-namespace", vkNamespace),
stringifyArgument("--home-cluster-id", homeCluster.ClusterID),
stringifyArgument("--home-cluster-name", homeCluster.ClusterName),
}
if opts.IpamEndpoint != "" {
args = append(args, stringifyArgument("--ipam-server", opts.IpamEndpoint))
}
if len(resourceOffer.Spec.StorageClasses) > 0 {
args = append(args, "--enable-storage",
stringifyArgument("--remote-real-storage-class-name",
getDefaultStorageClass(resourceOffer.Spec.StorageClasses).StorageClassName))
}
if extraAnnotations := opts.NodeExtraAnnotations.StringMap; len(extraAnnotations) != 0 {
args = append(args, stringifyArgument("--node-extra-annotations", opts.NodeExtraAnnotations.String()))
}
if extraLabels := opts.NodeExtraLabels.StringMap; len(extraLabels) != 0 {
args = append(args, stringifyArgument("--node-extra-labels", opts.NodeExtraLabels.String()))
}
args = append(args, opts.ExtraArgs...)
containerPorts := []v1.ContainerPort{}
args = append(args, stringifyArgument("--metrics-enabled", strconv.FormatBool(opts.MetricsEnabled)))
if opts.MetricsEnabled {
args = append(args, stringifyArgument("--metrics-address", opts.MetricsAddress))
metrAddrSplit := strings.Split(opts.MetricsAddress, ":")
metricsPort, err := strconv.ParseInt(metrAddrSplit[len(metrAddrSplit)-1], 10, 32)
if err != nil {
metrAddrSplit := strings.Split(vk.MetricsAddress, ":")
// if the metrics address is not valid, use the default one
metricsPort, _ = strconv.ParseInt(metrAddrSplit[len(metrAddrSplit)-1], 10, 32)
}
containerPorts = append(containerPorts, v1.ContainerPort{
Name: "metrics",
ContainerPort: int32(metricsPort),
Protocol: v1.ProtocolTCP,
})
}
return []v1.Container{
{
Name: "virtual-kubelet",
Resources: pod.ForgeContainerResources(opts.RequestsCPU, opts.LimitsCPU, opts.RequestsRAM, opts.LimitsRAM),
Image: vkImage,
Command: command,
Args: args,
Env: []v1.EnvVar{
{
Name: "POD_IP",
ValueFrom: &v1.EnvVarSource{FieldRef: &v1.ObjectFieldSelector{FieldPath: "status.podIP"}},
},
},
Ports: containerPorts,
},
}
}
func forgeVKPodSpec(
vkNamespace string,
homeCluster, remoteCluster *discoveryv1alpha1.ClusterIdentity, opts *VirtualKubeletOpts,
resourceOffer *sharingv1alpha1.ResourceOffer) v1.PodSpec {
nodeName := virtualKubelet.VirtualNodeName(remoteCluster)
return v1.PodSpec{
Containers: forgeVKContainers(opts.ContainerImage, homeCluster, remoteCluster,
nodeName, vkNamespace, opts, resourceOffer),
ServiceAccountName: vk.ServiceAccountName,
}
}
func stringifyArgument(key, value string) string {
return fmt.Sprintf("%s=%s", key, value)
}