-
Notifications
You must be signed in to change notification settings - Fork 1.4k
/
Copy pathmachine-api-operator.go
147 lines (127 loc) · 4.61 KB
/
machine-api-operator.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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
package manifests
import (
"context"
"github.com/ghodss/yaml"
"github.com/pkg/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"github.com/openshift/installer/pkg/asset"
"github.com/openshift/installer/pkg/asset/installconfig"
"github.com/openshift/installer/pkg/asset/tls"
"github.com/openshift/installer/pkg/rhcos"
)
const (
maoTargetNamespace = "openshift-cluster-api"
// DefaultChannel is the default RHCOS channel for the cluster.
DefaultChannel = "tested"
)
// machineAPIOperator generates the network-operator-*.yml files
type machineAPIOperator struct {
Config *maoOperatorConfig
File *asset.File
}
var _ asset.WritableAsset = (*machineAPIOperator)(nil)
// maoOperatorConfig contains configuration for mao managed stack
// TODO(enxebre): move up to github.com/coreos/tectonic-config (to install-config? /rchopra)
type maoOperatorConfig struct {
metav1.TypeMeta `json:",inline"`
TargetNamespace string `json:"targetNamespace"`
APIServiceCA string `json:"apiServiceCA"`
Provider string `json:"provider"`
AWS *awsConfig `json:"aws"`
Libvirt *libvirtConfig `json:"libvirt"`
OpenStack *openstackConfig `json:"openstack"`
}
type libvirtConfig struct {
ClusterName string `json:"clusterName"`
URI string `json:"uri"`
NetworkName string `json:"networkName"`
IPRange string `json:"iprange"`
Replicas int `json:"replicas"`
}
type awsConfig struct {
ClusterName string `json:"clusterName"`
ClusterID string `json:"clusterID"`
Region string `json:"region"`
AvailabilityZone string `json:"availabilityZone"`
Image string `json:"image"`
Replicas int `json:"replicas"`
}
type openstackConfig struct {
ClusterName string `json:"clusterName"`
ClusterID string `json:"clusterID"`
Region string `json:"region"`
Replicas int `json:"replicas"`
}
// Name returns a human friendly name for the operator
func (mao *machineAPIOperator) Name() string {
return "Machine API Operator"
}
// Dependencies returns all of the dependencies directly needed by an
// machineAPIOperator asset.
func (mao *machineAPIOperator) Dependencies() []asset.Asset {
return []asset.Asset{
&installconfig.InstallConfig{},
&tls.AggregatorCA{},
}
}
// Generate generates the network-operator-config.yml and network-operator-manifest.yml files
func (mao *machineAPIOperator) Generate(dependencies asset.Parents) error {
installConfig := &installconfig.InstallConfig{}
aggregatorCA := &tls.AggregatorCA{}
dependencies.Get(installConfig, aggregatorCA)
mao.Config = &maoOperatorConfig{
TypeMeta: metav1.TypeMeta{
APIVersion: "v1",
Kind: "machineAPIOperatorConfig",
},
TargetNamespace: maoTargetNamespace,
APIServiceCA: string(aggregatorCA.Cert()),
Provider: tectonicCloudProvider(installConfig.Config.Platform),
}
switch {
case installConfig.Config.Platform.AWS != nil:
var ami string
ami, err := rhcos.AMI(context.TODO(), DefaultChannel, installConfig.Config.Platform.AWS.Region)
if err != nil {
return errors.Wrapf(err, "failed to get AMI for %s config", mao.Name())
}
mao.Config.AWS = &awsConfig{
ClusterName: installConfig.Config.ObjectMeta.Name,
ClusterID: installConfig.Config.ClusterID,
Region: installConfig.Config.Platform.AWS.Region,
AvailabilityZone: "",
Image: ami,
Replicas: int(*installConfig.Config.Machines[1].Replicas),
}
case installConfig.Config.Platform.Libvirt != nil:
mao.Config.Libvirt = &libvirtConfig{
ClusterName: installConfig.Config.ObjectMeta.Name,
URI: installConfig.Config.Platform.Libvirt.URI,
NetworkName: installConfig.Config.Platform.Libvirt.Network.Name,
IPRange: installConfig.Config.Platform.Libvirt.Network.IPRange,
Replicas: int(*installConfig.Config.Machines[1].Replicas),
}
case installConfig.Config.Platform.OpenStack != nil:
mao.Config.OpenStack = &openstackConfig{
ClusterName: installConfig.Config.ObjectMeta.Name,
ClusterID: installConfig.Config.ClusterID,
Region: installConfig.Config.Platform.OpenStack.Region,
Replicas: int(*installConfig.Config.Machines[1].Replicas),
}
default:
return errors.Errorf("unknown provider for machine-api-operator")
}
data, err := yaml.Marshal(mao.Config)
if err != nil {
return errors.Wrapf(err, "failed to marshal %s config", mao.Name())
}
mao.File = &asset.File{
Filename: "machine-api-operator-config.yml",
Data: data,
}
return nil
}
// Files returns the files generated by the asset.
func (mao *machineAPIOperator) Files() []*asset.File {
return []*asset.File{mao.File}
}