-
Notifications
You must be signed in to change notification settings - Fork 1.9k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Fix docker stack services command Port output #943
Merged
thaJeztah
merged 1 commit into
docker:master
from
silvin-lubecki:fix-kubernetes-published-service
Mar 26, 2018
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,192 @@ | ||
package kubernetes | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/docker/cli/cli/command/formatter" | ||
"github.com/docker/cli/kubernetes/labels" | ||
"github.com/docker/docker/api/types/swarm" | ||
"github.com/gotestyourself/gotestyourself/assert" | ||
appsv1beta2 "k8s.io/api/apps/v1beta2" | ||
apiv1 "k8s.io/api/core/v1" | ||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
apimachineryTypes "k8s.io/apimachinery/pkg/types" | ||
apimachineryUtil "k8s.io/apimachinery/pkg/util/intstr" | ||
) | ||
|
||
func TestReplicasConversionNeedsAService(t *testing.T) { | ||
replicas := appsv1beta2.ReplicaSetList{ | ||
Items: []appsv1beta2.ReplicaSet{makeReplicaSet("unknown", 0, 0)}, | ||
} | ||
services := apiv1.ServiceList{} | ||
_, _, err := replicasToServices(&replicas, &services) | ||
assert.ErrorContains(t, err, "could not find service") | ||
} | ||
|
||
func TestKubernetesServiceToSwarmServiceConversion(t *testing.T) { | ||
testCases := []struct { | ||
replicas *appsv1beta2.ReplicaSetList | ||
services *apiv1.ServiceList | ||
expectedServices []swarm.Service | ||
expectedListInfo map[string]formatter.ServiceListInfo | ||
}{ | ||
// Match replicas with headless stack services | ||
{ | ||
&appsv1beta2.ReplicaSetList{ | ||
Items: []appsv1beta2.ReplicaSet{ | ||
makeReplicaSet("service1", 2, 5), | ||
makeReplicaSet("service2", 3, 3), | ||
}, | ||
}, | ||
&apiv1.ServiceList{ | ||
Items: []apiv1.Service{ | ||
makeKubeService("service1", "stack", "uid1", apiv1.ServiceTypeClusterIP, nil), | ||
makeKubeService("service2", "stack", "uid2", apiv1.ServiceTypeClusterIP, nil), | ||
makeKubeService("service3", "other-stack", "uid2", apiv1.ServiceTypeClusterIP, nil), | ||
}, | ||
}, | ||
[]swarm.Service{ | ||
makeSwarmService("stack_service1", "uid1", nil), | ||
makeSwarmService("stack_service2", "uid2", nil), | ||
}, | ||
map[string]formatter.ServiceListInfo{ | ||
"uid1": {"replicated", "2/5"}, | ||
"uid2": {"replicated", "3/3"}, | ||
}, | ||
}, | ||
// Headless service and LoadBalancer Service are tied to the same Swarm service | ||
{ | ||
&appsv1beta2.ReplicaSetList{ | ||
Items: []appsv1beta2.ReplicaSet{ | ||
makeReplicaSet("service", 1, 1), | ||
}, | ||
}, | ||
&apiv1.ServiceList{ | ||
Items: []apiv1.Service{ | ||
makeKubeService("service", "stack", "uid1", apiv1.ServiceTypeClusterIP, nil), | ||
makeKubeService("service-published", "stack", "uid2", apiv1.ServiceTypeLoadBalancer, []apiv1.ServicePort{ | ||
{ | ||
Port: 80, | ||
TargetPort: apimachineryUtil.FromInt(80), | ||
Protocol: apiv1.ProtocolTCP, | ||
}, | ||
}), | ||
}, | ||
}, | ||
[]swarm.Service{ | ||
makeSwarmService("stack_service", "uid1", []swarm.PortConfig{ | ||
{ | ||
PublishMode: swarm.PortConfigPublishModeIngress, | ||
PublishedPort: 80, | ||
TargetPort: 80, | ||
Protocol: swarm.PortConfigProtocolTCP, | ||
}, | ||
}), | ||
}, | ||
map[string]formatter.ServiceListInfo{ | ||
"uid1": {"replicated", "1/1"}, | ||
}, | ||
}, | ||
// Headless service and NodePort Service are tied to the same Swarm service | ||
|
||
{ | ||
&appsv1beta2.ReplicaSetList{ | ||
Items: []appsv1beta2.ReplicaSet{ | ||
makeReplicaSet("service", 1, 1), | ||
}, | ||
}, | ||
&apiv1.ServiceList{ | ||
Items: []apiv1.Service{ | ||
makeKubeService("service", "stack", "uid1", apiv1.ServiceTypeClusterIP, nil), | ||
makeKubeService("service-random-ports", "stack", "uid2", apiv1.ServiceTypeNodePort, []apiv1.ServicePort{ | ||
{ | ||
Port: 35666, | ||
TargetPort: apimachineryUtil.FromInt(80), | ||
Protocol: apiv1.ProtocolTCP, | ||
}, | ||
}), | ||
}, | ||
}, | ||
[]swarm.Service{ | ||
makeSwarmService("stack_service", "uid1", []swarm.PortConfig{ | ||
{ | ||
PublishMode: swarm.PortConfigPublishModeHost, | ||
PublishedPort: 35666, | ||
TargetPort: 80, | ||
Protocol: swarm.PortConfigProtocolTCP, | ||
}, | ||
}), | ||
}, | ||
map[string]formatter.ServiceListInfo{ | ||
"uid1": {"replicated", "1/1"}, | ||
}, | ||
}, | ||
} | ||
|
||
for _, tc := range testCases { | ||
swarmServices, listInfo, err := replicasToServices(tc.replicas, tc.services) | ||
assert.NilError(t, err) | ||
assert.DeepEqual(t, tc.expectedServices, swarmServices) | ||
assert.DeepEqual(t, tc.expectedListInfo, listInfo) | ||
} | ||
} | ||
|
||
func makeReplicaSet(service string, available, replicas int32) appsv1beta2.ReplicaSet { | ||
return appsv1beta2.ReplicaSet{ | ||
ObjectMeta: metav1.ObjectMeta{ | ||
Labels: map[string]string{ | ||
labels.ForServiceName: service, | ||
}, | ||
}, | ||
Spec: appsv1beta2.ReplicaSetSpec{ | ||
Template: apiv1.PodTemplateSpec{ | ||
Spec: apiv1.PodSpec{ | ||
Containers: []apiv1.Container{ | ||
{ | ||
Image: "image", | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
Status: appsv1beta2.ReplicaSetStatus{ | ||
AvailableReplicas: available, | ||
Replicas: replicas, | ||
}, | ||
} | ||
} | ||
|
||
func makeKubeService(service, stack, uid string, serviceType apiv1.ServiceType, ports []apiv1.ServicePort) apiv1.Service { | ||
return apiv1.Service{ | ||
ObjectMeta: metav1.ObjectMeta{ | ||
Labels: map[string]string{ | ||
labels.ForStackName: stack, | ||
}, | ||
Name: service, | ||
UID: apimachineryTypes.UID(uid), | ||
}, | ||
Spec: apiv1.ServiceSpec{ | ||
Type: serviceType, | ||
Ports: ports, | ||
}, | ||
} | ||
} | ||
|
||
func makeSwarmService(service, id string, ports []swarm.PortConfig) swarm.Service { | ||
return swarm.Service{ | ||
ID: id, | ||
Spec: swarm.ServiceSpec{ | ||
Annotations: swarm.Annotations{ | ||
Name: service, | ||
}, | ||
TaskTemplate: swarm.TaskSpec{ | ||
ContainerSpec: &swarm.ContainerSpec{ | ||
Image: "image", | ||
}, | ||
}, | ||
}, | ||
Endpoint: swarm.Endpoint{ | ||
Ports: ports, | ||
}, | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit: There was some discussion around naming of this; as they're not fully "random", but "selected from an ephemeral range"
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ok, good to know, but unfortunately these names are defined in the already shipped compose controller...
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah, probably not a big issue, it's just that they are not really "random" (could be sequential), so "ephemeral" is possibly a better description.