Skip to content

Commit e7af183

Browse files
committed
feat(kubernetes): Add ServiceAccount provisioning support
1 parent d35767d commit e7af183

File tree

5 files changed

+48
-6
lines changed

5 files changed

+48
-6
lines changed

halyard-deploy/src/main/java/com/netflix/spinnaker/halyard/deploy/deployment/v1/KubectlDeployer.java

+6
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,8 @@ public RemoteAction deploy(
8787
String namespaceDefinition =
8888
service.getNamespaceYaml(resolvedConfiguration);
8989
String serviceDefinition = service.getServiceYaml(resolvedConfiguration);
90+
String serviceAccountDefinition =
91+
service.getServiceAccountYaml(resolvedConfiguration);
9092

9193
if (!executor.exists(namespaceDefinition)) {
9294
executor.apply(namespaceDefinition);
@@ -96,6 +98,10 @@ public RemoteAction deploy(
9698
executor.apply(serviceDefinition);
9799
}
98100

101+
if (!executor.exists(serviceAccountDefinition)) {
102+
executor.apply(serviceAccountDefinition);
103+
}
104+
99105
String resourceDefinition =
100106
service.getResourceYaml(
101107
executor, deploymentDetails, resolvedConfiguration);

halyard-deploy/src/main/java/com/netflix/spinnaker/halyard/deploy/spinnaker/v1/service/KubernetesSettings.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,8 @@ public class KubernetesSettings {
3131
Map<String, String> podAnnotations = new HashMap<>();
3232
Map<String, String> podLabels = new HashMap<>();
3333
Map<String, String> serviceLabels = new HashMap<>();
34+
Map<String, String> serviceAccountAnnotations = new HashMap<>();
3435
List<ConfigSource> volumes = new ArrayList<>();
35-
String serviceAccountName = null;
3636
String serviceType = "ClusterIP";
3737
String nodePort = null;
3838
Boolean useExecHealthCheck = true;

halyard-deploy/src/main/java/com/netflix/spinnaker/halyard/deploy/spinnaker/v1/service/distributed/kubernetes/v2/KubernetesV2Service.java

+13-1
Original file line numberDiff line numberDiff line change
@@ -193,6 +193,18 @@ default String getResourceYaml(
193193
.toString();
194194
}
195195

196+
default String getServiceAccountYaml(
197+
GenerateService.ResolvedConfiguration resolvedConfiguration) {
198+
ServiceSettings settings = resolvedConfiguration.getServiceSettings(getService());
199+
String namespace = getNamespace(settings);
200+
return new JinjaJarResource("/kubernetes/manifests/serviceAccount.yml")
201+
.addBinding("name", getService().getCanonicalName())
202+
.addBinding("namespace", getNamespace(settings))
203+
.addBinding(
204+
"serviceAccountAnnotations", settings.getKubernetes().getServiceAccountAnnotations())
205+
.toString();
206+
}
207+
196208
default String getPodSpecYaml(
197209
KubernetesV2Executor executor,
198210
AccountDeploymentDetails<KubernetesAccount> details,
@@ -250,7 +262,7 @@ default String getPodSpecYaml(
250262
.addBinding("initContainers", getInitContainers(details))
251263
.addBinding("hostAliases", getHostAliases(details))
252264
.addBinding("imagePullSecrets", settings.getKubernetes().getImagePullSecrets())
253-
.addBinding("serviceAccountName", settings.getKubernetes().getServiceAccountName())
265+
.addBinding("serviceAccountName", getService().getCanonicalName())
254266
.addBinding("terminationGracePeriodSeconds", terminationGracePeriodSeconds())
255267
.addBinding("nodeSelector", settings.getKubernetes().getNodeSelector())
256268
.addBinding("affinity", getAffinity(details))
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
apiVersion: v1
2+
kind: ServiceAccount
3+
metadata:
4+
name: spin-{{ name }}
5+
namespace: {{ namespace }}
6+
labels:
7+
app: spin
8+
cluster: spin-{{ name }}
9+
annotations: {
10+
{% for key, value in serviceAccountAnnotations.items() %}
11+
"{{ key }}": "{{ value }}"{% if not loop.last %}, {% endif %}
12+
{% endfor %}}

halyard-deploy/src/test/groovy/com/netflix/spinnaker/halyard/deploy/spinnaker/v1/service/distributed/kubernetes/v2/KubernetesV2ServiceTest.groovy

+16-4
Original file line numberDiff line numberDiff line change
@@ -395,15 +395,27 @@ class KubernetesV2ServiceTest extends Specification {
395395
yaml.contains('"tolerations": [{"key":"test","operator":"Equal","value":"a","effect":"NoSchedule"}]')
396396
}
397397

398-
def "Can we set ServiceAccountNames"() {
398+
def "Does the serviceAccountName get set correctly?"() {
399399
setup:
400400
def executor = Mock(KubernetesV2Executor)
401-
serviceSettings.getKubernetes().serviceAccountName = "customServiceAccount"
402401

403402
when:
404403
String podSpecYaml = testService.getPodSpecYaml(executor, details, config)
405404

406405
then:
407-
podSpecYaml.contains('"serviceAccountName": customServiceAccount')
406+
podSpecYaml.contains('"serviceAccountName": orca')
408407
}
409-
}
408+
409+
def "Can we set ServiceAccount.serviceAccountAnnotations?"() {
410+
setup:
411+
serviceSettings.getKubernetes().serviceAccountAnnotations = [
412+
"example-service-account-annotation": "test"
413+
]
414+
415+
when:
416+
String yaml = testService.getServiceAccountYaml(config)
417+
418+
then:
419+
yaml.matches(/(?ms).+annotations: \{.+"example-service-account-annotation": "test".+\}.*/)
420+
}
421+
}

0 commit comments

Comments
 (0)