Remove service annotation for namedPorts
This commit is contained in:
parent
c5e30973e5
commit
d98a052972
5 changed files with 3 additions and 398 deletions
|
|
@ -1,73 +0,0 @@
|
|||
/*
|
||||
Copyright 2015 The Kubernetes Authors.
|
||||
|
||||
Licensed under the Apache License, Version 2.0 (the "License");
|
||||
you may not use this file except in compliance with the License.
|
||||
You may obtain a copy of the License at
|
||||
|
||||
http://www.apache.org/licenses/LICENSE-2.0
|
||||
|
||||
Unless required by applicable law or agreed to in writing, software
|
||||
distributed under the License is distributed on an "AS IS" BASIS,
|
||||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
See the License for the specific language governing permissions and
|
||||
limitations under the License.
|
||||
*/
|
||||
|
||||
package service
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"strconv"
|
||||
|
||||
"github.com/golang/glog"
|
||||
|
||||
api_v1 "k8s.io/client-go/pkg/api/v1"
|
||||
)
|
||||
|
||||
const (
|
||||
// NamedPortAnnotation annotation used to map named port in services
|
||||
NamedPortAnnotation = "ingress.kubernetes.io/named-ports"
|
||||
)
|
||||
|
||||
type namedPortMapping map[string]string
|
||||
|
||||
// getPort returns the port defined in a named port
|
||||
func (npm namedPortMapping) getPort(name string) (string, bool) {
|
||||
val, ok := npm.getPortMappings()[name]
|
||||
return val, ok
|
||||
}
|
||||
|
||||
// getPortMappings returns a map containing the mapping of named ports names and number
|
||||
func (npm namedPortMapping) getPortMappings() map[string]string {
|
||||
data := npm[NamedPortAnnotation]
|
||||
var mapping map[string]string
|
||||
if data == "" {
|
||||
return mapping
|
||||
}
|
||||
if err := json.Unmarshal([]byte(data), &mapping); err != nil {
|
||||
glog.Errorf("unexpected error reading annotations: %v", err)
|
||||
}
|
||||
|
||||
return mapping
|
||||
}
|
||||
|
||||
// GetPortMapping returns the number of the named port or an error if is not valid
|
||||
func GetPortMapping(name string, s *api_v1.Service) (int32, error) {
|
||||
if s == nil {
|
||||
return -1, fmt.Errorf("impossible to extract por mapping from %v (missing service)", name)
|
||||
}
|
||||
namedPorts := s.ObjectMeta.Annotations
|
||||
val, ok := namedPortMapping(namedPorts).getPort(name)
|
||||
if ok {
|
||||
port, err := strconv.Atoi(val)
|
||||
if err != nil {
|
||||
return -1, fmt.Errorf("service %v contains an invalid port mapping for %v (%v), %v", s.Name, name, val, err)
|
||||
}
|
||||
|
||||
return int32(port), nil
|
||||
}
|
||||
|
||||
return -1, fmt.Errorf("there is no port with name %v", name)
|
||||
}
|
||||
|
|
@ -1,100 +0,0 @@
|
|||
/*
|
||||
Copyright 2017 The Kubernetes Authors.
|
||||
|
||||
Licensed under the Apache License, Version 2.0 (the "License");
|
||||
you may not use this file except in compliance with the License.
|
||||
You may obtain a copy of the License at
|
||||
|
||||
http://www.apache.org/licenses/LICENSE-2.0
|
||||
|
||||
Unless required by applicable law or agreed to in writing, software
|
||||
distributed under the License is distributed on an "AS IS" BASIS,
|
||||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
See the License for the specific language governing permissions and
|
||||
limitations under the License.
|
||||
*/
|
||||
|
||||
package service
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"testing"
|
||||
|
||||
meta_v1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
||||
api "k8s.io/client-go/pkg/api/v1"
|
||||
)
|
||||
|
||||
func fakeService(npa bool, ps bool, expectedP string) *api.Service {
|
||||
// fake name for the map of ports
|
||||
fakeNpa := NamedPortAnnotation
|
||||
if !npa {
|
||||
fakeNpa = "fake" + NamedPortAnnotation
|
||||
}
|
||||
|
||||
// fake ports
|
||||
fakePs, _ := json.Marshal(map[string]string{
|
||||
"port1": expectedP,
|
||||
"port2": "10211",
|
||||
})
|
||||
if !ps {
|
||||
fakePs, _ = json.Marshal(expectedP)
|
||||
}
|
||||
|
||||
// fake service
|
||||
return &api.Service{
|
||||
TypeMeta: meta_v1.TypeMeta{
|
||||
Kind: "ingress",
|
||||
APIVersion: "v1",
|
||||
},
|
||||
ObjectMeta: meta_v1.ObjectMeta{
|
||||
Annotations: map[string]string{
|
||||
fakeNpa: string(fakePs),
|
||||
},
|
||||
Namespace: api.NamespaceDefault,
|
||||
Name: "named-port-test",
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
func TestGetPortMappingSuccess(t *testing.T) {
|
||||
fakeS := fakeService(true, true, "10011")
|
||||
port, err := GetPortMapping("port1", fakeS)
|
||||
if err != nil {
|
||||
t.Errorf("failed to get port with name %s, error: %v", "port1", err)
|
||||
return
|
||||
}
|
||||
if port != 10011 {
|
||||
t.Errorf("%s: expected %d but returned %d", "port1", 10011, port)
|
||||
}
|
||||
}
|
||||
|
||||
func TestGetPortMappingFailedNamedPortMappingNotExist(t *testing.T) {
|
||||
fakeS := fakeService(false, true, "10011")
|
||||
_, err := GetPortMapping("port1", fakeS)
|
||||
if err == nil {
|
||||
t.Errorf("%s: expected error but returned nil", "port1")
|
||||
}
|
||||
}
|
||||
|
||||
func TestGetPortMappingFailedPortNotExist(t *testing.T) {
|
||||
fakeS := fakeService(true, true, "10011")
|
||||
_, err := GetPortMapping("port3", fakeS)
|
||||
if err == nil {
|
||||
t.Errorf("%s: expected error but returned nil", "port3")
|
||||
}
|
||||
}
|
||||
|
||||
func TestGetPortMappingFailedPortInvalid(t *testing.T) {
|
||||
fakeS := fakeService(true, true, "s2017")
|
||||
_, err := GetPortMapping("port1", fakeS)
|
||||
if err == nil {
|
||||
t.Errorf("%s: expected error but returned nil", "port1")
|
||||
}
|
||||
}
|
||||
|
||||
func TestGetPortMappingFailedApiServiceIsNil(t *testing.T) {
|
||||
_, err := GetPortMapping("port1", nil)
|
||||
if err == nil {
|
||||
t.Errorf("%s: expected error but returned nil", "port1")
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue