diff --git a/.dockerignore b/.dockerignore deleted file mode 100644 index 13db39de..00000000 --- a/.dockerignore +++ /dev/null @@ -1,3 +0,0 @@ -** - -!/bin/linux diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index c014e210..e2ca187d 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -2,7 +2,7 @@ name: CI on: push: - branches: [ main ] + branches: [ master ] tags: - "*" pull_request: @@ -21,7 +21,7 @@ jobs: with: go-version: 1.23 - name: Build - run: make + run: make build docs: runs-on: ubuntu-latest @@ -37,41 +37,6 @@ jobs: # make gen-docs # git diff --exit-code - linux: - runs-on: ubuntu-latest - steps: - - uses: actions/checkout@v3 - - name: Set up Go - uses: actions/setup-go@v4 - with: - go-version: 1.23 - - name: Build kg and kgctl for all Linux Architectures - run: make all-build - - darwin: - runs-on: ubuntu-latest - steps: - - uses: actions/checkout@v3 - - name: Set up Go - uses: actions/setup-go@v4 - with: - go-version: 1.23 - - name: Build kgctl for Darwin amd64 - run: make OS=darwin ARCH=amd64 - - name: Build kgctl for Darwin arm64 - run: make OS=darwin ARCH=arm64 - - windows: - runs-on: ubuntu-latest - steps: - - uses: actions/checkout@v3 - - name: Set up Go - uses: actions/setup-go@v4 - with: - go-version: 1.23 - - name: Build kgctl for Windows - run: make OS=windows - unit: runs-on: ubuntu-latest steps: @@ -94,25 +59,15 @@ jobs: - name: Run e2e Tests run: make e2e - lint: - runs-on: ubuntu-latest - steps: - - uses: actions/checkout@v3 - - name: Set up Go - uses: actions/setup-go@v4 - with: - go-version: 1.23 - - name: Lint Code - run: make lint - - container: - runs-on: ubuntu-latest - steps: - - uses: actions/checkout@v3 - - name: Set up Go - uses: actions/setup-go@v4 - with: - go-version: 1.23 - - name: Container - run: make container + # https://github.com/castai/kilo/issues/12 + #lint: + # runs-on: ubuntu-latest + # steps: + # - uses: actions/checkout@v3 + # - name: Set up Go + # uses: actions/setup-go@v4 + # with: + # go-version: 1.23 + # - name: Lint Code + # run: make lint diff --git a/.github/workflows/goreleaser.yml b/.github/workflows/goreleaser.yml new file mode 100644 index 00000000..7f086172 --- /dev/null +++ b/.github/workflows/goreleaser.yml @@ -0,0 +1,47 @@ +name: goreleaser + +on: + pull_request: + paths: + - .github/workflows/goreleaser.yml + - .goreleaser.yaml + push: + tags: + - 'v*' + +permissions: + contents: write + +jobs: + goreleaser: + runs-on: ubuntu-latest + steps: + - name: Checkout + uses: actions/checkout@v3 + with: + fetch-depth: 0 + - uses: actions/setup-go@v4 + with: + go-version: v1.23.0 + - name: SET LDFLAGS + run: echo "LDFLAGS=$(make ldflags)" >> $GITHUB_ENV + - name: Delete non-semver tags + run: 'git tag -d $(git tag -l | grep -v "^v")' + - name: Run GoReleaser on tag + if: github.event_name != 'pull_request' + uses: goreleaser/goreleaser-action@v4 + with: + distribution: goreleaser + version: latest + args: release --timeout 60m + env: + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} + - name: Run GoReleaser on pull request + if: github.event_name == 'pull_request' + uses: goreleaser/goreleaser-action@v4 + with: + distribution: goreleaser + version: latest + args: release --timeout 60m --snapshot + env: + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} \ No newline at end of file diff --git a/.github/workflows/images.yaml b/.github/workflows/images.yaml new file mode 100644 index 00000000..d16ea04e --- /dev/null +++ b/.github/workflows/images.yaml @@ -0,0 +1,110 @@ +name: Create and publish a Container image + +on: + push: + branches: + - master # Trigger the workflow on push to the master branch + tags: + - 'v*' # Trigger the workflow on version tags like v1.0.0, v2.0.0, etc. + pull_request: + branches: + - master # Trigger workflow when a PR targets the master branch + +permissions: + contents: read + packages: write + id-token: write + +# Defines two custom environment variables for the workflow. These are used for the Container registry domain, and a name for the Docker image that this workflow builds. +env: + REGISTRY: ghcr.io + IMAGE_NAME: ${{ github.repository }} + +jobs: + build-tagged-image: + runs-on: ubuntu-latest + if: github.event_name == 'push' && startsWith(github.ref, 'refs/tags/v') + steps: + - name: Checkout repository + uses: actions/checkout@v4 + + - name: Log in to the Container registry + uses: docker/login-action@65b78e6e13532edd9afa3aa52ac7964289d1a9c1 + with: + registry: ${{ env.REGISTRY }} + username: ${{ github.actor }} + password: ${{ secrets.GITHUB_TOKEN }} + + - name: Extract metadata (tags, labels) for Docker + id: meta + uses: docker/metadata-action@9ec57ed1fcdbf14dcef7dfbe97b2010124a938b7 + with: + images: ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }} + + - name: Build and push versioned Docker image + uses: docker/build-push-action@f2a1d5e99d037542a71f64918e516c093c6f3fc4 + with: + context: . + push: true + tags: ${{ steps.meta.outputs.tags }} # Version tag from the metadata action + labels: ${{ steps.meta.outputs.labels }} + + build-latest-image: + runs-on: ubuntu-latest + if: github.event_name == 'push' && github.ref == 'refs/heads/master' + steps: + - name: Checkout repository + uses: actions/checkout@v4 + + - name: Log in to the Container registry + uses: docker/login-action@65b78e6e13532edd9afa3aa52ac7964289d1a9c1 + with: + registry: ${{ env.REGISTRY }} + username: ${{ github.actor }} + password: ${{ secrets.GITHUB_TOKEN }} + + - name: Extract metadata (tags, labels) for Docker + id: meta + uses: docker/metadata-action@9ec57ed1fcdbf14dcef7dfbe97b2010124a938b7 + with: + images: ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }} + + - name: Build and push latest Docker image + uses: docker/build-push-action@f2a1d5e99d037542a71f64918e516c093c6f3fc4 + with: + context: . + push: true + tags: | + ${{ steps.meta.outputs.tags }} + ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}:latest + labels: ${{ steps.meta.outputs.labels }} + + build-pr-image: + runs-on: ubuntu-latest + if: github.event_name == 'pull_request' + steps: + - name: Checkout repository + uses: actions/checkout@v4 + + - name: Log in to the Container registry + uses: docker/login-action@65b78e6e13532edd9afa3aa52ac7964289d1a9c1 + with: + registry: ${{ env.REGISTRY }} + username: ${{ github.actor }} + password: ${{ secrets.GITHUB_TOKEN }} + + - name: Extract metadata (tags, labels) for Docker + id: meta + uses: docker/metadata-action@9ec57ed1fcdbf14dcef7dfbe97b2010124a938b7 + with: + images: ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }} + + - name: Build and push PR-specific Docker image + uses: docker/build-push-action@f2a1d5e99d037542a71f64918e516c093c6f3fc4 + with: + context: . + push: true + tags: | + ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}:pr-${{ github.event.pull_request.number }} + ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}:${{ github.sha }} # Short commit hash + labels: ${{ steps.meta.outputs.labels }} diff --git a/.github/workflows/release.yaml b/.github/workflows/release.yaml deleted file mode 100644 index 61ffabf8..00000000 --- a/.github/workflows/release.yaml +++ /dev/null @@ -1,21 +0,0 @@ -on: - release: - types: [created] -name: Handle Release -jobs: - kgctl: - runs-on: ubuntu-latest - steps: - - uses: actions/checkout@v3 - - name: Set up Go - uses: actions/setup-go@v4 - with: - go-version: 1.23 - - name: Build kgctl Binaries to Be Released - run: make release - - name: Publish Release - uses: skx/github-action-publish-binaries@master - env: - GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} - with: - args: 'bin/release/kgctl-*' diff --git a/.gitignore b/.gitignore index 50b4ba67..653f1014 100644 --- a/.gitignore +++ b/.gitignore @@ -5,9 +5,5 @@ bin/ tmp/ e2e/kind.yaml* -hack/tools/controller-gen -hack/tools/controller-gen-v0.16.1 -hack/tools/openshift-goimports -hack/tools/openshift-goimports-c72f1dc2e3aacfa00aece3391d938c9bc734e791 -hack/tools/yaml-patch -hack/tools/yaml-patch-v0.0.11 +hack/tools/ +dist/ \ No newline at end of file diff --git a/.goreleaser.yaml b/.goreleaser.yaml new file mode 100644 index 00000000..56d9069e --- /dev/null +++ b/.goreleaser.yaml @@ -0,0 +1,38 @@ +project_name: kilo-castai +version: 2 +env: + - CGO_ENABLED=0 + - LDFLAGS={{ .Env.LDFLAGS }} + +builds: + - id: kilo-kg + dir: cmd/kg + binary: kg + ldflags: "{{ .Env.LDFLAGS }}" + goos: + - linux + goarch: + - amd64 + - arm64 + - id: kilo-kgctl + dir: cmd/kgctl + binary: kilo-kgctl + ldflags: "{{ .Env.LDFLAGS }}" + goos: + - linux + goarch: + - amd64 + - arm64 + +archives: + - id: kilo-plugin + builds: + - kilo-kgctl + - kilo-kg + name_template: "kilo-plugin_{{ .Version }}_{{ .Os }}_{{ .Arch }}" + +release: + prerelease: auto + mode: keep-existing + github: + owner: castai diff --git a/Dockerfile b/Dockerfile index 028c9d29..da8675f0 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,20 +1,54 @@ -ARG FROM=alpine -FROM $FROM AS cni +# BUILDER PHASE +FROM --platform=${BUILDPLATFORM} docker.io/golang:1.23.0 AS builder +WORKDIR /workspace + +# Install dependencies. +RUN apt-get update && apt-get install -y jq && mkdir bin + +# Run this with docker build --build-arg goproxy=$(go env GOPROXY) to override the goproxy +ARG goproxy=https://proxy.golang.org +ENV GOPROXY=$goproxy + +# Copy the Go Modules manifests +COPY go.mod go.mod +COPY go.sum go.sum +USER 0 + +# Cache deps before building and copying source so that we don't need to re-download as much +# and so that source changes don't invalidate our downloaded layer +RUN --mount=type=cache,target=/go/pkg/mod \ + go mod download + +# Copy the sources +COPY ./ ./ + +ARG TARGETOS +ARG TARGETARCH + +RUN --mount=type=cache,target=/root/.cache/go-build \ + --mount=type=cache,target=/go/pkg/mod \ + make build OS=${TARGETOS} ARCH=${TARGETARCH} + +# CNI DOWNLOAD PHASE +FROM alpine AS cni ARG GOARCH=amd64 -ARG CNI_PLUGINS_VERSION=v1.1.1 +ARG CNI_PLUGINS_VERSION=v1.6.0 RUN apk add --no-cache curl && \ curl -Lo cni.tar.gz https://github.com/containernetworking/plugins/releases/download/$CNI_PLUGINS_VERSION/cni-plugins-linux-$GOARCH-$CNI_PLUGINS_VERSION.tgz && \ tar -xf cni.tar.gz -FROM $FROM +# FINAL IMAGE +FROM alpine ARG GOARCH ARG ALPINE_VERSION=v3.20 -LABEL maintainer="squat " +LABEL maintainer="Cast AI" RUN echo -e "https://alpine.global.ssl.fastly.net/alpine/$ALPINE_VERSION/main\nhttps://alpine.global.ssl.fastly.net/alpine/$ALPINE_VERSION/community" > /etc/apk/repositories && \ apk add --no-cache ipset iptables ip6tables graphviz font-noto + COPY --from=cni bridge host-local loopback portmap /opt/cni/bin/ ADD https://raw.githubusercontent.com/kubernetes-sigs/iptables-wrappers/e139a115350974aac8a82ec4b815d2845f86997e/iptables-wrapper-installer.sh / RUN chmod 700 /iptables-wrapper-installer.sh && /iptables-wrapper-installer.sh --no-sanity-check -COPY bin/linux/$GOARCH/kg /opt/bin/ -COPY bin/linux/$GOARCH/kgctl /opt/bin/ -ENTRYPOINT ["/opt/bin/kg"] + +COPY --from=builder workspace/bin/* /usr/local/bin/ + +ENTRYPOINT ["/usr/local/bin/kg"] diff --git a/Makefile b/Makefile index 68122bb4..2f321138 100644 --- a/Makefile +++ b/Makefile @@ -1,5 +1,5 @@ export GO111MODULE=on -.PHONY: push container clean container-name container-latest push-latest fmt lint test unit gomodtidy generate crds codegen manifest manfest-latest manifest-annotate manifest manfest-latest manifest-annotate release e2e +.PHONY: push container clean push-latest fmt lint test unit gomodtidy generate crds codegen e2e OS ?= $(shell go env GOOS) ARCH ?= $(shell go env GOARCH) @@ -14,7 +14,7 @@ RELEASE_BINS := $(addprefix bin/release/kgctl-, $(addprefix linux-, $(ALL_ARCH)) PROJECT := kilo PKG := github.com/squat/$(PROJECT) REGISTRY ?= index.docker.io -IMAGE ?= squat/$(PROJECT) +IMAGE ?= castai/$(PROJECT) FULLY_QUALIFIED_IMAGE := $(REGISTRY)/$(IMAGE) TAG := $(shell git describe --abbrev=0 --tags HEAD 2>/dev/null) @@ -27,18 +27,14 @@ ifneq ($(TAG),) endif DIRTY := $(shell test -z "$$(git diff --shortstat 2>/dev/null)" || echo -dirty) VERSION := $(VERSION)$(DIRTY) -LD_FLAGS := -buildvcs=false -ldflags '-X $(PKG)/pkg/version.Version=$(VERSION)' + +LDFLAGS := -X $(PKG)/pkg/version.Version=$(VERSION) SRC := $(shell find . -type f -name '*.go') GO_FILES ?= $$(find . -name '*.go') GO_PKGS ?= $$(go list ./...) -CONTROLLER_GEN_BINARY := bin/controller-gen -CLIENT_GEN_BINARY := bin/client-gen -DOCS_GEN_BINARY := bin/docs-gen -DEEPCOPY_GEN_BINARY := bin/deepcopy-gen -INFORMER_GEN_BINARY := bin/informer-gen -LISTER_GEN_BINARY := bin/lister-gen STATICCHECK_BINARY := bin/staticcheck +DOCS_GEN_BINARY := bin/docs-gen EMBEDMD_BINARY := bin/embedmd KIND_BINARY := $(shell pwd)/bin/kind KUBECTL_BINARY := $(shell pwd)/bin/kubectl @@ -107,53 +103,19 @@ imports: $(OPENSHIFT_GOIMPORTS) tools: $(CONTROLLER_GEN) $(YAML_PATCH) $(OPENSHIFT_GOIMPORTS) ## Install tools .PHONY: tools -build: $(BINS) - -build-%: - @$(MAKE) --no-print-directory OS=$(word 1,$(subst -, ,$*)) ARCH=$(word 2,$(subst -, ,$*)) build - -container-latest-%: - @$(MAKE) --no-print-directory ARCH=$* container-latest - -container-%: - @$(MAKE) --no-print-directory ARCH=$* container +build: WHAT ?= ./cmd/... +build: mkdirbin + GOOS=$(OS) GOARCH=$(ARCH) CGO_ENABLED=0 go build $(BUILDFLAGS) -ldflags="$(LDFLAGS)" -o bin $(WHAT) +.PHONY: build -push-latest-%: - @$(MAKE) --no-print-directory ARCH=$* push-latest +goreleaser: + LDFLAGS=$(LDFLAGS) goreleaser -push-%: - @$(MAKE) --no-print-directory ARCH=$* push - -all-build: $(addprefix build-$(OS)-, $(ALL_ARCH)) - -all-container: $(addprefix container-, $(ALL_ARCH)) - -all-push: $(addprefix push-, $(ALL_ARCH)) - -all-container-latest: $(addprefix container-latest-, $(ALL_ARCH)) - -all-push-latest: $(addprefix push-latest-, $(ALL_ARCH)) +ldflags: + @echo $(LDFLAGS) generate: codegen crds -$(BINS): $(SRC) go.mod - @mkdir -p bin/$(word 2,$(subst /, ,$@))/$(word 3,$(subst /, ,$@)) - @echo "building: $@" - @docker run --rm \ - -u $$(id -u):$$(id -g) \ - -v $$(pwd):/$(PROJECT) \ - -w /$(PROJECT) \ - $(BUILD_IMAGE) \ - /bin/sh -c " \ - GOARCH=$(word 3,$(subst /, ,$@)) \ - GOOS=$(word 2,$(subst /, ,$@)) \ - GOCACHE=/$(PROJECT)/.cache \ - CGO_ENABLED=0 \ - go build -o $@ \ - $(LD_FLAGS) \ - ./cmd/$(@F)/... \ - " - fmt: @echo $(GO_PKGS) gofmt -w -s $(GO_FILES) @@ -183,12 +145,15 @@ lint: $(STATICCHECK_BINARY) echo "$$fmt_res"; \ exit 1; \ fi - + unit: go test --race ./... test: lint unit e2e +mkdirbin: + mkdir -p bin/ + $(KIND_BINARY): curl -Lo $@ https://kind.sigs.k8s.io/dl/v0.25.0/kind-linux-$(ARCH) chmod +x $@ @@ -201,12 +166,12 @@ $(BASH_UNIT): curl -Lo $@ https://raw.githubusercontent.com/pgrange/bash_unit/v2.3.1/bash_unit chmod +x $@ -e2e: container $(KIND_BINARY) $(KUBECTL_BINARY) $(BASH_UNIT) bin/$(OS)/$(ARCH)/kgctl - KILO_IMAGE=$(IMAGE):$(ARCH)-$(VERSION) KIND_BINARY=$(KIND_BINARY) KUBECTL_BINARY=$(KUBECTL_BINARY) KGCTL_BINARY=$(shell pwd)/bin/$(OS)/$(ARCH)/kgctl $(BASH_UNIT) $(BASH_UNIT_FLAGS) ./e2e/setup.sh ./e2e/multi-cluster.sh ./e2e/handlers.sh ./e2e/kgctl.sh ./e2e/teardown.sh +e2e: mkdirbin container $(KIND_BINARY) $(KUBECTL_BINARY) $(BASH_UNIT) build + KILO_IMAGE=$(IMAGE):$(ARCH)-$(VERSION) KIND_BINARY=$(KIND_BINARY) KUBECTL_BINARY=$(KUBECTL_BINARY) KGCTL_BINARY=$(shell pwd)/bin/kgctl $(BASH_UNIT) $(BASH_UNIT_FLAGS) ./e2e/setup.sh ./e2e/multi-cluster.sh ./e2e/handlers.sh ./e2e/kgctl.sh ./e2e/teardown.sh -tmp/help.txt: bin/$(OS)/$(ARCH)/kg +tmp/help.txt: build mkdir -p tmp - bin//$(OS)/$(ARCH)/kg --help 2>&1 | head -n -1 > $@ + bin/kg --help 2>&1 | head -n -1 > $@ docs/kg.md: $(EMBEDMD_BINARY) tmp/help.txt $(EMBEDMD_BINARY) -w $@ @@ -228,109 +193,22 @@ website/build/index.html: website/docs/README.md docs/api.md yarn --cwd website install yarn --cwd website build -container: .container-$(ARCH)-$(VERSION) container-name -.container-$(ARCH)-$(VERSION): bin/linux/$(ARCH)/kg bin/linux/$(ARCH)/kgctl Dockerfile - @i=0; for a in $(ALL_ARCH); do [ "$$a" = $(ARCH) ] && break; i=$$((i+1)); done; \ - ia=""; iv=""; \ - j=0; for a in $(DOCKER_ARCH); do \ - [ "$$i" -eq "$$j" ] && ia=$$(echo "$$a" | awk '{print $$1}') && iv=$$(echo "$$a" | awk '{print $$2}') && break; j=$$((j+1)); \ - done; \ - SHA=$$(docker manifest inspect $(BASE_IMAGE) | jq '.manifests[] | select(.platform.architecture == "'$$ia'") | if .platform | has("variant") then select(.platform.variant == "'$$iv'") else . end | .digest' -r); \ - docker build -t $(IMAGE):$(ARCH)-$(VERSION) --build-arg FROM=$(BASE_IMAGE)@$$SHA --build-arg GOARCH=$(ARCH) . - @docker images -q $(IMAGE):$(ARCH)-$(VERSION) > $@ - -container-latest: .container-$(ARCH)-$(VERSION) - @docker tag $(IMAGE):$(ARCH)-$(VERSION) $(FULLY_QUALIFIED_IMAGE):$(ARCH)-latest - @echo "container: $(IMAGE):$(ARCH)-latest" - -container-name: - @echo "container: $(IMAGE):$(ARCH)-$(VERSION)" - -manifest: .manifest-$(VERSION) manifest-name -.manifest-$(VERSION): Dockerfile $(addprefix push-, $(ALL_ARCH)) - @docker manifest create --amend $(FULLY_QUALIFIED_IMAGE):$(VERSION) $(addsuffix -$(VERSION), $(addprefix $(FULLY_QUALIFIED_IMAGE):, $(ALL_ARCH))) - @$(MAKE) --no-print-directory manifest-annotate-$(VERSION) - @docker manifest push $(FULLY_QUALIFIED_IMAGE):$(VERSION) > $@ - -manifest-latest: Dockerfile $(addprefix push-latest-, $(ALL_ARCH)) - @docker manifest rm $(FULLY_QUALIFIED_IMAGE):latest || echo no old manifest - @docker manifest create --amend $(FULLY_QUALIFIED_IMAGE):latest $(addsuffix -latest, $(addprefix $(FULLY_QUALIFIED_IMAGE):, $(ALL_ARCH))) - @$(MAKE) --no-print-directory manifest-annotate-latest - @docker manifest push $(FULLY_QUALIFIED_IMAGE):latest - @echo "manifest: $(IMAGE):latest" - -manifest-annotate: manifest-annotate-$(VERSION) - -manifest-annotate-%: - @i=0; \ - for a in $(ALL_ARCH); do \ - annotate=; \ - j=0; for da in $(DOCKER_ARCH); do \ - if [ "$$j" -eq "$$i" ] && [ -n "$$da" ]; then \ - annotate="docker manifest annotate $(FULLY_QUALIFIED_IMAGE):$* $(FULLY_QUALIFIED_IMAGE):$$a-$* --os linux --arch"; \ - k=0; for ea in $$da; do \ - [ "$$k" = 0 ] && annotate="$$annotate $$ea"; \ - [ "$$k" != 0 ] && annotate="$$annotate --variant $$ea"; \ - k=$$((k+1)); \ - done; \ - $$annotate; \ - fi; \ - j=$$((j+1)); \ - done; \ - i=$$((i+1)); \ - done - -manifest-name: - @echo "manifest: $(IMAGE):$(VERSION)" - -push: .push-$(ARCH)-$(VERSION) push-name -.push-$(ARCH)-$(VERSION): .container-$(ARCH)-$(VERSION) -ifneq ($(REGISTRY),index.docker.io) - @docker tag $(IMAGE):$(ARCH)-$(VERSION) $(FULLY_QUALIFIED_IMAGE):$(ARCH)-$(VERSION) -endif - @docker push $(FULLY_QUALIFIED_IMAGE):$(ARCH)-$(VERSION) - @docker images -q $(IMAGE):$(ARCH)-$(VERSION) > $@ - -push-latest: container-latest - @docker push $(FULLY_QUALIFIED_IMAGE):$(ARCH)-latest - @echo "pushed: $(IMAGE):$(ARCH)-latest" - -push-name: - @echo "pushed: $(IMAGE):$(ARCH)-$(VERSION)" +container: + docker build -t $(FULLY_QUALIFIED_IMAGE):latest -f Dockerfile . -release: $(RELEASE_BINS) -$(RELEASE_BINS): - @make OS=$(word 2,$(subst -, ,$(@F))) ARCH=$(word 3,$(subst -, ,$(@F))) - mkdir -p $(@D) - cp bin/$(word 2,$(subst -, ,$(@F)))/$(word 3,$(subst -, ,$(@F)))/kgctl $@ +container-linux: + FULLY_QUALIFIED_IMAGE=$(FULLY_QUALIFIED_IMAGE- + OS=linux ARCH=amd64 make container -clean: container-clean bin-clean +clean: bin-clean rm -rf .cache -container-clean: - rm -rf .container-* .manifest-* .push-* - bin-clean: rm -rf bin gomodtidy: go mod tidy -$(CONTROLLER_GEN_BINARY): - go build -o $@ sigs.k8s.io/controller-tools/cmd/controller-gen - -$(CLIENT_GEN_BINARY): - go build -o $@ k8s.io/code-generator/cmd/client-gen - -$(DEEPCOPY_GEN_BINARY): - go build -o $@ k8s.io/code-generator/cmd/deepcopy-gen - -$(INFORMER_GEN_BINARY): - go build -o $@ k8s.io/code-generator/cmd/informer-gen - -$(LISTER_GEN_BINARY): - go build -o $@ k8s.io/code-generator/cmd/lister-gen - $(DOCS_GEN_BINARY): cmd/docs-gen/main.go go build -o $@ ./cmd/docs-gen diff --git a/cmd/kgctl/connect_linux.go b/cmd/kgctl/connect_linux.go index 15191dff..2d97a0b4 100644 --- a/cmd/kgctl/connect_linux.go +++ b/cmd/kgctl/connect_linux.go @@ -155,7 +155,7 @@ func runConnect(cmd *cobra.Command, args []string) error { } } publicKey := privateKey.PublicKey() - level.Info(logger).Log("msg", "generated public key", "key", publicKey) + level.Info(logger).Log("msg", "generated public key", "key", publicKey.String()) if _, err := opts.kc.KiloV1alpha1().Peers().Get(ctx, peerName, metav1.GetOptions{}); apierrors.IsNotFound(err) { peer := &v1alpha1.Peer{ diff --git a/pkg/mesh/backend.go b/pkg/mesh/backend.go index 203661d1..8685ffb9 100644 --- a/pkg/mesh/backend.go +++ b/pkg/mesh/backend.go @@ -36,7 +36,7 @@ const ( ) // DefaultKiloSubnet is the default CIDR for Kilo. -var DefaultKiloSubnet = &net.IPNet{IP: []byte{10, 4, 0, 0}, Mask: []byte{255, 255, 0, 0}} +var DefaultKiloSubnet = &net.IPNet{IP: []byte{10, 254, 0, 0}, Mask: []byte{255, 255, 0, 0}} // Granularity represents the abstraction level at which the network // should be meshed. diff --git a/pkg/mesh/topology.go b/pkg/mesh/topology.go index ca22bf61..78a45b7f 100644 --- a/pkg/mesh/topology.go +++ b/pkg/mesh/topology.go @@ -15,6 +15,7 @@ package mesh import ( + "encoding/json" "errors" "net" "sort" @@ -71,7 +72,7 @@ type Topology struct { // segment represents one logical unit in the topology that is united by one common WireGuard IP. type segment struct { - allowedIPs []net.IPNet + allowedIPs netIPNETSlice endpoint *wireguard.Endpoint key wgtypes.Key persistentKeepalive time.Duration @@ -95,6 +96,17 @@ type segment struct { allowedLocationIPs []net.IPNet } +type netIPNETSlice []net.IPNet + +// MarshalJSON marshals the allowedIPs to a JSON array of strings. +func (a netIPNETSlice) MarshalJSON() ([]byte, error) { + var s []string + for _, ip := range a { + s = append(s, ip.String()) + } + return json.Marshal(s) +} + // NewTopology creates a new Topology struct from a given set of nodes and peers. func NewTopology(nodes map[string]*Node, peers map[string]*Peer, granularity Granularity, hostname string, port int, key wgtypes.Key, subnet *net.IPNet, serviceCIDRs []*net.IPNet, persistentKeepalive time.Duration, logger log.Logger) (*Topology, error) { if logger == nil { @@ -149,9 +161,9 @@ func NewTopology(nodes map[string]*Node, peers map[string]*Peer, granularity Gra if location == localLocation && topoMap[location][leader].Name == hostname { t.leader = true } - var allowedIPs []net.IPNet + var allowedIPs netIPNETSlice allowedLocationIPsMap := make(map[string]struct{}) - var allowedLocationIPs []net.IPNet + var allowedLocationIPs netIPNETSlice var cidrs []*net.IPNet var hostnames []string var privateIPs []net.IP