@@ -13,24 +13,33 @@ import (
1313)
1414
1515type (
16- reconcilerNameContextKeyType struct {}
17- clientContextKeyType struct {}
18- componentContextKeyType struct {}
19- componentDigestContextKeyType struct {}
16+ reconcilerNameContextKeyType struct {}
17+ localClientContextKeyType struct {}
18+ clientContextKeyType struct {}
19+ componentContextKeyType struct {}
20+ componentNameContextKeyType struct {}
21+ componentNamespaceContextKeyType struct {}
22+ componentDigestContextKeyType struct {}
2023)
2124
2225var (
23- reconcilerNameContextKey = reconcilerNameContextKeyType {}
24- clientContextKey = clientContextKeyType {}
25- componentContextKey = componentContextKeyType {}
26- componentDigestContextKey = componentDigestContextKeyType {}
26+ reconcilerNameContextKey = reconcilerNameContextKeyType {}
27+ localClientContextKey = localClientContextKeyType {}
28+ clientContextKey = clientContextKeyType {}
29+ componentContextKey = componentContextKeyType {}
30+ componentNameContextKey = componentNameContextKeyType {}
31+ componentNamespaceContextKey = componentNamespaceContextKeyType {}
32+ componentDigestContextKey = componentDigestContextKeyType {}
2733)
2834
2935type Context interface {
3036 context.Context
3137 WithReconcilerName (reconcilerName string ) Context
38+ WithLocalClient (clnt cluster.Client ) Context
3239 WithClient (clnt cluster.Client ) Context
3340 WithComponent (component Component ) Context
41+ WithComponentName (componentName string ) Context
42+ WithComponentNamespace (componentNamespace string ) Context
3443 WithComponentDigest (componentDigest string ) Context
3544}
3645
@@ -46,6 +55,10 @@ func (c *contextImpl) WithReconcilerName(reconcilerName string) Context {
4655 return & contextImpl {Context : context .WithValue (c , reconcilerNameContextKey , reconcilerName )}
4756}
4857
58+ func (c * contextImpl ) WithLocalClient (clnt cluster.Client ) Context {
59+ return & contextImpl {Context : context .WithValue (c , localClientContextKey , clnt )}
60+ }
61+
4962func (c * contextImpl ) WithClient (clnt cluster.Client ) Context {
5063 return & contextImpl {Context : context .WithValue (c , clientContextKey , clnt )}
5164}
@@ -54,6 +67,14 @@ func (c *contextImpl) WithComponent(component Component) Context {
5467 return & contextImpl {Context : context .WithValue (c , componentContextKey , component )}
5568}
5669
70+ func (c * contextImpl ) WithComponentName (componentName string ) Context {
71+ return & contextImpl {Context : context .WithValue (c , componentNameContextKey , componentName )}
72+ }
73+
74+ func (c * contextImpl ) WithComponentNamespace (componentNamespace string ) Context {
75+ return & contextImpl {Context : context .WithValue (c , componentNamespaceContextKey , componentNamespace )}
76+ }
77+
5778func (c * contextImpl ) WithComponentDigest (componentDigest string ) Context {
5879 return & contextImpl {Context : context .WithValue (c , componentDigestContextKey , componentDigest )}
5980}
@@ -65,20 +86,42 @@ func ReconcilerNameFromContext(ctx context.Context) (string, error) {
6586 return "" , fmt .Errorf ("reconciler name not found in context" )
6687}
6788
89+ func LocalClientFromContext (ctx context.Context ) (cluster.Client , error ) {
90+ if clnt , ok := ctx .Value (localClientContextKey ).(cluster.Client ); ok {
91+ return clnt , nil
92+ }
93+ return nil , fmt .Errorf ("local client not found in context" )
94+ }
95+
6896func ClientFromContext (ctx context.Context ) (cluster.Client , error ) {
6997 if clnt , ok := ctx .Value (clientContextKey ).(cluster.Client ); ok {
7098 return clnt , nil
7199 }
72100 return nil , fmt .Errorf ("client not found in context" )
73101}
74102
103+ // TODO: should this method be parameterized?
75104func ComponentFromContext (ctx context.Context ) (Component , error ) {
76105 if component , ok := ctx .Value (componentContextKey ).(Component ); ok {
77106 return component , nil
78107 }
79108 return nil , fmt .Errorf ("component not found in context" )
80109}
81110
111+ func ComponentNameFromContext (ctx context.Context ) (string , error ) {
112+ if componentName , ok := ctx .Value (componentNameContextKey ).(string ); ok {
113+ return componentName , nil
114+ }
115+ return "" , fmt .Errorf ("component name not found in context" )
116+ }
117+
118+ func ComponentNamespaceFromContext (ctx context.Context ) (string , error ) {
119+ if componentNamespace , ok := ctx .Value (componentNamespaceContextKey ).(string ); ok {
120+ return componentNamespace , nil
121+ }
122+ return "" , fmt .Errorf ("component namespace not found in context" )
123+ }
124+
82125func ComponentDigestFromContext (ctx context.Context ) (string , error ) {
83126 if componentDigest , ok := ctx .Value (componentDigestContextKey ).(string ); ok {
84127 return componentDigest , nil
0 commit comments