Skip to content

Commit 081c585

Browse files
authored
Merge pull request #7228 from neoaggelos/feat/stdin-source
🌱 support clusterctl generate cluster with templates from stdin
2 parents b1018b2 + 93ea384 commit 081c585

File tree

4 files changed

+36
-3
lines changed

4 files changed

+36
-3
lines changed

cmd/clusterctl/client/cluster/template.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ package cluster
1919
import (
2020
"context"
2121
"encoding/base64"
22+
"io"
2223
"net/http"
2324
"net/url"
2425
"os"
@@ -129,6 +130,14 @@ func (t *templateClient) GetFromURL(templateURL, targetNamespace string, skipTem
129130
}
130131

131132
func (t *templateClient) getURLContent(templateURL string) ([]byte, error) {
133+
if templateURL == "-" {
134+
b, err := io.ReadAll(os.Stdin)
135+
if err != nil {
136+
return nil, errors.Wrapf(err, "failed to read stdin")
137+
}
138+
return b, nil
139+
}
140+
132141
rURL, err := url.Parse(templateURL)
133142
if err != nil {
134143
return nil, errors.Wrapf(err, "failed to parse %q", templateURL)

cmd/clusterctl/client/cluster/template_test.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -307,6 +307,12 @@ func Test_templateClient_GetFromURL(t *testing.T) {
307307
path := filepath.Join(tmpDir, "cluster-template.yaml")
308308
g.Expect(os.WriteFile(path, []byte(template), 0600)).To(Succeed())
309309

310+
// redirect stdin
311+
saveStdin := os.Stdin
312+
defer func() { os.Stdin = saveStdin }()
313+
os.Stdin, err = os.Open(path)
314+
g.Expect(err).NotTo(HaveOccurred())
315+
310316
type args struct {
311317
templateURL string
312318
targetNamespace string
@@ -338,6 +344,16 @@ func Test_templateClient_GetFromURL(t *testing.T) {
338344
want: template,
339345
wantErr: false,
340346
},
347+
{
348+
name: "Get from stdin",
349+
args: args{
350+
templateURL: "-",
351+
targetNamespace: "",
352+
skipTemplateProcess: false,
353+
},
354+
want: template,
355+
wantErr: false,
356+
},
341357
}
342358
for _, tt := range tests {
343359
t.Run(tt.name, func(t *testing.T) {

cmd/clusterctl/cmd/generate_cluster.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ func init() {
124124

125125
// flags for the url source
126126
generateClusterClusterCmd.Flags().StringVar(&gc.url, "from", "",
127-
"The URL to read the workload cluster template from. If unspecified, the infrastructure provider repository URL will be used")
127+
"The URL to read the workload cluster template from. If unspecified, the infrastructure provider repository URL will be used. If set to '-', the workload cluster template is read from stdin.")
128128

129129
// flags for the config map source
130130
generateClusterClusterCmd.Flags().StringVar(&gc.configMapName, "from-config-map", "",

docs/book/src/clusterctl/commands/generate-cluster.md

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -69,9 +69,10 @@ clusterctl generate cluster my-cluster --kubernetes-version v1.16.3 \
6969
Also following flags are available `--from-config-map-namespace` (defaults to current namespace) and `--from-config-map-key`
7070
(defaults to `template`).
7171

72-
#### GitHub or local file system folder
72+
#### GitHub, local file system folder or standard input
7373

74-
Use the `--from` flag to read cluster templates stored in a GitHub repository or in a local file system folder; e.g.
74+
Use the `--from` flag to read cluster templates stored in a GitHub repository, in a local file system folder,
75+
or from the standard input; e.g.
7576

7677
```bash
7778
clusterctl generate cluster my-cluster --kubernetes-version v1.16.3 \
@@ -85,6 +86,13 @@ clusterctl generate cluster my-cluster --kubernetes-version v1.16.3 \
8586
--from ~/my-template.yaml > my-cluster.yaml
8687
```
8788

89+
or
90+
91+
```bash
92+
cat ~/my-template.yaml | clusterctl generate cluster my-cluster --kubernetes-version v1.16.3 \
93+
--from - > my-cluster.yaml
94+
```
95+
8896
### Variables
8997

9098
If the selected cluster template expects some environment variables, the user should ensure those variables are set in advance.

0 commit comments

Comments
 (0)