2019-11-15 16:41:40 +03:00
|
|
|
// Package client provides an implementation of a restricted subset of kubernetes API client
|
2019-11-02 16:25:10 +03:00
|
|
|
package client
|
|
|
|
|
2019-11-15 16:41:40 +03:00
|
|
|
import (
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
"github.com/micro/go-micro/util/log"
|
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
// DefaultImage is default micro image
|
2019-11-19 23:49:01 +03:00
|
|
|
DefaultImage = "micro/go-micro"
|
2019-11-15 16:41:40 +03:00
|
|
|
)
|
|
|
|
|
2019-11-02 16:25:10 +03:00
|
|
|
// Kubernetes client
|
|
|
|
type Kubernetes interface {
|
2019-11-15 16:41:40 +03:00
|
|
|
// Create creates new API resource
|
|
|
|
Create(*Resource) error
|
|
|
|
// Get queries API resrouces
|
|
|
|
Get(*Resource, map[string]string) error
|
|
|
|
// Update patches existing API object
|
|
|
|
Update(*Resource) error
|
|
|
|
// Delete deletes API resource
|
|
|
|
Delete(*Resource) error
|
|
|
|
// List lists API resources
|
|
|
|
List(*Resource) error
|
2019-11-02 16:25:10 +03:00
|
|
|
}
|
|
|
|
|
2019-11-25 19:31:14 +03:00
|
|
|
// NewService returns default micro kubernetes service definition
|
2019-11-29 14:35:00 +03:00
|
|
|
func NewService(name, version, typ string) *Service {
|
2019-11-25 19:31:14 +03:00
|
|
|
log.Tracef("kubernetes default service: name: %s, version: %s", name, version)
|
2019-11-21 20:31:13 +03:00
|
|
|
|
2019-11-15 16:41:40 +03:00
|
|
|
Labels := map[string]string{
|
|
|
|
"name": name,
|
|
|
|
"version": version,
|
2019-11-29 14:35:00 +03:00
|
|
|
"micro": typ,
|
2019-11-15 16:41:40 +03:00
|
|
|
}
|
2019-11-08 17:12:03 +03:00
|
|
|
|
2019-11-15 16:41:40 +03:00
|
|
|
svcName := name
|
|
|
|
if len(version) > 0 {
|
|
|
|
// API service object name joins name and version over "-"
|
|
|
|
svcName = strings.Join([]string{name, version}, "-")
|
|
|
|
}
|
2019-11-07 10:44:57 +03:00
|
|
|
|
2019-11-15 16:41:40 +03:00
|
|
|
Metadata := &Metadata{
|
|
|
|
Name: svcName,
|
|
|
|
Namespace: "default",
|
|
|
|
Version: version,
|
|
|
|
Labels: Labels,
|
|
|
|
}
|
|
|
|
|
|
|
|
Spec := &ServiceSpec{
|
|
|
|
Type: "ClusterIP",
|
|
|
|
Selector: Labels,
|
|
|
|
Ports: []ServicePort{{
|
2019-12-10 17:05:05 +03:00
|
|
|
"service-port", 9090, "",
|
2019-11-15 16:41:40 +03:00
|
|
|
}},
|
|
|
|
}
|
2019-11-07 10:44:57 +03:00
|
|
|
|
2019-11-15 16:41:40 +03:00
|
|
|
return &Service{
|
|
|
|
Metadata: Metadata,
|
|
|
|
Spec: Spec,
|
|
|
|
}
|
2019-11-07 10:44:57 +03:00
|
|
|
}
|
|
|
|
|
2019-11-25 19:31:14 +03:00
|
|
|
// NewService returns default micro kubernetes deployment definition
|
2019-11-29 14:35:00 +03:00
|
|
|
func NewDeployment(name, version, typ string) *Deployment {
|
2019-11-25 19:31:14 +03:00
|
|
|
log.Tracef("kubernetes default deployment: name: %s, version: %s", name, version)
|
2019-11-21 20:31:13 +03:00
|
|
|
|
2019-11-15 16:41:40 +03:00
|
|
|
Labels := map[string]string{
|
|
|
|
"name": name,
|
|
|
|
"version": version,
|
2019-11-29 14:35:00 +03:00
|
|
|
"micro": typ,
|
2019-11-15 16:41:40 +03:00
|
|
|
}
|
|
|
|
|
2019-11-22 20:10:00 +03:00
|
|
|
depName := name
|
|
|
|
if len(version) > 0 {
|
|
|
|
// API deployment object name joins name and version over "-"
|
|
|
|
depName = strings.Join([]string{name, version}, "-")
|
|
|
|
}
|
2019-11-15 16:41:40 +03:00
|
|
|
|
|
|
|
Metadata := &Metadata{
|
2019-11-25 19:31:14 +03:00
|
|
|
Name: depName,
|
|
|
|
Namespace: "default",
|
|
|
|
Version: version,
|
|
|
|
Labels: Labels,
|
|
|
|
Annotations: map[string]string{},
|
2019-11-15 16:41:40 +03:00
|
|
|
}
|
|
|
|
|
2019-11-21 20:31:13 +03:00
|
|
|
// enable go modules by default
|
|
|
|
env := EnvVar{
|
|
|
|
Name: "GO111MODULE",
|
|
|
|
Value: "on",
|
|
|
|
}
|
|
|
|
|
2019-11-15 16:41:40 +03:00
|
|
|
Spec := &DeploymentSpec{
|
|
|
|
Replicas: 1,
|
|
|
|
Selector: &LabelSelector{
|
|
|
|
MatchLabels: Labels,
|
|
|
|
},
|
|
|
|
Template: &Template{
|
|
|
|
Metadata: Metadata,
|
|
|
|
PodSpec: &PodSpec{
|
|
|
|
Containers: []Container{{
|
|
|
|
Name: name,
|
|
|
|
Image: DefaultImage,
|
2019-11-21 20:31:13 +03:00
|
|
|
Env: []EnvVar{env},
|
2019-11-25 19:31:14 +03:00
|
|
|
Command: []string{"go", "run", "main.go"},
|
2019-11-15 16:41:40 +03:00
|
|
|
Ports: []ContainerPort{{
|
2019-12-10 17:05:05 +03:00
|
|
|
Name: "service-port",
|
2019-11-15 16:41:40 +03:00
|
|
|
ContainerPort: 8080,
|
|
|
|
}},
|
|
|
|
}},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
return &Deployment{
|
|
|
|
Metadata: Metadata,
|
|
|
|
Spec: Spec,
|
|
|
|
}
|
2019-11-07 10:44:57 +03:00
|
|
|
}
|