Skip to content

Commit

Permalink
feat: don't take control over DWO which is not managed by Che
Browse files Browse the repository at this point in the history
  • Loading branch information
sleshchenko committed Sep 24, 2021
1 parent f4d8016 commit dff323b
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 2 deletions.
12 changes: 10 additions & 2 deletions pkg/deploy/dev-workspace/dev_workspace.go
Original file line number Diff line number Diff line change
Expand Up @@ -432,11 +432,19 @@ func syncObject(deployContext *deploy.DeployContext, obj2sync *Object2Sync, name
return false, err
}

cheNamespaceOwnership := actual.(metav1.Object).GetAnnotations()[deploy.CheEclipseOrgNamespace]

// don't sync when object exists but it's not managed by Che
if exists && cheNamespaceOwnership == "" {
return true, nil
}

// sync objects if it has been created by same operator
// or it is the only operator with the `spec.devWorkspace.enable: true`
if !exists ||
(actual.(metav1.Object).GetAnnotations()[deploy.CheEclipseOrgHash256] != obj2sync.hash256 &&
(actual.(metav1.Object).GetAnnotations()[deploy.CheEclipseOrgNamespace] == deployContext.CheCluster.Namespace || isOnlyOneOperatorManagesDWResources)) {
isOnlyOneOperatorManagesDWResources ||
(cheNamespaceOwnership == deployContext.CheCluster.Namespace &&
actual.(metav1.Object).GetAnnotations()[deploy.CheEclipseOrgHash256] != obj2sync.hash256) {

setAnnotations(deployContext, obj2sync)
return deploy.Sync(deployContext, obj2sync.obj)
Expand Down
41 changes: 41 additions & 0 deletions pkg/deploy/dev-workspace/dev_workspace_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -435,6 +435,47 @@ func TestShouldSyncObjectIfItWasCreatedBySameOriginHashDifferent(t *testing.T) {
}
}

func TestShouldNotSyncObjectIfItIsNotMangedByChe(t *testing.T) {
deployContext := deploy.GetTestDeployContext(nil, []runtime.Object{})

initialObject := deploy.GetConfigMapSpec(deployContext, "test", map[string]string{"a": "b"}, "test")
initialObject.SetAnnotations(map[string]string{})
deploy.Create(deployContext, initialObject)

// creates initial object
newObject := deploy.GetConfigMapSpec(deployContext, "test", map[string]string{"a": "c"}, "test")
obj2sync := &Object2Sync{
obj: newObject,
hash256: "newHash",
}

// tries to sync object with a new
_, err := syncObject(deployContext, obj2sync, "eclipse-che")
if err != nil {
t.Fatalf("Failed to sync object: %v", err)
}

// reads object and check content, object supposed to be updated
// it was created by the same origin
actual := &corev1.ConfigMap{}
exists, err := deploy.GetNamespacedObject(deployContext, "test", actual)
if err != nil {
t.Fatalf("Failed to get object: %v", err)
} else if !exists {
t.Fatalf("Object not found")
}

if actual.GetAnnotations()[deploy.CheEclipseOrgHash256] != "" {
t.Fatalf("Invalid hash")
}
if actual.GetAnnotations()[deploy.CheEclipseOrgNamespace] != "" {
t.Fatalf("Invalid namespace")
}
if actual.Data["a"] != "b" {
t.Fatalf("Invalid data")
}
}

func TestShouldNotSyncObjectIfThereIsAnotherCheCluster(t *testing.T) {
cheCluster := &orgv1.CheCluster{
ObjectMeta: metav1.ObjectMeta{
Expand Down

0 comments on commit dff323b

Please sign in to comment.