github.com/shashidharatd/test-infra@v0.0.0-20171006011030-71304e1ca560/mungegithub/Makefile (about)

     1  all: container
     2  
     3  # Path Prefix: the path the the mungegithub directory.
     4  DIR := $(shell dirname $(abspath $(lastword $(MAKEFILE_LIST))))
     5  CD := cd $(DIR) && 
     6  
     7  DATE := $(shell date +%F)
     8  GIT := $(shell git rev-parse --short HEAD)
     9  
    10  TAG ?= $(DATE)-$(GIT)
    11  
    12  DEFAULTREPO := gcr.io/k8s-testimages
    13  REPO ?= $(DEFAULTREPO)
    14  APP ?= submit-queue
    15  CONTAINER := $(REPO)/$(APP):$(TAG)
    16  
    17  KUBECONFIG ?= $(HOME)/.kube/config
    18  
    19  TARGET ?= kubernetes
    20  
    21  TOKEN ?= "./token"
    22  token64=$(shell base64 $(TOKEN))
    23  
    24  HOOKSECRET ?= "./hook-secret"
    25  hooksecret64=$(shell base64 $(HOOKSECRET))
    26  
    27  READONLY ?= true
    28  
    29  CLUSTER ?= mungegithub
    30  PROJECT ?= k8s-mungegithub
    31  ZONE ?= us-central1-b
    32  
    33  get-cluster-credentials:
    34  	gcloud container clusters get-credentials "$(CLUSTER)" --project="$(PROJECT)" --zone="$(ZONE)"
    35  
    36  # just build the binary
    37  mungegithub build:
    38  	$(CD) GOBIN=$(DIR) CGO_ENABLED=0 GOOS=linux go install -installsuffix cgo -ldflags '-w'
    39  
    40  test: mungegithub
    41  	# in case of error ignore all lines of 'getsockopt: connection refused' these are background go threads that don't matter
    42  	$(CD) CGO_ENABLED=0 GOOS=linux go test $(shell go list ./... | grep -v 'vendor/') | grep -v 'getsockopt: connection refused'
    43  
    44  # build the container with the binary
    45  container: mungegithub
    46  	$(CD) docker build --pull -t $(CONTAINER) -f Dockerfile-$(APP) .
    47  
    48  # push the container
    49  push: container
    50  ifneq (,$(findstring gcr.io,$(REPO)))
    51  	gcloud docker -- push $(CONTAINER)
    52  else
    53  	docker push $(CONTAINER)
    54  endif
    55  
    56  # Launch the container on a cluster (with --dry-run).
    57  # The cluster will likely need a service to get access to the web interface (see service.yaml)
    58  # The cluster will need a github oauth token (the secret target makes that easy to create)
    59  deploy: push deployment get-cluster-credentials
    60  	# Deploy the new deployment
    61  	$(CD) kubectl --kubeconfig=$(KUBECONFIG) apply -f $(APP)/local.deployment.yaml --record
    62  
    63  # A new configuration is pushed by using the configmap specified using $(TARGET) and $(APP).
    64  push_config: get-cluster-credentials
    65  	# pushes a new configmap.
    66  	$(CD) kubectl --kubeconfig=$(KUBECONFIG) create configmap $(TARGET)-$(APP)-config --from-file=config=$(APP)/deployment/$(TARGET)/configmap.yaml --dry-run -o yaml | kubectl apply -f -
    67  
    68  # Creates a service resource spec for the specified target and app.
    69  service:
    70  ifneq ("", "$(wildcard $(APP)/service.yaml)")
    71  	# updating service.yaml by replacing @@ with $(TARGET).
    72  	$(CD) sed -e 's!@@!$(TARGET)!g' $(APP)/service.yaml > $(APP)/local.service.yaml
    73  endif
    74  
    75  # Pushes a service resource spec.
    76  push_service: service get-cluster-credentials
    77  ifneq ("", "$(wildcard $(APP)/service.yaml)")
    78  	# Applying service.yaml.
    79  	$(CD) kubectl --kubeconfig=$(KUBECONFIG) apply -f $(APP)/local.service.yaml
    80  endif
    81  
    82  # updates the deployment.yaml with current build information and sets it to --dry-run
    83  deployment:
    84  	# update the deployment.yaml with the current date and git hash
    85  	$(CD) sed -e 's|[[:digit:]]\{4\}-[[:digit:]]\{2\}-[[:digit:]]\{2\}-[[:xdigit:]]\+|$(TAG)|g' $(APP)/deployment.yaml > $(APP)/local.deployment.yaml
    86  	# update the deployment.yaml with the current repo (if not gcr.io
    87  	$(CD) sed -i -e 's|gcr.io/k8s-testimages|$(REPO)|g' $(APP)/local.deployment.yaml
    88  ifeq ($(READONLY),false)
    89  	# update the deployment.yaml with --dry-run=false
    90  	$(CD) sed -i -e 's!^\([[:space:]]\+\)- --dry-run=true!\1- --dry-run=false!g' $(APP)/local.deployment.yaml
    91  endif
    92  	# update the deployment.yaml with label "readonly: true"
    93  	$(CD) sed -i -e 's!^\([[:space:]]\+\)app: $(APP)!\1app: $(APP)\n\1readonly: "$(READONLY)"!g' $(APP)/local.deployment.yaml
    94  	# String-replacement of @@ by $(TARGET) in the deployment file.
    95  	$(CD) sed -i -e 's!@@!$(TARGET)!g' $(APP)/local.deployment.yaml
    96  
    97  # simple transformation of a github oauth secret file to a kubernetes secret
    98  secret:
    99  	# Updating secret.yaml and outputing to $(APP)/local.secret.yaml.
   100  	$(CD) sed -e 's|<TOKEN>|$(token64)|' $(APP)/secret.yaml > $(APP)/local.secret.yaml
   101  	$(CD) sed -i -e 's|<HOOKSECRET>|$(hooksecret64)|' $(APP)/local.secret.yaml
   102  	# String-replacement of @@ by $(TARGET) in the secrets file.
   103  	$(CD) sed -i -e 's!@@!$(TARGET)!g' $(APP)/local.secret.yaml
   104  
   105  # Generate and deploy the token secret.
   106  push_secret: secret get-cluster-credentials
   107  	# Appling local.secret.yaml.
   108  	$(CD) kubectl --kubeconfig=$(KUBECONFIG) apply -f $(APP)/local.secret.yaml
   109  
   110  # Generate resource spec files for the persistent volume and claim.
   111  volume:
   112  ifneq ("", "$(and $(wildcard $(APP)/pv.yaml), $(wildcard $(APP)/pvc.yaml))")
   113  	# Updating pv.yaml and pvc.yaml by replacing @@ with TARGET.
   114  	$(CD) sed -e 's!@@!$(TARGET)!g' $(APP)/pv.yaml > $(APP)/local.pv.yaml
   115  	$(CD) sed -e 's!@@!$(TARGET)!g' $(APP)/pvc.yaml > $(APP)/local.pvc.yaml
   116  endif
   117  
   118  # Create the persistent volume and persistent volume claim.
   119  push_volume: volume get-cluster-credentials
   120  ifneq ("", "$(and $(wildcard $(APP)/pv.yaml), $(wildcard $(APP)/pvc.yaml))")
   121  	# Creating persistent volume and persistent volume claim.
   122  	$(CD) kubectl --kubeconfig=$(KUBECONFIG) create -f $(APP)/local.pv.yaml
   123  	# Sleeping for 1s to allow persistent volume to be created.
   124  	@sleep 1
   125  	$(CD) kubectl --kubeconfig=$(KUBECONFIG) create -f $(APP)/local.pvc.yaml
   126  	# Sleeping for 4s to allow claim to bind to volume.
   127  	@sleep 4
   128  endif
   129  
   130  # Deploys after completeing first time setup such as creating the volume and volume claim, and creating the token secret, and service.
   131  first_time_deploy: push_volume push_secret push_service push_config deploy
   132  
   133  clean:
   134  	$(CD) rm -f mungegithub $(APP)/local.deployment.yaml $(APP)/local.secret.yaml
   135  
   136  # pull down current public queue state, and run UI based off that data
   137  ui-stub:
   138  	@$(CD) /bin/bash -c "wget -q -r -nH -P ./submit-queue/www http://submit-queue.k8s.io/{prs,github-e2e-queue,history,sq-stats,stats,users,health,google-internal-ci,priority-info,merge-info}; \
   139  	pushd ./submit-queue/www; \
   140  	python -m SimpleHTTPServer;"
   141  
   142  help:
   143  	@echo "ENVIRONMENT VARS:"
   144  	@echo " REPO=       repository for the docker image being build. Default: $(REPO)"
   145  	@echo " TOKEN=      file with github oauth token, needed in secret. Default: $(TOKEN)"
   146  	@echo " KUBECONFIG= kubeconfig file for deployment. Default: $(KUBECONFIG)"
   147  	@echo " READONLY=   should the container actually mute github objects or just do everything else. Default: $(READONLY)"
   148  	@echo " APP=        which application you are trying to deploy. cherrypick or submit-queue. Default: $(APP)"
   149  	@echo " TARGET=     which repository this should run against. ex: contrib, test-infra, kubernetes. Default: $(TARGET)"
   150  	@echo ""
   151  	@echo "TARGETS:"
   152  	@echo " all:              runs 'container'"
   153  	@echo " mungegithub:      builds the binary"
   154  	@echo " container:        builds the binary and creates a container with the binary"
   155  	@echo " push:             pushes the container to the registry"
   156  	@echo " deploy:           launches/updates the app on a kubernetes cluster"
   157  	@echo " push_config:      applys changes to configMaps for the app/target. $(APP)/deployment/$(TARGET)/configmap.yaml"
   158  	@echo " deployment:       updates $(APP)/deployment.yaml and places results in $(APP)/local.deployment.yaml"
   159  	@echo " ui-stub:          grab upstream submit-queue data and launch the web ui on port 8000"
   160  	@echo " secret:           updates $(APP)/secret.yaml with TOKEN and HOOKSECRET and creates $(APP)/local.secret.yaml"
   161  	@echo " push_secret:      creates $(APP)/local.secret.yaml using the secret make target and applies it to the cluster."
   162  	@echo " service:           updates $(APP)/service.yaml with TARGET and creates $(APP)/local.service.yaml"
   163  	@echo " push_service:      creates $(APP)/local.service.yaml using the service make target and applies it to the cluster."
   164  	@echo " clean:            deletes the binary and local files (does not delete old containers)"
   165  
   166  
   167  .PHONY: all mungegithub test container push push_config service push_service deployment secret push_secret volume push_volume first_time_deploy clean ui-stub help