Skip to content

Logging#8

Merged
wrkode merged 14 commits intomainfrom
logging
Apr 3, 2023
Merged

Logging#8
wrkode merged 14 commits intomainfrom
logging

Conversation

@wrkode
Copy link
Copy Markdown
Owner

@wrkode wrkode commented Mar 24, 2023

This PR addresses the issues: #1, #2, #3, #4, #5, #6, #7

@wrkode wrkode self-assigned this Mar 24, 2023
@wrkode wrkode marked this pull request as draft March 24, 2023 20:08
@wrkode wrkode marked this pull request as ready for review March 24, 2023 22:17
@wrkode
Copy link
Copy Markdown
Owner Author

wrkode commented Mar 25, 2023

@flavio please review

Comment thread app/namespace-watcher.go Outdated

cpuLimitMin, err := resource.ParseQuantity(limits.CpuLimitMin)
if err != nil {
logrus.Fatalf("error parsing CPU_LIMIT_MIN: ", err)
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

It should return the error, given this function returns a error type. The logrus.Fatalf would panic

Comment thread app/namespace-watcher.go Outdated
if err != nil {
return err
}
logrus.Warn("Created LimitRange for namespace: ", namespaceName)
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

This is a Info message IMHO

Comment thread app/namespace-watcher.go Outdated
if err != nil {
return err
}
logrus.Warn("Updated LimitRange for namespace: ", namespaceName)
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Also this one should be an Info message

Comment thread app/namespace-watcher.go Outdated
Comment on lines +115 to +119
func lookupEnvOrEmpty(key string) string {
value, _ := os.LookupEnv(key)

return value
}
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

This is not needed, just use os.Getenv which does exactly the same thing

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

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

I'm not sure I understand.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

You can remove this function and just invoke os.Getenv instead of lookupEnvOrEmpty. The STD-lib function does exactly the very same thing as this code you wrote

Comment thread app/namespace-watcher.go Outdated
Comment on lines +22 to +27
CpuLimitMax string
MemLimitMax string
EphemeralStorageLimitMax string
CpuLimitMin string
MemLimitMin string
EphemeralStorageLimitMin string
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

use resource.Quantity instead

Comment thread app/namespace-watcher.go Outdated
//Evaluate if environment variables are not set or set to ""
setLimits.CpuLimitMin = lookupEnvOrEmpty("CPU_LIMIT_MIN")
if setLimits.CpuLimitMin == "" || len(strings.TrimSpace(setLimits.CpuLimitMin)) == 0 {
logrus.Fatal("Environment variable CPU_LIMIT_MIN is not set or empty: ", setLimits.CpuLimitMin)
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Some considerations about these conditional checks:

  • Do not use Fatal, it causes the program to exit immediately
  • Given you have a default in place, it's not even worth to mention that to the user. If you really want to, use the Trace or Debug level
  • I don't think you have to do all this "massaging" of the user input
  • There's a lot of repetition across these if statements. Maybe you can create a function like parseValueOrDefault(envKey string, defaultValue resource.Quantity) (resource.Quantity, error)

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

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

I'm not putting a default in place.
I thought of Fatal because the watcher should fail/stop if the values can't be used?

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

I see, I got confused. I though you had a default in place. It's up to you to decide what to do when one of these settings is missing.

Fine with me if you want to exit with an error, hence Fatal 😄

Copy link
Copy Markdown
Owner Author

@wrkode wrkode Mar 29, 2023

Choose a reason for hiding this comment

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

On a second thought. It might be a good idea to fall back to a set of defaults for if the use set limits are empty or wrong set user input.
I will add this into a feature for a new PR

Comment thread app/namespace-watcher.go Outdated
if err != nil {
fmt.Println("Failed to get Kubernetes config:", err)
logrus.Fatal("Failed to get Kubernetes config: ", err)
os.Exit(1)
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

this line is not needed because Fatal causes the exit too

Comment thread app/namespace-watcher.go Outdated
if err != nil {
fmt.Println("Failed to create Kubernetes client:", err)
logrus.Fatal("Failed to create Kubernetes client: ", err)
os.Exit(1)
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

same here

Comment thread app/namespace-watcher.go Outdated
if err != nil {
fmt.Println("Failed to watch namespaces:", err)
logrus.Fatal("Failed to watch namespaces: ", err)
os.Exit(1)
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

same here

Comment thread app/namespace-watcher.go Outdated
err := createOrUpdateLimitRange(clientset, namespaceName, setLimits)
if err != nil {
fmt.Printf("Failed to create LimitRange for namespace %s: %v\n", namespaceName, err)
logrus.Warn("Failed to create LimitRange for namespace: ", namespaceName, " ", err)
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

This is an Error

@wrkode wrkode marked this pull request as draft March 28, 2023 16:48
@wrkode
Copy link
Copy Markdown
Owner Author

wrkode commented Mar 29, 2023

@flavio please review the latest PR changes.

@wrkode wrkode marked this pull request as ready for review March 29, 2023 11:14
Comment thread app/namespace-watcher.go Outdated
q, err := resource.ParseQuantity(value)
if err != nil {
logrus.Error("Error parsing ", key, ": ", err)
return resource.Quantity{}
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

In this case I would definitely return with an error. The user has to be made aware of that, the program has to exit

Comment thread app/namespace-watcher.go Outdated
Comment on lines +155 to +163
excludedNamespaces := map[string]struct{}{
"default": {},
"cattle": {},
"kube-system": {},
"kube-public": {},
"istio-system": {},
"kube-node-lease": {},
"kube-local": {},
}
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Comment thread app/namespace-watcher.go Outdated
Comment on lines +99 to +101
if strings.Contains(namespaceName, "cattle") {
return true
}
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Instead of doing that, read the list of namespaces that the user provided and then add to the cattle and other names to it.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Update: you can even drop that, you're already adding cattle and other namespaces to the excluded list

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

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

I have addressed this, but for some reason I must explicitly check for cattle word in the namespace.

@wrkode
Copy link
Copy Markdown
Owner Author

wrkode commented Mar 29, 2023

@flavio ready for new review

Comment thread app/namespace-watcher.go
Comment on lines +173 to +175
isExcluded := strings.Contains(namespaceName, "cattle") || excludedNamespaces.Contains(namespaceName)

if isExcluded {
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

The cattle namespace is already added to the list of excluded namespaces (see line 150).

This code can be simplified in this way:

Suggested change
isExcluded := strings.Contains(namespaceName, "cattle") || excludedNamespaces.Contains(namespaceName)
if isExcluded {
if excludedNamespaces.Contains(namespaceName) {

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

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

I will address in rc1

Comment thread app/namespace-watcher.go
isExcluded := strings.Contains(namespaceName, "cattle") || excludedNamespaces.Contains(namespaceName)

if isExcluded {
logrus.Info("Checking if namespace should be skipped: ", namespaceName, " - Excluded: ", isExcluded)
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

This log message should be placed outside of the if statement, right now this is shown only when the check has already been done. Moreover, this is more of a debug message, you should not pollute the production logs with this message.

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

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

I will address in rc1

Comment thread deployment.yaml
- apiGroups: [""]
resources: ["namespaces", "limitranges"]
verbs: ["get", "watch", "list", "create", "delete"]
verbs: ["get", "watch", "list", "create", "update", "delete"]
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

I think you should split that into 2 groups: one for namespace. Here you get only watch I think. The other one for limitranges, this one requires more permissions (I think get, list, create and delete)

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

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

I will address in rc1

@wrkode wrkode merged commit 140c2cd into main Apr 3, 2023
@wrkode wrkode deleted the logging branch April 3, 2023 17:34
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.

2 participants