diff --git a/main.go b/main.go index c94d6a7a4..636eb9aad 100644 --- a/main.go +++ b/main.go @@ -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) } diff --git a/pkg/templates/nstemplatetiers/nstemplatetier_generator.go b/pkg/templates/nstemplatetiers/nstemplatetier_generator.go index 745186f2d..2d67cc90f 100644 --- a/pkg/templates/nstemplatetiers/nstemplatetier_generator.go +++ b/pkg/templates/nstemplatetiers/nstemplatetier_generator.go @@ -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) @@ -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") } @@ -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) diff --git a/pkg/templates/nstemplatetiers/nstemplatetier_generator_test.go b/pkg/templates/nstemplatetiers/nstemplatetier_generator_test.go index 7d3ece9a6..9122fae65 100644 --- a/pkg/templates/nstemplatetiers/nstemplatetier_generator_test.go +++ b/pkg/templates/nstemplatetiers/nstemplatetier_generator_test.go @@ -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) @@ -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) @@ -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) @@ -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) @@ -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 @@ -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()) @@ -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") @@ -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