This repository has been archived by the owner on Aug 22, 2023. It is now read-only.
generated from vshn/go-bootstrap
/
namespace.go
75 lines (67 loc) · 2.65 KB
/
namespace.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
package steps
import (
"context"
pipeline "github.com/ccremer/go-command-pipeline"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/labels"
"k8s.io/apimachinery/pkg/types"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/controller/controllerutil"
)
// AppuioOrganizationLabelKey is the label key required for setting ownership of a namespace
const AppuioOrganizationLabelKey = "appuio.io/organization"
// EnsureNamespace creates the namespace with given name and labels.
func EnsureNamespace(name string, labelSet labels.Set) func(ctx context.Context) error {
return func(ctx context.Context) error {
kube := GetClientFromContext(ctx)
instanceNamespace := getFromContextOrPanic(ctx, InstanceNamespaceKey{}).(*corev1.Namespace)
instanceNsLabels := instanceNamespace.Labels
copyLabels := labels.Set{}
if org, exists := instanceNsLabels[AppuioOrganizationLabelKey]; exists {
copyLabels[AppuioOrganizationLabelKey] = org
}
deploymentNamespace := &corev1.Namespace{
ObjectMeta: metav1.ObjectMeta{Name: name},
}
_, err := controllerutil.CreateOrUpdate(ctx, kube, deploymentNamespace, func() error {
deploymentNamespace.Labels = labels.Merge(deploymentNamespace.Labels, labels.Merge(copyLabels, labelSet))
return nil
})
pipeline.StoreInContext(ctx, DeploymentNamespaceKey{}, deploymentNamespace)
return err
}
}
// DeleteNamespaceFn deletes the namespace where the instance is deployed.
// Ignore "not found" error and returns nil if deployment namespace is unknown.
func DeleteNamespaceFn() func(ctx context.Context) error {
return func(ctx context.Context) error {
kube := GetClientFromContext(ctx)
instance := GetInstanceFromContext(ctx)
if instance.Status.GetDeploymentNamespace() == "" {
// Namespace might not ever have existed, skip
return nil
}
deploymentNamespace := &corev1.Namespace{
ObjectMeta: metav1.ObjectMeta{
Name: instance.Status.GetDeploymentNamespace(),
},
}
propagation := metav1.DeletePropagationBackground
deleteOptions := &client.DeleteOptions{
PropagationPolicy: &propagation,
}
err := kube.Delete(ctx, deploymentNamespace, deleteOptions)
return client.IgnoreNotFound(err)
}
}
// FetchNamespaceFn fetches the namespace of the given name and stores it in the context with given key.
func FetchNamespaceFn(namespaceName string, contextKey any) func(ctx context.Context) error {
return func(ctx context.Context) error {
kube := GetClientFromContext(ctx)
ns := &corev1.Namespace{}
err := kube.Get(ctx, types.NamespacedName{Name: namespaceName}, ns)
pipeline.StoreInContext(ctx, contextKey, ns)
return err
}
}