Skip to content

Commit c417467

Browse files
committed
Add investigation bootstrap script and make target
1 parent d9e5489 commit c417467

File tree

3 files changed

+125
-1
lines changed

3 files changed

+125
-1
lines changed

Makefile

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,11 @@ test-interceptor: check-go121-install check-jq-install check-vault-install build
7777
@echo "Running e2e tests for interceptor..."
7878
cd interceptor && ./test/e2e.sh
7979

80+
##@ Boilerplate:
81+
.PHONY: boilerplate
82+
bootstrap-investigation: ## Bootstrap a new boilerplate investigation
83+
@cd hack && ./bootstrap-investigation.sh
84+
8085
##@ Template-updater:
8186
.PHONY: template-updater
8287
template-updater: build-template-updater lint-template-updater ## Run all targets for template-updater

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ The required investigation is identified by CAD based on the incident and its pa
6666
As PagerDuty itself does not provide finer granularity for webhooks than service-based, CAD filters out the alerts it should investigate. For more information, please refer to https://support.pagerduty.com/docs/webhooks.
6767

6868
To add a new alert investigation:
69-
- create a mapping for the alert to the `GetInvestigation` function in `mapping.go` and write a corresponding CAD investigation (e.g. `Investigate()` in `chgm.go`).
69+
- run `make bootstrap-investigation` to generate boilerplate code in `pkg/investigations` (This creates the corresponding folder & .go file, and also appends the investigation to the `availableInvestigations` interface in `registry.go`.).
7070
- if the alert is not yet routed to CAD, add a webhook to the service your alert fires on. For production, the service should also have an escalation policy that escalates to SRE on CAD automation timeout.
7171

7272
## Testing locally

hack/bootstrap-investigation.sh

Lines changed: 119 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
1+
#!/usr/bin/env bash
2+
3+
set -e
4+
5+
read -p "Enter the new investigation (package) name: " INVESTIGATION_NAME
6+
if [[ "${INVESTIGATION_NAME}" == "" ]] ; then
7+
echo "Investigation name cannot be empty."
8+
exit 1
9+
elif [[ "${INVESTIGATION_NAME}" =~ [^a-zA-Z0-9_] ]] ; then
10+
echo "Investigation name must be alphanumeric."
11+
exit 1
12+
fi
13+
14+
read -p "Enter new investigation description: " INVESTIGATION_DESCRIPTION
15+
if [[ "${INVESTIGATION_DESCRIPTION}" == "" ]] ; then
16+
INVESTIGATION_DESCRIPTION="TODO"
17+
fi
18+
19+
read -p "Should Investigate Alert (y/n): " INVESTIGATE_ALERT_BOOL
20+
if [[ "${INVESTIGATE_ALERT_BOOL}" == "y" ]] ; then
21+
read -p "Investigation alert string: " INVESTIGATION_ALERT_STRING
22+
INVESTIGATION_ALERT="strings.Contains(alert, ${INVESTIGATION_ALERT_STRING})"
23+
elif [[ "${INVESTIGATE_ALERT_BOOL}" == "n" ]] ; then
24+
INVESTIGATION_ALERT="false"
25+
else
26+
echo "Invalid input. Please enter 'y' or 'n'."
27+
exit 1
28+
fi
29+
30+
INVESTIGATION_NAME=$(echo "${INVESTIGATION_NAME}" | tr '[:upper:]' '[:lower:]')
31+
32+
INVESTIGATION_DIR="../pkg/investigations/${INVESTIGATION_NAME}"
33+
34+
if [ -d "${INVESTIGATION_DIR}" ]; then
35+
echo "Investigation of name ${INVESTIGATION_NAME} already exists."
36+
exit 1
37+
fi
38+
39+
mkdir -p "${INVESTIGATION_DIR}"
40+
ls "${INVESTIGATION_DIR}"
41+
42+
touch "${INVESTIGATION_DIR}/${INVESTIGATION_NAME}.go"
43+
touch "${INVESTIGATION_DIR}/metadata.yaml"
44+
touch "${INVESTIGATION_DIR}/README.md"
45+
46+
# Create README.md file
47+
cat <<EOF > "${INVESTIGATION_DIR}/README.md"
48+
# ${INVESTIGATION_NAME} Investigation
49+
50+
*TODO*
51+
52+
EOF
53+
54+
# Create metadata.yaml file
55+
cat <<EOF > "${INVESTIGATION_DIR}/metadata.yaml"
56+
name: ${INVESTIGATION_NAME}
57+
rbac:
58+
roles: []
59+
clusterRoleRules: []
60+
customerDataAccess: false
61+
62+
EOF
63+
64+
# Create boilerplate investigation file
65+
cat <<EOF > "${INVESTIGATION_DIR}/${INVESTIGATION_NAME}.go"
66+
// Package ${INVESTIGATION_NAME} contains...TODO
67+
package ${INVESTIGATION_NAME}
68+
69+
import (
70+
"strings"
71+
72+
"github.com/openshift/configuration-anomaly-detection/pkg/investigations/investigation"
73+
"github.com/openshift/configuration-anomaly-detection/pkg/logging"
74+
"github.com/openshift/configuration-anomaly-detection/pkg/notewriter"
75+
)
76+
77+
type Investigation struct{}
78+
79+
func (c *Investigation) Run(r *investigation.Resources) (investigation.InvestigationResult, error) {
80+
result := investigation.InvestigationResult{}
81+
82+
// Initialize PagerDuty note writer
83+
notes := notewriter.New(r.Name, logging.RawLogger)
84+
85+
// TODO: Implement investigation logic here
86+
87+
return result, r.PdClient.EscalateIncidentWithNote(notes.String())
88+
}
89+
90+
func (c *Investigation) Name() string {
91+
return "${INVESTIGATION_NAME}"
92+
}
93+
94+
func (c *Investigation) Description() string {
95+
return "${INVESTIGATION_DESCRIPTION}"
96+
}
97+
98+
func (c *Investigation) ShouldInvestigateAlert(alert string) bool {
99+
return ${INVESTIGATION_ALERT}
100+
}
101+
102+
func (c *Investigation) IsExperimental() bool {
103+
// TODO: Update to false when graduating to production.
104+
return true
105+
}
106+
107+
EOF
108+
109+
echo "${INVESTIGATION_NAME} created in ${INVESTIGATION_DIR}"
110+
echo "metadata.yaml file created in ${INVESTIGATION_DIR}"
111+
112+
# Update registry.go to contain new investigation
113+
if ! grep -q "${INVESTIGATION_NAME}" ../pkg/investigations/registry.go && ! grep -q "${INVESTIGATION_NAME}" ../pkg/investigations/registry.go; then
114+
sed -i "/import (/a \\\t\"github.com/openshift/configuration-anomaly-detection/pkg/investigations/${INVESTIGATION_NAME}\"" ../pkg/investigations/registry.go
115+
sed -i "/var availableInvestigations = \[/a \\\t&${INVESTIGATION_NAME}.Investigation{}," ../pkg/investigations/registry.go
116+
echo "${INVESTIGATION_NAME} added to registry.go"
117+
else
118+
echo "${INVESTIGATION_NAME} already exists in registry.go"
119+
fi

0 commit comments

Comments
 (0)