github.com/abayer/test-infra@v0.0.5/mungegithub/Makefile (about)

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