Skip to content
Snippets Groups Projects
Commit f0f1e598 authored by Michał Bień's avatar Michał Bień
Browse files

Added readiness service

parent a0e3f985
No related branches found
No related tags found
No related merge requests found
...@@ -7,6 +7,7 @@ option java_outer_classname = "JanitorManager"; ...@@ -7,6 +7,7 @@ option java_outer_classname = "JanitorManager";
enum Status { enum Status {
FAILED = 0; FAILED = 0;
OK = 1; OK = 1;
PENDING = 2;
} }
message Instance { message Instance {
...@@ -49,4 +50,8 @@ service BasicAuthService { ...@@ -49,4 +50,8 @@ service BasicAuthService {
service CertManagerService { service CertManagerService {
rpc DeleteIfExists(InstanceRequest) returns (ServiceResponse); rpc DeleteIfExists(InstanceRequest) returns (ServiceResponse);
}
service ReadinessService {
rpc CheckIfReady(InstanceRequest) returns (ServiceResponse);
} }
\ No newline at end of file
...@@ -56,6 +56,7 @@ func RunServer() error { ...@@ -56,6 +56,7 @@ func RunServer() error {
confAPI := v1.NewConfigServiceServer(kubeAPI, gitAPI) confAPI := v1.NewConfigServiceServer(kubeAPI, gitAPI)
authAPI := v1.NewBasicAuthServiceServer(kubeAPI) authAPI := v1.NewBasicAuthServiceServer(kubeAPI)
certAPI := v1.NewCertManagerServiceServer(kubeAPI) certAPI := v1.NewCertManagerServiceServer(kubeAPI)
readyAPI := v1.NewReadinessServiceServer(kubeAPI)
return grpc.RunServer(ctx, confAPI, authAPI, certAPI, cfg.GRPCPort) return grpc.RunServer(ctx, confAPI, authAPI, certAPI, readyAPI, cfg.GRPCPort)
} }
\ No newline at end of file
...@@ -11,7 +11,8 @@ import ( ...@@ -11,7 +11,8 @@ import (
"code.geant.net/stash/scm/nmaas/nmaas-janitor/pkg/api/v1" "code.geant.net/stash/scm/nmaas/nmaas-janitor/pkg/api/v1"
) )
func RunServer(ctx context.Context, confAPI v1.ConfigServiceServer, authAPI v1.BasicAuthServiceServer, certAPI v1.CertManagerServiceServer, port string) error { func RunServer(ctx context.Context, confAPI v1.ConfigServiceServer, authAPI v1.BasicAuthServiceServer,
certAPI v1.CertManagerServiceServer, readyAPI v1.ReadinessServiceServer, port string) error {
listen, err := net.Listen("tcp", ":"+port) listen, err := net.Listen("tcp", ":"+port)
if err != nil { if err != nil {
return err return err
...@@ -22,6 +23,7 @@ func RunServer(ctx context.Context, confAPI v1.ConfigServiceServer, authAPI v1.B ...@@ -22,6 +23,7 @@ func RunServer(ctx context.Context, confAPI v1.ConfigServiceServer, authAPI v1.B
v1.RegisterConfigServiceServer(server, confAPI) v1.RegisterConfigServiceServer(server, confAPI)
v1.RegisterBasicAuthServiceServer(server, authAPI) v1.RegisterBasicAuthServiceServer(server, authAPI)
v1.RegisterCertManagerServiceServer(server, certAPI) v1.RegisterCertManagerServiceServer(server, certAPI)
v1.RegisterReadinessServiceServer(server, readyAPI)
// graceful shutdown // graceful shutdown
c := make(chan os.Signal, 1) c := make(chan os.Signal, 1)
......
...@@ -33,6 +33,10 @@ type certManagerServiceServer struct { ...@@ -33,6 +33,10 @@ type certManagerServiceServer struct {
kubeAPI kube.CoreV1Interface kubeAPI kube.CoreV1Interface
} }
type readinessServiceServer struct {
kubeAPI kube.CoreV1Interface
}
func NewConfigServiceServer(kubeAPI kube.CoreV1Interface, gitAPI *gitlab.Client) v1.ConfigServiceServer { func NewConfigServiceServer(kubeAPI kube.CoreV1Interface, gitAPI *gitlab.Client) v1.ConfigServiceServer {
return &configServiceServer{kubeAPI: kubeAPI, gitAPI: gitAPI} return &configServiceServer{kubeAPI: kubeAPI, gitAPI: gitAPI}
} }
...@@ -45,6 +49,10 @@ func NewCertManagerServiceServer(kubeAPI kube.CoreV1Interface) v1.CertManagerSer ...@@ -45,6 +49,10 @@ func NewCertManagerServiceServer(kubeAPI kube.CoreV1Interface) v1.CertManagerSer
return &certManagerServiceServer{kubeAPI: kubeAPI} return &certManagerServiceServer{kubeAPI: kubeAPI}
} }
func NewReadinessServiceServer(kubeAPI kube.CoreV1Interface) v1.ReadinessServiceServer {
return &readinessServiceServer{kubeAPI: kubeAPI}
}
func checkAPI(api string) error { func checkAPI(api string) error {
if len(api) > 0 { if len(api) > 0 {
if apiVersion != api { if apiVersion != api {
...@@ -377,4 +385,46 @@ func (s *certManagerServiceServer) DeleteIfExists(ctx context.Context, req *v1.I ...@@ -377,4 +385,46 @@ func (s *certManagerServiceServer) DeleteIfExists(ctx context.Context, req *v1.I
} }
return prepareResponse(v1.Status_OK, "Secret deleted successfully"), nil return prepareResponse(v1.Status_OK, "Secret deleted successfully"), nil
}
func (s *readinessServiceServer) CheckIfReady(ctx context.Context, req *v1.InstanceRequest) (*v1.ServiceResponse, error) {
// check if the API version requested by client is supported by server
if err := checkAPI(req.Api); err != nil {
return nil, err
}
depl := req.Deployment
//check if given k8s namespace exists
_, err := s.kubeAPI.Namespaces().Get(depl.Namespace, metav1.GetOptions{})
if err != nil {
return prepareResponse(v1.Status_FAILED, "Namespace not found!"), err
}
app, err := s.kubeAPI.ReplicationControllers(depl.Namespace).Get(depl.Uid, metav1.GetOptions{})
if err != nil {
return prepareResponse(v1.Status_FAILED, "Deployment not found!"), err
}
if *app.Spec.Replicas == app.Status.ReadyReplicas {
return prepareResponse(v1.Status_OK, "Deployment is ready"), nil
}
pod, err := s.kubeAPI.Pods(depl.Namespace).Get(depl.Uid, metav1.GetOptions{})
if err != nil {
return prepareResponse(v1.Status_FAILED, "Pod not found!"), err
}
switch pod.Status.Phase {
case apiv1.PodUnknown:
case apiv1.PodFailed:
default:
return prepareResponse(v1.Status_FAILED, "Pod is in wrong state or has crashed"), nil
case apiv1.PodPending:
return prepareResponse(v1.Status_PENDING, "Pod is pending"), nil
case apiv1.PodRunning:
return prepareResponse(v1.Status_PENDING, "Pod has not initialized yet"), nil
}
return prepareResponse(v1.Status_PENDING, "Unknown status"), err
} }
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment