-
Notifications
You must be signed in to change notification settings - Fork 2
/
customK8sCollectApplier.go
57 lines (44 loc) · 1.37 KB
/
customK8sCollectApplier.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
package resource
import (
"context"
"fmt"
"github.com/cloudogu/k8s-apply-lib/apply"
k8sv1 "github.com/cloudogu/k8s-dogu-operator/api/v1"
"github.com/cloudogu/k8s-dogu-operator/internal/cloudogu"
"sigs.k8s.io/controller-runtime/pkg/log"
)
type collectApplier struct {
applier cloudogu.Applier
}
// NewCollectApplier creates a K8s resource applier that filters and collects deployment resources for a later,
// customized application.
func NewCollectApplier(applier cloudogu.Applier) *collectApplier {
return &collectApplier{applier: applier}
}
// CollectApply applies the given resource.
func (ca *collectApplier) CollectApply(ctx context.Context, customK8sResources map[string]string, doguResource *k8sv1.Dogu) error {
logger := log.FromContext(ctx)
if len(customK8sResources) == 0 {
logger.Info("No custom K8s resources found")
return nil
}
targetNamespace := doguResource.ObjectMeta.Namespace
namespaceTemplate := struct {
Namespace string
}{
Namespace: targetNamespace,
}
for file, yamlDocs := range customK8sResources {
logger.Info(fmt.Sprintf("Applying custom K8s resources from file %s", file))
err := apply.NewBuilder(ca.applier).
WithNamespace(targetNamespace).
WithOwner(doguResource).
WithTemplate(file, namespaceTemplate).
WithYamlResource(file, []byte(yamlDocs)).
ExecuteApply()
if err != nil {
return err
}
}
return nil
}