/
update_resource_operation.go
84 lines (67 loc) · 1.97 KB
/
update_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
package opertn
import (
"context"
"fmt"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"github.com/werf/nelm/pkg/kubeclnt"
"github.com/werf/nelm/pkg/resrc"
"github.com/werf/nelm/pkg/resrcid"
)
var _ Operation = (*UpdateResourceOperation)(nil)
const TypeUpdateResourceOperation = "update"
const TypeExtraPostUpdateResourceOperation = "extra-post-update"
func NewUpdateResourceOperation(
resource *resrcid.ResourceID,
unstruct *unstructured.Unstructured,
kubeClient kubeclnt.KubeClienter,
opts UpdateResourceOperationOptions,
) (*UpdateResourceOperation, error) {
return &UpdateResourceOperation{
resource: resource,
unstruct: unstruct,
kubeClient: kubeClient,
manageableBy: opts.ManageableBy,
extraPost: opts.ExtraPost,
}, nil
}
type UpdateResourceOperationOptions struct {
ManageableBy resrc.ManageableBy
ExtraPost bool
}
type UpdateResourceOperation struct {
resource *resrcid.ResourceID
unstruct *unstructured.Unstructured
kubeClient kubeclnt.KubeClienter
manageableBy resrc.ManageableBy
extraPost bool
status Status
}
func (o *UpdateResourceOperation) Execute(ctx context.Context) error {
if _, err := o.kubeClient.Apply(ctx, o.resource, o.unstruct, kubeclnt.KubeClientApplyOptions{}); err != nil {
o.status = StatusFailed
return fmt.Errorf("error applying resource: %w", err)
}
o.status = StatusCompleted
return nil
}
func (o *UpdateResourceOperation) ID() string {
if o.extraPost {
return TypeExtraPostUpdateResourceOperation + "/" + o.resource.ID()
}
return TypeUpdateResourceOperation + "/" + o.resource.ID()
}
func (o *UpdateResourceOperation) HumanID() string {
return "update resource: " + o.resource.HumanID()
}
func (o *UpdateResourceOperation) Status() Status {
return o.status
}
func (o *UpdateResourceOperation) Type() Type {
if o.extraPost {
return TypeExtraPostUpdateResourceOperation
}
return TypeUpdateResourceOperation
}
func (o *UpdateResourceOperation) Empty() bool {
return false
}