Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add field pod of ibpeer(#143) #73

Merged
merged 3 commits into from
Apr 7, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions src/app.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ import { MinioModule } from './minio/minio.module';
import { ChaincodeModule } from './chaincode/chaincode.module';
import { ChaincodebuildModule } from './chaincodebuild/chaincodebuild.module';
import { RealtimeLogModule } from './realtime-log/realtime-log.module';
import { PodModule } from './pod/pod.module';
import imageConfig from './config/image.config';
import minioConfig from './config/minio.config';

Expand Down Expand Up @@ -105,6 +106,7 @@ const GRAPHQL_PATH = '/bff';
ChaincodeModule,
ChaincodebuildModule,
RealtimeLogModule,
PodModule,
],
controllers: [AppController],
providers: [
Expand Down
2 changes: 2 additions & 0 deletions src/chaincodebuild/chaincodebuild.gql
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ query getChaincodebuilds($network: String!, $version: String, $displayName: Stri
}
channels {
name
displayName
}
}
}
Expand All @@ -49,6 +50,7 @@ query getChaincodebuild($name: String!) {
}
channels {
name
displayName
epolicy {
name
value
Expand Down
2 changes: 2 additions & 0 deletions src/epolicy/epolicy.gql
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ query getEpolicies($network: String) {
displayName
description
channel
channelDisplayName
value
creationTimestamp
lastHeartbeatTime
Expand Down Expand Up @@ -35,6 +36,7 @@ mutation deleteEpolicy($name: String!) {
query getChannelsForCreateEpolicy($network: String!) {
channelsForCreateEpolicy(network: $network) {
name
displayName
members {
name
}
Expand Down
25 changes: 24 additions & 1 deletion src/epolicy/epolicy.resolver.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,15 @@
import { Args, Mutation, Query, Resolver } from '@nestjs/graphql';
import {
Args,
Mutation,
Parent,
Query,
ResolveField,
Resolver,
} from '@nestjs/graphql';
import DataLoader from 'dataloader';
import { ChannelLoader } from 'src/channel/channel.loader';
import { Channel } from 'src/channel/models/channel.model';
import { Loader } from 'src/common/dataloader';
import { Auth } from 'src/common/decorators/auth.decorator';
import { K8sV1Status } from 'src/common/models/k8s-v1-status.model';
import { JwtAuth } from 'src/types';
Expand Down Expand Up @@ -44,4 +54,17 @@ export class EpolicyResolver {
): Promise<Channel[]> {
return this.epolicyService.getChannelsForCreateEpolicy(auth, network);
}

@ResolveField(() => String, {
nullable: true,
description: '所在通道名称(displayName)',
})
async channelDisplayName(
@Parent() epolicy: Epolicy,
@Loader(ChannelLoader) channelLoader: DataLoader<Channel['name'], Channel>,
): Promise<string> {
const { channel } = epolicy;
const chan = await channelLoader.load(channel);
return chan?.displayName;
}
}
3 changes: 3 additions & 0 deletions src/epolicy/models/epolicy.model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@ export class Epolicy {
/** 所在通道 */
channel: string;

/** 所在通道名称(displayName) */
channelDisplayName?: string;

/** 描述 */
description?: string;

Expand Down
5 changes: 5 additions & 0 deletions src/ibppeer/ibppeer.gql
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,11 @@ query getIbppeers($organization: String!) {
}
channels
networks
pod {
name
namespace
containers
}
}
}

Expand Down
3 changes: 2 additions & 1 deletion src/ibppeer/ibppeer.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,11 @@ import { Module } from '@nestjs/common';
import { IbppeerService } from './ibppeer.service';
import { IbppeerResolver } from './ibppeer.resolver';
import { ConfigmapModule } from 'src/configmap/configmap.module';
import { PodModule } from 'src/pod/pod.module';

@Module({
providers: [IbppeerService, IbppeerResolver],
exports: [IbppeerService],
imports: [ConfigmapModule],
imports: [ConfigmapModule, PodModule],
})
export class IbppeerModule {}
16 changes: 15 additions & 1 deletion src/ibppeer/ibppeer.resolver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,18 @@ import { Network } from 'src/network/models/network.model';
import { NetworkLoader } from 'src/network/network.loader';
import { Organization } from 'src/organization/models/organization.model';
import { OrganizationLoader } from 'src/organization/organization.loader';
import { Pod } from 'src/pod/models/pod.model';
import { PodService } from 'src/pod/pod.service';
import { JwtAuth } from 'src/types';
import { IbppeerService } from './ibppeer.service';
import { Ibppeer } from './models/ibppeer.model';

@Resolver(() => Ibppeer)
export class IbppeerResolver {
constructor(private readonly ibppeerService: IbppeerService) {}
constructor(
private readonly ibppeerService: IbppeerService,
private readonly podService: PodService,
) {}

@Query(() => [Ibppeer], { description: '获取组织下的节点列表' })
async ibppeers(
Expand Down Expand Up @@ -121,4 +126,13 @@ export class IbppeerResolver {
const { enrolluser } = ibppeer;
return enrolluser === preferred_username;
}

@ResolveField(() => Pod, {
description: '获取节点实时日志所需要的信息',
})
async pod(@Auth() auth: JwtAuth, @Parent() ibppeer: Ibppeer): Promise<Pod> {
const { name, namespace } = ibppeer;
const pods = await this.podService.getPods(auth, namespace, { app: name });
return pods[0];
}
}
13 changes: 13 additions & 0 deletions src/pod/models/pod.model.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { Field, ID, ObjectType } from '@nestjs/graphql';

@ObjectType({ description: 'pod 信息' })
export class Pod {
@Field(() => ID, { description: 'name' })
name: string;

/** namespace */
namespace: string;

/** 容器 */
containers: string[];
}
8 changes: 8 additions & 0 deletions src/pod/pod.module.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { Module } from '@nestjs/common';
import { PodService } from './pod.service';

@Module({
providers: [PodService],
exports: [PodService],
})
export class PodModule {}
37 changes: 37 additions & 0 deletions src/pod/pod.service.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import { Injectable } from '@nestjs/common';
import { KubernetesService } from 'src/kubernetes/kubernetes.service';
import { AnyObj, JwtAuth, K8s } from 'src/types';
import { Pod } from './models/pod.model';

@Injectable()
export class PodService {
constructor(private readonly k8sService: KubernetesService) {}

format(pod: K8s.V1Pod): Pod {
return {
name: pod.metadata?.name,
namespace: pod.metadata?.namespace,
containers: pod.spec?.containers?.map((c) => c?.name),
};
}

async getPod(auth: JwtAuth, name: string, namespace: string): Promise<Pod> {
const k8s = await this.k8sService.getClient(auth);
const { body } = await k8s.pod.read(name, namespace);
return this.format(body);
}

async getPods(
auth: JwtAuth,
namespace: string,
matchLabels?: AnyObj,
): Promise<Pod[]> {
const k8s = await this.k8sService.getClient(auth);
const { body } = await k8s.pod.list(namespace, {
labelSelector: Object.entries(matchLabels || {})
?.map((d) => `${d[0]}=${d[1]}`)
?.join(','),
});
return body.items.map((d) => this.format(d));
}
}
18 changes: 18 additions & 0 deletions src/schema.gql
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,9 @@ type Epolicy {
"""所在通道"""
channel: String!

"""所在通道名称(displayName)"""
channelDisplayName: String

"""创建时间"""
creationTimestamp: String

Expand Down Expand Up @@ -254,6 +257,9 @@ type Ibppeer {
"""加入的网络"""
networks: [String!]

"""获取节点实时日志所需要的信息"""
pod: Pod!

"""运行状态"""
status: CrdStatusType
}
Expand Down Expand Up @@ -642,6 +648,18 @@ type Organization {
users: [User!]
}

"""pod 信息"""
type Pod {
"""容器"""
containers: [String!]!

"""name"""
name: ID!

"""namespace"""
namespace: String!
}

type Proposal {
"""创建时间"""
creationTimestamp: String!
Expand Down