Skip to content

Commit

Permalink
Add additional error messaging if we fail to update the CustomResourc…
Browse files Browse the repository at this point in the history
…e status due to a lack of permissions
  • Loading branch information
dlbock committed Dec 7, 2020
1 parent 9a56b7a commit 6dabe74
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 11 deletions.
3 changes: 2 additions & 1 deletion src/main/java/com/instana/operator/AgentDeployer.java
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@
import static com.instana.operator.util.StringUtils.isBlank;
import static io.fabric8.kubernetes.client.Watcher.Action.ADDED;
import static io.fabric8.kubernetes.client.Watcher.Action.DELETED;
import static java.net.HttpURLConnection.HTTP_CONFLICT;

@ApplicationScoped
public class AgentDeployer {
Expand Down Expand Up @@ -204,7 +205,7 @@ void createResource(int nRetries, MixedOperation<T, L, D, R> op, Factory<T, L, D
customResourceState.update(created);
} catch (KubernetesClientException e) {
// For status codes, see https://github.com/kubernetes/community/blob/master/contributors/devel/sig-architecture/api-conventions.md#http-status-codes
if (e.getCode() == 409 && nRetries > 1) {
if (e.getCode() == HTTP_CONFLICT && nRetries > 1) {
// Another resource of the same name exists in the same namespace.
// Maybe it's currently being removed, try again in a few seconds.
executor.schedule(() -> createResource(nRetries - 1, op, factory), 10, TimeUnit.SECONDS);
Expand Down
32 changes: 22 additions & 10 deletions src/main/java/com/instana/operator/CustomResourceState.java
Original file line number Diff line number Diff line change
@@ -1,23 +1,28 @@
package com.instana.operator;

import static com.instana.operator.client.KubernetesClientProducer.CRD_NAME;
import static com.instana.operator.util.ResourceUtils.name;
import static java.net.HttpURLConnection.HTTP_FORBIDDEN;

import java.util.Optional;

import javax.enterprise.context.ApplicationScoped;
import javax.inject.Inject;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.instana.operator.customresource.DoneableInstanaAgent;
import com.instana.operator.customresource.InstanaAgent;
import com.instana.operator.customresource.InstanaAgentList;
import com.instana.operator.customresource.InstanaAgentStatus;
import com.instana.operator.customresource.ResourceInfo;

import io.fabric8.kubernetes.api.model.HasMetadata;
import io.fabric8.kubernetes.api.model.Pod;
import io.fabric8.kubernetes.client.KubernetesClientException;
import io.fabric8.kubernetes.client.dsl.MixedOperation;
import io.fabric8.kubernetes.client.dsl.Resource;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.enterprise.context.ApplicationScoped;
import javax.inject.Inject;
import java.util.Optional;

import static com.instana.operator.client.KubernetesClientProducer.CRD_NAME;
import static com.instana.operator.util.ResourceUtils.name;

@ApplicationScoped
public class CustomResourceState {
Expand Down Expand Up @@ -127,7 +132,14 @@ private void update() {
try {
client.inNamespace(customResource.getMetadata().getNamespace()).createOrReplace(customResource);
} catch (Exception e) {
LOGGER.warn("Failed to update " + CRD_NAME + " " + name(customResource) + ": " + e.getMessage());
StringBuilder errorMessage = new StringBuilder();
errorMessage.append("Failed to update Custom Resource").append(CRD_NAME).append(name(customResource));
if (e instanceof KubernetesClientException) {
if (((KubernetesClientException)e).getCode() == HTTP_FORBIDDEN) {
errorMessage.append(". Please ensure that the operator has the appropriate cluster role permissions.");
}
}
LOGGER.warn(errorMessage.toString() + ": " + e.getMessage());
// No need to System.exit() if we cannot update the status. Ignore this and carry on.
}
}
Expand Down

0 comments on commit 6dabe74

Please sign in to comment.