@@ -13,34 +13,38 @@ namespace KubernetesWatchGenerator
13
13
{
14
14
class Program
15
15
{
16
+ static readonly Dictionary < string , string > ClassNameMap = new Dictionary < string , string > ( ) ;
17
+
16
18
static async Task Main ( string [ ] args )
17
19
{
18
20
if ( args . Length < 2 )
19
21
{
20
- Console . Error . WriteLine ( $ "usage { args [ 0 ] } path/to/csharp.settings ") ;
22
+ Console . Error . WriteLine ( $ "usage { args [ 0 ] } path/to/generated ") ;
21
23
Environment . Exit ( 1 ) ;
22
24
}
23
25
24
- var ( kubernetesBranch , outputDirectory ) = LoadSettings ( args [ 1 ] ) ;
25
-
26
- var specUrl = $ "https://raw.githubusercontent.com/kubernetes/kubernetes/{ kubernetesBranch } /api/openapi-spec/swagger.json";
27
- var specPath = $ "{ kubernetesBranch } -swagger.json";
26
+ var outputDirectory = args [ 1 ] ;
28
27
29
- // Download the Kubernetes spec, and cache it locally. Don't download it if already present in the cache.
30
- if ( ! File . Exists ( specPath ) )
28
+ // Read the spec trimmed
29
+ // here we cache all name in gen project for later use
30
+ var swagger = await SwaggerDocument . FromFileAsync ( Path . Combine ( args [ 1 ] , "swagger.json" ) ) ;
31
+ foreach ( var ( k , v ) in swagger . Definitions )
31
32
{
32
- HttpClient client = new HttpClient ( ) ;
33
- using ( var response = await client . GetAsync ( specUrl , HttpCompletionOption . ResponseHeadersRead ) )
34
- using ( var stream = await response . Content . ReadAsStreamAsync ( ) )
35
- using ( var output = File . Open ( specPath , FileMode . Create , FileAccess . ReadWrite ) )
33
+ if ( v . ExtensionData ? [ "x-kubernetes-group-version-kind" ] != null )
36
34
{
37
- await stream . CopyToAsync ( output ) ;
35
+ var groupVersionKindElements = ( object [ ] ) v . ExtensionData [ "x-kubernetes-group-version-kind" ] ;
36
+ var groupVersionKind = ( Dictionary < string , object > ) groupVersionKindElements [ 0 ] ;
37
+
38
+ var group = ( string ) groupVersionKind [ "group" ] ;
39
+ var kind = ( string ) groupVersionKind [ "kind" ] ;
40
+ var version = ( string ) groupVersionKind [ "version" ] ;
41
+ ClassNameMap [ $ "{ group } _{ kind } _{ version } "] = ToPascalCase ( k . Replace ( "." , "" ) ) ;
38
42
}
39
43
}
40
44
41
- // Read the spec
42
- var swagger = await SwaggerDocument . FromFileAsync ( specPath ) ;
43
45
46
+ // gen project removed all watch operations, so here we switch back to unprocessed version
47
+ swagger = await SwaggerDocument . FromFileAsync ( Path . Combine ( args [ 1 ] , "swagger.json.unprocessed" ) ) ;
44
48
45
49
// Register helpers used in the templating.
46
50
Helpers . Register ( nameof ( ToXmlDoc ) , ToXmlDoc ) ;
@@ -58,17 +62,6 @@ static async Task Main(string[] args)
58
62
// That's usually because there are different version of the same object (e.g. for deployments).
59
63
var blacklistedOperations = new HashSet < string > ( )
60
64
{
61
- "watchAppsV1beta1NamespacedDeployment" ,
62
- "watchAppsV1beta2NamespacedDeployment" ,
63
- "watchExtensionsV1beta1NamespacedDeployment" ,
64
- "watchExtensionsV1beta1NamespacedNetworkPolicy" ,
65
- "watchPolicyV1beta1PodSecurityPolicy" ,
66
- "watchExtensionsV1beta1PodSecurityPolicy" ,
67
- "watchExtensionsV1beta1NamespacedIngress" ,
68
- "watchNamespacedIngress" ,
69
- "watchExtensionsV1beta1NamespacedIngressList" ,
70
- "watchNetworkingV1beta1NamespacedIngress" ,
71
- "watchNetworkingV1beta1NamespacedIngressList" ,
72
65
} ;
73
66
74
67
var watchOperations = swagger . Operations . Where (
@@ -83,16 +76,7 @@ static async Task Main(string[] args)
83
76
// Generate the interface declarations
84
77
var skippedTypes = new HashSet < string > ( )
85
78
{
86
- "V1beta1Deployment" ,
87
- "V1beta1DeploymentList" ,
88
- "V1beta1DeploymentRollback" ,
89
- "V1beta1DeploymentRollback" ,
90
- "V1beta1Scale" ,
91
- "V1beta1PodSecurityPolicy" ,
92
- "V1beta1PodSecurityPolicyList" ,
93
79
"V1WatchEvent" ,
94
- "V1beta1Ingress" ,
95
- "V1beta1IngressList"
96
80
} ;
97
81
98
82
var definitions = swagger . Definitions . Values
@@ -101,40 +85,7 @@ static async Task Main(string[] args)
101
85
&& d . ExtensionData . ContainsKey ( "x-kubernetes-group-version-kind" )
102
86
&& ! skippedTypes . Contains ( GetClassName ( d ) ) ) ;
103
87
104
- // Render.
105
- Render . FileToFile ( "ModelExtensions.cs.template" , definitions , $ "{ outputDirectory } ModelExtensions.cs") ;
106
- }
107
-
108
- private static ( string kubernetesBranch , string outputDirectory ) LoadSettings ( string path )
109
- {
110
- var fileInfo = new FileInfo ( path ) ;
111
-
112
- if ( ! fileInfo . Exists )
113
- {
114
- Console . Error . WriteLine ( "Cannot find csharp.settings" ) ;
115
- Environment . Exit ( 1 ) ;
116
- }
117
-
118
- using ( var s = new StreamReader ( fileInfo . OpenRead ( ) ) )
119
- {
120
- string l ;
121
- while ( ( l = s . ReadLine ( ) ) != null )
122
- {
123
- if ( l . Contains ( "KUBERNETES_BRANCH" ) )
124
- {
125
- var kubernetesBranch = l . Split ( "=" ) [ 1 ] ;
126
- var outputDirectory = Path . Combine ( fileInfo . DirectoryName , @"src/KubernetesClient/generated/" ) ;
127
-
128
- Console . WriteLine ( $ "Using branch { kubernetesBranch } output { outputDirectory } ") ;
129
-
130
- return ( kubernetesBranch , outputDirectory ) ;
131
- }
132
- }
133
- }
134
-
135
- Console . Error . WriteLine ( "Cannot find KUBERNETES_BRANCH in csharp.settings" ) ;
136
- Environment . Exit ( 1 ) ;
137
- return ( null , null ) ;
88
+ Render . FileToFile ( "ModelExtensions.cs.template" , definitions , Path . Combine ( outputDirectory , "ModelExtensions.cs" ) ) ;
138
89
}
139
90
140
91
static void ToXmlDoc ( RenderContext context , IList < object > arguments , IDictionary < string , object > options , RenderBlock fn , RenderBlock inverse )
@@ -150,7 +101,7 @@ static void ToXmlDoc(RenderContext context, IList<object> arguments, IDictionary
150
101
{
151
102
if ( ! first )
152
103
{
153
- context . Write ( Environment . NewLine ) ;
104
+ context . Write ( " \n " ) ;
154
105
context . Write ( " /// " ) ;
155
106
}
156
107
else
@@ -178,24 +129,24 @@ static void GetClassName(RenderContext context, IList<object> arguments, IDictio
178
129
static string GetClassName ( SwaggerOperation watchOperation )
179
130
{
180
131
var groupVersionKind = ( Dictionary < string , object > ) watchOperation . ExtensionData [ "x-kubernetes-group-version-kind" ] ;
132
+ return GetClassName ( groupVersionKind ) ;
133
+ }
134
+
135
+ private static string GetClassName ( Dictionary < string , object > groupVersionKind )
136
+ {
181
137
var group = ( string ) groupVersionKind [ "group" ] ;
182
138
var kind = ( string ) groupVersionKind [ "kind" ] ;
183
139
var version = ( string ) groupVersionKind [ "version" ] ;
184
140
185
- var className = $ "{ ToPascalCase ( version ) } { kind } ";
186
- return className ;
141
+ return ClassNameMap [ $ "{ group } _{ kind } _{ version } "] ;
187
142
}
188
143
189
144
private static string GetClassName ( JsonSchema4 definition )
190
145
{
191
146
var groupVersionKindElements = ( object [ ] ) definition . ExtensionData [ "x-kubernetes-group-version-kind" ] ;
192
147
var groupVersionKind = ( Dictionary < string , object > ) groupVersionKindElements [ 0 ] ;
193
148
194
- var group = groupVersionKind [ "group" ] as string ;
195
- var version = groupVersionKind [ "version" ] as string ;
196
- var kind = groupVersionKind [ "kind" ] as string ;
197
-
198
- return $ "{ ToPascalCase ( version ) } { ToPascalCase ( kind ) } ";
149
+ return GetClassName ( groupVersionKind ) ;
199
150
}
200
151
201
152
static void GetKind ( RenderContext context , IList < object > arguments , IDictionary < string , object > options , RenderBlock fn , RenderBlock inverse )
@@ -350,7 +301,7 @@ private static string GetPathExpression(SwaggerOperationDescription operation)
350
301
{
351
302
string pathExpression = operation . Path ;
352
303
353
- if ( pathExpression . StartsWith ( "/" ) )
304
+ if ( pathExpression . StartsWith ( "/" ) )
354
305
{
355
306
pathExpression = pathExpression . Substring ( 1 ) ;
356
307
}
0 commit comments