Skip to content

Commit

Permalink
Merge pull request #13624 from vbotbuildovich/backport-pr-12847-v23.2…
Browse files Browse the repository at this point in the history
….x-447

[v23.2.x] Redpanda-Operator: manage decommission for helm charts
  • Loading branch information
alejandroEsc authored Sep 25, 2023
2 parents 9a95f58 + e6866df commit f9a5db6
Show file tree
Hide file tree
Showing 47 changed files with 1,644 additions and 133 deletions.
2 changes: 1 addition & 1 deletion src/go/k8s/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ FROM --platform=$BUILDPLATFORM public.ecr.aws/docker/library/golang:1.21.0 as bu
ARG TARGETARCH
ARG TARGETOS

# Copy the rpk as a close depedency
# Copy the rpk as a close dependency
WORKDIR /workspace
COPY rpk/ rpk/

Expand Down
7 changes: 4 additions & 3 deletions src/go/k8s/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ NAMESPACE ?= "redpanda"

ifeq (aarch64,$(shell uname -m))
TARGETARCH = arm64
else ifeq (x86_64,$(shell uname -m))
TARGETARCH = amd64
else
TARGETARCH ?= $(shell uname -m)
endif
Expand Down Expand Up @@ -117,7 +119,7 @@ e2e-tests: kuttl test docker-build docker-build-configurator

# Execute end to end tests v2
e2e-tests-v2: kuttl
echo "~~~ Running kuttl tests :k8s:"
echo "~~~ Running kuttl tests :k8s:redpanda"
$(KUTTL) test $(TEST_ONLY_FLAG) --config kuttl-v2-test.yaml $(KUTTL_TEST_FLAGS)

# Execute end to end unstable tests
Expand Down Expand Up @@ -201,11 +203,10 @@ gofumpt-lint: gofumpt-install
goimports: goimports-install
goimports -w .


.PHONY: golangci-lint-install
golangci-lint-install:
mkdir -p $(GOLANGCI_LINT)
curl -sSfL https://raw.githubusercontent.com/golangci/golangci-lint/master/install.sh | sh -s -- -b $(GOLANGCI_LINT) v1.52.2
curl -sSfL https://raw.githubusercontent.com/golangci/golangci-lint/master/install.sh | sh -s -- -b $(GOLANGCI_LINT) v1.54.2

.PHONY: golangci-lint
golangci-lint:
Expand Down
5 changes: 5 additions & 0 deletions src/go/k8s/config/rbac/bases/operator/kustomization.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
apiVersion: kustomize.config.k8s.io/v1beta1
kind: Kustomization
resources:
- role.yaml
- role_binding.yaml
49 changes: 37 additions & 12 deletions src/go/k8s/config/rbac/bases/operator/role.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,6 @@ metadata:
creationTimestamp: null
name: manager-role
rules:
- apiGroups:
- apps
resources:
- configmaps
verbs:
- create
- delete
- get
- list
- patch
- update
- watch
- apiGroups:
- apps
resources:
Expand Down Expand Up @@ -131,6 +119,17 @@ rules:
- get
- list
- watch
- apiGroups:
- ""
resources:
- persistentvolumes
verbs:
- delete
- get
- list
- patch
- update
- watch
- apiGroups:
- ""
resources:
Expand Down Expand Up @@ -328,6 +327,13 @@ rules:
- get
- patch
- update
- apiGroups:
- apps
resources:
- statefulsets/status
verbs:
- patch
- update
- apiGroups:
- batch
resources:
Expand Down Expand Up @@ -441,6 +447,25 @@ rules:
verbs:
- create
- patch
- apiGroups:
- ""
resources:
- persistentvolumeclaims
verbs:
- delete
- get
- list
- patch
- update
- watch
- apiGroups:
- ""
resources:
- pods
verbs:
- get
- list
- watch
- apiGroups:
- ""
resources:
Expand Down
2 changes: 1 addition & 1 deletion src/go/k8s/controllers/redpanda/console_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ const (
)

//+kubebuilder:rbac:groups=apps,resources=deployments,verbs=get;list;watch;create;update;patch;delete
//+kubebuilder:rbac:groups=apps,resources=configmaps,verbs=get;list;watch;create;update;patch;delete
//+kubebuilder:rbac:groups=core,resources=configmaps,verbs=get;list;watch;create;update;patch;delete
//+kubebuilder:rbac:groups=core,resources=events,verbs=get;list;watch;create;update;patch

//+kubebuilder:rbac:groups=redpanda.vectorized.io,resources=consoles,verbs=get;list;watch;create;update;patch;delete
Expand Down
89 changes: 87 additions & 2 deletions src/go/k8s/controllers/redpanda/redpanda_controller_utils.go
Original file line number Diff line number Diff line change
@@ -1,21 +1,94 @@
package redpanda

import (
"context"
"fmt"
"io"
"net"
"net/http"
"os"
"path/filepath"
"time"

"k8s.io/utils/pointer"

"github.com/fluxcd/pkg/runtime/logger"
"github.com/fluxcd/source-controller/controllers"
"github.com/go-logr/logr"
"helm.sh/helm/v3/pkg/action"
"helm.sh/helm/v3/pkg/cli"
"helm.sh/helm/v3/pkg/registry"
corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/types"
"k8s.io/apimachinery/pkg/util/errors"
"k8s.io/utils/pointer"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/event"
"sigs.k8s.io/controller-runtime/pkg/predicate"
)

const (
K8sInstanceLabelKey = "app.kubernetes.io/instance"
K8sNameLabelKey = "app.kubernetes.io/name"
K8sComponentLabelKey = "app.kubernetes.io/component"
K8sManagedByLabelKey = "app.kubernetes.io/managed-by"

EnvHelmReleaseNameKey = "REDPANDA_HELM_RELEASE_NAME"
)

var UpdateEventFilter = predicate.Funcs{
CreateFunc: func(e event.CreateEvent) bool { return false },
UpdateFunc: func(e event.UpdateEvent) bool { return true },
DeleteFunc: func(e event.DeleteEvent) bool { return false },
GenericFunc: func(e event.GenericEvent) bool { return false },
}

var DeleteEventFilter = predicate.Funcs{
CreateFunc: func(e event.CreateEvent) bool { return false },
UpdateFunc: func(e event.UpdateEvent) bool { return false },
DeleteFunc: func(e event.DeleteEvent) bool { return true },
GenericFunc: func(e event.GenericEvent) bool { return false },
}

// Check to see if the release name of a helm chart matches the name of a redpanda object
// this is by design for the operator
func isValidReleaseName(releaseName string, redpandaNameList []string) bool {
for i := range redpandaNameList {
if releaseName == redpandaNameList[i] {
return true
}
}
return false
}

func getHelmValues(log logr.Logger, releaseName, namespace string) (map[string]interface{}, error) {
settings := cli.New()
actionConfig := new(action.Configuration)
if err := actionConfig.Init(settings.RESTClientGetter(), namespace, os.Getenv("HELM_DRIVER"), func(format string, v ...interface{}) { Debugf(log, format, v) }); err != nil {
return nil, fmt.Errorf("could not create action-config for helm driver: %w", err)
}

gv := action.NewGetValues(actionConfig)
gv.AllValues = true

return gv.Run(releaseName)
}

func bestTrySetRetainPV(c client.Client, log logr.Logger, ctx context.Context, name, namespace string) {
log.WithName("RedpandaNodePVCReconciler.bestTrySetRetainPV")
pv := &corev1.PersistentVolume{}
if getErr := c.Get(ctx, types.NamespacedName{Name: name, Namespace: namespace}, pv); getErr != nil {
Infof(log, "could not change retain policy of pv %s", pv.Name)
return
}
// try to set reclaim policy, fail if we cannot set this to avoid data loss
if pv.Spec.PersistentVolumeReclaimPolicy != corev1.PersistentVolumeReclaimRetain {
pv.Spec.PersistentVolumeReclaimPolicy = corev1.PersistentVolumeReclaimRetain
if updateErr := c.Update(ctx, pv); updateErr != nil {
// no need to place error here. we simply move on and not attempt to remove the pv
Infof(log, "could not set reclaim policy for %s; continuing: %s", pv.Name, updateErr.Error())
}
}
}

func ClientGenerator(isLogin bool) (*registry.Client, string, error) {
if isLogin {
// create a temporary file to store the credentials
Expand Down Expand Up @@ -107,3 +180,15 @@ func StartFileServer(path, address string, l logr.Logger) {
func IsBoolPointerNILorEqual(a *bool, b bool) bool {
return a == nil || pointer.BoolEqual(a, pointer.Bool(b))
}

func Infof(log logr.Logger, format string, a ...interface{}) {
log.Info(fmt.Sprintf(format, a...))
}

func Debugf(log logr.Logger, format string, a ...interface{}) {
log.V(logger.DebugLevel).Info(fmt.Sprintf(format, a...))
}

func Tracef(log logr.Logger, format string, a ...interface{}) {
log.V(logger.TraceLevel).Info(fmt.Sprintf(format, a...))
}
Loading

0 comments on commit f9a5db6

Please sign in to comment.