-
Notifications
You must be signed in to change notification settings - Fork 30
Expand file tree
/
Copy pathmain.go
More file actions
166 lines (143 loc) · 6.34 KB
/
main.go
File metadata and controls
166 lines (143 loc) · 6.34 KB
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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
// Copyright © 2024 The vjailbreak authors
package main
import (
"context"
"fmt"
"os"
"strings"
"time"
"github.com/platform9/vjailbreak/v2v-helper/migrate"
"github.com/platform9/vjailbreak/v2v-helper/nbd"
"github.com/platform9/vjailbreak/v2v-helper/openstack"
"github.com/platform9/vjailbreak/v2v-helper/pkg/constants"
"github.com/platform9/vjailbreak/v2v-helper/pkg/utils"
"github.com/platform9/vjailbreak/v2v-helper/reporter"
"github.com/platform9/vjailbreak/v2v-helper/vcenter"
"github.com/platform9/vjailbreak/v2v-helper/vm"
)
func main() {
ctx, cancel := context.WithCancel(context.Background())
defer cancel() // Ensure context is canceled when we exit
// Initialize error reporter early
eventReporter, err := reporter.NewReporter()
if err != nil {
utils.PrintLog(fmt.Sprintf("Failed to create reporter: %v", err))
return
}
eventReporterChan := make(chan string)
podLabelWatcherChan := make(chan string)
ackChan := make(chan struct{})
defer close(eventReporterChan)
defer close(podLabelWatcherChan)
// Start reporter goroutines
eventReporter.UpdatePodEvents(ctx, eventReporterChan, ackChan)
eventReporter.WatchPodLabels(ctx, podLabelWatcherChan)
// Helper function to report and handle errors
handleError := func(msg string) {
if reporter.IsRunningInPod() {
eventReporterChan <- msg
// Wait for the reporter to process the message
<-ackChan
}
utils.PrintLog(msg)
}
client, err := utils.GetInclusterClient()
if err != nil {
handleError(fmt.Sprintf("Failed to get in-cluster client: %v", err))
}
migrationparams, err := utils.GetMigrationParams(ctx, client)
if err != nil {
handleError(fmt.Sprintf("Failed to get migration parameters: %v", err))
}
utils.WriteToLogFile(fmt.Sprintf("----- Migration started at %s for VM %s -----", time.Now().Format(time.RFC3339), migrationparams.SourceVMName))
var (
vCenterURL = strings.TrimSpace(os.Getenv("VCENTER_HOST"))
vCenterUserName = strings.TrimSpace(os.Getenv("VCENTER_USERNAME"))
vCenterPassword = strings.TrimSpace(os.Getenv("VCENTER_PASSWORD"))
vCenterInsecure = strings.EqualFold(strings.TrimSpace(os.Getenv("VCENTER_INSECURE")), constants.TrueString)
openstackInsecure = strings.EqualFold(strings.TrimSpace(os.Getenv("OS_INSECURE")), constants.TrueString)
)
openstackProjectName := strings.TrimSpace(os.Getenv("OS_PROJECT_NAME"))
if openstackProjectName == "" {
openstackProjectName = strings.TrimSpace(os.Getenv("OS_TENANT_NAME"))
}
starttime, _ := time.Parse(time.RFC3339, migrationparams.DataCopyStart)
cutstart, _ := time.Parse(time.RFC3339, migrationparams.VMcutoverStart)
cutend, _ := time.Parse(time.RFC3339, migrationparams.VMcutoverEnd)
// Validate vCenter connection
vcclient, err := vcenter.VCenterClientBuilder(ctx, vCenterUserName, vCenterPassword, vCenterURL, vCenterInsecure)
if err != nil {
handleError(fmt.Sprintf("Failed to validate vCenter connection: %v", err))
}
utils.PrintLog(fmt.Sprintf("Connected to vCenter: %s\n", vCenterURL))
defer vcclient.VCClient.CloseIdleConnections()
// Validate OpenStack connection
openstackclients, err := openstack.NewOpenStackClients(openstackInsecure)
if err != nil {
handleError(fmt.Sprintf("Failed to validate OpenStack connection: %v", err))
}
openstackclients.K8sClient = client
utils.PrintLog("Connected to OpenStack")
// Get thumbprint
thumbprint, err := vcenter.GetThumbprint(vCenterURL)
if err != nil {
handleError(fmt.Sprintf("Failed to get thumbprint: %s", err))
}
utils.PrintLog(fmt.Sprintf("VCenter Thumbprint: %s\n", thumbprint))
// Retrieve the source VM
vmops, err := vm.VMOpsBuilder(ctx, *vcclient, migrationparams.SourceVMName, client)
if err != nil {
handleError(fmt.Sprintf("Failed to get source VM: %v", err))
}
migrationobj := migrate.Migrate{
URL: vCenterURL,
UserName: vCenterUserName,
Password: vCenterPassword,
Insecure: vCenterInsecure,
Networknames: utils.RemoveEmptyStrings(strings.Split(migrationparams.OpenstackNetworkNames, ",")),
Networkports: utils.RemoveEmptyStrings(strings.Split(migrationparams.OpenstackNetworkPorts, ",")),
Volumetypes: utils.RemoveEmptyStrings(strings.Split(migrationparams.OpenstackVolumeTypes, ",")),
Virtiowin: migrationparams.OpenstackVirtioWin,
Ostype: migrationparams.OpenstackOSType,
Thumbprint: thumbprint,
Convert: migrationparams.OpenstackConvert,
DisconnectSourceNetwork: migrationparams.DisconnectSourceNetwork,
Openstackclients: openstackclients,
Vcclient: vcclient,
VMops: vmops,
Nbdops: []nbd.NBDOperations{},
EventReporter: eventReporterChan,
PodLabelWatcher: podLabelWatcherChan,
InPod: reporter.IsRunningInPod(),
MigrationTimes: migrate.MigrationTimes{
DataCopyStart: starttime,
VMCutoverStart: cutstart,
VMCutoverEnd: cutend,
},
MigrationType: migrationparams.MigrationType,
PerformHealthChecks: migrationparams.PerformHealthChecks,
HealthCheckPort: migrationparams.HealthCheckPort,
K8sClient: client,
TargetFlavorId: migrationparams.TARGET_FLAVOR_ID,
TargetAvailabilityZone: migrationparams.TargetAvailabilityZone,
AssignedIP: migrationparams.AssignedIP,
SecurityGroups: utils.RemoveEmptyStrings(strings.Split(migrationparams.SecurityGroups, ",")),
UseFlavorless: os.Getenv("USE_FLAVORLESS") == "true",
TenantName: openstackProjectName,
Reporter: eventReporter,
}
if err := migrationobj.MigrateVM(ctx); err != nil {
msg := fmt.Sprintf("Failed to migrate VM: %v", err)
// Try to power on the VM if migration failed
powerOnErr := vmops.VMPowerOn()
if powerOnErr != nil {
msg += fmt.Sprintf("\nAlso Failed to power on VM after migration failure: %v", powerOnErr)
} else {
msg += fmt.Sprintf("\nVM %s was powered on after migration failure", migrationparams.SourceVMName)
}
handleError(msg)
utils.PrintLog(fmt.Sprintf("----- Migration completed with errors at %s for VM %s -----", time.Now().Format(time.RFC3339), migrationparams.SourceVMName))
return
}
utils.PrintLog(fmt.Sprintf("----- Migration completed successfully at %s for VM %s -----", time.Now().Format(time.RFC3339), migrationparams.SourceVMName))
}