-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnode.go
102 lines (93 loc) · 3.04 KB
/
node.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
package main
import (
"context"
"encoding/json"
"fmt"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/utils/pointer"
ctrlruntimeclient "sigs.k8s.io/controller-runtime/pkg/client"
clusterv1alpha1 "github.com/kubermatic/machine-controller/pkg/apis/cluster/v1alpha1"
gce "github.com/kubermatic/machine-controller/pkg/cloudprovider/provider/gce/types"
providerconfig "github.com/kubermatic/machine-controller/pkg/providerconfig/types"
apiv1 "k8c.io/kubermatic/v2/pkg/api/v1"
kubermaticv1 "k8c.io/kubermatic/v2/pkg/apis/kubermatic/v1"
)
// CreateMachineDeployment creates a user cluster node
func CreateMachineDeployment(ctx context.Context, client ctrlruntimeclient.Client, name, network, subnetwork string, cluster *kubermaticv1.Cluster) error {
// GCE settings
cloudConfig, err := json.Marshal(gce.CloudProviderSpec{
Zone: providerconfig.ConfigVarString{Value: "europe-west2-a"},
MachineType: providerconfig.ConfigVarString{Value: "e2-highcpu-2"},
DiskSize: 25,
DiskType: providerconfig.ConfigVarString{Value: "pd-standard"},
Preemptible: providerconfig.ConfigVarBool{Value: pointer.Bool(false)},
Network: providerconfig.ConfigVarString{Value: network},
Subnetwork: providerconfig.ConfigVarString{Value: subnetwork},
AssignPublicIPAddress: &providerconfig.ConfigVarBool{Value: pointer.Bool(true)},
MultiZone: providerconfig.ConfigVarBool{Value: pointer.Bool(false)},
Regional: providerconfig.ConfigVarBool{Value: pointer.Bool(false)},
Tags: []string{fmt.Sprintf("kubernetes-cluster-%s", cluster.Name)},
})
if err != nil {
return err
}
// OS settings
osConfig, err := json.Marshal(apiv1.OperatingSystemSpec{
Ubuntu: &apiv1.UbuntuSpec{
DistUpgradeOnBoot: false,
},
})
if err != nil {
return err
}
// Provider settings
providerConfig, err := json.Marshal(providerconfig.Config{
CloudProvider: "gce",
CloudProviderSpec: runtime.RawExtension{
Raw: cloudConfig,
},
OperatingSystem: "ubuntu",
OperatingSystemSpec: runtime.RawExtension{
Raw: osConfig,
},
})
if err != nil {
return err
}
machineDeployment := clusterv1alpha1.MachineDeployment{
ObjectMeta: metav1.ObjectMeta{
Name: name,
Namespace: "kube-system",
},
Spec: clusterv1alpha1.MachineDeploymentSpec{
Replicas: pointer.Int32(2),
Selector: metav1.LabelSelector{
MatchLabels: map[string]string{
"machine": name,
},
},
Template: clusterv1alpha1.MachineTemplateSpec{
ObjectMeta: metav1.ObjectMeta{
Labels: map[string]string{
"machine": name,
},
},
Spec: clusterv1alpha1.MachineSpec{
ProviderSpec: clusterv1alpha1.ProviderSpec{
Value: &runtime.RawExtension{
Raw: providerConfig,
},
},
Versions: clusterv1alpha1.MachineVersionInfo{
Kubelet: cluster.Spec.Version.String(),
},
},
},
},
}
if err := client.Create(ctx, &machineDeployment); err != nil {
return err
}
return nil
}