This repository has been archived by the owner on Nov 30, 2023. It is now read-only.
/
checkcreationprogress.go
56 lines (47 loc) · 2.27 KB
/
checkcreationprogress.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
package clusterreleaseversion
import (
"context"
"fmt"
"github.com/giantswarm/conditions/pkg/conditions"
"github.com/giantswarm/microerror"
capi "sigs.k8s.io/cluster-api/api/v1beta1"
capiconditions "sigs.k8s.io/cluster-api/util/conditions"
)
// isCreationCompleted checks if the cluster creation has been completed.
func (r *Resource) isCreationCompleted(ctx context.Context, cluster *capi.Cluster) (bool, error) {
r.logger.Debugf(ctx, "checking if cluster creation has been completed")
// Here we expect that Cluster Creating conditions is set to True.
creatingCondition, creatingConditionSet := conditions.GetCreating(cluster)
if !creatingConditionSet || !conditions.IsTrue(&creatingCondition) {
err := microerror.Maskf(
conditions.UnexpectedConditionStatusError,
conditions.ExpectedTrueErrorMessage(cluster, conditions.Creating))
return false, err
}
// Now let's check if the cluster became Ready, which should mean that the
// creation was completed.
readyCondition := capiconditions.Get(cluster, capi.ReadyCondition)
if !conditions.IsTrue(readyCondition) {
r.logger.Debugf(ctx, "Cluster not ready, condition Ready!=True, creation is still in progress")
return false, nil
}
// Cluster is Ready, this should mean that the creation has been completed.
r.logger.Debugf(ctx, "Cluster condition Ready=True, creation should be completed")
// Just a quick sanity check before we call it a win, cluster can become
// ready only after the creation has been initiated.
if readyCondition.LastTransitionTime.Before(&creatingCondition.LastTransitionTime) {
errorMessageFormat := "Ready was set to True (at %s), before Creating was set to false (at %s), " +
"that means that the cluster was ready before the creation has started, which is not possible"
// log the error, in case the caller ignores it
r.logger.LogCtx(ctx, "level", "warning", "message", fmt.Sprintf(errorMessageFormat, readyCondition.LastTransitionTime, creatingCondition.LastTransitionTime))
err := microerror.Maskf(
conditions.UnexpectedConditionStatusError,
errorMessageFormat,
readyCondition.LastTransitionTime,
creatingCondition.LastTransitionTime)
return false, err
}
// Cluster creation has been completed! :)
r.logger.Debugf(ctx, "cluster creation has been completed")
return true, nil
}