Skip to content
This repository was archived by the owner on May 2, 2024. It is now read-only.

feat(replicated-library): add preflight pre-install hook#78

Merged
xavpaice merged 7 commits into
mainfrom
add-preflight
May 23, 2023
Merged

feat(replicated-library): add preflight pre-install hook#78
xavpaice merged 7 commits into
mainfrom
add-preflight

Conversation

@DexterYan

Copy link
Copy Markdown
Contributor

This is another approach to add feature to sc-75516

@DexterYan

DexterYan commented May 19, 2023

Copy link
Copy Markdown
Contributor Author

example

troubleshoot:
  support-bundle:
  ....
  preflights:
    my-preflights:
      image: replicated/preflight:latest
      enabled: true
      collectors:
        - run:
            collectorName: "static-hi"
            image: "alpine:3"
            command: ["echo", "hi static!"]
      analyzers:
        - textAnalyze:
            checkName: Said hi!
            fileName: /static-hi.log
            regex: "hi static"
            outcomes:
              - fail:
                  message: Didn't say hi.
              - pass:
                  message: Said hi!


output

---
# Source: wg-easy/templates/replicated-library.yaml
apiVersion: v1
kind: ServiceAccount
metadata:
  name: test-preflight-my-preflights
  labels:
    helm.sh/chart: wg-easy-0.1.0
    app.kubernetes.io/name: wg-easy
    app.kubernetes.io/instance: test
    app.kubernetes.io/version: "7"
    app.kubernetes.io/managed-by: Helm
    troubleshoot.io/kind: preflight
  annotations:
    "helm.sh/hook": pre-install, pre-upgrade
    "helm.sh/hook-weight": "-6"
    "helm.sh/hook-delete-policy": before-hook-creation, hook-succeeded, hook-failed
secrets:
  - name: test-wg-easy-preflight-my-preflights
---
# Source: wg-easy/templates/replicated-library.yaml
apiVersion: v1
kind: Secret
metadata:
  name: test-wg-easy-preflight-my-preflights
  labels:
    helm.sh/chart: wg-easy-0.1.0
    app.kubernetes.io/name: wg-easy
    app.kubernetes.io/instance: test
    app.kubernetes.io/version: "7"
    app.kubernetes.io/managed-by: Helm
    troubleshoot.io/kind: preflight
  annotations:
    "helm.sh/hook": pre-install, pre-upgrade
    "helm.sh/hook-weight": "-6"
    "helm.sh/hook-delete-policy": before-hook-creation, hook-succeeded, hook-failed
stringData:
  preflight.yaml: |-
    apiVersion: troubleshoot.sh/v1beta2
    kind: Preflight
    metadata:
      name: test-wg-easy-preflight-my-preflights
    spec:
      collectors:      
        - run:
            collectorName: static-hi
            command:
            - echo
            - hi static!
            image: alpine:3
      analyzers:      
        - textAnalyze:
            checkName: Said hi!
            fileName: /static-hi.log
            outcomes:
            - fail:
                message: Didn't say hi.
            - pass:
                message: Said hi!
            regex: hi static
---
# Source: wg-easy/templates/replicated-library.yaml
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRole
metadata:
  name: test-preflight-my-preflights
  labels:
    helm.sh/chart: wg-easy-0.1.0
    app.kubernetes.io/name: wg-easy
    app.kubernetes.io/instance: test
    app.kubernetes.io/version: "7"
    app.kubernetes.io/managed-by: Helm
    troubleshoot.io/kind: preflight
  annotations:
    "helm.sh/hook": pre-install, pre-upgrade
    "helm.sh/hook-weight": "-6"
    "helm.sh/hook-delete-policy": before-hook-creation, hook-succeeded, hook-failed
rules:
  - apiGroups:
      - ""
    resources: 
      - "namespaces"
    verbs: 
      - "get"
      - "watch"
      - "list"
  - apiGroups:
      - ""
    resources: 
      - "nodes"
    verbs: 
      - "get"
      - "watch"
      - "list"
  - apiGroups:
      - ""
    resources: 
      - "pods"
    verbs: 
      - "get"
      - "watch"
      - "list"
      - "create"
  - apiGroups: 
      - "apiextensions.k8s.io"
    resources: 
      - "customresourcedefinitions"
    verbs: 
      - "get"
      - "watch"
      - "list"
  - apiGroups: 
      - "storage.k8s.io"
    resources: 
      - "storageclasses"
    verbs: 
      - "get"
      - "watch"
      - "list"
  - apiGroups:
      - ""
    resources: 
      - "pods/log"
    verbs: 
      - "get"
      - "list"
---
# Source: wg-easy/templates/replicated-library.yaml
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRoleBinding
metadata:
  name: test-preflight-my-preflights
  labels:
    helm.sh/chart: wg-easy-0.1.0
    app.kubernetes.io/name: wg-easy
    app.kubernetes.io/instance: test
    app.kubernetes.io/version: "7"
    app.kubernetes.io/managed-by: Helm
    troubleshoot.io/kind: preflight
  annotations:
    "helm.sh/hook": pre-install, pre-upgrade
    "helm.sh/hook-weight": "-6"
    "helm.sh/hook-delete-policy": before-hook-creation, hook-succeeded, hook-failed
subjects:
- kind: ServiceAccount
  name: test-preflight-my-preflights
  namespace: default
roleRef:
  kind: ClusterRole 
  name: test-preflight-my-preflights
  apiGroup: rbac.authorization.k8s.io
---
# Source: wg-easy/templates/replicated-library.yaml
apiVersion: v1
kind: Pod
metadata:
  name: test-preflight-check
  labels:
    helm.sh/chart: wg-easy-0.1.0
    app.kubernetes.io/name: wg-easy
    app.kubernetes.io/instance: test
    app.kubernetes.io/version: "7"
    app.kubernetes.io/managed-by: Helm
    troubleshoot.io/kind: preflight
  annotations:
    "helm.sh/hook": pre-install, pre-upgrade
    "helm.sh/show-output": "true"
    "helm.sh/hook-weight": "-5"
    "helm.sh/hook-delete-policy": before-hook-creation, hook-succeeded, hook-failed
    "helm.sh/hook-output-log-policy": hook-failed, hook-succeeded
spec:
  serviceAccountName: test-preflight-my-preflights
  restartPolicy: Never
  volumes:
    - name: preflights
      secret:
        secretName: test-wg-easy-preflight-my-preflights
  containers:
    - name: pre-install-job
      image:  replicated/preflight:latest
      command:
        - "preflight"
        - "--interactive=false"
        - "/preflights/preflight.yaml"
      volumeMounts:
        - name: preflights
          mountPath: /preflights

@DexterYan
DexterYan marked this pull request as draft May 19, 2023 06:30
@chris-sanders

Copy link
Copy Markdown
Contributor

This looks really cool, FYI we're looking at moving some file paths #80 this will impact where some of your files live. Just keep an eye on that PR.


---
apiVersion: v1
kind: ServiceAccount

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

suspect the service account, and RBAC roles, need to be setup using the library's RBAC tooling

@DexterYan

Copy link
Copy Markdown
Contributor Author

if using

preflights:
    my-preflights:
      image: replicated/preflight:latest
      enabled: true
      analyzers:
        - clusterVersion:
            outcomes:
              - fail:
                  when: "< 1.20.0"
                  message: This application requires at least Kubernetes 1.20.0, and recommends 1.22.0.
                  uri: https://www.kubernetes.io
              - warn:
                  when: "< 1.22.0"
                  message: Your cluster meets the minimum version of Kubernetes, but we recommend you update to 1.22.0 or later.
                  uri: https://kubernetes.io
              - pass:
                  when: ">= 1.22.0"
                  message: Your cluster meets the recommended and required versions of Kubernetes.

it is working as expected when the pod is running success.

However, if I am using

  preflights:
    my-preflights:
      image: replicated/preflight
      enabled: true
      collectors:
        - clusterInfo: {}
        - clusterResources: {}

The pod will fail, then the pod had been deleted automatically. However, sa, clusterrole, clusterrolebinding had not been deleted. Seems hook-failed has not trigger those delete

@DexterYan
DexterYan marked this pull request as ready for review May 23, 2023 01:06
@xavpaice
xavpaice merged commit a103840 into main May 23, 2023
@xavpaice
xavpaice deleted the add-preflight branch May 23, 2023 02:36
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants