Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
/*
* Copyright 2018 Google LLC. All rights reserved.
*
* Licensed 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 com.google.cloud.tools.skaffold.yaml;

import com.google.common.base.Preconditions;
import com.google.common.collect.ImmutableList;

/**
* Automatically generates contents of a skaffold.yaml from a provided set of Kubernetes manifests.
*/
public class SkaffoldYamlGenerator {

private final ImmutableList<String> manifestPaths;

/**
* Creates a new {@link SkaffoldYamlGenerator}.
*
* @param manifestPaths a non-empty list of paths to Kubernetes yamls (may include glob patterns)
*/
public SkaffoldYamlGenerator(ImmutableList<String> manifestPaths) {
Preconditions.checkArgument(manifestPaths.size() > 0);
this.manifestPaths = manifestPaths;
}

/**
* Generates the skaffold.yaml contents.
*
* @return the skaffold.yaml contents as a string
*/
public String generate() {
StringBuilder output = new StringBuilder();
output.append("apiVersion: skaffold/v1alpha2\n");
output.append("kind: Config\n");
output.append("deploy:\n");
output.append(" kubectl:\n");

// Add manifests
output.append(" manifests:\n");
for (String manifestPath : manifestPaths) {
output.append(" - ");
output.append(manifestPath);
output.append("\n");
}

return output.toString();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
/*
* Copyright 2018 Google LLC. All rights reserved.
*
* Licensed 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 com.google.cloud.tools.skaffold.yaml;

import com.google.common.base.Joiner;
import com.google.common.collect.ImmutableList;
import com.google.common.io.Resources;
import java.io.IOException;
import java.net.URISyntaxException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.List;
import org.junit.Assert;
import org.junit.Test;

/** Tests for {@link SkaffoldYamlGenerator}. */
public class SkaffoldYamlGeneratorTest {

@Test
public void testGenerate() throws URISyntaxException, IOException {
// Read all lines and join with \n to avoid Windows test failing
List<String> lines =
Files.readAllLines(
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should include charset here.

Paths.get(Resources.getResource("SkaffoldYamlGeneratorTest/generated.yaml").toURI()),
StandardCharsets.UTF_8);
String expected = Joiner.on('\n').join(lines) + "\n";

ImmutableList<String> paths = ImmutableList.of("MANIFEST_PATH_1", "MANIFEST_PATH_2");
SkaffoldYamlGenerator generator = new SkaffoldYamlGenerator(paths);

Assert.assertEquals(expected, generator.generate());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
apiVersion: skaffold/v1alpha2
kind: Config
deploy:
kubectl:
manifests:
- MANIFEST_PATH_1
- MANIFEST_PATH_2