forked from fabric8io/fabric8-maven-plugin
-
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.
Fix fabric8io#931: Add ServiceAccount enricher and configuration
- Loading branch information
1 parent
290152e
commit a41f727
Showing
9 changed files
with
249 additions
and
2 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
39 changes: 39 additions & 0 deletions
39
core/src/main/java/io/fabric8/maven/core/config/ServiceAccountConfig.java
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,39 @@ | ||
/** | ||
* Copyright 2016 Red Hat, Inc. | ||
* | ||
* Red Hat licenses this file to you under the Apache License, version | ||
* 2.0 (the "License"); you may not use this file except in compliance | ||
* with the License. You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or | ||
* implied. See the License for the specific language governing | ||
* permissions and limitations under the License. | ||
*/ | ||
|
||
package io.fabric8.maven.core.config; | ||
|
||
import org.apache.maven.plugins.annotations.Parameter; | ||
|
||
public class ServiceAccountConfig { | ||
@Parameter | ||
private String name; | ||
|
||
@Parameter | ||
private String deploymentRef; | ||
|
||
public ServiceAccountConfig(String name) { | ||
this.name = name; | ||
} | ||
|
||
public String getName() { | ||
return name; | ||
} | ||
|
||
public String getDeploymentRef() { | ||
return deploymentRef; | ||
} | ||
} |
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
106 changes: 106 additions & 0 deletions
106
...her/standard/src/main/java/io/fabric8/maven/enricher/standard/ServiceAccountEnricher.java
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,106 @@ | ||
/** | ||
* Copyright 2016 Red Hat, Inc. | ||
* | ||
* Red Hat licenses this file to you under the Apache License, version | ||
* 2.0 (the "License"); you may not use this file except in compliance | ||
* with the License. You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or | ||
* implied. See the License for the specific language governing | ||
* permissions and limitations under the License. | ||
*/ | ||
|
||
package io.fabric8.maven.enricher.standard; | ||
|
||
import io.fabric8.kubernetes.api.builder.TypedVisitor; | ||
import io.fabric8.kubernetes.api.model.HasMetadata; | ||
import io.fabric8.kubernetes.api.model.KubernetesListBuilder; | ||
import io.fabric8.kubernetes.api.model.ServiceAccount; | ||
import io.fabric8.kubernetes.api.model.ServiceAccountBuilder; | ||
import io.fabric8.kubernetes.api.model.apps.DeploymentBuilder; | ||
import io.fabric8.maven.core.config.ResourceConfig; | ||
import io.fabric8.maven.core.config.ServiceAccountConfig; | ||
import io.fabric8.maven.enricher.api.BaseEnricher; | ||
import io.fabric8.maven.enricher.api.MavenEnricherContext; | ||
|
||
import java.util.ArrayList; | ||
import java.util.HashMap; | ||
import java.util.List; | ||
import java.util.Map; | ||
|
||
public class ServiceAccountEnricher extends BaseEnricher { | ||
public ServiceAccountEnricher(MavenEnricherContext enricherContext) { | ||
super(enricherContext, "fmp-serviceaccount"); | ||
} | ||
|
||
@Override | ||
public void addMissingResources(KubernetesListBuilder builder) { | ||
Map<String, String> deploymentToSaPair = new HashMap<>(); | ||
List<ServiceAccount> serviceAccounts = new ArrayList<>(); | ||
|
||
// Check XML config and see if there are any service accounts specified | ||
ResourceConfig xmlResourceConfig = getConfiguration().getResource().orElse(null); | ||
if(xmlResourceConfig != null && xmlResourceConfig.getServiceAccounts() != null) { | ||
for(ServiceAccountConfig serviceAccountConfig : xmlResourceConfig.getServiceAccounts()) { | ||
if(serviceAccountConfig.getName() != null) { | ||
serviceAccounts.add(createServiceAccount(builder, serviceAccountConfig.getName())); | ||
} | ||
if(serviceAccountConfig.getDeploymentRef() != null) { | ||
deploymentToSaPair.put(serviceAccountConfig.getDeploymentRef(), serviceAccountConfig.getName()); | ||
} | ||
} | ||
} | ||
|
||
// If any service account is referenced in deployment spec, then | ||
// create sa on fly. | ||
builder.accept(new TypedVisitor<DeploymentBuilder>() { | ||
@Override | ||
public void visit(DeploymentBuilder deploymentBuilder) { | ||
String serviceAccountName = getServiceAccountNameFromSpec(deploymentBuilder); | ||
if(serviceAccountName != null && getServiceAccountFromList(builder, serviceAccountName) == null) { | ||
serviceAccounts.add(createServiceAccount(builder, serviceAccountName)); | ||
} | ||
if(deploymentToSaPair.containsKey(deploymentBuilder.buildMetadata().getName())) { | ||
deploymentBuilder.editSpec() | ||
.editTemplate() | ||
.editSpec() | ||
.withServiceAccountName(deploymentToSaPair.get(deploymentBuilder.buildMetadata().getName())) | ||
.endSpec() | ||
.endTemplate() | ||
.endSpec(); | ||
} | ||
} | ||
}); | ||
|
||
builder.addAllToServiceAccountItems(serviceAccounts); | ||
} | ||
|
||
private ServiceAccount createServiceAccount(KubernetesListBuilder builder, String serviceAccountName) { | ||
return new ServiceAccountBuilder() | ||
.withNewMetadata().withName(serviceAccountName).endMetadata() | ||
.build(); | ||
} | ||
|
||
private String getServiceAccountNameFromSpec(DeploymentBuilder builder) { | ||
if(builder.buildSpec().getTemplate().getSpec().getServiceAccountName() != null) { | ||
return builder.buildSpec().getTemplate().getSpec().getServiceAccountName(); | ||
} | ||
if(builder.buildSpec().getTemplate().getSpec().getServiceAccount() != null) { | ||
return builder.buildSpec().getTemplate().getSpec().getServiceAccount(); | ||
} | ||
return null; | ||
} | ||
|
||
private ServiceAccount getServiceAccountFromList(KubernetesListBuilder builder, String serviceAccountName) { | ||
for(HasMetadata item : builder.buildItems()) { | ||
if(item instanceof ServiceAccount && item.getMetadata().getName().equals(serviceAccountName)) { | ||
return (ServiceAccount)item; | ||
} | ||
} | ||
return null; | ||
} | ||
} |
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
76 changes: 76 additions & 0 deletions
76
...standard/src/test/java/io/fabric8/maven/enricher/standard/ServiceAccountEnricherTest.java
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,76 @@ | ||
/** | ||
* Copyright 2016 Red Hat, Inc. | ||
* | ||
* Red Hat licenses this file to you under the Apache License, version | ||
* 2.0 (the "License"); you may not use this file except in compliance | ||
* with the License. You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or | ||
* implied. See the License for the specific language governing | ||
* permissions and limitations under the License. | ||
*/ | ||
package io.fabric8.maven.enricher.standard; | ||
|
||
import io.fabric8.kubernetes.api.model.KubernetesListBuilder; | ||
import io.fabric8.kubernetes.api.model.ServiceAccount; | ||
import io.fabric8.kubernetes.api.model.apps.DeploymentBuilder; | ||
import io.fabric8.maven.core.config.ResourceConfig; | ||
import io.fabric8.maven.core.config.ServiceAccountConfig; | ||
import io.fabric8.maven.core.model.Configuration; | ||
import io.fabric8.maven.enricher.api.MavenEnricherContext; | ||
import mockit.Expectations; | ||
import mockit.Mocked; | ||
import org.junit.Test; | ||
|
||
import java.util.Collections; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
|
||
public class ServiceAccountEnricherTest { | ||
@Mocked | ||
private MavenEnricherContext context; | ||
|
||
@Override | ||
protected Object clone() throws CloneNotSupportedException { | ||
return super.clone(); | ||
} | ||
|
||
@Test | ||
public void testServiceAccountCreationFromConfig() { | ||
new Expectations() {{ | ||
context.getConfiguration(); | ||
result = new Configuration.Builder() | ||
.resource(new ResourceConfig.Builder() | ||
.withServiceAccounts(Collections.singletonList(new ServiceAccountConfig("ribbon"))).build()) | ||
.build(); | ||
}}; | ||
final KubernetesListBuilder builder = new KubernetesListBuilder(); | ||
|
||
enrichAndAssert(builder); | ||
} | ||
|
||
@Test | ||
public void testServiceAccountCreationFromFragment() { | ||
final KubernetesListBuilder builder = new KubernetesListBuilder() | ||
.withItems(new DeploymentBuilder().withNewMetadata().withName("cheese").endMetadata() | ||
.withNewSpec().withNewTemplate().withNewSpec() | ||
.addNewContainer().withImage("cheese-image").endContainer() | ||
.withServiceAccount("ribbon") | ||
.endSpec().endTemplate().endSpec().build()); | ||
|
||
enrichAndAssert(builder); | ||
} | ||
|
||
private void enrichAndAssert(KubernetesListBuilder builder) { | ||
final ServiceAccountEnricher saEnricher = new ServiceAccountEnricher(context); | ||
saEnricher.addMissingResources(builder); | ||
|
||
final ServiceAccount serviceAccount = (ServiceAccount) builder.buildLastItem(); | ||
assertThat(serviceAccount).isNotNull(); | ||
assertThat(serviceAccount.getMetadata().getName()).isEqualTo("ribbon"); | ||
} | ||
} |
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