Skip to content
This repository has been archived by the owner on Oct 6, 2023. It is now read-only.

Commit

Permalink
support OR-ed selector, namespaceSelector support in ClusterThrottle
Browse files Browse the repository at this point in the history
  • Loading branch information
everpeace committed Jan 22, 2019
1 parent 0f96809 commit ed1f4f6
Show file tree
Hide file tree
Showing 19 changed files with 600 additions and 115 deletions.
22 changes: 15 additions & 7 deletions README.md
Expand Up @@ -87,10 +87,14 @@ metadata:
spec:
# throttler name which responsible for this Throttle custom resource
throttlerName: kube-throttler
# you can write any label selector freely
# you can write any label selector freely
# items under selecterTerms are evaluated OR-ed
# each selecterTerm item are evaluated AND-ed
selector:
matchLabels:
throttle: t1
selecterTerms:
- podSelector:
matchLabels:
throttle: t1
# you can set a threshold of the throttle
threshold:
# limiting total count of resources
Expand Down Expand Up @@ -144,8 +148,10 @@ $ kubectl get throttle t1 -o yaml
spec:
throttlerName: kube-throttler
selector:
matchLabels:
throttle: t1
selecterTerms:
- podSelector:
matchLabels:
throttle: t1
threshold:
resourceCounts:
pod: 5
Expand Down Expand Up @@ -249,8 +255,10 @@ $ kubectl get throttle t1 -o yaml
...
spec:
selector:
matchLabels:
throttle: t1
selecterTerms:
- podSelector:
matchLabels:
throttle: t1
threshold:
resourceCounts:
pod: 5
Expand Down
2 changes: 1 addition & 1 deletion build.sbt
Expand Up @@ -87,7 +87,7 @@ lazy val root = (project in file("."))
dockerUsername := Some("everpeace"),
packageName in Docker := "kube-throttler",
maintainer in Docker := "Shingo Omura <https://github.com/everpeace>",
dockerBaseImage := "frolvlad/alpine-oraclejdk8:8.181.13-slim",
dockerBaseImage := "adoptopenjdk/openjdk8:x86_64-alpine-jdk8u191-b12",
dockerExposedPorts := Seq(
4321 /* kube-throttle (kube-scheduler extender) */,
5005 /* for jvm debug */,
Expand Down
22 changes: 22 additions & 0 deletions deploy/0-crd.yaml
Expand Up @@ -30,6 +30,16 @@ spec:
properties:
selector:
type: object
required:
- selectorTerms
properties:
selectorTerms:
type: array
items:
type: object
properties:
podSelector:
type: object
threshold:
type: object
properties:
Expand Down Expand Up @@ -73,6 +83,18 @@ spec:
properties:
selector:
type: object
required:
- selectorTerms
properties:
selectorTerms:
type: array
items:
type: object
properties:
namespaceSelector:
type: object
podSelector:
type: object
threshold:
type: object
properties:
Expand Down
2 changes: 1 addition & 1 deletion deploy/2-rbac.yaml
Expand Up @@ -10,7 +10,7 @@ metadata:
name: kube-throttler
rules:
- apiGroups: [""]
resources: ["pods"]
resources: ["pods", "namespaces"]
verbs: ["get","list","watch"]
- apiGroups: ["schedule.k8s.everpeace.github.com"]
resources: ["clusterthrottles"]
Expand Down
9 changes: 7 additions & 2 deletions example/clthrottle.yaml
Expand Up @@ -5,8 +5,13 @@ metadata:
spec:
throttlerName: kube-throttler
selector:
matchLabels:
throttle: t1
selectorTerms:
- namespaceSelector:
matchLabels:
throttle: 'true'
podSelector:
matchLabels:
throttle: t1
threshold:
resourceCounts:
pod: 5
Expand Down
6 changes: 4 additions & 2 deletions example/throttle.yaml
Expand Up @@ -5,8 +5,10 @@ metadata:
spec:
throttlerName: kube-throttler
selector:
matchLabels:
throttle: t1
selectorTerms:
- podSelector:
matchLabels:
throttle: t1
threshold:
resourceCounts:
pod: 5
Expand Down
2 changes: 1 addition & 1 deletion project/plugins.sbt
@@ -1,7 +1,7 @@
resolvers += Resolver.bintrayRepo("kamon-io", "sbt-plugins")
addSbtPlugin("com.lucidchart" % "sbt-scalafmt" % "1.15")
addSbtPlugin("com.github.gseitz" % "sbt-release" % "1.0.9")
addSbtPlugin("com.typesafe.sbt" % "sbt-native-packager" % "1.3.6")
addSbtPlugin("com.typesafe.sbt" % "sbt-native-packager" % "1.3.15")
addSbtPlugin("de.heikoseeberger" % "sbt-header" % "5.0.0")
addSbtPlugin("io.kamon" % "sbt-aspectj-runner" % "1.1.0")
addSbtPlugin("com.lightbend.sbt" % "sbt-javaagent" % "0.1.4")
Expand Up @@ -25,20 +25,34 @@ import cats.instances.list._

trait ClusterThrottleControllerLogic {

def isClusterThrottleAlreadyActiveFor(pod: Pod, clthrottle: v1alpha1.ClusterThrottle): Boolean =
clthrottle.isAlreadyActiveFor(pod)
def isClusterThrottleInsufficientFor(pod: Pod, clthrottle: v1alpha1.ClusterThrottle): Boolean =
clthrottle.isInsufficientFor(pod)
def isClusterThrottleAlreadyActiveFor(
pod: Pod,
ns: Namespace,
clthrottle: v1alpha1.ClusterThrottle
): Boolean =
clthrottle.isAlreadyActiveFor(pod, ns)
def isClusterThrottleInsufficientFor(
pod: Pod,
ns: Namespace,
clthrottle: v1alpha1.ClusterThrottle
): Boolean =
clthrottle.isInsufficientFor(pod, ns)
def calcNextClusterThrottleStatuses(
targetClusterThrottles: Set[v1alpha1.ClusterThrottle],
podsInAllNamespaces: Set[Pod]
podsInAllNamespaces: Set[Pod],
namespaces: Map[String, Namespace]
): List[(ObjectKey, v1alpha1.ClusterThrottle.Status)] = {

for {
clthrottle <- targetClusterThrottles.toList

matchedPods = podsInAllNamespaces.filter(p =>
clthrottle.spec.selector.matches(p.metadata.labels))
matchedPods = podsInAllNamespaces.filter { p =>
if (namespaces.contains(p.namespace)) {
clthrottle.spec.selector.matches(p, namespaces(p.namespace))
} else {
false
}
}
runningPods = matchedPods
.filter(p => p.status.exists(_.phase.exists(_ == Pod.Phase.Running)))
.toList
Expand Down

0 comments on commit ed1f4f6

Please sign in to comment.