From 7d75b8f53f8175d2055d894ad77ba766bb07372d Mon Sep 17 00:00:00 2001 From: Ioannis Tsouvalas Date: Wed, 31 Jul 2024 15:24:12 +0300 Subject: [PATCH] Improve logging and update dependencies - Added debug logging capabilities to AuthService and LogService - Configured log level via environment variable in LogService - Updated worker pool configuration in main scheduler - Enhanced verification logging with debug level logs - Updated go.mod to use latest versions of dependencies - github.com/labstack/echo/v4 to v4.9.0 - golang.org/x/net to v0.21.0 - golang.org/x/crypto to v0.25.0 - golang.org/x/sys to v0.22.0 - golang.org/x/text to v0.16.0 - gopkg.in/yaml.v3 to v3.0.1 - Ran go mod tidy to clean up module files --- cf/auth_service.go | 21 +++++++++++---------- cmd/scheduler/main.go | 18 +++++++++++++++++- core/log_service.go | 1 + go.mod | 17 ++++++++--------- go.sum | 27 ++++++++++++++------------- logger/log_service.go | 18 ++++++++++++++++++ 6 files changed, 69 insertions(+), 33 deletions(-) diff --git a/cf/auth_service.go b/cf/auth_service.go index b2aa8a1..ea8e924 100644 --- a/cf/auth_service.go +++ b/cf/auth_service.go @@ -16,6 +16,7 @@ import ( type Logger interface { Info(tag, message string) Error(tag, message string) + Debug(tag, message string) } type AuthService struct { @@ -32,43 +33,43 @@ func NewAuthService(client *cf.Client, logger Logger) *AuthService { func (service *AuthService) Verify(auth string) error { tag := "AuthService.Verify" - service.logger.Info(tag, "Starting verification process") + service.logger.Debug(tag, "Starting verification process") username, err := getUsername(auth) if err != nil { service.logger.Error(tag, fmt.Sprintf("Error getting username: %v", err)) return err } - service.logger.Info(tag, fmt.Sprintf("Username obtained: %s", username)) + service.logger.Debug(tag, fmt.Sprintf("Username obtained: %s", username)) user, err := service.getUser(username) if err != nil { service.logger.Error(tag, fmt.Sprintf("Error getting user: %v", err)) return err } - // Debugging = noisy - // service.logger.Info(tag, fmt.Sprintf("User obtained: %v", user)) + + service.logger.Debug(tag, fmt.Sprintf("User obtained: %v", user)) roles, err := service.getUserRoles(user) if err != nil { service.logger.Error(tag, fmt.Sprintf("Error getting user roles: %v", err)) return err } - // Debugging = noisy - // service.logger.Info(tag, fmt.Sprintf("User roles obtained: %v", roles)) + + service.logger.Debug(tag, fmt.Sprintf("User roles obtained: %v", roles)) tokenScopes, err := getTokenScopes(auth, service.logger) if err != nil { service.logger.Error(tag, fmt.Sprintf("Error getting token scopes: %v", err)) return err } - service.logger.Info(tag, fmt.Sprintf("Token scopes obtained: %v", tokenScopes)) + service.logger.Debug(tag, fmt.Sprintf("Token scopes obtained: %v", tokenScopes)) // Check all the roles, but return good early if we find one that works. // Check token scopes for cloud_controller.admin for _, scope := range tokenScopes { if scope == "cloud_controller.admin" { - service.logger.Info(tag, "User has cloud_controller.admin scope") + service.logger.Debug(tag, "User has cloud_controller.admin scope") return nil } } @@ -76,7 +77,7 @@ func (service *AuthService) Verify(auth string) error { // Check CF roles for space_manager or space_developer for _, role := range roles { if role.Type == "space_manager" || role.Type == "space_developer" { - service.logger.Info(tag, fmt.Sprintf("User has role: %s", role.Type)) + service.logger.Debug(tag, fmt.Sprintf("User has role: %s", role.Type)) return nil } } @@ -208,6 +209,6 @@ func getTokenScopes(auth string, logger Logger) ([]string, error) { return nil, err } - logger.Info(tag, fmt.Sprintf("Scopes found in token: %v", claims.Scope)) + logger.Debug(tag, fmt.Sprintf("Scopes found in token: %v", claims.Scope)) return claims.Scope, nil } diff --git a/cmd/scheduler/main.go b/cmd/scheduler/main.go index 276dd59..742dae4 100644 --- a/cmd/scheduler/main.go +++ b/cmd/scheduler/main.go @@ -102,6 +102,22 @@ func main() { log.Info(tag, "got the cf client set up") + log.Info(tag, "trying to acquire the desired number of workers") + + workerNumStr := os.Getenv("workerNum") + if len(workerNumStr) == 0 { + workerNumStr = "20" + log.Info(tag, "No `workerNum` provided, defaulting to 20") + } else { + log.Info(tag, fmt.Sprintf("workerNum set to %s", workerNumStr)) + } + + workerNum, err := strconv.Atoi(workerNumStr) + if err != nil { + log.Error("Invalid workerNum: %v", err.Error()) + os.Exit(255) + } + auth := cf.NewAuthService(cfclient, log) jobs := postgres.NewJobService(db) calls := postgres.NewCallService(db) @@ -116,7 +132,7 @@ func main() { }, ) - workers := workerpool.New(10) + workers := workerpool.New(workerNum) defer workers.StopWait() cronService := cron.NewCronService(log) diff --git a/core/log_service.go b/core/log_service.go index 792215f..3f03c91 100644 --- a/core/log_service.go +++ b/core/log_service.go @@ -3,4 +3,5 @@ package core type LogService interface { Info(string, string) Error(string, string) + Debug(string, string) } diff --git a/go.mod b/go.mod index 0e42bd0..5be0270 100644 --- a/go.mod +++ b/go.mod @@ -1,22 +1,22 @@ module github.com/cloudfoundry-community/ocf-scheduler -go 1.17 +go 1.22 require ( - github.com/DATA-DOG/godog v0.7.13 github.com/cloudfoundry-community/go-cfclient v0.0.0-20220218061834-a7f54630885c github.com/cloudfoundry-community/go-uaa v0.3.1 github.com/ess/hype v1.1.4 github.com/ess/testscope v1.0.0 github.com/gammazero/workerpool v1.1.2 github.com/google/uuid v1.3.0 - github.com/labstack/echo/v4 v4.7.2 + github.com/labstack/echo/v4 v4.9.0 github.com/lib/pq v1.10.4 github.com/robfig/cron/v3 v3.0.1 github.com/rubenv/sql-migrate v1.1.1 github.com/sirupsen/logrus v1.8.1 github.com/spf13/cobra v1.2.1 github.com/spf13/viper v1.8.1 + golang.org/x/oauth2 v0.0.0-20210402161424-2e8d93401602 gopkg.in/khaiql/dbcleaner.v2 v2.3.0 ) @@ -50,14 +50,13 @@ require ( github.com/subosito/gotenv v1.2.0 // indirect github.com/valyala/bytebufferpool v1.0.0 // indirect github.com/valyala/fasttemplate v1.2.1 // indirect - golang.org/x/crypto v0.0.0-20210817164053-32db794688a5 // indirect - golang.org/x/net v0.0.0-20211015210444-4f30a5c0130f // indirect - golang.org/x/oauth2 v0.0.0-20210402161424-2e8d93401602 // indirect - golang.org/x/sys v0.0.0-20211103235746-7861aae1554b // indirect - golang.org/x/text v0.3.7 // indirect + golang.org/x/crypto v0.25.0 // indirect + golang.org/x/net v0.21.0 // indirect + golang.org/x/sys v0.22.0 // indirect + golang.org/x/text v0.16.0 // indirect google.golang.org/appengine v1.6.7 // indirect google.golang.org/protobuf v1.26.0 // indirect gopkg.in/ini.v1 v1.62.0 // indirect gopkg.in/yaml.v2 v2.4.0 // indirect - gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b // indirect + gopkg.in/yaml.v3 v3.0.1 // indirect ) diff --git a/go.sum b/go.sum index a0f6eca..867b16e 100644 --- a/go.sum +++ b/go.sum @@ -117,7 +117,6 @@ github.com/gobuffalo/packr/v2 v2.8.3/go.mod h1:0SahksCVcx4IMnigTjiFuyldmTrdTctXs github.com/godbus/dbus/v5 v5.0.4/go.mod h1:xhWf0FNVPg57R7Z0UbKHbJfkEywrmjJnf7w5xrFpKfA= github.com/godror/godror v0.24.2/go.mod h1:wZv/9vPiUib6tkoDl+AZ/QLf5YZgMravZ7jxH2eQWAE= github.com/gogo/protobuf v1.3.2/go.mod h1:P1XiOD3dCwIKUDQYPy72D8LYyHL2YPYrpS2s69NZV8Q= -github.com/golang-jwt/jwt v3.2.2+incompatible/go.mod h1:8pz2t5EyA70fFQQSrl6XZXzqecmYZeUEB8OUGHkxJ+I= github.com/golang-sql/civil v0.0.0-20190719163853-cb61b32ac6fe/go.mod h1:8vg3r2VgvsThLBIFL93Qb5yWzgyZWhEmBwUJWevAkK0= github.com/golang/glog v0.0.0-20160126235308-23def4e6c14b/go.mod h1:SBH7ygxi8pfUlaOkMMuAQtPIUF8ecWP5IEl/CR7VP2Q= github.com/golang/groupcache v0.0.0-20190702054246-869f871628b6/go.mod h1:cIg4eruTrX1D+g88fzRXU5OdNfaM+9IcxsU14FzY7Hc= @@ -235,8 +234,8 @@ github.com/kr/pretty v0.2.1/go.mod h1:ipq/a2n7PKx3OHsz4KJII5eveXtPO4qwEXGdVfWzfn github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ= github.com/kr/text v0.1.0 h1:45sCR5RtlFHMR4UwH9sdQ5TC8v0qDQCHnXt+kaKSTVE= github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI= -github.com/labstack/echo/v4 v4.7.2 h1:Kv2/p8OaQ+M6Ex4eGimg9b9e6icoxA42JSlOR3msKtI= -github.com/labstack/echo/v4 v4.7.2/go.mod h1:xkCDAdFCIf8jsFQ5NnbK7oqaF/yU1A1X20Ltm0OvSks= +github.com/labstack/echo/v4 v4.9.0 h1:wPOF1CE6gvt/kmbMR4dGzWvHMPT+sAEUJOwOTtvITVY= +github.com/labstack/echo/v4 v4.9.0/go.mod h1:xkCDAdFCIf8jsFQ5NnbK7oqaF/yU1A1X20Ltm0OvSks= github.com/labstack/gommon v0.3.1 h1:OomWaJXm7xR6L1HmEtGyQf26TEn7V6X88mktX9kee9o= github.com/labstack/gommon v0.3.1/go.mod h1:uW6kP17uPlLJsD3ijUYn3/M5bAxtlZhMI6m3MFxTMTM= github.com/lib/pq v1.2.0/go.mod h1:5WUZQaWbwv1U+lTReE5YruASi9Al49XbQIvNi/34Woo= @@ -379,8 +378,8 @@ golang.org/x/crypto v0.0.0-20190820162420-60c769a6c586/go.mod h1:yigFU9vqHzYiE8U golang.org/x/crypto v0.0.0-20191011191535-87dc89f01550/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI= golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto= golang.org/x/crypto v0.0.0-20200820211705-5c72a883971a/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto= -golang.org/x/crypto v0.0.0-20210817164053-32db794688a5 h1:HWj/xjIHfjYU5nVXpTM0s39J9CbLn7Cc5a7IC5rwsMQ= -golang.org/x/crypto v0.0.0-20210817164053-32db794688a5/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc= +golang.org/x/crypto v0.25.0 h1:ypSNr+bnYL2YhwoMt2zPxHFmbAN1KZs/njMG3hxUp30= +golang.org/x/crypto v0.25.0/go.mod h1:T+wALwcMOSE0kXgUAnPAHqTLW+XHgcELELW8VaDgm/M= golang.org/x/exp v0.0.0-20190121172915-509febef88a4/go.mod h1:CJ0aWSM057203Lf6IL+f9T1iT9GByDxfZKAQTCR3kQA= golang.org/x/exp v0.0.0-20190306152737-a1d7652674e8/go.mod h1:CJ0aWSM057203Lf6IL+f9T1iT9GByDxfZKAQTCR3kQA= golang.org/x/exp v0.0.0-20190510132918-efd6b22b2522/go.mod h1:ZjyILWgesfNpC6sMxTJOJm9Kp84zZh5NQWvqDGG3Qr8= @@ -455,8 +454,8 @@ golang.org/x/net v0.0.0-20210226172049-e18ecbb05110/go.mod h1:m0MpNAwzfU5UDzcl9v golang.org/x/net v0.0.0-20210316092652-d523dce5a7f4/go.mod h1:RBQZq4jEuRlivfhVLdyRGr576XBO4/greRjx4P4O3yc= golang.org/x/net v0.0.0-20210405180319-a5a99cb37ef4/go.mod h1:p54w0d4576C0XHj96bSt6lcn1PtDYWL6XObtHCRCNQM= golang.org/x/net v0.0.0-20210805182204-aaa1db679c0d/go.mod h1:9nx3DQGgdP8bBQD5qxJ1jj9UTztislL4KSBs9R2vV5Y= -golang.org/x/net v0.0.0-20211015210444-4f30a5c0130f h1:OfiFi4JbukWwe3lzw+xunroH1mnC1e2Gy5cxNJApiSY= -golang.org/x/net v0.0.0-20211015210444-4f30a5c0130f/go.mod h1:9nx3DQGgdP8bBQD5qxJ1jj9UTztislL4KSBs9R2vV5Y= +golang.org/x/net v0.21.0 h1:AQyQV4dYCvJ7vGmJyKki9+PBdyvhkSd8EIx/qb0AYv4= +golang.org/x/net v0.21.0/go.mod h1:bIjVDfnllIU7BJ2DNgfnXvpSvtn8VRwhlsaeUTyUS44= golang.org/x/oauth2 v0.0.0-20180821212333-d2e6202438be/go.mod h1:N/0e6XlmueqKjAGxoOufVs8QHGRruUQn6yWY3a++T0U= golang.org/x/oauth2 v0.0.0-20190130055435-99b60b757ec1/go.mod h1:gOpvHmFTYa4IltrdGE7lF6nIHvwfUNPOp7c8zoXwtLw= golang.org/x/oauth2 v0.0.0-20190226205417-e64efc72b421/go.mod h1:gOpvHmFTYa4IltrdGE7lF6nIHvwfUNPOp7c8zoXwtLw= @@ -532,11 +531,13 @@ golang.org/x/sys v0.0.0-20210616094352-59db8d763f22/go.mod h1:oPkhp1MJrh7nUepCBc golang.org/x/sys v0.0.0-20210630005230-0f9fa26af87c/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.0.0-20210809222454-d867a43fc93e/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.0.0-20210927094055-39ccf1dd6fa6/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= -golang.org/x/sys v0.0.0-20211103235746-7861aae1554b h1:1VkfZQv42XQlA/jchYumAnv1UPo6RgF9rJFkTgZIxO4= golang.org/x/sys v0.0.0-20211103235746-7861aae1554b/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/sys v0.22.0 h1:RI27ohtqKCnwULzJLqkv897zojh5/DwS/ENaMzUOaWI= +golang.org/x/sys v0.22.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA= golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo= -golang.org/x/term v0.0.0-20210927222741-03fcf44c2211 h1:JGgROgKl9N8DuW20oFS5gxc+lE67/N3FcwmBPMe7ArY= golang.org/x/term v0.0.0-20210927222741-03fcf44c2211/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8= +golang.org/x/term v0.22.0 h1:BbsgPEJULsl2fV/AT3v15Mjva5yXKQDyKf+TbDz7QJk= +golang.org/x/term v0.22.0/go.mod h1:F3qCibpT5AMpCRfhfT53vVJwhLtIVHhB9XDjfFvnMI4= golang.org/x/text v0.0.0-20170915032832-14c0d48ead0c/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= golang.org/x/text v0.3.1-0.20180807135948-17ff2d5776d2/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= @@ -545,12 +546,11 @@ golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ= golang.org/x/text v0.3.4/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ= golang.org/x/text v0.3.5/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ= golang.org/x/text v0.3.6/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ= -golang.org/x/text v0.3.7 h1:olpwvP2KacW1ZWvsR7uQhoyTYvKAupfQrRGBFM352Gk= -golang.org/x/text v0.3.7/go.mod h1:u+2+/6zg+i71rQMx5EYifcz6MCKuco9NR6JIITiCfzQ= +golang.org/x/text v0.16.0 h1:a94ExnEXNtEwYLGJSIUxnWoxoRz/ZcCsV63ROupILh4= +golang.org/x/text v0.16.0/go.mod h1:GhwF1Be+LQoKShO3cGOHzqOgRrGaYc9AvblQOmPVHnI= golang.org/x/time v0.0.0-20181108054448-85acf8d2951c/go.mod h1:tRJNPiyCQ0inRvYxbN9jk5I+vvW/OXSQhTDSoE431IQ= golang.org/x/time v0.0.0-20190308202827-9d24e82272b4/go.mod h1:tRJNPiyCQ0inRvYxbN9jk5I+vvW/OXSQhTDSoE431IQ= golang.org/x/time v0.0.0-20191024005414-555d28b269f0/go.mod h1:tRJNPiyCQ0inRvYxbN9jk5I+vvW/OXSQhTDSoE431IQ= -golang.org/x/time v0.0.0-20201208040808-7e3f01d25324/go.mod h1:tRJNPiyCQ0inRvYxbN9jk5I+vvW/OXSQhTDSoE431IQ= golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ= golang.org/x/tools v0.0.0-20190114222345-bf090417da8b/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ= golang.org/x/tools v0.0.0-20190226205152-f727befe758c/go.mod h1:9Yl7xja0Znq3iFh3HoIrodX9oNMXvdceNzlUR8zjMvY= @@ -734,8 +734,9 @@ gopkg.in/yaml.v2 v2.3.0/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= gopkg.in/yaml.v2 v2.4.0 h1:D8xgwECY7CYvx+Y2n4sBz93Jn9JRvxdiyyo8CTfuKaY= gopkg.in/yaml.v2 v2.4.0/go.mod h1:RDklbk79AGWmwhnvt/jBztapEOGDOx6ZbXqjP6csGnQ= gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= -gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b h1:h8qDotaEPuJATrMmW04NCwg7v22aHH28wwpauUhK9Oo= gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= +gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= +gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= honnef.co/go/tools v0.0.0-20190102054323-c2f93a96b099/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4= honnef.co/go/tools v0.0.0-20190106161140-3f1c8253044a/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4= honnef.co/go/tools v0.0.0-20190418001031-e561f6794a2a/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4= diff --git a/logger/log_service.go b/logger/log_service.go index 2f017fd..4734243 100644 --- a/logger/log_service.go +++ b/logger/log_service.go @@ -2,6 +2,7 @@ package logger import ( "os" + "strings" "github.com/sirupsen/logrus" ) @@ -16,6 +17,19 @@ func New() *LogService { log.Out = os.Stdout + // Set log level from environment variable + level := os.Getenv("LOG_LEVEL") + switch strings.ToLower(level) { + case "debug": + log.SetLevel(logrus.DebugLevel) + case "info": + log.SetLevel(logrus.InfoLevel) + case "error": + log.SetLevel(logrus.ErrorLevel) + default: + log.SetLevel(logrus.InfoLevel) // Default to Info level + } + return &LogService{log: log} } @@ -26,3 +40,7 @@ func (service *LogService) Info(context string, message string) { func (service *LogService) Error(context string, message string) { service.log.WithField("context", context).Error(message) } + +func (service *LogService) Debug(context string, message string) { + service.log.WithField("context", context).Debug(message) +}