/
strategyselector.go
85 lines (72 loc) · 3.12 KB
/
strategyselector.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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
package infrastructure
import (
"context"
"fmt"
"github.com/gardener/gardener/extensions/pkg/controller"
extensionsv1alpha1 "github.com/gardener/gardener/pkg/apis/extensions/v1alpha1"
"github.com/gardener/gardener/pkg/extensions"
"github.com/go-logr/logr"
)
// Reconciler is an interface for the infrastructure reconciliation.
type Reconciler interface {
// Reconcile manages infrastructure resources according to desired spec.
Reconcile(ctx context.Context, infra *extensionsv1alpha1.Infrastructure, cluster *controller.Cluster) error
// Delete removes any created infrastructure resource on the provider.
Delete(ctx context.Context, infra *extensionsv1alpha1.Infrastructure, cluster *controller.Cluster) error
// Restore restores the infrastructure after a control plane migration. Effectively it performs a recovery of data from the infrastructure.status.state and
// proceeds to reconcile.
Restore(ctx context.Context, infra *extensionsv1alpha1.Infrastructure, cluster *controller.Cluster) error
}
// ReconcilerFactory can construct the different infrastructure reconciler implementations.
type ReconcilerFactory interface {
Build(useFlow bool) (Reconciler, error)
}
// ReconcilerFactoryImpl is an implementation of a ReconcilerFactory
type ReconcilerFactoryImpl struct {
ctx context.Context
log logr.Logger
a *actuator
infra *extensionsv1alpha1.Infrastructure
}
// Build builds the Reconciler according to the arguments.
func (f ReconcilerFactoryImpl) Build(useFlow bool) (Reconciler, error) {
if useFlow {
reconciler, err := NewFlowReconciler(f.a, f.log, f.a.disableProjectedTokenMount)
if err != nil {
return nil, fmt.Errorf("failed to init flow reconciler: %w", err)
}
return reconciler, nil
}
reconciler, err := NewTerraformReconciler(f.a, f.log, f.a.restConfig, f.a.disableProjectedTokenMount)
if err != nil {
return nil, fmt.Errorf("failed to init terraform reconciler: %w", err)
}
return reconciler, nil
}
// StrategySelector decides the reconciler used.
type StrategySelector interface {
Select(infrastructure *extensionsv1alpha1.Infrastructure, cluster *extensions.Cluster) (bool, error)
}
// SelectorFunc decides the reconciler used.
type SelectorFunc func(*extensionsv1alpha1.Infrastructure, *extensions.Cluster) (bool, error)
// Select selects the reconciler implementation.
func (s SelectorFunc) Select(infrastructure *extensionsv1alpha1.Infrastructure, cluster *extensions.Cluster) (bool, error) {
return s(infrastructure, cluster)
}
// OnReconcile returns true if the operation should use the Flow for the given cluster.
func OnReconcile(infra *extensionsv1alpha1.Infrastructure, cluster *extensions.Cluster) (bool, error) {
hasState, err := hasFlowState(infra.Status)
if err != nil {
return false, err
}
if hasState {
return true, nil
}
return hasState || HasFlowAnnotation(infra, cluster), nil
}
// OnDelete returns true if the operation should use the Flow deletion for the given cluster.
func OnDelete(infra *extensionsv1alpha1.Infrastructure, _ *extensions.Cluster) (bool, error) {
return hasFlowState(infra.Status)
}
// OnRestore decides the reconciler used on migration.
var OnRestore = OnDelete