Skip to content

Commit

Permalink
Add Resource Usage API
Browse files Browse the repository at this point in the history
  • Loading branch information
wzshiming committed Jul 27, 2023
1 parent 8b2015d commit 10b4f5e
Show file tree
Hide file tree
Showing 35 changed files with 3,100 additions and 97 deletions.
4 changes: 2 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ require (
github.com/creack/pty v1.1.18
github.com/emicklei/go-restful/v3 v3.10.2
github.com/fsnotify/fsnotify v1.6.0
github.com/google/cel-go v0.16.0
github.com/google/cel-go v0.17.1
github.com/google/go-cmp v0.5.9
github.com/itchyny/gojq v0.12.13
github.com/nxadm/tail v1.4.8
Expand All @@ -23,7 +23,7 @@ require (
github.com/wzshiming/cmux v0.3.2
github.com/wzshiming/cron v0.2.1
github.com/wzshiming/ctc v1.2.3
github.com/wzshiming/easycel v0.4.0
github.com/wzshiming/easycel v0.5.0-rc.1
go.uber.org/atomic v1.11.0
golang.org/x/exp v0.0.0-20230713183714-613f0c0eb8a1
golang.org/x/sync v0.3.0
Expand Down
8 changes: 4 additions & 4 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -89,8 +89,8 @@ github.com/golang/protobuf v1.5.0/go.mod h1:FsONVRAS9T7sI+LIUmWTfcYkHO4aIWwzhcaS
github.com/golang/protobuf v1.5.2/go.mod h1:XVQd3VNwM+JqD3oG2Ue2ip4fOMUkwXdXDdiuN0vRsmY=
github.com/golang/protobuf v1.5.3 h1:KhyjKVUg7Usr/dYsdSqoFveMYd5ko72D+zANwlG1mmg=
github.com/golang/protobuf v1.5.3/go.mod h1:XVQd3VNwM+JqD3oG2Ue2ip4fOMUkwXdXDdiuN0vRsmY=
github.com/google/cel-go v0.16.0 h1:DG9YQ8nFCFXAs/FDDwBxmL1tpKNrdlGUM9U3537bX/Y=
github.com/google/cel-go v0.16.0/go.mod h1:HXZKzB0LXqer5lHHgfWAnlYwJaQBDKMjxjulNQzhwhY=
github.com/google/cel-go v0.17.1 h1:s2151PDGy/eqpCI80/8dl4VL3xTkqI/YubXLXCFw0mw=
github.com/google/cel-go v0.17.1/go.mod h1:HXZKzB0LXqer5lHHgfWAnlYwJaQBDKMjxjulNQzhwhY=
github.com/google/gnostic v0.5.7-v3refs h1:FhTMOKj2VhjpouxvWJAV1TL304uMlb9zcDqkl6cEI54=
github.com/google/gnostic v0.5.7-v3refs/go.mod h1:73MKFl6jIHelAJNaBGFzt3SPtZULs9dYrGFt8OiIsHQ=
github.com/google/go-cmp v0.2.0/go.mod h1:oXzfMopK8JAjlY9xF4vHSVASa0yLyX7SntLO5aqRK0M=
Expand Down Expand Up @@ -226,8 +226,8 @@ github.com/wzshiming/cron v0.2.1 h1:jtJlyGG4Od+xE0A36DtqTeyLhGssE95dsghqLdemXxI=
github.com/wzshiming/cron v0.2.1/go.mod h1:qXPkSl0AQLmjchtcqv7L6X8FSOLqy8hXYIXv9J1cKO4=
github.com/wzshiming/ctc v1.2.3 h1:q+hW3IQNsjIlOFBTGZZZeIXTElFM4grF4spW/errh/c=
github.com/wzshiming/ctc v1.2.3/go.mod h1:2tVAtIY7SUyraSk0JxvwmONNPFL4ARavPuEsg5+KA28=
github.com/wzshiming/easycel v0.4.0 h1:flPpJYS2bXQj1Jgk2yOp5oB6FvflvPM0VCHXx/qOJDk=
github.com/wzshiming/easycel v0.4.0/go.mod h1:qg3oAkmPOLJEUFlFsxBxYbp0cXHgbq2sZEHVq7SmUp8=
github.com/wzshiming/easycel v0.5.0-rc.1 h1:+PU2rAam/lcjAewZMw6O7XwXFChmqvDn0eNMGfWsiZo=
github.com/wzshiming/easycel v0.5.0-rc.1/go.mod h1:wqtnBqpBuUpk0i3dz4/QYT8HsO9uu1V5tn2wkFQhMMk=
github.com/wzshiming/llrb v0.2.1 h1:+T/PE0lrrB2LE8487fYcRlkZct5E9L/Gi8mf6mUtEK8=
github.com/wzshiming/llrb v0.2.1/go.mod h1:xlAM5hpCBIT3dTy/6CbNPxHMR1JfIMSyk2lV68wz3Yw=
github.com/wzshiming/trie v0.1.1 h1:02AaBSZGhs6Aqljp8fz4xq/Mg8omFBPIlrUS0pJ11ks=
Expand Down
135 changes: 135 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,135 @@
---
apiVersion: apiextensions.k8s.io/v1
kind: CustomResourceDefinition
metadata:
annotations:
controller-gen.kubebuilder.io/version: v0.12.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:
type: string
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: {}
119 changes: 119 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,119 @@
---
apiVersion: apiextensions.k8s.io/v1
kind: CustomResourceDefinition
metadata:
annotations:
controller-gen.kubebuilder.io/version: v0.12.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:
type: string
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
37 changes: 37 additions & 0 deletions kustomize/kwokctl/resource/pod.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,48 @@ 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 }}
12 changes: 12 additions & 0 deletions kustomize/rbac/role.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,18 @@ rules:
- patch
- update
- watch
- apiGroups:
- kwok.x-k8s.io
resources:
- resourceusages
verbs:
- create
- delete
- get
- list
- patch
- update
- watch
- apiGroups:
- kwok.x-k8s.io
resources:
Expand Down
Loading

0 comments on commit 10b4f5e

Please sign in to comment.