Skip to content
This repository was archived by the owner on Jun 13, 2023. It is now read-only.

Commit 013cd85

Browse files
feat: pointer of installinfo and postrun hooks (#192)
1 parent b84c96f commit 013cd85

17 files changed

+250
-164
lines changed

controllers/manifest_controller.go

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ type RequeueIntervals struct {
6262
}
6363

6464
type OperationRequest struct {
65-
Info types.InstallInfo
65+
Info *types.InstallInfo
6666
Mode internalTypes.Mode
6767
ResponseChan internalTypes.ResponseChan
6868
}
@@ -212,8 +212,11 @@ func (r *ManifestReconciler) HandleReadyState(ctx context.Context, logger logr.L
212212
}
213213

214214
for _, deployInfo := range deployInfos {
215-
ready, err := manifest.ConsistencyCheck(logger, deployInfo, []types.ObjectTransform{},
216-
r.CacheManager.GetRendererCache())
215+
ready, err := manifest.ConsistencyCheck(manifest.OperationOptions{
216+
Logger: logger,
217+
InstallInfo: deployInfo,
218+
Cache: r.CacheManager.GetRendererCache(),
219+
})
217220

218221
// prepare chart response object
219222
chartResponse := &internalTypes.InstallResponse{
@@ -266,20 +269,24 @@ func (r *ManifestReconciler) updateManifestStatus(ctx context.Context, manifestO
266269
return r.Status().Update(ctx, manifestObj.SetObservedGeneration())
267270
}
268271

269-
func (r *ManifestReconciler) HandleCharts(deployInfo types.InstallInfo, mode internalTypes.Mode,
272+
func (r *ManifestReconciler) HandleCharts(deployInfo *types.InstallInfo, mode internalTypes.Mode,
270273
logger logr.Logger,
271274
) *internalTypes.InstallResponse {
272275
// evaluate create or delete chart
273276
create := mode == internalTypes.CreateMode
274277

275278
var ready bool
276279
var err error
280+
281+
options := manifest.OperationOptions{
282+
Logger: logger,
283+
InstallInfo: deployInfo,
284+
Cache: r.CacheManager.GetRendererCache(),
285+
}
277286
if create {
278-
ready, err = manifest.InstallChart(logger, deployInfo, []types.ObjectTransform{},
279-
r.CacheManager.GetRendererCache())
287+
ready, err = manifest.InstallChart(options)
280288
} else {
281-
ready, err = manifest.UninstallChart(logger, deployInfo, []types.ObjectTransform{},
282-
r.CacheManager.GetRendererCache())
289+
ready, err = manifest.UninstallChart(options)
283290
}
284291

285292
return &internalTypes.InstallResponse{

controllers/workers.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ func NewManifestWorkers(logger logr.Logger, workersConcurrentManifests int) *Man
3333
}
3434

3535
func (mw *ManifestWorkerPool) StartWorkers(ctx context.Context, jobChan <-chan OperationRequest,
36-
handlerFn func(manifestTypes.InstallInfo, internalTypes.Mode, logr.Logger) *internalTypes.InstallResponse,
36+
handlerFn func(*manifestTypes.InstallInfo, internalTypes.Mode, logr.Logger) *internalTypes.InstallResponse,
3737
) {
3838
for worker := 1; worker <= mw.GetWorkerPoolSize(); worker++ {
3939
go func(ctx context.Context, workerId int, deployJob <-chan OperationRequest) {

internal/pkg/custom/verify.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,13 +23,13 @@ type Resource struct {
2323
}
2424

2525
func (r *Resource) DefaultFn(context.Context, *unstructured.Unstructured, logr.Logger,
26-
types.ClusterInfo,
26+
*types.ClusterInfo,
2727
) (bool, error) {
2828
return true, nil
2929
}
3030

3131
func (r *Resource) CheckFn(ctx context.Context, manifestObj *unstructured.Unstructured, logger logr.Logger,
32-
clusterInfo types.ClusterInfo,
32+
clusterInfo *types.ClusterInfo,
3333
) (bool, error) {
3434
// if manifest resource is in deleting state - validate check
3535
if !manifestObj.GetDeletionTimestamp().IsZero() {

internal/pkg/prepare/deploy.go

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ const configReadError = "reading install %s resulted in an error for " + v1alpha
3131
// each representing an installation artifact.
3232
func GetInstallInfos(ctx context.Context, manifestObj *v1alpha1.Manifest, defaultClusterInfo types.ClusterInfo,
3333
flags internalTypes.ReconcileFlagConfig, processorCache types.RendererCache,
34-
) ([]types.InstallInfo, error) {
34+
) ([]*types.InstallInfo, error) {
3535
namespacedName := client.ObjectKeyFromObject(manifestObj)
3636

3737
// extract config
@@ -80,8 +80,8 @@ func GetInstallInfos(ctx context.Context, manifestObj *v1alpha1.Manifest, defaul
8080

8181
// parse installs
8282
baseDeployInfo := types.InstallInfo{
83-
ClusterInfo: clusterInfo,
84-
ResourceInfo: types.ResourceInfo{
83+
ClusterInfo: &clusterInfo,
84+
ResourceInfo: &types.ResourceInfo{
8585
Crds: crds,
8686
BaseResource: &unstructured.Unstructured{Object: manifestObjMetadata},
8787
CustomResources: []*unstructured.Unstructured{},
@@ -101,7 +101,7 @@ func GetInstallInfos(ctx context.Context, manifestObj *v1alpha1.Manifest, defaul
101101
baseDeployInfo.CustomResources = append(baseDeployInfo.CustomResources, &manifestObj.Spec.Resource)
102102
}
103103

104-
return parseInstallations(manifestObj, flags.Codec, configs, baseDeployInfo, flags.InsecureRegistry)
104+
return parseInstallations(manifestObj, flags.Codec, configs, &baseDeployInfo, flags.InsecureRegistry)
105105
}
106106

107107
func getDestinationConfigAndClient(ctx context.Context, defaultClusterInfo types.ClusterInfo,
@@ -162,10 +162,10 @@ func parseInstallConfigs(decodedConfig interface{}) ([]interface{}, error) {
162162
}
163163

164164
func parseInstallations(manifestObj *v1alpha1.Manifest, codec *types.Codec,
165-
configs []interface{}, baseDeployInfo types.InstallInfo, insecureRegistry bool,
166-
) ([]types.InstallInfo, error) {
165+
configs []interface{}, baseDeployInfo *types.InstallInfo, insecureRegistry bool,
166+
) ([]*types.InstallInfo, error) {
167167
namespacedName := client.ObjectKeyFromObject(manifestObj)
168-
deployInfos := make([]types.InstallInfo, 0)
168+
deployInfos := make([]*types.InstallInfo, 0)
169169

170170
for _, install := range manifestObj.Spec.Installs {
171171
deployInfo := baseDeployInfo

pkg/applier/ssa.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ func NewSSAApplier(clients *client.SingletonClients, logger logr.Logger) *SetApp
3737
}
3838
}
3939

40-
func (s *SetApplier) Apply(deployInfo types.InstallInfo, objects *types.ManifestResources,
40+
func (s *SetApplier) Apply(deployInfo *types.InstallInfo, objects *types.ManifestResources,
4141
namespace string,
4242
) (bool, error) {
4343
// Populate the namespace on any namespace-scoped objects
@@ -67,7 +67,7 @@ func (s *SetApplier) Apply(deployInfo types.InstallInfo, objects *types.Manifest
6767
return expectedLength == len(results), nil
6868
}
6969

70-
func (s *SetApplier) Delete(deployInfo types.InstallInfo, objects *types.ManifestResources,
70+
func (s *SetApplier) Delete(deployInfo *types.InstallInfo, objects *types.ManifestResources,
7171
namespace string,
7272
) (bool, error) {
7373
// Populate the namespace on any namespace-scoped objects
@@ -140,7 +140,7 @@ func (s *SetApplier) adjustNs(objects *types.ManifestResources, namespace string
140140
}
141141

142142
func (s *SetApplier) execute(
143-
deployInfo types.InstallInfo,
143+
deployInfo *types.InstallInfo,
144144
objects []*unstructured.Unstructured,
145145
) ([]*unstructured.Unstructured, error) {
146146
appliedObjects := make([]*unstructured.Unstructured, 0)

pkg/client/factory.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ type SingletonClients struct {
7676
unstructuredRESTClientCache map[string]resource.RESTClient
7777
}
7878

79-
func NewSingletonClients(info types.ClusterInfo, logger logr.Logger) (*SingletonClients, error) {
79+
func NewSingletonClients(info *types.ClusterInfo, logger logr.Logger) (*SingletonClients, error) {
8080
if err := setKubernetesDefaults(info.Config); err != nil {
8181
return nil, err
8282
}

pkg/declarative/options.go

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,3 +53,20 @@ func WithFinalizer(finalizer string) ReconcilerOption {
5353
return allOptions
5454
}
5555
}
56+
57+
// WithPostRun adds run hooks after installation/uninstallation or consistency checks.
58+
func WithPostRun(runs ...types.PostRun) ReconcilerOption {
59+
return func(allOptions manifestOptions) manifestOptions {
60+
allOptions.postRuns = append(allOptions.postRuns, runs...)
61+
return allOptions
62+
}
63+
}
64+
65+
func With(option ...ReconcilerOption) ReconcilerOption {
66+
return func(allOptions manifestOptions) manifestOptions {
67+
for i := range option {
68+
allOptions = option[i](allOptions)
69+
}
70+
return allOptions
71+
}
72+
}

pkg/declarative/reconciler.go

Lines changed: 34 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ type manifestOptions struct {
4242
verify bool
4343
resourceLabels map[string]string
4444
objectTransforms []types.ObjectTransform
45+
postRuns []types.PostRun
4546
manifestResolver types.ManifestResolver
4647
finalizer string
4748
}
@@ -188,14 +189,19 @@ func (r *ManifestReconciler) HandleProcessingState(ctx context.Context, objectIn
188189
if err != nil {
189190
return err
190191
}
191-
192-
ready, err := manifest.InstallChart(logger, installInfo, r.options.objectTransforms,
193-
r.cacheManager.GetRendererCache())
192+
ready, err := manifest.InstallChart(manifest.OperationOptions{
193+
Logger: logger,
194+
InstallInfo: installInfo,
195+
ResourceTransforms: r.options.objectTransforms,
196+
PostRuns: r.options.postRuns,
197+
Cache: r.cacheManager.GetRendererCache(),
198+
})
194199
if err != nil {
195200
logger.Error(nil, fmt.Sprintf("error while installing resource %s %s",
196201
client.ObjectKeyFromObject(objectInstance), err.Error()))
197202
return r.setStatusForObjectInstance(ctx, objectInstance, status.WithState(types.StateError))
198203
}
204+
199205
if ready {
200206
return r.setStatusForObjectInstance(ctx, objectInstance, status.WithState(types.StateReady))
201207
}
@@ -236,8 +242,13 @@ func (r *ManifestReconciler) HandleDeletingState(ctx context.Context, objectInst
236242
return err
237243
}
238244

239-
readyToBeDeleted, err := manifest.UninstallChart(logger, installInfo, r.options.objectTransforms,
240-
r.cacheManager.GetRendererCache())
245+
readyToBeDeleted, err := manifest.UninstallChart(manifest.OperationOptions{
246+
Logger: logger,
247+
InstallInfo: installInfo,
248+
ResourceTransforms: r.options.objectTransforms,
249+
PostRuns: r.options.postRuns,
250+
Cache: r.cacheManager.GetRendererCache(),
251+
})
241252
if err != nil {
242253
logger.Error(err, fmt.Sprintf("error while deleting resource %s", client.ObjectKeyFromObject(objectInstance)))
243254
status.State = types.StateError
@@ -276,8 +287,13 @@ func (r *ManifestReconciler) HandleReadyState(ctx context.Context, objectInstanc
276287
}
277288

278289
// verify installed resources
279-
ready, err := manifest.ConsistencyCheck(logger, installInfo, r.options.objectTransforms,
280-
r.cacheManager.GetRendererCache())
290+
ready, err := manifest.ConsistencyCheck(manifest.OperationOptions{
291+
Logger: logger,
292+
InstallInfo: installInfo,
293+
ResourceTransforms: r.options.objectTransforms,
294+
PostRuns: r.options.postRuns,
295+
Cache: r.cacheManager.GetRendererCache(),
296+
})
281297

282298
// update only if resources not ready OR an error occurred during chart verification
283299
if err != nil {
@@ -292,37 +308,37 @@ func (r *ManifestReconciler) HandleReadyState(ctx context.Context, objectInstanc
292308

293309
func (r *ManifestReconciler) prepareInstallInfo(ctx context.Context, objectInstance types.BaseCustomObject,
294310
installSpec types.InstallationSpec, releaseName string,
295-
) (types.InstallInfo, error) {
296-
unstructuredObj := &unstructured.Unstructured{}
311+
) (*types.InstallInfo, error) {
312+
obj := &unstructured.Unstructured{}
297313
var err error
298314
switch typedObject := objectInstance.(type) {
299315
case types.CustomObject:
300-
unstructuredObj.Object, err = runtime.DefaultUnstructuredConverter.ToUnstructured(typedObject)
316+
obj.Object, err = runtime.DefaultUnstructuredConverter.ToUnstructured(typedObject)
301317
if err != nil {
302-
return types.InstallInfo{}, err
318+
return &types.InstallInfo{}, err
303319
}
304320
case *unstructured.Unstructured:
305-
unstructuredObj = typedObject
321+
obj = typedObject
306322
default:
307-
return types.InstallInfo{}, getTypeError(client.ObjectKeyFromObject(objectInstance).String())
323+
return &types.InstallInfo{}, getTypeError(client.ObjectKeyFromObject(objectInstance).String())
308324
}
309325

310-
return types.InstallInfo{
326+
return &types.InstallInfo{
311327
Ctx: ctx,
312328
ChartInfo: &types.ChartInfo{
313329
ChartPath: installSpec.ChartPath,
314330
ReleaseName: releaseName,
315331
Flags: installSpec.ChartFlags,
316332
},
317-
ClusterInfo: types.ClusterInfo{
333+
ClusterInfo: &types.ClusterInfo{
318334
// destination cluster rest config
319335
Config: r.mgr.GetConfig(),
320336
// destination cluster rest client
321337
Client: r.mgr.GetClient(),
322338
},
323-
ResourceInfo: types.ResourceInfo{
339+
ResourceInfo: &types.ResourceInfo{
324340
// base operator resource to be passed for custom checks
325-
BaseResource: unstructuredObj,
341+
BaseResource: obj,
326342
},
327343
CheckReadyStates: r.options.verify,
328344
}, nil
@@ -334,6 +350,7 @@ func (r *ManifestReconciler) applyOptions(opts ...ReconcilerOption) error {
334350
verify: false,
335351
resourceLabels: make(map[string]string, 0),
336352
objectTransforms: []types.ObjectTransform{},
353+
postRuns: []types.PostRun{},
337354
}
338355

339356
for _, opt := range opts {

0 commit comments

Comments
 (0)