Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 34 additions & 4 deletions e2e/nomostest/config_sync.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,16 @@ const (
shortSyncPollingPeriod = 5 * time.Second
)

// InstallMethod defines how Config Sync should be installed
type InstallMethod string

const (
// InstallMethodApply uses server-side apply (default)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It doesn't look like this is currently the default. If the intent is to retain default behavior, I'd suggest updating the function signature to accept a variadic list of options (for example func InstallConfigSync(nt *NT, opts ...InstallConfigSyncOpts) error {)

InstallMethodApply InstallMethod = "apply"
// InstallMethodUpdate uses client-side update
InstallMethodUpdate InstallMethod = "update"
)

var (
// baseDir is the path to the Nomos repository root from test case files.
//
Expand Down Expand Up @@ -230,16 +240,36 @@ func parseConfigSyncManifests(nt *NT) ([]client.Object, error) {
}

// InstallConfigSync installs ConfigSync on the test cluster
func InstallConfigSync(nt *NT) error {
nt.T.Log("[SETUP] Installing Config Sync")
func InstallConfigSync(nt *NT, method InstallMethod) error {
nt.T.Log("[SETUP] Installing Config Sync using method: ", method)
objs, err := parseConfigSyncManifests(nt)
if err != nil {
return err
}
for _, o := range objs {
nt.T.Logf("installConfigSync obj: %v", core.GKNN(o))
if err := nt.KubeClient.Apply(o); err != nil {
return err
switch method {
case InstallMethodApply:
if err := nt.KubeClient.Apply(o); err != nil {
return err
}
case InstallMethodUpdate:
currentObj := o.DeepCopyObject().(client.Object)
Copy link

Copilot AI Sep 11, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The type assertion .(client.Object) could panic if DeepCopyObject() returns an object that doesn't implement client.Object. Consider using a type switch or checking the assertion result to handle this more safely.

Suggested change
currentObj := o.DeepCopyObject().(client.Object)
currentObjObj := o.DeepCopyObject()
currentObj, ok := currentObjObj.(client.Object)
if !ok {
return fmt.Errorf("object %v does not implement client.Object", core.GKNN(o))
}

Copilot uses AI. Check for mistakes.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A DeepCopy should not be necessary here for every object, can this be switched to populating an empty object (e.g. Unstructured)?

if err := nt.KubeClient.Get(currentObj.GetName(), currentObj.GetNamespace(), currentObj); err != nil {
if apierrors.IsNotFound(err) {
if err := nt.KubeClient.Create(o); err != nil {
return err
}
} else {
return err
}
} else {
// Attach existing resourceVersion to the object
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The comment doesn't add much context, I'd suggest explaining "why" it's attaching the resourceVersion to the object.

In this instance, the current state of the object on the cluster does not matter and the intent is to fully update to the original configuration declared in the manifest

o.SetResourceVersion(currentObj.GetResourceVersion())
if err := nt.KubeClient.Update(o); err != nil {
return err
}
Comment on lines +267 to +271
Copy link

Copilot AI Sep 11, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Setting the resource version on the object being updated could cause conflicts if the object was modified between the Get and Update operations. Consider using a retry mechanism or server-side apply to handle concurrent modifications more reliably.

Suggested change
// Attach existing resourceVersion to the object
o.SetResourceVersion(currentObj.GetResourceVersion())
if err := nt.KubeClient.Update(o); err != nil {
return err
}
// Attach existing resourceVersion to the object and retry on conflict
const maxUpdateAttempts = 5
var lastErr error
for attempt := 1; attempt <= maxUpdateAttempts; attempt++ {
o.SetResourceVersion(currentObj.GetResourceVersion())
if err := nt.KubeClient.Update(o); err != nil {
if apierrors.IsConflict(err) {
// Re-fetch the latest object and retry
if err := nt.KubeClient.Get(currentObj.GetName(), currentObj.GetNamespace(), currentObj); err != nil {
lastErr = err
break
}
lastErr = err
continue
} else {
lastErr = err
break
}
} else {
lastErr = nil
break
}
}
if lastErr != nil {
return lastErr
}

Copilot uses AI. Check for mistakes.

}
}
}
return nil
Expand Down
2 changes: 1 addition & 1 deletion e2e/nomostest/new.go
Original file line number Diff line number Diff line change
Expand Up @@ -385,7 +385,7 @@ func FreshTestEnv(t nomostesting.NTB, opts *ntopts.New) *NT {
return setupRegistry(nt)
})
tg.Go(func() error {
return InstallConfigSync(nt)
return InstallConfigSync(nt, InstallMethodApply)
})
tg.Go(func() error {
return installPrometheus(nt)
Expand Down
24 changes: 13 additions & 11 deletions e2e/testcases/cli_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1297,13 +1297,14 @@ func TestApiResourceFormatting(t *testing.T) {
}

func TestNomosMigrate(t *testing.T) {
nt := nomostest.New(t, nomostesting.NomosCLI, ntopts.SkipConfigSyncInstall)
nt := nomostest.New(t, nomostesting.NomosCLI)

nt.T.Cleanup(func() {
// Restore state of Config Sync installation after test
if err := nomostest.InstallConfigSync(nt); err != nil {
if err := nomostest.InstallConfigSync(nt, nomostest.InstallMethodUpdate); err != nil {
nt.T.Fatal(err)
}
nt.Must(nt.WatchForAllSyncs())
})
nt.T.Cleanup(func() {
cmObj := &unstructured.Unstructured{
Expand Down Expand Up @@ -1451,11 +1452,11 @@ func TestNomosMigrate(t *testing.T) {
configmanagement.RGControllerName, configmanagement.RGControllerNamespace)
})
tg.Go(func() error {
return nt.Watcher.WatchForNotFound(kinds.Deployment(),
return nt.Watcher.WatchForCurrentStatus(kinds.Deployment(),
core.RootReconcilerName(configsync.RootSyncName), configsync.ControllerNamespace)
})
tg.Go(func() error {
return nt.Watcher.WatchForNotFound(kinds.RootSyncV1Beta1(),
return nt.Watcher.WatchForCurrentStatus(kinds.RootSyncV1Beta1(),
configsync.RootSyncName, configsync.ControllerNamespace)
})
if err := tg.Wait(); err != nil {
Expand All @@ -1464,14 +1465,14 @@ func TestNomosMigrate(t *testing.T) {
}

func TestNomosMigrateMonoRepo(t *testing.T) {
nt := nomostest.New(t, nomostesting.NomosCLI, ntopts.SkipConfigSyncInstall)
nt := nomostest.New(t, nomostesting.NomosCLI)

nt.T.Cleanup(func() {
// Restore state of Config Sync installation after test.
// This also emulates upgrading to the current version after migrating
if err := nomostest.InstallConfigSync(nt); err != nil {
if err := nomostest.InstallConfigSync(nt, nomostest.InstallMethodUpdate); err != nil {
nt.T.Fatal(err)
}
nt.Must(nt.WatchForAllSyncs())
})
nt.T.Cleanup(func() {
crds := []string{
Expand Down Expand Up @@ -1707,13 +1708,14 @@ func TestNomosMigrateMonoRepo(t *testing.T) {
// This test case validates the behavior of the uninstall script defined
// at installation/uninstall_configmanagement.sh
func TestACMUninstallScript(t *testing.T) {
nt := nomostest.New(t, nomostesting.NomosCLI, ntopts.SkipConfigSyncInstall)
nt := nomostest.New(t, nomostesting.NomosCLI)

nt.T.Cleanup(func() {
// Restore state of Config Sync installation after test
if err := nomostest.InstallConfigSync(nt); err != nil {
if err := nomostest.InstallConfigSync(nt, nomostest.InstallMethodUpdate); err != nil {
nt.T.Fatal(err)
}
nt.Must(nt.WatchForAllSyncs())
})
nt.T.Cleanup(func() {
cmObj := &unstructured.Unstructured{
Expand Down Expand Up @@ -1861,11 +1863,11 @@ func TestACMUninstallScript(t *testing.T) {
configmanagement.RGControllerName, configmanagement.RGControllerNamespace)
})
tg.Go(func() error {
return nt.Watcher.WatchForNotFound(kinds.Deployment(),
return nt.Watcher.WatchForCurrentStatus(kinds.Deployment(),
core.RootReconcilerName(configsync.RootSyncName), configsync.ControllerNamespace)
})
tg.Go(func() error {
return nt.Watcher.WatchForNotFound(kinds.RootSyncV1Beta1(),
return nt.Watcher.WatchForCurrentStatus(kinds.RootSyncV1Beta1(),
configsync.RootSyncName, configsync.ControllerNamespace)
})
if err := tg.Wait(); err != nil {
Expand Down