Skip to content

Commit

Permalink
feat(provider/kubernetes): v1 pod logs
Browse files Browse the repository at this point in the history
support pod logs for v1 provider. going to be useful when working on run
job logs as well.
  • Loading branch information
ethanfrogers committed Nov 20, 2017
1 parent 8e09a79 commit 169af83
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -244,6 +244,12 @@ class KubernetesApiAdaptor {
}
}

String getLog(String namespace, String name, String containerId) {
exceptionWrapper("pod.logs", "Get Logs $name", namespace) {
client.pods().inNamespace(namespace).withName(name).inContainer(containerId).getLog()
}
}

List<ReplicationController> getReplicationControllers(String namespace) {
exceptionWrapper("replicationControllers.list", "Get Replication Controllers", namespace) {
client.replicationControllers().inNamespace(namespace).list().items
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,25 @@ import com.netflix.spinnaker.cats.cache.CacheData
import com.netflix.spinnaker.clouddriver.kubernetes.KubernetesCloudProvider
import com.netflix.spinnaker.clouddriver.kubernetes.v1.model.KubernetesV1Instance
import com.netflix.spinnaker.clouddriver.kubernetes.v1.caching.Keys
import com.netflix.spinnaker.clouddriver.security.ProviderVersion
import com.netflix.spinnaker.clouddriver.model.InstanceProvider
import com.netflix.spinnaker.clouddriver.security.AccountCredentialsProvider
import com.netflix.spinnaker.clouddriver.kubernetes.v1.security.KubernetesV1Credentials
import com.netflix.spinnaker.clouddriver.kubernetes.security.KubernetesNamedAccountCredentials
import org.springframework.beans.factory.annotation.Autowired
import groovy.util.logging.Slf4j

import org.springframework.stereotype.Component

@Slf4j
@Component
class KubernetesV1InstanceProvider implements InstanceProvider<KubernetesV1Instance> {
private final Cache cacheView
private final ObjectMapper objectMapper

@Autowired
AccountCredentialsProvider accountCredentialsProvider

@Autowired
KubernetesV1InstanceProvider(Cache cacheView, ObjectMapper objectMapper) {
this.cacheView = cacheView
Expand Down Expand Up @@ -68,6 +78,38 @@ class KubernetesV1InstanceProvider implements InstanceProvider<KubernetesV1Insta

@Override
String getConsoleOutput(String account, String region, String id) {
return null
KubernetesNamedAccountCredentials<KubernetesV1Credentials> credentials;
try {
credentials = (KubernetesNamedAccountCredentials) accountCredentialsProvider.getCredentials(account)
} catch(Exception e) {
log.warn("Failure getting account credentials for ${account}")
return null
}
if (credentials == null || credentials.getProviderVersion() != ProviderVersion.v1) {
return null
}

def trueCredentials = credentials.credentials
def pod = trueCredentials.apiAdaptor.getPod(region, id)
if (pod == null ) {
return null
}

String podName = pod.getMetadata().getName()
StringBuilder result = new StringBuilder()

pod.getSpec().getContainers().collect { container ->
result.append("===== ${container.getName()} =====\n\n")
try {
String log = trueCredentials.apiAdaptor.getLog(region, podName, container.getName())
result.append(log)
} catch(Exception e){
result.append(e.getMessage())
}
result.append("\n\n")
}

return result.toString()

}
}

0 comments on commit 169af83

Please sign in to comment.