Skip to content

Commit 0a93a7d

Browse files
committed
api: Align APIs with CRDs to use integers instead of floats
1 parent ab4cd67 commit 0a93a7d

File tree

21 files changed

+274
-375
lines changed

21 files changed

+274
-375
lines changed

pkg/apis/controller/v1beta1/appwrapper.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -254,10 +254,10 @@ type AppWrapperStatus struct {
254254
// Resources consumed
255255

256256
// The number of CPU consumed by all pods belonging to the AppWrapper.
257-
TotalCPU int32 `json:"totalcpu,omitempty"`
257+
TotalCPU int64 `json:"totalcpu,omitempty"`
258258

259259
// The amount of memory consumed by all pods belonging to the AppWrapper.
260-
TotalMemory int32 `json:"totalmemory,omitempty"`
260+
TotalMemory int64 `json:"totalmemory,omitempty"`
261261

262262
// The total number of GPUs consumed by all pods belonging to the AppWrapper.
263263
TotalGPU int64 `json:"totalgpu,omitempty"`

pkg/apis/controller/v1beta1/queuejob.go

Lines changed: 2 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,4 @@
11
/*
2-
Copyright 2018 The Kubernetes Authors.
3-
4-
Licensed under the Apache License, Version 2.0 (the "License");
5-
you may not use this file except in compliance with the License.
6-
You may obtain a copy of the License at
7-
8-
http://www.apache.org/licenses/LICENSE-2.0
9-
10-
Unless required by applicable law or agreed to in writing, software
11-
distributed under the License is distributed on an "AS IS" BASIS,
12-
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13-
See the License for the specific language governing permissions and
14-
limitations under the License.
15-
*/
16-
/*
172
Copyright 2019, 2021 The Multi-Cluster App Dispatcher Authors.
183
194
Licensed under the Apache License, Version 2.0 (the "License");
@@ -28,6 +13,7 @@ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
2813
See the License for the specific language governing permissions and
2914
limitations under the License.
3015
*/
16+
3117
package v1beta1
3218

3319
import (
@@ -43,7 +29,7 @@ type QueueJob struct {
4329

4430
metav1.ObjectMeta `json:"metadata,omitempty" protobuf:"bytes,1,opt,name=metadata"`
4531

46-
// Specification of the desired behavior of a cron job, including the minAvailable
32+
// Specification of the desired behavior of a cron job
4733
Spec QueueJobSpec `json:"spec,omitempty" protobuf:"bytes,2,opt,name=spec"`
4834

4935
// Current status of QueueJob

pkg/controller/clusterstate/api/histogram_info.go

Lines changed: 24 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -13,71 +13,70 @@ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1313
See the License for the specific language governing permissions and
1414
limitations under the License.
1515
*/
16+
1617
package api
1718

1819
import (
19-
"github.com/prometheus/client_golang/prometheus"
20-
"math"
21-
2220
"k8s.io/klog/v2"
21+
22+
"github.com/prometheus/client_golang/prometheus"
2323
)
2424

2525
const (
26-
BucketCount = 20 //Must be > 0
27-
tolerance = 0.1
26+
BucketCount = 20 // Must be > 0
2827
)
28+
2929
type ResourceHistogram struct {
3030
MilliCPU *prometheus.Histogram
3131
Memory *prometheus.Histogram
3232
GPU *prometheus.Histogram
3333
}
3434

3535
func NewResourceHistogram(min *Resource, max *Resource) *ResourceHistogram {
36-
3736
start := max.MilliCPU
3837
width := 1.0
3938
count := 2
40-
diff := math.Abs(min.MilliCPU - max.MilliCPU)
41-
if diff >= tolerance {
39+
diff := max.MilliCPU - min.MilliCPU
40+
if diff > 0 {
4241
start = min.MilliCPU
43-
width = (diff/(BucketCount - 1))
42+
width = float64(diff) / (BucketCount - 1)
4443
count = BucketCount + 1
4544
}
4645
klog.V(10).Infof("[NewResourceHistogram] Start histogram numbers for CPU: start=%f, width=%f, count=%d",
4746
start, width, count)
4847
millicpuHist := prometheus.NewHistogram(prometheus.HistogramOpts{
49-
Name: "millicpu",
50-
Buckets: prometheus.LinearBuckets(start, width, count),})
48+
Name: "millicpu",
49+
Buckets: prometheus.LinearBuckets(float64(start), width, count)})
5150

5251
start = max.Memory
5352
width = 1.0
5453
count = 2
55-
diff = math.Abs(min.Memory - max.Memory)
56-
if diff >= tolerance {
54+
diff = max.Memory - min.Memory
55+
if diff > 0 {
5756
start = min.Memory
58-
width = (diff/(BucketCount - 1))
57+
width = float64(diff) / (BucketCount - 1)
5958
count = BucketCount + 1
6059
}
6160
klog.V(10).Infof("[NewResourceHistogram] Start histogram numbers for Memory: start=%f, width=%f, count=%d",
6261
start, width, count)
6362
memoryHist := prometheus.NewHistogram(prometheus.HistogramOpts{
64-
Name: "memory",
65-
Buckets: prometheus.LinearBuckets(start, width, count),})
63+
Name: "memory",
64+
Buckets: prometheus.LinearBuckets(float64(start), width, count)})
6665

67-
start = float64(max.GPU)
66+
start = max.GPU
6867
width = 1.0
6968
count = 2
70-
diff = math.Abs(float64(min.GPU - max.GPU))
71-
if diff >= tolerance {
72-
start = float64(min.GPU)
73-
width = (diff/(BucketCount - 1))
69+
diff = max.GPU - min.GPU
70+
if diff >= 0 {
71+
start = min.GPU
72+
width = float64(diff) / (BucketCount - 1)
7473
count = BucketCount + 1
7574
}
7675
klog.V(10).Infof("[NewResourceHistogram] Start histogram numbers for GPU: start=%f, width=%f, count=%d",
7776
start, width, count)
7877
gpuHist := prometheus.NewHistogram(prometheus.HistogramOpts{
79-
Name: "gpu",
80-
Buckets: prometheus.LinearBuckets(start, width, count),})
78+
Name: "gpu",
79+
Buckets: prometheus.LinearBuckets(float64(start), width, count)})
8180

8281
rh := &ResourceHistogram{
8382
MilliCPU: &millicpuHist,
@@ -88,9 +87,7 @@ func NewResourceHistogram(min *Resource, max *Resource) *ResourceHistogram {
8887
}
8988

9089
func (rh *ResourceHistogram) Observer(r *Resource) {
91-
(*rh.MilliCPU).Observe(r.MilliCPU)
92-
(*rh.Memory).Observe(r.Memory)
90+
(*rh.MilliCPU).Observe(float64(r.MilliCPU))
91+
(*rh.Memory).Observe(float64(r.Memory))
9392
(*rh.GPU).Observe(float64(r.GPU))
9493
}
95-
96-

pkg/controller/clusterstate/api/resource_info.go

Lines changed: 15 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,4 @@
11
/*
2-
Copyright 2017 The Kubernetes Authors.
3-
4-
Licensed under the Apache License, Version 2.0 (the "License");
5-
you may not use this file except in compliance with the License.
6-
You may obtain a copy of the License at
7-
8-
http://www.apache.org/licenses/LICENSE-2.0
9-
10-
Unless required by applicable law or agreed to in writing, software
11-
distributed under the License is distributed on an "AS IS" BASIS,
12-
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13-
See the License for the specific language governing permissions and
14-
limitations under the License.
15-
*/
16-
/*
172
Copyright 2019, 2021 The Multi-Cluster App Dispatcher Authors.
183
194
Licensed under the Apache License, Version 2.0 (the "License");
@@ -28,18 +13,18 @@ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
2813
See the License for the specific language governing permissions and
2914
limitations under the License.
3015
*/
16+
3117
package api
3218

3319
import (
3420
"fmt"
35-
"math"
3621

3722
v1 "k8s.io/api/core/v1"
3823
)
3924

4025
type Resource struct {
41-
MilliCPU float64
42-
Memory float64
26+
MilliCPU int64
27+
Memory int64
4328
GPU int64
4429
}
4530

@@ -65,17 +50,17 @@ func (r *Resource) Clone() *Resource {
6550
return clone
6651
}
6752

68-
var minMilliCPU float64 = 10
69-
var minMemory float64 = 10 * 1024 * 1024
53+
var minMilliCPU int64 = 10
54+
var minMemory int64 = 10 * 1024 * 1024
7055

7156
func NewResource(rl v1.ResourceList) *Resource {
7257
r := EmptyResource()
7358
for rName, rQuant := range rl {
7459
switch rName {
7560
case v1.ResourceCPU:
76-
r.MilliCPU += float64(rQuant.MilliValue())
61+
r.MilliCPU += rQuant.MilliValue()
7762
case v1.ResourceMemory:
78-
r.Memory += float64(rQuant.Value())
63+
r.Memory += rQuant.Value()
7964
case GPUResourceName:
8065
q, _ := rQuant.AsInt64()
8166
r.GPU += q
@@ -116,12 +101,12 @@ func (r *Resource) Replace(rr *Resource) *Resource {
116101
return r
117102
}
118103

119-
//Sub subtracts two Resource objects.
104+
// Sub subtracts two Resource objects.
120105
func (r *Resource) Sub(rr *Resource) (*Resource, error) {
121106
return r.NonNegSub(rr)
122107
}
123108

124-
//Sub subtracts two Resource objects and return zero for negative subtractions.
109+
// Sub subtracts two Resource objects and return zero for negative subtractions.
125110
func (r *Resource) NonNegSub(rr *Resource) (*Resource, error) {
126111
// Check for negative calculation
127112
var isNegative bool
@@ -164,24 +149,24 @@ func (r *Resource) Less(rr *Resource) bool {
164149
}
165150

166151
func (r *Resource) LessEqual(rr *Resource) bool {
167-
return (r.MilliCPU < rr.MilliCPU || math.Abs(rr.MilliCPU-r.MilliCPU) < 0.01) &&
168-
(r.Memory < rr.Memory || math.Abs(rr.Memory-r.Memory) < 1) &&
169-
(r.GPU <= rr.GPU)
152+
return r.MilliCPU < rr.MilliCPU &&
153+
r.Memory < rr.Memory &&
154+
r.GPU <= rr.GPU
170155
}
171156

172157
func (r *Resource) String() string {
173-
return fmt.Sprintf("cpu %0.2f, memory %0.2f, GPU %d",
158+
return fmt.Sprintf("cpu %d, memory %d, GPU %d",
174159
r.MilliCPU, r.Memory, r.GPU)
175160
}
176161

177-
func (r *Resource) Get(rn v1.ResourceName) (float64, error) {
162+
func (r *Resource) Get(rn v1.ResourceName) (int64, error) {
178163
switch rn {
179164
case v1.ResourceCPU:
180165
return r.MilliCPU, nil
181166
case v1.ResourceMemory:
182167
return r.Memory, nil
183168
case GPUResourceName:
184-
return float64(r.GPU), nil
169+
return r.GPU, nil
185170
default:
186171
err := fmt.Errorf("resource not supported %v", rn)
187172
return 0.0, err

pkg/controller/metrics/adapter/provider/provider.go

Lines changed: 8 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,4 @@
11
/*
2-
Copyright 2017 The Kubernetes Authors.
3-
4-
Licensed under the Apache License, Version 2.0 (the "License");
5-
you may not use this file except in compliance with the License.
6-
You may obtain a copy of the License at
7-
8-
http://www.apache.org/licenses/LICENSE-2.0
9-
10-
Unless required by applicable law or agreed to in writing, software
11-
distributed under the License is distributed on an "AS IS" BASIS,
12-
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13-
See the License for the specific language governing permissions and
14-
limitations under the License.
15-
*/
16-
/*
172
Copyright 2019, 2021 The Multi-Cluster App Dispatcher Authors.
183
194
Licensed under the Apache License, Version 2.0 (the "License");
@@ -28,6 +13,7 @@ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
2813
See the License for the specific language governing permissions and
2914
limitations under the License.
3015
*/
16+
3117
package provider
3218

3319
import (
@@ -37,7 +23,6 @@ import (
3723
"time"
3824

3925
"github.com/emicklei/go-restful"
40-
"k8s.io/klog/v2"
4126

4227
apierr "k8s.io/apimachinery/pkg/api/errors"
4328
apimeta "k8s.io/apimachinery/pkg/api/meta"
@@ -47,12 +32,13 @@ import (
4732
"k8s.io/apimachinery/pkg/runtime/schema"
4833
"k8s.io/apimachinery/pkg/types"
4934
"k8s.io/client-go/dynamic"
35+
"k8s.io/klog/v2"
5036
"k8s.io/metrics/pkg/apis/custom_metrics"
5137
"k8s.io/metrics/pkg/apis/external_metrics"
5238

53-
clusterstatecache "github.com/project-codeflare/multi-cluster-app-dispatcher/pkg/controller/clusterstate/cache"
5439
"github.com/kubernetes-sigs/custom-metrics-apiserver/pkg/provider"
5540
"github.com/kubernetes-sigs/custom-metrics-apiserver/pkg/provider/helpers"
41+
clusterstatecache "github.com/project-codeflare/multi-cluster-app-dispatcher/pkg/controller/clusterstate/cache"
5642
)
5743

5844
// CustomMetricResource wraps provider.CustomMetricInfo in a struct which stores the Name and Namespace of the resource
@@ -157,7 +143,7 @@ func NewFakeProvider(client dynamic.Interface, mapper apimeta.RESTMapper, cluste
157143
// webService creates a restful.WebService with routes set up for receiving fake metrics
158144
// These writing routes have been set up to be identical to the format of routes which metrics are read from.
159145
// There are 3 metric types available: namespaced, root-scoped, and namespaces.
160-
// (Note: Namespaces, we're assuming, are themselves namespaced resources, but for consistency with how metrics are retreived they have a separate route)
146+
// (Note: Namespaces, we're assuming, are themselves namespaced resources, but for consistency with how metrics are retrieved they have a separate route)
161147
func (p *clusterMetricsProvider) webService() *restful.WebService {
162148
klog.V(10).Infof("Entered webService()")
163149
ws := new(restful.WebService)
@@ -360,7 +346,7 @@ func (p *clusterMetricsProvider) GetExternalMetric(namespace string, metricSelec
360346
p.valuesLock.RLock()
361347
defer p.valuesLock.RUnlock()
362348

363-
matchingMetrics := []external_metrics.ExternalMetricValue{}
349+
var matchingMetrics []external_metrics.ExternalMetricValue
364350
for _, metric := range p.externalMetrics {
365351
klog.V(9).Infof("[GetExternalMetric] externalMetricsInfo: %s, externalMetricValue: %v, externalMetricLabels: %v ",
366352
metric.info.Metric, metric.Value, metric.labels)
@@ -374,15 +360,15 @@ func (p *clusterMetricsProvider) GetExternalMetric(namespace string, metricSelec
374360
klog.V(9).Infof("[GetExternalMetric] Cache resources: %v", resources)
375361

376362
klog.V(10).Infof("[GetExternalMetric] Setting memory metric Value: %f.", resources.Memory)
377-
metricValue.Value = *resource.NewQuantity(int64(resources.Memory), resource.DecimalSI)
378-
//metricValue.Value = *resource.NewQuantity(4500000000, resource.DecimalSI)
363+
metricValue.Value = *resource.NewQuantity(resources.Memory, resource.DecimalSI)
364+
// metricValue.Value = *resource.NewQuantity(4500000000, resource.DecimalSI)
379365
} else if strings.Compare(labelVal, "cpu") == 0 {
380366
// Set cpu Value
381367
resources := p.cache2.GetUnallocatedResources()
382368
klog.V(9).Infof("[GetExternalMetric] Cache resources: %f", resources)
383369

384370
klog.V(10).Infof("[GetExternalMetric] Setting cpu metric Value: %v.", resources.MilliCPU)
385-
metricValue.Value = *resource.NewQuantity(int64(resources.MilliCPU), resource.DecimalSI)
371+
metricValue.Value = *resource.NewQuantity(resources.MilliCPU, resource.DecimalSI)
386372
} else if strings.Compare(labelVal, "gpu") == 0 {
387373
// Set gpu Value
388374
resources := p.cache2.GetUnallocatedResources()

0 commit comments

Comments
 (0)