Skip to content
This repository has been archived by the owner on Aug 22, 2023. It is now read-only.

Commit

Permalink
Merge pull request #100 from vshn/fix-ns-labels
Browse files Browse the repository at this point in the history
Add appuio.io/organization label to namespace
  • Loading branch information
ccremer authored Jul 5, 2022
2 parents 878075c + a7b1b17 commit d6e7aa8
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 3 deletions.
1 change: 1 addition & 0 deletions operator/standalone/provisioning.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ func (p *CreateStandalonePipeline) Run(ctx context.Context) error {
return pipeline.NewPipeline().
WithSteps(
pipeline.NewStepFromFunc("fetch operator config", steps.FetchOperatorConfigFn(p.operatorNamespace)),
pipeline.NewStepFromFunc("fetch instance namespace", steps.FetchNamespaceFn(instance.Namespace, steps.InstanceNamespaceKey{})),

pipeline.NewStepFromFunc("add finalizer", steps.AddFinalizerFn(instance, finalizer)),
pipeline.NewStepFromFunc("mark instance as progressing", steps.MarkInstanceAsProgressingFn()),
Expand Down
3 changes: 3 additions & 0 deletions operator/steps/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,9 @@ type ConnectionSecretKey struct{}
// CredentialSecretKey identifies the credential secret for PostgreSQL in the context.
type CredentialSecretKey struct{}

// InstanceNamespaceKey identifies the namespace resource of the instance in the context.
type InstanceNamespaceKey struct{}

// SetClientInContext sets the given client in the context.
func SetClientInContext(ctx context.Context, c client.Client) {
pipeline.StoreInContext(ctx, ClientKey{}, c)
Expand Down
2 changes: 1 addition & 1 deletion operator/steps/helmrelease_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,7 @@ func TestApplyValuesFromInstance(t *testing.T) {
assert.Equal(t, testValues, result)
}

func TestCreateStandalonePipeline_IsHelmReleaseReady(t *testing.T) {
func TestIsHelmReleaseReady(t *testing.T) {
// Arrange
ctx := pipeline.MutableContext(context.Background())
instance := newInstance("release-ready", "my-app")
Expand Down
24 changes: 23 additions & 1 deletion operator/steps/namespace.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,20 +6,30 @@ import (
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, labelSet)
deploymentNamespace.Labels = labels.Merge(deploymentNamespace.Labels, labels.Merge(copyLabels, labelSet))
return nil
})
pipeline.StoreInContext(ctx, DeploymentNamespaceKey{}, deploymentNamespace)
Expand Down Expand Up @@ -51,3 +61,15 @@ func DeleteNamespaceFn() func(ctx context.Context) error {
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
}
}
20 changes: 19 additions & 1 deletion operator/steps/namespace_it_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"github.com/stretchr/testify/suite"
"github.com/vshn/appcat-service-postgresql/operator/operatortest"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/labels"
"k8s.io/apimachinery/pkg/types"
"testing"
Expand All @@ -24,10 +25,12 @@ func (ts *NamespaceSuite) BeforeTest(suiteName, testName string) {
SetClientInContext(ts.Context, ts.Client)
}

func (ts *NamespaceSuite) Test_EnsureDeploymentNamespace() {
func (ts *NamespaceSuite) Test_EnsureNamespace() {
// Arrange
instance := newInstance("test-ensure-namespace", "my-app")
instanceNs := &corev1.Namespace{ObjectMeta: metav1.ObjectMeta{Name: instance.Namespace, Labels: map[string]string{"appuio.io/organization": "organization"}}}
SetInstanceInContext(ts.Context, instance)
pipeline.StoreInContext(ts.Context, InstanceNamespaceKey{}, instanceNs)

// Act
err := EnsureNamespace("sv-postgresql-s-merry-vigilante-7b16", labels.Set{
Expand All @@ -41,6 +44,7 @@ func (ts *NamespaceSuite) Test_EnsureDeploymentNamespace() {
ts.FetchResource(types.NamespacedName{Name: "sv-postgresql-s-merry-vigilante-7b16"}, ns)
ts.Assert().Equal(ns.Labels["app.kubernetes.io/instance"], instance.Name)
ts.Assert().Equal(ns.Labels["app.kubernetes.io/instance-namespace"], instance.Namespace)
ts.Assert().Equal(ns.Labels["appuio.io/organization"], "organization", "label required by APPUiO Cloud")
}

func (ts *NamespaceSuite) Test_DeleteNamespace() {
Expand Down Expand Up @@ -79,3 +83,17 @@ func (ts *NamespaceSuite) Test_DeleteNamespace() {
})
}
}

func (ts *NamespaceSuite) Test_FetchNamespaceFn() {
// Arrange
ns := "fetch-ns"
ts.EnsureNS(ns)

// Act
err := FetchNamespaceFn(ns, InstanceNamespaceKey{})(ts.Context)
ts.Require().NoError(err)

// Assert
result := getFromContextOrPanic(ts.Context, InstanceNamespaceKey{}).(*corev1.Namespace)
ts.Assert().Equal(ns, result.Name, "ns name")
}

0 comments on commit d6e7aa8

Please sign in to comment.