Skip to content

Commit

Permalink
fix: forward context in nstemplatetier generator (#916)
Browse files Browse the repository at this point in the history
Signed-off-by: Francesco Ilario <filario@redhat.com>
Co-authored-by: Alexey Kazakov <alkazako@redhat.com>
  • Loading branch information
filariow and alexeykazakov authored Nov 6, 2023
1 parent e0d24ab commit a8aa9be
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 15 deletions.
2 changes: 1 addition & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -389,7 +389,7 @@ func main() { // nolint:gocyclo
// create or update all NSTemplateTiers on the cluster at startup
setupLog.Info("Creating/updating the NSTemplateTier resources")
nstemplatetierAssets := assets.NewAssets(nstemplatetiers.AssetNames, nstemplatetiers.Asset)
if err := nstemplatetiers.CreateOrUpdateResources(mgr.GetScheme(), mgr.GetClient(), namespace, nstemplatetierAssets); err != nil {
if err := nstemplatetiers.CreateOrUpdateResources(stopChannel, mgr.GetScheme(), mgr.GetClient(), namespace, nstemplatetierAssets); err != nil {
setupLog.Error(err, "")
os.Exit(1)
}
Expand Down
9 changes: 4 additions & 5 deletions pkg/templates/nstemplatetiers/nstemplatetier_generator.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ var log = logf.Log.WithName("templates")

// CreateOrUpdateResources generates the NSTemplateTier resources from the cluster resource template and namespace templates,
// then uses the manager's client to create or update the resources on the cluster.
func CreateOrUpdateResources(s *runtime.Scheme, client runtimeclient.Client, namespace string, assets assets.Assets) error {
func CreateOrUpdateResources(ctx context.Context, s *runtime.Scheme, client runtimeclient.Client, namespace string, assets assets.Assets) error {

// initialize tier generator, loads templates from assets
generator, err := newNSTemplateTierGenerator(s, client, namespace, assets)
Expand All @@ -38,7 +38,7 @@ func CreateOrUpdateResources(s *runtime.Scheme, client runtimeclient.Client, nam
}

// create the TierTemplate resources
err = generator.createTierTemplates()
err = generator.createTierTemplates(ctx)
if err != nil {
return errors.Wrap(err, "unable to create TierTemplates")
}
Expand Down Expand Up @@ -297,14 +297,13 @@ func (t *tierGenerator) newTierTemplates(basedOnTierFileRevision string, tierDat
}

// createTierTemplates creates all TierTemplate resources from the tier map
func (t *tierGenerator) createTierTemplates() error {

func (t *tierGenerator) createTierTemplates(ctx context.Context) error {
// create the templates
for _, tierTmpls := range t.templatesByTier {
for _, tierTmpl := range tierTmpls.tierTemplates {
log.Info("creating TierTemplate", "namespace", tierTmpl.Namespace, "name", tierTmpl.Name)
// using the "standard" client since we don't need to support updates on such resources, they should be immutable
if err := t.client.Create(context.TODO(), tierTmpl); err != nil && !apierrors.IsAlreadyExists(err) {
if err := t.client.Create(ctx, tierTmpl); err != nil && !apierrors.IsAlreadyExists(err) {
return errors.Wrapf(err, "unable to create the '%s' TierTemplate in namespace '%s'", tierTmpl.Name, tierTmpl.Namespace)
}
log.Info("TierTemplate resource created", "namespace", tierTmpl.Namespace, "name", tierTmpl.Name)
Expand Down
18 changes: 9 additions & 9 deletions pkg/templates/nstemplatetiers/nstemplatetier_generator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ func TestCreateOrUpdateResources(t *testing.T) {
assets := assets.NewAssets(testnstemplatetiers.AssetNames, testnstemplatetiers.Asset)

// when
err := nstemplatetiers.CreateOrUpdateResources(s, clt, namespace, assets)
err := nstemplatetiers.CreateOrUpdateResources(context.TODO(), s, clt, namespace, assets)

// then
require.NoError(t, err)
Expand Down Expand Up @@ -169,11 +169,11 @@ func TestCreateOrUpdateResources(t *testing.T) {
clt := commontest.NewFakeClient(t)

// when
err := nstemplatetiers.CreateOrUpdateResources(s, clt, namespace, testassets)
err := nstemplatetiers.CreateOrUpdateResources(context.TODO(), s, clt, namespace, testassets)
require.NoError(t, err)

// when calling CreateOrUpdateResources a second time
err = nstemplatetiers.CreateOrUpdateResources(s, clt, namespace, testassets)
err = nstemplatetiers.CreateOrUpdateResources(context.TODO(), s, clt, namespace, testassets)

// then
require.NoError(t, err)
Expand Down Expand Up @@ -225,7 +225,7 @@ func TestCreateOrUpdateResources(t *testing.T) {
clt := commontest.NewFakeClient(t)

// when
err := nstemplatetiers.CreateOrUpdateResources(s, clt, namespace, testassets)
err := nstemplatetiers.CreateOrUpdateResources(context.TODO(), s, clt, namespace, testassets)
require.NoError(t, err)

// given a new set of tier templates (same content but new revisions, which is what we'll want to check here)
Expand All @@ -251,7 +251,7 @@ func TestCreateOrUpdateResources(t *testing.T) {
})

// when calling CreateOrUpdateResources a second time
err = nstemplatetiers.CreateOrUpdateResources(s, clt, namespace, testassets)
err = nstemplatetiers.CreateOrUpdateResources(context.TODO(), s, clt, namespace, testassets)

// then
require.NoError(t, err)
Expand Down Expand Up @@ -356,7 +356,7 @@ func TestCreateOrUpdateResources(t *testing.T) {
})
clt := commontest.NewFakeClient(t)
// when
err := nstemplatetiers.CreateOrUpdateResources(s, clt, namespace, fakeAssets)
err := nstemplatetiers.CreateOrUpdateResources(context.TODO(), s, clt, namespace, fakeAssets)
// then
require.Error(t, err)
assert.Equal(t, "unable to init NSTemplateTier generator: unable to load templates: an error", err.Error()) // error occurred while creating TierTemplate resources
Expand All @@ -376,7 +376,7 @@ func TestCreateOrUpdateResources(t *testing.T) {
}
assets := assets.NewAssets(testnstemplatetiers.AssetNames, testnstemplatetiers.Asset)
// when
err := nstemplatetiers.CreateOrUpdateResources(s, clt, namespace, assets)
err := nstemplatetiers.CreateOrUpdateResources(context.TODO(), s, clt, namespace, assets)
// then
require.Error(t, err)
assert.Regexp(t, "unable to create NSTemplateTiers: unable to create or update the '\\w+' NSTemplateTier: unable to create resource of kind: NSTemplateTier, version: v1alpha1: an error", err.Error())
Expand All @@ -400,7 +400,7 @@ func TestCreateOrUpdateResources(t *testing.T) {
}
testassets := assets.NewAssets(testnstemplatetiers.AssetNames, testnstemplatetiers.Asset)
// when
err := nstemplatetiers.CreateOrUpdateResources(s, clt, namespace, testassets)
err := nstemplatetiers.CreateOrUpdateResources(context.TODO(), s, clt, namespace, testassets)
// then
require.Error(t, err)
assert.Contains(t, err.Error(), "unable to create NSTemplateTiers: unable to create or update the 'advanced' NSTemplateTier: unable to create resource of kind: NSTemplateTier, version: v1alpha1: unable to update the resource")
Expand All @@ -421,7 +421,7 @@ func TestCreateOrUpdateResources(t *testing.T) {
}
testassets := assets.NewAssets(testnstemplatetiers.AssetNames, testnstemplatetiers.Asset)
// when
err := nstemplatetiers.CreateOrUpdateResources(s, clt, namespace, testassets)
err := nstemplatetiers.CreateOrUpdateResources(context.TODO(), s, clt, namespace, testassets)
// then
require.Error(t, err)
assert.Regexp(t, fmt.Sprintf("unable to create the '\\w+-\\w+-\\w+-\\w+' TierTemplate in namespace '%s'", namespace), err.Error()) // we can't tell for sure which namespace will fail first, but the error should match the given regex
Expand Down

0 comments on commit a8aa9be

Please sign in to comment.