Split implementations from generic code
This commit is contained in:
parent
d1e8a629ca
commit
ed9a416b01
107 changed files with 5777 additions and 3546 deletions
110
core/pkg/k8s/main.go
Normal file
110
core/pkg/k8s/main.go
Normal file
|
|
@ -0,0 +1,110 @@
|
|||
/*
|
||||
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 k8s
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
"strings"
|
||||
|
||||
"k8s.io/kubernetes/pkg/api"
|
||||
clientset "k8s.io/kubernetes/pkg/client/clientset_generated/internalclientset"
|
||||
)
|
||||
|
||||
// IsValidService checks if exists a service with the specified name
|
||||
func IsValidService(kubeClient clientset.Interface, name string) (*api.Service, error) {
|
||||
ns, name, err := ParseNameNS(name)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return kubeClient.Core().Services(ns).Get(name)
|
||||
}
|
||||
|
||||
// IsValidSecret checks if exists a secret with the specified name
|
||||
func IsValidSecret(kubeClient clientset.Interface, name string) (*api.Secret, error) {
|
||||
ns, name, err := ParseNameNS(name)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return kubeClient.Core().Secrets(ns).Get(name)
|
||||
}
|
||||
|
||||
// ParseNameNS parses a string searching a namespace and name
|
||||
func ParseNameNS(input string) (string, string, error) {
|
||||
nsName := strings.Split(input, "/")
|
||||
if len(nsName) != 2 {
|
||||
return "", "", fmt.Errorf("invalid format (namespace/name) found in '%v'", input)
|
||||
}
|
||||
|
||||
return nsName[0], nsName[1], nil
|
||||
}
|
||||
|
||||
// GetNodeIP returns the IP address of a node in the cluster
|
||||
func GetNodeIP(kubeClient clientset.Interface, name string) string {
|
||||
var externalIP string
|
||||
node, err := kubeClient.Core().Nodes().Get(name)
|
||||
if err != nil {
|
||||
return externalIP
|
||||
}
|
||||
|
||||
for _, address := range node.Status.Addresses {
|
||||
if address.Type == api.NodeExternalIP {
|
||||
if address.Address != "" {
|
||||
externalIP = address.Address
|
||||
break
|
||||
}
|
||||
}
|
||||
|
||||
if externalIP == "" && address.Type == api.NodeLegacyHostIP {
|
||||
externalIP = address.Address
|
||||
}
|
||||
}
|
||||
return externalIP
|
||||
}
|
||||
|
||||
// PodInfo contains runtime information about the pod running the Ingres controller
|
||||
type PodInfo struct {
|
||||
Name string
|
||||
Namespace string
|
||||
NodeIP string
|
||||
// Labels selectors of the running pod
|
||||
// This is used to search for other Ingress controller pods
|
||||
Labels map[string]string
|
||||
}
|
||||
|
||||
// GetPodDetails returns runtime information about the pod:
|
||||
// name, namespace and IP of the node where it is running
|
||||
func GetPodDetails(kubeClient clientset.Interface) (*PodInfo, error) {
|
||||
podName := os.Getenv("POD_NAME")
|
||||
podNs := os.Getenv("POD_NAMESPACE")
|
||||
|
||||
if podName == "" && podNs == "" {
|
||||
return nil, fmt.Errorf("unable to get POD information (missing POD_NAME or POD_NAMESPACE environment variable")
|
||||
}
|
||||
|
||||
pod, _ := kubeClient.Core().Pods(podNs).Get(podName)
|
||||
if pod == nil {
|
||||
return nil, fmt.Errorf("unable to get POD information")
|
||||
}
|
||||
|
||||
return &PodInfo{
|
||||
Name: podName,
|
||||
Namespace: podNs,
|
||||
NodeIP: GetNodeIP(kubeClient, pod.Spec.NodeName),
|
||||
Labels: pod.GetLabels(),
|
||||
}, nil
|
||||
}
|
||||
116
core/pkg/k8s/main_test.go
Normal file
116
core/pkg/k8s/main_test.go
Normal file
|
|
@ -0,0 +1,116 @@
|
|||
/*
|
||||
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 k8s
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"k8s.io/kubernetes/pkg/api"
|
||||
testclient "k8s.io/kubernetes/pkg/client/clientset_generated/internalclientset/fake"
|
||||
)
|
||||
|
||||
func TestParseNameNS(t *testing.T) {
|
||||
|
||||
tests := []struct {
|
||||
title string
|
||||
input string
|
||||
ns string
|
||||
name string
|
||||
expErr bool
|
||||
}{
|
||||
{"empty string", "", "", "", true},
|
||||
{"demo", "demo", "", "", true},
|
||||
{"kube-system", "kube-system", "", "", true},
|
||||
{"default/kube-system", "default/kube-system", "default", "kube-system", false},
|
||||
}
|
||||
|
||||
for _, test := range tests {
|
||||
ns, name, err := ParseNameNS(test.input)
|
||||
if test.expErr {
|
||||
if err == nil {
|
||||
t.Errorf("%v: expected error but retuned nil", test.title)
|
||||
}
|
||||
continue
|
||||
}
|
||||
if test.ns != ns {
|
||||
t.Errorf("%v: expected %v but retuned %v", test.title, test.ns, ns)
|
||||
}
|
||||
if test.name != name {
|
||||
t.Errorf("%v: expected %v but retuned %v", test.title, test.name, name)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestIsValidService(t *testing.T) {
|
||||
fk := testclient.NewSimpleClientset(&api.Service{
|
||||
ObjectMeta: api.ObjectMeta{
|
||||
Namespace: api.NamespaceDefault,
|
||||
Name: "demo",
|
||||
},
|
||||
})
|
||||
|
||||
_, err := IsValidService(fk, "")
|
||||
if err == nil {
|
||||
t.Errorf("expected error but retuned nil")
|
||||
}
|
||||
s, err := IsValidService(fk, "default/demo")
|
||||
if err != nil {
|
||||
t.Errorf("unexpected error: %v", err)
|
||||
}
|
||||
if s == nil {
|
||||
t.Errorf("expected a Service but retuned nil")
|
||||
}
|
||||
|
||||
fk = testclient.NewSimpleClientset()
|
||||
s, err = IsValidService(fk, "default/demo")
|
||||
if err == nil {
|
||||
t.Errorf("expected an error but retuned nil")
|
||||
}
|
||||
if s != nil {
|
||||
t.Errorf("unexpected Service returned: %v", s)
|
||||
}
|
||||
}
|
||||
|
||||
func TestIsValidSecret(t *testing.T) {
|
||||
fk := testclient.NewSimpleClientset(&api.Secret{
|
||||
ObjectMeta: api.ObjectMeta{
|
||||
Namespace: api.NamespaceDefault,
|
||||
Name: "demo",
|
||||
},
|
||||
})
|
||||
|
||||
_, err := IsValidSecret(fk, "")
|
||||
if err == nil {
|
||||
t.Errorf("expected error but retuned nil")
|
||||
}
|
||||
s, err := IsValidSecret(fk, "default/demo")
|
||||
if err != nil {
|
||||
t.Errorf("unexpected error: %v", err)
|
||||
}
|
||||
if s == nil {
|
||||
t.Errorf("expected a Secret but retuned nil")
|
||||
}
|
||||
|
||||
fk = testclient.NewSimpleClientset()
|
||||
s, err = IsValidSecret(fk, "default/demo")
|
||||
if err == nil {
|
||||
t.Errorf("expected an error but retuned nil")
|
||||
}
|
||||
if s != nil {
|
||||
t.Errorf("unexpected Secret returned: %v", s)
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue