/
configmap.go
87 lines (71 loc) · 2.91 KB
/
configmap.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
/*
Copyright 2020 The Kubermatic Kubernetes Platform contributors.
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 openvpn
import (
"fmt"
"net"
"strings"
kubermaticv1 "github.com/kubermatic/kubermatic/api/pkg/crd/kubermatic/v1"
"github.com/kubermatic/kubermatic/api/pkg/resources"
"github.com/kubermatic/kubermatic/api/pkg/resources/reconciling"
corev1 "k8s.io/api/core/v1"
)
type serverClientConfigsData interface {
Cluster() *kubermaticv1.Cluster
NodeAccessNetwork() string
}
// ServerClientConfigsConfigMapCreator returns a ConfigMap containing the ClientConfig for the OpenVPN server. It lives inside the seed-cluster
func ServerClientConfigsConfigMapCreator(data serverClientConfigsData) reconciling.NamedConfigMapCreatorGetter {
return func() (string, reconciling.ConfigMapCreator) {
return resources.OpenVPNClientConfigsConfigMapName, func(cm *corev1.ConfigMap) (*corev1.ConfigMap, error) {
cm.Labels = resources.BaseAppLabels(name, nil)
var iroutes []string
// iroute for pod network
if len(data.Cluster().Spec.ClusterNetwork.Pods.CIDRBlocks) < 1 {
return nil, fmt.Errorf("cluster.Spec.ClusterNetwork.Pods.CIDRBlocks must contain at least one entry")
}
_, podNet, err := net.ParseCIDR(data.Cluster().Spec.ClusterNetwork.Pods.CIDRBlocks[0])
if err != nil {
return nil, err
}
iroutes = append(iroutes, fmt.Sprintf("iroute %s %s",
podNet.IP.String(),
net.IP(podNet.Mask).String()))
// iroute for service network
if len(data.Cluster().Spec.ClusterNetwork.Services.CIDRBlocks) < 1 {
return nil, fmt.Errorf("cluster.Spec.ClusterNetwork.Services.CIDRBlocks must contain at least one entry")
}
_, serviceNet, err := net.ParseCIDR(data.Cluster().Spec.ClusterNetwork.Services.CIDRBlocks[0])
if err != nil {
return nil, err
}
iroutes = append(iroutes, fmt.Sprintf("iroute %s %s",
serviceNet.IP.String(),
net.IP(serviceNet.Mask).String()))
_, nodeAccessNetwork, err := net.ParseCIDR(data.NodeAccessNetwork())
if err != nil {
return nil, fmt.Errorf("failed to parse node access network %s: %v", data.NodeAccessNetwork(), err)
}
iroutes = append(iroutes, fmt.Sprintf("iroute %s %s",
nodeAccessNetwork.IP.String(),
net.IP(nodeAccessNetwork.Mask).String()))
if cm.Data == nil {
cm.Data = map[string]string{}
}
// trailing newline
iroutes = append(iroutes, "")
cm.Data["user-cluster-client"] = strings.Join(iroutes, "\n")
return cm, nil
}
}
}