-
Notifications
You must be signed in to change notification settings - Fork 1
/
recreate_resource_operation.go
127 lines (105 loc) · 3.68 KB
/
recreate_resource_operation.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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
package opertn
import (
"context"
"fmt"
"time"
"k8s.io/apimachinery/pkg/api/meta"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"k8s.io/client-go/dynamic"
"github.com/werf/kubedog/pkg/trackers/dyntracker"
"github.com/werf/kubedog/pkg/trackers/dyntracker/statestore"
"github.com/werf/kubedog/pkg/trackers/dyntracker/util"
"github.com/werf/nelm/pkg/kubeclnt"
"github.com/werf/nelm/pkg/resrc"
"github.com/werf/nelm/pkg/resrcid"
)
var _ Operation = (*RecreateResourceOperation)(nil)
const TypeRecreateResourceOperation = "recreate"
const TypeExtraPostRecreateResourceOperation = "extra-post-recreate"
func NewRecreateResourceOperation(
resource *resrcid.ResourceID,
unstruct *unstructured.Unstructured,
absenceTaskState *util.Concurrent[*statestore.AbsenceTaskState],
kubeClient kubeclnt.KubeClienter,
dynamicClient dynamic.Interface,
mapper meta.ResettableRESTMapper,
opts RecreateResourceOperationOptions,
) *RecreateResourceOperation {
return &RecreateResourceOperation{
resource: resource,
unstruct: unstruct,
taskState: absenceTaskState,
kubeClient: kubeClient,
dynamicClient: dynamicClient,
mapper: mapper,
manageableBy: opts.ManageableBy,
forceReplicas: opts.ForceReplicas,
deletionTrackTimeout: opts.DeletionTrackTimeout,
deletionTrackPollPeriod: opts.DeletionTrackPollPeriod,
extraPost: opts.ExtraPost,
}
}
type RecreateResourceOperationOptions struct {
ManageableBy resrc.ManageableBy
ForceReplicas *int
DeletionTrackTimeout time.Duration
DeletionTrackPollPeriod time.Duration
ExtraPost bool
}
type RecreateResourceOperation struct {
resource *resrcid.ResourceID
unstruct *unstructured.Unstructured
taskState *util.Concurrent[*statestore.AbsenceTaskState]
kubeClient kubeclnt.KubeClienter
dynamicClient dynamic.Interface
mapper meta.ResettableRESTMapper
manageableBy resrc.ManageableBy
forceReplicas *int
deletionTrackTimeout time.Duration
deletionTrackPollPeriod time.Duration
extraPost bool
status Status
}
func (o *RecreateResourceOperation) Execute(ctx context.Context) error {
if err := o.kubeClient.Delete(ctx, o.resource, kubeclnt.KubeClientDeleteOptions{}); err != nil {
o.status = StatusFailed
return fmt.Errorf("error deleting resource: %w", err)
}
tracker := dyntracker.NewDynamicAbsenceTracker(o.taskState, o.dynamicClient, o.mapper, dyntracker.DynamicAbsenceTrackerOptions{
Timeout: o.deletionTrackTimeout,
PollPeriod: o.deletionTrackPollPeriod,
})
if err := tracker.Track(ctx); err != nil {
o.status = StatusFailed
return fmt.Errorf("track resource absence: %w", err)
}
if _, err := o.kubeClient.Create(ctx, o.resource, o.unstruct, kubeclnt.KubeClientCreateOptions{
ForceReplicas: o.forceReplicas,
}); err != nil {
o.status = StatusFailed
return fmt.Errorf("error creating resource: %w", err)
}
o.status = StatusCompleted
return nil
}
func (o *RecreateResourceOperation) ID() string {
if o.extraPost {
return TypeExtraPostRecreateResourceOperation + "/" + o.resource.ID()
}
return TypeRecreateResourceOperation + "/" + o.resource.ID()
}
func (o *RecreateResourceOperation) HumanID() string {
return "recreate resource: " + o.resource.HumanID()
}
func (o *RecreateResourceOperation) Status() Status {
return o.status
}
func (o *RecreateResourceOperation) Type() Type {
if o.extraPost {
return TypeExtraPostRecreateResourceOperation
}
return TypeRecreateResourceOperation
}
func (o *RecreateResourceOperation) Empty() bool {
return false
}