projectcapsule / capsule

Multi-tenancy and policy-based framework for Kubernetes.
https://capsule.clastix.io
Apache License 2.0
1.58k stars 155 forks source link

Support for API Priority and Fairness per tenant #180

Open bsctl opened 3 years ago

bsctl commented 3 years ago

Describe the feature

Kubernetes introduced the API Priority and Fairness (APF) feature [beta in k8s 1.20] to control the behaviour of the Kubernetes API server in an overload situation. See here for details.

When multiple tenants are deployed, a buggy or malicious application running in a tenant can overload the APIs server or it can compete with other tenants and starve their applications.

It would be nice to implement support for APF in Capsule:

apiVersion: flowcontrol.apiserver.k8s.io/v1beta1
kind: FlowSchema
metadata:
  name: capsule-system-service-account
spec:
  distinguisherMethod:
    type: ByNamespace
  matchingPrecedence: 1000  # <-- precedence just lower than kube-system-service-accounts
  priorityLevelConfiguration:
    name: workload-high  # <-- same priority of system controllers
  rules:
  - nonResourceRules:
    - nonResourceURLs:
      - '*'
      verbs:
      - '*'
    resourceRules:
    - apiGroups:
      - '*'
      clusterScope: true
      namespaces:
      - '*'
      resources:
      - '*'
      verbs:
      - '*'
    subjects:
    - kind: ServiceAccount
      serviceAccount:
        name: capsule # <-- Capsule ServiceAccount
        namespace: capsule-system # <-- Capsule Namespace
apiVersion: flowcontrol.apiserver.k8s.io/v1beta1
kind: FlowSchema
metadata:
  name: oil
spec:
  distinguisherMethod:
    type: ByUser
  matchingPrecedence: 9900 
  priorityLevelConfiguration:
    name: global-default
  rules:
  - nonResourceRules:
    - nonResourceURLs:
      - '*'
      verbs:
      - '*'
    resourceRules:
    - apiGroups:
      - '*'
      clusterScope: true
      namespaces:
      - '*'
      resources:
      - '*'
      verbs:
      - '*'
    subjects:
    - kind: Group
       group:
          name: capsule.clastix.io
    - kind: User
       user:
          name: alice

What would the new user story look like?

  1. When Capsule is installed, the related FlowSchema is created.
  2. When a new tenant is created/modified/deleted, the related FlowSchema is created/modified/deleted

Expected behavior

Support for API Priority and Fairness per tenant

bsctl commented 3 years ago

@prometherion do you have any concern about this enhancement?

prometherion commented 3 years ago

How to handle fairness when an Owner owns multiple Tenant resources?

We would end up with duplicates matching different values, not sure how the Kubernetes controller would deal with them.