Skip to content

Commit b13ab1f

Browse files
Add helper functions for running and logging (#7)
* Improve Makefile - Added a target that checks if Docker or Podman are installed and sets the variable DOCKER_OR_PODMAN to the executable. - Add and set the default target to help - Set helpful variables for the Makefile - Check if shellcheck is installed before linting - Improved the container image variables Signed-off-by: Panagiotis Georgiadis <pgeorgia@redhat.com> * Add functions for creating directories and logging command output - create_directory: function that creates a directory if it doesn't already exist, but logs an error and exits if the directory already exists - run_and_log: function that runs a command and logs the output to a file. If the command fails, it will retry once. If the command fails again, the error message will be logged to the error log file. - Added error, no-output counters and log files - Added exit status check and removed unnecessary parts - The exit_if_binary_not_installed function now calls the run_and_log function to log the command output. - The exit_if_not_openshift function now calls the run_and_log function to log the command output and logs an error message if it's not an OpenShift cluster and exits. - All the commands were modified to use the new functionallity. Signed-off-by: Panagiotis Georgiadis <pgeorgia@redhat.com> * Add Testing info Signed-off-by: Panagiotis Georgiadis <pgeorgia@redhat.com> * Return exit code properly from run_and_log() Signed-off-by: Panagiotis Georgiadis <pgeorgia@redhat.com> * Fix typos in README Signed-off-by: Panagiotis Georgiadis <pgeorgia@redhat.com> * Remove duplicate entries and change managed_dir Signed-off-by: Panagiotis Georgiadis <pgeorgia@redhat.com> * Update README tree Signed-off-by: Panagiotis Georgiadis <pgeorgia@redhat.com> * Update README.md * Update README.md Signed-off-by: Panagiotis Georgiadis <pgeorgia@redhat.com> Co-authored-by: Regina Scott <50851526+reginapizza@users.noreply.github.com>
1 parent 5e3bc95 commit b13ab1f

File tree

4 files changed

+526
-171
lines changed

4 files changed

+526
-171
lines changed

.github/workflows/lint.yml

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
name: static checks
2+
on:
3+
workflow_dispatch:
4+
push:
5+
branches:
6+
- "main"
7+
pull_request:
8+
branches:
9+
- "*"
10+
11+
jobs:
12+
gofmt:
13+
name: "Ensure the code is shellchecked"
14+
runs-on: ubuntu-latest
15+
steps:
16+
- uses: actions/checkout@master
17+
- name: "ShellCheck"
18+
run: make lint

Makefile

Lines changed: 44 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -12,42 +12,51 @@
1212
## See the License for the specific language governing permissions and
1313
## limitations under the License.
1414

15-
default: build
16-
17-
HUB ?= quay.io/redhat-developer
18-
TAG ?= latest
19-
20-
lint:
21-
@if command -v shellcheck >/dev/null; then \
22-
find . -name '*.sh' -print0 | xargs -0 -r shellcheck; \
23-
else \
24-
echo "shellcheck not found, installing it now..."; \
25-
if command -v apt-get >/dev/null; then \
26-
sudo apt-get install -y shellcheck; \
27-
elif command -v yum >/dev/null; then \
28-
sudo yum install -y shellcheck; \
29-
elif command -v dnf >/dev/null; then \
30-
sudo dnf install -y shellcheck; \
31-
elif command -v pacman >/dev/null; then \
32-
sudo pacman -S --noconfirm shellcheck; \
33-
else \
34-
echo "shellcheck not found and unable to install it automatically"; \
35-
echo "Please install shellcheck manually and run the lint target again"; \
36-
exit 1; \
37-
fi; \
38-
find . -name '*.sh' -print0 | xargs -0 -r shellcheck; \
15+
# Check if Docker or Podman is installed and set the variable DOCKER_OR_PODMAN to the executable.
16+
DOCKER_OR_PODMAN := $(shell command -v podman || command -v docker)
17+
18+
# Add all the targets that are not associated with a file for a PHONY.
19+
# This will prevent any errors if a file with the same name as a target is present in the directory.
20+
.PHONY: help lint image push clean
21+
22+
# Set the default target to help
23+
default: help
24+
25+
# Set the helpful variables for the Makefile
26+
DOCKERFILE_ROOT=$(shell pwd)
27+
28+
# Set the variables for the container image
29+
CONTAINER_REGISTRY ?= quay.io
30+
REGISTRY_USERNAME ?= redhat-developer
31+
CONTAINER_IMAGE_NAME ?= gitops-must-gather
32+
CONTAINER_IMAGE_TAG ?= latest
33+
CONTAINER_IMAGE_LINK ?= ${CONTAINER_REGISTRY}/${REGISTRY_USERNAME}/${CONTAINER_IMAGE_NAME}:${CONTAINER_IMAGE_TAG}
34+
35+
help: ## Display this help menu
36+
@grep -E '^[a-zA-Z_-]+:.*?## .*$$' $(MAKEFILE_LIST) | sort | awk 'BEGIN {FS = ":.*?## "}; {printf "\033[36m%-30s\033[0m %s\n", $$1, $$2}'
37+
38+
# Check if Docker or Podman is installed and if shellcheck is installed. If not, exit with an error.
39+
# This is done to prevent the user from running the commands that require Docker or Podman if they are not present.
40+
check-docker-podman:
41+
@if ! command -v docker >/dev/null && ! command -v podman >/dev/null; then \
42+
echo "Neither Docker nor Podman were found. Please install one of them and run the command again."; \
43+
exit 1; \
3944
fi
4045

41-
image:
42-
@if command -v podman >/dev/null; then \
43-
podman build -t ${HUB}/gitops-must-gather:${TAG}; \
44-
else \
45-
docker build -t ${HUB}/gitops-must-gather:${TAG}; \
46+
check-shellcheck:
47+
@if ! command -v shellcheck >/dev/null; then \
48+
echo "shellcheck was not found. Please install it and run the command again."; \
49+
exit 1; \
4650
fi
4751

48-
push: image
49-
@if command -v podman >/dev/null; then \
50-
podman push ${HUB}/gitops-must-gather:${TAG}; \
51-
else \
52-
docker push ${HUB}/gitops-must-gather:${TAG}; \
53-
fi
52+
lint: check-shellcheck ## Lint the shell scripts
53+
find . -name '*.sh' -print0 | xargs -0 -r $ shellcheck
54+
55+
image: check-docker-podman ## Build the image
56+
${DOCKER_OR_PODMAN} build -t ${CONTAINER_IMAGE_LINK} ${DOCKERFILE_ROOT}
57+
58+
push: image ## Push the image to the container registry
59+
${DOCKER_OR_PODMAN} push ${CONTAINER_IMAGE_LINK}
60+
61+
clean: check-docker-podman ## Clean up the built image
62+
${DOCKER_OR_PODMAN} rmi ${CONTAINER_IMAGE_LINK}

README.md

Lines changed: 175 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -1,86 +1,199 @@
11
# GitOps Operator Must-Gather
2-
=================
32

43
`GitOps must-gather` is a tool to gather information about the gitop-operator. It is built on top of [OpenShift must-gather](https://github.com/openshift/must-gather).
54

6-
### Usage
5+
## Usage
6+
77
```sh
88
oc adm must-gather --image=quay.io/redhat-developer/gitops-must-gather:latest
99
```
1010

1111
The command above will create a local directory with a dump of the OpenShift GitOps state. Note that this command will only get data related to the GitOps Operator in your OpenShift cluster.
1212

1313
You will get a dump of:
14+
1415
- Information for the subscription of the gitops-operator
1516
- The GitOps Operator namespace (and its children objects)
1617
- All namespaces where ArgoCD objects exist in, plus all objects in those namespaces, such as ArgoCD, Applications, ApplicationSets, and AppProjects, and configmaps
1718
- No secrets will be collected
18-
- A list of list of the namespaces that are managed by gitops-operator identified namespaces and resources from those namespaces.
19+
- A list of the namespaces that are managed by gitops-operator identified namespaces and resources from those namespaces.
1920
- All GitOps CRD's objects and definitions
2021
- Operator logs
2122
- Logs of Argo CD
2223
- Warning and error-level Events
2324

25+
In addition to that, we will get a summary of:
26+
27+
- All executed commands: `must-gather-script-commands.txt`
28+
- Errors: `must-gather-script-errors.txt`
29+
- Commands that produced no output: `must-gather-script-no-output.txt`
30+
31+
All the output of the commands is stored into 3 different formats:
32+
33+
- `*.txt` that represents the normal view without any structure.
34+
- `*.yaml` that is the YAML output of the command.
35+
- `*.json` that is the JSON output of the command.
36+
2437
In order to get data about other parts of the cluster (not specific to gitops-operator) you should run just `oc adm must-gather` (without passing a custom image). Run `oc adm must-gather -h` to see more options.
2538

2639
An example of the GitOps must-gather output would be something like the following, where there are two argocd instances in namespaces `openshift-gitops` and `foo` and an additional namespace called `foo-managed` which is managed by namespace `foo`:
27-
```
40+
41+
```shell
2842
cluster-gitops
29-
└── gitops
30-
├── appprojects.yaml
31-
├── crds.yaml
32-
├── namespace_openshift-gitops_resources
33-
│   ├── application_controller_logs.txt
34-
│   ├── applications
35-
│   ├── applicationsets
36-
│   ├── argocd.yaml
37-
│   ├── deployments
38-
│   │   ├── cluster.yaml
39-
│   │   └── kam.yaml
40-
│   ├── dex-server_logs.txt
41-
│   ├── error-events.txt
42-
│   ├── pods
43-
│   │   ├── cluster-5db4b95547-rdz2m.yaml
44-
│   │   └── kam-fff7f474f-d27c8.yaml
45-
│   ├── redis_logs.txt
46-
│   ├── replicasets
47-
│   │   ├── cluster-5db4b95547.yaml
48-
│   │   └── kam-fff7f474f.yaml
49-
│   ├── repo-server_logs.txt
50-
│   ├── routes
51-
│   │   └── kam.yaml
52-
│   ├── server_logs.txt
53-
│   ├── services
54-
│   │   ├── cluster.yaml
55-
│   │   └── kam.yaml
56-
│   ├── statefulsets
57-
│   └── warning-events.txt
58-
├── namespace_foo_resources
59-
│   ├── application_controller_logs.txt
60-
│   ├── applications
61-
│   │   └── guestbook.yaml
62-
│   ├── applicationsets
63-
│   │   └── guestbook.yaml
64-
│   ├── argocd.yaml
65-
│   ├── deployments
66-
│   ├── dex-server_logs.txt
67-
│   ├── error-events.txt
68-
│   ├── managedNamespace_foo-managed
69-
│   │   ├── deployments
70-
│   │   ├── pods
71-
│   │   ├── replicasets
72-
│   │   ├── routes
73-
│   │   ├── services
74-
│   │   └── statefulsets
75-
│   ├── pods
76-
│   ├── redis_logs.txt
77-
│   ├── replicasets
78-
│   ├── repo-server_logs.txt
79-
│   ├── routes
80-
│   ├── server_logs.txt
81-
│   ├── services
82-
│   ├── statefulsets
83-
│   └── warning-events.txt
84-
├── oc-version.txt
85-
└── subscription.yaml
86-
```
43+
└── gitops
44+
├── clusterversion.txt
45+
├── crds
46+
│ ├── crds.json/.txt./.yaml
47+
├── managedNamespace_foo-managed
48+
│ └── resources
49+
│ ├── deployments
50+
│ ├── pods
51+
│ ├── replicasets
52+
│ ├── routes
53+
│ ├── services
54+
│ └── statefulsets
55+
├── namespace_foo_resources
56+
│ ├── argocd
57+
│ │ ├── applications
58+
│ │ │ ├── applications.json/.txt./.yaml
59+
│ │ │ └── guestbook.yaml
60+
│ │ ├── applicationsets
61+
│ │ │ ├── applicationsets.txt
62+
│ │ │ ├── guestbook.json
63+
│ │ │ ├── guestbook.txt
64+
│ │ │ └── guestbook.yaml
65+
│ │ ├── argocd.json
66+
│ │ ├── argocd.txt
67+
│ │ ├── argocd.yaml
68+
│ │ ├── logs
69+
│ │ │ └── server-logs.txt
70+
│ │ ├── managed-namespaces.txt
71+
│ │ └── sourceNamespaces.txt
72+
│ ├── deployments
73+
│ │ ├── argocd-dex-server.json/.txt./.yaml
74+
│ │ ├── argocd-redis.json/.txt./.yaml
75+
│ │ ├── argocd-repo-server.json/.txt./.yaml
76+
│ │ ├── argocd-server.json/.txt./.yaml
77+
│ │ └── deployments.txt
78+
│ ├── events
79+
│ │ ├── all-events.txt
80+
│ │ └── warning-events.txt
81+
│ ├── pods
82+
│ │ ├── argocd-application-controller-0.json/.txt./.yaml
83+
│ │ ├── argocd-dex-server-69f99bdd45-g84b9.json/.txt./.yaml
84+
│ │ ├── argocd-dex-server-6d4f7d9d48-rkk9d.json/.txt./.yaml
85+
│ │ ├── argocd-redis-78d4849f68-pxxbp.json/.txt./.yaml
86+
│ │ ├── argocd-repo-server-6cfc8bbd5f-w4bsg.json/.txt./.yaml
87+
│ │ ├── argocd-server-5dc69475bf-98m6s.json/.txt./.yaml
88+
│ │ └── pods.txt
89+
│ ├── replicaSets
90+
│ │ ├── argocd-dex-server-69f99bdd45.json/.txt./.yaml
91+
│ │ ├── argocd-dex-server-6d4f7d9d48.json/.txt./.yaml
92+
│ │ ├── argocd-redis-78d4849f68.json/.txt./.yaml
93+
│ │ ├── argocd-repo-server-6cfc8bbd5f.json/.txt./.yaml
94+
│ │ ├── argocd-server-5dc69475bf.json/.txt./.yaml
95+
│ │ └── replicaSets.txt
96+
│ ├── routes
97+
│ │ ├── argocd-server.json/.txt./.yaml
98+
│ │ └── routes.txt
99+
│ ├── services
100+
│ │ ├── argocd-dex-server.json/.txt./.yaml
101+
│ │ ├── argocd-metrics.json/.txt./.yaml
102+
│ │ ├── argocd-redis.json/.txt./.yaml
103+
│ │ ├── argocd-repo-server.json/.txt./.yaml
104+
│ │ ├── argocd-server.json/.txt./.yaml
105+
│ │ ├── argocd-server-metrics.json/.txt./.yaml
106+
│ │ └── services.txt
107+
│ └── statefulsets
108+
│ ├── argocd-application-controller.json/.txt./.yaml
109+
│ └── statefulsets.txt
110+
├── namespace_openshift-gitops_resources
111+
│ ├── argocd
112+
│ │ ├── applications
113+
│ │ ├── applicationsets
114+
│ │ ├── appprojects
115+
│ │ ├── appprojects.json/.txt./.yaml
116+
│ │ ├── argocd.txt
117+
│ │ ├── logs
118+
│ │ │ ├── application-controller-logs.txt
119+
│ │ │ ├── dex-server-logs.txt
120+
│ │ │ ├── redis-logs.txt
121+
│ │ │ ├── repo-server-logs.txt
122+
│ │ │ └── server-logs.txt
123+
│ │ ├── openshift-gitops.json
124+
│ │ ├── openshift-gitops.txt
125+
│ │ ├── openshift-gitops.yaml
126+
│ │ └── sourceNamespaces.txt
127+
│ ├── deployments
128+
│ │ ├── cluster.json/.txt./.yaml
129+
│ │ ├── deployments.txt
130+
│ │ ├── kam.json
131+
│ │ ├── kam.txt
132+
│ │ ├── kam.yaml
133+
│ │ ├── openshift-gitops-applicationset-controller.json/.txt./.yaml
134+
│ │ ├── openshift-gitops-dex-server.json/.txt./.yaml
135+
│ │ ├── openshift-gitops-redis.json/.txt./.yaml
136+
│ │ ├── openshift-gitops-repo-server.json/.txt./.yaml
137+
│ │ ├── openshift-gitops-server.json/.txt./.yaml
138+
│ ├── events
139+
│ ├── pods
140+
│ │ ├── cluster-5db4b95547-mks98.json/.txt./.yaml
141+
│ │ ├── kam-fff7f474f-t875v.json/.txt./.yaml
142+
│ │ ├── openshift-gitops-application-controller-0.json/.txt./.yaml
143+
│ │ ├── openshift-gitops-applicationset-controller-5dbdfcc689-6x4vf.json/.txt./.yaml
144+
│ │ ├── openshift-gitops-dex-server-5bf6f4f684-ghtqf.json/.txt./.yaml
145+
│ │ ├── openshift-gitops-redis-664cdd4757-f9jcc.json/.txt./.yaml
146+
│ │ ├── openshift-gitops-repo-server-6795d6d8cd-x7hzc.json/.txt./.yaml
147+
│ │ ├── openshift-gitops-server-6cc58f9cc8-fx8g7.json/.txt./.yaml
148+
│ │ └── pods.txt
149+
│ ├── replicaSets
150+
│ │ ├── cluster-5db4b95547.json/.txt./.yaml
151+
│ │ ├── kam-fff7f474f.json/.txt./.yaml
152+
│ │ ├── openshift-gitops-applicationset-controller-5dbdfcc689.json/.txt./.yaml
153+
│ │ ├── openshift-gitops-dex-server-5bf6f4f684.json/.txt./.yaml
154+
│ │ ├── openshift-gitops-dex-server-684c85d5d7.json/.txt./.yaml
155+
│ │ ├── openshift-gitops-redis-664cdd4757.json/.txt./.yaml
156+
│ │ ├── openshift-gitops-repo-server-6795d6d8cd.json/.txt./.yaml
157+
│ │ ├── openshift-gitops-server-6cc58f9cc8.json/.txt./.yaml
158+
│ │ └── replicaSets.txt
159+
│ ├── routes
160+
│ │ ├── kam.json/.txt./.yaml
161+
│ │ ├── openshift-gitops-server.json/.txt./.yaml
162+
│ │ └── routes.txt
163+
│ ├── services
164+
│ │ ├── cluster.json/.txt./.yaml
165+
│ │ ├── kam.json/.txt./.yaml
166+
│ │ ├── openshift-gitops-applicationset-controller.json/.txt./.yaml
167+
│ │ ├── openshift-gitops-dex-server.json/.txt./.yaml
168+
│ │ ├── openshift-gitops-metrics.json/.txt./.yaml
169+
│ │ ├── openshift-gitops-redis.json/.txt./.yaml
170+
│ │ ├── openshift-gitops-repo-server.json/.txt./.yaml
171+
│ │ ├── openshift-gitops-server.json/.txt./.yaml
172+
│ │ ├── openshift-gitops-server-metrics.json/.txt./.yaml
173+
│ │ └── services.txt
174+
│ └── statefulsets
175+
│ ├── openshift-gitops-application-controller.json/.txt./.yaml
176+
│ └── statefulsets.txt
177+
├── oc-version.txt
178+
├── subscription.json/.txt./.yaml
179+
├── must-gather-script-commands.txt
180+
├── must-gather-script-no-output.txt
181+
└── must-gather-script-errors.txt
182+
```
183+
Note: most of the resource outputs are given in 3 file types: `.json`, `.yaml`, and `.txt`, however those files are combined in this tree for clarity and conciseness.
184+
185+
## Testing
186+
187+
You can run the script locally from your workstation.
188+
To do that you need an OpenShift cluster and you will have to install the Red Hat GitOps Operator.
189+
Then you can run the script like this:
190+
191+
```shell
192+
chmod +x ./gather_gitops.sh
193+
./gather_gitops.sh --base-collection-path .
194+
```
195+
196+
Last but not least, please make sure you run `make lint` before pushing new changes.
197+
This requires `shellcheck` to be installed in your machine.
198+
199+
For more information about `building` and `pushing` the image, see `make help`.

0 commit comments

Comments
 (0)