-
Notifications
You must be signed in to change notification settings - Fork 9
/
op_ensureruntimeconfig.go
156 lines (131 loc) · 5.44 KB
/
op_ensureruntimeconfig.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
// Copyright 2022 Namespace Labs Inc; All rights reserved.
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
package kubeops
import (
"context"
"encoding/json"
applycorev1 "k8s.io/client-go/applyconfigurations/core/v1"
"namespacelabs.dev/foundation/framework/kubernetes/kubedef"
"namespacelabs.dev/foundation/framework/rpcerrors/multierr"
"namespacelabs.dev/foundation/internal/fnerrors"
"namespacelabs.dev/foundation/internal/planning/deploy"
"namespacelabs.dev/foundation/schema"
"namespacelabs.dev/foundation/std/execution"
"namespacelabs.dev/foundation/std/tasks"
)
const runtimeConfigVersion = 0
func registerEnsureRuntimeConfig() {
execution.RegisterFuncs(execution.Funcs[*kubedef.OpEnsureRuntimeConfig]{
Handle: func(ctx context.Context, inv *schema.SerializedInvocation, ensure *kubedef.OpEnsureRuntimeConfig) (*execution.HandleResult, error) {
action := tasks.Action("kubernetes.ensure-runtime-config").
Scope(ensure.Deployable.GetPackageRef().AsPackageName()).
Arg("deployable", ensure.Deployable.GetPackageRef().Canonical()).
HumanReadablef(inv.Description)
return tasks.Return(ctx, action, func(ctx context.Context) (*execution.HandleResult, error) {
data := map[string]string{}
output := &kubedef.EnsureRuntimeConfigOutput{}
// We here generate the runtime and resource configuration which
// is injected to servers. Ideally this configmap would be
// immutable, but in order to minimize churn we update buildvcs
// in place, if one is available.
//
// The name of the configmap is derived from the contents of the
// runtime and resource configurations. If these don't change,
// then the resulting configmap will be the same (albeit with an
// updated buildvcs).
hashInputs := map[string]any{
"version": runtimeConfigVersion,
}
if ensure.RuntimeConfig != nil {
serializedConfig, err := json.Marshal(ensure.RuntimeConfig)
if err != nil {
return nil, fnerrors.InternalError("failed to serialize runtime configuration: %w", err)
}
data["runtime.json"] = string(serializedConfig)
hashInputs["runtime.json"] = string(serializedConfig)
output.SerializedRuntimeJson = string(serializedConfig)
}
if ensure.BuildVcs != nil {
serializedConfig, err := json.Marshal(ensure.BuildVcs)
if err != nil {
return nil, fnerrors.InternalError("failed to serialize runtime configuration: %w", err)
}
// Deliberately not an hash input.
data["buildvcs.json"] = string(serializedConfig)
}
resourceData, err := deploy.BuildResourceMap(ctx, ensure.Dependency)
if err != nil {
return nil, err
}
if len(ensure.InjectResource) > 0 {
if resourceData == nil {
resourceData = map[string]deploy.RawJSONObject{}
}
var errs []error
for _, injected := range ensure.InjectResource {
var m deploy.RawJSONObject
if err := json.Unmarshal(injected.SerializedJson, &m); err != nil {
errs = append(errs, err)
} else {
resourceData[injected.GetResourceRef().Canonical()] = m
}
}
if err := multierr.New(errs...); err != nil {
return nil, fnerrors.InternalError("failed to handle injected resources: %w", err)
}
}
if len(resourceData) > 0 {
serializedConfig, err := json.Marshal(resourceData)
if err != nil {
return nil, fnerrors.InternalError("failed to serialize resource configuration: %w", err)
}
data["resources.json"] = string(serializedConfig)
hashInputs["resources.json"] = string(serializedConfig)
output.SerializedResourceJson = string(serializedConfig)
}
if len(data) > 0 && ensure.PersistConfiguration {
configDigest, err := schema.DigestOf(hashInputs)
if err != nil {
return nil, fnerrors.InternalError("failed to digest runtime configuration: %w", err)
}
deploymentId := kubedef.MakeDeploymentId(ensure.Deployable)
configId := kubedef.MakeResourceName(deploymentId, configDigest.Hex[:8], "runtimecfg", "namespace", "so")
cluster, err := kubedef.InjectedKubeClusterNamespace(ctx)
if err != nil {
return nil, err
}
annotations := kubedef.MakeAnnotations(cluster.KubeConfig().Environment, ensure.Deployable.GetPackageRef().AsPackageName())
labels := kubedef.MakeLabels(cluster.KubeConfig().Environment, ensure.Deployable)
if _, err := cluster.Cluster().(kubedef.KubeCluster).PreparedClient().Clientset.CoreV1().
ConfigMaps(cluster.KubeConfig().Namespace).
Apply(ctx,
applycorev1.ConfigMap(configId, cluster.KubeConfig().Namespace).
WithAnnotations(annotations).
WithLabels(labels).
WithLabels(map[string]string{
kubedef.K8sKind: kubedef.K8sRuntimeConfigKind,
}).
WithData(data), kubedef.Ego()); err != nil {
return nil, err
}
output.ConfigId = configId
}
return &execution.HandleResult{
Outputs: []execution.Output{
{InstanceID: kubedef.RuntimeConfigOutput(ensure.Deployable), Message: output},
},
}, nil
})
},
PlanOrder: func(ctx context.Context, ensure *kubedef.OpEnsureRuntimeConfig) (*schema.ScheduleOrder, error) {
cluster, err := kubedef.InjectedKubeClusterNamespace(ctx)
if err != nil {
return nil, err
}
return &schema.ScheduleOrder{
SchedAfterCategory: []string{kubedef.MakeNamespaceCat(cluster.KubeConfig().Namespace)},
}, nil
},
})
}