diff --git a/cmd/healthcheck.go b/cmd/healthcheck.go new file mode 100644 index 000000000..3ccd2871b --- /dev/null +++ b/cmd/healthcheck.go @@ -0,0 +1,38 @@ +package main + +import ( + "context" + "fmt" + + "github.com/crazy-max/diun/v4/pb" + "google.golang.org/grpc" + "google.golang.org/grpc/credentials/insecure" +) + +// HealthcheckCmd holds healthcheck command +type HealthcheckerCmd struct { + Test HealthcheckCmd `cmd:"" help:"Run a healthcheck."` +} + +// HealthcheckCmd holds healthcheck test command +type HealthcheckCmd struct { + GRPCAuthority string `name:"grpc-authority" default:"127.0.0.1:42286" help:"Link to Diun gRPC server."` +} + +func (s *HealthcheckCmd) Run(_ *Context) error { + conn, err := grpc.NewClient(s.GRPCAuthority, grpc.WithTransportCredentials(insecure.NewCredentials())) + if err != nil { + return err + } + defer conn.Close() + + healthcheckSvc := pb.NewHealthcheckServiceClient(conn) + + nt, err := healthcheckSvc.Healthcheck(context.Background(), &pb.HealthcheckRequest{}) + if err != nil { + return err + } + + fmt.Println(nt.Message) + return nil +} diff --git a/cmd/main.go b/cmd/main.go index 6d514f22c..a7cdbed2f 100644 --- a/cmd/main.go +++ b/cmd/main.go @@ -15,10 +15,11 @@ import ( var ( version = "dev" cli struct { - Version kong.VersionFlag `name:"version" help:"Print version information."` - Serve ServeCmd `cmd:"" help:"Starts Diun server."` - Image ImageCmd `cmd:"" help:"Manage image manifests."` - Notif NotifCmd `cmd:"" help:"Manage notifications."` + Version kong.VersionFlag `name:"version" help:"Print version information."` + Serve ServeCmd `cmd:"" help:"Starts Diun server."` + Image ImageCmd `cmd:"" help:"Manage image manifests."` + Notif NotifCmd `cmd:"" help:"Manage notifications."` + Healthcheck HealthcheckerCmd `cmd:"" help:"Verify that Diun is running."` } ) diff --git a/internal/grpc/client.go b/internal/grpc/client.go index 83fb0dbc0..f81f97bc9 100644 --- a/internal/grpc/client.go +++ b/internal/grpc/client.go @@ -21,6 +21,7 @@ type Client struct { notif *notif.Client pb.UnimplementedImageServiceServer pb.UnimplementedNotifServiceServer + pb.UnimplementedHealthcheckServiceServer } // New creates a new grpc instance @@ -36,6 +37,7 @@ func New(authority string, db *db.Client, notif *notif.Client) (*Client, error) c.server = grpc.NewServer() pb.RegisterImageServiceServer(c.server, c) pb.RegisterNotifServiceServer(c.server, c) + pb.RegisterHealthcheckServiceServer(c.server, c) return c, nil } diff --git a/internal/grpc/healthcheck.go b/internal/grpc/healthcheck.go new file mode 100644 index 000000000..b9f0e1769 --- /dev/null +++ b/internal/grpc/healthcheck.go @@ -0,0 +1,13 @@ +package grpc + +import ( + "context" + + "github.com/crazy-max/diun/v4/pb" +) + +func (c *Client) Healthcheck(_ context.Context, _ *pb.HealthcheckRequest) (*pb.HealthcheckResponse, error) { + return &pb.HealthcheckResponse{ + Message: "Diun is running", + }, nil +} diff --git a/pb/gen.go b/pb/gen.go index 9fbf0ce7b..9da53ea44 100644 --- a/pb/gen.go +++ b/pb/gen.go @@ -1,3 +1,3 @@ package pb -//go:generate protoc --go_out=paths=source_relative:. --go-grpc_out=paths=source_relative:. image.proto notif.proto +//go:generate protoc --go_out=paths=source_relative:. --go-grpc_out=paths=source_relative:. image.proto notif.proto healthcheck.proto diff --git a/pb/healthcheck.pb.go b/pb/healthcheck.pb.go new file mode 100644 index 000000000..cb3a710ee --- /dev/null +++ b/pb/healthcheck.pb.go @@ -0,0 +1,164 @@ +// Code generated by protoc-gen-go. DO NOT EDIT. +// versions: +// protoc-gen-go v1.36.11 +// protoc v3.17.3 +// source: healthcheck.proto + +package pb + +import ( + protoreflect "google.golang.org/protobuf/reflect/protoreflect" + protoimpl "google.golang.org/protobuf/runtime/protoimpl" + reflect "reflect" + sync "sync" + unsafe "unsafe" +) + +const ( + // Verify that this generated code is sufficiently up-to-date. + _ = protoimpl.EnforceVersion(20 - protoimpl.MinVersion) + // Verify that runtime/protoimpl is sufficiently up-to-date. + _ = protoimpl.EnforceVersion(protoimpl.MaxVersion - 20) +) + +type HealthcheckRequest struct { + state protoimpl.MessageState `protogen:"open.v1"` + unknownFields protoimpl.UnknownFields + sizeCache protoimpl.SizeCache +} + +func (x *HealthcheckRequest) Reset() { + *x = HealthcheckRequest{} + mi := &file_healthcheck_proto_msgTypes[0] + ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) + ms.StoreMessageInfo(mi) +} + +func (x *HealthcheckRequest) String() string { + return protoimpl.X.MessageStringOf(x) +} + +func (*HealthcheckRequest) ProtoMessage() {} + +func (x *HealthcheckRequest) ProtoReflect() protoreflect.Message { + mi := &file_healthcheck_proto_msgTypes[0] + if x != nil { + ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) + if ms.LoadMessageInfo() == nil { + ms.StoreMessageInfo(mi) + } + return ms + } + return mi.MessageOf(x) +} + +// Deprecated: Use HealthcheckRequest.ProtoReflect.Descriptor instead. +func (*HealthcheckRequest) Descriptor() ([]byte, []int) { + return file_healthcheck_proto_rawDescGZIP(), []int{0} +} + +type HealthcheckResponse struct { + state protoimpl.MessageState `protogen:"open.v1"` + Message string `protobuf:"bytes,1,opt,name=message,proto3" json:"message,omitempty"` + unknownFields protoimpl.UnknownFields + sizeCache protoimpl.SizeCache +} + +func (x *HealthcheckResponse) Reset() { + *x = HealthcheckResponse{} + mi := &file_healthcheck_proto_msgTypes[1] + ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) + ms.StoreMessageInfo(mi) +} + +func (x *HealthcheckResponse) String() string { + return protoimpl.X.MessageStringOf(x) +} + +func (*HealthcheckResponse) ProtoMessage() {} + +func (x *HealthcheckResponse) ProtoReflect() protoreflect.Message { + mi := &file_healthcheck_proto_msgTypes[1] + if x != nil { + ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) + if ms.LoadMessageInfo() == nil { + ms.StoreMessageInfo(mi) + } + return ms + } + return mi.MessageOf(x) +} + +// Deprecated: Use HealthcheckResponse.ProtoReflect.Descriptor instead. +func (*HealthcheckResponse) Descriptor() ([]byte, []int) { + return file_healthcheck_proto_rawDescGZIP(), []int{1} +} + +func (x *HealthcheckResponse) GetMessage() string { + if x != nil { + return x.Message + } + return "" +} + +var File_healthcheck_proto protoreflect.FileDescriptor + +const file_healthcheck_proto_rawDesc = "" + + "\n" + + "\x11healthcheck.proto\x12\x02pb\"\x14\n" + + "\x12HealthcheckRequest\"/\n" + + "\x13HealthcheckResponse\x12\x18\n" + + "\amessage\x18\x01 \x01(\tR\amessage2V\n" + + "\x12HealthcheckService\x12@\n" + + "\vHealthcheck\x12\x16.pb.HealthcheckRequest\x1a\x17.pb.HealthcheckResponse\"\x00B\x1eZ\x1cgithub.com/crazy-max/diun/pbb\x06proto3" + +var ( + file_healthcheck_proto_rawDescOnce sync.Once + file_healthcheck_proto_rawDescData []byte +) + +func file_healthcheck_proto_rawDescGZIP() []byte { + file_healthcheck_proto_rawDescOnce.Do(func() { + file_healthcheck_proto_rawDescData = protoimpl.X.CompressGZIP(unsafe.Slice(unsafe.StringData(file_healthcheck_proto_rawDesc), len(file_healthcheck_proto_rawDesc))) + }) + return file_healthcheck_proto_rawDescData +} + +var file_healthcheck_proto_msgTypes = make([]protoimpl.MessageInfo, 2) +var file_healthcheck_proto_goTypes = []any{ + (*HealthcheckRequest)(nil), // 0: pb.HealthcheckRequest + (*HealthcheckResponse)(nil), // 1: pb.HealthcheckResponse +} +var file_healthcheck_proto_depIdxs = []int32{ + 0, // 0: pb.HealthcheckService.Healthcheck:input_type -> pb.HealthcheckRequest + 1, // 1: pb.HealthcheckService.Healthcheck:output_type -> pb.HealthcheckResponse + 1, // [1:2] is the sub-list for method output_type + 0, // [0:1] is the sub-list for method input_type + 0, // [0:0] is the sub-list for extension type_name + 0, // [0:0] is the sub-list for extension extendee + 0, // [0:0] is the sub-list for field type_name +} + +func init() { file_healthcheck_proto_init() } +func file_healthcheck_proto_init() { + if File_healthcheck_proto != nil { + return + } + type x struct{} + out := protoimpl.TypeBuilder{ + File: protoimpl.DescBuilder{ + GoPackagePath: reflect.TypeOf(x{}).PkgPath(), + RawDescriptor: unsafe.Slice(unsafe.StringData(file_healthcheck_proto_rawDesc), len(file_healthcheck_proto_rawDesc)), + NumEnums: 0, + NumMessages: 2, + NumExtensions: 0, + NumServices: 1, + }, + GoTypes: file_healthcheck_proto_goTypes, + DependencyIndexes: file_healthcheck_proto_depIdxs, + MessageInfos: file_healthcheck_proto_msgTypes, + }.Build() + File_healthcheck_proto = out.File + file_healthcheck_proto_goTypes = nil + file_healthcheck_proto_depIdxs = nil +} diff --git a/pb/healthcheck.proto b/pb/healthcheck.proto new file mode 100644 index 000000000..3f1d32d0f --- /dev/null +++ b/pb/healthcheck.proto @@ -0,0 +1,14 @@ +syntax = "proto3"; +option go_package = "github.com/crazy-max/diun/pb"; + +package pb; + +message HealthcheckRequest {} + +message HealthcheckResponse { + string message = 1; +} + +service HealthcheckService { + rpc Healthcheck(HealthcheckRequest) returns (HealthcheckResponse) {} +} diff --git a/pb/healthcheck_grpc.pb.go b/pb/healthcheck_grpc.pb.go new file mode 100644 index 000000000..ad3185610 --- /dev/null +++ b/pb/healthcheck_grpc.pb.go @@ -0,0 +1,121 @@ +// Code generated by protoc-gen-go-grpc. DO NOT EDIT. +// versions: +// - protoc-gen-go-grpc v1.6.0 +// - protoc v3.17.3 +// source: healthcheck.proto + +package pb + +import ( + context "context" + grpc "google.golang.org/grpc" + codes "google.golang.org/grpc/codes" + status "google.golang.org/grpc/status" +) + +// This is a compile-time assertion to ensure that this generated file +// is compatible with the grpc package it is being compiled against. +// Requires gRPC-Go v1.64.0 or later. +const _ = grpc.SupportPackageIsVersion9 + +const ( + HealthcheckService_Healthcheck_FullMethodName = "/pb.HealthcheckService/Healthcheck" +) + +// HealthcheckServiceClient is the client API for HealthcheckService service. +// +// For semantics around ctx use and closing/ending streaming RPCs, please refer to https://pkg.go.dev/google.golang.org/grpc/?tab=doc#ClientConn.NewStream. +type HealthcheckServiceClient interface { + Healthcheck(ctx context.Context, in *HealthcheckRequest, opts ...grpc.CallOption) (*HealthcheckResponse, error) +} + +type healthcheckServiceClient struct { + cc grpc.ClientConnInterface +} + +func NewHealthcheckServiceClient(cc grpc.ClientConnInterface) HealthcheckServiceClient { + return &healthcheckServiceClient{cc} +} + +func (c *healthcheckServiceClient) Healthcheck(ctx context.Context, in *HealthcheckRequest, opts ...grpc.CallOption) (*HealthcheckResponse, error) { + cOpts := append([]grpc.CallOption{grpc.StaticMethod()}, opts...) + out := new(HealthcheckResponse) + err := c.cc.Invoke(ctx, HealthcheckService_Healthcheck_FullMethodName, in, out, cOpts...) + if err != nil { + return nil, err + } + return out, nil +} + +// HealthcheckServiceServer is the server API for HealthcheckService service. +// All implementations must embed UnimplementedHealthcheckServiceServer +// for forward compatibility. +type HealthcheckServiceServer interface { + Healthcheck(context.Context, *HealthcheckRequest) (*HealthcheckResponse, error) + mustEmbedUnimplementedHealthcheckServiceServer() +} + +// UnimplementedHealthcheckServiceServer must be embedded to have +// forward compatible implementations. +// +// NOTE: this should be embedded by value instead of pointer to avoid a nil +// pointer dereference when methods are called. +type UnimplementedHealthcheckServiceServer struct{} + +func (UnimplementedHealthcheckServiceServer) Healthcheck(context.Context, *HealthcheckRequest) (*HealthcheckResponse, error) { + return nil, status.Error(codes.Unimplemented, "method Healthcheck not implemented") +} +func (UnimplementedHealthcheckServiceServer) mustEmbedUnimplementedHealthcheckServiceServer() {} +func (UnimplementedHealthcheckServiceServer) testEmbeddedByValue() {} + +// UnsafeHealthcheckServiceServer may be embedded to opt out of forward compatibility for this service. +// Use of this interface is not recommended, as added methods to HealthcheckServiceServer will +// result in compilation errors. +type UnsafeHealthcheckServiceServer interface { + mustEmbedUnimplementedHealthcheckServiceServer() +} + +func RegisterHealthcheckServiceServer(s grpc.ServiceRegistrar, srv HealthcheckServiceServer) { + // If the following call panics, it indicates UnimplementedHealthcheckServiceServer was + // embedded by pointer and is nil. This will cause panics if an + // unimplemented method is ever invoked, so we test this at initialization + // time to prevent it from happening at runtime later due to I/O. + if t, ok := srv.(interface{ testEmbeddedByValue() }); ok { + t.testEmbeddedByValue() + } + s.RegisterService(&HealthcheckService_ServiceDesc, srv) +} + +func _HealthcheckService_Healthcheck_Handler(srv interface{}, ctx context.Context, dec func(interface{}) error, interceptor grpc.UnaryServerInterceptor) (interface{}, error) { + in := new(HealthcheckRequest) + if err := dec(in); err != nil { + return nil, err + } + if interceptor == nil { + return srv.(HealthcheckServiceServer).Healthcheck(ctx, in) + } + info := &grpc.UnaryServerInfo{ + Server: srv, + FullMethod: HealthcheckService_Healthcheck_FullMethodName, + } + handler := func(ctx context.Context, req interface{}) (interface{}, error) { + return srv.(HealthcheckServiceServer).Healthcheck(ctx, req.(*HealthcheckRequest)) + } + return interceptor(ctx, in, info, handler) +} + +// HealthcheckService_ServiceDesc is the grpc.ServiceDesc for HealthcheckService service. +// It's only intended for direct use with grpc.RegisterService, +// and not to be introspected or modified (even as a copy) +var HealthcheckService_ServiceDesc = grpc.ServiceDesc{ + ServiceName: "pb.HealthcheckService", + HandlerType: (*HealthcheckServiceServer)(nil), + Methods: []grpc.MethodDesc{ + { + MethodName: "Healthcheck", + Handler: _HealthcheckService_Healthcheck_Handler, + }, + }, + Streams: []grpc.StreamDesc{}, + Metadata: "healthcheck.proto", +}