Skip to content

Commit ba91ae8

Browse files
AlinsRanCopilot
andcommitted
feat: validate APISIX resources in webhooks
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
1 parent 15b2f39 commit ba91ae8

20 files changed

Lines changed: 1642 additions & 82 deletions

internal/adc/client/client.go

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -174,6 +174,43 @@ func (c *Client) DeleteConfig(ctx context.Context, args Task) error {
174174
return err
175175
}
176176

177+
func (c *Client) Validate(ctx context.Context, task Task) error {
178+
if len(task.Configs) == 0 || task.Resources == nil {
179+
return nil
180+
}
181+
182+
fileIOStart := time.Now()
183+
syncFilePath, cleanup, err := prepareSyncFile(task.Resources)
184+
if err != nil {
185+
pkgmetrics.RecordFileIODuration("prepare_sync_file", "failure", time.Since(fileIOStart).Seconds())
186+
return err
187+
}
188+
pkgmetrics.RecordFileIODuration("prepare_sync_file", adctypes.StatusSuccess, time.Since(fileIOStart).Seconds())
189+
defer cleanup()
190+
191+
args := BuildADCExecuteArgs(syncFilePath, task.Labels, task.ResourceTypes)
192+
193+
var errs types.ADCValidationErrors
194+
for _, config := range task.Configs {
195+
if config.BackendType == "" {
196+
config.BackendType = c.defaultMode
197+
}
198+
if err := c.executor.Validate(ctx, config, args); err != nil {
199+
var validationErr types.ADCValidationError
200+
if errors.As(err, &validationErr) {
201+
errs.Errors = append(errs.Errors, validationErr)
202+
continue
203+
}
204+
return err
205+
}
206+
}
207+
208+
if len(errs.Errors) > 0 {
209+
return errs
210+
}
211+
return nil
212+
}
213+
177214
func (c *Client) Sync(ctx context.Context) (map[string]types.ADCExecutionErrors, error) {
178215
c.syncMu.Lock()
179216
defer c.syncMu.Unlock()

0 commit comments

Comments
 (0)