Skip to content

Commit

Permalink
Update to reference new builtin ds
Browse files Browse the repository at this point in the history
  • Loading branch information
sblausten committed Sep 20, 2023
1 parent ea30146 commit 159dfd5
Showing 1 changed file with 29 additions and 53 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -41,46 +41,46 @@ These are the steps to set that up.

1. Visit Tech Insights and click into the Data Sources tab. Click the ADD DATA SOURCE button to create a new data source.

![a button on a web interface](./add-ds-button.png)
![a button on a web interface](./add-ds-button.png)

2. Give the data source a sensible name, like “Dockerfile facts”, and a description like “Captures various facts about Dockerfiles at the root of each repo”.

![two filled inputs](./ds-name.png)
![two filled inputs](./ds-name.png)

3. In the Data Provider section, choose the type Component repository file, and set the location to “Dockerfile”.
4. In the field labelled “Select entity to test data source against”, choose a Backstage component which you know has the `github.com/project-slug` entity set on it’s `catalog-info.yaml` file, and which you know has a `Dockerfile` at it’s root.
5. Click VIEW. The Dockerfile will be fetched from the repository and displayed. We can use the contents of this Dockerfile to write & test queries which will extract facts from the Dockerfile.

![](./ds-test-results.png)
![](./ds-test-results.png)

6. We’re going to use the REGEX parser to extract the base image version from the Dockerfile.

![](./ds-field-extraction.png)
![](./ds-field-extraction.png)

To do that, add a fact with the name “Base image version”, and a regular expression which captures the image version with a capture group. Save this fact as a string type.
To do that, add a fact with the name “Base image version”, and a regular expression which captures the image version with a capture group. Save this fact as a string type.

In the example above, the base image comes from a directory called `roadie-main` in the Google Cloud Artifact Registry (gcr.io). It’s based on ubuntu 20. Any digits and underscores after the colon are the part we want to record.
In the example above, the base image comes from a directory called `roadie-main` in the Google Cloud Artifact Registry (gcr.io). It’s based on ubuntu 20. Any digits and underscores after the colon are the part we want to record.

```
FROM gcr.io\/roadie-main\/ubuntu-20.*:([\d\._]+)
```
This regular expression will successfully match a base image version directive in a Dockerfile.
This regular expression will successfully match a base image version directive in a Dockerfile.
```
FROM gcr.io/roadie-main/ubuntu-20:0.9.8
```
You will need to tweak the regular expression to successfully capture your base image version. We recommend iterating on your regular expression in a third-party tool like [RegExr](https://regexr.com/).
You will need to tweak the regular expression to successfully capture your base image version. We recommend iterating on your regular expression in a third-party tool like [RegExr](https://regexr.com/).
7. Once you feel like you have the right regular expression, you can test it against the Dockerfile you fetched earlier. Click the CHECK FACTS button.
![](./ds-field-extraction-results.png)
8. Use the “Applies to” filter to target this data source at some components which you expect to have Dockerfiles. We recommend starting with a highly targeted filter for initial experimentation and iteration. You can widen the filter later to capture more results. We’re using a “demo” tag to accomplish this.
![](./ds-filters.png)
![](./ds-filters.png)
9. Save the data source by clicking SAVE.
10. You should now have a Data Source called Dockerfile facts. You may need to wait some time for the data source to collect all of the Dockerfile base image versions. It must contact the GitHub APIs for each component which is captured by the filter.
Expand All @@ -106,63 +106,39 @@ In the next section, we will create a Check which can show a pass or fail result
## Create a check that shows which software is not using the latest base image
### Detect which Components have a Dockerfile
There’s not much sense telling teams who don’t use Docker that they need to uptake the latest Docker base image that the platform team has released. To omit non-Docker from the results, we need to first determine which software has a Dockerfile in it’s repository.
A “component repository directory” Data Source will record the list of files which exist in the root of the repository of each Component in the Backstage catalog. We can then use this to determine which components are using Dockerfiles.
1. Create a new data source, as before.
2. Give it a name like “List of files in each GitHub repository” and a description like “This fact retriever lists all of the files in the repository associated with each component, starting at the root of the repo.”.
![](./file-list-ds-about.png)
3. In the Data Provider section, choose the type Component repository directory, and set the location to a period, representing the root of the directory.
4. Choose a Backstage component which you know has the `github.com/project-slug` entity set on it’s `catalog-info.yaml` file and click VIEW. The list of files in the repo associated with the Component will be fetched and displayed in JSON format.
![](./file-list-ds-test-results.png)
5. In the field extraction section, add the fact name “File list”, the description “A list of files available from the root of the repository”, and choose the type “Set”. Click CHECK FACTS to test the results. You should see a set of the file paths in the repository has been created.
![](./ds-file-list-extraction-results.png)
6. Set some filters in the Applies to section, and click “SAVE”.
![](./ds-filters.png)
7. Once it runs, you should now have a data source which records a set of all of the files in the repository associated with each component.
![](./file-list-ds-results.png)
### Create a check
Now that we can determine which components are using Dockerfiles, and we can extract the base image version from those files, let’s write a check to combine both of these properties.
The builtin "Repository Files Data Source" gives us all the file paths of a component's repository. We can use this to determine which components are using Dockerfiles. We can also now extract the base image version from those files.
Let’s write a check to combine both of these properties.
1. Visit Tech Insights and click into the Checks tab. Click the ADD CHECK button to create a new check.
![](./add-check-button.png)
![](./add-check-button.png)
2. Give the check a sensible name, like “Apps must use latest Docker base image version”, and a description like “Using the latest Docker base image version ensures you have the best performance and security fixes from the platform team.”
![](./check-about.png)
![](./check-about.png)
3. In the Conditions section, we’re going to create a compound check which combines both the List of files in each GitHub repository Data Source and the Dockerfile facts repository. Click the “ADD CONDITION” button to add a second set of fields in this section. Set the boolean logic selector to “OR”.
![](./check-conditions-empty.png)
![](./check-conditions-empty.png)
4. In the first set of condition inputs, use the following values.
| Input name | Value |
| --- | --- |
| Data Source | List of files in each GitHub repositiory |
| Fact | List of files |
| Fact operator | Does not contain |
| Value | Dockerfile |
| Input name | Value |
| --- | --- |
| Data Source | Repository Files Data Source |
| Fact | List of files |
| Fact operator | Does not contain |
| Value | Dockerfile |
5. In the second set of condition inputs, use these values.
| Input name | Value |
| --- | --- |
| Data Source | Dockerfile facts |
Expand All @@ -178,7 +154,7 @@ Now that we can determine which components are using Dockerfiles, and we can ext
6. Use the filters to target this check at the same set of components as the Data Sources target.
![](./ds-filters.png)
![](./ds-filters.png)
7. Save the check by clicking “SAVE”. If you’re not quite ready to go live yet, you can use the “SAVE AS DRAFT” button to save the check but ensure only admins can see it.
Expand Down

0 comments on commit 159dfd5

Please sign in to comment.