Skip to content

Commit

Permalink
Remove code duplication
Browse files Browse the repository at this point in the history
Signed-off-by: Mathieu Champlon <mathieu.champlon@docker.com>
  • Loading branch information
mat007 committed May 4, 2018
1 parent f4d304f commit 1d935a2
Showing 1 changed file with 41 additions and 58 deletions.
99 changes: 41 additions & 58 deletions cli/command/stack/kubernetes/conversion.go
Original file line number Diff line number Diff line change
Expand Up @@ -161,72 +161,22 @@ func replicasToServices(replicas *appsv1beta2.ReplicaSetList, daemons *appsv1bet
result := make([]swarm.Service, l)
infos := make(map[string]formatter.ServiceListInfo, l)
for i, r := range replicas.Items {
serviceName := r.Labels[labels.ForServiceName]
serviceHeadless, ok := findService(services, serviceName)
if !ok {
return nil, nil, fmt.Errorf("could not find service '%s'", serviceName)
}
stack, ok := serviceHeadless.Labels[labels.ForStackName]
if ok {
stack += "_"
}
uid := string(serviceHeadless.UID)
s := swarm.Service{
ID: uid,
Spec: swarm.ServiceSpec{
Annotations: swarm.Annotations{
Name: stack + serviceHeadless.Name,
},
TaskTemplate: swarm.TaskSpec{
ContainerSpec: &swarm.ContainerSpec{
Image: getContainerImage(r.Spec.Template.Spec.Containers),
},
},
},
}
if serviceNodePort, ok := findService(services, serviceName+publishedOnRandomPortSuffix); ok && serviceNodePort.Spec.Type == apiv1.ServiceTypeNodePort {
s.Endpoint = serviceEndpoint(serviceNodePort, swarm.PortConfigPublishModeHost)
}
if serviceLoadBalancer, ok := findService(services, serviceName+publishedServiceSuffix); ok && serviceLoadBalancer.Spec.Type == apiv1.ServiceTypeLoadBalancer {
s.Endpoint = serviceEndpoint(serviceLoadBalancer, swarm.PortConfigPublishModeIngress)
s, uid, err := replicaToService(r.Labels, services, r.Spec.Template.Spec.Containers)
if err != nil {
return nil, nil, err
}
result[i] = s
result[i] = *s
infos[uid] = formatter.ServiceListInfo{
Mode: "replicated",
Replicas: fmt.Sprintf("%d/%d", r.Status.AvailableReplicas, r.Status.Replicas),
}
}
for i, d := range daemons.Items {
serviceName := d.Labels[labels.ForServiceName]
serviceHeadless, ok := findService(services, serviceName)
if !ok {
return nil, nil, fmt.Errorf("could not find service '%s'", serviceName)
}
stack, ok := serviceHeadless.Labels[labels.ForStackName]
if ok {
stack += "_"
}
uid := string(serviceHeadless.UID)
s := swarm.Service{
ID: uid,
Spec: swarm.ServiceSpec{
Annotations: swarm.Annotations{
Name: stack + serviceHeadless.Name,
},
TaskTemplate: swarm.TaskSpec{
ContainerSpec: &swarm.ContainerSpec{
Image: getContainerImage(d.Spec.Template.Spec.Containers),
},
},
},
}
if serviceNodePort, ok := findService(services, serviceName+publishedOnRandomPortSuffix); ok && serviceNodePort.Spec.Type == apiv1.ServiceTypeNodePort {
s.Endpoint = serviceEndpoint(serviceNodePort, swarm.PortConfigPublishModeHost)
}
if serviceLoadBalancer, ok := findService(services, serviceName+publishedServiceSuffix); ok && serviceLoadBalancer.Spec.Type == apiv1.ServiceTypeLoadBalancer {
s.Endpoint = serviceEndpoint(serviceLoadBalancer, swarm.PortConfigPublishModeIngress)
s, uid, err := replicaToService(d.Labels, services, d.Spec.Template.Spec.Containers)
if err != nil {
return nil, nil, err
}
result[i+len(replicas.Items)] = s
result[i+len(replicas.Items)] = *s
infos[uid] = formatter.ServiceListInfo{
Mode: "global",
Replicas: fmt.Sprintf("%d/%d", d.Status.NumberReady, d.Status.DesiredNumberScheduled),
Expand All @@ -238,6 +188,39 @@ func replicasToServices(replicas *appsv1beta2.ReplicaSetList, daemons *appsv1bet
return result, infos, nil
}

func replicaToService(labls map[string]string, services *apiv1.ServiceList, containers []apiv1.Container) (*swarm.Service, string, error) {
serviceName := labls[labels.ForServiceName]
serviceHeadless, ok := findService(services, serviceName)
if !ok {
return nil, "", fmt.Errorf("could not find service '%s'", serviceName)
}
stack, ok := serviceHeadless.Labels[labels.ForStackName]
if ok {
stack += "_"
}
uid := string(serviceHeadless.UID)
s := &swarm.Service{
ID: uid,
Spec: swarm.ServiceSpec{
Annotations: swarm.Annotations{
Name: stack + serviceHeadless.Name,
},
TaskTemplate: swarm.TaskSpec{
ContainerSpec: &swarm.ContainerSpec{
Image: getContainerImage(containers),
},
},
},
}
if serviceNodePort, ok := findService(services, serviceName+publishedOnRandomPortSuffix); ok && serviceNodePort.Spec.Type == apiv1.ServiceTypeNodePort {
s.Endpoint = serviceEndpoint(serviceNodePort, swarm.PortConfigPublishModeHost)
}
if serviceLoadBalancer, ok := findService(services, serviceName+publishedServiceSuffix); ok && serviceLoadBalancer.Spec.Type == apiv1.ServiceTypeLoadBalancer {
s.Endpoint = serviceEndpoint(serviceLoadBalancer, swarm.PortConfigPublishModeIngress)
}
return s, uid, nil
}

func findService(services *apiv1.ServiceList, name string) (apiv1.Service, bool) {
for _, s := range services.Items {
if s.Name == name {
Expand Down

0 comments on commit 1d935a2

Please sign in to comment.