Skip to content

Commit

Permalink
feat: container inspection
Browse files Browse the repository at this point in the history
  • Loading branch information
robot9706 committed Oct 6, 2023
1 parent 9165ab7 commit c1b2b4d
Show file tree
Hide file tree
Showing 29 changed files with 410 additions and 6,464 deletions.
3,250 changes: 0 additions & 3,250 deletions protobuf/go/agent/agent.pb.go

This file was deleted.

500 changes: 0 additions & 500 deletions protobuf/go/agent/agent_grpc.pb.go

This file was deleted.

2,280 changes: 0 additions & 2,280 deletions protobuf/go/common/common.pb.go

This file was deleted.

10 changes: 10 additions & 0 deletions protobuf/proto/agent.proto
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ service Agent {
rpc DeleteContainers(common.DeleteContainersRequest) returns (common.Empty);
rpc ContainerLog(stream common.ContainerLogMessage) returns (common.Empty);
rpc TokenReplaced(common.Empty) returns (common.Empty);
rpc ContainerInspect(common.ContainerInspectMessage) returns (common.Empty);
}

/**
Expand All @@ -57,6 +58,7 @@ message AgentCommand {
common.DeleteContainersRequest deleteContainers = 9;
ContainerLogRequest containerLog = 10;
ReplaceTokenRequest replaceToken = 11;
ContainerInspectRequest containerInspect = 12;
}
}

Expand Down Expand Up @@ -272,6 +274,14 @@ message ContainerLogRequest {
uint32 tail = 3;
}

/*
* Container inspect
*
*/
message ContainerInspectRequest {
common.ContainerIdentifier container = 1;
}

/*
* Connection close
*
Expand Down
6 changes: 6 additions & 0 deletions protobuf/proto/common.proto
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,12 @@ message ContainerLogMessage {
string log = 100;
}

message ContainerInspectMessage {
string prefix = 1;
string name = 2;
string inspection = 3;
}

enum NetworkMode {
NETWORK_MODE_UNSPECIFIED = 0;
BRIDGE = 1;
Expand Down
10 changes: 10 additions & 0 deletions web/backend/proto/agent.proto
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ service Agent {
rpc DeleteContainers(common.DeleteContainersRequest) returns (common.Empty);
rpc ContainerLog(stream common.ContainerLogMessage) returns (common.Empty);
rpc TokenReplaced(common.Empty) returns (common.Empty);
rpc ContainerInspect(common.ContainerInspectMessage) returns (common.Empty);
}

/**
Expand All @@ -57,6 +58,7 @@ message AgentCommand {
common.DeleteContainersRequest deleteContainers = 9;
ContainerLogRequest containerLog = 10;
ReplaceTokenRequest replaceToken = 11;
ContainerInspectRequest containerInspect = 12;
}
}

Expand Down Expand Up @@ -272,6 +274,14 @@ message ContainerLogRequest {
uint32 tail = 3;
}

/*
* Container inspect
*
*/
message ContainerInspectRequest {
common.ContainerIdentifier container = 1;
}

/*
* Connection close
*
Expand Down
6 changes: 6 additions & 0 deletions web/backend/proto/common.proto
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,12 @@ message ContainerLogMessage {
string log = 100;
}

message ContainerInspectMessage {
string prefix = 1;
string name = 2;
string inspection = 3;
}

enum NetworkMode {
NETWORK_MODE_UNSPECIFIED = 0;
BRIDGE = 1;
Expand Down
5 changes: 5 additions & 0 deletions web/backend/src/app/agent/agent.grpc.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import {
AgentController as GrpcAgentController,
} from 'src/grpc/protobuf/proto/agent'
import {
ContainerInspectMessage,
ContainerLogMessage,
ContainerStateListMessage,
DeleteContainersRequest,
Expand Down Expand Up @@ -62,6 +63,10 @@ export default class AgentController implements GrpcAgentController {
return this.service.handleContainerLog(call.connection, request)
}

containerInspect(request: ContainerInspectMessage, _: Metadata, call: NodeGrpcCall): Observable<Empty> {
return this.service.handleContainerInspect(call.connection, request)
}

async tokenReplaced(_: Empty, __: Metadata, call: NodeGrpcCall): Promise<Empty> {
return await this.service.tokenReplaced(call.connection)
}
Expand Down
9 changes: 9 additions & 0 deletions web/backend/src/app/agent/agent.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import { CruxNotFoundException } from 'src/exception/crux-exception'
import { AgentAbortUpdate, AgentCommand, AgentInfo, CloseReason } from 'src/grpc/protobuf/proto/agent'
import {
ContainerIdentifier,
ContainerInspectMessage,
ContainerLogMessage,
ContainerStateListMessage,
DeleteContainersRequest,
Expand Down Expand Up @@ -253,6 +254,14 @@ export default class AgentService {
)
}

handleContainerInspect(connection: GrpcNodeConnection, request: ContainerInspectMessage): Observable<Empty> {
const agent = this.getByIdOrThrow(connection.nodeId)

agent.onContainerInspect(request)

return of(Empty)
}

async tokenReplaced(connection: GrpcNodeConnection): Promise<Empty> {
const agent = this.getByIdOrThrow(connection.nodeId)

Expand Down
5 changes: 5 additions & 0 deletions web/backend/src/app/node/node.dto.ts
Original file line number Diff line number Diff line change
Expand Up @@ -232,3 +232,8 @@ export class NodeAuditLogListDto extends PaginatedList<NodeAuditLogDto> {

total: number
}

export class ContainerInspectionDto {
@IsString()
inspection: string
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import {
ROUTE_NODES,
ROUTE_NODE_ID,
} from './node.const'
import { ContainerDto } from './node.dto'
import { ContainerDto, ContainerInspectionDto } from './node.dto'
import NodeService from './node.service'

@Controller(`${ROUTE_NODES}/${ROUTE_NODE_ID}/${ROUTE_CONTAINERS}`)
Expand All @@ -43,6 +43,19 @@ export default class NodeGlobalContainerHttpController {
return await this.service.getContainers(nodeId, prefix)
}

@Get(`${ROUTE_NAME}/inspect`)
@HttpCode(HttpStatus.OK)
@ApiOperation({
description: 'Request must include `nodeId`, and the `name` of the container.',
summary: 'Inspect a specific container on a node.',
})
@ApiBadRequestResponse({ description: 'Bad request for container inspect.' })
@ApiForbiddenResponse({ description: 'Unauthorized request for container inspect.' })
@UuidParams(PARAM_NODE_ID)
async inspectContainer(@NodeId() nodeId: string, @Name() name: string): Promise<ContainerInspectionDto> {
return await this.service.inspectContainer(nodeId, GLOBAL_PREFIX, name)
}

@Post(`${ROUTE_NAME}/start`)
@HttpCode(HttpStatus.NO_CONTENT)
@ApiOperation({
Expand Down
8 changes: 8 additions & 0 deletions web/backend/src/app/node/node.mapper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import AgentInstaller from 'src/domain/agent-installer'
import { NodeWithToken } from 'src/domain/node'
import { fromTimestamp } from 'src/domain/utils'
import {
ContainerInspectMessage,
ContainerOperation,
ContainerStateItem,
ContainerStateListMessage,
Expand All @@ -17,6 +18,7 @@ import {
BasicNodeDto,
BasicNodeWithStatus,
ContainerDto,
ContainerInspectionDto,
ContainerOperationDto,
ContainerState,
NodeConnectionStatus,
Expand Down Expand Up @@ -129,4 +131,10 @@ export default class NodeMapper {
return ContainerOperation.UNRECOGNIZED
}
}

containerInspectionMessageToDto(it: ContainerInspectMessage): ContainerInspectionDto {
return {
inspection: it.inspection,
}
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Controller, Delete, HttpCode, HttpStatus, Post, UseGuards } from '@nestjs/common'
import { Controller, Delete, Get, HttpCode, HttpStatus, Post, UseGuards } from '@nestjs/common'
import {
ApiBadRequestResponse,
ApiForbiddenResponse,
Expand All @@ -21,6 +21,7 @@ import {
ROUTE_NODE_ID,
ROUTE_PREFIX,
} from './node.const'
import { ContainerInspectionDto } from './node.dto'
import NodeService from './node.service'

@Controller(`${ROUTE_NODES}/${ROUTE_NODE_ID}/${ROUTE_PREFIX}/${ROUTE_CONTAINERS}`)
Expand All @@ -29,6 +30,23 @@ import NodeService from './node.service'
export default class NodePrefixContainerHttpController {
constructor(private service: NodeService) {}

@Get(`${ROUTE_NAME}/inspect`)
@HttpCode(HttpStatus.OK)
@ApiOperation({
description: 'Request must include `nodeId`, and the `name` of the container.',
summary: 'Inspect a specific container on a node.',
})
@ApiBadRequestResponse({ description: 'Bad request for container inspect.' })
@ApiForbiddenResponse({ description: 'Unauthorized request for container inspect.' })
@UuidParams(PARAM_NODE_ID)
async inspectContainer(
@NodeId() nodeId: string,
@Prefix() prefix: string,
@Name() name: string,
): Promise<ContainerInspectionDto> {
return await this.service.inspectContainer(nodeId, prefix, name)
}

@Post(`${ROUTE_NAME}/start`)
@HttpCode(HttpStatus.NO_CONTENT)
@ApiOperation({
Expand Down
22 changes: 21 additions & 1 deletion web/backend/src/app/node/node.service.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,17 @@
import { Injectable, Logger } from '@nestjs/common'
import { Prisma } from '@prisma/client'
import { EmptyError, Observable, filter, firstValueFrom, map, mergeAll, mergeWith, of, timeout } from 'rxjs'
import {
EmptyError,
Observable,
filter,
firstValueFrom,
lastValueFrom,
map,
mergeAll,
mergeWith,
of,
timeout,
} from 'rxjs'
import { Agent, AgentConnectionMessage } from 'src/domain/agent'
import {
ContainerCommandRequest,
Expand All @@ -14,6 +25,7 @@ import PrismaService from 'src/services/prisma.service'
import AgentService from '../agent/agent.service'
import {
ContainerDto,
ContainerInspectionDto,
CreateNodeDto,
NodeAuditLogListDto,
NodeAuditLogQueryDto,
Expand Down Expand Up @@ -350,6 +362,14 @@ export default class NodeService {
}
}

async inspectContainer(nodeId: string, prefix: string, name: string): Promise<ContainerInspectionDto> {
const agent = this.agentService.getByIdOrThrow(nodeId)
const watcher = agent.getContainerInspection(prefix, name)
const inspectionMessage = await lastValueFrom(watcher)

return this.mapper.containerInspectionMessageToDto(inspectionMessage)
}

private static snakeCaseToCamelCase(snake: string): string {
return snake.toLocaleLowerCase().replace(/([-_][a-z])/g, it => it.replace('_', '').toLocaleUpperCase())
}
Expand Down
Loading

0 comments on commit c1b2b4d

Please sign in to comment.