Skip to content

Commit

Permalink
Handle resource already exists in Apply
Browse files Browse the repository at this point in the history
We're getting already exists errors when applying the
same resource in parallel tests.

```
    logger.go:146: 2024-09-20T11:05:08.890Z	FATAL	environment/magic.go:243	failed to install CA certificates and issuer: failed to create resource certificates.cert-manager.io "selfsigned-ca" already exists - Resource:
        apiVersion: cert-manager.io/v1
        kind: Certificate
        metadata:
          name: selfsigned-ca
          namespace: serverless-tests
        spec:
          commonName: selfsigned-ca
          isCA: true
          issuerRef:
            group: cert-manager.io
            kind: Issuer
            name: selfsigned-issuer
          privateKey:
            algorithm: ECDSA
            size: 256
          secretName: eventshub-ca
        	{"test": "TestPingSourceTLS"}
```

Signed-off-by: Pierangelo Di Pilato <[email protected]>
  • Loading branch information
pierDipi authored and knative-prow-robot committed Sep 24, 2024
1 parent 00d94f4 commit 5a23ae2
Showing 1 changed file with 5 additions and 2 deletions.
7 changes: 5 additions & 2 deletions pkg/manifest/manifest.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,18 +86,21 @@ func (f *YamlManifest) Apply(spec *unstructured.Unstructured) error {
if err != nil {
return err
}
gvr, _ := meta.UnsafeGuessKindToResource(spec.GroupVersionKind())
if current == nil {
f.log.Info("Creating type ", spec.GroupVersionKind(), " name ", spec.GetName())
gvr, _ := meta.UnsafeGuessKindToResource(spec.GroupVersionKind())
if _, err := f.client.Resource(gvr).Namespace(spec.GetNamespace()).Create(context.Background(), spec, v1.CreateOptions{}); err != nil {
// We might be applying the same resource in parallel, in that case, update the resource.
if errors.IsAlreadyExists(err) {
return f.Apply(spec)
}
return fmt.Errorf("failed to create resource %v - Resource:\n%s", err, toYaml(spec))
}
} else {
// Update existing one
if UpdateChanged(spec.UnstructuredContent(), current.UnstructuredContent()) {
f.log.Info("Updating type ", spec.GroupVersionKind(), " name ", spec.GetName())

gvr, _ := meta.UnsafeGuessKindToResource(spec.GroupVersionKind())
if _, err = f.client.Resource(gvr).Namespace(current.GetNamespace()).Update(context.Background(), current, v1.UpdateOptions{}); err != nil {
return fmt.Errorf("failed to update resource %v - Resource:\n%s", err, toYaml(spec))
}
Expand Down

0 comments on commit 5a23ae2

Please sign in to comment.