1212using Newtonsoft . Json ;
1313using Octopus . Tentacle . Configuration ;
1414using Octopus . Tentacle . Configuration . Instances ;
15+ using Octopus . Tentacle . Contracts ;
1516using Octopus . Tentacle . Contracts . KubernetesScriptServiceV1 ;
1617using Octopus . Tentacle . Core . Diagnostics ;
1718using Octopus . Tentacle . Core . Services . Scripts . Locking ;
@@ -205,7 +206,7 @@ async Task CreatePod(StartKubernetesScriptCommandV1 command, IScriptWorkspace wo
205206 Name = podName ,
206207 NamespaceProperty = KubernetesConfig . Namespace ,
207208 Labels = Merge ( scriptPodTemplate ? . Spec . PodMetadata ? . Labels , GetScriptPodLabels ( tentacleScriptLog , command ) ) ,
208- Annotations = Merge ( scriptPodTemplate ? . Spec . PodMetadata ? . Annotations , ParseScriptPodAnnotations ( tentacleScriptLog ) )
209+ Annotations = Merge ( scriptPodTemplate ? . Spec . PodMetadata ? . Annotations , GetScriptPodAnnotations ( tentacleScriptLog , command ) )
209210 } ,
210211 //if the script pod template spec has been defined, use that
211212 Spec = scriptPodTemplate ? . Spec . PodSpec ?? new V1PodSpec
@@ -380,7 +381,7 @@ protected V1ResourceRequirements GetScriptPodResourceRequirements(InMemoryTentac
380381 var message = $ "Failed to deserialize env.{ KubernetesConfig . PodResourceJsonVariableName } into valid pod resource requirements.{ Environment . NewLine } JSON value: { json } { Environment . NewLine } Using default resource requests for script pod.";
381382 //if we can't parse the JSON, fall back to the defaults below and warn the user
382383 log . WarnFormat ( e , message ) ;
383- //write a verbose message to the script log.
384+ //write a verbose message to the script log.
384385 tentacleScriptLog . Verbose ( message ) ;
385386 }
386387 }
@@ -433,7 +434,14 @@ V1Affinity ParseScriptPodAffinity(InMemoryTentacleScriptLog tentacleScriptLog)
433434 KubernetesConfig . PodAnnotationsJsonVariableName ,
434435 "pod annotations" ) ;
435436
436- Dictionary < string , string > ? GetScriptPodLabels ( InMemoryTentacleScriptLog tentacleScriptLog , StartKubernetesScriptCommandV1 command )
437+ Dictionary < string , string > ? GetScriptPodAnnotations ( InMemoryTentacleScriptLog tentacleScriptLog , StartKubernetesScriptCommandV1 command )
438+ {
439+ var annotations = ParseScriptPodAnnotations ( tentacleScriptLog ) ?? new Dictionary < string , string > ( ) ;
440+ annotations . AddRange ( GetAuthContext ( command ) ) ;
441+ return annotations ;
442+ }
443+
444+ Dictionary < string , string > GetScriptPodLabels ( InMemoryTentacleScriptLog tentacleScriptLog , StartKubernetesScriptCommandV1 command )
437445 {
438446 var labels = new Dictionary < string , string >
439447 {
@@ -451,9 +459,54 @@ V1Affinity ParseScriptPodAffinity(InMemoryTentacleScriptLog tentacleScriptLog)
451459 labels . AddRange ( extraLabels ) ;
452460 }
453461
462+ labels . Add ( $ "{ KubernetesConfig . AgentLabelNamespace } /permissions", "enabled" ) ;
463+ labels . AddRange ( GetAuthContext ( command , true ) ) ;
464+
454465 return labels ;
455466 }
456467
468+ static Dictionary < string , string > GetAuthContext ( StartKubernetesScriptCommandV1 command , bool hash = false )
469+ {
470+ var dict = new Dictionary < string , string > ( ) ;
471+
472+ if ( command . AuthContext is null )
473+ {
474+ return dict ;
475+ }
476+
477+ dict [ $ "{ KubernetesConfig . AgentLabelNamespace } /project"] = hash
478+ ? HashValue ( command . AuthContext . ProjectSlug )
479+ : command . AuthContext . ProjectSlug ;
480+
481+ dict [ $ "{ KubernetesConfig . AgentLabelNamespace } /environment"] = hash
482+ ? HashValue ( command . AuthContext . EnvironmentSlug )
483+ : command . AuthContext . EnvironmentSlug ;
484+
485+ if ( command . AuthContext . TenantSlug is not null )
486+ {
487+ dict [ $ "{ KubernetesConfig . AgentLabelNamespace } /tenant"] = hash
488+ ? HashValue ( command . AuthContext . TenantSlug )
489+ : command . AuthContext . TenantSlug ;
490+ }
491+
492+ dict [ $ "{ KubernetesConfig . AgentLabelNamespace } /step"] = hash
493+ ? HashValue ( command . AuthContext . StepSlug )
494+ : command . AuthContext . StepSlug ;
495+
496+ dict [ $ "{ KubernetesConfig . AgentLabelNamespace } /space"] = hash
497+ ? HashValue ( command . AuthContext . SpaceSlug )
498+ : command . AuthContext . SpaceSlug ;
499+
500+ return dict ;
501+ }
502+
503+ static string HashValue ( string value )
504+ {
505+ using var sha1 = SHA1 . Create ( ) ;
506+ var bytes = sha1 . ComputeHash ( Encoding . UTF8 . GetBytes ( value ) ) ;
507+ return BitConverter . ToString ( bytes ) . Replace ( "-" , "" ) ;
508+ }
509+
457510 [ return : NotNullIfNotNull ( "defaultValue" ) ]
458511 T ? ParseScriptPodJson < T > ( InMemoryTentacleScriptLog tentacleScriptLog , string ? json , string envVarName , string description , T ? defaultValue = null ) where T : class
459512 {
@@ -472,7 +525,7 @@ V1Affinity ParseScriptPodAffinity(InMemoryTentacleScriptLog tentacleScriptLog)
472525
473526 //if we can't parse the JSON, fall back to the defaults below and warn the user
474527 log . WarnFormat ( e , message ) ;
475- //write a verbose message to the script log.
528+ //write a verbose message to the script log.
476529 tentacleScriptLog . Verbose ( message ) ;
477530 }
478531
0 commit comments