-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
ae1af2b
commit f239cb6
Showing
13 changed files
with
124 additions
and
48 deletions.
There are no files selected for viewing
This file contains 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 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 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 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 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 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 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 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 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 was deleted.
Oops, something went wrong.
This file contains 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,58 @@ | ||
package operatorcontext | ||
|
||
import ( | ||
"context" | ||
"time" | ||
) | ||
|
||
const EnvLocal = "local" | ||
|
||
var _ context.Context = (*Context)(nil) | ||
|
||
type Context struct { | ||
Te string | ||
Env string | ||
inner context.Context | ||
} | ||
|
||
func (c *Context) IsLocal() bool { | ||
return c.Env == EnvLocal | ||
} | ||
|
||
func Discover(ctx context.Context) (*Context, error) { | ||
err := ctx.Err() | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
// This should come from the environment/context somewhere | ||
// there should be 1:1 mapping between TE/env:cluster | ||
te := "local" | ||
env := EnvLocal | ||
|
||
return &Context{Te: te, Env: env, inner: ctx}, nil | ||
} | ||
|
||
func DiscoverOrDie(ctx context.Context) *Context { | ||
context, err := Discover(ctx) | ||
if err != nil { | ||
panic(err) | ||
} | ||
return context | ||
} | ||
|
||
func (c *Context) Deadline() (deadline time.Time, ok bool) { | ||
return c.inner.Deadline() | ||
} | ||
|
||
func (c *Context) Done() <-chan struct{} { | ||
return c.inner.Done() | ||
} | ||
|
||
func (c *Context) Err() error { | ||
return c.inner.Err() | ||
} | ||
|
||
func (c *Context) Value(key any) any { | ||
return c.inner.Value(key) | ||
} |
This file contains 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,41 @@ | ||
package operatorcontext | ||
|
||
import ( | ||
"context" | ||
"testing" | ||
|
||
. "github.com/onsi/gomega" | ||
) | ||
|
||
func TestDiscoversOk(t *testing.T) { | ||
RegisterTestingT(t) | ||
|
||
operatorContext, err := Discover(context.Background()) | ||
Expect(err).NotTo(HaveOccurred()) | ||
Expect(operatorContext).NotTo(BeNil()) | ||
} | ||
|
||
func TestCancellationBefore(t *testing.T) { | ||
RegisterTestingT(t) | ||
|
||
ctx, cancel := context.WithCancel(context.Background()) | ||
cancel() | ||
operatorContext, err := Discover(ctx) | ||
Expect(operatorContext).To(BeNil()) | ||
Expect(ctx.Err()).To(MatchError("context canceled")) | ||
Expect(err).To(MatchError("context canceled")) | ||
|
||
} | ||
|
||
func TestCancellationAfter(t *testing.T) { | ||
RegisterTestingT(t) | ||
|
||
ctx, cancel := context.WithCancel(context.Background()) | ||
operatorContext, err := Discover(ctx) | ||
Expect(err).NotTo(HaveOccurred()) | ||
Expect(operatorContext).NotTo(BeNil()) | ||
Expect(ctx.Err()).To(Succeed()) | ||
|
||
cancel() | ||
Expect(ctx.Err()).To(MatchError("context canceled")) | ||
} |
This file contains 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