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

Add PodDisruptionBudget processor #123

Merged
merged 2 commits into from
Oct 24, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions examples/app/templates/myapp-pdb.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
apiVersion: policy/v1
kind: PodDisruptionBudget
metadata:
name: {{ include "app.fullname" . }}-myapp-pdb
labels:
app: nginx
{{- include "app.labels" . | nindent 4 }}
spec:
minAvailable: {{ .Values.myappPdb.minAvailable }}
maxUnavailable: {{ .Values.myappPdb.maxUnavailable }}
selector:
matchLabels:
app: nginx
{{- include "app.selectorLabels" . | nindent 6 }}
2 changes: 2 additions & 0 deletions examples/app/values.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,8 @@ myapp:
repository: gcr.io/kubebuilder/kube-rbac-proxy
tag: v0.8.0
replicas: 3
myappPdb:
minAvailable: 2
myappService:
ports:
- name: https
Expand Down
9 changes: 6 additions & 3 deletions pkg/app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,16 @@ package app

import (
"context"
"github.com/arttor/helmify/pkg/file"
"github.com/arttor/helmify/pkg/processor/job"
"github.com/arttor/helmify/pkg/processor/statefulset"
"io"
"os"
"os/signal"
"syscall"

"github.com/arttor/helmify/pkg/file"
"github.com/arttor/helmify/pkg/processor/job"
"github.com/arttor/helmify/pkg/processor/poddisruptionbudget"
"github.com/arttor/helmify/pkg/processor/statefulset"

"github.com/sirupsen/logrus"

"github.com/arttor/helmify/pkg/config"
Expand Down Expand Up @@ -64,6 +66,7 @@ func Start(stdin io.Reader, config config.Config) error {
webhook.MutatingWebhook(),
job.NewCron(),
job.NewJob(),
poddisruptionbudget.New(),
).WithDefaultProcessor(processor.Default())
if len(config.Files) != 0 {
file.Walk(config.Files, config.FilesRecursively, func(filename string, fileReader io.Reader) {
Expand Down
107 changes: 107 additions & 0 deletions pkg/processor/poddisruptionbudget/pdb.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
package poddisruptionbudget

import (
"bytes"
"fmt"
"io"

"github.com/arttor/helmify/pkg/processor"

"github.com/arttor/helmify/pkg/helmify"
yamlformat "github.com/arttor/helmify/pkg/yaml"
"github.com/iancoleman/strcase"
policyv1 "k8s.io/api/policy/v1"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/runtime/schema"
"sigs.k8s.io/yaml"
)

const (
pdbTempSpec = `
spec:
minAvailable: {{ .Values.%[1]s.minAvailable }}
maxUnavailable: {{ .Values.%[1]s.maxUnavailable }}
selector:
%[2]s
{{- include "%[3]s.selectorLabels" . | nindent 6 }}`
)

var pdbGVC = schema.GroupVersionKind{
Group: "policy",
Version: "v1",
Kind: "PodDisruptionBudget",
}

// New creates processor for k8s Service resource.
func New() helmify.Processor {
return &pdb{}
}

type pdb struct{}

// Process k8s Service object into template. Returns false if not capable of processing given resource type.
func (r pdb) Process(appMeta helmify.AppMetadata, obj *unstructured.Unstructured) (bool, helmify.Template, error) {
if obj.GroupVersionKind() != pdbGVC {
return false, nil, nil
}
pdb := policyv1.PodDisruptionBudget{}
err := runtime.DefaultUnstructuredConverter.FromUnstructured(obj.Object, &pdb)
if err != nil {
return true, nil, fmt.Errorf("%w: unable to cast to pdb", err)
}
spec := pdb.Spec
values := helmify.Values{}

meta, err := processor.ProcessObjMeta(appMeta, obj)
if err != nil {
return true, nil, err
}

name := appMeta.TrimName(obj.GetName())
nameCamel := strcase.ToLowerCamel(name)

selector, _ := yaml.Marshal(pdb.Spec.Selector)
selector = yamlformat.Indent(selector, 4)
selector = bytes.TrimRight(selector, "\n ")

if spec.MaxUnavailable != nil {
_, err := values.Add(spec.MaxUnavailable.IntValue(), nameCamel, "maxUnavailable")
if err != nil {
return true, nil, err
}
}

if spec.MinAvailable != nil {
_, err := values.Add(spec.MinAvailable.IntValue(), nameCamel, "minAvailable")
if err != nil {
return true, nil, err
}
}

res := meta + fmt.Sprintf(pdbTempSpec, nameCamel, selector, appMeta.ChartName())
return true, &result{
name: name,
data: res,
values: values,
}, nil
}

type result struct {
name string
data string
values helmify.Values
}

func (r *result) Filename() string {
return r.name + ".yaml"
}

func (r *result) Values() helmify.Values {
return r.values
}

func (r *result) Write(writer io.Writer) error {
_, err := writer.Write([]byte(r.data))
return err
}
42 changes: 42 additions & 0 deletions pkg/processor/poddisruptionbudget/pdb_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package poddisruptionbudget

import (
"os"
"testing"

"github.com/arttor/helmify/pkg/metadata"

"github.com/arttor/helmify/internal"
"github.com/stretchr/testify/assert"
)

const pdbYaml = `apiVersion: policy/v1
kind: PodDisruptionBudget
metadata:
labels:
control-plane: controller-manager
name: my-operator-controller-manager-pdb
namespace: my-operator-system
spec:
minAvailable: 2
selector:
matchLabels:
control-plane: controller-manager`

func Test_pdb_Process(t *testing.T) {
var testInstance pdb

t.Run("processed", func(t *testing.T) {
obj := internal.GenerateObj(pdbYaml)
processed, tt, err := testInstance.Process(&metadata.Service{}, obj)
_ = tt.Write(os.Stdout)
assert.NoError(t, err)
assert.Equal(t, true, processed)
})
t.Run("skipped", func(t *testing.T) {
obj := internal.TestNs
processed, _, err := testInstance.Process(&metadata.Service{}, obj)
assert.NoError(t, err)
assert.Equal(t, false, processed)
})
}
12 changes: 12 additions & 0 deletions test_data/sample-app.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -334,3 +334,15 @@ spec:
resources:
requests:
storage: 1Gi
---
apiVersion: policy/v1
kind: PodDisruptionBudget
metadata:
labels:
app: nginx
name: myapp-pdb
spec:
minAvailable: 2
selector:
matchLabels:
app: nginx