瀏覽代碼

Add comments and logging to pkg/cloud (#2560)

Signed-off-by: thomasvn <thomasnguyen96@gmail.com>
Thomas Nguyen 2 年之前
父節點
當前提交
4dec36ece8
共有 2 個文件被更改,包括 21 次插入13 次删除
  1. 12 4
      pkg/cloud/azure/storagebillingparser.go
  2. 9 9
      pkg/cloud/config/controller.go

+ 12 - 4
pkg/cloud/azure/storagebillingparser.go

@@ -133,6 +133,9 @@ func (asbp *AzureStorageBillingParser) parseCSV(start, end time.Time, reader *cs
 	return nil
 }
 
+// getMostRecentBlobs returns a list of filepaths on the Azure Storage
+// Container. It uses the "Last Modified Time" of the file to determine which
+// has the latest month-to-date billing data.
 func (asbp *AzureStorageBillingParser) getMostRecentBlobs(start, end time.Time, client *azblob.Client, ctx context.Context) ([]string, error) {
 	log.Infof("Azure Storage: retrieving most recent reports from: %v - %v", start, end)
 
@@ -141,7 +144,7 @@ func (asbp *AzureStorageBillingParser) getMostRecentBlobs(start, end time.Time,
 	if err != nil {
 		return nil, err
 	}
-	mostResentBlobs := make(map[string]container.BlobItem)
+	mostRecentBlobs := make(map[string]container.BlobItem)
 
 	pager := client.NewListBlobsFlatPager(asbp.Container, &azblob.ListBlobsFlatOptions{
 		Include: container.ListBlobsInclude{Deleted: false, Versions: false},
@@ -165,12 +168,12 @@ func (asbp *AzureStorageBillingParser) getMostRecentBlobs(start, end time.Time,
 			for _, month := range monthStrs {
 				if strings.Contains(*blobInfo.Name, month) {
 					// check if blob is the newest seen for this month
-					if prevBlob, ok := mostResentBlobs[month]; ok {
+					if prevBlob, ok := mostRecentBlobs[month]; ok {
 						if prevBlob.Properties.CreationTime.After(*blobInfo.Properties.CreationTime) {
 							continue
 						}
 					}
-					mostResentBlobs[month] = *blobInfo
+					mostRecentBlobs[month] = *blobInfo
 				}
 			}
 		}
@@ -179,7 +182,7 @@ func (asbp *AzureStorageBillingParser) getMostRecentBlobs(start, end time.Time,
 	// convert blob names into blob urls and move from map into ordered list of blob names
 	var blobNames []string
 	for _, month := range monthStrs {
-		if blob, ok := mostResentBlobs[month]; ok {
+		if blob, ok := mostRecentBlobs[month]; ok {
 			blobNames = append(blobNames, *blob.Name)
 		}
 	}
@@ -187,6 +190,11 @@ func (asbp *AzureStorageBillingParser) getMostRecentBlobs(start, end time.Time,
 	return blobNames, nil
 }
 
+// getMonthStrings returns a list of month strings in the format
+// "YYYYMMDD-YYYYMMDD", where the dates are exactly the first and last day of
+// the month. It includes all month strings which would capture the start and
+// end parameters.
+// For example: ["20240201-20240229", "20240101-20240131", "20231201-20231231"]
 func (asbp *AzureStorageBillingParser) getMonthStrings(start, end time.Time) ([]string, error) {
 	if start.After(end) {
 		return []string{}, fmt.Errorf("start date must be before end date")

+ 9 - 9
pkg/cloud/config/controller.go

@@ -66,7 +66,7 @@ func (c *Controller) pullWatchers() {
 	defer c.lock.Unlock()
 	statuses, err := c.load()
 	if err != nil {
-		log.Errorf("failed to load statuses when pulling watchers %s", err)
+		log.Warnf("Controller: pullWatchers: %s. Proceeding to create the file", err.Error())
 		statuses = Statuses{}
 	}
 	for source, watcher := range c.watchers {
@@ -81,7 +81,7 @@ func (c *Controller) pullWatchers() {
 				if _, ok := watcherConfsByKey[status.Key]; !ok {
 					err := c.deleteConfig(status.Key, status.Source, statuses)
 					if err != nil {
-						log.Errorf("Conrtoller: pullWatchers: %s", err.Error())
+						log.Errorf("Controller: pullWatchers: %s", err.Error())
 					}
 				}
 
@@ -99,7 +99,7 @@ func (c *Controller) pullWatchers() {
 				// remove the existing config
 				err := c.deleteConfig(key, source, statuses)
 				if err != nil {
-					log.Errorf("Conrtoller: pullWatchers: %s", err.Error())
+					log.Errorf("Controller: pullWatchers: %s", err.Error())
 				}
 
 			}
@@ -109,7 +109,7 @@ func (c *Controller) pullWatchers() {
 
 			configType, err := ConfigTypeFromConfig(conf)
 			if err != nil {
-				log.Errorf("failed to get config type for config with key: %s", conf.Key())
+				log.Errorf("Controller: pullWatchers: failed to get config type for config with key: %s", conf.Key())
 				continue
 			}
 
@@ -156,7 +156,7 @@ func (c *Controller) pullWatchers() {
 			}
 			err = c.save(statuses)
 			if err != nil {
-				log.Errorf("failed to save statuses %s", err.Error())
+				log.Errorf("Controller: pullWatchers: failed to save statuses %s", err.Error())
 			}
 		}
 	}
@@ -324,13 +324,13 @@ func (c *Controller) deleteConfig(key string, source ConfigSource, statuses Stat
 func (c *Controller) load() (Statuses, error) {
 	raw, err := os.ReadFile(c.path)
 	if err != nil {
-		return nil, fmt.Errorf("ConfigController: failed to load config statuses from file: %w", err)
+		return nil, fmt.Errorf("failed to load config statuses from file: %w", err)
 	}
 
 	statuses := Statuses{}
 	err = json.Unmarshal(raw, &statuses)
 	if err != nil {
-		return nil, fmt.Errorf("ConfigController: failed to marshal config statuses: %s", err.Error())
+		return nil, fmt.Errorf("failed to marshal config statuses: %s", err.Error())
 	}
 
 	return statuses, nil
@@ -340,12 +340,12 @@ func (c *Controller) save(statuses Statuses) error {
 
 	raw, err := json.Marshal(statuses)
 	if err != nil {
-		return fmt.Errorf("ConfigController: failed to marshal config statuses: %s", err)
+		return fmt.Errorf("failed to marshal config statuses: %s", err)
 	}
 
 	err = os.WriteFile(c.path, raw, 0644)
 	if err != nil {
-		return fmt.Errorf("ConfigController: failed to save config statuses to file: %s", err)
+		return fmt.Errorf("failed to save config statuses to file: %s", err)
 	}
 
 	return nil