-
Notifications
You must be signed in to change notification settings - Fork 396
/
render.go
113 lines (97 loc) · 2.93 KB
/
render.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
package operator
import (
"bytes"
"fmt"
"net"
"text/template"
"github.com/Masterminds/sprig"
"github.com/apparentlymart/go-cidr/cidr"
"github.com/ghodss/yaml"
configv1 "github.com/openshift/api/config/v1"
mcfgv1 "github.com/openshift/machine-config-operator/pkg/apis/machineconfiguration.openshift.io/v1"
"github.com/openshift/machine-config-operator/pkg/operator/assets"
)
type renderConfig struct {
TargetNamespace string
Version string
ControllerConfig mcfgv1.ControllerConfigSpec
APIServerURL string
Images *RenderConfigImages
KubeAPIServerServingCA string
}
func renderAsset(config *renderConfig, path string) ([]byte, error) {
objBytes, err := assets.Asset(path)
if err != nil {
return nil, fmt.Errorf("error getting asset %s: %v", path, err)
}
funcs := sprig.TxtFuncMap()
funcs["toYAML"] = toYAML
tmpl, err := template.New(path).Funcs(funcs).Parse(string(objBytes))
if err != nil {
return nil, fmt.Errorf("failed to parse asset %s: %v", path, err)
}
buf := new(bytes.Buffer)
if err := tmpl.Execute(buf, config); err != nil {
return nil, fmt.Errorf("failed to execute template: %v", err)
}
return buf.Bytes(), nil
}
func toYAML(i interface{}) []byte {
out, err := yaml.Marshal(i)
if err != nil {
panic(err)
}
return out
}
// createDiscoveredControllerConfigSpec uses the Infrastructure and Network global configuration to discover various
// fields for the controller spec.
// Infrastructure provides information about the platform, etcd discovery domain.
// Network provides the service network that is used to calculate the cluster DNS IP.
func createDiscoveredControllerConfigSpec(infra *configv1.Infrastructure, network *configv1.Network, proxy *configv1.Proxy) (*mcfgv1.ControllerConfigSpec, error) {
if len(network.Spec.ServiceNetwork) == 0 {
return nil, fmt.Errorf("service cidr is empty in Network")
}
dnsIP, err := clusterDNSIP(network.Spec.ServiceNetwork[0])
if err != nil {
return nil, err
}
platform := "none"
//nolint:staticcheck
switch infra.Status.Platform {
case configv1.AWSPlatformType:
platform = "aws"
case configv1.AzurePlatformType:
platform = "azure"
case configv1.BareMetalPlatformType:
platform = "baremetal"
case configv1.GCPPlatformType:
platform = "gcp"
case configv1.OpenStackPlatformType:
platform = "openstack"
case configv1.LibvirtPlatformType:
platform = "libvirt"
case configv1.VSpherePlatformType:
platform = "vsphere"
}
ccSpec := &mcfgv1.ControllerConfigSpec{
ClusterDNSIP: dnsIP,
CloudProviderConfig: "",
EtcdDiscoveryDomain: infra.Status.EtcdDiscoveryDomain,
Platform: platform,
}
if proxy != nil {
ccSpec.Proxy = &proxy.Status
}
return ccSpec, nil
}
func clusterDNSIP(iprange string) (string, error) {
_, network, err := net.ParseCIDR(iprange)
if err != nil {
return "", err
}
ip, err := cidr.Host(network, 10)
if err != nil {
return "", err
}
return ip.String(), nil
}