Skip to content

Commit 0cbfd57

Browse files
committed
readme: added instructions for sync
1 parent 8eb6220 commit 0cbfd57

File tree

1 file changed

+89
-0
lines changed

1 file changed

+89
-0
lines changed

README.md

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -198,3 +198,92 @@ func Function(
198198

199199
}
200200
```
201+
202+
### Sync Tool and Operator Types
203+
204+
Previously, packages relied on importing the types from individual operators and adding them as dependencies. This has led to eco-goinfra having many dependencies, making it harder to reuse and increasing the chance of version conflicts between indirect dependencies.
205+
206+
By using just the runtime client and removing dependencies on individual operators, the sync tool reduces the number of eco-goinfra dependencies and makes packages more modular. It will periodically copy over types from operator repos to this repo by cloning the desired path in the operator repo, applying changes in the config file, then copying it into this repo.
207+
208+
#### Running
209+
210+
To run the sync tool, use the `lib-sync` makefile target.
211+
212+
```
213+
make lib-sync
214+
```
215+
216+
It may be faster to sync only one config file instead using the `--config-file` flag.
217+
218+
```
219+
go run ./internal/sync --config-file ./internal/sync/configs/<config-file.yaml>
220+
```
221+
222+
If the sync fails while adding a new set of operator types, remove the synced directory from `schemes/` and rerun the sync.
223+
224+
#### Configuration
225+
226+
Config files for the sync tool live in the [internal/sync/configs](./internal/sync/configs/) directory. A good example of all the features available is in the [nvidia-config.yaml](./internal/sync/configs/nvidia-config.yaml) file.
227+
228+
Each config is a YAML document with a top level list of objects that follow this form:
229+
230+
```yaml
231+
- name: operator-repo # 1
232+
sync: true # 2
233+
repo_link: "https://github.com/operator/repo" # 3
234+
branch: main # 4
235+
remote_api_directory: pkg/apis/v1 # 5
236+
local_api_directory: schemes/operator/operatortypes # 6
237+
replace_imports: # 7
238+
- old: '"github.com/operator/repo/pkg/apis/v1/config"' # 8
239+
new: '"github.com/openshift-kni/eco-goinfra/pkg/schemes/operator/operatortypes/config"'
240+
- old: '"github.com/operator/repo/pkg/apis/v1/utils/exec"'
241+
new: exec "github.com/openshift-kni/eco-goinfra/pkg/schemes/operator/operatortypes/executils" # 9
242+
excludes:
243+
- "*_test.go" # 10
244+
```
245+
246+
1. Name, which does not have to be unique, identifies the repos in logs and controls where it is cloned during the sync. It does not affect the outcome of the sync itself and can be set as desired.
247+
2. Sync is whether the operator repo will be synced both periodically and when `make lib-sync` is used manually.
248+
3. Repo link is the url of the operator repo itself. It does not need to end in `.git`.
249+
4. Branch is the branch of the operator repo to sync with.
250+
5. Remote API directory is the path in the operator repo to sync with, relative to the operator repo root.
251+
6. Local API directory is the path in eco-goinfra where the remote API directory should be synced to. Relative to the eco-goinfra root, it should start with `schemes/`.
252+
7. Since the operator may import other parts of the repo or other repos in the same organization, these will need to be replaced with imports from eco-goinfra once synced. This field is optional.
253+
8. Import replacement is done through find and replace, so we include the double quotes to make sure it is an import being matched.
254+
9. If the package name in eco-goinfra is different than the operator repo, the import should be renamed so the code still works.
255+
10. Excludes is an optional list of file patterns to exclude. Since tests and mocks may add their own dependencies, excluding them can reduce how many other dependencies need to be synced.
256+
257+
Like in the [nvidia-config.yaml](./internal/sync/configs/nvidia-config.yaml) example, it is often the case that one repo will import a few others. All of the imported repos should be specified in the sync config to avoid adding new dependencies.
258+
259+
#### Using the Synced Types
260+
261+
Instead of directly adding the scheme synced from the operator repo to the [clients](./pkg/clients/) package, the client scheme should be updated in `NewBuilder()` or `Pull()` functions. The [metallb](./pkg/metallb/metallb.go) package provides a good example of this.
262+
263+
```go
264+
// in NewBuilder
265+
err := apiClient.AttachScheme(mlbtypes.AddToScheme)
266+
if err != nil {
267+
glog.V(100).Infof("Failed to add metallb scheme to client schemes")
268+
269+
return nil
270+
}
271+
```
272+
273+
[metallb_test.go](./pkg/metallb/metallb_test.go) provides an example of how to use the schemes in unit tests. There should be a variable for the test schemes.
274+
275+
```go
276+
var mlbTestSchemes = []clients.SchemeAttacher{
277+
mlbtypes.AddToScheme,
278+
}
279+
```
280+
281+
And these schemes should be provided when creating the test client.
282+
283+
```go
284+
// in buildMetalLbTestClientWithDummyObject
285+
clients.GetTestClients(clients.TestClientParams{
286+
K8sMockObjects: buildDummyMetalLb(),
287+
SchemeAttachers: mlbTestSchemes,
288+
})
289+
```

0 commit comments

Comments
 (0)