/
create.go
117 lines (97 loc) · 3.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
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
package status
import (
"context"
"github.com/giantswarm/apiextensions-application/api/v1alpha1"
"github.com/giantswarm/app/v7/pkg/key"
"github.com/giantswarm/errors/tenant"
"github.com/giantswarm/microerror"
apierrors "k8s.io/apimachinery/pkg/api/errors"
"k8s.io/apimachinery/pkg/types"
"github.com/giantswarm/app-operator/v6/service/controller/app/controllercontext"
)
func (r *Resource) EnsureCreated(ctx context.Context, obj interface{}) error {
cr, err := key.ToApp(obj)
if err != nil {
return microerror.Mask(err)
}
cc, err := controllercontext.FromContext(ctx)
if err != nil {
return microerror.Mask(err)
}
if cc.Status.ClusterStatus.IsDeleting {
r.logger.Debugf(ctx, "namespace %#q is being deleted, no need to reconcile resource", cr.Namespace)
r.logger.Debugf(ctx, "canceling resource")
return nil
}
var chart v1alpha1.Chart
var desiredStatus v1alpha1.AppStatus
if cc.Status.ChartStatus.Status != "" {
desiredStatus = v1alpha1.AppStatus{
Release: v1alpha1.AppStatusRelease{
Reason: cc.Status.ChartStatus.Reason,
Status: cc.Status.ChartStatus.Status,
},
}
} else {
if cc.Status.ClusterStatus.IsUnavailable {
r.logger.Debugf(ctx, "workload cluster is unavailable")
r.logger.Debugf(ctx, "canceling resource")
return nil
}
r.logger.Debugf(ctx, "finding status for chart %#q in namespace %#q", cr.Name, r.chartNamespace)
chartName := key.ChartName(cr, r.workloadClusterID)
err = cc.Clients.K8s.CtrlClient().Get(
ctx,
types.NamespacedName{Name: chartName, Namespace: r.chartNamespace},
&chart,
)
if apierrors.IsNotFound(err) {
r.logger.Debugf(ctx, "did not find chart %#q in namespace %#q", cr.Name, r.chartNamespace)
r.logger.Debugf(ctx, "canceling resource")
return nil
} else if tenant.IsAPINotAvailable(err) {
// We should not hammer tenant API if it is not available, the workload cluster
// might be initializing. We will retry on next reconciliation loop.
r.logger.Debugf(ctx, "workload cluster is not available.")
r.logger.Debugf(ctx, "canceling resource")
return nil
} else if err != nil {
return microerror.Mask(err)
}
r.logger.Debugf(ctx, "found status for chart %#q in namespace %#q", cr.Name, r.chartNamespace)
chartStatus := key.ChartStatus(chart)
desiredStatus = v1alpha1.AppStatus{
AppVersion: chartStatus.AppVersion,
Release: v1alpha1.AppStatusRelease{
Reason: chartStatus.Reason,
Status: chartStatus.Release.Status,
},
Version: chartStatus.Version,
}
if chartStatus.Release.LastDeployed != nil {
desiredStatus.Release.LastDeployed = *chartStatus.Release.LastDeployed
}
}
if !equals(desiredStatus, key.AppStatus(cr)) {
r.logger.Debugf(ctx, "setting status for app %#q in namespace %#q", cr.Name, cr.Namespace)
// Get app CR again to ensure the resource version is correct.
var currentCR v1alpha1.App
err = r.ctrlClient.Get(
ctx,
types.NamespacedName{Name: cr.Name, Namespace: cr.Namespace},
¤tCR,
)
if err != nil {
return microerror.Mask(err)
}
currentCR.Status = desiredStatus
err = r.ctrlClient.Status().Update(ctx, ¤tCR)
if err != nil {
return microerror.Mask(err)
}
r.logger.Debugf(ctx, "status set for app %#q in namespace %#q", cr.Name, cr.Namespace)
} else {
r.logger.Debugf(ctx, "status already set for app %#q in namespace %#q", cr.Name, cr.Namespace)
}
return nil
}