-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add full AZ awareness to capacity plugins
This is the first half of a refactor of the scrape methods towards full AZ awareness. Scraping can return data either for the entire region, or broken down by AZs. This distinction is represented by the enum type Topological. For capacity scraping, this does not present a huge change, because several capacitors already report capacity by AZ. What changes is that: - subcapacities are now reported on the AZ (instead of regional) level - the region-wide capacity is computed outside of the plugin by summing the individual AZ capacities The frontend does not change in any way at this point. These changes will come later, once the user-visible changes have been agreed upon. This changeset is about preparing the things that we will definitely need, and making data available to the collector that we will need to surface in the frontend later on.
- Loading branch information
Showing
11 changed files
with
218 additions
and
181 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
/******************************************************************************* | ||
* | ||
* Copyright 2023 SAP SE | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You should have received a copy of the License along with this | ||
* program. If not, you may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
* | ||
*******************************************************************************/ | ||
|
||
package core | ||
|
||
import ( | ||
"slices" | ||
"sort" | ||
) | ||
|
||
// Topological is a container for data that can either be reported for | ||
// the entire region at once, or separately by AZ. | ||
// Exactly one field shall be non-nil. | ||
type Topological[D TopologicalData[D]] struct { | ||
Regional *D | ||
PerAZ map[string]*D | ||
} | ||
|
||
// Regional is a shorthand to construct a Topological instance with the Regional member filled. | ||
func Regional[D TopologicalData[D]](data D) Topological[D] { | ||
return Topological[D]{Regional: &data} | ||
} | ||
|
||
// PerAZ is a shorthand to construct a Topological instance with the PerAZ member filled. | ||
func PerAZ[D TopologicalData[D]](data map[string]*D) Topological[D] { | ||
return Topological[D]{PerAZ: data} | ||
} | ||
|
||
// Sum returns a sum of all data in this container. | ||
// If the Regional field is filled, that data is returned directly. | ||
// Otherwise, all entries in the PerAZ field are summed together. | ||
func (t Topological[D]) Sum() D { | ||
if t.PerAZ == nil { | ||
return *t.Regional | ||
} | ||
|
||
//fold AZ data in a well-defined order for deterministic test result | ||
azNames := make([]string, 0, len(t.PerAZ)) | ||
for az := range t.PerAZ { | ||
azNames = append(azNames, az) | ||
} | ||
sort.Strings(azNames) | ||
|
||
var result D | ||
for _, az := range azNames { | ||
result = result.add(*t.PerAZ[az]) | ||
} | ||
return result | ||
} | ||
|
||
// TopologicalData is an interfaces for types that can be put into the Topological container. | ||
type TopologicalData[Self any] interface { | ||
// List of permitted types. This is required for type inference, as explained here: | ||
// <https://stackoverflow.com/a/73851453> | ||
CapacityData | ||
|
||
// Computes the sum of this structure and `other`. | ||
// This is used to implement Topological.Sum(). | ||
add(other Self) Self | ||
} | ||
|
||
// CapacityData contains capacity data for a single project resource. | ||
type CapacityData struct { | ||
Capacity uint64 | ||
Usage uint64 //NOTE: currently only relevant on AZ level, regional level uses the aggregation of project usages | ||
Subcapacities []any //only if supported by plugin and enabled in config | ||
} | ||
|
||
// add implements the TopologicalData interface. | ||
// | ||
//nolint:unused // looks like a linter bug | ||
func (d CapacityData) add(other CapacityData) CapacityData { | ||
return CapacityData{ | ||
Capacity: d.Capacity + other.Capacity, | ||
Usage: d.Usage + other.Usage, | ||
Subcapacities: append(slices.Clone(d.Subcapacities), other.Subcapacities...), | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.