Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: check server capabilities usin the preferred resources endpoint #54

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions .dockerignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
.git
.github
.vscode
.tools
artifacts
examples
hack
38 changes: 27 additions & 11 deletions .github/workflows/build.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -8,38 +8,54 @@ on:

jobs:
build:
strategy:
matrix:
go-version: [1.13.x]
os: [ubuntu-latest]
runs-on: ${{ matrix.os }}
runs-on: ubuntu-latest
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please move to the publish as discussed on the call today.

steps:
- uses: actions/checkout@master
- name: Install Go
uses: actions/setup-go@v2
with:
go-version: ${{ matrix.go-version }}
- name: Set up QEMU
uses: docker/setup-qemu-action@v1
- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v1

- name: Set Username/Repo and ImagePrefix as ENV vars
run: |
echo "IMAGE_PREFIX=ghcr.io/${GITHUB_REPOSITORY,,}" >>${GITHUB_ENV}

- name: Build x86_64 container into library
uses: docker/build-push-action@v2
with:
context: .
file: ./Dockerfile
outputs: "type=docker,push=false"
build-args: |
VERSION=latest-dev
GIT_COMMIT=${{ github.sha }}
platforms: linux/amd64
tags: |
ghcr.io/openfaas/ingress-operator:${{ github.sha }}
${{ env.IMAGE_PREFIX }}:${{ github.sha }}

build-multi-arch:
# run in parallel to build because multi-arch is slow
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@master
- name: Set up QEMU
uses: docker/setup-qemu-action@v1
- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v1

- name: Set Username/Repo and ImagePrefix as ENV vars
run: |
echo "IMAGE_PREFIX=ghcr.io/${GITHUB_REPOSITORY,,}" >>${GITHUB_ENV}

- name: Build multi-arch containers for validation only
uses: docker/build-push-action@v2
with:
context: .
file: ./Dockerfile
outputs: "type=image,push=false"
build-args: |
VERSION=latest-dev
GIT_COMMIT=${{ github.sha }}
platforms: linux/amd64,linux/arm/v7,linux/arm64
tags: |
ghcr.io/openfaas/ingress-operator:${{ github.sha }}
${{ env.IMAGE_PREFIX }}:${{ github.sha }}
37 changes: 21 additions & 16 deletions .github/workflows/publish.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -7,40 +7,45 @@ on:

jobs:
publish:
strategy:
matrix:
go-version: [1.13.x]
os: [ubuntu-latest]
runs-on: ${{ matrix.os }}
runs-on: ubuntu-latest
permissions:
actions: read
checks: write
issues: read
packages: write
pull-requests: read
repository-projects: read
statuses: read
steps:
- uses: actions/checkout@master
- name: Install Go
uses: actions/setup-go@v2
with:
go-version: ${{ matrix.go-version }}
- name: Set up QEMU
uses: docker/setup-qemu-action@v1
- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v1
- name: Get TAG
id: get_tag
run: echo ::set-output name=TAG::${GITHUB_REF#refs/tags/}
- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v1
- name: Set Username/Repo and ImagePrefix as ENV vars
run: |
echo "IMAGE_PREFIX=ghcr.io/${GITHUB_REPOSITORY,,}" >>${GITHUB_ENV}

- name: Login to Docker Registry
uses: docker/login-action@v1
with:
username: ${{ secrets.DOCKER_USERNAME }}
password: ${{ secrets.DOCKER_PASSWORD }}
registry: ghcr.io
username: ${{ github.repository_owner }}
password: ${{ secrets.GITHUB_TOKEN }}
- name: Push containers
uses: docker/build-push-action@v2
with:
context: .
file: ./Dockerfile
outputs: "type=registry,push=true"
build-args: |
VERSION=${{ steps.get_tag.outputs.TAG }}
GIT_COMMIT=${{ github.sha }}
platforms: linux/amd64,linux/arm/v7,linux/arm64
tags: |
ghcr.io/openfaas/ingress-operator:${{ github.sha }}
ghcr.io/openfaas/ingress-operator:${{ steps.get_tag.outputs.TAG }}
ghcr.io/openfaas/ingress-operator:latest
${{ env.IMAGE_PREFIX }}:${{ github.sha }}
${{ env.IMAGE_PREFIX }}:${{ steps.get_tag.outputs.TAG }}
${{ env.IMAGE_PREFIX }}:latest
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,5 @@ ingress-operator
config
password.txt
faas-netes/**
test.yaml
cluster.yaml
11 changes: 7 additions & 4 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,14 +1,19 @@
FROM --platform=${BUILDPLATFORM:-linux/amd64} teamserverless/license-check:0.3.9 as license-check
FROM --platform=${BUILDPLATFORM:-linux/amd64} golang:1.15 as builder
FROM --platform=${BUILDPLATFORM:-linux/amd64} golang:1.16 as builder

ARG TARGETPLATFORM
ARG BUILDPLATFORM
ARG TARGETOS
ARG TARGETARCH

ARG GIT_COMMIT
ARG VERSION

ENV CGO_ENABLED=0
ENV GO111MODULE=on
ENV GOFLAGS=-mod=vendor
ENV GOOS=${TARGETOS}
ENV GOARCH=${TARGETARCH}

COPY --from=license-check /license-check /usr/bin/

Expand All @@ -22,9 +27,7 @@ ARG OPTS

RUN gofmt -l -d $(find . -type f -name '*.go' -not -path "./vendor/*")
RUN go test -mod=vendor -v ./...
RUN VERSION=$(git describe --all --exact-match `git rev-parse HEAD` | grep tags | sed 's/tags\///') && \
GIT_COMMIT=$(git rev-list -1 HEAD) && \
env ${OPTS} GOOS=${TARGETOS} GOARCH=${TARGETARCH} CGO_ENABLED=${CGO_ENABLED} GOOS=linux go build -mod=vendor -ldflags "-s -w \
RUN go build -mod=vendor -ldflags "-s -w \
-X github.com/openfaas-incubator/ingress-operator/pkg/version.Release=${VERSION} \
-X github.com/openfaas-incubator/ingress-operator/pkg/version.SHA=${GIT_COMMIT}" \
-a -installsuffix cgo -o ingress-operator . && \
Expand Down
78 changes: 60 additions & 18 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
.PHONY: build push manifest test verify-codegen charts
TAG?=latest

.GIT_COMMIT=$(shell git rev-parse HEAD)
.GIT_VERSION=$(shell git describe --tags 2>/dev/null || echo "$(.GIT_COMMIT)")
.GIT_UNTRACKEDCHANGES := $(shell git status --porcelain --untracked-files=no)
ifneq ($(.GIT_UNTRACKEDCHANGES),)
.GIT_COMMIT := $(.GIT_COMMIT)-dirty
endif

# docker manifest command will work with Docker CLI 18.03 or newer
# but for now it's still experimental feature so we need to enable that
export DOCKER_CLI_EXPERIMENTAL=enabled
Expand All @@ -11,6 +17,13 @@ GOPATH := $(shell go env GOPATH)
CODEGEN_VERSION := $(shell hack/print-codegen-version.sh)
CODEGEN_PKG := $(GOPATH)/pkg/mod/k8s.io/code-generator@${CODEGEN_VERSION}

OWNER?=openfaas
REPOSITORY?="ghcr.io/$(OWNER)"
NAME := ingress-operator

ARCH?=linux/amd64
MULTIARCH?=linux/amd64,linux/arm/v7,linux/arm64

$(TOOLS_DIR)/code-generator.mod: go.mod
@echo "syncing code-generator tooling version"
@cd $(TOOLS_DIR) && go mod edit -require "k8s.io/code-generator@${CODEGEN_VERSION}"
Expand All @@ -19,34 +32,63 @@ ${CODEGEN_PKG}: $(TOOLS_DIR)/code-generator.mod
@echo "(re)installing k8s.io/code-generator-${CODEGEN_VERSION}"
@cd $(TOOLS_DIR) && go mod download -modfile=code-generator.mod

.PHONY: build
build:
docker build -t ghcr.io/openfaas/ingress-operator:$(TAG)-amd64 . -f Dockerfile
docker build --build-arg OPTS="GOARCH=arm64" -t ghcr.io/openfaas/ingress-operator:$(TAG)-arm64 . -f Dockerfile
docker build --build-arg OPTS="GOARCH=arm GOARM=6" -t ghcr.io/openfaas/ingress-operator:$(TAG)-armhf . -f Dockerfile

push:
docker push ghcr.io/openfaas/ingress-operator:$(TAG)-amd64
docker push ghcr.io/openfaas/ingress-operator:$(TAG)-arm64
docker push ghcr.io/openfaas/ingress-operator:$(TAG)-armhf

manifest:
docker manifest create --amend ghcr.io/openfaas/ingress-operator:$(TAG) \
ghcr.io/openfaas/ingress-operator:$(TAG)-amd64 \
ghcr.io/openfaas/ingress-operator:$(TAG)-arm64 \
ghcr.io/openfaas/ingress-operator:$(TAG)-armhf
docker manifest annotate ghcr.io/openfaas/ingress-operator:$(TAG) ghcr.io/openfaas/ingress-operator:$(TAG)-arm64 --os linux --arch arm64
docker manifest annotate ghcr.io/openfaas/ingress-operator:$(TAG) ghcr.io/openfaas/ingress-operator:$(TAG)-armhf --os linux --arch arm --variant v6
docker manifest push -p ghcr.io/openfaas/ingress-operator:$(TAG)
@echo "building $(REPOSITORY)/$(NAME):$(TAG)"
@docker build \
--build-arg VERSION=$(.GIT_VERSION) \
--build-arg GIT_COMMIT=$(.GIT_COMMIT) \
-t $(REPOSITORY)/$(NAME):$(TAG) .

.PHONY: build-buildx
build-buildx:
@echo $(REPOSITORY)/$(NAME):$(TAG) && \
docker buildx create --use --name=multiarch --node=multiarch && \
docker buildx build \
--push \
--platform $(ARCH) \
--build-arg VERSION=$(.GIT_VERSION) \
--build-arg GIT_COMMIT=$(.GIT_COMMIT) \
--tag $(REPOSITORY)/$(NAME):$(TAG) \
.

.PHONY: build-buildx-all
build-buildx-all:
@echo "build $(REPOSITORY)/$(NAME):$(TAG) for $(MULTIARCH)"
@docker buildx create --use --name=multiarch --node=multiarch && \
docker buildx build \
--platform $(MULTIARCH) \
--output "type=image,push=false" \
--build-arg VERSION=$(.GIT_VERSION) \
--build-arg GIT_COMMIT=$(.GIT_COMMIT) \
--tag $(REPOSITORY)/$(NAME):$(TAG) \
.

.PHONY: publish-buildx-all
publish-buildx-all:
@echo "build and publish $(REPOSITORY)/$(NAME):$(TAG) for $(MULTIARCH)"
@docker buildx create --use --name=multiarch --node=multiarch && \
docker buildx build \
--platform $(MULTIARCH) \
--push=true \
--build-arg VERSION=$(.GIT_VERSION) \
--build-arg GIT_COMMIT=$(.GIT_COMMIT) \
--tag $(REPOSITORY)/$(NAME):$(TAG) \
.

.PHONY: test
test:
go test -v ./...

.PHONY: verify-codegen
verify-codegen: ${CODEGEN_PKG}
./hack/verify-codegen.sh

.PHONY: update-codegen
update-codegen: ${CODEGEN_PKG}
./hack/update-codegen.sh

.PHONY: charts
charts:
cd chart && helm package ingress-operator/
mv chart/*.tgz docs/
Expand Down
51 changes: 47 additions & 4 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package main
import (
"flag"
"os"
"strings"
"time"

clientset "github.com/openfaas-incubator/ingress-operator/pkg/client/clientset/versioned"
Expand Down Expand Up @@ -85,21 +86,25 @@ func main() {
faasInformerFactory := informers.
NewSharedInformerFactoryWithOptions(faasClient, defaultResync, faasInformerOpt)

capabilities, err := getCapabilities(kubeClient)
capabilities, err := getPreferredAvailableAPIs(kubeClient, "Ingress")
if err != nil {
klog.Fatalf("Error retrieving Kubernetes cluster capabilities: %s", err.Error())
}

klog.Infof("cluster supports ingress in: %s", capabilities)

var ctrl controller
if capabilities.Has("extensions/v1beta1") {
ctrl = controllerv1beta1.NewController(
// prefer v1, if it is available, this removes any deprecation warnings
if capabilities.Has("networking.k8s.io/v1") {
ctrl = controllerv1.NewController(
kubeClient,
faasClient,
kubeInformerFactory,
faasInformerFactory,
)
} else {
ctrl = controllerv1.NewController(
// use v1beta1 by default
ctrl = controllerv1beta1.NewController(
kubeClient,
faasClient,
kubeInformerFactory,
Expand Down Expand Up @@ -139,6 +144,15 @@ func (c Capabilities) Has(wanted string) bool {
return c[wanted]
}

func (c Capabilities) String() string {
keys := make([]string, 0, len(c))
for k := range c {
keys = append(keys, k)
}
return strings.Join(keys, ", ")
}

// getCapabilities returns the list of available api groups in the cluster.
func getCapabilities(client kubernetes.Interface) (Capabilities, error) {

groupList, err := client.Discovery().ServerGroups()
Expand All @@ -155,3 +169,32 @@ func getCapabilities(client kubernetes.Interface) (Capabilities, error) {

return caps, nil
}

// getPreferredAvailableAPIs queries the cluster for the preferred resources information and returns a Capabilities
// instance containing those api groups that support the specified kind.
//
// kind should be the title case singular name of the kind. For example, "Ingress" is the kind for a resource "ingress".
func getPreferredAvailableAPIs(client kubernetes.Interface, kind string) (Capabilities, error) {
discoveryclient := client.Discovery()
lists, err := discoveryclient.ServerPreferredResources()
if err != nil {
return nil, err
}

caps := Capabilities{}
for _, list := range lists {
if len(list.APIResources) == 0 {
continue
}
for _, resource := range list.APIResources {
if len(resource.Verbs) == 0 {
continue
}
if resource.Kind == kind {
caps[list.GroupVersion] = true
}
}
}

return caps, nil
}