
Automatic merge from submit-queue [nginx-ingress-controller]: Use ClientConfig to configure connection fixes #1459 Running with `docker run`: ``` core@localhost ~ $ docker run -it aledbf/nginx-third-party:0.31 bash root@f6a96f46eab0:/# export KUBERNETES_MASTER=http://172.17.4.99:8080 root@f6a96f46eab0:/# /nginx-ingress-controller --default-backend-service=default/nginx-errors I0802 14:44:58.604384 7 main.go:94] Using build: https://github.com/aledbf/contrib - git-5b9146a W0802 14:44:58.605282 7 main.go:118] unexpected error getting runtime information: unable to get POD information (missing POD_NAME or POD_NAMESPACE environment variable) I0802 14:44:58.607270 7 main.go:123] Validated default/nginx-errors as the default backend W0802 14:44:58.611322 7 ssl.go:132] no file dhparam.pem found in secrets I0802 14:44:58.615637 7 controller.go:1128] starting NGINX loadbalancer controller I0802 14:44:58.615902 7 command.go:35] Starting NGINX process... ``` Running inside in a cluster: ``` I0802 14:47:50.254736 1 main.go:94] Using build: https://github.com/aledbf/contrib - git-5b9146a I0802 14:47:50.254920 1 merged_client_builder.go:103] No kubeconfig could be created, falling back to service account. I0802 14:47:50.343440 1 main.go:123] Validated default/nginx-errors as the default backend W0802 14:47:50.343677 1 ssl.go:132] no file dhparam.pem found in secrets I0802 14:47:50.347322 1 controller.go:1128] starting NGINX loadbalancer controller I0802 14:47:50.347870 1 command.go:35] Starting NGINX process... ``` <!-- Reviewable:start --> --- This change is [<img src="https://reviewable.kubernetes.io/review_button.svg" height="34" align="absmiddle" alt="Reviewable"/>](https://reviewable.kubernetes.io/reviews/kubernetes/contrib/1467) <!-- Reviewable:end -->
301 lines
7.5 KiB
Go
301 lines
7.5 KiB
Go
/*
|
|
Copyright 2015 The Kubernetes Authors All rights reserved.
|
|
|
|
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 main
|
|
|
|
import (
|
|
"fmt"
|
|
"io/ioutil"
|
|
"os"
|
|
"strings"
|
|
"time"
|
|
|
|
"github.com/golang/glog"
|
|
|
|
"k8s.io/kubernetes/pkg/api"
|
|
apierrs "k8s.io/kubernetes/pkg/api/errors"
|
|
"k8s.io/kubernetes/pkg/apis/extensions"
|
|
"k8s.io/kubernetes/pkg/client/cache"
|
|
"k8s.io/kubernetes/pkg/client/unversioned"
|
|
"k8s.io/kubernetes/pkg/util/wait"
|
|
"k8s.io/kubernetes/pkg/util/workqueue"
|
|
)
|
|
|
|
// StoreToIngressLister makes a Store that lists Ingress.
|
|
type StoreToIngressLister struct {
|
|
cache.Store
|
|
}
|
|
|
|
// StoreToSecretsLister makes a Store that lists Secrets.
|
|
type StoreToSecretsLister struct {
|
|
cache.Store
|
|
}
|
|
|
|
// StoreToConfigmapLister makes a Store that lists Configmap.
|
|
type StoreToConfigmapLister struct {
|
|
cache.Store
|
|
}
|
|
|
|
// taskQueue manages a work queue through an independent worker that
|
|
// invokes the given sync function for every work item inserted.
|
|
type taskQueue struct {
|
|
// queue is the work queue the worker polls
|
|
queue workqueue.RateLimitingInterface
|
|
// sync is called for each item in the queue
|
|
sync func(string) error
|
|
// workerDone is closed when the worker exits
|
|
workerDone chan struct{}
|
|
}
|
|
|
|
func (t *taskQueue) run(period time.Duration, stopCh <-chan struct{}) {
|
|
wait.Until(t.worker, period, stopCh)
|
|
}
|
|
|
|
// enqueue enqueues ns/name of the given api object in the task queue.
|
|
func (t *taskQueue) enqueue(obj interface{}) {
|
|
key, err := keyFunc(obj)
|
|
if err != nil {
|
|
glog.Infof("could not get key for object %+v: %v", obj, err)
|
|
return
|
|
}
|
|
t.queue.Add(key)
|
|
}
|
|
|
|
func (t *taskQueue) requeue(key string) {
|
|
t.queue.AddRateLimited(key)
|
|
}
|
|
|
|
// worker processes work in the queue through sync.
|
|
func (t *taskQueue) worker() {
|
|
for {
|
|
key, quit := t.queue.Get()
|
|
if quit {
|
|
close(t.workerDone)
|
|
return
|
|
}
|
|
glog.V(3).Infof("syncing %v", key)
|
|
if err := t.sync(key.(string)); err != nil {
|
|
glog.Warningf("requeuing %v, err %v", key, err)
|
|
t.requeue(key.(string))
|
|
} else {
|
|
t.queue.Forget(key)
|
|
}
|
|
|
|
t.queue.Done(key)
|
|
}
|
|
}
|
|
|
|
// shutdown shuts down the work queue and waits for the worker to ACK
|
|
func (t *taskQueue) shutdown() {
|
|
t.queue.ShutDown()
|
|
<-t.workerDone
|
|
}
|
|
|
|
// NewTaskQueue creates a new task queue with the given sync function.
|
|
// The sync function is called for every element inserted into the queue.
|
|
func NewTaskQueue(syncFn func(string) error) *taskQueue {
|
|
return &taskQueue{
|
|
queue: workqueue.NewRateLimitingQueue(workqueue.DefaultControllerRateLimiter()),
|
|
sync: syncFn,
|
|
workerDone: make(chan struct{}),
|
|
}
|
|
}
|
|
|
|
// getPodDetails returns runtime information about the pod: name, namespace and IP of the node
|
|
func getPodDetails(kubeClient *unversioned.Client) (*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")
|
|
}
|
|
|
|
err := waitForPodRunning(kubeClient, podNs, podName, time.Millisecond*200, time.Second*30)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
pod, _ := kubeClient.Pods(podNs).Get(podName)
|
|
if pod == nil {
|
|
return nil, fmt.Errorf("unable to get POD information")
|
|
}
|
|
|
|
node, err := kubeClient.Nodes().Get(pod.Spec.NodeName)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
var externalIP string
|
|
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 &podInfo{
|
|
PodName: podName,
|
|
PodNamespace: podNs,
|
|
NodeIP: externalIP,
|
|
}, nil
|
|
}
|
|
|
|
func isValidService(kubeClient *unversioned.Client, name string) error {
|
|
if name == "" {
|
|
return fmt.Errorf("empty string is not a valid service name")
|
|
}
|
|
|
|
parts := strings.Split(name, "/")
|
|
if len(parts) != 2 {
|
|
return fmt.Errorf("invalid name format (namespace/name) in service '%v'", name)
|
|
}
|
|
|
|
_, err := kubeClient.Services(parts[0]).Get(parts[1])
|
|
return err
|
|
}
|
|
|
|
func isHostValid(host string, cns []string) bool {
|
|
for _, cn := range cns {
|
|
if matchHostnames(cn, host) {
|
|
return true
|
|
}
|
|
}
|
|
|
|
return false
|
|
}
|
|
|
|
func matchHostnames(pattern, host string) bool {
|
|
host = strings.TrimSuffix(host, ".")
|
|
pattern = strings.TrimSuffix(pattern, ".")
|
|
|
|
if len(pattern) == 0 || len(host) == 0 {
|
|
return false
|
|
}
|
|
|
|
patternParts := strings.Split(pattern, ".")
|
|
hostParts := strings.Split(host, ".")
|
|
|
|
if len(patternParts) != len(hostParts) {
|
|
return false
|
|
}
|
|
|
|
for i, patternPart := range patternParts {
|
|
if i == 0 && patternPart == "*" {
|
|
continue
|
|
}
|
|
if patternPart != hostParts[i] {
|
|
return false
|
|
}
|
|
}
|
|
|
|
return true
|
|
}
|
|
|
|
func parseNsName(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
|
|
}
|
|
|
|
func waitForPodRunning(kubeClient *unversioned.Client, ns, podName string, interval, timeout time.Duration) error {
|
|
condition := func(pod *api.Pod) (bool, error) {
|
|
if pod.Status.Phase == api.PodRunning {
|
|
return true, nil
|
|
}
|
|
return false, nil
|
|
}
|
|
|
|
return waitForPodCondition(kubeClient, ns, podName, condition, interval, timeout)
|
|
}
|
|
|
|
// waitForPodCondition waits for a pod in state defined by a condition (func)
|
|
func waitForPodCondition(kubeClient *unversioned.Client, ns, podName string, condition func(pod *api.Pod) (bool, error),
|
|
interval, timeout time.Duration) error {
|
|
return wait.PollImmediate(interval, timeout, func() (bool, error) {
|
|
pod, err := kubeClient.Pods(ns).Get(podName)
|
|
if err != nil {
|
|
if apierrs.IsNotFound(err) {
|
|
return false, nil
|
|
}
|
|
}
|
|
|
|
done, err := condition(pod)
|
|
if err != nil {
|
|
return false, err
|
|
}
|
|
if done {
|
|
return true, nil
|
|
}
|
|
|
|
return false, nil
|
|
})
|
|
}
|
|
|
|
// ingAnnotations represents Ingress annotations.
|
|
type ingAnnotations map[string]string
|
|
|
|
const (
|
|
// ingressClassKey picks a specific "class" for the Ingress. The controller
|
|
// only processes Ingresses with this annotation either unset, or set
|
|
// to either nginxIngressClass or the empty string.
|
|
ingressClassKey = "kubernetes.io/ingress.class"
|
|
nginxIngressClass = "nginx"
|
|
)
|
|
|
|
func (ing ingAnnotations) ingressClass() string {
|
|
val, ok := ing[ingressClassKey]
|
|
if !ok {
|
|
return ""
|
|
}
|
|
return val
|
|
}
|
|
|
|
// isNGINXIngress returns true if the given Ingress either doesn't specify the
|
|
// ingress.class annotation, or it's set to "nginx".
|
|
func isNGINXIngress(ing *extensions.Ingress) bool {
|
|
class := ingAnnotations(ing.ObjectMeta.Annotations).ingressClass()
|
|
return class == "" || class == nginxIngressClass
|
|
}
|
|
|
|
const (
|
|
snakeOilPem = "/etc/ssl/certs/ssl-cert-snakeoil.pem"
|
|
snakeOilKey = "/etc/ssl/private/ssl-cert-snakeoil.key"
|
|
)
|
|
|
|
// getFakeSSLCert returns the snake oil ssl certificate created by the command
|
|
// make-ssl-cert generate-default-snakeoil --force-overwrite
|
|
func getFakeSSLCert() (string, string) {
|
|
cert, err := ioutil.ReadFile(snakeOilPem)
|
|
if err != nil {
|
|
return "", ""
|
|
}
|
|
|
|
key, err := ioutil.ReadFile(snakeOilKey)
|
|
if err != nil {
|
|
return "", ""
|
|
}
|
|
|
|
return string(cert), string(key)
|
|
}
|