-
Notifications
You must be signed in to change notification settings - Fork 1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
14 changed files
with
711 additions
and
105 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
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,146 @@ | ||
/* | ||
Copyright 2018 The Volcano Authors. | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
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 api | ||
|
||
import ( | ||
"fmt" | ||
|
||
"github.com/golang/glog" | ||
|
||
v1 "k8s.io/api/core/v1" | ||
"k8s.io/client-go/tools/cache" | ||
) | ||
|
||
// NamespaceName is name of namespace | ||
type NamespaceName string | ||
|
||
const ( | ||
// NamespaceWeightKey is the key in ResourceQuota.spec.hard indicating the weight of this namespace | ||
NamespaceWeightKey = "volcano.sh/namespace.weight" | ||
// DefaultNamespaceWeight is the default weight of namespace | ||
DefaultNamespaceWeight = 1 | ||
) | ||
|
||
// NamespaceInfo records information of namespace | ||
type NamespaceInfo struct { | ||
// Name is the name of this namespace | ||
Name NamespaceName | ||
// Weight is the highest weight among many ResourceQuota. | ||
Weight int64 | ||
} | ||
|
||
// GetWeight returns weight of a namespace, any invalid case would get default value | ||
func (n *NamespaceInfo) GetWeight() int64 { | ||
if n == nil || n.Weight == 0 { | ||
return DefaultNamespaceWeight | ||
} | ||
return n.Weight | ||
} | ||
|
||
type quotaItem struct { | ||
name string | ||
weight int64 | ||
} | ||
|
||
func quotaItemKeyFunc(obj interface{}) (string, error) { | ||
item, ok := obj.(*quotaItem) | ||
if !ok { | ||
return "", fmt.Errorf("obj with type %T could not parse", obj) | ||
} | ||
return item.name, nil | ||
} | ||
|
||
// for big root heap | ||
func quotaItemLessFunc(a interface{}, b interface{}) bool { | ||
A := a.(*quotaItem) | ||
B := b.(*quotaItem) | ||
return A.weight > B.weight | ||
} | ||
|
||
// NamespaceCollection will record all details about namespace | ||
type NamespaceCollection struct { | ||
Name string | ||
|
||
quotaWeight *cache.Heap | ||
} | ||
|
||
// NewNamespaceCollection creates new NamespaceCollection object to record all information about a namespace | ||
func NewNamespaceCollection(name string) *NamespaceCollection { | ||
n := &NamespaceCollection{ | ||
Name: name, | ||
quotaWeight: cache.NewHeap(quotaItemKeyFunc, quotaItemLessFunc), | ||
} | ||
// add at least one item into quotaWeight. | ||
// Because cache.Heap.Pop would be blocked until queue is not empty | ||
n.updateWeight("aItem{ | ||
name: NamespaceWeightKey, | ||
weight: DefaultNamespaceWeight, | ||
}) | ||
return n | ||
} | ||
|
||
func (n *NamespaceCollection) deleteWeight(q *quotaItem) { | ||
n.quotaWeight.Delete(q) | ||
} | ||
|
||
func (n *NamespaceCollection) updateWeight(q *quotaItem) { | ||
n.quotaWeight.Update(q) | ||
} | ||
|
||
func itemFromQuota(quota *v1.ResourceQuota) *quotaItem { | ||
var weight int64 = DefaultNamespaceWeight | ||
|
||
quotaWeight, ok := quota.Spec.Hard[NamespaceWeightKey] | ||
if ok { | ||
weight = quotaWeight.Value() | ||
} | ||
|
||
item := "aItem{ | ||
name: quota.Name, | ||
weight: weight, | ||
} | ||
return item | ||
} | ||
|
||
// Update modify the registered information according quota object | ||
func (n *NamespaceCollection) Update(quota *v1.ResourceQuota) { | ||
n.updateWeight(itemFromQuota(quota)) | ||
} | ||
|
||
// Delete remove the registered information according quota object | ||
func (n *NamespaceCollection) Delete(quota *v1.ResourceQuota) { | ||
n.deleteWeight(itemFromQuota(quota)) | ||
} | ||
|
||
// Snapshot will clone a NamespaceInfo without Heap according NamespaceCollection | ||
func (n *NamespaceCollection) Snapshot() *NamespaceInfo { | ||
var weight int64 = DefaultNamespaceWeight | ||
|
||
obj, err := n.quotaWeight.Pop() | ||
if err != nil { | ||
glog.Warningf("namespace %s, quota weight meets error %v when pop", n.Name, err) | ||
} else { | ||
item := obj.(*quotaItem) | ||
weight = item.weight | ||
n.quotaWeight.Add(item) | ||
} | ||
|
||
return &NamespaceInfo{ | ||
Name: NamespaceName(n.Name), | ||
Weight: weight, | ||
} | ||
} |
Oops, something went wrong.