Skip to content

Commit

Permalink
add a Makefile
Browse files Browse the repository at this point in the history
Adds the following human-facing targets:

- merge: builds and installs the merge binary
- config: creates a merged config using the merge binary
- test: tests the merged config using go test
- peribolos: clones a local copy of kubernetes/test-infra and installs
  the peribolos binary
- deploy: runs periobolos to deploy the merged config if tests pass
- clean: remove all intermediary configs and binaries

Overrideable env vars:

- GITHUB_TOKEN_PATH: value for --github-token-path passed to peribolos
- TEST_INFRA_PATH: set to location of a pre-existing clone of
  kubernetes/test-infra to avoid cloning a temporary copy

One way this differs from bazel is that we're not necessarily getting
the latest version of test-infra each time we run `make deploy` but I
don't foresee peribolos changing that frequently, and a `make clean` is
enough to get us back to "latest" state.
  • Loading branch information
spiffxp committed Sep 24, 2021
1 parent 916cbbf commit 4019523
Show file tree
Hide file tree
Showing 3 changed files with 113 additions and 5 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@
/bazel-*
.DS_Store
.idea/
_output/
86 changes: 86 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
# Copyright 2021 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.

SHELL := /bin/bash

# available for override
GITHUB_TOKEN_PATH ?= /etc/github-token/token
TEST_INFRA_PATH ?= $(OUTPUT_DIR)/tmp/test-infra

# intentionally hardcoded list to ensure it's high friction to remove someone
ADMINS = cblecker fejta idvoretskyi mrbobbytables nikhita spiffxp
ORGS = $(shell find ./config -type d -mindepth 1 -maxdepth 1 | cut -d/ -f3)

# use absolute path to ./_output, which is .gitignored
OUTPUT_DIR := $(shell pwd)/_output
OUTPUT_BIN_DIR := $(OUTPUT_DIR)/bin

MERGE_CMD := $(OUTPUT_BIN_DIR)/merge
PERIBOLOS_CMD := $(OUTPUT_BIN_DIR)/peribolos

MERGED_CONFIG := $(OUTPUT_DIR)/gen-config.yaml

# convenience targets for humans
.PHONY: clean
clean:
rm -rf $(OUTPUT_DIR)

.PHONY: build
build:
go build ./...

.PHONY: merge
merge: $(MERGE_CMD)

.PHONY: config
config: $(MERGED_CONFIG)

.PHONY: peribolos
peribolos: $(PERIBOLOS_CMD)

.PHONY: test
test: config
go test ./... --config=$(MERGED_CONFIG)

.PHONY: deploy # --confirm
deploy: config test peribolos
$(PERIBOLOS_CMD) \
--config-path $(MERGED_CONFIG) \
--fix-org \
--fix-org-members \
--fix-teams \
--fix-team-members \
--github-token-path=$(GITHUB_TOKEN_PATH) \
$(patsubst %, --required-admins=%, $(ADMINS)) \
$@

# actual targets that only get built if they don't already exist
$(MERGE_CMD):
mkdir -p "$(OUTPUT_BIN_DIR)"
go build -v -o "$(OUTPUT_BIN_DIR)" ./cmd/merge

$(MERGED_CONFIG): $(MERGE_CMD) config/**/*.yaml
mkdir -p "$(OUTPUT_DIR)"
$(MERGE_CMD) \
--merge-teams \
$(shell for o in $(ORGS); do echo "--org-part=$$o=config/$$o/org.yaml"; done) \
> $(MERGED_CONFIG)

$(TEST_INFRA_PATH):
mkdir -p $(TEST_INFRA_PATH)
git clone --depth=1 https://github.com/kubernetes/test-infra $(TEST_INFRA_PATH)

$(PERIBOLOS_CMD): $(TEST_INFRA_PATH)
cd $(TEST_INFRA_PATH) && \
go build -v -o $(PERIBOLOS_CMD) ./prow/cmd/peribolos
31 changes: 26 additions & 5 deletions config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ limitations under the License.
package config

import (
"flag"
"fmt"
"io/ioutil"
"os"
Expand All @@ -31,6 +32,31 @@ import (
"github.com/ghodss/yaml"
)

var configPath = flag.String("config", "config.yaml", "Path to generated config")

var cfg org.FullConfig

func TestMain(m *testing.M) {
flag.Parse()
if *configPath == "" {
fmt.Println("--config must be set")
os.Exit(1)
}

raw, err := ioutil.ReadFile(*configPath)
if err != nil {
fmt.Printf("cannot read generated config.yaml from %s: %v\n", *configPath, err)
os.Exit(1)
}

if err := yaml.Unmarshal(raw, &cfg); err != nil {
fmt.Printf("cannot unmarshal generated config.yaml from %s: %v\n", *configPath, err)
os.Exit(1)
}

os.Exit(m.Run())
}

type owners struct {
Reviewers []string `json:"reviewers,omitempty"`
Approvers []string `json:"approvers"`
Expand Down Expand Up @@ -191,11 +217,6 @@ func testOrg(targetDir string, t *testing.T) {
}

func TestAllOrgs(t *testing.T) {
raw, err := ioutil.ReadFile("config.yaml")
var cfg org.FullConfig
if err := yaml.Unmarshal(raw, &cfg); err != nil {
t.Fatalf("cannot read config.yaml from //config:gen-config.yaml: %v", err)
}
f, err := os.Open(".")
if err != nil {
t.Fatalf("cannot read config: %v", err)
Expand Down

0 comments on commit 4019523

Please sign in to comment.