Revert "update to use containerd seccomp package"

This reverts commit 4f8e065faf055d3f0463a92622297ca3afac07f4.
This commit is contained in:
Jess Frazelle 2018-03-22 09:15:36 -04:00
parent 09243b740c
commit 60f032f6f5
8199 changed files with 1598219 additions and 30742 deletions

View file

@ -0,0 +1,4 @@
# Kubernetes client libraries
This package (and sub-packages) holds the client libraries for the kubernetes integration in
the docker platform. Most of the code is currently generated.

View file

@ -0,0 +1,50 @@
package kubernetes
import (
apiv1beta1 "github.com/docker/cli/kubernetes/compose/v1beta1"
"github.com/pkg/errors"
apimachinerymetav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime/schema"
"k8s.io/client-go/kubernetes"
)
// StackVersion represents the detected Compose Component on Kubernetes side.
type StackVersion string
const (
// StackAPIV1Beta1 is returned if it's the most recent version available.
StackAPIV1Beta1 = StackVersion("v1beta1")
)
// GetStackAPIVersion returns the most recent stack API installed.
func GetStackAPIVersion(clientSet *kubernetes.Clientset) (StackVersion, error) {
groups, err := clientSet.Discovery().ServerGroups()
if err != nil {
return "", err
}
return getAPIVersion(groups)
}
func getAPIVersion(groups *metav1.APIGroupList) (StackVersion, error) {
switch {
case findVersion(apiv1beta1.SchemeGroupVersion, groups.Groups):
return StackAPIV1Beta1, nil
default:
return "", errors.Errorf("failed to find a Stack API version")
}
}
func findVersion(stackAPI schema.GroupVersion, groups []apimachinerymetav1.APIGroup) bool {
for _, group := range groups {
if group.Name == stackAPI.Group {
for _, version := range group.Versions {
if version.Version == stackAPI.Version {
return true
}
}
}
}
return false
}

View file

@ -0,0 +1,49 @@
package kubernetes
import (
"testing"
"github.com/gotestyourself/gotestyourself/assert"
is "github.com/gotestyourself/gotestyourself/assert/cmp"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)
func TestGetStackAPIVersion(t *testing.T) {
var tests = []struct {
description string
groups *metav1.APIGroupList
err bool
expectedStack StackVersion
}{
{"no stack api", makeGroups(), true, ""},
{"v1beta1", makeGroups(groupVersion{"compose.docker.com", []string{"v1beta1"}}), false, StackAPIV1Beta1},
}
for _, test := range tests {
version, err := getAPIVersion(test.groups)
if test.err {
assert.ErrorContains(t, err, "")
} else {
assert.NilError(t, err)
}
assert.Check(t, is.Equal(test.expectedStack, version))
}
}
type groupVersion struct {
name string
versions []string
}
func makeGroups(versions ...groupVersion) *metav1.APIGroupList {
groups := make([]metav1.APIGroup, len(versions))
for i := range versions {
groups[i].Name = versions[i].name
for _, v := range versions[i].versions {
groups[i].Versions = append(groups[i].Versions, metav1.GroupVersionForDiscovery{Version: v})
}
}
return &metav1.APIGroupList{
Groups: groups,
}
}

View file

@ -0,0 +1,88 @@
package clientset
import (
composev1beta1 "github.com/docker/cli/kubernetes/client/clientset_generated/clientset/typed/compose/v1beta1"
glog "github.com/golang/glog"
discovery "k8s.io/client-go/discovery"
rest "k8s.io/client-go/rest"
flowcontrol "k8s.io/client-go/util/flowcontrol"
)
type Interface interface {
Discovery() discovery.DiscoveryInterface
ComposeV1beta1() composev1beta1.ComposeV1beta1Interface
// Deprecated: please explicitly pick a version if possible.
Compose() composev1beta1.ComposeV1beta1Interface
}
// Clientset contains the clients for groups. Each group has exactly one
// version included in a Clientset.
type Clientset struct {
*discovery.DiscoveryClient
*composev1beta1.ComposeV1beta1Client
}
// ComposeV1beta1 retrieves the ComposeV1beta1Client
func (c *Clientset) ComposeV1beta1() composev1beta1.ComposeV1beta1Interface {
if c == nil {
return nil
}
return c.ComposeV1beta1Client
}
// Deprecated: Compose retrieves the default version of ComposeClient.
// Please explicitly pick a version.
func (c *Clientset) Compose() composev1beta1.ComposeV1beta1Interface {
if c == nil {
return nil
}
return c.ComposeV1beta1Client
}
// Discovery retrieves the DiscoveryClient
func (c *Clientset) Discovery() discovery.DiscoveryInterface {
if c == nil {
return nil
}
return c.DiscoveryClient
}
// NewForConfig creates a new Clientset for the given config.
func NewForConfig(c *rest.Config) (*Clientset, error) {
configShallowCopy := *c
if configShallowCopy.RateLimiter == nil && configShallowCopy.QPS > 0 {
configShallowCopy.RateLimiter = flowcontrol.NewTokenBucketRateLimiter(configShallowCopy.QPS, configShallowCopy.Burst)
}
var cs Clientset
var err error
cs.ComposeV1beta1Client, err = composev1beta1.NewForConfig(&configShallowCopy)
if err != nil {
return nil, err
}
cs.DiscoveryClient, err = discovery.NewDiscoveryClientForConfig(&configShallowCopy)
if err != nil {
glog.Errorf("failed to create the DiscoveryClient: %v", err)
return nil, err
}
return &cs, nil
}
// NewForConfigOrDie creates a new Clientset for the given config and
// panics if there is an error in the config.
func NewForConfigOrDie(c *rest.Config) *Clientset {
var cs Clientset
cs.ComposeV1beta1Client = composev1beta1.NewForConfigOrDie(c)
cs.DiscoveryClient = discovery.NewDiscoveryClientForConfigOrDie(c)
return &cs
}
// New creates a new Clientset for the given RESTClient.
func New(c rest.Interface) *Clientset {
var cs Clientset
cs.ComposeV1beta1Client = composev1beta1.New(c)
cs.DiscoveryClient = discovery.NewDiscoveryClient(c)
return &cs
}

View file

@ -0,0 +1,4 @@
// This package is generated by client-gen with custom arguments.
// This package has the automatically generated clientset.
package clientset

View file

@ -0,0 +1,4 @@
// This package is generated by client-gen with custom arguments.
// This package contains the scheme of the automatically generated clientset.
package scheme

View file

@ -0,0 +1,37 @@
package scheme
import (
composev1beta1 "github.com/docker/cli/kubernetes/compose/v1beta1"
v1 "k8s.io/apimachinery/pkg/apis/meta/v1"
runtime "k8s.io/apimachinery/pkg/runtime"
schema "k8s.io/apimachinery/pkg/runtime/schema"
serializer "k8s.io/apimachinery/pkg/runtime/serializer"
)
var Scheme = runtime.NewScheme()
var Codecs = serializer.NewCodecFactory(Scheme)
var ParameterCodec = runtime.NewParameterCodec(Scheme)
func init() {
v1.AddToGroupVersion(Scheme, schema.GroupVersion{Version: "v1"})
AddToScheme(Scheme)
}
// AddToScheme adds all types of this clientset into the given scheme. This allows composition
// of clientsets, like in:
//
// import (
// "k8s.io/client-go/kubernetes"
// clientsetscheme "k8s.io/client-go/kuberentes/scheme"
// aggregatorclientsetscheme "k8s.io/kube-aggregator/pkg/client/clientset_generated/clientset/scheme"
// )
//
// kclientset, _ := kubernetes.NewForConfig(c)
// aggregatorclientsetscheme.AddToScheme(clientsetscheme.Scheme)
//
// After this, RawExtensions in Kubernetes types will serialize kube-aggregator types
// correctly.
func AddToScheme(scheme *runtime.Scheme) {
composev1beta1.AddToScheme(scheme)
}

View file

@ -0,0 +1,72 @@
package v1beta1
import (
"github.com/docker/cli/kubernetes/client/clientset_generated/clientset/scheme"
v1beta1 "github.com/docker/cli/kubernetes/compose/v1beta1"
serializer "k8s.io/apimachinery/pkg/runtime/serializer"
rest "k8s.io/client-go/rest"
)
type ComposeV1beta1Interface interface {
RESTClient() rest.Interface
StacksGetter
}
// ComposeV1beta1Client is used to interact with features provided by the compose.docker.com group.
type ComposeV1beta1Client struct {
restClient rest.Interface
}
func (c *ComposeV1beta1Client) Stacks(namespace string) StackInterface {
return newStacks(c, namespace)
}
// NewForConfig creates a new ComposeV1beta1Client for the given config.
func NewForConfig(c *rest.Config) (*ComposeV1beta1Client, error) {
config := *c
if err := setConfigDefaults(&config); err != nil {
return nil, err
}
client, err := rest.RESTClientFor(&config)
if err != nil {
return nil, err
}
return &ComposeV1beta1Client{client}, nil
}
// NewForConfigOrDie creates a new ComposeV1beta1Client for the given config and
// panics if there is an error in the config.
func NewForConfigOrDie(c *rest.Config) *ComposeV1beta1Client {
client, err := NewForConfig(c)
if err != nil {
panic(err)
}
return client
}
// New creates a new ComposeV1beta1Client for the given RESTClient.
func New(c rest.Interface) *ComposeV1beta1Client {
return &ComposeV1beta1Client{c}
}
func setConfigDefaults(config *rest.Config) error {
gv := v1beta1.SchemeGroupVersion
config.GroupVersion = &gv
config.APIPath = "/apis"
config.NegotiatedSerializer = serializer.DirectCodecFactory{CodecFactory: scheme.Codecs}
if config.UserAgent == "" {
config.UserAgent = rest.DefaultKubernetesUserAgent()
}
return nil
}
// RESTClient returns a RESTClient that is used to communicate
// with API server by this client implementation.
func (c *ComposeV1beta1Client) RESTClient() rest.Interface {
if c == nil {
return nil
}
return c.restClient
}

View file

@ -0,0 +1,4 @@
// This package is generated by client-gen with custom arguments.
// This package has the automatically generated typed clients.
package v1beta1

View file

@ -0,0 +1,3 @@
package v1beta1
type StackExpansion interface{}

View file

@ -0,0 +1,158 @@
package v1beta1
import (
scheme "github.com/docker/cli/kubernetes/client/clientset_generated/clientset/scheme"
v1beta1 "github.com/docker/cli/kubernetes/compose/v1beta1"
v1 "k8s.io/apimachinery/pkg/apis/meta/v1"
types "k8s.io/apimachinery/pkg/types"
watch "k8s.io/apimachinery/pkg/watch"
rest "k8s.io/client-go/rest"
)
// StacksGetter has a method to return a StackInterface.
// A group's client should implement this interface.
type StacksGetter interface {
Stacks(namespace string) StackInterface
}
// StackInterface has methods to work with Stack resources.
type StackInterface interface {
Create(*v1beta1.Stack) (*v1beta1.Stack, error)
Update(*v1beta1.Stack) (*v1beta1.Stack, error)
UpdateStatus(*v1beta1.Stack) (*v1beta1.Stack, error)
Delete(name string, options *v1.DeleteOptions) error
DeleteCollection(options *v1.DeleteOptions, listOptions v1.ListOptions) error
Get(name string, options v1.GetOptions) (*v1beta1.Stack, error)
List(opts v1.ListOptions) (*v1beta1.StackList, error)
Watch(opts v1.ListOptions) (watch.Interface, error)
Patch(name string, pt types.PatchType, data []byte, subresources ...string) (result *v1beta1.Stack, err error)
StackExpansion
}
var _ StackInterface = &stacks{}
// stacks implements StackInterface
type stacks struct {
client rest.Interface
ns string
}
// newStacks returns a Stacks
func newStacks(c *ComposeV1beta1Client, namespace string) *stacks {
return &stacks{
client: c.RESTClient(),
ns: namespace,
}
}
// Create takes the representation of a stack and creates it. Returns the server's representation of the stack, and an error, if there is any.
func (c *stacks) Create(stack *v1beta1.Stack) (result *v1beta1.Stack, err error) {
result = &v1beta1.Stack{}
err = c.client.Post().
Namespace(c.ns).
Resource("stacks").
Body(stack).
Do().
Into(result)
return
}
// Update takes the representation of a stack and updates it. Returns the server's representation of the stack, and an error, if there is any.
func (c *stacks) Update(stack *v1beta1.Stack) (result *v1beta1.Stack, err error) {
result = &v1beta1.Stack{}
err = c.client.Put().
Namespace(c.ns).
Resource("stacks").
Name(stack.Name).
Body(stack).
Do().
Into(result)
return
}
// UpdateStatus was generated because the type contains a Status member.
// Add a +genclientstatus=false comment above the type to avoid generating UpdateStatus().
func (c *stacks) UpdateStatus(stack *v1beta1.Stack) (result *v1beta1.Stack, err error) {
result = &v1beta1.Stack{}
err = c.client.Put().
Namespace(c.ns).
Resource("stacks").
Name(stack.Name).
SubResource("status").
Body(stack).
Do().
Into(result)
return
}
// Delete takes name of the stack and deletes it. Returns an error if one occurs.
func (c *stacks) Delete(name string, options *v1.DeleteOptions) error {
return c.client.Delete().
Namespace(c.ns).
Resource("stacks").
Name(name).
Body(options).
Do().
Error()
}
// DeleteCollection deletes a collection of objects.
func (c *stacks) DeleteCollection(options *v1.DeleteOptions, listOptions v1.ListOptions) error {
return c.client.Delete().
Namespace(c.ns).
Resource("stacks").
VersionedParams(&listOptions, scheme.ParameterCodec).
Body(options).
Do().
Error()
}
// Get takes name of the stack, and returns the corresponding stack object, and an error if there is any.
func (c *stacks) Get(name string, options v1.GetOptions) (result *v1beta1.Stack, err error) {
result = &v1beta1.Stack{}
err = c.client.Get().
Namespace(c.ns).
Resource("stacks").
Name(name).
VersionedParams(&options, scheme.ParameterCodec).
Do().
Into(result)
return
}
// List takes label and field selectors, and returns the list of Stacks that match those selectors.
func (c *stacks) List(opts v1.ListOptions) (result *v1beta1.StackList, err error) {
result = &v1beta1.StackList{}
err = c.client.Get().
Namespace(c.ns).
Resource("stacks").
VersionedParams(&opts, scheme.ParameterCodec).
Do().
Into(result)
return
}
// Watch returns a watch.Interface that watches the requested stacks.
func (c *stacks) Watch(opts v1.ListOptions) (watch.Interface, error) {
opts.Watch = true
return c.client.Get().
Namespace(c.ns).
Resource("stacks").
VersionedParams(&opts, scheme.ParameterCodec).
Watch()
}
// Patch applies the patch and returns the patched stack.
func (c *stacks) Patch(name string, pt types.PatchType, data []byte, subresources ...string) (result *v1beta1.Stack, err error) {
result = &v1beta1.Stack{}
err = c.client.Patch(pt).
Namespace(c.ns).
Resource("stacks").
SubResource(subresources...).
Name(name).
Body(data).
Do().
Into(result)
return
}

View file

@ -0,0 +1,5 @@
// +k8s:deepcopy-gen=package,register
// +groupName=compose.docker.com
// Package compose is the internal version of the API.
package compose

View file

@ -0,0 +1,43 @@
package compose
import (
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/runtime/schema"
)
// GroupName is the group name used to register these objects
const GroupName = "compose.docker.com"
// SchemeGroupVersion is group version used to register these objects
var SchemeGroupVersion = schema.GroupVersion{Group: GroupName, Version: runtime.APIVersionInternal}
// Kind takes an unqualified kind and returns back a Group qualified GroupKind
func Kind(kind string) schema.GroupKind {
return SchemeGroupVersion.WithKind(kind).GroupKind()
}
// Resource takes an unqualified resource and returns back a Group qualified GroupResource
func Resource(resource string) schema.GroupResource {
return SchemeGroupVersion.WithResource(resource).GroupResource()
}
var (
// SchemeBuilder collects functions that add things to a scheme. It's to allow
// code to compile without explicitly referencing generated types. You should
// declare one in each package that will have generated deep copy or conversion
// functions.
SchemeBuilder = runtime.NewSchemeBuilder(addKnownTypes)
// AddToScheme applies all the stored functions to the scheme. A non-nil error
// indicates that one function failed and the attempt was abandoned.
AddToScheme = SchemeBuilder.AddToScheme
)
// adds the list of known types to api.Scheme.
func addKnownTypes(scheme *runtime.Scheme) error {
scheme.AddKnownTypes(SchemeGroupVersion,
&Stack{},
&StackList{},
)
return nil
}

View file

@ -0,0 +1,117 @@
package compose
import (
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)
// ImpersonationConfig holds information use to impersonate calls from the compose controller
type ImpersonationConfig struct {
// UserName is the username to impersonate on each request.
UserName string
// Groups are the groups to impersonate on each request.
Groups []string
// Extra is a free-form field which can be used to link some authentication information
// to authorization information. This field allows you to impersonate it.
Extra map[string][]string
}
// Stack defines a stack object to be register in the kubernetes API
// +genclient=true
// +k8s:deepcopy-gen:interfaces=k8s.io/apimachinery/pkg/runtime.Object
type Stack struct {
metav1.TypeMeta
metav1.ObjectMeta
Spec StackSpec
Status StackStatus
}
// StackStatus defines the observed state of Stack
type StackStatus struct {
Phase StackPhase
Message string
}
// StackSpec defines the desired state of Stack
type StackSpec struct {
ComposeFile string
Owner ImpersonationConfig
}
// StackPhase defines the status phase in which the stack is.
type StackPhase string
// These are valid conditions of a stack.
const (
// Available means the stack is available.
StackAvailable StackPhase = "Available"
// Progressing means the deployment is progressing.
StackProgressing StackPhase = "Progressing"
// StackFailure is added in a stack when one of its members fails to be created
// or deleted.
StackFailure StackPhase = "Failure"
)
// StackList defines a list of stacks
// +k8s:deepcopy-gen:interfaces=k8s.io/apimachinery/pkg/runtime.Object
type StackList struct {
metav1.TypeMeta
metav1.ListMeta
Items []Stack
}
// Owner defines the owner of a stack. It is used to impersonate the controller calls
// to kubernetes api.
type Owner struct {
metav1.TypeMeta
metav1.ObjectMeta
Owner ImpersonationConfig
}
// OwnerList defines a list of owner.
type OwnerList struct {
metav1.TypeMeta
metav1.ListMeta
Items []Owner
}
// FIXME(vdemeester) are those necessary ??
// NewStatus is newStatus
func (Stack) NewStatus() interface{} {
return StackStatus{}
}
// GetStatus returns the status
func (pc *Stack) GetStatus() interface{} {
return pc.Status
}
// SetStatus sets the status
func (pc *Stack) SetStatus(s interface{}) {
pc.Status = s.(StackStatus)
}
// GetSpec returns the spec
func (pc *Stack) GetSpec() interface{} {
return pc.Spec
}
// SetSpec sets the spec
func (pc *Stack) SetSpec(s interface{}) {
pc.Spec = s.(StackSpec)
}
// GetObjectMeta returns the ObjectMeta
func (pc *Stack) GetObjectMeta() *metav1.ObjectMeta {
return &pc.ObjectMeta
}
// SetGeneration sets the Generation
func (pc *Stack) SetGeneration(generation int64) {
pc.ObjectMeta.Generation = generation
}
// GetGeneration returns the Generation
func (pc Stack) GetGeneration() int64 {
return pc.ObjectMeta.Generation
}

View file

@ -0,0 +1,11 @@
// Package v1beta1 holds the v1beta1 versions of our stack structures.
// API versions allow the api contract for a resource to be changed while keeping
// backward compatibility by support multiple concurrent versions
// of the same resource
//
// +k8s:openapi-gen=true
// +k8s:deepcopy-gen=package,register
// +k8s:conversion-gen=github.com/docker/cli/kubernetes/compose
// +k8s:defaulter-gen=TypeMeta
// +groupName=compose.docker.com
package v1beta1 // import "github.com/docker/cli/kubernetes/compose/v1beta1"

View file

@ -0,0 +1,25 @@
package v1beta1
import (
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"github.com/docker/cli/kubernetes/compose"
)
// Owner defines the owner of a stack. It is used to impersonate the controller calls
// to kubernetes api.
// +genclient=true
// +k8s:deepcopy-gen:interfaces=k8s.io/apimachinery/pkg/runtime.Object
// +subresource-request
type Owner struct {
metav1.TypeMeta `json:",inline"`
metav1.ObjectMeta `json:"metadata,omitempty"`
Owner compose.ImpersonationConfig `json:"owner,omitempty"`
}
// OwnerList defines a list of owner.
type OwnerList struct {
metav1.TypeMeta
metav1.ListMeta
Items []Owner
}

View file

@ -0,0 +1,47 @@
package v1beta1
import (
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/runtime/schema"
)
// GroupName is the group name used to register these objects
const GroupName = "compose.docker.com"
// SchemeGroupVersion is group version used to register these objects
var SchemeGroupVersion = schema.GroupVersion{Group: GroupName, Version: "v1beta1"}
var (
// SchemeBuilder collects functions that add things to a scheme. It's to allow
// code to compile without explicitly referencing generated types. You should
// declare one in each package that will have generated deep copy or conversion
// functions.
SchemeBuilder runtime.SchemeBuilder
localSchemeBuilder = &SchemeBuilder
// AddToScheme applies all the stored functions to the scheme. A non-nil error
// indicates that one function failed and the attempt was abandoned.
AddToScheme = localSchemeBuilder.AddToScheme
)
func init() {
localSchemeBuilder.Register(addKnownTypes)
}
// Adds the list of known types to api.Scheme.
func addKnownTypes(scheme *runtime.Scheme) error {
scheme.AddKnownTypes(SchemeGroupVersion,
&Stack{},
&StackList{},
&Owner{},
&OwnerList{},
)
metav1.AddToGroupVersion(scheme, SchemeGroupVersion)
return nil
}
// Resource takes an unqualified resource and returns a Group qualified GroupResource
func Resource(resource string) schema.GroupResource {
return SchemeGroupVersion.WithResource(resource).GroupResource()
}

View file

@ -0,0 +1,68 @@
package v1beta1
import (
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
)
// StackList defines a list of stacks
// +k8s:deepcopy-gen:interfaces=k8s.io/apimachinery/pkg/runtime.Object
type StackList struct {
metav1.TypeMeta `json:",inline"`
metav1.ListMeta `json:"metadata,omitempty" protobuf:"bytes,1,opt,name=metadata"`
Items []Stack `json:"items" protobuf:"bytes,2,rep,name=items"`
}
// +genclient=true
// +k8s:deepcopy-gen:interfaces=k8s.io/apimachinery/pkg/runtime.Object
// Stack defines a stack object to be register in the kubernetes API
// +k8s:openapi-gen=true
// +resource:path=stacks,strategy=StackStrategy
// +subresource:request=Owner,path=owner,rest=OwnerStackREST
type Stack struct {
metav1.TypeMeta `json:",inline"`
metav1.ObjectMeta `json:"metadata,omitempty"`
Spec StackSpec `json:"spec,omitempty"`
Status StackStatus `json:"status,omitempty"`
}
// StackSpec defines the desired state of Stack
type StackSpec struct {
ComposeFile string `json:"composeFile,omitempty"`
}
// StackPhase defines the status phase in which the stack is.
type StackPhase string
// These are valid conditions of a stack.
const (
// Available means the stack is available.
StackAvailable StackPhase = "Available"
// Progressing means the deployment is progressing.
StackProgressing StackPhase = "Progressing"
// StackFailure is added in a stack when one of its members fails to be created
// or deleted.
StackFailure StackPhase = "Failure"
)
// StackStatus defines the observed state of Stack
type StackStatus struct {
// Current condition of the stack.
// +optional
Phase StackPhase `json:"phase,omitempty" protobuf:"bytes,1,opt,name=phase,casttype=StackPhase"`
// A human readable message indicating details about the stack.
// +optional
Message string `json:"message,omitempty" protobuf:"bytes,5,opt,name=message"`
}
// Clone implements the Cloner interface for kubernetes
func (s *Stack) Clone() (*Stack, error) {
scheme := runtime.NewScheme()
if err := AddToScheme(scheme); err != nil {
return nil, err
}
return s.DeepCopy(), nil
}

View file

@ -0,0 +1,9 @@
version: "3.2"
services:
nginx:
image: nginx
deploy:
replicas: 2
redis:
image: redis:alpine

View file

@ -0,0 +1,10 @@
version: "3.2"
services:
nginx:
image: nginx
deploy:
replicas: 2
redis:
image: redis:alpine
deploy:
replicas: 5

View file

@ -0,0 +1,10 @@
version: "3.2"
services:
redis:
image: redis:alpine
deploy:
resources:
limits:
memory: 64M
reservations:
memory: 64M

View file

@ -0,0 +1,11 @@
version: "3.2"
services:
redis:
image: redis:alpine
deploy:
resources:
limits:
memory: 64M
reservations:
memory: 64M
replicas: 5

View file

@ -0,0 +1,6 @@
version: "3.2"
services:
redis:
image: redis:alpine
deploy:
replicas: 2

View file

@ -0,0 +1,6 @@
version: "3.2"
services:
redis:
image: redis:alpine
deploy:
replicas: 5

View file

@ -0,0 +1,4 @@
version: "3.2"
services:
redis:
image: redis:alpine

View file

@ -0,0 +1,6 @@
version: "3.2"
services:
redis:
image: redis:alpine
deploy:
replicas: 5

View file

@ -0,0 +1,169 @@
// +build !ignore_autogenerated
// This file was autogenerated by conversion-gen. Do not edit it manually!
package v1beta1
import (
compose "github.com/docker/cli/kubernetes/compose"
conversion "k8s.io/apimachinery/pkg/conversion"
runtime "k8s.io/apimachinery/pkg/runtime"
)
func init() {
localSchemeBuilder.Register(RegisterConversions)
}
// RegisterConversions adds conversion functions to the given scheme.
// Public to allow building arbitrary schemes.
func RegisterConversions(scheme *runtime.Scheme) error {
return scheme.AddGeneratedConversionFuncs(
Convert_v1beta1_Owner_To_compose_Owner,
Convert_compose_Owner_To_v1beta1_Owner,
Convert_v1beta1_Stack_To_compose_Stack,
Convert_compose_Stack_To_v1beta1_Stack,
Convert_v1beta1_StackList_To_compose_StackList,
Convert_compose_StackList_To_v1beta1_StackList,
Convert_v1beta1_StackSpec_To_compose_StackSpec,
Convert_compose_StackSpec_To_v1beta1_StackSpec,
Convert_v1beta1_StackStatus_To_compose_StackStatus,
Convert_compose_StackStatus_To_v1beta1_StackStatus,
)
}
func autoConvert_v1beta1_Owner_To_compose_Owner(in *Owner, out *compose.Owner, s conversion.Scope) error {
out.ObjectMeta = in.ObjectMeta
out.Owner = in.Owner
return nil
}
// Convert_v1beta1_Owner_To_compose_Owner is an autogenerated conversion function.
func Convert_v1beta1_Owner_To_compose_Owner(in *Owner, out *compose.Owner, s conversion.Scope) error {
return autoConvert_v1beta1_Owner_To_compose_Owner(in, out, s)
}
func autoConvert_compose_Owner_To_v1beta1_Owner(in *compose.Owner, out *Owner, s conversion.Scope) error {
out.ObjectMeta = in.ObjectMeta
out.Owner = in.Owner
return nil
}
// Convert_compose_Owner_To_v1beta1_Owner is an autogenerated conversion function.
func Convert_compose_Owner_To_v1beta1_Owner(in *compose.Owner, out *Owner, s conversion.Scope) error {
return autoConvert_compose_Owner_To_v1beta1_Owner(in, out, s)
}
func autoConvert_v1beta1_Stack_To_compose_Stack(in *Stack, out *compose.Stack, s conversion.Scope) error {
out.ObjectMeta = in.ObjectMeta
if err := Convert_v1beta1_StackSpec_To_compose_StackSpec(&in.Spec, &out.Spec, s); err != nil {
return err
}
if err := Convert_v1beta1_StackStatus_To_compose_StackStatus(&in.Status, &out.Status, s); err != nil {
return err
}
return nil
}
// Convert_v1beta1_Stack_To_compose_Stack is an autogenerated conversion function.
func Convert_v1beta1_Stack_To_compose_Stack(in *Stack, out *compose.Stack, s conversion.Scope) error {
return autoConvert_v1beta1_Stack_To_compose_Stack(in, out, s)
}
func autoConvert_compose_Stack_To_v1beta1_Stack(in *compose.Stack, out *Stack, s conversion.Scope) error {
out.ObjectMeta = in.ObjectMeta
if err := Convert_compose_StackSpec_To_v1beta1_StackSpec(&in.Spec, &out.Spec, s); err != nil {
return err
}
if err := Convert_compose_StackStatus_To_v1beta1_StackStatus(&in.Status, &out.Status, s); err != nil {
return err
}
return nil
}
// Convert_compose_Stack_To_v1beta1_Stack is an autogenerated conversion function.
func Convert_compose_Stack_To_v1beta1_Stack(in *compose.Stack, out *Stack, s conversion.Scope) error {
return autoConvert_compose_Stack_To_v1beta1_Stack(in, out, s)
}
func autoConvert_v1beta1_StackList_To_compose_StackList(in *StackList, out *compose.StackList, s conversion.Scope) error {
out.ListMeta = in.ListMeta
if in.Items != nil {
in, out := &in.Items, &out.Items
*out = make([]compose.Stack, len(*in))
for i := range *in {
if err := Convert_v1beta1_Stack_To_compose_Stack(&(*in)[i], &(*out)[i], s); err != nil {
return err
}
}
} else {
out.Items = nil
}
return nil
}
// Convert_v1beta1_StackList_To_compose_StackList is an autogenerated conversion function.
func Convert_v1beta1_StackList_To_compose_StackList(in *StackList, out *compose.StackList, s conversion.Scope) error {
return autoConvert_v1beta1_StackList_To_compose_StackList(in, out, s)
}
func autoConvert_compose_StackList_To_v1beta1_StackList(in *compose.StackList, out *StackList, s conversion.Scope) error {
out.ListMeta = in.ListMeta
if in.Items != nil {
in, out := &in.Items, &out.Items
*out = make([]Stack, len(*in))
for i := range *in {
if err := Convert_compose_Stack_To_v1beta1_Stack(&(*in)[i], &(*out)[i], s); err != nil {
return err
}
}
} else {
out.Items = make([]Stack, 0)
}
return nil
}
// Convert_compose_StackList_To_v1beta1_StackList is an autogenerated conversion function.
func Convert_compose_StackList_To_v1beta1_StackList(in *compose.StackList, out *StackList, s conversion.Scope) error {
return autoConvert_compose_StackList_To_v1beta1_StackList(in, out, s)
}
func autoConvert_v1beta1_StackSpec_To_compose_StackSpec(in *StackSpec, out *compose.StackSpec, s conversion.Scope) error {
out.ComposeFile = in.ComposeFile
return nil
}
func Convert_compose_StackSpec_To_v1beta1_StackSpec(in *compose.StackSpec, out *StackSpec, s conversion.Scope) error {
return autoConvert_compose_StackSpec_To_v1beta1_StackSpec(in, out, s)
}
// Convert_v1beta1_StackSpec_To_compose_StackSpec is an autogenerated conversion function.
func Convert_v1beta1_StackSpec_To_compose_StackSpec(in *StackSpec, out *compose.StackSpec, s conversion.Scope) error {
return autoConvert_v1beta1_StackSpec_To_compose_StackSpec(in, out, s)
}
func autoConvert_compose_StackSpec_To_v1beta1_StackSpec(in *compose.StackSpec, out *StackSpec, s conversion.Scope) error {
out.ComposeFile = in.ComposeFile
return nil
}
func autoConvert_v1beta1_StackStatus_To_compose_StackStatus(in *StackStatus, out *compose.StackStatus, s conversion.Scope) error {
out.Phase = compose.StackPhase(in.Phase)
out.Message = in.Message
return nil
}
// Convert_v1beta1_StackStatus_To_compose_StackStatus is an autogenerated conversion function.
func Convert_v1beta1_StackStatus_To_compose_StackStatus(in *StackStatus, out *compose.StackStatus, s conversion.Scope) error {
return autoConvert_v1beta1_StackStatus_To_compose_StackStatus(in, out, s)
}
func autoConvert_compose_StackStatus_To_v1beta1_StackStatus(in *compose.StackStatus, out *StackStatus, s conversion.Scope) error {
out.Phase = StackPhase(in.Phase)
out.Message = in.Message
return nil
}
// Convert_compose_StackStatus_To_v1beta1_StackStatus is an autogenerated conversion function.
func Convert_compose_StackStatus_To_v1beta1_StackStatus(in *compose.StackStatus, out *StackStatus, s conversion.Scope) error {
return autoConvert_compose_StackStatus_To_v1beta1_StackStatus(in, out, s)
}

View file

@ -0,0 +1,205 @@
// +build !ignore_autogenerated
// This file was autogenerated by deepcopy-gen. Do not edit it manually!
package v1beta1
import (
reflect "reflect"
conversion "k8s.io/apimachinery/pkg/conversion"
runtime "k8s.io/apimachinery/pkg/runtime"
)
// Deprecated: register deep-copy functions.
func init() {
SchemeBuilder.Register(RegisterDeepCopies)
}
// Deprecated: RegisterDeepCopies adds deep-copy functions to the given scheme. Public
// to allow building arbitrary schemes.
func RegisterDeepCopies(scheme *runtime.Scheme) error {
return scheme.AddGeneratedDeepCopyFuncs(
conversion.GeneratedDeepCopyFunc{Fn: func(in interface{}, out interface{}, c *conversion.Cloner) error {
in.(*Owner).DeepCopyInto(out.(*Owner))
return nil
}, InType: reflect.TypeOf(&Owner{})},
conversion.GeneratedDeepCopyFunc{Fn: func(in interface{}, out interface{}, c *conversion.Cloner) error {
in.(*OwnerList).DeepCopyInto(out.(*OwnerList))
return nil
}, InType: reflect.TypeOf(&OwnerList{})},
conversion.GeneratedDeepCopyFunc{Fn: func(in interface{}, out interface{}, c *conversion.Cloner) error {
in.(*Stack).DeepCopyInto(out.(*Stack))
return nil
}, InType: reflect.TypeOf(&Stack{})},
conversion.GeneratedDeepCopyFunc{Fn: func(in interface{}, out interface{}, c *conversion.Cloner) error {
in.(*StackList).DeepCopyInto(out.(*StackList))
return nil
}, InType: reflect.TypeOf(&StackList{})},
conversion.GeneratedDeepCopyFunc{Fn: func(in interface{}, out interface{}, c *conversion.Cloner) error {
in.(*StackSpec).DeepCopyInto(out.(*StackSpec))
return nil
}, InType: reflect.TypeOf(&StackSpec{})},
conversion.GeneratedDeepCopyFunc{Fn: func(in interface{}, out interface{}, c *conversion.Cloner) error {
in.(*StackStatus).DeepCopyInto(out.(*StackStatus))
return nil
}, InType: reflect.TypeOf(&StackStatus{})},
)
}
// DeepCopyInto is an autogenerated deepcopy function, copying the receiver, writing into out. in must be non-nil.
func (in *Owner) DeepCopyInto(out *Owner) {
*out = *in
out.TypeMeta = in.TypeMeta
in.ObjectMeta.DeepCopyInto(&out.ObjectMeta)
in.Owner.DeepCopyInto(&out.Owner)
return
}
// DeepCopyInto is an autogenerated deepcopy function, copying the receiver, creating a new Owner.
func (x *Owner) DeepCopy() *Owner {
if x == nil {
return nil
}
out := new(Owner)
x.DeepCopyInto(out)
return out
}
// DeepCopyObject is an autogenerated deepcopy function, copying the receiver, creating a new runtime.Object.
func (x *Owner) DeepCopyObject() runtime.Object {
if c := x.DeepCopy(); c != nil {
return c
} else {
return nil
}
}
// DeepCopyInto is an autogenerated deepcopy function, copying the receiver, writing into out. in must be non-nil.
func (in *OwnerList) DeepCopyInto(out *OwnerList) {
*out = *in
out.TypeMeta = in.TypeMeta
out.ListMeta = in.ListMeta
if in.Items != nil {
in, out := &in.Items, &out.Items
*out = make([]Owner, len(*in))
for i := range *in {
(*in)[i].DeepCopyInto(&(*out)[i])
}
}
return
}
// DeepCopyInto is an autogenerated deepcopy function, copying the receiver, creating a new OwnerList.
func (x *OwnerList) DeepCopy() *OwnerList {
if x == nil {
return nil
}
out := new(OwnerList)
x.DeepCopyInto(out)
return out
}
// DeepCopyObject is an autogenerated deepcopy function, copying the receiver, creating a new runtime.Object.
func (x *OwnerList) DeepCopyObject() runtime.Object {
if c := x.DeepCopy(); c != nil {
return c
} else {
return nil
}
}
// DeepCopyInto is an autogenerated deepcopy function, copying the receiver, writing into out. in must be non-nil.
func (in *Stack) DeepCopyInto(out *Stack) {
*out = *in
out.TypeMeta = in.TypeMeta
in.ObjectMeta.DeepCopyInto(&out.ObjectMeta)
out.Spec = in.Spec
out.Status = in.Status
return
}
// DeepCopyInto is an autogenerated deepcopy function, copying the receiver, creating a new Stack.
func (x *Stack) DeepCopy() *Stack {
if x == nil {
return nil
}
out := new(Stack)
x.DeepCopyInto(out)
return out
}
// DeepCopyObject is an autogenerated deepcopy function, copying the receiver, creating a new runtime.Object.
func (x *Stack) DeepCopyObject() runtime.Object {
if c := x.DeepCopy(); c != nil {
return c
} else {
return nil
}
}
// DeepCopyInto is an autogenerated deepcopy function, copying the receiver, writing into out. in must be non-nil.
func (in *StackList) DeepCopyInto(out *StackList) {
*out = *in
out.TypeMeta = in.TypeMeta
out.ListMeta = in.ListMeta
if in.Items != nil {
in, out := &in.Items, &out.Items
*out = make([]Stack, len(*in))
for i := range *in {
(*in)[i].DeepCopyInto(&(*out)[i])
}
}
return
}
// DeepCopyInto is an autogenerated deepcopy function, copying the receiver, creating a new StackList.
func (x *StackList) DeepCopy() *StackList {
if x == nil {
return nil
}
out := new(StackList)
x.DeepCopyInto(out)
return out
}
// DeepCopyObject is an autogenerated deepcopy function, copying the receiver, creating a new runtime.Object.
func (x *StackList) DeepCopyObject() runtime.Object {
if c := x.DeepCopy(); c != nil {
return c
} else {
return nil
}
}
// DeepCopyInto is an autogenerated deepcopy function, copying the receiver, writing into out. in must be non-nil.
func (in *StackSpec) DeepCopyInto(out *StackSpec) {
*out = *in
return
}
// DeepCopyInto is an autogenerated deepcopy function, copying the receiver, creating a new StackSpec.
func (x *StackSpec) DeepCopy() *StackSpec {
if x == nil {
return nil
}
out := new(StackSpec)
x.DeepCopyInto(out)
return out
}
// DeepCopyInto is an autogenerated deepcopy function, copying the receiver, writing into out. in must be non-nil.
func (in *StackStatus) DeepCopyInto(out *StackStatus) {
*out = *in
return
}
// DeepCopyInto is an autogenerated deepcopy function, copying the receiver, creating a new StackStatus.
func (x *StackStatus) DeepCopy() *StackStatus {
if x == nil {
return nil
}
out := new(StackStatus)
x.DeepCopyInto(out)
return out
}

View file

@ -0,0 +1,233 @@
// +build !ignore_autogenerated
// This file was autogenerated by deepcopy-gen. Do not edit it manually!
package compose
import (
reflect "reflect"
conversion "k8s.io/apimachinery/pkg/conversion"
runtime "k8s.io/apimachinery/pkg/runtime"
)
// Deprecated: register deep-copy functions.
func init() {
SchemeBuilder.Register(RegisterDeepCopies)
}
// Deprecated: RegisterDeepCopies adds deep-copy functions to the given scheme. Public
// to allow building arbitrary schemes.
func RegisterDeepCopies(scheme *runtime.Scheme) error {
return scheme.AddGeneratedDeepCopyFuncs(
conversion.GeneratedDeepCopyFunc{Fn: func(in interface{}, out interface{}, c *conversion.Cloner) error {
in.(*ImpersonationConfig).DeepCopyInto(out.(*ImpersonationConfig))
return nil
}, InType: reflect.TypeOf(&ImpersonationConfig{})},
conversion.GeneratedDeepCopyFunc{Fn: func(in interface{}, out interface{}, c *conversion.Cloner) error {
in.(*Owner).DeepCopyInto(out.(*Owner))
return nil
}, InType: reflect.TypeOf(&Owner{})},
conversion.GeneratedDeepCopyFunc{Fn: func(in interface{}, out interface{}, c *conversion.Cloner) error {
in.(*OwnerList).DeepCopyInto(out.(*OwnerList))
return nil
}, InType: reflect.TypeOf(&OwnerList{})},
conversion.GeneratedDeepCopyFunc{Fn: func(in interface{}, out interface{}, c *conversion.Cloner) error {
in.(*Stack).DeepCopyInto(out.(*Stack))
return nil
}, InType: reflect.TypeOf(&Stack{})},
conversion.GeneratedDeepCopyFunc{Fn: func(in interface{}, out interface{}, c *conversion.Cloner) error {
in.(*StackList).DeepCopyInto(out.(*StackList))
return nil
}, InType: reflect.TypeOf(&StackList{})},
conversion.GeneratedDeepCopyFunc{Fn: func(in interface{}, out interface{}, c *conversion.Cloner) error {
in.(*StackSpec).DeepCopyInto(out.(*StackSpec))
return nil
}, InType: reflect.TypeOf(&StackSpec{})},
conversion.GeneratedDeepCopyFunc{Fn: func(in interface{}, out interface{}, c *conversion.Cloner) error {
in.(*StackStatus).DeepCopyInto(out.(*StackStatus))
return nil
}, InType: reflect.TypeOf(&StackStatus{})},
)
}
// DeepCopyInto is an autogenerated deepcopy function, copying the receiver, writing into out. in must be non-nil.
func (in *ImpersonationConfig) DeepCopyInto(out *ImpersonationConfig) {
*out = *in
if in.Groups != nil {
in, out := &in.Groups, &out.Groups
*out = make([]string, len(*in))
copy(*out, *in)
}
if in.Extra != nil {
in, out := &in.Extra, &out.Extra
*out = make(map[string][]string, len(*in))
for key, val := range *in {
if val == nil {
(*out)[key] = nil
} else {
(*out)[key] = make([]string, len(val))
copy((*out)[key], val)
}
}
}
return
}
// DeepCopyInto is an autogenerated deepcopy function, copying the receiver, creating a new ImpersonationConfig.
func (x *ImpersonationConfig) DeepCopy() *ImpersonationConfig {
if x == nil {
return nil
}
out := new(ImpersonationConfig)
x.DeepCopyInto(out)
return out
}
// DeepCopyInto is an autogenerated deepcopy function, copying the receiver, writing into out. in must be non-nil.
func (in *Owner) DeepCopyInto(out *Owner) {
*out = *in
out.TypeMeta = in.TypeMeta
in.ObjectMeta.DeepCopyInto(&out.ObjectMeta)
in.Owner.DeepCopyInto(&out.Owner)
return
}
// DeepCopyInto is an autogenerated deepcopy function, copying the receiver, creating a new Owner.
func (x *Owner) DeepCopy() *Owner {
if x == nil {
return nil
}
out := new(Owner)
x.DeepCopyInto(out)
return out
}
func (x *Owner) DeepCopyObject() runtime.Object {
if c := x.DeepCopy(); c != nil {
return c
} else {
return nil
}
}
// DeepCopyInto is an autogenerated deepcopy function, copying the receiver, writing into out. in must be non-nil.
func (in *OwnerList) DeepCopyInto(out *OwnerList) {
*out = *in
out.TypeMeta = in.TypeMeta
out.ListMeta = in.ListMeta
if in.Items != nil {
in, out := &in.Items, &out.Items
*out = make([]Owner, len(*in))
for i := range *in {
(*in)[i].DeepCopyInto(&(*out)[i])
}
}
return
}
// DeepCopyInto is an autogenerated deepcopy function, copying the receiver, creating a new OwnerList.
func (x *OwnerList) DeepCopy() *OwnerList {
if x == nil {
return nil
}
out := new(OwnerList)
x.DeepCopyInto(out)
return out
}
// DeepCopyInto is an autogenerated deepcopy function, copying the receiver, writing into out. in must be non-nil.
func (in *Stack) DeepCopyInto(out *Stack) {
*out = *in
out.TypeMeta = in.TypeMeta
in.ObjectMeta.DeepCopyInto(&out.ObjectMeta)
in.Spec.DeepCopyInto(&out.Spec)
out.Status = in.Status
return
}
// DeepCopyInto is an autogenerated deepcopy function, copying the receiver, creating a new Stack.
func (x *Stack) DeepCopy() *Stack {
if x == nil {
return nil
}
out := new(Stack)
x.DeepCopyInto(out)
return out
}
// DeepCopyObject is an autogenerated deepcopy function, copying the receiver, creating a new runtime.Object.
func (x *Stack) DeepCopyObject() runtime.Object {
if c := x.DeepCopy(); c != nil {
return c
} else {
return nil
}
}
// DeepCopyInto is an autogenerated deepcopy function, copying the receiver, writing into out. in must be non-nil.
func (in *StackList) DeepCopyInto(out *StackList) {
*out = *in
out.TypeMeta = in.TypeMeta
out.ListMeta = in.ListMeta
if in.Items != nil {
in, out := &in.Items, &out.Items
*out = make([]Stack, len(*in))
for i := range *in {
(*in)[i].DeepCopyInto(&(*out)[i])
}
}
return
}
// DeepCopyInto is an autogenerated deepcopy function, copying the receiver, creating a new StackList.
func (x *StackList) DeepCopy() *StackList {
if x == nil {
return nil
}
out := new(StackList)
x.DeepCopyInto(out)
return out
}
// DeepCopyObject is an autogenerated deepcopy function, copying the receiver, creating a new runtime.Object.
func (x *StackList) DeepCopyObject() runtime.Object {
if c := x.DeepCopy(); c != nil {
return c
} else {
return nil
}
}
// DeepCopyInto is an autogenerated deepcopy function, copying the receiver, writing into out. in must be non-nil.
func (in *StackSpec) DeepCopyInto(out *StackSpec) {
*out = *in
in.Owner.DeepCopyInto(&out.Owner)
return
}
// DeepCopyInto is an autogenerated deepcopy function, copying the receiver, creating a new StackSpec.
func (x *StackSpec) DeepCopy() *StackSpec {
if x == nil {
return nil
}
out := new(StackSpec)
x.DeepCopyInto(out)
return out
}
// DeepCopyInto is an autogenerated deepcopy function, copying the receiver, writing into out. in must be non-nil.
func (in *StackStatus) DeepCopyInto(out *StackStatus) {
*out = *in
return
}
// DeepCopyInto is an autogenerated deepcopy function, copying the receiver, creating a new StackStatus.
func (x *StackStatus) DeepCopy() *StackStatus {
if x == nil {
return nil
}
out := new(StackStatus)
x.DeepCopyInto(out)
return out
}

View file

@ -0,0 +1,24 @@
package kubernetes
import (
"os"
"path/filepath"
"github.com/docker/docker/pkg/homedir"
restclient "k8s.io/client-go/rest"
"k8s.io/client-go/tools/clientcmd"
)
// NewKubernetesConfig resolves the path to the desired Kubernetes configuration file, depending
// environment variable and command line flag.
func NewKubernetesConfig(configFlag string) (*restclient.Config, error) {
kubeConfig := configFlag
if kubeConfig == "" {
if config := os.Getenv("KUBECONFIG"); config != "" {
kubeConfig = config
} else {
kubeConfig = filepath.Join(homedir.Get(), ".kube/config")
}
}
return clientcmd.BuildConfigFromFlags("", kubeConfig)
}

View file

@ -0,0 +1,4 @@
//
// +domain=docker.com
package kubernetes

View file

@ -0,0 +1,58 @@
package labels
import (
"fmt"
"strings"
)
const (
// ForServiceName is the label for the service name.
ForServiceName = "com.docker.service.name"
// ForStackName is the label for the stack name.
ForStackName = "com.docker.stack.namespace"
// ForServiceID is the label for the service id.
ForServiceID = "com.docker.service.id"
)
// ForService gives the labels to select a given service in a stack.
func ForService(stackName, serviceName string) map[string]string {
labels := map[string]string{}
if serviceName != "" {
labels[ForServiceName] = serviceName
}
if stackName != "" {
labels[ForStackName] = stackName
}
if serviceName != "" && stackName != "" {
labels[ForServiceID] = stackName + "-" + serviceName
}
return labels
}
// Merge merges multiple lists of labels.
func Merge(labelsList ...map[string]string) map[string]string {
merged := map[string]string{}
for _, labels := range labelsList {
for k, v := range labels {
merged[k] = v
}
}
return merged
}
// SelectorForStack gives the labelSelector to use for a given stack.
// Specific service names can be passed to narrow down the selection.
func SelectorForStack(stackName string, serviceNames ...string) string {
switch len(serviceNames) {
case 0:
return fmt.Sprintf("%s=%s", ForStackName, stackName)
case 1:
return fmt.Sprintf("%s=%s,%s=%s", ForStackName, stackName, ForServiceName, serviceNames[0])
default:
return fmt.Sprintf("%s=%s,%s in (%s)", ForStackName, stackName, ForServiceName, strings.Join(serviceNames, ","))
}
}

View file

@ -0,0 +1,23 @@
package labels
import (
"testing"
"github.com/gotestyourself/gotestyourself/assert"
is "github.com/gotestyourself/gotestyourself/assert/cmp"
)
func TestForService(t *testing.T) {
labels := ForService("stack", "service")
assert.Check(t, is.Len(labels, 3))
assert.Check(t, is.Equal("stack", labels["com.docker.stack.namespace"]))
assert.Check(t, is.Equal("service", labels["com.docker.service.name"]))
assert.Check(t, is.Equal("stack-service", labels["com.docker.service.id"]))
}
func TestSelectorForStack(t *testing.T) {
assert.Check(t, is.Equal("com.docker.stack.namespace=demostack", SelectorForStack("demostack")))
assert.Check(t, is.Equal("com.docker.stack.namespace=stack,com.docker.service.name=service", SelectorForStack("stack", "service")))
assert.Check(t, is.Equal("com.docker.stack.namespace=stack,com.docker.service.name in (service1,service2)", SelectorForStack("stack", "service1", "service2")))
}