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
This commit is contained in:
João Palet 2024-07-02 09:26:47 +01:00 committed by GitHub
parent b54c671082
commit 7e51a0a5d5
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 44 additions and 38 deletions

View file

@ -1522,30 +1522,28 @@ func getMaintenanceTimes(ctx context.Context, cl *ske.Cluster, m *Model) (startT
return "", "", fmt.Errorf("converting maintenance object %w", core.DiagsToError(diags.Errors())) return "", "", fmt.Errorf("converting maintenance object %w", core.DiagsToError(diags.Errors()))
} }
if maintenance.Start.IsNull() || maintenance.Start.IsUnknown() { startTime = startTimeAPI.Format("15:04:05Z07:00")
startTime = startTimeAPI.Format("15:04:05Z07:00") if !(maintenance.Start.IsNull() || maintenance.Start.IsUnknown()) {
} else {
startTimeTF, err := time.Parse("15:04:05Z07:00", maintenance.Start.ValueString()) startTimeTF, err := time.Parse("15:04:05Z07:00", maintenance.Start.ValueString())
if err != nil { if err != nil {
return "", "", fmt.Errorf("parsing start time '%s' from TF config as RFC time: %w", maintenance.Start.ValueString(), err) return "", "", fmt.Errorf("parsing start time '%s' from TF config as RFC time: %w", maintenance.Start.ValueString(), err)
} }
if startTimeAPI.Format("15:04:05Z07:00") != startTimeTF.Format("15:04:05Z07:00") { // If the start times from the API and the TF model just differ in format, we keep the current TF model value
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()) if startTimeAPI.Format("15:04:05Z07:00") == startTimeTF.Format("15:04:05Z07:00") {
startTime = maintenance.Start.ValueString()
} }
startTime = maintenance.Start.ValueString()
} }
if maintenance.End.IsNull() || maintenance.End.IsUnknown() { endTime = endTimeAPI.Format("15:04:05Z07:00")
endTime = endTimeAPI.Format("15:04:05Z07:00") if !(maintenance.End.IsNull() || maintenance.End.IsUnknown()) {
} else {
endTimeTF, err := time.Parse("15:04:05Z07:00", maintenance.End.ValueString()) endTimeTF, err := time.Parse("15:04:05Z07:00", maintenance.End.ValueString())
if err != nil { if err != nil {
return "", "", fmt.Errorf("parsing end time '%s' from TF config as RFC time: %w", maintenance.End.ValueString(), err) return "", "", fmt.Errorf("parsing end time '%s' from TF config as RFC time: %w", maintenance.End.ValueString(), err)
} }
if endTimeAPI.Format("15:04:05Z07:00") != endTimeTF.Format("15:04:05Z07:00") { // If the end times from the API and the TF model just differ in format, we keep the current TF model value
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()) if endTimeAPI.Format("15:04:05Z07:00") == endTimeTF.Format("15:04:05Z07:00") {
endTime = maintenance.End.ValueString()
} }
endTime = maintenance.End.ValueString()
} }
return startTime, endTime, nil return startTime, endTime, nil

View file

@ -1461,36 +1461,44 @@ func TestGetMaintenanceTimes(t *testing.T) {
endExpected: "14:15:16Z", endExpected: "14:15:16Z",
}, },
{ {
description: "tf_state_doesnt_match_1", description: "api_takes_precedence_if_different_1",
startAPI: "0001-02-03T04:05:06+07:08", startAPI: "0001-02-03T04:05:06+07:08",
startTF: utils.Ptr("00:00:00+07:08"), startTF: utils.Ptr("00:00:00+07:08"),
endAPI: "0011-12-13T14:15:16+17:18", endAPI: "0011-12-13T14:15:16+17:18",
endTF: utils.Ptr("14:15:16+17:18"), endTF: utils.Ptr("14:15:16+17:18"),
isValid: false, isValid: true,
startExpected: "04:05:06+07:08",
endExpected: "14:15:16+17:18",
}, },
{ {
description: "tf_state_doesnt_match_2", description: "api_takes_precedence_if_different_2",
startAPI: "0001-02-03T04:05:06+07:08", startAPI: "0001-02-03T04:05:06+07:08",
startTF: utils.Ptr("04:05:06+07:08"), startTF: utils.Ptr("04:05:06+07:08"),
endAPI: "0011-12-13T14:15:16+17:18", endAPI: "0011-12-13T14:15:16+17:18",
endTF: utils.Ptr("00:00:00+17:18"), endTF: utils.Ptr("00:00:00+17:18"),
isValid: false, isValid: true,
startExpected: "04:05:06+07:08",
endExpected: "14:15:16+17:18",
}, },
{ {
description: "tf_state_doesnt_match_3", description: "api_takes_precedence_if_different_3",
startAPI: "0001-02-03T04:05:06+07:08", startAPI: "0001-02-03T04:05:06+07:08",
startTF: utils.Ptr("04:05:06Z"), startTF: utils.Ptr("04:05:06Z"),
endAPI: "0011-12-13T14:15:16+17:18", endAPI: "0011-12-13T14:15:16+17:18",
endTF: utils.Ptr("14:15:16+17:18"), endTF: utils.Ptr("14:15:16+17:18"),
isValid: false, isValid: true,
startExpected: "04:05:06+07:08",
endExpected: "14:15:16+17:18",
}, },
{ {
description: "tf_state_doesnt_match_4", description: "api_takes_precedence_if_different_3",
startAPI: "0001-02-03T04:05:06+07:08", startAPI: "0001-02-03T04:05:06+07:08",
startTF: utils.Ptr("04:05:06+07:08"), startTF: utils.Ptr("04:05:06+07:08"),
endAPI: "0011-12-13T14:15:16+17:18", endAPI: "0011-12-13T14:15:16+17:18",
endTF: utils.Ptr("14:15:16Z"), endTF: utils.Ptr("14:15:16Z"),
isValid: false, isValid: true,
startExpected: "04:05:06+07:08",
endExpected: "14:15:16+17:18",
}, },
} }
for _, tt := range tests { for _, tt := range tests {
@ -1530,10 +1538,10 @@ func TestGetMaintenanceTimes(t *testing.T) {
t.Fatalf("getMaintenanceTimes didn't fail on invalid input") t.Fatalf("getMaintenanceTimes didn't fail on invalid input")
} }
if tt.startExpected != start { if tt.startExpected != start {
t.Errorf("extected start '%s', got '%s'", tt.startExpected, start) t.Errorf("expected start '%s', got '%s'", tt.startExpected, start)
} }
if tt.endExpected != end { if tt.endExpected != end {
t.Errorf("extected end '%s', got '%s'", tt.endExpected, end) t.Errorf("expected end '%s', got '%s'", tt.endExpected, end)
} }
}) })
} }