diff --git a/README.md b/README.md index 1576ccc..796ac0d 100644 --- a/README.md +++ b/README.md @@ -15,6 +15,7 @@ This Microservice provides the source of truth for master data. * limit max clusters * tenant members * tenant project members +* version ## Design diff --git a/api/v1/version.pb.go b/api/v1/version.pb.go new file mode 100644 index 0000000..58a14b8 --- /dev/null +++ b/api/v1/version.pb.go @@ -0,0 +1,205 @@ +// Code generated by protoc-gen-go. DO NOT EDIT. +// versions: +// protoc-gen-go v1.36.5 +// protoc (unknown) +// source: v1/version.proto + +package v1 + +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 GetVersionRequest struct { + state protoimpl.MessageState `protogen:"open.v1"` + unknownFields protoimpl.UnknownFields + sizeCache protoimpl.SizeCache +} + +func (x *GetVersionRequest) Reset() { + *x = GetVersionRequest{} + mi := &file_v1_version_proto_msgTypes[0] + ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) + ms.StoreMessageInfo(mi) +} + +func (x *GetVersionRequest) String() string { + return protoimpl.X.MessageStringOf(x) +} + +func (*GetVersionRequest) ProtoMessage() {} + +func (x *GetVersionRequest) ProtoReflect() protoreflect.Message { + mi := &file_v1_version_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 GetVersionRequest.ProtoReflect.Descriptor instead. +func (*GetVersionRequest) Descriptor() ([]byte, []int) { + return file_v1_version_proto_rawDescGZIP(), []int{0} +} + +type GetVersionResponse struct { + state protoimpl.MessageState `protogen:"open.v1"` + Version string `protobuf:"bytes,1,opt,name=version,proto3" json:"version,omitempty"` + Revision string `protobuf:"bytes,2,opt,name=revision,proto3" json:"revision,omitempty"` + GitSha1 string `protobuf:"bytes,3,opt,name=git_sha1,json=gitSha1,proto3" json:"git_sha1,omitempty"` + BuildDate string `protobuf:"bytes,4,opt,name=build_date,json=buildDate,proto3" json:"build_date,omitempty"` + unknownFields protoimpl.UnknownFields + sizeCache protoimpl.SizeCache +} + +func (x *GetVersionResponse) Reset() { + *x = GetVersionResponse{} + mi := &file_v1_version_proto_msgTypes[1] + ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) + ms.StoreMessageInfo(mi) +} + +func (x *GetVersionResponse) String() string { + return protoimpl.X.MessageStringOf(x) +} + +func (*GetVersionResponse) ProtoMessage() {} + +func (x *GetVersionResponse) ProtoReflect() protoreflect.Message { + mi := &file_v1_version_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 GetVersionResponse.ProtoReflect.Descriptor instead. +func (*GetVersionResponse) Descriptor() ([]byte, []int) { + return file_v1_version_proto_rawDescGZIP(), []int{1} +} + +func (x *GetVersionResponse) GetVersion() string { + if x != nil { + return x.Version + } + return "" +} + +func (x *GetVersionResponse) GetRevision() string { + if x != nil { + return x.Revision + } + return "" +} + +func (x *GetVersionResponse) GetGitSha1() string { + if x != nil { + return x.GitSha1 + } + return "" +} + +func (x *GetVersionResponse) GetBuildDate() string { + if x != nil { + return x.BuildDate + } + return "" +} + +var File_v1_version_proto protoreflect.FileDescriptor + +var file_v1_version_proto_rawDesc = string([]byte{ + 0x0a, 0x10, 0x76, 0x31, 0x2f, 0x76, 0x65, 0x72, 0x73, 0x69, 0x6f, 0x6e, 0x2e, 0x70, 0x72, 0x6f, + 0x74, 0x6f, 0x12, 0x02, 0x76, 0x31, 0x22, 0x13, 0x0a, 0x11, 0x47, 0x65, 0x74, 0x56, 0x65, 0x72, + 0x73, 0x69, 0x6f, 0x6e, 0x52, 0x65, 0x71, 0x75, 0x65, 0x73, 0x74, 0x22, 0x84, 0x01, 0x0a, 0x12, + 0x47, 0x65, 0x74, 0x56, 0x65, 0x72, 0x73, 0x69, 0x6f, 0x6e, 0x52, 0x65, 0x73, 0x70, 0x6f, 0x6e, + 0x73, 0x65, 0x12, 0x18, 0x0a, 0x07, 0x76, 0x65, 0x72, 0x73, 0x69, 0x6f, 0x6e, 0x18, 0x01, 0x20, + 0x01, 0x28, 0x09, 0x52, 0x07, 0x76, 0x65, 0x72, 0x73, 0x69, 0x6f, 0x6e, 0x12, 0x1a, 0x0a, 0x08, + 0x72, 0x65, 0x76, 0x69, 0x73, 0x69, 0x6f, 0x6e, 0x18, 0x02, 0x20, 0x01, 0x28, 0x09, 0x52, 0x08, + 0x72, 0x65, 0x76, 0x69, 0x73, 0x69, 0x6f, 0x6e, 0x12, 0x19, 0x0a, 0x08, 0x67, 0x69, 0x74, 0x5f, + 0x73, 0x68, 0x61, 0x31, 0x18, 0x03, 0x20, 0x01, 0x28, 0x09, 0x52, 0x07, 0x67, 0x69, 0x74, 0x53, + 0x68, 0x61, 0x31, 0x12, 0x1d, 0x0a, 0x0a, 0x62, 0x75, 0x69, 0x6c, 0x64, 0x5f, 0x64, 0x61, 0x74, + 0x65, 0x18, 0x04, 0x20, 0x01, 0x28, 0x09, 0x52, 0x09, 0x62, 0x75, 0x69, 0x6c, 0x64, 0x44, 0x61, + 0x74, 0x65, 0x32, 0x46, 0x0a, 0x0e, 0x56, 0x65, 0x72, 0x73, 0x69, 0x6f, 0x6e, 0x53, 0x65, 0x72, + 0x76, 0x69, 0x63, 0x65, 0x12, 0x34, 0x0a, 0x03, 0x47, 0x65, 0x74, 0x12, 0x15, 0x2e, 0x76, 0x31, + 0x2e, 0x47, 0x65, 0x74, 0x56, 0x65, 0x72, 0x73, 0x69, 0x6f, 0x6e, 0x52, 0x65, 0x71, 0x75, 0x65, + 0x73, 0x74, 0x1a, 0x16, 0x2e, 0x76, 0x31, 0x2e, 0x47, 0x65, 0x74, 0x56, 0x65, 0x72, 0x73, 0x69, + 0x6f, 0x6e, 0x52, 0x65, 0x73, 0x70, 0x6f, 0x6e, 0x73, 0x65, 0x42, 0x68, 0x0a, 0x06, 0x63, 0x6f, + 0x6d, 0x2e, 0x76, 0x31, 0x42, 0x0c, 0x56, 0x65, 0x72, 0x73, 0x69, 0x6f, 0x6e, 0x50, 0x72, 0x6f, + 0x74, 0x6f, 0x50, 0x01, 0x5a, 0x28, 0x67, 0x69, 0x74, 0x68, 0x75, 0x62, 0x2e, 0x63, 0x6f, 0x6d, + 0x2f, 0x6d, 0x65, 0x74, 0x61, 0x6c, 0x2d, 0x73, 0x74, 0x61, 0x63, 0x6b, 0x2f, 0x6d, 0x61, 0x73, + 0x74, 0x65, 0x72, 0x64, 0x61, 0x74, 0x61, 0x2d, 0x61, 0x70, 0x69, 0x2f, 0x76, 0x31, 0xa2, 0x02, + 0x03, 0x56, 0x58, 0x58, 0xaa, 0x02, 0x02, 0x56, 0x31, 0xca, 0x02, 0x02, 0x56, 0x31, 0xe2, 0x02, + 0x0e, 0x56, 0x31, 0x5c, 0x47, 0x50, 0x42, 0x4d, 0x65, 0x74, 0x61, 0x64, 0x61, 0x74, 0x61, 0xea, + 0x02, 0x02, 0x56, 0x31, 0x62, 0x06, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x33, +}) + +var ( + file_v1_version_proto_rawDescOnce sync.Once + file_v1_version_proto_rawDescData []byte +) + +func file_v1_version_proto_rawDescGZIP() []byte { + file_v1_version_proto_rawDescOnce.Do(func() { + file_v1_version_proto_rawDescData = protoimpl.X.CompressGZIP(unsafe.Slice(unsafe.StringData(file_v1_version_proto_rawDesc), len(file_v1_version_proto_rawDesc))) + }) + return file_v1_version_proto_rawDescData +} + +var file_v1_version_proto_msgTypes = make([]protoimpl.MessageInfo, 2) +var file_v1_version_proto_goTypes = []any{ + (*GetVersionRequest)(nil), // 0: v1.GetVersionRequest + (*GetVersionResponse)(nil), // 1: v1.GetVersionResponse +} +var file_v1_version_proto_depIdxs = []int32{ + 0, // 0: v1.VersionService.Get:input_type -> v1.GetVersionRequest + 1, // 1: v1.VersionService.Get:output_type -> v1.GetVersionResponse + 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_v1_version_proto_init() } +func file_v1_version_proto_init() { + if File_v1_version_proto != nil { + return + } + type x struct{} + out := protoimpl.TypeBuilder{ + File: protoimpl.DescBuilder{ + GoPackagePath: reflect.TypeOf(x{}).PkgPath(), + RawDescriptor: unsafe.Slice(unsafe.StringData(file_v1_version_proto_rawDesc), len(file_v1_version_proto_rawDesc)), + NumEnums: 0, + NumMessages: 2, + NumExtensions: 0, + NumServices: 1, + }, + GoTypes: file_v1_version_proto_goTypes, + DependencyIndexes: file_v1_version_proto_depIdxs, + MessageInfos: file_v1_version_proto_msgTypes, + }.Build() + File_v1_version_proto = out.File + file_v1_version_proto_goTypes = nil + file_v1_version_proto_depIdxs = nil +} diff --git a/api/v1/version_grpc.pb.go b/api/v1/version_grpc.pb.go new file mode 100644 index 0000000..fbb372d --- /dev/null +++ b/api/v1/version_grpc.pb.go @@ -0,0 +1,119 @@ +// Code generated by protoc-gen-go-grpc. DO NOT EDIT. +// versions: +// - protoc-gen-go-grpc v1.5.1 +// - protoc (unknown) +// source: v1/version.proto + +package v1 + +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 ( + VersionService_Get_FullMethodName = "/v1.VersionService/Get" +) + +// VersionServiceClient is the client API for VersionService 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 VersionServiceClient interface { + Get(ctx context.Context, in *GetVersionRequest, opts ...grpc.CallOption) (*GetVersionResponse, error) +} + +type versionServiceClient struct { + cc grpc.ClientConnInterface +} + +func NewVersionServiceClient(cc grpc.ClientConnInterface) VersionServiceClient { + return &versionServiceClient{cc} +} + +func (c *versionServiceClient) Get(ctx context.Context, in *GetVersionRequest, opts ...grpc.CallOption) (*GetVersionResponse, error) { + cOpts := append([]grpc.CallOption{grpc.StaticMethod()}, opts...) + out := new(GetVersionResponse) + err := c.cc.Invoke(ctx, VersionService_Get_FullMethodName, in, out, cOpts...) + if err != nil { + return nil, err + } + return out, nil +} + +// VersionServiceServer is the server API for VersionService service. +// All implementations should embed UnimplementedVersionServiceServer +// for forward compatibility. +type VersionServiceServer interface { + Get(context.Context, *GetVersionRequest) (*GetVersionResponse, error) +} + +// UnimplementedVersionServiceServer should 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 UnimplementedVersionServiceServer struct{} + +func (UnimplementedVersionServiceServer) Get(context.Context, *GetVersionRequest) (*GetVersionResponse, error) { + return nil, status.Errorf(codes.Unimplemented, "method Get not implemented") +} +func (UnimplementedVersionServiceServer) testEmbeddedByValue() {} + +// UnsafeVersionServiceServer may be embedded to opt out of forward compatibility for this service. +// Use of this interface is not recommended, as added methods to VersionServiceServer will +// result in compilation errors. +type UnsafeVersionServiceServer interface { + mustEmbedUnimplementedVersionServiceServer() +} + +func RegisterVersionServiceServer(s grpc.ServiceRegistrar, srv VersionServiceServer) { + // If the following call pancis, it indicates UnimplementedVersionServiceServer 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(&VersionService_ServiceDesc, srv) +} + +func _VersionService_Get_Handler(srv interface{}, ctx context.Context, dec func(interface{}) error, interceptor grpc.UnaryServerInterceptor) (interface{}, error) { + in := new(GetVersionRequest) + if err := dec(in); err != nil { + return nil, err + } + if interceptor == nil { + return srv.(VersionServiceServer).Get(ctx, in) + } + info := &grpc.UnaryServerInfo{ + Server: srv, + FullMethod: VersionService_Get_FullMethodName, + } + handler := func(ctx context.Context, req interface{}) (interface{}, error) { + return srv.(VersionServiceServer).Get(ctx, req.(*GetVersionRequest)) + } + return interceptor(ctx, in, info, handler) +} + +// VersionService_ServiceDesc is the grpc.ServiceDesc for VersionService service. +// It's only intended for direct use with grpc.RegisterService, +// and not to be introspected or modified (even as a copy) +var VersionService_ServiceDesc = grpc.ServiceDesc{ + ServiceName: "v1.VersionService", + HandlerType: (*VersionServiceServer)(nil), + Methods: []grpc.MethodDesc{ + { + MethodName: "Get", + Handler: _VersionService_Get_Handler, + }, + }, + Streams: []grpc.StreamDesc{}, + Metadata: "v1/version.proto", +} diff --git a/pkg/service/version.go b/pkg/service/version.go new file mode 100644 index 0000000..93d99a7 --- /dev/null +++ b/pkg/service/version.go @@ -0,0 +1,19 @@ +package service + +import ( + "context" + + v1 "github.com/metal-stack/masterdata-api/api/v1" + "github.com/metal-stack/v" +) + +type versionService struct { +} + +func NewVersionService() *versionService { + return &versionService{} +} +func (vs *versionService) Get(ctx context.Context, request *v1.GetVersionRequest) (*v1.GetVersionResponse, error) { + res := v1.GetVersionResponse{Version: v.Version, Revision: v.Revision, BuildDate: v.BuildDate, GitSha1: v.GitSHA1} + return &res, nil +} diff --git a/pkg/service/version_test.go b/pkg/service/version_test.go new file mode 100644 index 0000000..1648208 --- /dev/null +++ b/pkg/service/version_test.go @@ -0,0 +1,28 @@ +package service + +import ( + "context" + "testing" + + v1 "github.com/metal-stack/masterdata-api/api/v1" + "github.com/metal-stack/v" + "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" +) + +func TestGetVersion(t *testing.T) { + + vs := &versionService{} + ctx := context.Background() + + expected := v1.GetVersionResponse{Version: v.Version, Revision: v.Revision, BuildDate: v.BuildDate, GitSha1: v.GitSHA1} + + result, err := vs.Get(ctx, &v1.GetVersionRequest{}) + + require.NoError(t, err) + assert.NotNil(t, result) + assert.Equal(t, expected.Version, result.Version) + assert.Equal(t, expected.Revision, result.Revision) + assert.Equal(t, expected.BuildDate, result.BuildDate) + assert.Equal(t, expected.GitSha1, result.GitSha1) +} diff --git a/proto/v1/version.proto b/proto/v1/version.proto new file mode 100644 index 0000000..10ad748 --- /dev/null +++ b/proto/v1/version.proto @@ -0,0 +1,16 @@ +syntax = "proto3"; + +package v1; + +service VersionService { + rpc Get(GetVersionRequest) returns (GetVersionResponse); +} + +message GetVersionRequest {} + +message GetVersionResponse { + string version = 1; + string revision = 2; + string git_sha1 = 3; + string build_date = 4; +} diff --git a/server/main.go b/server/main.go index 6eed43f..ae7dba7 100644 --- a/server/main.go +++ b/server/main.go @@ -247,11 +247,13 @@ func run() error { // FIXME db should not be required here tenantService := service.NewTenantService(db, logger, ts, tms) tenantMemberService := service.NewTenantMemberService(logger, ts, tms) + versionService := service.NewVersionService() apiv1.RegisterProjectServiceServer(grpcServer, projectService) apiv1.RegisterProjectMemberServiceServer(grpcServer, projectMemberService) apiv1.RegisterTenantServiceServer(grpcServer, tenantService) apiv1.RegisterTenantMemberServiceServer(grpcServer, tenantMemberService) + apiv1.RegisterVersionServiceServer(grpcServer, versionService) healthv1.RegisterHealthServer(grpcServer, healthServer) srvMetrics.InitializeMetrics(grpcServer)