-
Notifications
You must be signed in to change notification settings - Fork 106
/
approve_freight_v1alpha1.go
155 lines (138 loc) · 3.6 KB
/
approve_freight_v1alpha1.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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
package api
import (
"context"
"errors"
"fmt"
"connectrpc.com/connect"
corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/runtime/schema"
"k8s.io/apimachinery/pkg/types"
"sigs.k8s.io/controller-runtime/pkg/client"
kargoapi "github.com/akuity/kargo/api/v1alpha1"
"github.com/akuity/kargo/internal/api/user"
"github.com/akuity/kargo/internal/kubeclient"
svcv1alpha1 "github.com/akuity/kargo/pkg/api/service/v1alpha1"
)
func (s *server) ApproveFreight(
ctx context.Context,
req *connect.Request[svcv1alpha1.ApproveFreightRequest],
) (*connect.Response[svcv1alpha1.ApproveFreightResponse], error) {
project := req.Msg.GetProject()
if err := validateFieldNotEmpty("project", project); err != nil {
return nil, err
}
name := req.Msg.GetName()
alias := req.Msg.GetAlias()
if (name == "" && alias == "") ||
(name != "" && alias != "") {
return nil, connect.NewError(
connect.CodeInvalidArgument,
errors.New("exactly one of name or alias should not be empty"),
)
}
stageName := req.Msg.GetStage()
if err := validateFieldNotEmpty("stage", stageName); err != nil {
return nil, err
}
if err := s.validateProjectExistsFn(ctx, project); err != nil {
return nil, err
}
freight, err := s.getFreightByNameOrAliasFn(
ctx,
s.client,
project,
name,
alias,
)
if err != nil {
return nil, fmt.Errorf("get freight: %w", err)
}
if freight == nil {
if name != "" {
err = fmt.Errorf("freight %q not found in namespace %q", name, project)
} else {
err = fmt.Errorf("freight with alias %q not found in namespace %q", alias, project)
}
return nil, connect.NewError(connect.CodeNotFound, err)
}
stage, err := s.getStageFn(
ctx,
s.client,
client.ObjectKey{
Namespace: project,
Name: stageName,
},
)
if err != nil {
return nil, fmt.Errorf("get stage: %w", err)
}
if stage == nil {
return nil, connect.NewError(
connect.CodeNotFound,
fmt.Errorf("Stage %q not found in namespace %q", stageName, project),
)
}
if err := s.authorizeFn(
ctx,
"promote",
schema.GroupVersionResource{
Group: kargoapi.GroupVersion.Group,
Version: kargoapi.GroupVersion.Version,
Resource: "stages",
},
"",
types.NamespacedName{
Namespace: project,
Name: stageName,
},
); err != nil {
return nil, err
}
newStatus := *freight.Status.DeepCopy()
if newStatus.ApprovedFor == nil {
newStatus.ApprovedFor = map[string]kargoapi.ApprovedStage{}
}
if _, ok := newStatus.ApprovedFor[stageName]; ok {
return &connect.Response[svcv1alpha1.ApproveFreightResponse]{}, nil
}
newStatus.ApprovedFor[stageName] = kargoapi.ApprovedStage{}
if err := s.patchFreightStatusFn(ctx, freight, newStatus); err != nil {
return nil, fmt.Errorf("patch status: %w", err)
}
var actor string
eventMsg := fmt.Sprintf("Freight approved for Stage %q", stageName)
if u, ok := user.InfoFromContext(ctx); ok {
actor = kargoapi.FormatEventUserActor(u)
eventMsg += fmt.Sprintf(" by %q", actor)
}
s.recorder.AnnotatedEventf(
freight,
kargoapi.NewFreightApprovedEventAnnotations(actor, freight, stageName),
corev1.EventTypeNormal,
kargoapi.EventReasonFreightApproved,
eventMsg,
)
return &connect.Response[svcv1alpha1.ApproveFreightResponse]{}, nil
}
func (s *server) patchFreightStatus(
ctx context.Context,
freight *kargoapi.Freight,
newStatus kargoapi.FreightStatus,
) error {
if err := kubeclient.PatchStatus(
ctx,
s.client,
freight,
func(status *kargoapi.FreightStatus) {
*status = newStatus
},
); err != nil {
return fmt.Errorf(
"error patching Freight %q status in namespace %q: %w",
freight.Name,
freight.Namespace,
err,
)
}
return nil
}