/
deployer.go
167 lines (139 loc) · 6.15 KB
/
deployer.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
// SPDX-FileCopyrightText: 2020 SAP SE or an SAP affiliate company and Gardener contributors.
//
// SPDX-License-Identifier: Apache-2.0
package helm
import (
"context"
"time"
"github.com/gardener/landscaper/pkg/components/registries"
"github.com/gardener/component-cli/ociclient/cache"
"sigs.k8s.io/controller-runtime/pkg/client"
lsv1alpha1 "github.com/gardener/landscaper/apis/core/v1alpha1"
helmv1alpha1 "github.com/gardener/landscaper/apis/deployer/helm/v1alpha1"
crval "github.com/gardener/landscaper/apis/deployer/utils/continuousreconcile/validation"
lserrors "github.com/gardener/landscaper/apis/errors"
"github.com/gardener/landscaper/controller-utils/pkg/logging"
cnudieutils "github.com/gardener/landscaper/pkg/components/cnudie/utils"
deployerlib "github.com/gardener/landscaper/pkg/deployer/lib"
cr "github.com/gardener/landscaper/pkg/deployer/lib/continuousreconcile"
"github.com/gardener/landscaper/pkg/deployer/lib/extension"
"github.com/gardener/landscaper/pkg/deployer/lib/timeout"
)
const (
cacheIdentifier = "helm-deployer-controller"
)
const (
TimeoutCheckpointHelmStartReconcile = "helm deployer: start reconcile"
TimeoutCheckpointHelmStartProgressing = "helm deployer: start progressing"
TimeoutCheckpointHelmStartApplyFiles = "helm deployer: start apply files"
TimeoutCheckpointHelmStartDelete = "helm deployer: start delete"
TimeoutCheckpointHelmStartDeleting = "helm deployer: start deleting"
TimeoutCheckpointHelmBeforeReadinessCheck = "helm deployer: before readiness check"
TimeoutCheckpointHelmBeforeReadingExportValues = "helm deployer: before reading export values"
TimeoutCheckpointHelmStartApplyManifests = "helm deployer: start apply manifests"
TimeoutCheckpointHelmStartCreateManifests = "helm deployer: start create manifests"
TimeoutCheckpointHelmDefaultReadinessChecks = "helm deployer: default readiness checks"
TimeoutCheckpointHelmCustomReadinessChecks = "helm deployer: custom readiness checks"
)
// NewDeployer creates a new deployer that reconciles deploy items of type helm.
func NewDeployer(lsUncachedClient, lsCachedClient, hostUncachedClient, hostCachedClient client.Client,
log logging.Logger,
config helmv1alpha1.Configuration) (deployerlib.Deployer, error) {
var sharedCache cache.Cache
if config.OCI != nil && config.OCI.Cache != nil {
var err error
sharedCache, err = cache.NewCache(log.Logr(), cnudieutils.ToOCICacheOptions(config.OCI.Cache, cacheIdentifier)...)
if err != nil {
return nil, err
}
}
registries.SetOCMLibraryMode(config.UseOCMLib)
dep := &deployer{
lsUncachedClient: lsUncachedClient,
lsCachedClient: lsCachedClient,
hostUncachedClient: hostUncachedClient,
hostCachedClient: hostCachedClient,
log: log,
config: config,
sharedCache: sharedCache,
hooks: extension.ReconcileExtensionHooks{},
}
dep.hooks.RegisterHookSetup(cr.ContinuousReconcileExtensionSetup(dep.NextReconcile))
return dep, nil
}
type deployer struct {
lsUncachedClient client.Client
lsCachedClient client.Client
hostUncachedClient client.Client
hostCachedClient client.Client
log logging.Logger
config helmv1alpha1.Configuration
sharedCache cache.Cache
hooks extension.ReconcileExtensionHooks
}
func (d *deployer) Reconcile(ctx context.Context, lsCtx *lsv1alpha1.Context, di *lsv1alpha1.DeployItem, rt *lsv1alpha1.ResolvedTarget) error {
if _, err := timeout.TimeoutExceeded(ctx, di, TimeoutCheckpointHelmStartReconcile); err != nil {
return err
}
helm, err := New(d.lsUncachedClient, d.lsCachedClient, d.hostUncachedClient, d.hostCachedClient, d.config, di, rt, lsCtx, d.sharedCache)
if err != nil {
err = lserrors.NewWrappedError(err, "Reconcile", "newRootLogger", err.Error())
return err
}
if _, err := timeout.TimeoutExceeded(ctx, di, TimeoutCheckpointHelmStartProgressing); err != nil {
return err
}
di.Status.Phase = lsv1alpha1.DeployItemPhases.Progressing
// filesForManifestDeployer and crdsForManifestDeployer are only required for the helm manifest deployer and otherwise empty
filesForManifestDeployer, crdsForManifestDeployer, values, ch, err := helm.Template(ctx)
if err != nil {
err = lserrors.NewWrappedError(err, "Reconcile", "Template", err.Error())
return err
}
exports, err := helm.constructExportsFromValues(values)
if err != nil {
err = lserrors.NewWrappedError(err, "Reconcile", "ConstructExportFromValues", err.Error())
return err
}
if _, err := timeout.TimeoutExceeded(ctx, di, TimeoutCheckpointHelmStartApplyFiles); err != nil {
return err
}
return helm.ApplyFiles(ctx, filesForManifestDeployer, crdsForManifestDeployer, exports, ch)
}
func (d *deployer) Delete(ctx context.Context, lsCtx *lsv1alpha1.Context, di *lsv1alpha1.DeployItem, rt *lsv1alpha1.ResolvedTarget) error {
if _, err := timeout.TimeoutExceeded(ctx, di, TimeoutCheckpointHelmStartDelete); err != nil {
return err
}
helm, err := New(d.lsUncachedClient, d.lsCachedClient, d.hostUncachedClient, d.hostCachedClient, d.config, di, rt, lsCtx, d.sharedCache)
if err != nil {
return err
}
if _, err := timeout.TimeoutExceeded(ctx, di, TimeoutCheckpointHelmStartDeleting); err != nil {
return err
}
return helm.DeleteFiles(ctx)
}
func (d *deployer) Abort(ctx context.Context, lsCtx *lsv1alpha1.Context, di *lsv1alpha1.DeployItem, rt *lsv1alpha1.ResolvedTarget) error {
d.log.Info("abort is not yet implemented")
return nil
}
func (d *deployer) ExtensionHooks() extension.ReconcileExtensionHooks {
return d.hooks
}
func (d *deployer) NextReconcile(ctx context.Context, last time.Time, di *lsv1alpha1.DeployItem) (*time.Time, error) {
// todo: directly parse deploy items
helm, err := New(d.lsUncachedClient, d.lsCachedClient, d.hostUncachedClient, d.hostCachedClient, d.config, di, nil, nil, d.sharedCache)
if err != nil {
return nil, err
}
if crval.ContinuousReconcileSpecIsEmpty(helm.ProviderConfiguration.ContinuousReconcile) {
// no continuous reconciliation configured
return nil, nil
}
schedule, err := cr.Schedule(helm.ProviderConfiguration.ContinuousReconcile)
if err != nil {
return nil, err
}
next := schedule.Next(last)
return &next, nil
}