diff --git a/runtime/kubernetes/kubernetes.go b/runtime/kubernetes/kubernetes.go index eabdb4b8..293597a5 100644 --- a/runtime/kubernetes/kubernetes.go +++ b/runtime/kubernetes/kubernetes.go @@ -270,9 +270,9 @@ func (k *kubernetes) Create(s *runtime.Service, opts ...runtime.CreateOption) er if len(options.Type) == 0 { options.Type = k.options.Type } - if len(k.options.Source) > 0 { - s.Source = k.options.Source - } + + // determine the full source for this service + options.Source = k.sourceForService(s.Name) service := newService(s, options) @@ -329,7 +329,8 @@ func (k *kubernetes) List() ([]*runtime.Service, error) { func (k *kubernetes) Update(s *runtime.Service) error { // create new kubernetes micro service service := newService(s, runtime.CreateOptions{ - Type: k.options.Type, + Type: k.options.Type, + Source: k.sourceForService(s.Name), }) // update build time annotation @@ -432,3 +433,15 @@ func NewRuntime(opts ...runtime.Option) runtime.Runtime { client: client, } } + +// sourceForService determines the nested package name for github +// e.g src: docker.pkg.github.com/micro/services an srv: users/api +// would become docker.pkg.github.com/micro/services/users-api +func (k *kubernetes) sourceForService(name string) string { + if !strings.HasPrefix(k.options.Source, "docker.pkg.github.com") { + return k.options.Source + } + + formattedName := strings.ReplaceAll(name, "/", "-") + return fmt.Sprintf("%v/%v", k.options.Source, formattedName) +} diff --git a/runtime/kubernetes/service.go b/runtime/kubernetes/service.go index 60fb31a5..72fcc636 100644 --- a/runtime/kubernetes/service.go +++ b/runtime/kubernetes/service.go @@ -34,9 +34,9 @@ func newService(s *runtime.Service, c runtime.CreateOptions) *service { kservice := client.NewService(name, version, c.Type) kdeploy := client.NewDeployment(name, version, c.Type) - if len(s.Source) > 0 { + if len(c.Source) > 0 { for i := range kdeploy.Spec.Template.PodSpec.Containers { - kdeploy.Spec.Template.PodSpec.Containers[i].Image = s.Source + kdeploy.Spec.Template.PodSpec.Containers[i].Image = c.Source kdeploy.Spec.Template.PodSpec.Containers[i].Command = []string{} kdeploy.Spec.Template.PodSpec.Containers[i].Args = []string{name} } diff --git a/runtime/options.go b/runtime/options.go index 197f3d7f..ab73a02e 100644 --- a/runtime/options.go +++ b/runtime/options.go @@ -53,6 +53,8 @@ type CreateOptions struct { Type string // Retries before failing deploy Retries int + // Source of the service + Source string } // ReadOptions queries runtime services @@ -72,6 +74,13 @@ func CreateType(t string) CreateOption { } } +// CreateSource sets the source of service to create +func CreateSource(t string) CreateOption { + return func(o *CreateOptions) { + o.Source = t + } +} + // WithCommand specifies the command to execute func WithCommand(args ...string) CreateOption { return func(o *CreateOptions) { diff --git a/runtime/service/service.go b/runtime/service/service.go index c46c5ba4..f7e3e97a 100644 --- a/runtime/service/service.go +++ b/runtime/service/service.go @@ -35,6 +35,11 @@ func (s *svc) Create(svc *runtime.Service, opts ...runtime.CreateOption) error { o(&options) } + // set the default source from MICRO_RUNTIME_SOURCE + if len(svc.Source) == 0 { + svc.Source = s.options.Source + } + // runtime service create request req := &pb.CreateRequest{ Service: &pb.Service{ diff --git a/util/kubernetes/client/util.go b/util/kubernetes/client/util.go index 83e17b11..747ac9cb 100644 --- a/util/kubernetes/client/util.go +++ b/util/kubernetes/client/util.go @@ -91,6 +91,8 @@ func CertsFromPEM(pemCerts []byte) ([]*x509.Certificate, error) { func Format(v string) string { // to lower case v = strings.ToLower(v) + // / to dashes + v = strings.ReplaceAll(v, "/", "-") // dots to dashes v = strings.ReplaceAll(v, ".", "-") // limit to 253 chars