Skip to content

Commit

Permalink
feat: add annotation to the custom namesapce
Browse files Browse the repository at this point in the history
Signed-off-by: Ashutosh <mail.ashutosh8@gmail.com>
  • Loading branch information
ashutosh16 committed Aug 11, 2022
1 parent e03364f commit bacc34e
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 4 deletions.
7 changes: 7 additions & 0 deletions pkg/sync/common/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -140,3 +140,10 @@ type ResourceSyncResult struct {
// indicates the particular phase of the sync that this is for
SyncPhase SyncPhase
}

// NamespaceMetadata is a metadata that namespace can have, includes all the objects
// users want to create.
type NamespaceMetadata struct {
Annotations map[string]string
Labels map[string]string
}
16 changes: 15 additions & 1 deletion pkg/sync/sync_context.go
Original file line number Diff line number Diff line change
Expand Up @@ -152,10 +152,11 @@ func WithResourceModificationChecker(enabled bool, diffResults *diff.DiffResultL
}

// WithNamespaceCreation will create non-exist namespace
func WithNamespaceCreation(createNamespace bool, namespaceModifier func(*unstructured.Unstructured) bool) SyncOpt {
func WithNamespaceCreation(createNamespace bool, nsMetaData common.NamespaceMetadata, namespaceModifier func(*unstructured.Unstructured) bool) SyncOpt {
return func(ctx *syncContext) {
ctx.createNamespace = createNamespace
ctx.namespaceModifier = namespaceModifier
ctx.nsMetaData = &nsMetaData
}
}

Expand Down Expand Up @@ -349,6 +350,7 @@ type syncContext struct {

createNamespace bool
namespaceModifier func(*unstructured.Unstructured) bool
nsMetaData *common.NamespaceMetadata

syncWaveHook common.SyncWaveHook

Expand Down Expand Up @@ -775,7 +777,19 @@ func (sc *syncContext) autoCreateNamespace(tasks syncTasks) syncTasks {
}

if isNamespaceCreationNeeded {

nsSpec := &v1.Namespace{TypeMeta: metav1.TypeMeta{APIVersion: "v1", Kind: kube.NamespaceKind}, ObjectMeta: metav1.ObjectMeta{Name: sc.namespace}}

//set user provided namespace object meta data
if sc.nsMetaData != nil {
if sc.nsMetaData.Annotations != nil {
nsSpec.Annotations = sc.nsMetaData.Annotations
}
if sc.nsMetaData.Labels != nil {
nsSpec.Labels = sc.nsMetaData.Labels
}
}

unstructuredObj, err := kube.ToUnstructured(nsSpec)
if err == nil {
liveObj, err := sc.kubectl.GetResource(context.TODO(), sc.config, unstructuredObj.GroupVersionKind(), unstructuredObj.GetName(), metav1.NamespaceNone)
Expand Down
19 changes: 16 additions & 3 deletions pkg/sync/sync_context_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -733,6 +733,14 @@ func TestNamespaceAutoCreation(t *testing.T) {
task, err := createNamespaceTask(syncCtx.namespace)
assert.NoError(t, err, "Failed creating test data: namespace task")

n := synccommon.NamespaceMetadata{Annotations: map[string]string{
"fake.annotation.io/fake1": "v1",
},
Labels: map[string]string{
"labels": "fake",
},
}

//Namespace auto creation pre-sync task should not be there
//since there is namespace resource in syncCtx.resources
t.Run("no pre-sync task 1", func(t *testing.T) {
Expand Down Expand Up @@ -777,24 +785,29 @@ func TestNamespaceAutoCreation(t *testing.T) {
Version: task.version(),
Status: task.syncStatus,
Message: task.message,
HookType: task.hookType(),
HookType: task.hookType(),git
HookPhase: task.operationState,
SyncPhase: task.phase,
}
syncCtx.syncRes = map[string]synccommon.ResourceSyncResult{}
syncCtx.nsMetaData = &n
syncCtx.syncRes[task.resultKey()] = res

tasks, successful := syncCtx.getSyncTasks()

assert.True(t, successful)
assert.Len(t, tasks, 2)
assert.Contains(t, tasks, task)
assert.Equal(t, tasks[0].targetObj.GetAnnotations(), n.Annotations)
assert.Equal(t, tasks[0].targetObj.GetLabels(), n.Labels)

})

}

func createNamespaceTask(namespace string) (*syncTask, error) {

nsSpec := &corev1.Namespace{TypeMeta: v1.TypeMeta{APIVersion: "v1", Kind: kube.NamespaceKind}, ObjectMeta: v1.ObjectMeta{Name: namespace}}
nsSpec.Annotations = map[string]string{"fake.annotation.io/fake1": "v1"}
nsSpec.Labels = map[string]string{"labels": "fake"}
unstructuredObj, err := kube.ToUnstructured(nsSpec)

task := &syncTask{phase: synccommon.SyncPhasePreSync, targetObj: unstructuredObj}
Expand Down

0 comments on commit bacc34e

Please sign in to comment.