-
Notifications
You must be signed in to change notification settings - Fork 20
/
providerconfig.go
73 lines (58 loc) · 1.81 KB
/
providerconfig.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
package resolvers
import (
"context"
corev1 "k8s.io/api/core/v1"
kextv1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1"
"k8s.io/apimachinery/pkg/runtime/schema"
"k8s.io/apimachinery/pkg/types"
"github.com/99designs/gqlgen/graphql"
"github.com/pkg/errors"
"github.com/upbound/xgql/internal/auth"
"github.com/upbound/xgql/internal/graph/model"
)
type providerConfig struct {
clients ClientCache
}
func (r *providerConfig) Events(ctx context.Context, obj *model.ProviderConfig) (*model.EventConnection, error) {
e := &events{clients: r.clients}
return e.Resolve(ctx, &corev1.ObjectReference{
APIVersion: obj.APIVersion,
Kind: obj.Kind,
Name: obj.Metadata.Name,
UID: types.UID(obj.Metadata.UID),
})
}
func (r *providerConfig) Definition(ctx context.Context, obj *model.ProviderConfig) (model.ProviderConfigDefinition, 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
}
in := &kextv1.CustomResourceDefinitionList{}
if err := c.List(ctx, in); err != nil {
graphql.AddError(ctx, errors.Wrap(err, errListCRDs))
return nil, nil
}
gv, err := schema.ParseGroupVersion(obj.APIVersion)
if err != nil {
// This should be pretty much impossible - the API server should not
// return resources with malformed API versions.
graphql.AddError(ctx, errors.Wrap(err, errMalformedAPIVersion))
return nil, nil
}
for i := range in.Items {
crd := in.Items[i] // So we don't take the address of a range variable.
if crd.Spec.Group != gv.Group {
continue
}
if crd.Spec.Names.Kind != obj.Kind {
continue
}
out := model.GetCustomResourceDefinition(&crd)
return &out, nil
}
return nil, nil
}