This repository has been archived by the owner on Mar 16, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 102
/
defaults.go
63 lines (53 loc) · 2.02 KB
/
defaults.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
package defaults
import (
"github.com/acorn-io/baaah/pkg/router"
internalv1 "github.com/acorn-io/runtime/pkg/apis/internal.acorn.io/v1"
"github.com/acorn-io/runtime/pkg/condition"
"github.com/acorn-io/runtime/pkg/config"
)
// Calculate is a handler that sets the defaults for an AppInstance to its status if
// and only if its generation is different from its observedGeneration.
//
// This is necessary because querying for defaults will result in all running
// AppInstances using that default to redeploy when a default changes. By
// calculating the defaults only when the generation changes, we can ensure that
// updated defaults are only applied when an AppInstance is updated directly.
func Calculate(req router.Request, resp router.Response) (err error) {
appInstance := req.Object.(*internalv1.AppInstance)
status := condition.Setter(appInstance, resp, internalv1.AppInstanceConditionDefaults)
defer func() {
if err == nil {
status.Success()
} else {
status.Error(err)
// Disable prune because we are short-circuiting the handlers and don't want objects getting cleaned up accidentally.
resp.DisablePrune()
// Don't return the error so the other conditions don't get the same information.
err = nil
}
}()
// addVolumeClassDefaults should run everytime as the function itself will not overwrite any existing
// defaults. Effectively, this means that volume defaults only get set if they have not been set before.
if err = addVolumeClassDefaults(req.Ctx, req.Client, appInstance); err != nil {
return err
}
if appInstance.Generation != appInstance.Status.ObservedGeneration {
if err = calculate(req, appInstance); err != nil {
return err
}
}
return nil
}
func calculate(req router.Request, appInstance *internalv1.AppInstance) error {
cfg, err := config.Get(req.Ctx, req.Client)
if err != nil {
return err
}
if err = AddDefaultRegion(req.Ctx, req.Client, appInstance); err != nil {
return err
}
if err = addDefaultMemory(req, cfg, appInstance); err != nil {
return err
}
return nil
}