Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
Signed-off-by: Matej Vašek <[email protected]>
  • Loading branch information
matejvasek committed Mar 25, 2024
1 parent ed05446 commit a5b936c
Show file tree
Hide file tree
Showing 3 changed files with 129 additions and 0 deletions.
22 changes: 22 additions & 0 deletions .github/workflows/build-images.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
name: Build Images

on: [pull_request]

jobs:
build:
name: Build job
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- uses: ./.github/composite/go-setup
- name: Install uuid
run: |
sudo apt-get update
sudo apt-get install uuid
- name: Build Util Image
run: |
docker buildx create --name multiarch --driver docker-container --use
docker buildx build . -f Dockerfile.utils \
--platform=linux/ppc64le,linux/s390x,linux/amd64,linux/arm64 \
--push \
-t "ttl.sh/knative/func-util-$(uuid):2h"
21 changes: 21 additions & 0 deletions Dockerfile.utils
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
FROM --platform=$BUILDPLATFORM index.docker.io/library/golang:1.22.1-alpine3.19 AS builder

ARG BUILDPLATFORM
ARG TARGETPLATFORM
ARG TARGETARCH

COPY . .

RUN GOARCH=$TARGETARCH go build -o deploy -trimpath -ldflags '-w -s' ./cmd/func-deployer

#########################

FROM --platform=$TARGETPLATFORM index.docker.io/library/alpine:latest

RUN apk add --no-cache socat tar \
&& addgroup func -g 1000 \
&& adduser func -u 1001 -D -G func

COPY --from=builder /go/deploy /usr/local/bin/

USER func:func
86 changes: 86 additions & 0 deletions cmd/func-deployer/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
package main

import (
"context"
"fmt"
"os"
"os/signal"
"syscall"

fn "knative.dev/func/pkg/functions"
"knative.dev/func/pkg/k8s"
"knative.dev/func/pkg/knative"
)

func main() {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()

sigs := make(chan os.Signal, 1)
signal.Notify(sigs, syscall.SIGINT, syscall.SIGTERM)
go func() {
<-sigs
cancel()
<-sigs // second sigint/sigterm is treated as sigkill
os.Exit(137)
}()

err := deploy(ctx)
if err != nil {
_, _ = fmt.Fprintf(os.Stderr, "ERROR: %s\n", err)
os.Exit(1)
}
}
func deploy(ctx context.Context) error {
var err error
deployer := knative.NewDeployer(
knative.WithDeployerVerbose(true),
knative.WithDeployerDecorator(deployDecorator{}))

var root string
if len(os.Args) > 1 {
root = os.Args[1]
} else {
root, err = os.Getwd()
if err != nil {
return fmt.Errorf("cannot determine working directory: %w", err)
}
}

f, err := fn.NewFunction(root)
if err != nil {
return fmt.Errorf("cannot load function: %w", err)
}
if len(os.Args) > 2 {
f.Deploy.Image = os.Args[2]
}
if f.Deploy.Image == "" {
f.Deploy.Image = f.Image
}

res, err := deployer.Deploy(ctx, f)
if err != nil {
return fmt.Errorf("cannont deploy the function: %w", err)
}

fmt.Printf("function has been deployed\n%+v\n", res)
return nil
}

type deployDecorator struct {
oshDec k8s.OpenshiftMetadataDecorator
}

func (d deployDecorator) UpdateAnnotations(function fn.Function, annotations map[string]string) map[string]string {
if k8s.IsOpenShift() {
return d.oshDec.UpdateAnnotations(function, annotations)
}
return annotations
}

func (d deployDecorator) UpdateLabels(function fn.Function, labels map[string]string) map[string]string {
if k8s.IsOpenShift() {
return d.oshDec.UpdateLabels(function, labels)
}
return labels
}

0 comments on commit a5b936c

Please sign in to comment.