fix: clear endpointStatuses for podMonitoring without endpoints · GoogleCloudPlatform/prometheus-engine@a342d9b (original) (raw)

`@@ -45,11 +45,26 @@ import (

`

45

45

`)

`

46

46

``

47

47

`type updateTargetStatusTestCase struct {

`

48

``

`-

desc string

`

49

``

`-

targets []*prometheusv1.TargetsResult

`

50

``

`-

podMonitorings []monitoringv1.PodMonitoring

`

51

``

`-

clusterPodMonitorings []monitoringv1.ClusterPodMonitoring

`

52

``

`-

expErr func(err error) bool

`

``

48

`+

desc string

`

``

49

`+

targets []*prometheusv1.TargetsResult

`

``

50

`+

podMonitorings []monitoringv1.PodMonitoring

`

``

51

`+

initializeStatus []monitoringv1.PodMonitoringStatus

`

``

52

`+

clusterPodMonitorings []monitoringv1.ClusterPodMonitoring

`

``

53

`+

initializeClusterStatus []monitoringv1.PodMonitoringStatus

`

``

54

`+

expErr func(err error) bool

`

``

55

`+

}

`

``

56

+

``

57

`+

func (tc *updateTargetStatusTestCase) getPodMonitoringCRDs() []monitoringv1.PodMonitoringCRD {

`

``

58

`+

var combinedList []monitoringv1.PodMonitoringCRD

`

``

59

+

``

60

`+

for _, pm := range tc.podMonitorings {

`

``

61

`+

combinedList = append(combinedList, &pm)

`

``

62

`+

}

`

``

63

+

``

64

`+

for _, pm := range tc.clusterPodMonitorings {

`

``

65

`+

combinedList = append(combinedList, &pm)

`

``

66

`+

}

`

``

67

`+

return combinedList

`

53

68

`}

`

54

69

``

55

70

`// Given a list of test cases on PodMonitoring, creates a new list containing

`

`@@ -84,6 +99,7 @@ func expand(testCases []updateTargetStatusTestCase) []updateTargetStatusTestCase

`

84

99

` }

`

85

100

`clusterTargets = append(clusterTargets, targetClusterPodMonitoring)

`

86

101

` }

`

``

102

+

87

103

`for _, pm := range tc.podMonitorings {

`

88

104

`pmCopy := pm.DeepCopy()

`

89

105

`cpm := monitoringv1.ClusterPodMonitoring{

`

`@@ -103,26 +119,41 @@ func expand(testCases []updateTargetStatusTestCase) []updateTargetStatusTestCase

`

103

119

` }

`

104

120

`clusterPodMonitorings = append(clusterPodMonitorings, cpm)

`

105

121

` }

`

``

122

+

``

123

`+

initializeClusterStatus := make([]monitoringv1.PodMonitoringStatus, 0, len(tc.initializeStatus))

`

``

124

`+

for _, status := range tc.initializeStatus {

`

``

125

`+

statusCopy := status.DeepCopy()

`

``

126

+

``

127

`+

for idx, status := range statusCopy.EndpointStatuses {

`

``

128

`+

statusCopy.EndpointStatuses[idx].Name = podMonitoringScrapePoolToClusterPodMonitoringScrapePool(status.Name)

`

``

129

`+

}

`

``

130

`+

initializeClusterStatus = append(initializeClusterStatus, *statusCopy)

`

``

131

`+

}

`

``

132

+

106

133

`dataPodMonitorings := updateTargetStatusTestCase{

`

107

``

`-

desc: tc.desc + "-pod-monitoring",

`

108

``

`-

targets: tc.targets,

`

109

``

`-

podMonitorings: tc.podMonitorings,

`

110

``

`-

expErr: tc.expErr,

`

``

134

`+

desc: tc.desc + "-pod-monitoring",

`

``

135

`+

targets: tc.targets,

`

``

136

`+

podMonitorings: tc.podMonitorings,

`

``

137

`+

initializeStatus: tc.initializeStatus,

`

``

138

`+

expErr: tc.expErr,

`

111

139

` }

`

112

140

`dataFinal = append(dataFinal, dataPodMonitorings)

`

113

141

`dataClusterPodMonitorings := updateTargetStatusTestCase{

`

114

``

`-

desc: tc.desc + "-cluster-pod-monitoring",

`

115

``

`-

targets: clusterTargets,

`

116

``

`-

clusterPodMonitorings: clusterPodMonitorings,

`

117

``

`-

expErr: tc.expErr,

`

``

142

`+

desc: tc.desc + "-cluster-pod-monitoring",

`

``

143

`+

targets: clusterTargets,

`

``

144

`+

clusterPodMonitorings: clusterPodMonitorings,

`

``

145

`+

initializeClusterStatus: initializeClusterStatus,

`

``

146

`+

expErr: tc.expErr,

`

118

147

` }

`

119

148

`prometheusTargetsBoth := append(tc.targets, clusterTargets...)

`

120

149

`dataBoth := updateTargetStatusTestCase{

`

121

``

`-

desc: tc.desc + "-both",

`

122

``

`-

targets: prometheusTargetsBoth,

`

123

``

`-

podMonitorings: tc.podMonitorings,

`

124

``

`-

clusterPodMonitorings: clusterPodMonitorings,

`

125

``

`-

expErr: tc.expErr,

`

``

150

`+

desc: tc.desc + "-both",

`

``

151

`+

targets: prometheusTargetsBoth,

`

``

152

`+

podMonitorings: tc.podMonitorings,

`

``

153

`+

initializeStatus: tc.initializeStatus,

`

``

154

`+

clusterPodMonitorings: clusterPodMonitorings,

`

``

155

`+

initializeClusterStatus: initializeClusterStatus,

`

``

156

`+

expErr: tc.expErr,

`

126

157

` }

`

127

158

`dataFinal = append(dataFinal, dataClusterPodMonitorings)

`

128

159

`dataFinal = append(dataFinal, dataBoth)

`

`@@ -1225,27 +1256,100 @@ func TestUpdateTargetStatus(t *testing.T) {

`

1225

1256

`return err.Error() == "unknown scrape kind "unknown""

`

1226

1257

` },

`

1227

1258

` },

`

``

1259

`+

// No targets, with PodMonitoring config.

`

``

1260

`+

{

`

``

1261

`+

desc: "no-targets-no-match",

`

``

1262

`+

podMonitorings: []monitoringv1.PodMonitoring{

`

``

1263

`+

{

`

``

1264

`+

ObjectMeta: metav1.ObjectMeta{Name: "prom-example-1", Namespace: "gmp-test"},

`

``

1265

`+

Spec: monitoringv1.PodMonitoringSpec{},

`

``

1266

+

``

1267

`+

Status: monitoringv1.PodMonitoringStatus{

`

``

1268

`+

MonitoringStatus: monitoringv1.MonitoringStatus{

`

``

1269

`+

ObservedGeneration: 2,

`

``

1270

`+

Conditions: []monitoringv1.MonitoringCondition{{

`

``

1271

`+

Type: monitoringv1.ConfigurationCreateSuccess,

`

``

1272

`+

Status: corev1.ConditionTrue,

`

``

1273

`+

LastUpdateTime: metav1.Time{},

`

``

1274

`+

LastTransitionTime: metav1.Time{},

`

``

1275

`+

Reason: "",

`

``

1276

`+

Message: "",

`

``

1277

`+

}},

`

``

1278

`+

},

`

``

1279

`+

},

`

``

1280

`+

},

`

``

1281

`+

},

`

``

1282

`+

initializeStatus: []monitoringv1.PodMonitoringStatus{

`

``

1283

`+

{

`

``

1284

`+

MonitoringStatus: monitoringv1.MonitoringStatus{

`

``

1285

`+

ObservedGeneration: 2,

`

``

1286

`+

Conditions: []monitoringv1.MonitoringCondition{{

`

``

1287

`+

Type: monitoringv1.ConfigurationCreateSuccess,

`

``

1288

`+

Status: corev1.ConditionTrue,

`

``

1289

`+

LastUpdateTime: metav1.Time{},

`

``

1290

`+

LastTransitionTime: metav1.Time{},

`

``

1291

`+

Reason: "",

`

``

1292

`+

Message: "",

`

``

1293

`+

}},

`

``

1294

`+

},

`

``

1295

`+

EndpointStatuses: []monitoringv1.ScrapeEndpointStatus{

`

``

1296

`+

{

`

``

1297

`+

Name: "PodMonitoring/gmp-test/prom-example-1/metrics",

`

``

1298

`+

ActiveTargets: 1,

`

``

1299

`+

UnhealthyTargets: 0,

`

``

1300

`+

LastUpdateTime: date,

`

``

1301

`+

SampleGroups: []monitoringv1.SampleGroup{

`

``

1302

`+

{

`

``

1303

`+

SampleTargets: []monitoringv1.SampleTarget{

`

``

1304

`+

{

`

``

1305

`+

Health: "up",

`

``

1306

`+

Labels: map[model.LabelName]model.LabelValue{

`

``

1307

`+

"instance": "a",

`

``

1308

`+

},

`

``

1309

`+

LastScrapeDurationSeconds: "1.2",

`

``

1310

`+

},

`

``

1311

`+

},

`

``

1312

`+

Count: ptr.To(int32(1)),

`

``

1313

`+

},

`

``

1314

`+

},

`

``

1315

`+

CollectorsFraction: "1",

`

``

1316

`+

},

`

``

1317

`+

},

`

``

1318

`+

},

`

``

1319

`+

},

`

``

1320

`+

},

`

1228

1321

` })

`

1229

1322

``

1230

1323

`for _, testCase := range testCases {

`

1231

1324

`t.Run(fmt.Sprintf("target-status-conversion-%s", testCase.desc), func(t *testing.T) {

`

1232

1325

`clientBuilder := newFakeClientBuilder()

`

1233

``

`-

for _, podMonitoring := range testCase.podMonitorings {

`

``

1326

`+

for i, podMonitoring := range testCase.podMonitorings {

`

1234

1327

`pmCopy := podMonitoring.DeepCopy()

`

1235

``

`-

pmCopy.GetPodMonitoringStatus().EndpointStatuses = nil

`

``

1328

`+

if len(testCase.initializeStatus) > 0 {

`

``

1329

`+

pmCopy.Status = testCase.initializeStatus[i]

`

``

1330

`+

} else {

`

``

1331

`+

pmCopy.GetPodMonitoringStatus().EndpointStatuses = nil

`

``

1332

`+

}

`

1236

1333

`clientBuilder.WithObjects(pmCopy)

`

1237

1334

` }

`

1238

``

`-

for _, clusterPodMonitoring := range testCase.clusterPodMonitorings {

`

``

1335

`+

for i, clusterPodMonitoring := range testCase.clusterPodMonitorings {

`

1239

1336

`pmCopy := clusterPodMonitoring.DeepCopy()

`

1240

``

`-

pmCopy.GetPodMonitoringStatus().EndpointStatuses = nil

`

``

1337

`+

if len(testCase.initializeClusterStatus) > 0 {

`

``

1338

`+

pmCopy.Status = testCase.initializeClusterStatus[i]

`

``

1339

`+

} else {

`

``

1340

`+

pmCopy.GetPodMonitoringStatus().EndpointStatuses = nil

`

``

1341

`+

}

`

1241

1342

`clientBuilder.WithObjects(pmCopy)

`

1242

1343

` }

`

1243

1344

``

1244

1345

`kubeClient := clientBuilder.Build()

`

1245

1346

``

1246

``

`-

err := updateTargetStatus(context.Background(), testr.New(t), kubeClient, testCase.targets)

`

``

1347

`+

// fetchTargets(ctx, logger, opts, nil, targetFetchFromMap(prometheusTargetMap), kubeClient)

`

``

1348

`+

err := updateTargetStatus(context.Background(), testr.New(t), kubeClient, testCase.targets, testCase.getPodMonitoringCRDs())

`

1247

1349

`if err != nil && (testCase.expErr == nil || !testCase.expErr(err)) {

`

1248

1350

`t.Fatalf("unexpected error updating target status: %s", err)

`

``

1351

`+

} else if err == nil && (testCase.expErr != nil) {

`

``

1352

`+

t.Fatalf("expected error missing when updating target status")

`

1249

1353

` }

`

1250

1354

``

1251

1355

`for _, podMonitoring := range testCase.podMonitorings {

`

`@@ -1602,24 +1706,6 @@ func TestShouldPoll(t *testing.T) {

`

1602

1706

`should: false,

`

1603

1707

`expErr: true,

`

1604

1708

` },

`

1605

``

`-

{

`

1606

``

`-

desc: "should not poll targets - no podmonitorings",

`

1607

``

`-

objs: []client.Object{

`

1608

``

`-

&monitoringv1.OperatorConfig{

`

1609

``

`-

ObjectMeta: metav1.ObjectMeta{

`

1610

``

`-

Name: "config",

`

1611

``

`-

Namespace: "gmp-public",

`

1612

``

`-

},

`

1613

``

`-

Features: monitoringv1.OperatorFeatures{

`

1614

``

`-

TargetStatus: monitoringv1.TargetStatusSpec{

`

1615

``

`-

Enabled: true,

`

1616

``

`-

},

`

1617

``

`-

},

`

1618

``

`-

},

`

1619

``

`-

},

`

1620

``

`-

should: false,

`

1621

``

`-

expErr: false,

`

1622

``

`-

},

`

1623

1709

` {

`

1624

1710

`desc: "should not poll targets - disabled",

`

1625

1711

`objs: []client.Object{

`