-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Add client and resource files and respective build files * Add gazelle prefix * Add client interface and stub for GCE client * Add gcp client interface and gce client * Change proto package * Updated dependencies * Create GCP client for GCE * Add files to help with testing * Create outline for unit tests * Update build file for test dependencies * Add regex filtering * Create framework for testing get resources * Add test cases for get resources for GCE client * Add delete resource test, and begin documenting test file * Add documentation, update build files and delete unnecessary test files * Fix tiny indentenation mistake * Add client and resource files and respective build files * Add gazelle prefix * Add client interface and stub for GCE client * Add gcp client interface and gce client * Updated dependencies * Create GCP client for GCE * Add files to help with testing * Create outline for unit tests * Update build file for test dependencies * Add regex filtering * Create framework for testing get resources * Add test cases for get resources for GCE client * Add delete resource test, and begin documenting test file * Add documentation, update build files and delete unnecessary test files * Fix tiny indentenation mistake * Change indententation again * Begin outlining Reaper logic * Continue work on Reaper * Pass context as Auth parameter * Add test for name and skip filtering * Add copyright info to resources test file * Updated go module files * Fixed bug in test file * Clean up test resources file * Fix overengineering of error handling and logging in reaper * Begin work on reaper unit tests * Continue adding to reaper tests * Add test case for checking TTL of watched resource * Add test logic for update reaper * Add test cases for reaper update * Add test logic for reaper run * Add mock clock to WatchedResource for testing * Configure reaper tests to run with mock clock * Add more test cases for reaper * Remove test files and comments * Clean up code, update bazel files, and add documentation * Fix deleted comment * Refactor everything to use pointers * Add logging for when resource is deleted * Add integration test outline * Work on integration test * Update gitignore * Add methods to help build protos * Work on handling duplicate watched resources * Handle duplicate watched resources * Restructure integration test and do some cleaning up * Fix small bugs * Update build files and add documentation * Create reaper manager * Refactor reaper and finish manager logic * Add unit test for running reaper on schedule * Add reaper manager integration test * Refactor reaper to allow for updating watching resources * Add checks to integration test * Refactor update config to not make any web calls, and give that functionality to GetResources * Update tests for reaper refactor * Add documentation * Add delete and list methods to reaper manager * Fix logging bug * Add framework for testing reaper manager * Remove skip filter from reaperconfig proto * Add reaper manager tests * Refactor manager and add test for monitoring reapers * Deleted unnecessary testing files * Add gcs client, and implement client interface * Add unit test for auth and delete for gcs client * Begin adding support for monitoring GCS buckets and objects separately * Finish GCS client and begin work on tests * Add GCS to factory method, and refactor GCE for consistant project structure * Return error instead of log.Fatal * Return error when parsing schedule * Begin working on gRPC server * Continue work on grpc interface * Finish grpc server * Finish gRPC server and client * Add command line arguement for client * Finish client command line tool * Add unit tests, fix concurency bugs * Fix minor bugs, add documentation, clean up code * Add basic logging functions * Fix build files * Modified test * Fix mistake in BUILD file * Add custom logger interface * Refactor existing logging to use custom logger * Implement cloud logging configured with flags * Clean up logger code * Clean up and add license header * Make logging thread safe, and fix tests * Delete unnecessary file * Fix merge mistake in proto * Change command line naming and add message when using cloud logging * add error check when creating cloud logger Co-authored-by: Brian <bmodel@google.com>
- Loading branch information
1 parent
1e84ba3
commit 60cae77
Showing
15 changed files
with
261 additions
and
28 deletions.
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
Empty file.
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
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 |
---|---|---|
@@ -0,0 +1,9 @@ | ||
load("@io_bazel_rules_go//go:def.bzl", "go_library") | ||
|
||
go_library( | ||
name = "go_default_library", | ||
srcs = ["logger.go"], | ||
importpath = "github.com/googleinterns/cloudai-gcp-test-resource-reaper/pkg/logger", | ||
visibility = ["//visibility:public"], | ||
deps = ["@com_google_cloud_go_logging//:go_default_library"], | ||
) |
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 |
---|---|---|
@@ -0,0 +1,152 @@ | ||
// Copyright 2020 Google LLC | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// https://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package logger | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"log" | ||
"os" | ||
"sync" | ||
|
||
"cloud.google.com/go/logging" | ||
) | ||
|
||
// Logger handles writing local logs to a file and cloud logs to Stackdriver. | ||
type Logger struct { | ||
*log.Logger | ||
*CloudLogger | ||
mux *sync.Mutex | ||
} | ||
|
||
var logger *Logger | ||
|
||
// CreateLogger initializes the logger for the server. The logs will be written to a local | ||
// file called logs.txt. | ||
func CreateLogger() error { | ||
logFile, err := os.OpenFile("logs.txt", os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0666) | ||
if err != nil { | ||
return err | ||
} | ||
fileLogger := log.New(logFile, "", log.Ldate|log.Ltime) | ||
logger = &Logger{ | ||
Logger: fileLogger, | ||
CloudLogger: nil, | ||
mux: &sync.Mutex{}, | ||
} | ||
return nil | ||
} | ||
|
||
// Log outputs to the necessary logs. Arguments are handled in the manner of fmt.Println. | ||
func Log(v ...interface{}) { | ||
logger.mux.Lock() | ||
logger.log(v...) | ||
logger.mux.Unlock() | ||
} | ||
|
||
// Logf takes a format string and message and writes it to the necessary logs. Arguments are | ||
// handled in the manner of fmt.Printf. | ||
func Logf(format string, v ...interface{}) { | ||
logger.mux.Lock() | ||
logger.logf(format, v...) | ||
logger.mux.Unlock() | ||
} | ||
|
||
// Error outputs an error to the necessary logs. | ||
func Error(v ...interface{}) { | ||
logger.mux.Lock() | ||
logger.error(v...) | ||
logger.mux.Unlock() | ||
} | ||
|
||
// Close closes the logger. | ||
func Close() { | ||
if logger.CloudLogger != nil { | ||
logger.CloudLogger.closeLogger() | ||
} | ||
} | ||
|
||
// AddCloudLogger adds stackdriver logging to the logger in the given project and log name. | ||
func AddCloudLogger(ctx context.Context, projectID, loggerName string) error { | ||
cloudLogger, err := createCloudLogger(ctx, projectID, loggerName) | ||
if err != nil { | ||
return err | ||
} | ||
logger.CloudLogger = cloudLogger | ||
return nil | ||
} | ||
|
||
func (l *Logger) log(v ...interface{}) { | ||
l.Logger.Println(v...) | ||
if l.CloudLogger != nil { | ||
l.CloudLogger.log(v...) | ||
} | ||
} | ||
|
||
func (l *Logger) logf(format string, v ...interface{}) { | ||
l.Logger.Printf(format, v...) | ||
if l.CloudLogger != nil { | ||
l.CloudLogger.logf(format, v...) | ||
} | ||
} | ||
|
||
func (l *Logger) error(v ...interface{}) { | ||
l.Logger.Println(v...) | ||
if l.CloudLogger != nil { | ||
l.CloudLogger.error(v...) | ||
} | ||
} | ||
|
||
// CloudLogger handles writing logs to stackdriver. | ||
type CloudLogger struct { | ||
*logging.Logger | ||
*logging.Client | ||
} | ||
|
||
func createCloudLogger(ctx context.Context, projectID, loggerName string) (*CloudLogger, error) { | ||
logClient, err := logging.NewClient(ctx, projectID) | ||
if err != nil { | ||
return nil, err | ||
} | ||
return &CloudLogger{ | ||
Logger: logClient.Logger(loggerName), | ||
Client: logClient, | ||
}, nil | ||
} | ||
|
||
func (l *CloudLogger) log(v ...interface{}) { | ||
l.Logger.Log( | ||
logging.Entry{Payload: fmt.Sprintln(v...)}, | ||
) | ||
} | ||
|
||
func (l *CloudLogger) logf(format string, v ...interface{}) { | ||
l.Logger.Log( | ||
logging.Entry{Payload: fmt.Sprintf(format, v...)}, | ||
) | ||
} | ||
|
||
func (l *CloudLogger) error(v ...interface{}) { | ||
l.Logger.Log( | ||
logging.Entry{ | ||
Payload: fmt.Sprintln(v...), | ||
Severity: logging.Error, | ||
}, | ||
) | ||
} | ||
|
||
func (l *CloudLogger) closeLogger() { | ||
l.Client.Close() | ||
} |
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
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.