-
Notifications
You must be signed in to change notification settings - Fork 0
orchestrator upgrade fix #68
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Draft
guptagunjan
wants to merge
1
commit into
main
Choose a base branch
from
gg/upgrade-fix
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -5,6 +5,7 @@ | |
|
|
||
| import ( | ||
| "context" | ||
| "errors" | ||
| "fmt" | ||
| "github.com/open-edge-platform/orch-library/go/dazl" | ||
| projectActiveWatcherv1 "github.com/open-edge-platform/orch-utils/tenancy-datamodel/build/apis/projectactivewatcher.edge-orchestrator.intel.com/v1" | ||
|
|
@@ -35,7 +36,7 @@ | |
| ) | ||
|
|
||
| type ProjectManager interface { | ||
| CreateProject(orgName string, projectName string, projectUUID string, project NexusProjectInterface) | ||
| CreateProject(orgName string, projectName string, projectUUID string, project NexusProjectInterface, action string) | ||
| DeleteProject(orgName string, projectName string, projectUUID string, project NexusProjectInterface) | ||
| ManifestTag() string | ||
| } | ||
|
|
@@ -189,7 +190,11 @@ | |
| } | ||
| if watcherObj != nil { | ||
| log.Debug("Setting watcher annotations") | ||
| annotations := make(map[string]string) | ||
| // Preserve existing annotations and update only the manifest tag | ||
| annotations := watcherObj.GetAnnotations() | ||
| if annotations == nil { | ||
| annotations = make(map[string]string) | ||
| } | ||
| annotations[ManifestTagAnnotationKey] = h.dispatcher.ManifestTag() | ||
| watcherObj.SetAnnotations(annotations) | ||
| return watcherObj.Update(context.Background()) | ||
|
|
@@ -318,27 +323,73 @@ | |
| }) | ||
|
|
||
| if err != nil { | ||
| log.Errorf("Failed to create ProjectActiveWatcher object with an error: %v", err) | ||
| return err | ||
| // Handle the case where watcher already exists (upgrade scenario) | ||
| if strings.Contains(err.Error(), "already exists") { | ||
| log.Infof("ActiveWatcher already exists for project %s, retrieving existing watcher", project.DisplayName()) | ||
| watcherObj, err = project.GetActiveWatchers(ctx, appName) | ||
| if err != nil { | ||
| log.Errorf("Failed to get existing ProjectActiveWatcher object with an error: %v", err) | ||
| return err | ||
| } | ||
| if watcherObj == nil { | ||
| log.Errorf("Existing ProjectActiveWatcher object is nil for project %s", project.DisplayName()) | ||
| return errors.New("existing watcher is nil") | ||
| } | ||
| } else { | ||
| log.Errorf("Failed to create ProjectActiveWatcher object with an error: %v", err) | ||
| return err | ||
| } | ||
| } | ||
|
|
||
| var action string | ||
|
|
||
| // Always check manifest tag, regardless of watcher status | ||
| // This ensures we handle upgrade scenarios where existing watchers lack manifest tags | ||
| annotations := watcherObj.GetAnnotations() | ||
| currentTag := "" | ||
| if annotations != nil { | ||
| currentTag = annotations[ManifestTagAnnotationKey] | ||
| } | ||
| expectedTag := h.dispatcher.ManifestTag() | ||
|
|
||
| // Check if watcher is already properly provisioned and has correct manifest tag | ||
| if watcherObj.GetSpec().StatusIndicator == projectActiveWatcherv1.StatusIndicationIdle && | ||
| watcherObj.GetSpec().Message == "Created" && | ||
| currentTag == expectedTag { | ||
| // Everything is correct, no need to reprocess | ||
| log.Infof("Watch %s for project %s already provisioned with correct manifest tag %s", | ||
| watcherObj.DisplayName(), project.DisplayName(), currentTag) | ||
| return nil | ||
| } | ||
|
|
||
| // If we reach here, we need to process/update the project | ||
| if watcherObj.GetSpec().StatusIndicator == projectActiveWatcherv1.StatusIndicationIdle && watcherObj.GetSpec().Message == "Created" { | ||
| // This is a rerun of an event we already processed - check for update | ||
| log.Infof("Watch %s for project %s already provisioned", watcherObj.DisplayName(), project.DisplayName()) | ||
| log.Debugf("existing watcher annotations are: %+v", watcherObj.GetAnnotations()) | ||
| annotations := watcherObj.GetAnnotations() | ||
| if annotations[ManifestTagAnnotationKey] == h.dispatcher.ManifestTag() { | ||
| // Manifest tag is correct | ||
| log.Infof("Manifest tag is correct, no need to update") | ||
| return nil | ||
| } | ||
| log.Infof("Manifest tag is not correct, updating. Have %s, want %s", annotations[ManifestTagAnnotationKey], h.dispatcher.ManifestTag()) | ||
| log.Infof("Watch %s for project %s already provisioned but manifest tag needs update. Have '%s', want '%s'", | ||
| watcherObj.DisplayName(), project.DisplayName(), currentTag, expectedTag) | ||
| action = "update" | ||
| } else if currentTag != expectedTag { | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can we add a comment differentiating how the update in this if statement is different from the previous one? Not sure why we need both. |
||
| log.Infof("Manifest tag missing or incorrect for project %s. Have '%s', want '%s'. Current status: %s", | ||
| project.DisplayName(), currentTag, expectedTag, watcherObj.GetSpec().StatusIndicator) | ||
| action = "update" | ||
| } else { | ||
| action = "created" | ||
| } | ||
|
|
||
| // Always ensure manifest tag is set correctly before processing | ||
| if currentTag != expectedTag { | ||
| log.Infof("Setting manifest tag for project %s from '%s' to '%s'", project.DisplayName(), currentTag, expectedTag) | ||
| if annotations == nil { | ||
| annotations = make(map[string]string) | ||
| } | ||
| annotations[ManifestTagAnnotationKey] = expectedTag | ||
| watcherObj.SetAnnotations(annotations) | ||
| err = watcherObj.Update(context.Background()) | ||
| if err != nil { | ||
| log.Errorf("Failed to update manifest tag annotation: %v", err) | ||
| return err | ||
| } | ||
| log.Infof("Successfully updated manifest tag for project %s to %s", project.DisplayName(), expectedTag) | ||
| } | ||
|
|
||
| if nexus.IsAlreadyExists(err) { | ||
| log.Infof("Watch %s already exists for project %s", watcherObj.DisplayName(), project.DisplayName()) | ||
|
|
@@ -354,7 +405,7 @@ | |
| // If there is an error, validateArgs() will also set the watcher status appropriately. | ||
| return err | ||
| } | ||
| h.dispatcher.CreateProject(organizationName, project.DisplayName(), project.GetUID(), project) | ||
| h.dispatcher.CreateProject(organizationName, project.DisplayName(), project.GetUID(), project, action) | ||
|
|
||
| log.Infof("Active watcher %s %s created for Project %s", watcherObj.DisplayName(), action, project.DisplayName()) | ||
|
|
||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,135 @@ | ||
| // SPDX-FileCopyrightText: (C) 2025 Intel Corporation | ||
| // SPDX-License-Identifier: Apache-2.0 | ||
|
|
||
| package nexus | ||
|
|
||
| import ( | ||
| "testing" | ||
|
|
||
| projectActiveWatcherv1 "github.com/open-edge-platform/orch-utils/tenancy-datamodel/build/apis/projectactivewatcher.edge-orchestrator.intel.com/v1" | ||
| "github.com/stretchr/testify/suite" | ||
| ) | ||
|
|
||
| type UpgradeScenarioTestSuite struct { | ||
| suite.Suite | ||
| } | ||
|
|
||
| func (s *UpgradeScenarioTestSuite) TestUpgradeScenarioWithoutManifestTag() { | ||
| // Test simulates upgrade scenario where existing watcher lacks manifest-tag | ||
| m := &MockProjectManager{} | ||
| h := NewNexusHook(m) | ||
|
|
||
| project := NewMockNexusProject("test-project-upgrade", "test-uid-123") | ||
|
|
||
| // First, create the project normally (simulating pre-upgrade state) | ||
| err := h.projectCreated(project) | ||
| s.NoError(err, "Expected no error when creating project initially") | ||
|
|
||
| // Now simulate upgrade by removing manifest-tag from the watcher | ||
| // (this simulates pre-upgrade watcher without manifest-tag) | ||
| watcher := project.activeWatchers["config-provisioner"] | ||
| s.NotNil(watcher, "Expected active watcher to exist") | ||
|
|
||
| // Remove the manifest-tag to simulate pre-upgrade state | ||
| annotations := watcher.GetAnnotations() | ||
| delete(annotations, ManifestTagAnnotationKey) | ||
|
|
||
| // Now call projectCreated again which should handle the upgrade scenario | ||
| err = h.projectCreated(project) | ||
| s.NoError(err, "Expected no error when handling upgrade scenario") | ||
| } | ||
|
|
||
| func (s *UpgradeScenarioTestSuite) TestUpgradeScenarioWithIncorrectManifestTag() { | ||
| // Test simulates upgrade scenario where existing watcher has wrong manifest-tag | ||
| m := &MockProjectManager{} | ||
| h := NewNexusHook(m) | ||
|
|
||
| project := NewMockNexusProject("test-project-upgrade", "test-uid-456") | ||
|
|
||
| // First, create the project normally | ||
| err := h.projectCreated(project) | ||
| s.NoError(err, "Expected no error when creating project initially") | ||
|
|
||
| // Now simulate upgrade by changing manifest-tag to an old value | ||
| watcher := project.activeWatchers["config-provisioner"] | ||
| s.NotNil(watcher, "Expected active watcher to exist") | ||
|
|
||
| // Set an old manifest-tag to simulate upgrade from older version | ||
| annotations := watcher.GetAnnotations() | ||
| annotations[ManifestTagAnnotationKey] = "v1.2.0" // Old tag, different from current v1.3.5 | ||
|
|
||
| // Now call projectCreated again which should handle the upgrade scenario | ||
| err = h.projectCreated(project) | ||
| s.NoError(err, "Expected no error when handling upgrade scenario with incorrect manifest tag") | ||
| } | ||
|
|
||
| func (s *UpgradeScenarioTestSuite) TestCorrectManifestTagNoUpdate() { | ||
| // Test simulates scenario where manifest tag is already correct (no upgrade needed) | ||
| m := &MockProjectManager{} | ||
| h := NewNexusHook(m) | ||
|
|
||
| project := NewMockNexusProject("correct-tag-project", "correct-uid-789") | ||
|
|
||
| // First, create the project normally | ||
| err := h.projectCreated(project) | ||
| s.NoError(err, "Expected no error when creating project initially") | ||
|
|
||
| // Simulate the project creation completing successfully by updating watcher status | ||
| watcher := project.activeWatchers["config-provisioner"] | ||
| s.NotNil(watcher, "Expected active watcher to exist") | ||
|
|
||
| // Simulate successful completion - this would normally be done by the ProjectManager | ||
| annotations := watcher.GetAnnotations() | ||
| annotations[ManifestTagAnnotationKey] = "v1.3.5" // Set correct manifest tag | ||
| watcher.GetSpec().StatusIndicator = projectActiveWatcherv1.StatusIndicationIdle | ||
| watcher.GetSpec().Message = "Created" | ||
|
|
||
| initialCreatedCount := len(m.created) | ||
|
|
||
| // Now call projectCreated again - since manifest tag is already correct, no update should occur | ||
| err = h.projectCreated(project) | ||
| s.NoError(err, "Expected no error when manifest tag is already correct") | ||
|
|
||
| // Verify that the manifest tag remains correct | ||
| annotations = watcher.GetAnnotations() | ||
| s.Contains(annotations, ManifestTagAnnotationKey, "Expected manifest tag to be present") | ||
| s.Equal("v1.3.5", annotations[ManifestTagAnnotationKey], "Expected manifest tag to remain unchanged") | ||
|
|
||
| // Verify that CreateProject was NOT called again since no update was needed | ||
| s.Equal(initialCreatedCount, len(m.created), "Expected no additional project creation when manifest tag is correct") | ||
| } | ||
|
|
||
| func (s *UpgradeScenarioTestSuite) TestSameManifestTagNoProcessing() { | ||
| // Test simulates scenario where manifest tag hasn't changed during upgrade | ||
| m := &MockProjectManager{} | ||
| h := NewNexusHook(m) | ||
|
|
||
| project := NewMockNexusProject("same-tag-project", "same-tag-uid-999") | ||
|
|
||
| // First, create the project normally | ||
| err := h.projectCreated(project) | ||
| s.NoError(err, "Expected no error when creating project initially") | ||
|
|
||
| // Simulate successful completion with correct manifest tag | ||
| watcher := project.activeWatchers["config-provisioner"] | ||
| s.NotNil(watcher, "Expected active watcher to exist") | ||
|
|
||
| annotations := watcher.GetAnnotations() | ||
| annotations[ManifestTagAnnotationKey] = "v1.3.5" // Same as MockProjectManager returns | ||
| watcher.GetSpec().StatusIndicator = projectActiveWatcherv1.StatusIndicationIdle | ||
| watcher.GetSpec().Message = "Created" | ||
|
|
||
| initialCreatedCount := len(m.created) | ||
|
|
||
| // Simulate "upgrade" where manifest tag hasn't actually changed | ||
| // This should result in early exit with no processing | ||
| err = h.projectCreated(project) | ||
| s.NoError(err, "Expected no error when manifest tag is unchanged") | ||
|
|
||
| // Verify that NO additional processing occurred | ||
| s.Equal(initialCreatedCount, len(m.created), "Expected no project creation when manifest tag unchanged") | ||
| } | ||
|
|
||
| func TestUpgradeScenario(t *testing.T) { | ||
| suite.Run(t, &UpgradeScenarioTestSuite{}) | ||
| } | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
what happens if annotations[ManifestTagAnnotationKey] is not present?