-
Notifications
You must be signed in to change notification settings - Fork 2
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
feat: refactoring watch logic for e2e tests #431
Open
dpadhiar
wants to merge
1
commit into
main
Choose a base branch
from
refactor-watch
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+77
−151
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,15 +3,13 @@ package e2e | |
import ( | ||
"context" | ||
"fmt" | ||
"os" | ||
"path/filepath" | ||
"strings" | ||
"time" | ||
|
||
. "github.com/onsi/gomega" | ||
appsv1 "k8s.io/api/apps/v1" | ||
corev1 "k8s.io/api/core/v1" | ||
"sigs.k8s.io/yaml" | ||
|
||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured" | ||
|
@@ -152,13 +150,6 @@ func watchISBServiceRollout() { | |
} | ||
defer watcher.Stop() | ||
|
||
file, err := os.OpenFile(filepath.Join(ResourceChangesISBServiceOutputPath, "isbservice_rollout.yaml"), os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644) | ||
if err != nil { | ||
fmt.Printf("Failed to open log file: %v\n", err) | ||
return | ||
} | ||
defer file.Close() | ||
|
||
for { | ||
select { | ||
case event := <-watcher.ResultChan(): | ||
|
@@ -172,11 +163,9 @@ func watchISBServiceRollout() { | |
Spec: rollout.Spec, | ||
Status: rollout.Status, | ||
} | ||
bytes, _ := yaml.Marshal(rl) | ||
updateLog := fmt.Sprintf("%s\n%v\n\n%s\n", LogSpacer, time.Now().Format(time.RFC3339Nano), string(bytes)) | ||
_, err = file.WriteString(updateLog) | ||
|
||
err := writeToFile(filepath.Join(ResourceChangesISBServiceOutputPath, "isbservice_rollout.yaml"), rl) | ||
if err != nil { | ||
fmt.Printf("Failed to write to log file: %v\n", err) | ||
return | ||
} | ||
} | ||
|
@@ -197,13 +186,6 @@ func watchISBService() { | |
} | ||
defer watcher.Stop() | ||
|
||
file, err := os.OpenFile(filepath.Join(ResourceChangesISBServiceOutputPath, "isbservice.yaml"), os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644) | ||
if err != nil { | ||
fmt.Printf("Failed to open log file: %v\n", err) | ||
return | ||
} | ||
defer file.Close() | ||
|
||
for { | ||
select { | ||
case event := <-watcher.ResultChan(): | ||
|
@@ -223,11 +205,9 @@ func watchISBService() { | |
Spec: isbsvc.Spec, | ||
Status: isbsvc.Status, | ||
} | ||
bytes, _ := yaml.Marshal(output) | ||
updateLog := fmt.Sprintf("%s\n%v\n\n%s\n", LogSpacer, time.Now().Format(time.RFC3339Nano), string(bytes)) | ||
_, err = file.WriteString(updateLog) | ||
|
||
err = writeToFile(filepath.Join(ResourceChangesISBServiceOutputPath, "isbservice.yaml"), output) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This can be a future PR, but if I were to imagine how to consolidate all of these functions into a single function, here is one idea:
|
||
if err != nil { | ||
fmt.Printf("Failed to write to log file: %v\n", err) | ||
return | ||
} | ||
} | ||
|
@@ -264,18 +244,8 @@ func watchStatefulSet() { | |
Status: sts.Status, | ||
} | ||
|
||
bytes, _ := yaml.Marshal(output) | ||
updateLog := fmt.Sprintf("%s\n%v\n\n%s\n", LogSpacer, time.Now().Format(time.RFC3339Nano), string(bytes)) | ||
fileName := filepath.Join(ResourceChangesISBServiceOutputPath, "statefulsets", strings.Join([]string{sts.Name, ".yaml"}, "")) | ||
file, err := os.OpenFile(fileName, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644) | ||
if err != nil { | ||
fmt.Printf("Failed to open log file: %v\n", err) | ||
return | ||
} | ||
defer file.Close() | ||
_, err = file.WriteString(updateLog) | ||
err := writeToFile(filepath.Join(ResourceChangesISBServiceOutputPath, "statefulsets", strings.Join([]string{sts.Name, ".yaml"}, "")), output) | ||
if err != nil { | ||
fmt.Printf("Failed to write to log file: %v\n", err) | ||
return | ||
} | ||
} | ||
|
@@ -285,3 +255,14 @@ func watchStatefulSet() { | |
} | ||
} | ||
} | ||
|
||
func startISBServiceRolloutWatches() { | ||
wg.Add(1) | ||
go watchISBServiceRollout() | ||
|
||
wg.Add(1) | ||
go watchISBService() | ||
|
||
wg.Add(1) | ||
go watchStatefulSet() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this is interesting that the file is being closed with every write. Might be a little less performant to continually open and close, but I guess it's working. The alternative would be to maintain a list of all open files and then close them all at the end. But since it's working, I'm fine as is. Maybe we can just add a "TODO" comment: "for better performance, don't close file after every write"
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That's right, this approach is much more cautious just cause I don't want any files to continue to be open for too long or not close to cause an issue.
Keeping a list of every open file would be good, we could automatically close everything when a test fails as well too.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm fine if you just want to add a "TODO" for now - up to you
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think I could add this change to this PR since we're adding the
writeToFile
func here and the other comment to consolidate further could be its own PR.