Преглед изворни кода

Merge pull request #1179 from kubecost/mmd/fix-cronjob-v1-controller-attribution

Attribute batch/v1 CronJob controller correctly
Niko Kovacevic пре 4 година
родитељ
комит
b9b8c54439
2 измењених фајлова са 72 додато и 1 уклоњено
  1. 5 1
      pkg/costmodel/costmodel.go
  2. 67 0
      pkg/costmodel/costmodel_test.go

+ 5 - 1
pkg/costmodel/costmodel.go

@@ -43,7 +43,11 @@ const (
 )
 
 // isCron matches a CronJob name and captures the non-timestamp name
-var isCron = regexp.MustCompile(`^(.+)-\d{10}$`)
+//
+// We support either a 10 character timestamp OR an 8 character timestamp
+// because batch/v1beta1 CronJobs creates Jobs with 10 character timestamps
+// and batch/v1 CronJobs create Jobs with 8 character timestamps.
+var isCron = regexp.MustCompile(`^(.+)-(\d{10}|\d{8})$`)
 
 type CostModel struct {
 	Cache                      clustercache.ClusterCache

+ 67 - 0
pkg/costmodel/costmodel_test.go

@@ -0,0 +1,67 @@
+package costmodel
+
+import (
+	"testing"
+)
+
+func Test_CostData_GetController_CronJob(t *testing.T) {
+	cases := []struct {
+		name string
+		cd   CostData
+
+		expectedName          string
+		expectedKind          string
+		expectedHasController bool
+	}{
+		{
+			name: "batch/v1beta1 CronJob Job name",
+			cd: CostData{
+				// batch/v1beta1 CronJobs create Jobs with a 10 character
+				// timestamp appended to the end of the name.
+				//
+				// It looks like this:
+				// CronJob: cronjob-1
+				// Job: cronjob-1-1651057200
+				// Pod: cronjob-1-1651057200-mf5c9
+				Jobs: []string{"cronjob-1-1651057200"},
+			},
+
+			expectedName:          "cronjob-1",
+			expectedKind:          "job",
+			expectedHasController: true,
+		},
+		{
+			name: "batch/v1 CronJob Job name",
+			cd: CostData{
+				// batch/v1CronJobs create Jobs with an 8 character timestamp
+				// appended to the end of the name.
+				//
+				// It looks like this:
+				// CronJob: cj-v1
+				// Job: cj-v1-27517770
+				// Pod: cj-v1-27517770-xkrgn
+				Jobs: []string{"cj-v1-27517770"},
+			},
+
+			expectedName:          "cj-v1",
+			expectedKind:          "job",
+			expectedHasController: true,
+		},
+	}
+
+	for _, c := range cases {
+		t.Run(c.name, func(t *testing.T) {
+			name, kind, hasController := c.cd.GetController()
+
+			if name != c.expectedName {
+				t.Errorf("Name mismatch. Expected: %s. Got: %s", c.expectedName, name)
+			}
+			if kind != c.expectedKind {
+				t.Errorf("Kind mismatch. Expected: %s. Got: %s", c.expectedKind, kind)
+			}
+			if hasController != c.expectedHasController {
+				t.Errorf("HasController mismatch. Expected: %t. Got: %t", c.expectedHasController, hasController)
+			}
+		})
+	}
+}