|
| 1 | +// Copyright 2025 SAP SE |
| 2 | +// SPDX-License-Identifier: Apache-2.0 |
| 3 | + |
| 4 | +package checks |
| 5 | + |
| 6 | +import ( |
| 7 | + "bytes" |
| 8 | + "context" |
| 9 | + "encoding/json" |
| 10 | + "log/slog" |
| 11 | + "net/http" |
| 12 | + "strconv" |
| 13 | + |
| 14 | + "github.com/cobaltcore-dev/cortex/internal/conf" |
| 15 | + httpapi "github.com/cobaltcore-dev/cortex/internal/scheduler/api/http" |
| 16 | + cortexopenstack "github.com/cobaltcore-dev/cortex/internal/sync/openstack" |
| 17 | + "github.com/gophercloud/gophercloud/v2" |
| 18 | + "github.com/gophercloud/gophercloud/v2/openstack" |
| 19 | + "github.com/gophercloud/gophercloud/v2/openstack/compute/v2/hypervisors" |
| 20 | + "github.com/sapcc/go-bits/must" |
| 21 | +) |
| 22 | + |
| 23 | +// Run all checks. |
| 24 | +func RunChecks(ctx context.Context, config conf.Config) { |
| 25 | + checkSchedulerReturnsValidHosts(ctx, config) |
| 26 | +} |
| 27 | + |
| 28 | +// Check that the scheduler returns a valid set of hosts. |
| 29 | +func checkSchedulerReturnsValidHosts(ctx context.Context, config conf.Config) { |
| 30 | + osConf := config.GetSyncConfig().OpenStack |
| 31 | + slog.Info("authenticating against openstack", "url", osConf.Keystone.URL) |
| 32 | + authOptions := gophercloud.AuthOptions{ |
| 33 | + IdentityEndpoint: osConf.Keystone.URL, |
| 34 | + Username: osConf.Keystone.OSUsername, |
| 35 | + DomainName: osConf.Keystone.OSUserDomainName, |
| 36 | + Password: osConf.Keystone.OSPassword, |
| 37 | + AllowReauth: true, |
| 38 | + Scope: &gophercloud.AuthScope{ |
| 39 | + ProjectName: osConf.Keystone.OSProjectName, |
| 40 | + DomainName: osConf.Keystone.OSProjectDomainName, |
| 41 | + }, |
| 42 | + } |
| 43 | + pc := must.Return(openstack.NewClient(authOptions.IdentityEndpoint)) |
| 44 | + must.Succeed(openstack.Authenticate(ctx, pc, authOptions)) |
| 45 | + url := must.Return(pc.EndpointLocator(gophercloud.EndpointOpts{ |
| 46 | + Type: "compute", |
| 47 | + Availability: gophercloud.Availability(osConf.Nova.Availability), |
| 48 | + })) |
| 49 | + sc := &gophercloud.ServiceClient{ProviderClient: pc, Endpoint: url, Type: "compute"} |
| 50 | + slog.Info("authenticated against openstack", "url", url) |
| 51 | + slog.Info("listing hypervisors") |
| 52 | + pages := must.Return(hypervisors.List(sc, hypervisors.ListOpts{}).AllPages(ctx)) |
| 53 | + var data = &struct { |
| 54 | + Hypervisors []cortexopenstack.Hypervisor `json:"hypervisors"` |
| 55 | + }{} |
| 56 | + must.Succeed(pages.(hypervisors.HypervisorPage).ExtractInto(data)) |
| 57 | + if len(data.Hypervisors) == 0 { |
| 58 | + panic("no hypervisors found") |
| 59 | + } |
| 60 | + slog.Info("found hypervisors", "count", len(data.Hypervisors)) |
| 61 | + |
| 62 | + var hosts []httpapi.ExternalSchedulerHost |
| 63 | + weights := make(map[string]float64) |
| 64 | + for _, h := range data.Hypervisors { |
| 65 | + weights[h.ServiceHost] = 1.0 |
| 66 | + hosts = append(hosts, httpapi.ExternalSchedulerHost{ |
| 67 | + ComputeHost: h.ServiceHost, |
| 68 | + HypervisorHostname: h.Hostname, |
| 69 | + }) |
| 70 | + } |
| 71 | + request := httpapi.ExternalSchedulerRequest{ |
| 72 | + Hosts: hosts, |
| 73 | + Weights: weights, |
| 74 | + } |
| 75 | + port := strconv.Itoa(config.GetSchedulerConfig().API.Port) |
| 76 | + apiURL := "http://cortex-scheduler:" + port + "/scheduler/nova/external" |
| 77 | + slog.Info("sending request to external scheduler", "apiURL", apiURL) |
| 78 | + |
| 79 | + requestBody := must.Return(json.Marshal(request)) |
| 80 | + buf := bytes.NewBuffer(requestBody) |
| 81 | + req := must.Return(http.NewRequestWithContext(ctx, http.MethodPost, apiURL, buf)) |
| 82 | + req.Header.Set("Content-Type", "application/json") |
| 83 | + //nolint:bodyclose // We don't care about the body here. |
| 84 | + respRaw := must.Return(http.DefaultClient.Do(req)) |
| 85 | + defer respRaw.Body.Close() |
| 86 | + if respRaw.StatusCode != http.StatusOK { |
| 87 | + panic("external scheduler API returned non-200 status code") |
| 88 | + } |
| 89 | + var resp httpapi.ExternalSchedulerResponse |
| 90 | + must.Succeed(json.NewDecoder(respRaw.Body).Decode(&resp)) |
| 91 | + if len(resp.Hosts) == 0 { |
| 92 | + panic("no hosts found in response") |
| 93 | + } |
| 94 | + slog.Info("check successful, got hosts", "count", len(resp.Hosts)) |
| 95 | +} |
0 commit comments