Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
K
k3s
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Registry
Registry
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Jacklull
k3s
Commits
74a99e1e
Commit
74a99e1e
authored
Apr 03, 2017
by
Harry Zhang
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Update generate staging client-go
parent
658e7053
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
142 additions
and
0 deletions
+142
-0
resource_helpers.go
staging/src/k8s.io/client-go/pkg/api/resource_helpers.go
+41
-0
resource_helpers.go
staging/src/k8s.io/client-go/pkg/api/v1/resource_helpers.go
+101
-0
No files found.
staging/src/k8s.io/client-go/pkg/api/resource_helpers.go
View file @
74a99e1e
...
...
@@ -17,6 +17,9 @@ limitations under the License.
package
api
import
(
"fmt"
"math"
"strconv"
"time"
"k8s.io/apimachinery/pkg/api/resource"
...
...
@@ -227,3 +230,41 @@ func PodRequestsAndLimits(pod *Pod) (reqs map[ResourceName]resource.Quantity, li
}
return
}
// ExtractContainerResourceValue extracts the value of a resource
// in an already known container
func
ExtractContainerResourceValue
(
fs
*
ResourceFieldSelector
,
container
*
Container
)
(
string
,
error
)
{
divisor
:=
resource
.
Quantity
{}
if
divisor
.
Cmp
(
fs
.
Divisor
)
==
0
{
divisor
=
resource
.
MustParse
(
"1"
)
}
else
{
divisor
=
fs
.
Divisor
}
switch
fs
.
Resource
{
case
"limits.cpu"
:
return
convertResourceCPUToString
(
container
.
Resources
.
Limits
.
Cpu
(),
divisor
)
case
"limits.memory"
:
return
convertResourceMemoryToString
(
container
.
Resources
.
Limits
.
Memory
(),
divisor
)
case
"requests.cpu"
:
return
convertResourceCPUToString
(
container
.
Resources
.
Requests
.
Cpu
(),
divisor
)
case
"requests.memory"
:
return
convertResourceMemoryToString
(
container
.
Resources
.
Requests
.
Memory
(),
divisor
)
}
return
""
,
fmt
.
Errorf
(
"unsupported container resource : %v"
,
fs
.
Resource
)
}
// convertResourceCPUToString converts cpu value to the format of divisor and returns
// ceiling of the value.
func
convertResourceCPUToString
(
cpu
*
resource
.
Quantity
,
divisor
resource
.
Quantity
)
(
string
,
error
)
{
c
:=
int64
(
math
.
Ceil
(
float64
(
cpu
.
MilliValue
())
/
float64
(
divisor
.
MilliValue
())))
return
strconv
.
FormatInt
(
c
,
10
),
nil
}
// convertResourceMemoryToString converts memory value to the format of divisor and returns
// ceiling of the value.
func
convertResourceMemoryToString
(
memory
*
resource
.
Quantity
,
divisor
resource
.
Quantity
)
(
string
,
error
)
{
m
:=
int64
(
math
.
Ceil
(
float64
(
memory
.
Value
())
/
float64
(
divisor
.
Value
())))
return
strconv
.
FormatInt
(
m
,
10
),
nil
}
staging/src/k8s.io/client-go/pkg/api/v1/resource_helpers.go
View file @
74a99e1e
...
...
@@ -17,10 +17,14 @@ limitations under the License.
package
v1
import
(
"fmt"
"math"
"strconv"
"time"
"k8s.io/apimachinery/pkg/api/resource"
metav1
"k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/client-go/pkg/api"
)
// Returns string version of ResourceName.
...
...
@@ -255,3 +259,100 @@ func GetResourceRequest(pod *Pod, resource ResourceName) int64 {
}
return
totalResources
}
// ExtractResourceValueByContainerName extracts the value of a resource
// by providing container name
func
ExtractResourceValueByContainerName
(
fs
*
ResourceFieldSelector
,
pod
*
Pod
,
containerName
string
)
(
string
,
error
)
{
container
,
err
:=
findContainerInPod
(
pod
,
containerName
)
if
err
!=
nil
{
return
""
,
err
}
return
ExtractContainerResourceValue
(
fs
,
container
)
}
// ExtractResourceValueByContainerNameAndNodeAllocatable extracts the value of a resource
// by providing container name and node allocatable
func
ExtractResourceValueByContainerNameAndNodeAllocatable
(
fs
*
ResourceFieldSelector
,
pod
*
Pod
,
containerName
string
,
nodeAllocatable
ResourceList
)
(
string
,
error
)
{
realContainer
,
err
:=
findContainerInPod
(
pod
,
containerName
)
if
err
!=
nil
{
return
""
,
err
}
containerCopy
,
err
:=
api
.
Scheme
.
DeepCopy
(
realContainer
)
if
err
!=
nil
{
return
""
,
fmt
.
Errorf
(
"failed to perform a deep copy of container object: %v"
,
err
)
}
container
,
ok
:=
containerCopy
.
(
*
Container
)
if
!
ok
{
return
""
,
fmt
.
Errorf
(
"unexpected type returned from deep copy of container object"
)
}
MergeContainerResourceLimits
(
container
,
nodeAllocatable
)
return
ExtractContainerResourceValue
(
fs
,
container
)
}
// ExtractContainerResourceValue extracts the value of a resource
// in an already known container
func
ExtractContainerResourceValue
(
fs
*
ResourceFieldSelector
,
container
*
Container
)
(
string
,
error
)
{
divisor
:=
resource
.
Quantity
{}
if
divisor
.
Cmp
(
fs
.
Divisor
)
==
0
{
divisor
=
resource
.
MustParse
(
"1"
)
}
else
{
divisor
=
fs
.
Divisor
}
switch
fs
.
Resource
{
case
"limits.cpu"
:
return
convertResourceCPUToString
(
container
.
Resources
.
Limits
.
Cpu
(),
divisor
)
case
"limits.memory"
:
return
convertResourceMemoryToString
(
container
.
Resources
.
Limits
.
Memory
(),
divisor
)
case
"requests.cpu"
:
return
convertResourceCPUToString
(
container
.
Resources
.
Requests
.
Cpu
(),
divisor
)
case
"requests.memory"
:
return
convertResourceMemoryToString
(
container
.
Resources
.
Requests
.
Memory
(),
divisor
)
}
return
""
,
fmt
.
Errorf
(
"Unsupported container resource : %v"
,
fs
.
Resource
)
}
// convertResourceCPUToString converts cpu value to the format of divisor and returns
// ceiling of the value.
func
convertResourceCPUToString
(
cpu
*
resource
.
Quantity
,
divisor
resource
.
Quantity
)
(
string
,
error
)
{
c
:=
int64
(
math
.
Ceil
(
float64
(
cpu
.
MilliValue
())
/
float64
(
divisor
.
MilliValue
())))
return
strconv
.
FormatInt
(
c
,
10
),
nil
}
// convertResourceMemoryToString converts memory value to the format of divisor and returns
// ceiling of the value.
func
convertResourceMemoryToString
(
memory
*
resource
.
Quantity
,
divisor
resource
.
Quantity
)
(
string
,
error
)
{
m
:=
int64
(
math
.
Ceil
(
float64
(
memory
.
Value
())
/
float64
(
divisor
.
Value
())))
return
strconv
.
FormatInt
(
m
,
10
),
nil
}
// findContainerInPod finds a container by its name in the provided pod
func
findContainerInPod
(
pod
*
Pod
,
containerName
string
)
(
*
Container
,
error
)
{
for
_
,
container
:=
range
pod
.
Spec
.
Containers
{
if
container
.
Name
==
containerName
{
return
&
container
,
nil
}
}
return
nil
,
fmt
.
Errorf
(
"container %s not found"
,
containerName
)
}
// MergeContainerResourceLimits checks if a limit is applied for
// the container, and if not, it sets the limit to the passed resource list.
func
MergeContainerResourceLimits
(
container
*
Container
,
allocatable
ResourceList
)
{
if
container
.
Resources
.
Limits
==
nil
{
container
.
Resources
.
Limits
=
make
(
ResourceList
)
}
for
_
,
resource
:=
range
[]
ResourceName
{
ResourceCPU
,
ResourceMemory
}
{
if
quantity
,
exists
:=
container
.
Resources
.
Limits
[
resource
];
!
exists
||
quantity
.
IsZero
()
{
if
cap
,
exists
:=
allocatable
[
resource
];
exists
{
container
.
Resources
.
Limits
[
resource
]
=
*
cap
.
Copy
()
}
}
}
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment