Skip to content
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

Use context consistently #165

Merged
merged 3 commits into from
Dec 10, 2021
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ import (
)

func TestStorageConstraintTemplate(t *testing.T) {
ctx := context.Background()

key := types.NamespacedName{
Name: "foo",
}
Expand All @@ -45,22 +47,22 @@ func TestStorageConstraintTemplate(t *testing.T) {

// Test Create
fetched := &ConstraintTemplate{}
g.Expect(c.Create(context.TODO(), created)).NotTo(gomega.HaveOccurred())
g.Expect(c.Create(ctx, created)).NotTo(gomega.HaveOccurred())

g.Expect(c.Get(context.TODO(), key, fetched)).NotTo(gomega.HaveOccurred())
g.Expect(c.Get(ctx, key, fetched)).NotTo(gomega.HaveOccurred())
g.Expect(fetched).To(gomega.Equal(created))

// Test Updating the Labels
updated := fetched.DeepCopy()
updated.Labels = map[string]string{"hello": "world"}
g.Expect(c.Update(context.TODO(), updated)).NotTo(gomega.HaveOccurred())
g.Expect(c.Update(ctx, updated)).NotTo(gomega.HaveOccurred())

g.Expect(c.Get(context.TODO(), key, fetched)).NotTo(gomega.HaveOccurred())
g.Expect(c.Get(ctx, key, fetched)).NotTo(gomega.HaveOccurred())
g.Expect(fetched).To(gomega.Equal(updated))

// Test Delete
g.Expect(c.Delete(context.TODO(), fetched)).NotTo(gomega.HaveOccurred())
g.Expect(c.Get(context.TODO(), key, fetched)).To(gomega.HaveOccurred())
g.Expect(c.Delete(ctx, fetched)).NotTo(gomega.HaveOccurred())
g.Expect(c.Get(ctx, key, fetched)).To(gomega.HaveOccurred())
}

func TestTypeConversion(t *testing.T) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ import (
)

func TestStorageConstraintTemplate(t *testing.T) {
ctx := context.Background()

key := types.NamespacedName{
Name: "foo",
}
Expand All @@ -45,22 +47,22 @@ func TestStorageConstraintTemplate(t *testing.T) {

// Test Create
fetched := &ConstraintTemplate{}
g.Expect(c.Create(context.TODO(), created)).NotTo(gomega.HaveOccurred())
g.Expect(c.Create(ctx, created)).NotTo(gomega.HaveOccurred())

g.Expect(c.Get(context.TODO(), key, fetched)).NotTo(gomega.HaveOccurred())
g.Expect(c.Get(ctx, key, fetched)).NotTo(gomega.HaveOccurred())
g.Expect(fetched).To(gomega.Equal(created))

// Test Updating the Labels
updated := fetched.DeepCopy()
updated.Labels = map[string]string{"hello": "world"}
g.Expect(c.Update(context.TODO(), updated)).NotTo(gomega.HaveOccurred())
g.Expect(c.Update(ctx, updated)).NotTo(gomega.HaveOccurred())

g.Expect(c.Get(context.TODO(), key, fetched)).NotTo(gomega.HaveOccurred())
g.Expect(c.Get(ctx, key, fetched)).NotTo(gomega.HaveOccurred())
g.Expect(fetched).To(gomega.Equal(updated))

// Test Delete
g.Expect(c.Delete(context.TODO(), fetched)).NotTo(gomega.HaveOccurred())
g.Expect(c.Get(context.TODO(), key, fetched)).To(gomega.HaveOccurred())
g.Expect(c.Delete(ctx, fetched)).NotTo(gomega.HaveOccurred())
g.Expect(c.Get(ctx, key, fetched)).To(gomega.HaveOccurred())
}

func TestTypeConversion(t *testing.T) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ import (
)

func TestStorageConstraintTemplate(t *testing.T) {
ctx := context.Background()

key := types.NamespacedName{
Name: "foo",
}
Expand All @@ -45,22 +47,22 @@ func TestStorageConstraintTemplate(t *testing.T) {

// Test Create
fetched := &ConstraintTemplate{}
g.Expect(c.Create(context.TODO(), created)).NotTo(gomega.HaveOccurred())
g.Expect(c.Create(ctx, created)).NotTo(gomega.HaveOccurred())

g.Expect(c.Get(context.TODO(), key, fetched)).NotTo(gomega.HaveOccurred())
g.Expect(c.Get(ctx, key, fetched)).NotTo(gomega.HaveOccurred())
g.Expect(fetched).To(gomega.Equal(created))

// Test Updating the Labels
updated := fetched.DeepCopy()
updated.Labels = map[string]string{"hello": "world"}
g.Expect(c.Update(context.TODO(), updated)).NotTo(gomega.HaveOccurred())
g.Expect(c.Update(ctx, updated)).NotTo(gomega.HaveOccurred())

g.Expect(c.Get(context.TODO(), key, fetched)).NotTo(gomega.HaveOccurred())
g.Expect(c.Get(ctx, key, fetched)).NotTo(gomega.HaveOccurred())
g.Expect(fetched).To(gomega.Equal(updated))

// Test Delete
g.Expect(c.Delete(context.TODO(), fetched)).NotTo(gomega.HaveOccurred())
g.Expect(c.Get(context.TODO(), key, fetched)).To(gomega.HaveOccurred())
g.Expect(c.Delete(ctx, fetched)).NotTo(gomega.HaveOccurred())
g.Expect(c.Get(ctx, key, fetched)).To(gomega.HaveOccurred())
}

func TestTypeConversion(t *testing.T) {
Expand Down
3 changes: 1 addition & 2 deletions constraint/pkg/client/backend.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package client

import (
"context"
"fmt"

"github.com/open-policy-agent/frameworks/constraint/pkg/client/drivers"
Expand Down Expand Up @@ -81,7 +80,7 @@ func (b *Backend) NewClient(opts ...Opt) (*Client, error) {
ErrCreatingClient)
}

if err := b.driver.Init(context.Background()); err != nil {
if err := b.driver.Init(); err != nil {
return nil, err
}

Expand Down
22 changes: 11 additions & 11 deletions constraint/pkg/client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -333,7 +333,7 @@ func (c *Client) createTemplateArtifacts(templ *templates.ConstraintTemplate) (*
}

// CreateCRD creates a CRD from template.
func (c *Client) CreateCRD(_ context.Context, templ *templates.ConstraintTemplate) (*apiextensions.CustomResourceDefinition, error) {
func (c *Client) CreateCRD(templ *templates.ConstraintTemplate) (*apiextensions.CustomResourceDefinition, error) {
if templ == nil {
return nil, fmt.Errorf("%w: got nil ConstraintTemplate",
ErrInvalidConstraintTemplate)
Expand All @@ -349,7 +349,7 @@ func (c *Client) CreateCRD(_ context.Context, templ *templates.ConstraintTemplat
// AddTemplate adds the template source code to OPA and registers the CRD with the client for
// schema validation on calls to AddConstraint. On error, the responses return value
// will still be populated so that partial results can be analyzed.
func (c *Client) AddTemplate(ctx context.Context, templ *templates.ConstraintTemplate) (*types.Responses, error) {
func (c *Client) AddTemplate(templ *templates.ConstraintTemplate) (*types.Responses, error) {
resp := types.NewResponses()

basicArtifacts, err := c.createBasicTemplateArtifacts(templ)
Expand All @@ -358,7 +358,7 @@ func (c *Client) AddTemplate(ctx context.Context, templ *templates.ConstraintTem
}

// return immediately if no change
if cached, err := c.GetTemplate(ctx, templ); err == nil && cached.SemanticEqual(templ) {
if cached, err := c.GetTemplate(templ); err == nil && cached.SemanticEqual(templ) {
resp.Handled[basicArtifacts.targetHandler.GetName()] = true
return resp, nil
}
Expand All @@ -371,7 +371,7 @@ func (c *Client) AddTemplate(ctx context.Context, templ *templates.ConstraintTem
c.mtx.Lock()
defer c.mtx.Unlock()

if err = c.backend.driver.PutModules(ctx, artifacts.namePrefix, artifacts.modules); err != nil {
if err = c.backend.driver.PutModules(artifacts.namePrefix, artifacts.modules); err != nil {
return resp, fmt.Errorf("%w: %v", local.ErrCompile, err)
}

Expand Down Expand Up @@ -417,7 +417,7 @@ func (c *Client) RemoveTemplate(ctx context.Context, templ *templates.Constraint
return resp, err
}

if _, err := c.backend.driver.DeleteModules(ctx, artifacts.namePrefix); err != nil {
if _, err := c.backend.driver.DeleteModules(artifacts.namePrefix); err != nil {
return resp, err
}

Expand All @@ -438,7 +438,7 @@ func (c *Client) RemoveTemplate(ctx context.Context, templ *templates.Constraint
}

// GetTemplate gets the currently recognized template.
func (c *Client) GetTemplate(_ context.Context, templ *templates.ConstraintTemplate) (*templates.ConstraintTemplate, error) {
func (c *Client) GetTemplate(templ *templates.ConstraintTemplate) (*templates.ConstraintTemplate, error) {
artifacts, err := c.createRawTemplateArtifacts(templ)
if err != nil {
return nil, err
Expand Down Expand Up @@ -651,7 +651,7 @@ func (c *Client) getConstraintNoLock(constraint *unstructured.Unstructured) (*un
}

// GetConstraint gets the currently recognized constraint.
func (c *Client) GetConstraint(_ context.Context, constraint *unstructured.Unstructured) (*unstructured.Unstructured, error) {
func (c *Client) GetConstraint(constraint *unstructured.Unstructured) (*unstructured.Unstructured, error) {
c.mtx.RLock()
defer c.mtx.RUnlock()

Expand Down Expand Up @@ -679,7 +679,7 @@ func (c *Client) validateConstraint(constraint *unstructured.Unstructured, lock

// ValidateConstraint returns an error if the constraint is not recognized or does not conform to
// the registered CRD for that constraint.
func (c *Client) ValidateConstraint(_ context.Context, constraint *unstructured.Unstructured) error {
func (c *Client) ValidateConstraint(constraint *unstructured.Unstructured) error {
return c.validateConstraint(constraint, true)
}

Expand All @@ -695,7 +695,7 @@ func (c *Client) init() error {
}

builtinPath := fmt.Sprintf("%s.hooks_builtin", hooks)
err := c.backend.driver.PutModule(context.Background(), builtinPath, libBuiltin.String())
err := c.backend.driver.PutModule(builtinPath, libBuiltin.String())
if err != nil {
return err
}
Expand Down Expand Up @@ -744,7 +744,7 @@ func (c *Client) init() error {
ErrCreatingClient, err)
}

err = c.backend.driver.PutModule(context.TODO(), modulePath, string(src))
err = c.backend.driver.PutModule(modulePath, string(src))
if err != nil {
return fmt.Errorf("%w: error %s from compiled source:\n%s",
ErrCreatingClient, err, src)
Expand All @@ -769,7 +769,7 @@ func (c *Client) Reset(ctx context.Context) error {
}
for name, v := range c.templates {
for _, t := range v.Targets {
if _, err := c.backend.driver.DeleteModule(ctx, fmt.Sprintf(`templates["%s"]["%s"]`, t, name)); err != nil {
if _, err := c.backend.driver.DeleteModule(fmt.Sprintf(`templates["%s"]["%s"]`, t, name)); err != nil {
return err
}
}
Expand Down
4 changes: 1 addition & 3 deletions constraint/pkg/client/client_addtemplate_bench_test.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package client

import (
"context"
"fmt"
"testing"

Expand Down Expand Up @@ -82,7 +81,6 @@ func BenchmarkClient_AddTemplate(b *testing.B) {

for i := 0; i < b.N; i++ {
b.StopTimer()
ctx := context.Background()
targets := Targets(&handler{})

d := local.New()
Expand All @@ -100,7 +98,7 @@ func BenchmarkClient_AddTemplate(b *testing.B) {
b.StartTimer()

for _, ct := range cts {
_, _ = c.AddTemplate(ctx, ct)
_, _ = c.AddTemplate(ct)
}
}
})
Expand Down
Loading