func()

in pkg/webhook/stage_webhook.go [73:101]


func (r *StageValidationWebhook) uniqueTargetNamespaces(ctx context.Context, stage *pipelineApi.Stage) error {
	stages := &pipelineApi.StageList{}

	if err := r.client.List(
		ctx,
		stages,
		client.InNamespace(stage.Namespace),
		client.Limit(listLimit),
	); err != nil {
		return fmt.Errorf("failed to list stages: %w", err)
	}

	for i := range stages.Items {
		if stages.Items[i].Name == stage.Name || stages.Items[i].Spec.ClusterName != stage.Spec.ClusterName {
			continue
		}

		if stages.Items[i].Spec.Namespace == stage.Spec.Namespace {
			return fmt.Errorf(
				"namespace %s is already used in CDPipeline %s Stage %s",
				stage.Spec.Namespace,
				stages.Items[i].Spec.CdPipeline,
				stages.Items[i].Name,
			)
		}
	}

	return nil
}