/
resource.go
69 lines (61 loc) · 1.83 KB
/
resource.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
package keepforcrs
import (
"github.com/giantswarm/k8sclient/v7/pkg/k8sclient"
"github.com/giantswarm/microerror"
"github.com/giantswarm/micrologger"
"k8s.io/apimachinery/pkg/runtime"
)
const (
Name = "keepforcrs"
)
type Config struct {
K8sClient k8sclient.Interface
Logger micrologger.Logger
// NewObjFunc is to return an instance of a pointer for the CR type that
// should be considered for keeping finalizers.
//
// &infrastructurev1alpha3.G8sControlPlane{}
// &apiv1alpha3.MachineDeployment{}
//
NewObjFunc func() runtime.Object
Provider string
}
// Resource receives the runtime object of the underlying controller it is wired
// into and keeps finalizers for that very controller in case the configured
// runtime objects do still exist. This is to have a reliable deletion for the
// following CRs.
//
// watch | delete
// ---------------------------------
// Cluster | G8sControlPlane
// Cluster | MachineDeployment
type Resource struct {
k8sClient k8sclient.Interface
logger micrologger.Logger
newObjFunc func() runtime.Object
provider string
}
func New(config Config) (*Resource, error) {
if config.K8sClient == nil {
return nil, microerror.Maskf(invalidConfigError, "%T.K8sClient must not be empty", config)
}
if config.Logger == nil {
return nil, microerror.Maskf(invalidConfigError, "%T.Logger must not be empty", config)
}
if config.NewObjFunc == nil {
return nil, microerror.Maskf(invalidConfigError, "%T.NewObjFunc must not be empty", config)
}
if config.Provider == "" {
return nil, microerror.Maskf(invalidConfigError, "%T.Provider must not be empty", config)
}
r := &Resource{
k8sClient: config.K8sClient,
logger: config.Logger,
newObjFunc: config.NewObjFunc,
provider: config.Provider,
}
return r, nil
}
func (r *Resource) Name() string {
return Name
}