@@ -43,6 +43,13 @@ public Install(
43
43
LongName = "ca-certs" ) ]
44
44
public string CaCertificatesPath { get ; set ; } = "/ca" ;
45
45
46
+ [ Option (
47
+ Description =
48
+ "If specified and set to true it will replace already existing webhooks" ,
49
+ ShortName = "r" ,
50
+ LongName = "replace-existing" ) ]
51
+ public bool ReplaceExistingWebhooks { get ; set ; }
52
+
46
53
public async Task < int > OnExecuteAsync ( CommandLineApplication app )
47
54
{
48
55
var client = app . GetRequiredService < IKubernetesClient > ( ) ;
@@ -116,7 +123,27 @@ await client.Create(
116
123
validatorConfig . Metadata . OwnerReferences = new List < V1OwnerReference > { deployment . MakeOwnerReference ( ) , } ;
117
124
}
118
125
119
- await client . Save ( validatorConfig ) ;
126
+ if ( ReplaceExistingWebhooks )
127
+ {
128
+ // var existingItems = await client.List<V1ValidatingWebhookConfiguration>();
129
+ // var existingItem = existingItems.FirstOrDefault(item => item.Name() == validatorConfig.Name());
130
+ var existingItem = await client . Get < V1ValidatingWebhookConfiguration > ( validatorConfig . Name ( ) ) ;
131
+ if ( existingItem != null )
132
+ {
133
+ await app . Out . WriteLineAsync ( "Validator existed, updating." ) ;
134
+ await client . Update ( existingItem ) ;
135
+ }
136
+ else
137
+ {
138
+ await app . Out . WriteLineAsync ( "Validator didn't exist, creating." ) ;
139
+ await client . Save ( validatorConfig ) ;
140
+ }
141
+ }
142
+ else
143
+ {
144
+ await app . Out . WriteLineAsync ( "Not updating validator, attempting to save." ) ;
145
+ await client . Save ( validatorConfig ) ;
146
+ }
120
147
121
148
await app . Out . WriteLineAsync ( "Create mutator definition." ) ;
122
149
var mutatorConfig = _mutatingWebhookConfigurationBuilder . BuildWebhookConfiguration ( webhookConfig ) ;
@@ -125,7 +152,25 @@ await client.Create(
125
152
mutatorConfig . Metadata . OwnerReferences = new List < V1OwnerReference > { deployment . MakeOwnerReference ( ) , } ;
126
153
}
127
154
128
- await client . Save ( mutatorConfig ) ;
155
+ if ( ReplaceExistingWebhooks )
156
+ {
157
+ var existingItem = await client . Get < V1MutatingWebhookConfiguration > ( mutatorConfig . Name ( ) ) ;
158
+ if ( existingItem != null )
159
+ {
160
+ await app . Out . WriteLineAsync ( "Mutator existed, updating." ) ;
161
+ await client . Update ( existingItem ) ;
162
+ }
163
+ else
164
+ {
165
+ await app . Out . WriteLineAsync ( "Mutator didn't exist, creating." ) ;
166
+ await client . Save ( mutatorConfig ) ;
167
+ }
168
+ }
169
+ else
170
+ {
171
+ await app . Out . WriteLineAsync ( "Not updating mutator, attempting to save." ) ;
172
+ await client . Save ( mutatorConfig ) ;
173
+ }
129
174
130
175
await app . Out . WriteLineAsync ( "Installed webhook service and admission configurations." ) ;
131
176
0 commit comments