apiVersion: apiextensions.k8s.io/v1beta1
kind: CustomResourceDefinition
metadata:
  name: clusterpolicies.kyverno.io
spec:
  group: kyverno.io
  versions:
    - name: v1
      served: true
      storage: true
  scope: Cluster
  names:
    kind: ClusterPolicy
    plural: clusterpolicies
    singular: clusterpolicy
    shortNames:
    - cpol
  subresources:
    status: {}
  validation:
    openAPIV3Schema:
      properties:
        status: {}
        spec:
          required:
          - rules
          properties:
          # default values to be handled by user
            validationFailureAction:
              type: string
              enum: 
              - enforce # blocks the resorce api-reques if a rule fails.
              - audit # allows resource creation and reports the failed validation rules as violations. Default
            background:
              type: boolean
            rules:
              type: array
              items:
                type: object
                required:
                - name
                - match
                properties:
                  name:
                    type: string
                  match:
                    type: object
                    required:
                    - resources
                    properties:
                      roles:
                        type: array
                        items:
                          type: string
                      clusterRoles:
                        type: array
                        items:
                          type: string
                      subjects:
                        type: array
                        items:
                          type: object
                          required:
                          - kind
                          - name
                          properties:
                            kind:
                              type: string
                            apiGroup:
                              type: string
                            name:
                              type: string
                            namespace:
                              type: string
                      resources:
                        type: object
                        minProperties: 1
                        properties:
                          kinds:
                            type: array
                            items:
                              type: string
                          name:
                            type: string
                          namespaces:
                            type: array
                            items:
                              type: string
                          selector:
                            properties:
                              matchLabels:
                                type: object
                                additionalProperties:
                                  type: string
                              matchExpressions:
                                type: array
                                items:
                                  type: object
                                  required:
                                  - key
                                  - operator
                                  properties:
                                    key:
                                      type: string
                                    operator:
                                      type: string
                                    values:
                                      type: array
                                      items:
                                        type: string
                  exclude:
                    type: object
                    properties:
                      roles:
                        type: array
                        items:
                          type: string
                      clusterRoles:
                        type: array
                        items:
                          type: string
                      subjects:
                        type: array
                        items:
                          type: object
                          required:
                          - kind
                          - name
                          properties:
                            kind:
                              type: string
                            apiGroup:
                              type: string
                            name:
                              type: string
                            namespace:
                              type: string
                      resources:
                        type: object
                        properties:
                          kinds:
                            type: array
                            items:
                              type: string
                          name:
                            type: string
                          namespaces:
                            type: array
                            items:
                              type: string
                          selector:
                            properties:
                              matchLabels:
                                type: object
                                additionalProperties:
                                  type: string
                              matchExpressions:
                                type: array
                                items:
                                  type: object
                                  required:
                                  - key
                                  - operator
                                  properties:
                                    key:
                                      type: string
                                    operator:
                                      type: string
                                    values:
                                      type: array
                                      items:
                                        type: string
                  preconditions:
                    type: array
                    items:
                      type: object
                      required:
                      - key  # can be of any type
                      - operator # typed
                      - value # can be of any type
                  mutate:
                    type: object
                    properties:
                      overlay:
                        AnyValue: {}
                      patches:
                        type: array
                        items:
                          type: object
                          required:
                          - path
                          - op
                          properties:
                            path:
                              type: string
                            op:
                              type: string
                              enum:
                              - add
                              - replace
                              - remove
                            value:
                              AnyValue: {}
                  validate:
                    type: object
                    properties:
                      message:
                        type: string
                      pattern:
                        AnyValue: {}
                      anyPattern:
                        AnyValue: {}
                      deny:
                        properties:
                          conditions:
                            type: array
                            items:
                              type: object
                              required:
                                - key  # can be of any type
                                - operator # typed
                                - value # can be of any type
                              properties:
                                operator:
                                  type: string
                                  enum:
                                  - Equal
                                  - Equals
                                  - NotEqual
                                  - NotEquals
                                key:
                                  type: string
                                value:
                                  type: string
                  generate:
                    type: object
                    required:
                    - kind
                    - name
                    properties:
                      kind:
                        type: string
                      name:
                        type: string
                      namespace:
                        type: string
                      clone: 
                        type: object
                        required:
                        - namespace
                        - name
                        properties:
                          namespace:
                            type: string
                          name:
                            type: string
                      data:
                        AnyValue: {}
---
apiVersion: apiextensions.k8s.io/v1beta1
kind: CustomResourceDefinition
metadata:
  name: clusterpolicyviolations.kyverno.io
spec:
  group: kyverno.io
  versions:
    - name: v1
      served: true
      storage: true
  scope: Cluster
  names:
    kind: ClusterPolicyViolation
    plural: clusterpolicyviolations
    singular: clusterpolicyviolation
    shortNames:
    - cpolv
  subresources:
    status: {}
  additionalPrinterColumns:
  - name: Policy
    type: string
    description: The policy that resulted in the violation
    JSONPath: .spec.policy
  - name: ResourceKind
    type: string
    description: The resource kind that cause the violation
    JSONPath: .spec.resource.kind
  - name: ResourceName
    type: string
    description: The resource name that caused the violation
    JSONPath: .spec.resource.name
  - name: Age
    type: date
    JSONPath: .metadata.creationTimestamp
  validation:
    openAPIV3Schema:
      properties:
        spec:
          required:
          - policy
          - resource
          - rules
          properties:
            policy:
              type: string
            resource:
              type: object
              required:
              - kind
              - name
              properties:
                kind:
                  type: string
                name:
                  type: string
            rules:
              type: array
              items:
                type: object
                required:
                - name
                - type
                - message
                properties:
                  name:
                    type: string
                  type:
                    type: string
                  message:
                    type: string
---
apiVersion: apiextensions.k8s.io/v1beta1
kind: CustomResourceDefinition
metadata:
  name: policyviolations.kyverno.io
spec:
  group: kyverno.io
  versions:
    - name: v1
      served: true
      storage: true
  scope: Namespaced
  names:
    kind: PolicyViolation
    plural: policyviolations
    singular: policyviolation
    shortNames:
    - polv
  subresources:
    status: {}
  additionalPrinterColumns:
  - name: Policy
    type: string
    description: The policy that resulted in the violation
    JSONPath: .spec.policy
  - name: ResourceKind
    type: string
    description: The resource kind that cause the violation
    JSONPath: .spec.resource.kind
  - name: ResourceName
    type: string
    description: The resource name that caused the violation
    JSONPath: .spec.resource.name
  - name: Age
    type: date
    JSONPath: .metadata.creationTimestamp
  validation:
    openAPIV3Schema:
      properties:
        spec:
          required:
          - policy
          - resource
          - rules
          properties:
            policy:
              type: string
            resource:
              type: object
              required:
              - kind
              - name
              properties:
                kind:
                  type: string
                name:
                  type: string
            rules:
              type: array
              items:
                type: object
                required:
                - name
                - type
                - message
                properties:
                  name:
                    type: string
                  type:
                    type: string
                  message:
                    type: string
---
apiVersion: apiextensions.k8s.io/v1beta1
kind: CustomResourceDefinition
metadata:
  name: generaterequests.kyverno.io
spec:
  group: kyverno.io
  versions:
    - name: v1
      served: true
      storage: true
  scope: Namespaced
  names:
    kind: GenerateRequest
    plural: generaterequests
    singular: generaterequest
    shortNames:
    - gr
  subresources:
    status: {}
  additionalPrinterColumns:
  - name: Policy
    type: string
    description: The policy that resulted in the violation
    JSONPath: .spec.policy
  - name: ResourceKind
    type: string
    description: The resource kind that cause the violation
    JSONPath: .spec.resource.kind
  - name: ResourceName
    type: string
    description: The resource name that caused the violation
    JSONPath: .spec.resource.name
  - name: ResourceNamespace
    type: string
    description: The resource namespace that caused the violation
    JSONPath: .spec.resource.namespace
  - name: status
    type : string
    description: Current state of generate request
    JSONPath: .status.state
  - name: Age
    type: date
    JSONPath: .metadata.creationTimestamp
  validation:
    openAPIV3Schema:
      properties:
        spec:
          required:
          - policy
          - resource
          properties:
            policy:
              type: string
            resource:
              type: object
              required:
              - kind 
              - name
              properties:
                kind:
                  type: string
                name: 
                  type: string
                namespace:
                  type: string    
---
kind: Namespace
apiVersion: v1
metadata: 
    name: "kyverno"
---
apiVersion: v1
kind: Service
metadata:
  namespace: kyverno
  name: kyverno-svc
  labels:
    app: kyverno
spec:
  ports:
  - port: 443
    targetPort: 443
  selector:
    app: kyverno
---
apiVersion: v1
kind: ServiceAccount
metadata:
  name: kyverno-service-account
  namespace: kyverno
---
apiVersion: rbac.authorization.k8s.io/v1beta1
kind: ClusterRole
metadata:
  name: kyverno:policyviolations
rules:
- apiGroups: ["kyverno.io"]
  resources:
  - policyviolations
  verbs: ["get", "list", "watch"]
---
kind: ClusterRoleBinding
apiVersion: rbac.authorization.k8s.io/v1
metadata:
  name: kyverno:webhook
roleRef:
  apiGroup: rbac.authorization.k8s.io
  kind: ClusterRole
  name: kyverno:webhook
subjects:
- kind: ServiceAccount
  name: kyverno-service-account
  namespace: kyverno
---
kind: ClusterRoleBinding
apiVersion: rbac.authorization.k8s.io/v1
metadata:
  name: kyverno:userinfo
roleRef:
  apiGroup: rbac.authorization.k8s.io
  kind: ClusterRole
  name: kyverno:userinfo
subjects:
- kind: ServiceAccount
  name: kyverno-service-account
  namespace: kyverno
---
kind: ClusterRoleBinding
apiVersion: rbac.authorization.k8s.io/v1
metadata:
  name: kyverno:customresources
roleRef:
  apiGroup: rbac.authorization.k8s.io
  kind: ClusterRole
  name: kyverno:customresources
subjects:
- kind: ServiceAccount
  name: kyverno-service-account
  namespace: kyverno 
---
kind: ClusterRoleBinding
apiVersion: rbac.authorization.k8s.io/v1
metadata:
  name: kyverno:policycontroller
roleRef:
  apiGroup: rbac.authorization.k8s.io
  kind: ClusterRole
  name: kyverno:policycontroller
subjects:
- kind: ServiceAccount
  name: kyverno-service-account
  namespace: kyverno 
---  
kind: ClusterRoleBinding
apiVersion: rbac.authorization.k8s.io/v1
metadata:
  name: kyverno:generatecontroller
roleRef:
  apiGroup: rbac.authorization.k8s.io
  kind: ClusterRole
  name: kyverno:generatecontroller
subjects:
- kind: ServiceAccount
  name: kyverno-service-account
  namespace: kyverno 
---
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRole
metadata:
  name: kyverno:webhook
rules:
# Dynamic creation of webhooks, events & certs
- apiGroups:
  - '*'
  resources:
  - events
  - mutatingwebhookconfigurations
  - validatingwebhookconfigurations
  - certificatesigningrequests
  - certificatesigningrequests/approval
  verbs:
  - create
  - delete
  - get 
  - list
  - patch
  - update
  - watch
- apiGroups:
  - certificates.k8s.io
  resources:
  - certificatesigningrequests
  - certificatesigningrequests/approval
  - certificatesigningrequests/status
  resourceNames:
    - kubernetes.io/legacy-unknown
  verbs:
  - create
  - delete
  - get 
  - update
  - watch
- apiGroups:
  - certificates.k8s.io
  resources:
  - signers
  resourceNames:
  - kubernetes.io/legacy-unknown
  verbs:
  - approve 
---
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRole
metadata:
  name: kyverno:userinfo
rules:
# get the roleRef for incoming api-request user
- apiGroups:
  - "*"
  resources:
  - rolebindings
  - clusterrolebindings
  - configmaps
  verbs:
  - watch
---
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRole
metadata:
  name: kyverno:customresources
rules:
# Kyverno CRs
- apiGroups:
  - '*'
  resources:
  - clusterpolicies
  - clusterpolicies/status
  - clusterpolicyviolations
  - clusterpolicyviolations/status
  - policyviolations
  - policyviolations/status
  - generaterequests
  - generaterequests/status
  verbs:
  - create
  - delete
  - get 
  - list 
  - patch
  - update
  - watch
---  
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRole
metadata:
  name: kyverno:policycontroller
rules:
# background processing, identify all existing resources
- apiGroups:
  - '*'
  resources:
  - '*'
  verbs:
  - get
  - list
  - update
---
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRole
metadata:
  name: kyverno:generatecontroller
rules:
# process generate rules to generate resources
- apiGroups:
  - "*"
  resources:
  - namespaces
  - networkpolicies
  - secrets
  - configmaps
  - resourcequotas
  - limitranges
  - clusterroles
  - rolebindings
  - clusterrolebindings
  verbs:
  - create
  - update
  - delete
  - get
# dynamic watches on trigger resources for generate rules
# re-evaluate the policy if the resource is updated
- apiGroups:
  - '*'
  resources:
  - namespaces
  verbs:
  - watch
---
apiVersion: v1
kind: ConfigMap
metadata:
  name: init-config
  namespace: kyverno
data:
  # resource types to be skipped by kyverno policy engine
  resourceFilters: "[Event,*,*][*,kube-system,*][*,kube-public,*][*,kube-node-lease,*][Node,*,*][APIService,*,*][TokenReview,*,*][SubjectAccessReview,*,*][*,kyverno,*][Binding,*,*][ReplicaSet,*,*]"
---
apiVersion: apps/v1
kind: Deployment
metadata:
  namespace: kyverno
  name: kyverno
  labels:
    app: kyverno
spec:
  selector:
    matchLabels:
      app: kyverno
  replicas: 1
  template:
    metadata:
      labels:
        app: kyverno
    spec:
      serviceAccountName: kyverno-service-account
      initContainers:
        - name: kyverno-pre
          image: nirmata/kyvernopre:v1.1.6-rc1
      containers:
        - name: kyverno
          image: registry-v2.nirmata.io/nirmata/kyverno:latest
          imagePullPolicy: Always
          args:
          - "--filterK8Resources=[Event,*,*][*,kube-system,*][*,kube-public,*][*,kube-node-lease,*][Node,*,*][APIService,*,*][TokenReview,*,*][SubjectAccessReview,*,*][*,kyverno,*][Binding,*,*][ReplicaSet,*,*]"
          # customize webhook timeout
          #- "--webhooktimeout=4"
          - "-v=2"
          ports:
          - containerPort: 443
          env:
          - name: INIT_CONFIG
            value: init-config
          resources:
            requests:
              memory: "50Mi"
              cpu: "100m"
            limits:
              memory: "128Mi"
