question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

createOrReplace on Secrets throws in case the resource already exists

See original GitHub issue

Describe the bug

Version of the kubernetes-client: 5.11.2

Performing 2 createOrReplace on the same Secret cause the second call to fail with this error:

io.fabric8.kubernetes.client.KubernetesClientException: Failure executing: GET at: https://192.168.64.3:8443/api/v1/secrets/test. Message: the server could not find the requested resource. Received status: Status(apiVersion=v1, code=404, details=StatusDetails(causes=[], group=null, kind=null, name=null, retryAfterSeconds=null, uid=null, additionalProperties={}), kind=Status, message=the server could not find the requested resource, metadata=ListMeta(_continue=null, remainingItemCount=null, resourceVersion=null, selfLink=null, additionalProperties={}), reason=NotFound, status=Failure, additionalProperties={}).

Fabric8 Kubernetes Client version

next (development version)

Steps to reproduce

Running a Main like this would reproduce the issue:

public class Main {

    private static Secret getSecret() {
        return new SecretBuilder()
                .withNewMetadata()
                .withName("test")
                .withNamespace("default")
                .endMetadata()
                .build();
    }
    
    public static void main(String[] args) {
        Config config = new ConfigBuilder().withNamespace(null).build();
        KubernetesClient client = new DefaultKubernetesClient(config);

        client.secrets().createOrReplace(getSecret());
        client.secrets().createOrReplace(getSecret());
    }
    
}

Expected behavior

The resource is seamlessly replaced.

Runtime

minikube

Kubernetes API Server version

1.22.3@latest

Environment

macOS

Fabric8 Kubernetes Client Logs

io.fabric8.kubernetes.client.KubernetesClientException: Failure executing: GET at: https://192.168.64.3:8443/api/v1/secrets/test. Message: the server could not find the requested resource. Received status: Status(apiVersion=v1, code=404, details=StatusDetails(causes=[], group=null, kind=null, name=null, retryAfterSeconds=null, uid=null, additionalProperties={}), kind=Status, message=the server could not find the requested resource, metadata=ListMeta(_continue=null, remainingItemCount=null, resourceVersion=null, selfLink=null, additionalProperties={}), reason=NotFound, status=Failure, additionalProperties={}).
    at io.fabric8.kubernetes.client.dsl.base.OperationSupport.requestFailure (OperationSupport.java:683)
    at io.fabric8.kubernetes.client.dsl.base.OperationSupport.requestFailure (OperationSupport.java:662)
    at io.fabric8.kubernetes.client.dsl.base.OperationSupport.assertResponseCode (OperationSupport.java:613)
    at io.fabric8.kubernetes.client.dsl.base.OperationSupport.handleResponse (OperationSupport.java:556)
    at io.fabric8.kubernetes.client.dsl.base.OperationSupport.handleResponse (OperationSupport.java:519)
    at io.fabric8.kubernetes.client.dsl.base.OperationSupport.handleGet (OperationSupport.java:488)
    at io.fabric8.kubernetes.client.dsl.base.OperationSupport.handleGet (OperationSupport.java:458)
    at io.fabric8.kubernetes.client.dsl.base.BaseOperation.handleGet (BaseOperation.java:696)
    at io.fabric8.kubernetes.client.dsl.base.BaseOperation.getMandatory (BaseOperation.java:182)
    at io.fabric8.kubernetes.client.dsl.base.BaseOperation.require (BaseOperation.java:163)
    at io.fabric8.kubernetes.client.dsl.base.HasMetadataOperation.requireFromServer (HasMetadataOperation.java:120)
    at io.fabric8.kubernetes.client.dsl.base.HasMetadataOperation.replace (HasMetadataOperation.java:187)
    at io.fabric8.kubernetes.client.dsl.base.HasMetadataOperation.replace (HasMetadataOperation.java:141)
    at io.fabric8.kubernetes.client.utils.CreateOrReplaceHelper.replace (CreateOrReplaceHelper.java:68)
    at io.fabric8.kubernetes.client.utils.CreateOrReplaceHelper.createOrReplace (CreateOrReplaceHelper.java:60)
    at io.fabric8.kubernetes.client.dsl.base.BaseOperation.createOrReplace (BaseOperation.java:316)
    at io.fabric8.kubernetes.client.dsl.base.BaseOperation.createOrReplace (BaseOperation.java:83)
    at io.fabric8.kubernetes.client.dsl.base.BaseOperation.createOrReplace (BaseOperation.java:306)
    at io.fabric8.kubernetes.client.dsl.base.BaseOperation.createOrReplace (BaseOperation.java:83)
    at org.keycloak.Main.main (Main.java:27)
    at org.codehaus.mojo.exec.ExecJavaMojo$1.run (ExecJavaMojo.java:254)
    at java.lang.Thread.run (Thread.java:829)

Additional context

I suspect the issue sits in the replace method, but I haven’t narrowed it down.

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:9 (9 by maintainers)

github_iconTop GitHub Comments

2reactions
shawkinscommented, Jan 18, 2022

We’ll address this in 6.0.0 - if the config has a null namespace, whether because there is not one on the context or the user calls withNamespace(null), the logic will assume an appropriate default - likely “default”. In the event of a checkNamespace call failing, the logic should inform the user to use inNamespace or the original namespace was null, to set a better default on the Config.

1reaction
shawkinscommented, Jan 18, 2022

I can add that the error doesn’t occur for e.g. deployments

The dsl logic will be different depending on the resource type - for deployments there is rolling logic that is implemented for a replace.

To restate what is happening under the covers in your test shown above:

Config config = new ConfigBuilder().withNamespace(null).build();
KubernetesClient client = new DefaultKubernetesClient(config);
client.secrets()...

is like calling:

client.secrets().inAnyNamespace()...

Note what operations are available when you call client.secrets().inAnyNamespace() - those method calls will still work as expected using withNamespace(null). However anything else you can call that has access to a passed in item may not be accounting for the possibility of a null namespace.

I can address this with a narrow change to the require logic: https://github.com/fabric8io/kubernetes-client/compare/master...shawkins:master

However all of the other operations would need similarly checked. If it’s the case that we always expect the initial config passed into the client to specify a default namespace, I’d rather add a validation just for that.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Terraform KeyVault Secrets (Error Already Exist)
I have a terraform template but instead of having multiple steps for each secret I have created a loop resource ...
Read more >
Warning for creates when the resource already exists #3593
The suggestion raised by @alexeyshishkin01 would be to issue a warning so the user knows that an existing resource is being overwritten by...
Read more >
Error conditions in Azure Databricks - Microsoft Learn
NAMESPACE_ALREADY_EXISTS. Cannot create namespace because it already exists. Choose a different name, drop the existing namespace, or add the IF ...
Read more >
Release Notes - Flyway by Redgate • Database Migrations ...
Bug fixes. Fix issue where post schema clean didn't include correct schemas. Clean mode now cleans more schemas for SQL Server integrated authentication...
Read more >
Exceptions and Exception Handling
A column that does not exist in a table is referenced in a query. ER_TABLE_EXISTS_ERROR. Table already exists. While creating a table, ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found