Skip to content
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

Do not allow service creation on ingress network #1600

Merged
merged 1 commit into from
Oct 5, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions manager/controlapi/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,24 @@ func validateEndpointSpec(epSpec *api.EndpointSpec) error {
return nil
}

func (s *Server) validateNetworks(networks []*api.NetworkAttachmentConfig) error {
for _, na := range networks {
var network *api.Network
s.store.View(func(tx store.ReadTx) {
network = store.FindNetwork(tx, na.Target)
})
if network == nil {
continue
}
if _, ok := network.Spec.Annotations.Labels["com.docker.swarm.internal"]; ok {
return grpc.Errorf(codes.InvalidArgument,
"Service cannot be explicitly attached to %q network which is a swarm internal network",
network.Spec.Annotations.Name)
}
}
return nil
}

func validateServiceSpec(spec *api.ServiceSpec) error {
if spec == nil {
return grpc.Errorf(codes.InvalidArgument, errInvalidArgument.Error())
Expand Down Expand Up @@ -259,6 +277,10 @@ func (s *Server) CreateService(ctx context.Context, request *api.CreateServiceRe
return nil, err
}

if err := s.validateNetworks(request.Spec.Networks); err != nil {
return nil, err
}

if err := s.checkPortConflicts(request.Spec, ""); err != nil {
return nil, err
}
Expand Down
16 changes: 16 additions & 0 deletions manager/state/store/networks.go
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,22 @@ func FindNetworks(tx ReadTx, by By) ([]*api.Network, error) {
return networkList, err
}

// FindNetwork is a utility function which returns the first
// network for which the target string matches the ID, or
// the name or the ID prefix.
func FindNetwork(tx ReadTx, target string) *api.Network {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@aboch does this function need to return errors?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No, it is a best effort function on the same line of the existing Getnetwork().
If one query fails (ID, name or id pefix), it moves to the next one.

Eventually, the outcome will be the same as we would have if we reported an error happening during one query: We would eventually not be able to identify an network.

From the caller perspective, it will be a failure regardless.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for clarifying.

if n := GetNetwork(tx, target); n != nil {
return n
}
if list, err := FindNetworks(tx, ByName(target)); err == nil && len(list) == 1 {
return list[0]
}
if list, err := FindNetworks(tx, ByIDPrefix(target)); err == nil && len(list) == 1 {
return list[0]
}
return nil
}

type networkIndexerByID struct{}

func (ni networkIndexerByID) FromArgs(args ...interface{}) ([]byte, error) {
Expand Down