-
Notifications
You must be signed in to change notification settings - Fork 20
/
mutation.go
177 lines (152 loc) · 5.5 KB
/
mutation.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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
package resolvers
import (
"context"
"github.com/99designs/gqlgen/graphql"
"github.com/pkg/errors"
kerrors "k8s.io/apimachinery/pkg/api/errors"
v1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"k8s.io/apimachinery/pkg/util/json"
"k8s.io/client-go/util/retry"
"github.com/crossplane/crossplane-runtime/pkg/fieldpath"
"github.com/crossplane/crossplane-runtime/pkg/resource"
"github.com/upbound/xgql/internal/auth"
"github.com/upbound/xgql/internal/graph/model"
)
const (
errCreateResource = "cannot create Kubernetes resource"
errUpdateResource = "cannot update Kubernetes resource"
errDeleteResource = "cannot delete Kubernetes resource"
errUnmarshalUnstructured = "cannot unmarshal input unstructured JSON"
errFmtUnmarshalPatch = "cannot unmarshal unstructured patch JSON at index %d"
errFmtPatch = "cannot apply patch at index %d"
)
// IsRetriable indicates that an error may succeed if retried.
func IsRetriable(err error) bool { //nolint:gocyclo // It's just a big old switch.
switch {
case kerrors.IsTimeout(err):
return true
case kerrors.IsServerTimeout(err):
return true
case kerrors.IsInternalError(err):
return true
case kerrors.IsTooManyRequests(err):
return true
case kerrors.IsUnexpectedServerError(err):
return true
case kerrors.ReasonForError(err) == v1.StatusReasonUnknown:
// This error doesn't seem to be from Kubernetes.
return true
default:
return false
}
}
type mutation struct {
clients ClientCache
}
func (r *mutation) CreateKubernetesResource(ctx context.Context, input model.CreateKubernetesResourceInput) (*model.CreateKubernetesResourcePayload, error) {
ctx, cancel := context.WithTimeout(ctx, timeout)
defer cancel()
creds, _ := auth.FromContext(ctx)
c, err := r.clients.Get(creds)
if err != nil {
graphql.AddError(ctx, errors.Wrap(err, errGetClient))
return nil, nil
}
u := &unstructured.Unstructured{}
if err := json.Unmarshal(input.Unstructured, u); err != nil {
graphql.AddError(ctx, errors.Wrap(err, errUnmarshalUnstructured))
return nil, nil
}
pv := fieldpath.Pave(u.Object)
for i, p := range input.Patches {
var v interface{}
if err := json.Unmarshal(p.Unstructured, &v); err != nil {
graphql.AddError(ctx, errors.Wrapf(err, errFmtUnmarshalPatch, i))
return nil, nil
}
if err := pv.SetValue(p.FieldPath, v); err != nil {
graphql.AddError(ctx, errors.Wrapf(err, errFmtPatch, i))
return nil, nil
}
}
if err := retry.OnError(retry.DefaultBackoff, IsRetriable, func() error { return c.Create(ctx, u) }); err != nil {
graphql.AddError(ctx, errors.Wrap(err, errCreateResource))
return nil, nil
}
kr, err := model.GetKubernetesResource(u)
if err != nil {
graphql.AddError(ctx, errors.Wrap(err, errModelResource))
return nil, nil
}
return &model.CreateKubernetesResourcePayload{Resource: kr}, nil
}
func (r *mutation) UpdateKubernetesResource(ctx context.Context, id model.ReferenceID, input model.UpdateKubernetesResourceInput) (*model.UpdateKubernetesResourcePayload, error) {
ctx, cancel := context.WithTimeout(ctx, timeout)
defer cancel()
creds, _ := auth.FromContext(ctx)
c, err := r.clients.Get(creds)
if err != nil {
graphql.AddError(ctx, errors.Wrap(err, errGetClient))
return nil, nil
}
u := &unstructured.Unstructured{}
if err := json.Unmarshal(input.Unstructured, u); err != nil {
graphql.AddError(ctx, errors.Wrap(err, errUnmarshalUnstructured))
return nil, nil
}
pv := fieldpath.Pave(u.Object)
for i, p := range input.Patches {
var v interface{}
if err := json.Unmarshal(p.Unstructured, &v); err != nil {
graphql.AddError(ctx, errors.Wrapf(err, errFmtUnmarshalPatch, i))
return nil, nil
}
if err := pv.SetValue(p.FieldPath, v); err != nil {
graphql.AddError(ctx, errors.Wrapf(err, errFmtPatch, i))
return nil, nil
}
}
// We expect the caller to read, modify, then update so the supplied
// unstructured JSON _should_ already have the correct GVK, namespace, and
// name. Nonetheless we inject those within the supplied ID just in case.
u.SetAPIVersion(id.APIVersion)
u.SetKind(id.Kind)
u.SetNamespace(id.Namespace)
u.SetName(id.Name)
if err := retry.OnError(retry.DefaultBackoff, IsRetriable, func() error { return c.Update(ctx, u) }); err != nil {
graphql.AddError(ctx, errors.Wrap(err, errUpdateResource))
return nil, nil
}
kr, err := model.GetKubernetesResource(u)
if err != nil {
graphql.AddError(ctx, errors.Wrap(err, errModelResource))
return nil, nil
}
return &model.UpdateKubernetesResourcePayload{Resource: kr}, nil
}
func (r *mutation) DeleteKubernetesResource(ctx context.Context, id model.ReferenceID) (*model.DeleteKubernetesResourcePayload, error) {
ctx, cancel := context.WithTimeout(ctx, timeout)
defer cancel()
creds, _ := auth.FromContext(ctx)
c, err := r.clients.Get(creds)
if err != nil {
graphql.AddError(ctx, errors.Wrap(err, errGetClient))
return nil, nil
}
u := &unstructured.Unstructured{}
u.SetAPIVersion(id.APIVersion)
u.SetKind(id.Kind)
u.SetNamespace(id.Namespace)
u.SetName(id.Name)
if err := retry.OnError(retry.DefaultBackoff, IsRetriable, func() error { return c.Delete(ctx, u) }); resource.IgnoreNotFound(err) != nil {
graphql.AddError(ctx, errors.Wrap(err, errDeleteResource))
return nil, nil //nolint:nilerr // IgnoreNotFound appears to trigger this linter.
}
kr, err := model.GetKubernetesResource(u)
if err != nil {
graphql.AddError(ctx, errors.Wrap(err, errModelResource))
return nil, nil
}
return &model.DeleteKubernetesResourcePayload{Resource: kr}, nil
}