-
Notifications
You must be signed in to change notification settings - Fork 0
/
fn.go
78 lines (64 loc) · 2.34 KB
/
fn.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
package main
import (
"context"
"strings"
"github.com/crossplane/crossplane-runtime/pkg/errors"
fnv1beta1 "github.com/crossplane/function-sdk-go/proto/v1beta1"
"github.com/crossplane/function-sdk-go/response"
"github.com/giantswarm/xfnlib/pkg/composite"
"github.com/giantswarm/crossplane-fn-describe-nodegroups/pkg/input/v1beta1"
)
const composedName = "crossplane-fn-describe-nodegroups"
// RunFunction Execute the desired reconcilliation state, creating any required resources
func (f *Function) RunFunction(_ context.Context, req *fnv1beta1.RunFunctionRequest) (rsp *fnv1beta1.RunFunctionResponse, err error) {
f.log.Info("preparing function", composedName, req.GetMeta().GetTag())
rsp = response.To(req, response.DefaultTTL)
var (
ac XrConfig = XrConfig{}
input v1beta1.Input
)
if ac.composed, err = composite.New(req, &input, &ac.composite); err != nil {
response.Fatal(rsp, errors.Wrap(err, "error setting up function "+composedName))
return rsp, nil
}
if input.Spec == nil {
response.Fatal(rsp, &composite.MissingSpec{})
return rsp, nil
}
if _, ok := ac.composed.ObservedComposed[input.Spec.ClusterRef]; !ok {
return rsp, nil
}
ac.cluster = &ac.composite.Spec.ClusterName
ac.namespace = &ac.composite.Spec.ClaimRef.Namespace
ac.region = &ac.composite.Spec.Region
ac.providerConfigRef = &ac.composite.Spec.CloudProviderConfigRef
ac.labels = ac.composite.Metadata.Labels
if ac.labels == nil {
ac.labels = make(map[string]string)
}
for k, v := range ac.composite.Spec.KubernetesAdditionalLabels {
ac.labels[k] = v
}
ac.labels["cluster.x-k8s.io/cluster-name"] = *ac.cluster
ac.labels["giantswarm.io/cluster"] = *ac.cluster
var provider string = ac.composite.Spec.CompositionSelector.MatchLabels.Provider
{
switch strings.ToLower(provider) {
case "aws":
f.log.Info("discovered aws provider", composedName, req.GetMeta().GetTag())
if err = f.CreateAWSNodegroupSpec(&ac); err != nil {
response.Fatal(rsp, errors.Wrapf(err, "cannot create composed resources from %T", req))
return rsp, nil
}
case "azure":
f.log.Info("Azure provider is not yet implemented")
case "gcp":
f.log.Info("GCP provider is not yet implemented")
}
}
if err = ac.composed.ToResponse(rsp); err != nil {
response.Fatal(rsp, errors.Wrapf(err, "cannot convert composition to response %T", rsp))
return
}
return rsp, nil
}