Skip to content

Commit

Permalink
cache for resource informer
Browse files Browse the repository at this point in the history
Signed-off-by: Mikhail Scherba <mikhail.scherba@flant.com>
  • Loading branch information
miklezzzz committed Oct 23, 2024
1 parent d65a21b commit 6e97384
Show file tree
Hide file tree
Showing 20 changed files with 400 additions and 329 deletions.
10 changes: 8 additions & 2 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
FROM --platform=${TARGETPLATFORM:-linux/amd64} flant/jq:b6be13d5-musl as libjq

# Go builder.
FROM --platform=${TARGETPLATFORM:-linux/amd64} golang:1.22-alpine3.18 AS builder
FROM --platform=${TARGETPLATFORM:-linux/amd64} golang:1.22-alpine AS builder

ARG appVersion=latest
RUN apk --no-cache add git ca-certificates gcc musl-dev libc-dev binutils-gold
Expand Down Expand Up @@ -30,6 +30,11 @@ RUN shellOpVer=$(go list -m all | grep shell-operator | cut -d' ' -f 2-) \
-o addon-operator \
./cmd/addon-operator

# Build helm post-renderer binary (required if helm3 binary is in use)
RUN CGO_ENABLED=1 \
GOOS=linux \
go build -o post-renderer ./cmd/post-renderer

# Final image
FROM --platform=${TARGETPLATFORM:-linux/amd64} alpine:3.16
ARG TARGETPLATFORM
Expand All @@ -44,11 +49,12 @@ RUN apk --no-cache add ca-certificates bash sed tini && \
wget https://get.helm.sh/helm-v3.10.3-${helmArch}.tar.gz -O /helm.tgz && \
tar -z -x -C /bin -f /helm.tgz --strip-components=1 ${helmArch}/helm && \
rm -f /helm.tgz

COPY --from=libjq /bin/jq /usr/bin
COPY --from=builder /app/addon-operator /
COPY --from=builder /app/post-renderer /
COPY --from=builder /app/shell-operator-clone/frameworks/shell/ /framework/shell/
COPY --from=builder /app/shell-operator-clone/shell_lib.sh /

WORKDIR /

RUN mkdir /global-hooks /modules
Expand Down
44 changes: 16 additions & 28 deletions cmd/addon-operator/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,8 +79,7 @@ func start(_ *kingpin.ParseContext) error {

err := run(ctx, operator)
if err != nil {
log.Error(err)
os.Exit(1)
log.Fatal(err)
}

return nil
Expand All @@ -90,22 +89,18 @@ func run(ctx context.Context, operator *addon_operator.AddonOperator) error {
bk := configmap.New(log.StandardLogger(), operator.KubeClient(), app.Namespace, app.ConfigMapName)
operator.SetupKubeConfigManager(bk)

err := operator.Setup()
if err != nil {
fmt.Printf("Setup is failed: %s\n", err)
os.Exit(1)
if err := operator.Setup(); err != nil {
log.Fatalf("setup failed: %s\n", err)
}

err = operator.Start(ctx)
if err != nil {
fmt.Printf("Start is failed: %s\n", err)
os.Exit(1)
if err := operator.Start(ctx); err != nil {
log.Fatalf("start failed: %s\n", err)
}

// Block action by waiting signals from OS.
utils_signal.WaitForProcessInterruption(func() {
operator.Stop()
os.Exit(1)
os.Exit(0)
})

return nil
Expand All @@ -114,25 +109,22 @@ func run(ctx context.Context, operator *addon_operator.AddonOperator) error {
func runHAMode(ctx context.Context, operator *addon_operator.AddonOperator) {
podName := os.Getenv("ADDON_OPERATOR_POD")
if len(podName) == 0 {
log.Info("ADDON_OPERATOR_POD env not set or empty")
os.Exit(1)
log.Fatal("ADDON_OPERATOR_POD env not set or empty")
}

podIP := os.Getenv("ADDON_OPERATOR_LISTEN_ADDRESS")
if len(podIP) == 0 {
log.Info("ADDON_OPERATOR_LISTEN_ADDRESS env not set or empty")
os.Exit(1)
log.Fatal("ADDON_OPERATOR_LISTEN_ADDRESS env not set or empty")
}

podNs := os.Getenv("ADDON_OPERATOR_NAMESPACE")
if len(podNs) == 0 {
log.Info("ADDON_OPERATOR_NAMESPACE env not set or empty")
os.Exit(1)
log.Fatal("ADDON_OPERATOR_NAMESPACE env not set or empty")
}

identity := fmt.Sprintf("%s.%s.%s.pod", podName, strings.ReplaceAll(podIP, ".", "-"), podNs)

err := operator.WithLeaderElector(&leaderelection.LeaderElectionConfig{
if err := operator.WithLeaderElector(&leaderelection.LeaderElectionConfig{
// Create a leaderElectionConfig for leader election
Lock: &resourcelock.LeaseLock{
LeaseMeta: v1.ObjectMeta{
Expand All @@ -151,29 +143,25 @@ func runHAMode(ctx context.Context, operator *addon_operator.AddonOperator) {
OnStartedLeading: func(ctx context.Context) {
err := run(ctx, operator)
if err != nil {
log.Info(err)
os.Exit(1)
log.Fatal(err)
}
},
OnStoppedLeading: func() {
log.Info("Restarting because the leadership was handed over")
operator.Stop()
os.Exit(1)
os.Exit(0)
},
},
ReleaseOnCancel: true,
})
if err != nil {
log.Error(err)
}); err != nil {
log.Fatal(err)
}

go func() {
<-ctx.Done()
log.Info("Context canceled received")
err := syscall.Kill(1, syscall.SIGUSR2)
if err != nil {
log.Infof("Couldn't shutdown addon-operator: %s\n", err)
os.Exit(1)
if err := syscall.Kill(1, syscall.SIGUSR2); err != nil {
log.Fatalf("Couldn't shutdown addon-operator: %s\n", err)
}
}()

Expand Down
36 changes: 36 additions & 0 deletions cmd/post-renderer/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package main

import (
"bytes"
"fmt"
"io"
"os"

"github.com/flant/addon-operator/pkg/helm/post_renderer"
)

func main() {
inputBytes, err := io.ReadAll(os.Stdin)
if err != nil {
fmt.Fprintf(os.Stderr, "couldn't read input from stdin: %s", err)
os.Exit(1)
}
buf := bytes.NewBuffer(inputBytes)

renderer := post_renderer.PostRenderer{
ExtraLabels: map[string]string{
"heritage": "addon-operator",
},
}

outputBytes, err := renderer.Run(buf)
if err != nil {
fmt.Fprintf(os.Stderr, "couldn't render input from stdin: %s", err)
os.Exit(1)
}

if _, err := os.Stdout.Write(outputBytes.Bytes()); err != nil {
fmt.Fprintf(os.Stderr, "couldn't write rendered output to stdout: %s", err)
os.Exit(1)
}
}
2 changes: 2 additions & 0 deletions docs/src/RUNNING.md
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,8 @@ Addon-operator expects that "helm" binary is available in $PATH. It detects Helm

**HELM_BIN_PATH** — a path to a Helm binary.

**HELM_POST_RENDERER_PATH** — a path to a Helm post-renderer binary.

**HELM3** — set to "yes" to disable auto-detection and explicitly enable compatibility with helm3.

**HELM_IGNORE_RELEASE** — a name of the release that should not be treated as the module's release. Prevent self-destruction when addon-operator release is stored in the same namespace as releases for modules.
Expand Down
45 changes: 22 additions & 23 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
module github.com/flant/addon-operator

go 1.22.8
go 1.22.4

require (
github.com/davecgh/go-spew v1.1.1
github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc
github.com/dominikbraun/graph v0.23.0
github.com/ettle/strcase v0.2.0
github.com/flant/kube-client v1.2.0
Expand All @@ -23,7 +23,6 @@ require (
github.com/sirupsen/logrus v1.9.3
github.com/stretchr/testify v1.9.0
github.com/tidwall/gjson v1.14.4
go.uber.org/goleak v1.2.1
gopkg.in/alecthomas/kingpin.v2 v2.2.6
gopkg.in/yaml.v3 v3.0.1
helm.sh/helm/v3 v3.14.2
Expand All @@ -32,7 +31,9 @@ require (
k8s.io/apimachinery v0.29.8
k8s.io/cli-runtime v0.29.0
k8s.io/client-go v0.29.8
k8s.io/utils v0.0.0-20230726121419-3b25d923346b
k8s.io/utils v0.0.0-20240711033017-18e509b52bc8
sigs.k8s.io/controller-runtime v0.17.0
sigs.k8s.io/kustomize/kyaml v0.14.3-0.20230601165947-6ce0bf390ce3
sigs.k8s.io/yaml v1.4.0
)

Expand All @@ -49,7 +50,7 @@ require (
github.com/alecthomas/units v0.0.0-20211218093645-b94a6e3cc137 // indirect
github.com/asaskevich/govalidator v0.0.0-20200428143746-21a406dcc535 // indirect
github.com/beorn7/perks v1.0.1 // indirect
github.com/cespare/xxhash/v2 v2.2.0 // indirect
github.com/cespare/xxhash/v2 v2.3.0 // indirect
github.com/chai2010/gettext-go v1.0.2 // indirect
github.com/containerd/containerd v1.7.20 // indirect
github.com/containerd/errdefs v0.1.0 // indirect
Expand All @@ -65,14 +66,15 @@ require (
github.com/docker/go-metrics v0.0.1 // indirect
github.com/emicklei/go-restful/v3 v3.11.0 // indirect
github.com/evanphx/json-patch v5.7.0+incompatible // indirect
github.com/evanphx/json-patch/v5 v5.9.0 // indirect
github.com/exponent-io/jsonpath v0.0.0-20151013193312-d6023ce2651d // indirect
github.com/fatih/color v1.13.0 // indirect
github.com/felixge/httpsnoop v1.0.4 // indirect
github.com/flant/libjq-go v1.6.3-0.20201126171326-c46a40ff22ee // indirect
github.com/fogleman/gg v1.3.0 // indirect
github.com/go-errors/errors v1.4.2 // indirect
github.com/go-gorp/gorp/v3 v3.1.0 // indirect
github.com/go-logr/logr v1.4.1 // indirect
github.com/go-logr/logr v1.4.2 // indirect
github.com/go-logr/stdr v1.2.2 // indirect
github.com/go-openapi/analysis v0.19.10 // indirect
github.com/go-openapi/errors v0.19.7 // indirect
Expand All @@ -82,7 +84,6 @@ require (
github.com/go-stack/stack v1.8.0 // indirect
github.com/gobwas/glob v0.2.3 // indirect
github.com/gogo/protobuf v1.3.2 // indirect
github.com/gojuno/minimock/v3 v3.4.0 // indirect
github.com/golang/freetype v0.0.0-20170609003504-e2365dfdc4a0 // indirect
github.com/golang/protobuf v1.5.4 // indirect
github.com/google/btree v1.0.1 // indirect
Expand Down Expand Up @@ -116,7 +117,7 @@ require (
github.com/mitchellh/mapstructure v1.5.0 // indirect
github.com/mitchellh/reflectwalk v1.0.2 // indirect
github.com/moby/locker v1.0.1 // indirect
github.com/moby/spdystream v0.2.0 // indirect
github.com/moby/spdystream v0.4.0 // indirect
github.com/moby/term v0.5.0 // indirect
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
github.com/modern-go/reflect2 v1.0.2 // indirect
Expand All @@ -126,11 +127,11 @@ require (
github.com/opencontainers/go-digest v1.0.0 // indirect
github.com/opencontainers/image-spec v1.1.0 // indirect
github.com/peterbourgon/diskv v2.0.1+incompatible // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2 // indirect
github.com/prometheus/client_golang v1.19.1 // indirect
github.com/prometheus/client_model v0.5.0 // indirect
github.com/prometheus/common v0.48.0 // indirect
github.com/prometheus/procfs v0.12.0 // indirect
github.com/prometheus/client_model v0.6.1 // indirect
github.com/prometheus/common v0.55.0 // indirect
github.com/prometheus/procfs v0.15.1 // indirect
github.com/rubenv/sql-migrate v1.5.2 // indirect
github.com/russross/blackfriday/v2 v2.1.0 // indirect
github.com/shopspring/decimal v1.3.1 // indirect
Expand All @@ -144,37 +145,35 @@ require (
github.com/xeipuuv/gojsonschema v1.2.0 // indirect
github.com/xlab/treeprint v1.2.0 // indirect
go.mongodb.org/mongo-driver v1.5.4 // indirect
go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp v0.46.1 // indirect
go.opentelemetry.io/otel v1.21.0 // indirect
go.opentelemetry.io/otel/metric v1.21.0 // indirect
go.opentelemetry.io/otel/trace v1.21.0 // indirect
go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp v0.53.0 // indirect
go.opentelemetry.io/otel v1.28.0 // indirect
go.opentelemetry.io/otel/metric v1.28.0 // indirect
go.opentelemetry.io/otel/trace v1.28.0 // indirect
go.starlark.net v0.0.0-20230525235612-a134d8f9ddca // indirect
golang.org/x/crypto v0.25.0 // indirect
golang.org/x/exp v0.0.0-20240719175910-8a7402abbf56 // indirect
golang.org/x/image v0.18.0 // indirect
golang.org/x/net v0.27.0 // indirect
golang.org/x/oauth2 v0.16.0 // indirect
golang.org/x/oauth2 v0.21.0 // indirect
golang.org/x/sync v0.7.0 // indirect
golang.org/x/sys v0.22.0 // indirect
golang.org/x/term v0.22.0 // indirect
golang.org/x/text v0.16.0 // indirect
golang.org/x/time v0.7.0 // indirect
google.golang.org/appengine v1.6.7 // indirect
google.golang.org/genproto/googleapis/rpc v0.0.0-20231212172506-995d672761c0 // indirect
google.golang.org/genproto/googleapis/rpc v0.0.0-20240701130421-f6361c86f094 // indirect
google.golang.org/grpc v1.59.0 // indirect
google.golang.org/protobuf v1.34.1 // indirect
google.golang.org/protobuf v1.34.2 // indirect
gopkg.in/inf.v0 v0.9.1 // indirect
gopkg.in/robfig/cron.v2 v2.0.0-20150107220207-be2e0b0deed5 // indirect
gopkg.in/yaml.v2 v2.4.0 // indirect
k8s.io/apiserver v0.29.0 // indirect
k8s.io/component-base v0.29.0 // indirect
k8s.io/klog/v2 v2.110.1 // indirect
k8s.io/kube-openapi v0.0.0-20231010175941-2dd684a91f00 // indirect
k8s.io/klog/v2 v2.130.1 // indirect
k8s.io/kube-openapi v0.0.0-20240228011516-70dd3763d340 // indirect
k8s.io/kubectl v0.29.0 // indirect
oras.land/oras-go v1.2.6 // indirect
sigs.k8s.io/json v0.0.0-20221116044647-bc3834ca7abd // indirect
sigs.k8s.io/kustomize/api v0.13.5-0.20230601165947-6ce0bf390ce3 // indirect
sigs.k8s.io/kustomize/kyaml v0.14.3-0.20230601165947-6ce0bf390ce3 // indirect
sigs.k8s.io/structured-merge-diff/v4 v4.4.1 // indirect
)

Expand Down
Loading

0 comments on commit 6e97384

Please sign in to comment.