/
waitForComponentStep.go
57 lines (48 loc) · 1.82 KB
/
waitForComponentStep.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
57
package component
import (
"context"
"fmt"
v1 "github.com/cloudogu/k8s-component-operator/pkg/api/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)
type waitForComponentStep struct {
client componentsClient
labelSelector string
namespace string
}
// NewWaitForComponentStep creates a new setup step which on waits for a component with a specific label
func NewWaitForComponentStep(client componentsClient, labelSelector string, namespace string) *waitForComponentStep {
return &waitForComponentStep{
client: client,
labelSelector: labelSelector,
namespace: namespace,
}
}
// GetStepDescription return the human-readable description of the step
func (wfcs *waitForComponentStep) GetStepDescription() string {
return fmt.Sprintf("Wait for component with selector %s to be installed", wfcs.labelSelector)
}
// PerformSetupStep implements all actions in this step
func (wfcs *waitForComponentStep) PerformSetupStep(ctx context.Context) error {
return wfcs.isComponentInstalled(ctx)
}
// isComponentInstalled does a watch on a component and returns nil if the component is installed
func (wfcs *waitForComponentStep) isComponentInstalled(ctx context.Context) error {
watch, err := wfcs.client.Watch(ctx, metav1.ListOptions{LabelSelector: wfcs.labelSelector})
if err != nil {
return fmt.Errorf("failed to create watch on component: %w", err)
}
for event := range watch.ResultChan() {
component, ok := event.Object.(*v1.Component)
if !ok {
watch.Stop()
return fmt.Errorf("error wait for component: failed to cast event to component: selector=[%s] type=[%s]; object=[%+v]",
wfcs.labelSelector, event.Type, event.Object)
}
if component.Status.Status == v1.ComponentStatusInstalled {
watch.Stop()
return nil
}
}
return fmt.Errorf("component is not ready: watch for component aborted")
}