Skip to content

Commit

Permalink
Merge pull request #740 from wzshiming/feat/resource-usage
Browse files Browse the repository at this point in the history
Add Resource Usage API
  • Loading branch information
wzshiming authored Jan 26, 2024
2 parents d2519e4 + 9abb653 commit fbec5ab
Show file tree
Hide file tree
Showing 60 changed files with 3,693 additions and 212 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ IMAGE_PLATFORMS ?= linux/amd64 linux/arm64

BINARY_PLATFORMS ?= linux/amd64 linux/arm64 darwin/amd64 darwin/arm64 windows/amd64 windows/arm64

MANIFESTS ?= kwok kwokctl stage/fast
MANIFESTS ?= kwok kwokctl stage/fast metrics/usage

BUILDER ?= docker
DOCKER_CLI_EXPERIMENTAL ?= enabled
Expand Down
148 changes: 148 additions & 0 deletions kustomize/crd/bases/kwok.x-k8s.io_clusterresourceusages.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,148 @@
---
apiVersion: apiextensions.k8s.io/v1
kind: CustomResourceDefinition
metadata:
annotations:
controller-gen.kubebuilder.io/version: v0.13.0
name: clusterresourceusages.kwok.x-k8s.io
spec:
group: kwok.x-k8s.io
names:
kind: ClusterResourceUsage
listKind: ClusterResourceUsageList
plural: clusterresourceusages
singular: clusterresourceusage
scope: Cluster
versions:
- name: v1alpha1
schema:
openAPIV3Schema:
description: ClusterResourceUsage provides cluster-wide resource usage.
properties:
apiVersion:
description: 'APIVersion defines the versioned schema of this representation
of an object. Servers should convert recognized schemas to the latest
internal value, and may reject unrecognized values. More info: https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md#resources'
type: string
kind:
description: 'Kind is a string value representing the REST resource this
object represents. Servers may infer this from the endpoint the client
submits requests to. Cannot be updated. In CamelCase. More info: https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md#types-kinds'
type: string
metadata:
type: object
spec:
description: Spec holds spec for cluster resource usage.
properties:
selector:
description: Selector is a selector to filter pods to configure.
properties:
matchNames:
description: MatchNames is a list of names to match. if not set,
all names will be matched.
items:
type: string
type: array
matchNamespaces:
description: MatchNamespaces is a list of namespaces to match.
if not set, all namespaces will be matched.
items:
type: string
type: array
type: object
usages:
description: Usages is a list of resource usage for the pod.
items:
description: ResourceUsageContainer holds spec for resource usage
container.
properties:
containers:
description: Containers is list of container names.
items:
type: string
type: array
usage:
additionalProperties:
description: ResourceUsageValue holds value for resource usage.
properties:
expression:
description: Expression is the expression for resource
usage.
type: string
value:
anyOf:
- type: integer
- type: string
description: Value is the value for resource usage.
pattern: ^(\+|-)?(([0-9]+(\.[0-9]*)?)|(\.[0-9]+))(([KMGTPE]i)|[numkMGTPE]|([eE](\+|-)?(([0-9]+(\.[0-9]*)?)|(\.[0-9]+))))?$
x-kubernetes-int-or-string: true
type: object
description: Usage is a list of resource usage for the container.
type: object
type: object
type: array
type: object
status:
description: Status holds status for cluster resource usage
properties:
conditions:
description: Conditions holds conditions for cluster resource usage
items:
description: Condition contains details for one aspect of the current
state of this API Resource.
properties:
lastTransitionTime:
description: LastTransitionTime is the last time the condition
transitioned from one status to another. This should be when
the underlying condition changed. If that is not known, then
using the time when the API field changed is acceptable.
format: date-time
type: string
message:
description: Message is a human readable message indicating
details about the transition. This may be an empty string.
maxLength: 32768
type: string
reason:
description: Reason contains a programmatic identifier indicating
the reason for the condition's last transition. Producers
of specific condition types may define expected values and
meanings for this field, and whether the values are considered
a guaranteed API. The value should be a CamelCase string.
This field may not be empty.
maxLength: 1024
minLength: 1
pattern: ^[A-Za-z]([A-Za-z0-9_,:]*[A-Za-z0-9_])?$
type: string
status:
description: Status of the condition
type: string
type:
description: Type of condition in CamelCase or in foo.example.com/CamelCase.
Many .condition.type values are consistent across resources
like Available, but because arbitrary conditions can be useful
(see .node.status.conditions), the ability to deconflict is
important. The regex it matches is (dns1123SubdomainFmt/)?(qualifiedNameFmt)
maxLength: 316
pattern: ^([a-z0-9]([-a-z0-9]*[a-z0-9])?(\.[a-z0-9]([-a-z0-9]*[a-z0-9])?)*/)?(([A-Za-z0-9][-A-Za-z0-9_.]*)?[A-Za-z0-9])$
type: string
required:
- lastTransitionTime
- message
- reason
- status
- type
type: object
type: array
x-kubernetes-list-map-keys:
- type
x-kubernetes-list-type: map
type: object
required:
- metadata
- spec
type: object
served: true
storage: true
subresources:
status: {}
132 changes: 132 additions & 0 deletions kustomize/crd/bases/kwok.x-k8s.io_resourceusages.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,132 @@
---
apiVersion: apiextensions.k8s.io/v1
kind: CustomResourceDefinition
metadata:
annotations:
controller-gen.kubebuilder.io/version: v0.13.0
name: resourceusages.kwok.x-k8s.io
spec:
group: kwok.x-k8s.io
names:
kind: ResourceUsage
listKind: ResourceUsageList
plural: resourceusages
singular: resourceusage
scope: Namespaced
versions:
- name: v1alpha1
schema:
openAPIV3Schema:
description: ResourceUsage provides resource usage for a single pod.
properties:
apiVersion:
description: 'APIVersion defines the versioned schema of this representation
of an object. Servers should convert recognized schemas to the latest
internal value, and may reject unrecognized values. More info: https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md#resources'
type: string
kind:
description: 'Kind is a string value representing the REST resource this
object represents. Servers may infer this from the endpoint the client
submits requests to. Cannot be updated. In CamelCase. More info: https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md#types-kinds'
type: string
metadata:
type: object
spec:
description: Spec holds spec for resource usage.
properties:
usages:
description: Usages is a list of resource usage for the pod.
items:
description: ResourceUsageContainer holds spec for resource usage
container.
properties:
containers:
description: Containers is list of container names.
items:
type: string
type: array
usage:
additionalProperties:
description: ResourceUsageValue holds value for resource usage.
properties:
expression:
description: Expression is the expression for resource
usage.
type: string
value:
anyOf:
- type: integer
- type: string
description: Value is the value for resource usage.
pattern: ^(\+|-)?(([0-9]+(\.[0-9]*)?)|(\.[0-9]+))(([KMGTPE]i)|[numkMGTPE]|([eE](\+|-)?(([0-9]+(\.[0-9]*)?)|(\.[0-9]+))))?$
x-kubernetes-int-or-string: true
type: object
description: Usage is a list of resource usage for the container.
type: object
type: object
type: array
type: object
status:
description: Status holds status for resource usage
properties:
conditions:
description: Conditions holds conditions for resource usage
items:
description: Condition contains details for one aspect of the current
state of this API Resource.
properties:
lastTransitionTime:
description: LastTransitionTime is the last time the condition
transitioned from one status to another. This should be when
the underlying condition changed. If that is not known, then
using the time when the API field changed is acceptable.
format: date-time
type: string
message:
description: Message is a human readable message indicating
details about the transition. This may be an empty string.
maxLength: 32768
type: string
reason:
description: Reason contains a programmatic identifier indicating
the reason for the condition's last transition. Producers
of specific condition types may define expected values and
meanings for this field, and whether the values are considered
a guaranteed API. The value should be a CamelCase string.
This field may not be empty.
maxLength: 1024
minLength: 1
pattern: ^[A-Za-z]([A-Za-z0-9_,:]*[A-Za-z0-9_])?$
type: string
status:
description: Status of the condition
type: string
type:
description: Type of condition in CamelCase or in foo.example.com/CamelCase.
Many .condition.type values are consistent across resources
like Available, but because arbitrary conditions can be useful
(see .node.status.conditions), the ability to deconflict is
important. The regex it matches is (dns1123SubdomainFmt/)?(qualifiedNameFmt)
maxLength: 316
pattern: ^([a-z0-9]([-a-z0-9]*[a-z0-9])?(\.[a-z0-9]([-a-z0-9]*[a-z0-9])?)*/)?(([A-Za-z0-9][-A-Za-z0-9_.]*)?[A-Za-z0-9])$
type: string
required:
- lastTransitionTime
- message
- reason
- status
- type
type: object
type: array
x-kubernetes-list-map-keys:
- type
x-kubernetes-list-type: map
type: object
required:
- metadata
- spec
type: object
served: true
storage: true
subresources:
status: {}
8 changes: 8 additions & 0 deletions kustomize/crd/embed.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,14 @@ var (
//go:embed bases/kwok.x-k8s.io_clusterportforwards.yaml
ClusterPortForward []byte

// ResourceUsage is the custom resource definition for resource usage.
//go:embed bases/kwok.x-k8s.io_resourceusages.yaml
ResourceUsage []byte

// ClusterResourceUsage is the custom resource definition for cluster resource usage.
//go:embed bases/kwok.x-k8s.io_clusterresourceusages.yaml
ClusterResourceUsage []byte

// Metric is the custom resource definition for metrics.
//go:embed bases/kwok.x-k8s.io_metrics.yaml
Metric []byte
Expand Down
36 changes: 36 additions & 0 deletions kustomize/kwokctl/resource/pod.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,47 @@ template: |-
{{ range $index, $container := .containers }}
- name: {{ $container.name }}
image: {{ $container.image }}
{{ if and $container $container.resources }}
{{ $resources := $container.resources }}
resources:
requests:
{{ range $key, $value := $resources.requests }}
{{ $key }}: {{ $value }}
{{ end }}
limits:
{{ if $resources.limits }}
{{ range $key, $value := $resources.requests }}
{{ $key }}: {{ or ( index $resources.limits $key ) $value }}
{{ end }}
{{ else }}
{{ range $key, $value := $resources.requests }}
{{ $key }}: {{ $value }}
{{ end }}
{{ end }}
{{ end }}
{{ end }}
initContainers:
{{ range $index, $container := .initContainers }}
- name: {{ $container.name }}
image: {{ $container.image }}
{{ if and $container $container.resources }}
{{ $resources := $container.resources }}
resources:
requests:
{{ range $key, $value := $resources.requests }}
{{ $key }}: {{ $value }}
{{ end }}
limits:
{{ if $resources.limits }}
{{ range $key, $value := $resources.requests }}
{{ $key }}: {{ or ( index $resources.limits $key ) $value }}
{{ end }}
{{ else }}
{{ range $key, $value := $resources.requests }}
{{ $key }}: {{ $value }}
{{ end }}
{{ end }}
{{ end }}
{{ end }}
hostNetwork: {{ .hostNetwork }}
nodeName: {{ .nodeName }}
4 changes: 4 additions & 0 deletions kustomize/metrics/resource/kustomization.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
apiVersion: kustomize.config.k8s.io/v1beta1
kind: Kustomization
resources:
- metrics-resource.yaml
Loading

0 comments on commit fbec5ab

Please sign in to comment.