Skip to content

Commit 9f7f265

Browse files
author
Aatman
authored
feat: multiple configs (#1)
* feat: multiple configs * chore: remove temp file at the end
1 parent 21b83ab commit 9f7f265

File tree

9 files changed

+610
-21
lines changed

9 files changed

+610
-21
lines changed

README.md

Lines changed: 120 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,16 +18,18 @@ $ pulumi plugin install resource kubernetes v2.4.2
1818

1919
## Usage
2020

21+
```
22+
$ go test
23+
$ go build
24+
```
2125
```
2226
$ printf 'apiVersion: v1
2327
kind: ServiceAccount
2428
metadata:
2529
name: my-service-account
2630
namespace: my-namespace' > temp.yaml
2731
```
28-
2932
```
30-
$ go build
3133
$ ./kube2cdk8s typescript -f temp.yaml
3234
new cdk8s.ApiObject("", this, {
3335
apiVersion: "v1",
@@ -38,3 +40,119 @@ new cdk8s.ApiObject("", this, {
3840
},
3941
});
4042
```
43+
```
44+
printf '---
45+
apiVersion: apps/v1
46+
kind: Deployment
47+
metadata:
48+
name: my-deployment
49+
namespace: my-namespace
50+
spec:
51+
selector:
52+
matchLabels:
53+
app: my-deployment
54+
replicas: 3
55+
template:
56+
metadata:
57+
labels:
58+
app: my-deployment
59+
spec:
60+
containers:
61+
- name: my-deployment
62+
image: my-image
63+
imagePullPolicy: Always
64+
ports:
65+
- containerPort: 8080
66+
---
67+
apiVersion: apps/v1
68+
kind: Deployment
69+
metadata:
70+
name: my-deployment-2
71+
namespace: my-namespace-2
72+
spec:
73+
selector:
74+
matchLabels:
75+
app: my-deployment-2
76+
replicas: 4
77+
template:
78+
metadata:
79+
labels:
80+
app: my-deployment-2
81+
spec:
82+
containers:
83+
- name: my-deployment-2
84+
image: my-image-2
85+
imagePullPolicy: Always
86+
ports:
87+
- containerPort: 8080' > temp.yaml
88+
```
89+
```
90+
$ ./kube2cdk8s typescript -m true -f temp.yaml
91+
new cdk8s.ApiObject("", this, {
92+
apiVersion: "apps/v1",
93+
kind: "Deployment",
94+
metadata: {
95+
name: "my-deployment",
96+
namespace: "my-namespace",
97+
},
98+
spec: {
99+
selector: {
100+
matchLabels: {
101+
app: "my-deployment",
102+
},
103+
},
104+
replicas: 3,
105+
template: {
106+
metadata: {
107+
labels: {
108+
app: "my-deployment",
109+
},
110+
},
111+
spec: {
112+
containers: [{
113+
name: "my-deployment",
114+
image: "my-image",
115+
imagePullPolicy: "Always",
116+
ports: [{
117+
containerPort: 8080,
118+
}],
119+
}],
120+
},
121+
},
122+
},
123+
});
124+
125+
new cdk8s.ApiObject("", this, {
126+
apiVersion: "apps/v1",
127+
kind: "Deployment",
128+
metadata: {
129+
name: "my-deployment-2",
130+
namespace: "my-namespace-2",
131+
},
132+
spec: {
133+
selector: {
134+
matchLabels: {
135+
app: "my-deployment-2",
136+
},
137+
},
138+
replicas: 4,
139+
template: {
140+
metadata: {
141+
labels: {
142+
app: "my-deployment-2",
143+
},
144+
},
145+
spec: {
146+
containers: [{
147+
name: "my-deployment-2",
148+
image: "my-image-2",
149+
imagePullPolicy: "Always",
150+
ports: [{
151+
containerPort: 8080,
152+
}],
153+
}],
154+
},
155+
},
156+
},
157+
});
158+
```

cmd/typescript.go

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,17 +15,30 @@ func TSCommand() *cobra.Command {
1515
Long: "convert k8s yaml to typescript",
1616
RunE: func(cmd *cobra.Command, args []string) error {
1717
filePath := viper.GetString("file")
18+
multiple := viper.GetBool("multiple")
19+
20+
var result string
1821

1922
if filePath == "" {
2023
log.Fatal("-f, --file is required")
2124
}
2225

23-
data, err := kube2cdk8s.Kube2CDK8S(filePath)
26+
if multiple {
27+
result, err := kube2cdk8s.Kube2CDK8SMultiple(filePath)
28+
if err != nil {
29+
return err
30+
}
31+
32+
fmt.Print(result)
33+
return nil
34+
}
35+
36+
result, err := kube2cdk8s.Kube2CDK8S(filePath)
2437
if err != nil {
2538
return err
2639
}
2740

28-
fmt.Print(data)
41+
fmt.Print(result)
2942
return nil
3043
}}
3144

main.go

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,10 @@ import (
2525
"github.com/spf13/viper"
2626
)
2727

28-
var manifestFile string
28+
var (
29+
manifestFile string
30+
multiple bool
31+
)
2932

3033
func configureCLI() *cobra.Command {
3134
rootCmd := &cobra.Command{Use: "kube2cdk8s", Long: "converts k8s yaml to cdk8s"}
@@ -38,6 +41,12 @@ func configureCLI() *cobra.Command {
3841
log.Println(err)
3942
}
4043

44+
rootCmd.PersistentFlags().BoolVarP(&multiple, "multiple", "m", false, "convert multiple yamls seperated by ---")
45+
err = viper.BindPFlag("multiple", rootCmd.PersistentFlags().Lookup("multiple"))
46+
if err != nil {
47+
log.Println(err)
48+
}
49+
4150
return rootCmd
4251
}
4352

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
new cdk8s.ApiObject("", this, {
2+
apiVersion: "apps/v1",
3+
kind: "Deployment",
4+
metadata: {
5+
name: "my-deployment",
6+
namespace: "my-namespace",
7+
},
8+
spec: {
9+
selector: {
10+
matchLabels: {
11+
app: "my-deployment",
12+
},
13+
},
14+
replicas: 3,
15+
template: {
16+
metadata: {
17+
labels: {
18+
app: "my-deployment",
19+
},
20+
},
21+
spec: {
22+
containers: [{
23+
name: "my-deployment",
24+
image: "my-image",
25+
imagePullPolicy: "Always",
26+
ports: [{
27+
containerPort: 8080,
28+
}],
29+
}],
30+
},
31+
},
32+
},
33+
});
34+
35+
new cdk8s.ApiObject("", this, {
36+
apiVersion: "apps/v1",
37+
kind: "Deployment",
38+
metadata: {
39+
name: "my-deployment-2",
40+
namespace: "my-namespace-2",
41+
},
42+
spec: {
43+
selector: {
44+
matchLabels: {
45+
app: "my-deployment-2",
46+
},
47+
},
48+
replicas: 4,
49+
template: {
50+
metadata: {
51+
labels: {
52+
app: "my-deployment-2",
53+
},
54+
},
55+
spec: {
56+
containers: [{
57+
name: "my-deployment-2",
58+
image: "my-image-2",
59+
imagePullPolicy: "Always",
60+
ports: [{
61+
containerPort: 8080,
62+
}],
63+
}],
64+
},
65+
},
66+
},
67+
});
68+
69+
70+
71+
72+
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
new cdk8s.ApiObject("", this, {
2+
apiVersion: "apps/v1",
3+
kind: "Deployment",
4+
metadata: {
5+
name: "my-deployment",
6+
namespace: "my-namespace",
7+
},
8+
spec: {
9+
selector: {
10+
matchLabels: {
11+
app: "my-deployment",
12+
},
13+
},
14+
replicas: 3,
15+
template: {
16+
metadata: {
17+
labels: {
18+
app: "my-deployment",
19+
},
20+
},
21+
spec: {
22+
containers: [{
23+
name: "my-deployment",
24+
image: "my-image",
25+
imagePullPolicy: "Always",
26+
ports: [{
27+
containerPort: 8080,
28+
}],
29+
}],
30+
},
31+
},
32+
},
33+
});
34+
35+
new cdk8s.ApiObject("", this, {
36+
apiVersion: "apps/v1",
37+
kind: "Deployment",
38+
metadata: {
39+
name: "my-deployment-2",
40+
namespace: "my-namespace-2",
41+
},
42+
spec: {
43+
selector: {
44+
matchLabels: {
45+
app: "my-deployment-2",
46+
},
47+
},
48+
replicas: 4,
49+
template: {
50+
metadata: {
51+
labels: {
52+
app: "my-deployment-2",
53+
},
54+
},
55+
spec: {
56+
containers: [{
57+
name: "my-deployment-2",
58+
image: "my-image-2",
59+
imagePullPolicy: "Always",
60+
ports: [{
61+
containerPort: 8080,
62+
}],
63+
}],
64+
},
65+
},
66+
},
67+
});
68+
69+

0 commit comments

Comments
 (0)