Skip to content

Commit

Permalink
Added helm post install hook (#401)
Browse files Browse the repository at this point in the history
  • Loading branch information
chatton authored Jan 31, 2022
1 parent 10346ab commit 7e25f8d
Show file tree
Hide file tree
Showing 8 changed files with 153 additions and 6 deletions.
2 changes: 1 addition & 1 deletion .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ issues:
- gochecknoglobals
- wrapcheck
- nlreturn
- path: main.go
- path: cmd/manager/main.go
linters:
- gochecknoglobals
- wrapcheck
Expand Down
2 changes: 1 addition & 1 deletion .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# checkout other hooks in https://pre-commit.com/hooks.html
repos:
- repo: https://github.com/golangci/golangci-lint
rev: v1.36.0
rev: v1.44.0
hooks:
- id: golangci-lint
- repo: https://github.com/shellcheck-py/shellcheck-py
Expand Down
2 changes: 1 addition & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ COPY go.sum go.sum
RUN go mod download

# Copy the go source & git info
COPY main.go main.go
COPY cmd/manager/main.go cmd/manager/main.go
COPY .git/ .git/
COPY pkg/ pkg/
COPY Makefile Makefile
Expand Down
36 changes: 36 additions & 0 deletions Dockerfile.post-install
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
# Build the manager binary
FROM golang:1.17 as builder

ENV CGO_ENABLED=0
ENV GOOS=linux
ENV GOARCH=amd64

WORKDIR /workspace
# Copy the Go Modules manifests
COPY go.mod go.mod
COPY go.sum go.sum
# 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 go mod download

# Copy the go source
COPY cmd/post-install/main.go cmd/post-install/main.go
COPY pkg/ pkg/
COPY Makefile Makefile
COPY hack/licenses licenses

RUN make post-install-hook

FROM busybox

LABEL name="MongoDB Atlas Operator Post Install Hook" \
maintainer="support@mongodb.com" \
vendor="MongoDB" \
release="1" \
summary="MongoDB Atlas Operator Post Install Hook Image"

WORKDIR /
COPY --from=builder /workspace/bin/helm-post-install .
COPY hack/licenses licenses

ENTRYPOINT ["/helm-post-install"]
9 changes: 7 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -80,11 +80,11 @@ e2e: run-kind ## Run e2e test. Command `make e2e focus=cluster-ns` run cluster-n
.PHONY: manager
manager: export PRODUCT_VERSION=$(shell git describe --tags --dirty --broken)
manager: generate fmt vet ## Build manager binary
go build -o bin/manager -ldflags="-X main.version=$(PRODUCT_VERSION)" main.go
go build -o bin/manager -ldflags="-X main.version=$(PRODUCT_VERSION)" cmd/manager/main.go

.PHONY: run
run: generate fmt vet manifests ## Run against the configured Kubernetes cluster in ~/.kube/config
go run ./main.go
go run ./cmd/manager/main.go

.PHONY: uninstall
uninstall: manifests kustomize ## Uninstall CRDs from a cluster
Expand Down Expand Up @@ -171,3 +171,8 @@ clear-atlas: export INPUT_ATLAS_PUBLIC_KEY=$(shell grep "ATLAS_PUBLIC_KEY" .actr
clear-atlas: export INPUT_ATLAS_PRIVATE_KEY=$(shell grep "ATLAS_PRIVATE_KEY" .actrc | cut -d "=" -f 2)
clear-atlas: ## Clear Atlas organization
bash .github/actions/cleanup/entrypoint.sh

.PHONY: post-install-hook
post-install-hook:
GOARCH=amd64 GOOS=linux CGO_ENABLED=0 go build -o bin/helm-post-install cmd/post-install/main.go
chmod +x bin/helm-post-install
File renamed without changes.
106 changes: 106 additions & 0 deletions cmd/post-install/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
package main

import (
"context"
"os"
"time"

"go.uber.org/zap"

mdbv1 "github.com/mongodb/mongodb-atlas-kubernetes/pkg/api/v1"
"github.com/mongodb/mongodb-atlas-kubernetes/pkg/api/v1/status"
"github.com/mongodb/mongodb-atlas-kubernetes/pkg/util/kube"

corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/runtime/schema"
"k8s.io/client-go/rest"
"sigs.k8s.io/controller-runtime/pkg/client"
)

const (
pollingInterval = time.Second * 10
pollingDuration = time.Minute * 20
)

func setupLogger() *zap.SugaredLogger {
log, err := zap.NewDevelopment()
if err != nil {
zap.S().Errorf("Error building logger config: %s", err)
os.Exit(1)
}

return log.Sugar()
}

// createK8sClient creates an in cluster client which can be used to fetch the current state of the AtlasCluster
// resource.
func createK8sClient() (client.Client, error) {
restCfg, err := rest.InClusterConfig()
if err != nil {
return nil, err
}

k8sClient, err := client.New(restCfg, client.Options{})

if err != nil {
return nil, err
}

k8sClient.Scheme().AddKnownTypes(schema.GroupVersion{Group: "atlas.mongodb.com", Version: "v1"}, &mdbv1.AtlasCluster{}, &mdbv1.AtlasClusterList{})
return k8sClient, nil
}

// isClusterReady returns a boolean indicating if the cluster has reached the ready state and is
// ready to be used.
func isClusterReady(logger *zap.SugaredLogger) (bool, error) {
k8sClient, err := createK8sClient()
if err != nil {
return false, err
}

ticker := time.NewTicker(pollingInterval)
defer ticker.Stop()

clusterName := os.Getenv("CLUSTER_NAME")
namespace := os.Getenv("NAMESPACE")

totalTime := time.Duration(0)
for range ticker.C {
if totalTime > pollingDuration {
break
}
totalTime += pollingInterval

atlasCluster := mdbv1.AtlasCluster{}
if err := k8sClient.Get(context.TODO(), kube.ObjectKey(namespace, clusterName), &atlasCluster); err != nil {
return false, err
}

// the atlas project has reached the ClusterReady state.
for _, cond := range atlasCluster.Status.Conditions {
if cond.Type == status.ClusterReadyType {
if cond.Status == corev1.ConditionTrue {
return true, nil
}
logger.Infof("Atlas Cluster %s is not yet ready", atlasCluster.Name)
}
}
}
return false, nil
}

func main() {
logger := setupLogger()

clusterIsReady, err := isClusterReady(logger)
if err != nil {
logger.Error(err)
os.Exit(1)
}

exitCode := 1
if clusterIsReady {
exitCode = 0
}
os.Exit(exitCode)
}
2 changes: 1 addition & 1 deletion helm-charts

0 comments on commit 7e25f8d

Please sign in to comment.