Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Playbooks #246

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions charts/kubernetes/.yamllint
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
extends: default

rules:
line-length:
max: 400
comments:
min-spaces-from-content: 1
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ kind: Playbook
metadata:
name: edit-kubernetes-manifests-gitops
spec:
title: 'Edit Kustomize Resource'
title: 'Edit'
icon: flux
parameters:
- default: 'chore: update $(.config.type)/$(.config.name)'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ spec:
labelSelector: 'kustomize.toolkit.fluxcd.io/name' # limits to namespaces created by flux
title: Request Namespace Access (GitOps)
category: Kubernetes
icon: add-shield
icon: k8s-namespace
description: Adds a Rolebinding to the given role in the selected Namespace
parameters:
- name: user
Expand Down Expand Up @@ -97,4 +97,4 @@ spec:
files:
- path: '$(.env.kustomize.path)/rolebinding-$(.config.name)-$(run.id).yaml'
content: '$delete'
{{- end}}
{{- end}}
106 changes: 106 additions & 0 deletions charts/kubernetes/templates/playbooks/kubectl-logs.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
# yaml-language-server: $schema=https://raw.githubusercontent.com/flanksource/duty/main/schema/openapi/playbook.schema.json
{{- if and .Values.playbooks.enabled .Values.playbooks.kubectlLogs }}
apiVersion: mission-control.flanksource.com/v1
kind: Playbook
metadata:
name: kubectl-logs
spec:
runsOn:
- {{` "{{- if .agent }}{{.agent.id}}{{ else }}local{{ end }}" `}}
actions:
- exec:
script: |
# escape delims passed through to stern
left="{""{"
right="}""}"

function template() {
local -n outvar=$1
outvar="$left $2 $right"
}

ns=" -n $(.config.tags.namespace)"
type=" $(.config.type)"
name="$(.config.name)"
template pod "color .PodColor .PodName"
template namespace ".Namespace"
template node ".NodeName"
template container "color .ContainerColor .ContainerName"
template msg ".Message"
template nl '"\n"'
stern="stern --color always --container-state all --no-follow --since $(.params.since) --tail $(.params.lines)"

case $type in
Kubernetes::Namespace)
$stern --template "$namespace/$pod $msg $nl" -n $name ".*"
;;
Kubernetes::Pod)
$stern --template "$container $msg $nl" $name $ns
;;
Kubernetes::Statefulset)
$stern --template "$pod $container $msg $nl" statefulset/$name $ns
;;
Kubernetes::ReplicaSet)
$stern --template "$pod $container $msg $nl" replicaset/$name $ns
;;
Kubernetes::Deployment)
$stern --template "$pod $container $msg $nl" deployment/$name $ns
;;
Kubernetes::Job)
$stern --template "$pod $container $msg $nl" job/$name $ns
;;
Kubernetes::DaemonSet)
$stern --template "$node $msg $nl" ds/$name $ns
;;
Kubernetes::Service)
$stern --template "$pod $container $msg $nl" svc/$name $ns
;;
Kubernetes::Node)
$stern --node $name $ns
;;
*)
echo -n "unknown type: $type"
;;
esac
name: Stern
category: Kubernetes
configs:
- types:
- Kubernetes::Pod
- Kubernetes::Deployment
- Kubernetes::Statefulset
- Kubernetes::Namespace
- Kubernetes::ReplicaSet
- Kubernetes::Job
- Kubernetes::Node
- Kubernetes::Service
- Kubernetes::DaemonSet
icon: kubernetes
parameters:
- default: 1h
label: Since
name: since
properties:
options:
- label: 1h
value: 1h
- label: 2h
value: 2h
- label: 4h
value: 4h
- label: 8h
value: 8h
- label: 12h
value: 12h
- label: 1d
value: 24h
- label: 3d
value: 72h
type: list
- default: "5000"
label: Max Lines
name: lines
type: text
title: Kubectl Logs
{{- end }}

Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ metadata:
name: request-namespace-access
spec:
title: Request Namespace Access
icon: add-shield
icon: k8s-namespace
category: Kubernetes
description: Adds a Rolebinding to the given role
parameters:
Expand Down Expand Up @@ -55,4 +55,4 @@ spec:
exec:
script: |
kubectl delete rolebinding "playbook-$(.run.id)" --namespace=$(.config.name)
{{- end}}
{{- end}}
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ kind: Playbook
metadata:
name: update-resource-image
spec:
title: Update image
icon: k8s-deployment
title: Update Image
icon: docker
category: Kubernetes
configs:
- types:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ kind: Playbook
metadata:
name: update-resource-requests-limits
spec:
title: Update Resource Requests and Limits
icon: k8s-deployment
title: Update Resources
icon: scale-up
category: Kubernetes
configs:
- types:
Expand All @@ -15,19 +15,19 @@ spec:
parameters:
- name: cpu_request
label: CPU Request
type: text
type: millicores
default: '{{` {{ (index .config.config.spec.template.spec.containers 0).resources.requests.cpu | default "" }} `}}'
- name: memory_request
label: Memory Request
type: text
type: bytes
default: '{{` {{ (index .config.config.spec.template.spec.containers 0).resources.requests.memory | default "" }} `}}'
- name: cpu_limit
label: CPU Limit
type: text
type: millicores
default: '{{` {{ (index .config.config.spec.template.spec.containers 0).resources.limits.cpu | default "" }} `}}'
- name: memory_limit
label: Memory Limit
type: text
type: bytes
default: '{{` {{ (index .config.config.spec.template.spec.containers 0).resources.limits.memory | default "" }} `}}'

env:
Expand Down
3 changes: 3 additions & 0 deletions charts/kubernetes/values.schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,9 @@
"createDeployment": {
"type": "boolean"
},
"kubectlLogs": {
"type": "boolean"
},
"deletePod": {
"type": "boolean"
},
Expand Down
13 changes: 7 additions & 6 deletions charts/kubernetes/values.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,12 @@ prometheus:
createConnection: true
connection: prometheus
url: # @schema type:object,null
# value: ''
# valueFrom: ''
# value: ''
# valueFrom: ''

# prometheus labels to inject: "\,label1=key1\,label2=key2\,label3=~key3"
# Note: comma(,) needs to be escaped as /, and prometheus.labels should always start with '/,' to work with existing labels
labels: ''
labels: ""
# Leave auth blank or use one of [username+password / bearer / oauth]
auth: # @schema type:object,null
# username:
Expand Down Expand Up @@ -66,7 +66,7 @@ metrics:
namespace_cpu: '1000 * sum(rate(container_cpu_usage_seconds_total{container!=""{{.Values.prometheus.labels | default .Values.prometheusLabels}}}[5m])) by (namespace)'
namespace_memory: 'sum(container_memory_working_set_bytes{container!="",pod!=""{{.Values.prometheus.labels | default .Values.prometheusLabels}}} * on(pod, namespace) group_left kube_pod_status_phase{phase="Running"{{.Values.prometheus.labels | default .Values.prometheusLabels}}} > 0) by (namespace)'
gke:
cluster_name: ''
cluster_name: ""
cluster_cpu: 'sum(rate(kubernetes_io:container_cpu_core_usage_time{container_name!="",cluster_name="{{.Values.metrics.queries.gke.cluster_name}}"{{.Values.prometheus.labels}}}[5m]))'
cluster_memory: 'sum(kubernetes_io:container_memory_used_bytes{cluster_name="{{.Values.metrics.queries.gke.cluster_name}}"{{.Values.prometheus.labels}}})'
node_cpu: 'sum(rate(label_replace(kubernetes_io:node_cpu_core_usage_time{cluster_name="{{.Values.metrics.queries.gke.cluster_name}}"{{.Values.prometheus.labels}}}, "node","$1","node_name", "(.+)")[5m:])) by (node)'
Expand All @@ -82,7 +82,7 @@ topology:
schedule: "@every 5m"
icon: kubernetes
groupBy:
tag: ''
tag: ""
selector: {}

scraper:
Expand Down Expand Up @@ -113,6 +113,7 @@ scraper:
- podmetrics.metrics.k8s.io
- nodemetrics.metrics.k8s.io
- customresourcedefinition
- componentstatuses
- controllerrevisions
- certificaterequests
- orders.acme.cert-manager.io
Expand Down Expand Up @@ -225,7 +226,7 @@ playbooks:
# If this is set to false, no playbooks will be created
# If true, all individual playbook values will be honored
enabled: true

kubectlLogs: true
deletePod: true
cleanupFailedPods: true
podSnapshot: false
Expand Down
Loading