Skip to content

Commit 7e51a0a

Browse files
authored
Fix SKE cluster maintenance updated outside TF causing error (#444)
* Fix SKE cluster maintenance updated outsise TF causing error * Add comment to logic * Simplify code
1 parent b54c671 commit 7e51a0a

2 files changed

Lines changed: 44 additions & 38 deletions

File tree

stackit/internal/services/ske/cluster/resource.go

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1522,30 +1522,28 @@ func getMaintenanceTimes(ctx context.Context, cl *ske.Cluster, m *Model) (startT
15221522
return "", "", fmt.Errorf("converting maintenance object %w", core.DiagsToError(diags.Errors()))
15231523
}
15241524

1525-
if maintenance.Start.IsNull() || maintenance.Start.IsUnknown() {
1526-
startTime = startTimeAPI.Format("15:04:05Z07:00")
1527-
} else {
1525+
startTime = startTimeAPI.Format("15:04:05Z07:00")
1526+
if !(maintenance.Start.IsNull() || maintenance.Start.IsUnknown()) {
15281527
startTimeTF, err := time.Parse("15:04:05Z07:00", maintenance.Start.ValueString())
15291528
if err != nil {
15301529
return "", "", fmt.Errorf("parsing start time '%s' from TF config as RFC time: %w", maintenance.Start.ValueString(), err)
15311530
}
1532-
if startTimeAPI.Format("15:04:05Z07:00") != startTimeTF.Format("15:04:05Z07:00") {
1533-
return "", "", fmt.Errorf("start time '%v' from API response doesn't match start time '%v' from TF config", *cl.Maintenance.TimeWindow.Start, maintenance.Start.ValueString())
1531+
// If the start times from the API and the TF model just differ in format, we keep the current TF model value
1532+
if startTimeAPI.Format("15:04:05Z07:00") == startTimeTF.Format("15:04:05Z07:00") {
1533+
startTime = maintenance.Start.ValueString()
15341534
}
1535-
startTime = maintenance.Start.ValueString()
15361535
}
15371536

1538-
if maintenance.End.IsNull() || maintenance.End.IsUnknown() {
1539-
endTime = endTimeAPI.Format("15:04:05Z07:00")
1540-
} else {
1537+
endTime = endTimeAPI.Format("15:04:05Z07:00")
1538+
if !(maintenance.End.IsNull() || maintenance.End.IsUnknown()) {
15411539
endTimeTF, err := time.Parse("15:04:05Z07:00", maintenance.End.ValueString())
15421540
if err != nil {
15431541
return "", "", fmt.Errorf("parsing end time '%s' from TF config as RFC time: %w", maintenance.End.ValueString(), err)
15441542
}
1545-
if endTimeAPI.Format("15:04:05Z07:00") != endTimeTF.Format("15:04:05Z07:00") {
1546-
return "", "", fmt.Errorf("end time '%v' from API response doesn't match end time '%v' from TF config", *cl.Maintenance.TimeWindow.End, maintenance.End.ValueString())
1543+
// If the end times from the API and the TF model just differ in format, we keep the current TF model value
1544+
if endTimeAPI.Format("15:04:05Z07:00") == endTimeTF.Format("15:04:05Z07:00") {
1545+
endTime = maintenance.End.ValueString()
15471546
}
1548-
endTime = maintenance.End.ValueString()
15491547
}
15501548

15511549
return startTime, endTime, nil

stackit/internal/services/ske/cluster/resource_test.go

Lines changed: 34 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1461,36 +1461,44 @@ func TestGetMaintenanceTimes(t *testing.T) {
14611461
endExpected: "14:15:16Z",
14621462
},
14631463
{
1464-
description: "tf_state_doesnt_match_1",
1465-
startAPI: "0001-02-03T04:05:06+07:08",
1466-
startTF: utils.Ptr("00:00:00+07:08"),
1467-
endAPI: "0011-12-13T14:15:16+17:18",
1468-
endTF: utils.Ptr("14:15:16+17:18"),
1469-
isValid: false,
1464+
description: "api_takes_precedence_if_different_1",
1465+
startAPI: "0001-02-03T04:05:06+07:08",
1466+
startTF: utils.Ptr("00:00:00+07:08"),
1467+
endAPI: "0011-12-13T14:15:16+17:18",
1468+
endTF: utils.Ptr("14:15:16+17:18"),
1469+
isValid: true,
1470+
startExpected: "04:05:06+07:08",
1471+
endExpected: "14:15:16+17:18",
14701472
},
14711473
{
1472-
description: "tf_state_doesnt_match_2",
1473-
startAPI: "0001-02-03T04:05:06+07:08",
1474-
startTF: utils.Ptr("04:05:06+07:08"),
1475-
endAPI: "0011-12-13T14:15:16+17:18",
1476-
endTF: utils.Ptr("00:00:00+17:18"),
1477-
isValid: false,
1474+
description: "api_takes_precedence_if_different_2",
1475+
startAPI: "0001-02-03T04:05:06+07:08",
1476+
startTF: utils.Ptr("04:05:06+07:08"),
1477+
endAPI: "0011-12-13T14:15:16+17:18",
1478+
endTF: utils.Ptr("00:00:00+17:18"),
1479+
isValid: true,
1480+
startExpected: "04:05:06+07:08",
1481+
endExpected: "14:15:16+17:18",
14781482
},
14791483
{
1480-
description: "tf_state_doesnt_match_3",
1481-
startAPI: "0001-02-03T04:05:06+07:08",
1482-
startTF: utils.Ptr("04:05:06Z"),
1483-
endAPI: "0011-12-13T14:15:16+17:18",
1484-
endTF: utils.Ptr("14:15:16+17:18"),
1485-
isValid: false,
1484+
description: "api_takes_precedence_if_different_3",
1485+
startAPI: "0001-02-03T04:05:06+07:08",
1486+
startTF: utils.Ptr("04:05:06Z"),
1487+
endAPI: "0011-12-13T14:15:16+17:18",
1488+
endTF: utils.Ptr("14:15:16+17:18"),
1489+
isValid: true,
1490+
startExpected: "04:05:06+07:08",
1491+
endExpected: "14:15:16+17:18",
14861492
},
14871493
{
1488-
description: "tf_state_doesnt_match_4",
1489-
startAPI: "0001-02-03T04:05:06+07:08",
1490-
startTF: utils.Ptr("04:05:06+07:08"),
1491-
endAPI: "0011-12-13T14:15:16+17:18",
1492-
endTF: utils.Ptr("14:15:16Z"),
1493-
isValid: false,
1494+
description: "api_takes_precedence_if_different_3",
1495+
startAPI: "0001-02-03T04:05:06+07:08",
1496+
startTF: utils.Ptr("04:05:06+07:08"),
1497+
endAPI: "0011-12-13T14:15:16+17:18",
1498+
endTF: utils.Ptr("14:15:16Z"),
1499+
isValid: true,
1500+
startExpected: "04:05:06+07:08",
1501+
endExpected: "14:15:16+17:18",
14941502
},
14951503
}
14961504
for _, tt := range tests {
@@ -1530,10 +1538,10 @@ func TestGetMaintenanceTimes(t *testing.T) {
15301538
t.Fatalf("getMaintenanceTimes didn't fail on invalid input")
15311539
}
15321540
if tt.startExpected != start {
1533-
t.Errorf("extected start '%s', got '%s'", tt.startExpected, start)
1541+
t.Errorf("expected start '%s', got '%s'", tt.startExpected, start)
15341542
}
15351543
if tt.endExpected != end {
1536-
t.Errorf("extected end '%s', got '%s'", tt.endExpected, end)
1544+
t.Errorf("expected end '%s', got '%s'", tt.endExpected, end)
15371545
}
15381546
})
15391547
}

0 commit comments

Comments
 (0)