Sergiy Markin 4ae2c3a101 Fixed lint and unit tests
This PS makes sure we have linter and unit tests
processed. The code has been reformatted to adhere
to Go's code formatting conventions.

Change-Id: I31f15d6d6c4b9bda7e3837941b6c9c3c3735aea7
2024-03-26 19:41:48 +00:00

55 lines
1.2 KiB
Go

package service
import (
"context"
"fmt"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
entry "opendev.org/airship/kubernetes-entrypoint/entrypoint"
"opendev.org/airship/kubernetes-entrypoint/util/env"
)
const FailingStatusFormat = "Service %v has no endpoints"
type Service struct {
name string
namespace string
}
func init() {
serviceEnv := fmt.Sprintf("%sSERVICE", entry.DependencyPrefix)
if serviceDeps := env.SplitEnvToDeps(serviceEnv); serviceDeps != nil {
if len(serviceDeps) > 0 {
for _, dep := range serviceDeps {
entry.Register(NewService(dep.Name, dep.Namespace))
}
}
}
}
func NewService(name string, namespace string) Service {
return Service{
name: name,
namespace: namespace,
}
}
func (s Service) IsResolved(ctx context.Context, entrypoint entry.EntrypointInterface) (bool, error) {
e, err := entrypoint.Client().Endpoints(s.namespace).Get(ctx, s.name, metav1.GetOptions{})
if err != nil {
return false, err
}
for _, subset := range e.Subsets {
if len(subset.Addresses) > 0 {
return true, nil
}
}
return false, fmt.Errorf(FailingStatusFormat, s.name)
}
func (s Service) String() string {
return fmt.Sprintf("Service %s in namespace %s", s.name, s.namespace)
}