/
create.go
76 lines (65 loc) · 2.34 KB
/
create.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
package tenantclients
import (
"context"
"github.com/aws/amazon-vpc-cni-k8s/pkg/apis/crd/v1alpha1"
"github.com/giantswarm/k8sclient/v7/pkg/k8sclient"
"github.com/giantswarm/microerror"
"github.com/giantswarm/tenantcluster/v6/pkg/tenantcluster"
apiextensionsv1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1"
"k8s.io/client-go/rest"
"github.com/giantswarm/aws-operator/v15/service/controller/controllercontext"
"github.com/giantswarm/aws-operator/v15/service/controller/key"
)
func (r *Resource) EnsureCreated(ctx context.Context, obj interface{}) error {
cr, err := r.toClusterFunc(ctx, obj)
if err != nil {
return microerror.Mask(err)
}
cc, err := controllercontext.FromContext(ctx)
if err != nil {
return microerror.Mask(err)
}
var restConfig *rest.Config
{
restConfig, err = r.tenant.NewRestConfig(ctx, key.ClusterID(&cr), key.ClusterAPIEndpoint(cr))
if tenantcluster.IsTimeout(err) {
r.logger.Debugf(ctx, "timeout fetching certificates")
r.logger.Debugf(ctx, "canceling resource")
return nil
} else if err != nil {
return microerror.Mask(err)
}
}
var k8sClient k8sclient.Interface
{
c := k8sclient.ClientsConfig{
Logger: r.logger,
RestConfig: rest.CopyConfig(restConfig),
SchemeBuilder: k8sclient.SchemeBuilder{
// The Tenant Clients are used to connect to manage ENIConfig CRs within
// the Tenant Cluster in order to properly configure the AWS CNI.
// Therefore it is important to add its specific scheme builders.
v1alpha1.AddToScheme,
// The Tenant Clients are used to delete the ENIConfig CRD after the migration to Cilium.
apiextensionsv1.AddToScheme,
},
}
k8sClient, err = k8sclient.NewClients(c)
if err != nil {
// On any error we want to handle the situation gracefully in order
// to not block the whole reconciliation. Our former approach of
// matching against specific errors was extremely brittle because
// every now and then new errors popped up which we never knew or
// handled before. This is extremely painful after fact in a
// immutable infrastructure because it is super hard to fix once it
// breaks after it is released.
r.logger.Debugf(ctx, "tenant API not available yet", "stack", microerror.JSON(err))
r.logger.Debugf(ctx, "canceling resource")
return nil
}
}
{
cc.Client.TenantCluster.K8s = k8sClient
}
return nil
}