Add git information during build

This commit is contained in:
Manuel de Brito Fontes 2016-04-06 11:46:06 -03:00
parent cf263c1390
commit 49c3af2c17
11 changed files with 48 additions and 67 deletions

View file

@ -4,8 +4,16 @@ all: push
TAG = 0.5 TAG = 0.5
PREFIX = gcr.io/google_containers/nginx-ingress-controller PREFIX = gcr.io/google_containers/nginx-ingress-controller
REPO_INFO=$(shell git config --get remote.origin.url)
ifndef VERSION
VERSION := git-$(shell git rev-parse --short HEAD)
endif
controller: controller.go clean controller: controller.go clean
CGO_ENABLED=0 GOOS=linux godep go build -a -installsuffix cgo -ldflags '-w' -o nginx-ingress-controller CGO_ENABLED=0 GOOS=linux godep go build -a -installsuffix cgo -ldflags \
"-w -X main.version=${VERSION} -X main.gitRepo=${REPO_INFO}" \
-o nginx-ingress-controller
container: controller container: controller
docker build -t $(PREFIX):$(TAG) . docker build -t $(PREFIX):$(TAG) .

View file

@ -60,7 +60,7 @@ type loadBalancerController struct {
svcLister cache.StoreToServiceLister svcLister cache.StoreToServiceLister
endpLister cache.StoreToEndpointsLister endpLister cache.StoreToEndpointsLister
nginx *nginx.Manager nginx *nginx.Manager
lbInfo *lbInfo podInfo *podInfo
defaultSvc string defaultSvc string
nxgConfigMap string nxgConfigMap string
tcpConfigMap string tcpConfigMap string
@ -84,7 +84,7 @@ type loadBalancerController struct {
// newLoadBalancerController creates a controller for nginx loadbalancer // newLoadBalancerController creates a controller for nginx loadbalancer
func newLoadBalancerController(kubeClient *client.Client, resyncPeriod time.Duration, defaultSvc, func newLoadBalancerController(kubeClient *client.Client, resyncPeriod time.Duration, defaultSvc,
namespace, nxgConfigMapName, tcpConfigMapName, udpConfigMapName string, lbRuntimeInfo *lbInfo) (*loadBalancerController, error) { namespace, nxgConfigMapName, tcpConfigMapName, udpConfigMapName string, runtimeInfo *podInfo) (*loadBalancerController, error) {
eventBroadcaster := record.NewBroadcaster() eventBroadcaster := record.NewBroadcaster()
eventBroadcaster.StartLogging(glog.Infof) eventBroadcaster.StartLogging(glog.Infof)
@ -93,7 +93,7 @@ func newLoadBalancerController(kubeClient *client.Client, resyncPeriod time.Dura
lbc := loadBalancerController{ lbc := loadBalancerController{
client: kubeClient, client: kubeClient,
stopCh: make(chan struct{}), stopCh: make(chan struct{}),
lbInfo: lbRuntimeInfo, podInfo: runtimeInfo,
nginx: nginx.NewManager(kubeClient), nginx: nginx.NewManager(kubeClient),
nxgConfigMap: nxgConfigMapName, nxgConfigMap: nxgConfigMapName,
tcpConfigMap: tcpConfigMapName, tcpConfigMap: tcpConfigMapName,
@ -271,22 +271,22 @@ func (lbc *loadBalancerController) updateIngressStatus(key string) {
lbIPs := ing.Status.LoadBalancer.Ingress lbIPs := ing.Status.LoadBalancer.Ingress
if !lbc.isStatusIPDefined(lbIPs) { if !lbc.isStatusIPDefined(lbIPs) {
glog.Infof("Updating loadbalancer %v/%v with IP %v", ing.Namespace, ing.Name, lbc.lbInfo.Address) glog.Infof("Updating loadbalancer %v/%v with IP %v", ing.Namespace, ing.Name, lbc.podInfo.NodeIP)
currIng.Status.LoadBalancer.Ingress = append(currIng.Status.LoadBalancer.Ingress, api.LoadBalancerIngress{ currIng.Status.LoadBalancer.Ingress = append(currIng.Status.LoadBalancer.Ingress, api.LoadBalancerIngress{
IP: lbc.lbInfo.Address, IP: lbc.podInfo.NodeIP,
}) })
if _, err := ingClient.UpdateStatus(currIng); err != nil { if _, err := ingClient.UpdateStatus(currIng); err != nil {
lbc.recorder.Eventf(currIng, api.EventTypeWarning, "UPDATE", "error: %v", err) lbc.recorder.Eventf(currIng, api.EventTypeWarning, "UPDATE", "error: %v", err)
return return
} }
lbc.recorder.Eventf(currIng, api.EventTypeNormal, "CREATE", "ip: %v", lbc.lbInfo.Address) lbc.recorder.Eventf(currIng, api.EventTypeNormal, "CREATE", "ip: %v", lbc.podInfo.NodeIP)
} }
} }
func (lbc *loadBalancerController) isStatusIPDefined(lbings []api.LoadBalancerIngress) bool { func (lbc *loadBalancerController) isStatusIPDefined(lbings []api.LoadBalancerIngress) bool {
for _, lbing := range lbings { for _, lbing := range lbings {
if lbing.IP == lbc.lbInfo.Address { if lbing.IP == lbc.podInfo.NodeIP {
return true return true
} }
} }
@ -442,7 +442,7 @@ func (lbc *loadBalancerController) getUpstreamServers(data []interface{}) ([]*ng
// default server - no servername. // default server - no servername.
servers[defServerName] = &nginx.Server{ servers[defServerName] = &nginx.Server{
Name: defServerName, Name: defServerName,
Locations: []*nginx.Location{&nginx.Location{ Locations: []*nginx.Location{{
Path: "/", Path: "/",
Upstream: *lbc.getDefaultUpstream(), Upstream: *lbc.getDefaultUpstream(),
}, },
@ -718,10 +718,10 @@ func (lbc *loadBalancerController) removeFromIngress() {
lbIPs := ing.Status.LoadBalancer.Ingress lbIPs := ing.Status.LoadBalancer.Ingress
if len(lbIPs) > 0 && lbc.isStatusIPDefined(lbIPs) { if len(lbIPs) > 0 && lbc.isStatusIPDefined(lbIPs) {
glog.Infof("Updating loadbalancer %v/%v. Removing IP %v", ing.Namespace, ing.Name, lbc.lbInfo.Address) glog.Infof("Updating loadbalancer %v/%v. Removing IP %v", ing.Namespace, ing.Name, lbc.podInfo.NodeIP)
for idx, lbStatus := range currIng.Status.LoadBalancer.Ingress { for idx, lbStatus := range currIng.Status.LoadBalancer.Ingress {
if lbStatus.IP == lbc.lbInfo.Address { if lbStatus.IP == lbc.podInfo.NodeIP {
currIng.Status.LoadBalancer.Ingress = append(currIng.Status.LoadBalancer.Ingress[:idx], currIng.Status.LoadBalancer.Ingress = append(currIng.Status.LoadBalancer.Ingress[:idx],
currIng.Status.LoadBalancer.Ingress[idx+1:]...) currIng.Status.LoadBalancer.Ingress[idx+1:]...)
break break
@ -733,7 +733,7 @@ func (lbc *loadBalancerController) removeFromIngress() {
continue continue
} }
lbc.recorder.Eventf(currIng, api.EventTypeNormal, "DELETE", "ip: %v", lbc.lbInfo.Address) lbc.recorder.Eventf(currIng, api.EventTypeNormal, "DELETE", "ip: %v", lbc.podInfo.NodeIP)
} }
} }
} }

View file

@ -28,10 +28,6 @@ spec:
timeoutSeconds: 5 timeoutSeconds: 5
# use downward API # use downward API
env: env:
- name: POD_IP
valueFrom:
fieldRef:
fieldPath: status.podIP
- name: POD_NAME - name: POD_NAME
valueFrom: valueFrom:
fieldRef: fieldRef:

View file

@ -22,10 +22,6 @@ spec:
timeoutSeconds: 5 timeoutSeconds: 5
# use downward API # use downward API
env: env:
- name: POD_IP
valueFrom:
fieldRef:
fieldPath: status.podIP
- name: POD_NAME - name: POD_NAME
valueFrom: valueFrom:
fieldRef: fieldRef:
@ -40,5 +36,5 @@ spec:
- containerPort: 443 - containerPort: 443
hostPort: 4444 hostPort: 4444
args: args:
- /nginx-ingress-controller-lb - /nginx-ingress-controller
- --default-backend-service=default/default-http-backend - --default-backend-service=default/default-http-backend

View file

@ -28,10 +28,6 @@ spec:
timeoutSeconds: 5 timeoutSeconds: 5
# use downward API # use downward API
env: env:
- name: POD_IP
valueFrom:
fieldRef:
fieldPath: status.podIP
- name: POD_NAME - name: POD_NAME
valueFrom: valueFrom:
fieldRef: fieldRef:

View file

@ -33,10 +33,6 @@ spec:
timeoutSeconds: 5 timeoutSeconds: 5
# use downward API # use downward API
env: env:
- name: POD_IP
valueFrom:
fieldRef:
fieldPath: status.podIP
- name: POD_NAME - name: POD_NAME
valueFrom: valueFrom:
fieldRef: fieldRef:

View file

@ -28,10 +28,6 @@ spec:
timeoutSeconds: 5 timeoutSeconds: 5
# use downward API # use downward API
env: env:
- name: POD_IP
valueFrom:
fieldRef:
fieldPath: status.podIP
- name: POD_NAME - name: POD_NAME
valueFrom: valueFrom:
fieldRef: fieldRef:

View file

@ -28,10 +28,6 @@ spec:
timeoutSeconds: 5 timeoutSeconds: 5
# use downward API # use downward API
env: env:
- name: POD_IP
valueFrom:
fieldRef:
fieldPath: status.podIP
- name: POD_NAME - name: POD_NAME
valueFrom: valueFrom:
fieldRef: fieldRef:

View file

@ -28,10 +28,6 @@ spec:
timeoutSeconds: 5 timeoutSeconds: 5
# use downward API # use downward API
env: env:
- name: POD_IP
valueFrom:
fieldRef:
fieldPath: status.podIP
- name: POD_NAME - name: POD_NAME
valueFrom: valueFrom:
fieldRef: fieldRef:

View file

@ -41,6 +41,10 @@ const (
) )
var ( var (
// value overwritten during build. This can be used to resolve issues.
version = "0.5"
gitRepo = "https://github.com/kubernetes/contrib"
flags = pflag.NewFlagSet("", pflag.ExitOnError) flags = pflag.NewFlagSet("", pflag.ExitOnError)
defaultSvc = flags.String("default-backend-service", "", defaultSvc = flags.String("default-backend-service", "",
@ -82,6 +86,8 @@ func main() {
flags.AddGoFlagSet(flag.CommandLine) flags.AddGoFlagSet(flag.CommandLine)
flags.Parse(os.Args) flags.Parse(os.Args)
glog.Infof("Using build: %v - %v", gitRepo, version)
if *buildCfg { if *buildCfg {
fmt.Printf("Example of ConfigMap to customize NGINX configuration:\n%v", nginx.ConfigMapAsString()) fmt.Printf("Example of ConfigMap to customize NGINX configuration:\n%v", nginx.ConfigMapAsString())
os.Exit(0) os.Exit(0)
@ -96,7 +102,7 @@ func main() {
glog.Fatalf("failed to create client: %v", err) glog.Fatalf("failed to create client: %v", err)
} }
lbInfo, err := getLBDetails(kubeClient) podInfo, err := getPodDetails(kubeClient)
if err != nil { if err != nil {
glog.Fatalf("unexpected error getting runtime information: %v", err) glog.Fatalf("unexpected error getting runtime information: %v", err)
} }
@ -106,7 +112,7 @@ func main() {
glog.Fatalf("no service with name %v found: %v", *defaultSvc, err) glog.Fatalf("no service with name %v found: %v", *defaultSvc, err)
} }
lbc, err := newLoadBalancerController(kubeClient, *resyncPeriod, *defaultSvc, *watchNamespace, *nxgConfigMap, *tcpConfigMapName, *udpConfigMapName, lbInfo) lbc, err := newLoadBalancerController(kubeClient, *resyncPeriod, *defaultSvc, *watchNamespace, *nxgConfigMap, *tcpConfigMapName, *udpConfigMapName, podInfo)
if err != nil { if err != nil {
glog.Fatalf("%v", err) glog.Fatalf("%v", err)
} }
@ -122,18 +128,22 @@ func main() {
} }
} }
// lbInfo contains runtime information about the pod // podInfo contains runtime information about the pod
type lbInfo struct { type podInfo struct {
Podname string PodName string
PodIP string
PodNamespace string PodNamespace string
Address string NodeIP string
} }
func registerHandlers(lbc *loadBalancerController) { func registerHandlers(lbc *loadBalancerController) {
mux := http.NewServeMux() mux := http.NewServeMux()
healthz.InstallHandler(mux, lbc.nginx) healthz.InstallHandler(mux, lbc.nginx)
http.HandleFunc("/build", func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusOK)
fmt.Fprint(w, "build: %v - %v", gitRepo, version)
})
http.HandleFunc("/stop", func(w http.ResponseWriter, r *http.Request) { http.HandleFunc("/stop", func(w http.ResponseWriter, r *http.Request) {
lbc.Stop() lbc.Stop()
}) })

View file

@ -97,27 +97,21 @@ func NewTaskQueue(syncFn func(string)) *taskQueue {
} }
} }
// getLBDetails returns runtime information about the pod (name, IP) and replication // getPodDetails returns runtime information about the pod: name, namespace and IP of the node
// controller or daemonset (namespace and name). func getPodDetails(kubeClient *unversioned.Client) (*podInfo, error) {
// This is required to watch for changes in annotations or configuration (ConfigMap)
func getLBDetails(kubeClient *unversioned.Client) (*lbInfo, error) {
podName := os.Getenv("POD_NAME") podName := os.Getenv("POD_NAME")
podNs := os.Getenv("POD_NAMESPACE") podNs := os.Getenv("POD_NAMESPACE")
err := waitForPodRunning(kubeClient, podNs, podName, time.Millisecond*200, time.Second*30)
if err != nil {
return nil, err
}
pod, _ := kubeClient.Pods(podNs).Get(podName) pod, _ := kubeClient.Pods(podNs).Get(podName)
if pod == nil { if pod == nil {
return nil, fmt.Errorf("Unable to get POD information") return nil, fmt.Errorf("Unable to get POD information")
} }
if pod.Status.Phase != api.PodRunning {
// we wait up to 30 seconds until the pod is running and
// it is possible to get the IP and name of the node
err := waitForPodRunning(kubeClient, podNs, podName, time.Millisecond*200, time.Second*30)
if err != nil {
return nil, err
}
}
node, err := kubeClient.Nodes().Get(pod.Spec.NodeName) node, err := kubeClient.Nodes().Get(pod.Spec.NodeName)
if err != nil { if err != nil {
return nil, err return nil, err
@ -137,13 +131,10 @@ func getLBDetails(kubeClient *unversioned.Client) (*lbInfo, error) {
} }
} }
podIP := os.Getenv("POD_IP") return &podInfo{
PodName: podName,
return &lbInfo{
PodIP: podIP,
Podname: podName,
PodNamespace: podNs, PodNamespace: podNs,
Address: externalIP, NodeIP: externalIP,
}, nil }, nil
} }