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

EVEREST-1511 | Add binaries (tools) for running in Helm chart hooks #712

Draft
wants to merge 17 commits into
base: main
Choose a base branch
from
Draft
1 change: 1 addition & 0 deletions .dockerignore
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
**
!bin/everest
!bin/helm-tools
!cmd
!migrations
!model
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/dev-be-ci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -263,7 +263,7 @@ jobs:

- name: Build Everest API Server
run: |
CGO_ENABLED=0 GOOS=linux GOARCH=amd64 make build-debug
CGO_ENABLED=0 GOOS=linux GOARCH=amd64 make build-debug build-helm-tools

- name: Build Everest docker container
uses: docker/metadata-action@v5
Expand Down
1 change: 1 addition & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ FROM scratch
WORKDIR /

COPY ./bin/everest /everest-api
COPY ./bin/helm-tools/ /helm-tools
COPY --from=build /etc/ssl/certs/ca-certificates.crt /etc/ssl/certs/

EXPOSE 8080
Expand Down
8 changes: 5 additions & 3 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ LD_FLAGS_API = -ldflags " $(FLAGS) -X 'github.com/percona/everest/pkg/version.Pr
LD_FLAGS_CLI = -ldflags " $(FLAGS) -X 'github.com/percona/everest/pkg/version.ProjectName=everestctl'"
LD_FLAGS_CLI_TEST = -ldflags " $(FLAGS) -X 'github.com/percona/everest/pkg/version.ProjectName=everestctl' \
-X 'github.com/percona/everest/pkg/version.EverestChannelOverride=fast-v0'"

default: help

help: ## Display this help message
Expand All @@ -21,10 +20,13 @@ help: ## Display this help message
init: ## Install development tools
cd tools && go generate -x -tags=tools

build: ## Build binaries
build-helm-tools: ## Build binaries for Helm chart hooks.
cd helm-tools && go generate -x

build: build-helm-tools ## Build binaries
go build -v $(LD_FLAGS_API) -o bin/everest ./cmd

build-cli: ## Build binaries
build-cli: ## Build binaries
go build -tags debug -v $(LD_FLAGS_CLI_TEST) -o bin/everestctl ./cmd/cli

release: FLAGS += -X 'github.com/percona/everest/cmd/config.TelemetryURL=https://check.percona.com' -X 'github.com/percona/everest/cmd/config.TelemetryInterval=24h'
Expand Down
70 changes: 70 additions & 0 deletions helm-tools/installplan-approver/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
// everest
// Copyright (C) 2023 Percona LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

// Package main ...
package main

import (
"context"
"flag"
"time"

"github.com/go-logr/zapr"
ctrlruntimelog "sigs.k8s.io/controller-runtime/pkg/log"

helmutils "github.com/percona/everest/helm-tools/utils"
"github.com/percona/everest/pkg/kubernetes/helm"
"github.com/percona/everest/pkg/logger"
)

var kubeconfigPath string //nolint:gochecknoglobals

func initFlags() {
flag.StringVar(&kubeconfigPath, "kubeconfig", "", "Path to kubeconfig file")
flag.Parse()
}

func main() {
initFlags()
logger := logger.MustInitLogger(false)
defer logger.Sync() //nolint:errcheck
// This is required because controller-runtime requires a logger
// to be set within 30 seconds of the program initialization.
ctrlruntimelog.SetLogger(zapr.NewLogger(logger))

l := logger.Sugar()
kubeClient, err := helmutils.NewClient(l, kubeconfigPath)
if err != nil {
l.Fatal(err)
}
helmInstaller := helm.New(l, kubeClient)
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Minute) //nolint:mnd
defer cancel()

if err := helmInstaller.ApproveEverestMonitoringInstallPlan(ctx); err != nil {
l.Fatalf("Failed to approve the install plan for Everest monitoring: %v", err)
}
l.Info("Installed Everest monitoring successfully")

if err := helmInstaller.ApproveEverestOperatorInstallPlan(ctx); err != nil {
l.Fatalf("Failed to approve the install plan Everest operator: %v", err)
}
l.Info("Installed Everest operator successfully")

if err := helmInstaller.ApproveDBNamespacesInstallPlans(ctx); err != nil {
l.Fatalf("Failed to approve the install plan(s) for DB namespaces: %v", err)
}
l.Info("Installed Everest DB namespaces successfully")
}
20 changes: 20 additions & 0 deletions helm-tools/tools.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
// everest
// Copyright (C) 2023 Percona LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

// Package helmtools provides a set of tools for working with Everest Helm charts.
package helmtools

//go:generate go build -o ../bin/helm-tools/installplan-approver ./installplan-approver
//go:generate go build -o ../bin/helm-tools/uninstall ./uninstall
73 changes: 73 additions & 0 deletions helm-tools/uninstall/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
// everest
// Copyright (C) 2023 Percona LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

// Package main ...
package main

import (
"context"
"flag"
"time"

"github.com/go-logr/zapr"
ctrlruntimelog "sigs.k8s.io/controller-runtime/pkg/log"

helmutils "github.com/percona/everest/helm-tools/utils"
"github.com/percona/everest/pkg/kubernetes/helm"
"github.com/percona/everest/pkg/logger"
)

//nolint:gochecknoglobals
var (
kubeconfigPath string
deleteDBs bool
)

func initFlags() {
flag.StringVar(&kubeconfigPath, "kubeconfig", "", "Path to kubeconfig file")
flag.BoolVar(&deleteDBs, "delete-dbs", false, "If set, force deletes all existing DBs in the cluster")
flag.Parse()
}

func main() {
initFlags()
logger := logger.MustInitLogger(false)
defer logger.Sync() //nolint:errcheck
// This is required because controller-runtime requires a logger
// to be set within 30 seconds of the program initialization.
ctrlruntimelog.SetLogger(zapr.NewLogger(logger))

l := logger.Sugar()
kubeClient, err := helmutils.NewClient(l, kubeconfigPath)
if err != nil {
l.Fatal(err)
}
helmInstaller := helm.New(l, kubeClient)
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Minute) //nolint:mnd
defer cancel()

if deleteDBs {
l.Info("Deleting all existing DBs in the cluster")
if err := helmInstaller.DeleteAllDatabaseClusters(ctx); err != nil {
l.Fatalf("Failed to delete all existing DBs: %v", err)
}
}

// Before the OLM namespace is terminated by helm uninstall, we must first
// delete the PackageServer CSV so that OLM is uninstalled gracefully, and does not get stuck.
if err := helmInstaller.DeleteOLM(ctx); err != nil {
l.Fatalf("Failed to delete OLM: %v", err)
}
}
32 changes: 32 additions & 0 deletions helm-tools/utils/utils.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
// everest
// Copyright (C) 2023 Percona LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

// Package helmutils provides common helpers for helm tools.
package helmutils

import (
"go.uber.org/zap"

"github.com/percona/everest/pkg/kubernetes"
)

// NewClient returns a new Kubernetes client.
// If kubeconfigPath is empty, it will use the in-cluster configuration.
func NewClient(l *zap.SugaredLogger, kubeconfigPath string) (kubernetes.KubernetesConnector, error) { //nolint:ireturn
if kubeconfigPath != "" {
return kubernetes.New(kubeconfigPath, l)
}
return kubernetes.NewInCluster(l)
}
2 changes: 2 additions & 0 deletions pkg/common/constants.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ const (
PerconaEverestOperatorDeploymentName = "everest-operator-controller-manager"
// EverestContainerNameInDeployment is the name of the Everest container in the deployment.
EverestContainerNameInDeployment = "everest"
// VictoriaMetricsOperatorName is the name of the VictoriaMetrics operator.
VictoriaMetricsOperatorName = "victoriametrics-operator"

// EverestOperatorName holds the name for Everest operator.
EverestOperatorName = "everest-operator"
Expand Down
9 changes: 4 additions & 5 deletions pkg/install/install.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,6 @@ type Install struct {
}

const (
vmOperatorName = "victoriametrics-operator"
operatorInstallThreads = 1

everestServiceAccount = "everest-admin"
Expand Down Expand Up @@ -358,11 +357,11 @@ func (o *Install) installVMOperator(ctx context.Context) error {
if err := o.kubeClient.CreateOperatorGroup(ctx, monitoringOperatorGroup, MonitoringNamespace, []string{}); err != nil {
return err
}
o.l.Infof("Installing %s operator", vmOperatorName)
o.l.Infof("Installing %s operator", common.VictoriaMetricsOperatorName)

params := kubernetes.InstallOperatorRequest{
Namespace: MonitoringNamespace,
Name: vmOperatorName,
Name: common.VictoriaMetricsOperatorName,
OperatorGroup: monitoringOperatorGroup,
CatalogSource: catalogSource,
CatalogSourceNamespace: kubernetes.OLMNamespace,
Expand All @@ -371,10 +370,10 @@ func (o *Install) installVMOperator(ctx context.Context) error {
}

if err := o.kubeClient.InstallOperator(ctx, params); err != nil {
o.l.Errorf("failed installing %s operator", vmOperatorName)
o.l.Errorf("failed installing %s operator", common.VictoriaMetricsOperatorName)
return err
}
o.l.Infof("%s operator has been installed", vmOperatorName)
o.l.Infof("%s operator has been installed", common.VictoriaMetricsOperatorName)
return nil
}

Expand Down
Loading
Loading