You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/tutorials/custom-analyzers.md
+45-40Lines changed: 45 additions & 40 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -5,7 +5,7 @@ We will create a custom analyzer that checks a Linux host for resource issues an
5
5
6
6
[Full example code](https://github.com/k8sgpt-ai/go-custom-analyzer)
7
7
8
-
###Why?
8
+
## Why?
9
9
10
10
There are usecases where you might want to create custom analyzers to check for specific issues in your environment. This would be in conjunction with the K8sGPT built-in analyzers.
11
11
For example, you may wish to scan the Kubernetes cluster nodes more deeply to understand if there are underlying issues that are related to issues in the cluster.
@@ -15,8 +15,6 @@ For example, you may wish to scan the Kubernetes cluster nodes more deeply to un
-[Golang](https://golang.org/doc/install) go1.22 or higher
17
17
18
-
19
-
20
18
### Writing a simple analyzer
21
19
22
20
The K8sGPT CLI, operator and custom analyzers all use a GRPC API to communicate with each other. The API is defined in the [buf.build/k8sgpt-ai/k8sgpt](https://buf.build/k8sgpt-ai/k8sgpt/docs/main:schema.v1) repository. Buf is a tool that helps you manage Protobuf files. You can install it by following the instructions [here](https://docs.buf.build/installation).
@@ -39,18 +37,19 @@ Once we have this structure let's create a simple main.go file with the followin
@@ -120,35 +122,29 @@ func (a *Handler) Run(context.Context, *v1.AnalyzerRunRequest) (*v1.AnalyzerRunR
120
122
```
121
123
122
124
This file contains the `Handler` struct which implements the `Run` method. This method is called when the analyzer is run. In this example, we are returning an error message.
123
-
The `Run` method takes a context and an `AnalyzerRunRequest` as arguments and returns an `AnalyzerRunResponse` and an error. Find the API available [here](https://buf.build/k8sgpt-ai/k8sgpt/file/main:schema/v1/analyzer.proto#L16).
125
+
The `Run` method takes a context and an `RunRequest` as arguments and returns an `RunResponse` and an error. Find the API available [here](https://buf.build/k8sgpt-ai/k8sgpt/file/1379a5a1889d4bf49494b2e2b8e36164:schema/v1/custom_analyzer.proto).
124
126
125
127
### Implementing some custom logic
126
128
127
129
Now that we have the basic structure in place, let's implement some custom logic. We will check the disk usage on the host and return an error if it is above a certain threshold.
Details: fmt.Sprintf("Disk usage is %d", diskUsage),
141
+
Error: []*v1.ErrorDetail{
142
+
{
143
+
Text: fmt.Sprintf("Disk usage is %d", diskUsage),
147
144
},
148
145
},
149
-
}
150
-
}
151
-
return response, nil
146
+
},
147
+
}, nil
152
148
}
153
149
```
154
150
@@ -157,22 +153,31 @@ func (a *Handler) Run(context.Context, *v1.AnalyzerRunRequest) (*v1.AnalyzerRunR
157
153
To test this with K8sGPT we need to update the local K8sGPT CLI configuration to point to the custom analyzer. We can do this by running the following command:
This will add the custom analyzer `diskuse` to the list of available analyzers in the K8sGPT CLI.
160
+
161
+
```bash
162
+
k8sgpt custom-analyzer list
163
+
Active:
164
+
> diskuse
166
165
```
167
166
168
-
This will add the custom analyzer to the list of available analyzers in the K8sGPT CLI.
169
167
To execute the analyzer we can run the following command:
170
168
169
+
- run the customer analyzer
170
+
171
+
```bash
172
+
go run main.go
173
+
```
174
+
175
+
- execute the analyzer
176
+
171
177
```bash
172
178
k8sgpt analyze --custom-analysis
173
179
```
174
180
175
181
## What's next?
176
182
177
183
Now you've got the basics of how to write a custom analyzer, you can extend this to check for other issues on your hosts or in your Kubernetes cluster. You can also create more complex analyzers that check for multiple issues and provide more detailed recommendations.
0 commit comments