Skip to content

Commit

Permalink
Chore: rework TimeConverter and remove dup definition (#599)
Browse files Browse the repository at this point in the history
  • Loading branch information
regadas authored Jan 9, 2023
1 parent c94823a commit dc6cac7
Show file tree
Hide file tree
Showing 5 changed files with 14 additions and 45 deletions.
28 changes: 1 addition & 27 deletions apis/flinkcluster/v1beta1/flinkcluster_types_util.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ func (j *JobStatus) IsSavepointUpToDate(spec *JobSpec, compareTime time.Time) bo
}

var stateMaxAge = int(*spec.MaxStateAgeToRestoreSeconds)
return !hasTimeElapsed(j.SavepointTime, compareTime, stateMaxAge)
return !util.HasTimeElapsed(j.SavepointTime, compareTime, stateMaxAge)
}

// ShouldRestart returns true if the controller should restart failed job.
Expand Down Expand Up @@ -121,32 +121,6 @@ func (r *RevisionStatus) IsUpdateTriggered() bool {
return r.CurrentRevision != r.NextRevision
}

// TimeConverter converts between time.Time and string.
type TimeConverter struct{}

// FromString converts string to time.Time.
func (tc *TimeConverter) FromString(timeStr string) time.Time {
timestamp, err := time.Parse(
time.RFC3339, timeStr)
if err != nil {
panic(fmt.Sprintf("Failed to parse time string: %s", timeStr))
}
return timestamp
}

// ToString converts time.Time to string.
func (tc *TimeConverter) ToString(timestamp time.Time) string {
return timestamp.Format(time.RFC3339)
}

// Check time has passed
func hasTimeElapsed(timeToCheckStr string, now time.Time, intervalSec int) bool {
tc := &TimeConverter{}
timeToCheck := tc.FromString(timeToCheckStr)
intervalPassedTime := timeToCheck.Add(time.Duration(int64(intervalSec) * int64(time.Second)))
return now.After(intervalPassedTime)
}

func isBlank(s *string) bool {
return s == nil || strings.TrimSpace(*s) == ""
}
Expand Down
5 changes: 3 additions & 2 deletions apis/flinkcluster/v1beta1/flinkcluster_types_util_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,13 @@ import (
"testing"
"time"

"github.com/spotify/flink-on-k8s-operator/internal/util"
"gotest.tools/v3/assert"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)

func TestIsSavepointUpToDate(t *testing.T) {
var tc = &TimeConverter{}
var tc = &util.TimeConverter{}
var savepointTime = time.Now()
var jobCompletionTime = savepointTime.Add(time.Second * 100)
var maxStateAgeToRestoreSeconds = int32(300)
Expand Down Expand Up @@ -92,7 +93,7 @@ func TestIsSavepointUpToDate(t *testing.T) {
}

func TestShouldRestartJob(t *testing.T) {
var tc = &TimeConverter{}
var tc = &util.TimeConverter{}
var restartOnFailure = JobRestartPolicyFromSavepointOnFailure
var neverRestart = JobRestartPolicyNever
var maxStateAgeToRestoreSeconds = int32(300) // 5 min
Expand Down
3 changes: 2 additions & 1 deletion apis/flinkcluster/v1beta1/flinkcluster_validate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (
"testing"
"time"

"github.com/spotify/flink-on-k8s-operator/internal/util"
"k8s.io/apimachinery/pkg/api/resource"

"gotest.tools/v3/assert"
Expand Down Expand Up @@ -371,7 +372,7 @@ func TestTaskManagerDeploymentTypeUpdate(t *testing.T) {

func TestUpdateJob(t *testing.T) {
var validator = &Validator{}
var tc = &TimeConverter{}
var tc = &util.TimeConverter{}
var maxStateAge = time.Duration(MaxStateAgeToRestore)
var jarFileNew = "gs://my-bucket/myjob-v2.jar"

Expand Down
15 changes: 0 additions & 15 deletions apis/flinkcluster/v1beta1/zz_generated.deepcopy.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions internal/util/time.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,14 @@ func (tc *TimeConverter) ToString(timestamp time.Time) string {
return timestamp.Format(time.RFC3339)
}

// Check time has passed
func HasTimeElapsed(timeToCheckStr string, now time.Time, intervalSec int) bool {
tc := &TimeConverter{}
timeToCheck := tc.FromString(timeToCheckStr)
intervalPassedTime := timeToCheck.Add(time.Duration(int64(intervalSec) * int64(time.Second)))
return now.After(intervalPassedTime)
}

// SetTimestamp sets the current timestamp to the target.
func SetTimestamp(target *string) {
var tc = &TimeConverter{}
Expand Down

0 comments on commit dc6cac7

Please sign in to comment.