Skip to content

Commit

Permalink
feat: Allow configuration of output YAML file extensions (#297) (#300)
Browse files Browse the repository at this point in the history
Fixes #296 

I've retained the current behavior as the default, and added a prop to `App` to allow configuration of the file extensions applied to the outputted YAML.

Please just let me know if you'd like any more information or if I need to make any changes!
Signed-off-by: Christopher Rybicki <rybickic@amazon.com>

Co-authored-by: S Heckler <55748928+heckler1@users.noreply.github.com>
  • Loading branch information
Chriscbr and heckler1 committed Jan 28, 2022
1 parent b3354cb commit e82b443
Show file tree
Hide file tree
Showing 5 changed files with 165 additions and 8 deletions.
37 changes: 37 additions & 0 deletions docs/java.md
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,7 @@ import org.cdk8s.App;

App.Builder.create()
// .outdir(java.lang.String)
// .outputFileExtension(java.lang.String)
// .yamlOutputType(YamlOutputType)
.build();
```
Expand All @@ -217,6 +218,15 @@ The directory to output Kubernetes manifests.

---

##### `outputFileExtension`<sup>Optional</sup> <a name="org.cdk8s.AppProps.parameter.outputFileExtension"></a>

- *Type:* `java.lang.String`
- *Default:* .k8s.yaml

The file extension to use for rendered YAML files.

---

##### `yamlOutputType`<sup>Optional</sup> <a name="org.cdk8s.AppProps.parameter.yamlOutputType"></a>

- *Type:* [`org.cdk8s.YamlOutputType`](#org.cdk8s.YamlOutputType)
Expand Down Expand Up @@ -267,6 +277,19 @@ The output directory into which manifests will be synthesized.

---

##### `outputFileExtension`<sup>Required</sup> <a name="org.cdk8s.App.property.outputFileExtension"></a>

```java
public java.lang.String getOutputFileExtension();
```

- *Type:* `java.lang.String`
- *Default:* .k8s.yaml

The file extension to use for rendered YAML files.

---

##### `yamlOutputType`<sup>Required</sup> <a name="org.cdk8s.App.property.yamlOutputType"></a>

```java
Expand Down Expand Up @@ -780,6 +803,7 @@ import org.cdk8s.AppProps;

AppProps.builder()
// .outdir(java.lang.String)
// .outputFileExtension(java.lang.String)
// .yamlOutputType(YamlOutputType)
.build();
```
Expand All @@ -797,6 +821,19 @@ The directory to output Kubernetes manifests.

---

##### `outputFileExtension`<sup>Optional</sup> <a name="org.cdk8s.AppProps.property.outputFileExtension"></a>

```java
public java.lang.String getOutputFileExtension();
```

- *Type:* `java.lang.String`
- *Default:* .k8s.yaml

The file extension to use for rendered YAML files.

---

##### `yamlOutputType`<sup>Optional</sup> <a name="org.cdk8s.AppProps.property.yamlOutputType"></a>

```java
Expand Down
47 changes: 47 additions & 0 deletions docs/python.md
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,7 @@ import cdk8s

cdk8s.App(
outdir: str = None,
output_file_extension: str = None,
yaml_output_type: YamlOutputType = None
)
```
Expand All @@ -225,6 +226,15 @@ The directory to output Kubernetes manifests.

---

##### `output_file_extension`<sup>Optional</sup> <a name="cdk8s.AppProps.parameter.output_file_extension"></a>

- *Type:* `str`
- *Default:* .k8s.yaml

The file extension to use for rendered YAML files.

---

##### `yaml_output_type`<sup>Optional</sup> <a name="cdk8s.AppProps.parameter.yaml_output_type"></a>

- *Type:* [`cdk8s.YamlOutputType`](#cdk8s.YamlOutputType)
Expand Down Expand Up @@ -275,6 +285,19 @@ The output directory into which manifests will be synthesized.

---

##### `output_file_extension`<sup>Required</sup> <a name="cdk8s.App.property.output_file_extension"></a>

```python
output_file_extension: str
```

- *Type:* `str`
- *Default:* .k8s.yaml

The file extension to use for rendered YAML files.

---

##### `yaml_output_type`<sup>Required</sup> <a name="cdk8s.App.property.yaml_output_type"></a>

```python
Expand Down Expand Up @@ -800,6 +823,7 @@ import cdk8s

cdk8s.AppProps(
outdir: str = None,
output_file_extension: str = None,
yaml_output_type: YamlOutputType = None
)
```
Expand All @@ -817,6 +841,19 @@ The directory to output Kubernetes manifests.

---

##### `output_file_extension`<sup>Optional</sup> <a name="cdk8s.AppProps.property.output_file_extension"></a>

```python
output_file_extension: str
```

- *Type:* `str`
- *Default:* .k8s.yaml

The file extension to use for rendered YAML files.

---

##### `yaml_output_type`<sup>Optional</sup> <a name="cdk8s.AppProps.property.yaml_output_type"></a>

```python
Expand Down Expand Up @@ -2484,6 +2521,7 @@ import cdk8s

cdk8s.Testing.app(
outdir: str = None,
output_file_extension: str = None,
yaml_output_type: YamlOutputType = None
)
```
Expand All @@ -2497,6 +2535,15 @@ The directory to output Kubernetes manifests.

---

###### `output_file_extension`<sup>Optional</sup> <a name="cdk8s.AppProps.parameter.output_file_extension"></a>

- *Type:* `str`
- *Default:* .k8s.yaml

The file extension to use for rendered YAML files.

---

###### `yaml_output_type`<sup>Optional</sup> <a name="cdk8s.AppProps.parameter.yaml_output_type"></a>

- *Type:* [`cdk8s.YamlOutputType`](#cdk8s.YamlOutputType)
Expand Down
26 changes: 26 additions & 0 deletions docs/typescript.md
Original file line number Diff line number Diff line change
Expand Up @@ -231,6 +231,19 @@ The output directory into which manifests will be synthesized.

---

##### `outputFileExtension`<sup>Required</sup> <a name="cdk8s.App.property.outputFileExtension"></a>

```typescript
public readonly outputFileExtension: string;
```

- *Type:* `string`
- *Default:* .k8s.yaml

The file extension to use for rendered YAML files.

---

##### `yamlOutputType`<sup>Required</sup> <a name="cdk8s.App.property.yamlOutputType"></a>

```typescript
Expand Down Expand Up @@ -677,6 +690,19 @@ The directory to output Kubernetes manifests.

---

##### `outputFileExtension`<sup>Optional</sup> <a name="cdk8s.AppProps.property.outputFileExtension"></a>

```typescript
public readonly outputFileExtension: string;
```

- *Type:* `string`
- *Default:* .k8s.yaml

The file extension to use for rendered YAML files.

---

##### `yamlOutputType`<sup>Optional</sup> <a name="cdk8s.AppProps.property.yamlOutputType"></a>

```typescript
Expand Down
29 changes: 21 additions & 8 deletions src/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,13 @@ export interface AppProps {
* @default - CDK8S_OUTDIR if defined, otherwise "dist"
*/
readonly outdir?: string;
/** How to divide the YAML output into files
/**
* The file extension to use for rendered YAML files
* @default .k8s.yaml
*/
readonly outputFileExtension?: string;
/**
* How to divide the YAML output into files
* @default YamlOutputType.FILE_PER_CHART
*/
readonly yamlOutputType?: YamlOutputType;
Expand Down Expand Up @@ -82,6 +88,12 @@ export class App extends Construct {
*/
public readonly outdir: string;

/**
* The file extension to use for rendered YAML files
* @default .k8s.yaml
*/
public readonly outputFileExtension: string;

/** How to divide the YAML output into files
* @default YamlOutputType.FILE_PER_CHART
*/
Expand All @@ -104,6 +116,7 @@ export class App extends Construct {
constructor(props: AppProps = { }) {
super(undefined as any, '');
this.outdir = props.outdir ?? process.env.CDK8S_OUTDIR ?? 'dist';
this.outputFileExtension = props.outputFileExtension ?? '.k8s.yaml';
this.yamlOutputType = props.yamlOutputType ?? YamlOutputType.FILE_PER_CHART;
}

Expand Down Expand Up @@ -133,7 +146,7 @@ export class App extends Construct {

if (charts.length > 0) {
Yaml.save(
path.join(this.outdir, 'app.k8s.yaml'), // There is no "app name", so we just hardcode the file name
path.join(this.outdir, `app${this.outputFileExtension}`), // There is no "app name", so we just hardcode the file name
apiObjectList.map((apiObject) => apiObject.toJson()),
);
}
Expand All @@ -146,7 +159,7 @@ export class App extends Construct {
const chartName = namer.name(chart);
const objects = chartToKube(chart);

Yaml.save(path.join(this.outdir, chartName), objects.map(obj => obj.toJson()));
Yaml.save(path.join(this.outdir, chartName+this.outputFileExtension), objects.map(obj => obj.toJson()));
}
break;

Expand All @@ -157,8 +170,8 @@ export class App extends Construct {
apiObjects.forEach((apiObject) => {
if (!(apiObject === undefined)) {
const fileName = `${`${apiObject.kind}.${apiObject.metadata.name}`
.replace(/[^0-9a-zA-Z-_.]/g, '')}.k8s.yaml`;
Yaml.save(path.join(this.outdir, fileName), [apiObject.toJson()]);
.replace(/[^0-9a-zA-Z-_.]/g, '')}`;
Yaml.save(path.join(this.outdir, fileName+this.outputFileExtension), [apiObject.toJson()]);
}
});
}
Expand All @@ -175,8 +188,8 @@ export class App extends Construct {
apiObjects.forEach((apiObject) => {
if (!(apiObject === undefined)) {
const fileName = `${`${apiObject.kind}.${apiObject.metadata.name}`
.replace(/[^0-9a-zA-Z-_.]/g, '')}.k8s.yaml`;
Yaml.save(path.join(fullOutDir, fileName), [apiObject.toJson()]);
.replace(/[^0-9a-zA-Z-_.]/g, '')}`;
Yaml.save(path.join(fullOutDir, fileName+this.outputFileExtension), [apiObject.toJson()]);
}
});
}
Expand Down Expand Up @@ -279,7 +292,7 @@ class SimpleChartNamer implements ChartNamer {
}

public name(chart: Chart) {
return `${Names.toDnsLabel(chart)}.k8s.yaml`;
return `${Names.toDnsLabel(chart)}`;
}
}

Expand Down
34 changes: 34 additions & 0 deletions test/app.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -424,6 +424,40 @@ test('apps with varying yamlOutputTypes; chart dependencies via custom construct
}
});

test('Modified file extensions with varying output types; two charts, no objects', () => {
const testSpecs = [
{
props: { yamlOutputType: YamlOutputType.FILE_PER_CHART, outputFileExtension: '.yaml' },
result: ['chart1.yaml', 'chart2.yaml'],
},
{
props: { yamlOutputType: YamlOutputType.FILE_PER_APP, outputFileExtension: '.yaml' },
result: ['app.yaml'],
},
{
props: { yamlOutputType: YamlOutputType.FILE_PER_RESOURCE, outputFileExtension: '.yaml' },
result: [],
},
{
props: { yamlOutputType: YamlOutputType.FOLDER_PER_CHART_FILE_PER_RESOURCE, outputFileExtension: '.yaml' },
result: ['chart1', 'chart2'],
},
];
for (const testSpec of testSpecs) {
// GIVEN
const app = Testing.app(testSpec.props);

// WHEN
new Chart(app, 'chart1');
new Chart(app, 'chart2');
app.synth();

// THEN
expect(fs.readdirSync(app.outdir)).toEqual(testSpec.result);
}
});


/**
* Get the list of files and folders in the source folder and sub folders (one level deep)
* @param sourceDir Folder in which to search for files and folders
Expand Down

0 comments on commit e82b443

Please sign in to comment.