Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use go-functional in repositories list methods #3383

Merged
merged 2 commits into from
Jul 19, 2024

Conversation

danail-branekov
Copy link
Member

Is there a related GitHub Issue?

No

What is this change about?

This eliminates quite some boilerplate code around filtering and mapping
k8s resources. go-functional also replaces our inhouse filtering
implementation.

Also, this change removes code duplications in methods such as
GetSomethingByAandB that list and filter resources. Instead of
implementing the list operation themselves, they delegate to the
relevant list method by constructing a list messages that matches the
criteria they need. As a further improvement we could get rid of those
methods and just invoke repo.List from the handlers.
As a consequence of this change those methods now return a NotFound
error rather than Unauthorised when the user is not allowed to list
the resources. Still, that should be OK

Does this PR introduce a breaking change?

No

Tag your pair, your PM, and/or team

@georgethebeatle

@danail-branekov danail-branekov force-pushed the use-iterators-in-repos branch 2 times, most recently from dcbaa3c to 0f99344 Compare July 17, 2024 15:55
Copy link
Contributor

@BooleanCat BooleanCat left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Using go-functional v2, see it.Lines - you can iterate over lines from a reader. - ignore I meant to add this as an inline comment.

Comment on lines +89 to +144
func readLines(r io.Reader, logger logr.Logger) []string {
lines := []string{}

var err error
var line []byte
bufReader := bufio.NewReader(r)
for {
line, err = bufReader.ReadBytes('\n')
lines = append(lines, string(line))
if err != nil {
if err != io.EOF {
logger.Info("failed to parse pod logs", "err", err)
}
break
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Using go-functional v2, see it.Lines - you can iterate over lines from a reader.

Copy link
Contributor

@BooleanCat BooleanCat Jul 17, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lines, errs := itx.LinesString(r).Unzip()
if len(errs.Collect()) > 0 {
  panic("Ahhh")
}
lines.Collect()

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Or if you know reading cannot fail such as with reading from a string buffer:

lines := itx.LinesString(r).Left().Collect()

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We wanted to get as much logs as we can regardless read errors. go-functional v1 LinesString did not look it supported that (i.e. it just failed) so we kept that code. When switching to v2 we would make use of itx.LinesString for sure

logReadCloser, err := k8sClient.CoreV1().Pods(pod.Namespace).GetLogs(pod.Name, &corev1.PodLogOptions{Timestamps: true, TailLines: &limit}).Stream(ctx)
if err != nil {
logger.Info("failed to fetch logs", "pod", pod.Name, "reason", err)
return iter.Lift([]LogRecord{})
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For this there's the more explicit iter.Exhausted[LogRecord]()

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ah, nice! Will use that :)

@danail-branekov danail-branekov force-pushed the use-iterators-in-repos branch 2 times, most recently from fd4eeed to 42b9d55 Compare July 18, 2024 13:56
This eliminates quite some boilerplate code around filtering and mapping
k8s resources. `go-functional` also replaces our inhouse filtering
implementation.

Also, this change removes code duplications in methods such as
`GetSomethingByAandB` that list and filter resources. Instead of
implementing the list operation themselves, they delegate to the
relevant `list` method by constructing a list messages that matches the
criteria they need. As a further improvement we could get rid of those
methods and just invoke `repo.List` from the handlers.
As a consequence of this change those methods now return a `NotFound`
error rather than `Unauthorised` when the user is not allowed to list
the resources. However, that is fine as the related api handlers mask
fobidden errors as not found, so no change from API users' point of view

Co-authored-by: Danail Branekov <danailster@gmail.com>
Co-authored-by: Georgi Sabev <georgethebeatle@gmail.com>
@georgethebeatle georgethebeatle merged commit ba0c707 into main Jul 19, 2024
11 checks passed
@georgethebeatle georgethebeatle deleted the use-iterators-in-repos branch July 19, 2024 10:16
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants