Skip to content

Commit c48e1e4

Browse files
authored
Add SkaffoldYamlGenerator (#16)
1 parent 7cf17bd commit c48e1e4

3 files changed

Lines changed: 116 additions & 0 deletions

File tree

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
/*
2+
* Copyright 2018 Google LLC. All rights reserved.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License"); you may not
5+
* use this file except in compliance with the License. You may obtain a copy of
6+
* the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12+
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13+
* License for the specific language governing permissions and limitations under
14+
* the License.
15+
*/
16+
17+
package com.google.cloud.tools.skaffold.yaml;
18+
19+
import com.google.common.base.Preconditions;
20+
import com.google.common.collect.ImmutableList;
21+
22+
/**
23+
* Automatically generates contents of a skaffold.yaml from a provided set of Kubernetes manifests.
24+
*/
25+
public class SkaffoldYamlGenerator {
26+
27+
private final ImmutableList<String> manifestPaths;
28+
29+
/**
30+
* Creates a new {@link SkaffoldYamlGenerator}.
31+
*
32+
* @param manifestPaths a non-empty list of paths to Kubernetes yamls (may include glob patterns)
33+
*/
34+
public SkaffoldYamlGenerator(ImmutableList<String> manifestPaths) {
35+
Preconditions.checkArgument(manifestPaths.size() > 0);
36+
this.manifestPaths = manifestPaths;
37+
}
38+
39+
/**
40+
* Generates the skaffold.yaml contents.
41+
*
42+
* @return the skaffold.yaml contents as a string
43+
*/
44+
public String generate() {
45+
StringBuilder output = new StringBuilder();
46+
output.append("apiVersion: skaffold/v1alpha2\n");
47+
output.append("kind: Config\n");
48+
output.append("deploy:\n");
49+
output.append(" kubectl:\n");
50+
51+
// Add manifests
52+
output.append(" manifests:\n");
53+
for (String manifestPath : manifestPaths) {
54+
output.append(" - ");
55+
output.append(manifestPath);
56+
output.append("\n");
57+
}
58+
59+
return output.toString();
60+
}
61+
}
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
/*
2+
* Copyright 2018 Google LLC. All rights reserved.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License"); you may not
5+
* use this file except in compliance with the License. You may obtain a copy of
6+
* the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12+
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13+
* License for the specific language governing permissions and limitations under
14+
* the License.
15+
*/
16+
17+
package com.google.cloud.tools.skaffold.yaml;
18+
19+
import com.google.common.base.Joiner;
20+
import com.google.common.collect.ImmutableList;
21+
import com.google.common.io.Resources;
22+
import java.io.IOException;
23+
import java.net.URISyntaxException;
24+
import java.nio.charset.StandardCharsets;
25+
import java.nio.file.Files;
26+
import java.nio.file.Paths;
27+
import java.util.List;
28+
import org.junit.Assert;
29+
import org.junit.Test;
30+
31+
/** Tests for {@link SkaffoldYamlGenerator}. */
32+
public class SkaffoldYamlGeneratorTest {
33+
34+
@Test
35+
public void testGenerate() throws URISyntaxException, IOException {
36+
// Read all lines and join with \n to avoid Windows test failing
37+
List<String> lines =
38+
Files.readAllLines(
39+
Paths.get(Resources.getResource("SkaffoldYamlGeneratorTest/generated.yaml").toURI()),
40+
StandardCharsets.UTF_8);
41+
String expected = Joiner.on('\n').join(lines) + "\n";
42+
43+
ImmutableList<String> paths = ImmutableList.of("MANIFEST_PATH_1", "MANIFEST_PATH_2");
44+
SkaffoldYamlGenerator generator = new SkaffoldYamlGenerator(paths);
45+
46+
Assert.assertEquals(expected, generator.generate());
47+
}
48+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
apiVersion: skaffold/v1alpha2
2+
kind: Config
3+
deploy:
4+
kubectl:
5+
manifests:
6+
- MANIFEST_PATH_1
7+
- MANIFEST_PATH_2

0 commit comments

Comments
 (0)