Skip to content

Commit

Permalink
Runtime custom docker img (micro#1168)
Browse files Browse the repository at this point in the history
* Add DeploymentOptions to K8s Client

* WithBaseImage for  Runtime

* Revert Change

* Fix sequencing
  • Loading branch information
ben-toogood committed Feb 6, 2020
1 parent a44dc90 commit d8110b7
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 4 deletions.
3 changes: 2 additions & 1 deletion runtime/kubernetes/kubernetes.go
Original file line number Diff line number Diff line change
Expand Up @@ -343,7 +343,8 @@ func (k *kubernetes) Delete(s *runtime.Service) error {

// create new kubernetes micro service
service := newService(s, runtime.CreateOptions{
Type: k.options.Type,
Type: k.options.Type,
BaseImage: k.options.Source,
})

return service.Stop(k.client)
Expand Down
11 changes: 10 additions & 1 deletion runtime/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ type Options struct {
Source string
}

// WithSource sets the host addresses to be used by the broker
// WithSource sets the base image / repository
func WithSource(src string) Option {
return func(o *Options) {
o.Source = src
Expand Down Expand Up @@ -51,6 +51,8 @@ type CreateOptions struct {
Output io.Writer
// Type of service to create
Type string
// Base image for docker
BaseImage string
}

// ReadOptions queries runtime services
Expand Down Expand Up @@ -92,6 +94,13 @@ func WithOutput(out io.Writer) CreateOption {
}
}

// WithBaseImage sets the docker img
func WithBaseImage(img string) CreateOption {
return func(o *CreateOptions) {
o.BaseImage = img
}
}

// ReadService returns services with the given name
func ReadService(service string) ReadOption {
return func(o *ReadOptions) {
Expand Down
6 changes: 4 additions & 2 deletions util/kubernetes/client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -226,9 +226,11 @@ func NewService(name, version, typ string) *Service {
}

// NewService returns default micro kubernetes deployment definition
func NewDeployment(name, version, typ string) *Deployment {
func NewDeployment(name, version, typ string, opts ...DeploymentOption) *Deployment {
log.Tracef("kubernetes default deployment: name: %s, version: %s", name, version)

options := NewDeploymentOptions(opts)

Labels := map[string]string{
"name": name,
"version": version,
Expand Down Expand Up @@ -265,7 +267,7 @@ func NewDeployment(name, version, typ string) *Deployment {
PodSpec: &PodSpec{
Containers: []Container{{
Name: name,
Image: DefaultImage,
Image: options.BaseImage,
Env: []EnvVar{env},
Command: []string{"go", "run", "main.go"},
Ports: []ContainerPort{{
Expand Down
26 changes: 26 additions & 0 deletions util/kubernetes/client/options.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
package client

type DeploymentOptions struct {
BaseImage string
}

type LogOptions struct {
Params map[string]string
}
Expand All @@ -10,6 +14,7 @@ type WatchOptions struct {

type LogOption func(*LogOptions)
type WatchOption func(*WatchOptions)
type DeploymentOption func(*DeploymentOptions)

// LogParams provides additional params for logs
func LogParams(p map[string]string) LogOption {
Expand All @@ -24,3 +29,24 @@ func WatchParams(p map[string]string) WatchOption {
w.Params = p
}
}

// WithBaseImage sets the base image for the deployment
func WithBaseImage(img string) DeploymentOption {
return func(d *DeploymentOptions) {
d.BaseImage = img
}
}

// NewDeploymentOptions returns an initialized DeploymentOptions
func NewDeploymentOptions(opts []DeploymentOption) DeploymentOptions {
var options DeploymentOptions
for _, o := range opts {
o(&options)
}

if options.BaseImage == "" {
options.BaseImage = DefaultImage
}

return options
}

0 comments on commit d8110b7

Please sign in to comment.