Skip to content
This repository has been archived by the owner on Jun 29, 2022. It is now read-only.

openebs-storage-class: Move to Helm Chart #1559

Merged
merged 1 commit into from
Sep 8, 2021
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
23 changes: 23 additions & 0 deletions assets/charts/components/openebs-storage-class/.helmignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# Patterns to ignore when building packages.
# This supports shell glob matching, relative path matching, and
# negation (prefixed with !). Only one pattern per line.
.DS_Store
# Common VCS dirs
.git/
.gitignore
.bzr/
.bzrignore
.hg/
.hgignore
.svn/
# Common backup files
*.swp
*.bak
*.tmp
*.orig
*~
# Various IDEs
.project
.idea/
*.tmproj
.vscode/
8 changes: 8 additions & 0 deletions assets/charts/components/openebs-storage-class/Chart.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
apiVersion: v2
name: openebs-storage-class
description: A Helm chart for Kubernetes
type: application
version: 0.1.0

# This should match the value of the openebs-operator helm chart.
appVersion: "2.10.0"
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
{{- range .Values.storageClasses }}
---
apiVersion: storage.k8s.io/v1
kind: StorageClass
metadata:
name: {{ .name }}
annotations:
openebs.io/cas-type: cstor
storageclass.kubernetes.io/is-default-class: "{{ .default }}"
cas.openebs.io/config: |
- name: StoragePoolClaim
value: "cstor-pool-{{ .name }}"
- name: ReplicaCount
value: "{{ .replicaCount }}"
provisioner: openebs.io/provisioner-iscsi
reclaimPolicy: "{{ .reclaimPolicy }}"
---
apiVersion: openebs.io/v1alpha1
kind: StoragePoolClaim
metadata:
name: cstor-pool-{{ .name }}
spec:
name: cstor-pool-{{ .name }}
type: disk
maxPools: 3
poolSpec:
poolType: striped
{{- if .disks }}
blockDevices:
blockDeviceList:
{{range .disks -}}
- {{.}}
{{end}}
{{- end }}
---
{{end}}
15 changes: 15 additions & 0 deletions assets/charts/components/openebs-storage-class/values.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
storageClasses:
- name: test
default: false
reclaimPolicy: Retain
replicaCount: 1
# disks:
# - foo
# - bar
# - name: notest
# default: false
# reclaimPolicy: Retain
# replicaCount: 1
# disks:
# - baz
# - zab
46 changes: 46 additions & 0 deletions pkg/assets/generated_assets.go

Large diffs are not rendered by default.

37 changes: 10 additions & 27 deletions pkg/components/openebs-storage-class/component.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,14 @@
package openebsstorageclass

import (
"bytes"
"fmt"
"text/template"

"github.com/hashicorp/hcl/v2"
"github.com/hashicorp/hcl/v2/gohcl"

"github.com/kinvolk/lokomotive/internal/template"
"github.com/kinvolk/lokomotive/pkg/components"
"github.com/kinvolk/lokomotive/pkg/components/util"
"github.com/kinvolk/lokomotive/pkg/k8sutil"
)

Expand Down Expand Up @@ -117,39 +117,22 @@ func (c *component) validateConfig() error {

// TODO: Convert to Helm chart.
func (c *component) RenderManifests() (map[string]string, error) {

scTmpl, err := template.New(Name).Parse(storageClassTmpl)
helmChart, err := components.Chart(Name)
if err != nil {
return nil, fmt.Errorf("parsing storage class template: %w", err)
return nil, fmt.Errorf("retrieving chart from assets: %w", err)
}

spTmpl, err := template.New(poolName).Parse(storagePoolTmpl)
values, err := template.Render(chartValuesTmpl, c)
if err != nil {
return nil, fmt.Errorf("parsing storage pool template: %w", err)
return nil, fmt.Errorf("render chart values template: %w", err)
}

var manifestsMap = make(map[string]string)

for _, sc := range c.StorageClasses {
var scBuffer bytes.Buffer
var spBuffer bytes.Buffer

if err := scTmpl.Execute(&scBuffer, sc); err != nil {
return nil, fmt.Errorf("executing storage class %q template: %w", sc.Name, err)
}

filename := fmt.Sprintf("%s-%s.yml", Name, sc.Name)
manifestsMap[filename] = scBuffer.String()

if err := spTmpl.Execute(&spBuffer, sc); err != nil {
return nil, fmt.Errorf("executing storage pool %q template: %w", sc.Name, err)
}

filename = fmt.Sprintf("%s-%s.yml", poolName, sc.Name)
manifestsMap[filename] = spBuffer.String()
renderedFiles, err := util.RenderChart(helmChart, Name, c.Metadata().Namespace.Name, values)
if err != nil {
return nil, fmt.Errorf("render chart: %w", err)
}

return manifestsMap, nil
return renderedFiles, nil
}

func (c *component) Metadata() components.Metadata {
Expand Down
101 changes: 101 additions & 0 deletions pkg/components/openebs-storage-class/component_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
package openebsstorageclass

import (
"fmt"
"testing"

"github.com/hashicorp/hcl/v2"
Expand Down Expand Up @@ -148,3 +149,103 @@ func TestConversion(t *testing.T) {
})
}
}

func TestFullConversion(t *testing.T) { //nolint:funlen
config := `component "openebs-storage-class" {
storage-class "replica1-no-disk-selected" {
replica_count = 1
}
storage-class "replica1" {
disks = ["disk1"]
replica_count = 1
}
storage-class "replica3" {
replica_count = 3
default = true
disks = ["disk2","disk3","disk4"]
}
}`
component := NewConfig()
m := testutil.RenderManifests(t, component, Name, config)

testCases := []struct {
name string
fn func(t *testing.T)
}{
{
name: "replica1-no-disk-selected-sc",
fn: func(t *testing.T) {
got := testutil.ConfigFromMap(t, m, k8sutil.ObjectMetadata{
Version: "storage.k8s.io/v1", Kind: "StorageClass", Name: "replica1-no-disk-selected",
})

expected := `- name: StoragePoolClaim
value: "cstor-pool-replica1-no-disk-selected"
- name: ReplicaCount
value: "1"
`
testutil.MatchJSONPathStringValue(t, got, "{.metadata.annotations.cas\\.openebs\\.io/config}", expected)
},
},
{
name: "replica1-no-disk-selected-spc",
fn: func(t *testing.T) {
got := testutil.ConfigFromMap(t, m, k8sutil.ObjectMetadata{
Version: "openebs.io/v1alpha1", Kind: "StoragePoolClaim", Name: "cstor-pool-replica1-no-disk-selected",
})

testutil.JSONPathExists(t, got, "{.spec.blockDevices}", "blockDevices is not found")
},
},
{
name: "replica1-verify-disks",
fn: func(t *testing.T) {
got := testutil.ConfigFromMap(t, m, k8sutil.ObjectMetadata{
Version: "openebs.io/v1alpha1", Kind: "StoragePoolClaim", Name: "cstor-pool-replica1",
})

expected := "disk1"
testutil.MatchJSONPathStringValue(t, got, "{.spec.blockDevices.blockDeviceList[0]}", expected)
},
},
{
name: "replica3-verify-disks",
fn: func(t *testing.T) {
got := testutil.ConfigFromMap(t, m, k8sutil.ObjectMetadata{
Version: "openebs.io/v1alpha1", Kind: "StoragePoolClaim", Name: "cstor-pool-replica3",
})

expected := []string{"disk2", "disk3", "disk4"}

for idx, exp := range expected {
jpath := fmt.Sprintf("{.spec.blockDevices.blockDeviceList[%d]}", idx)
testutil.MatchJSONPathStringValue(t, got, jpath, exp)
}
},
},
{
name: "replica3-sc",
fn: func(t *testing.T) {
got := testutil.ConfigFromMap(t, m, k8sutil.ObjectMetadata{
Version: "storage.k8s.io/v1", Kind: "StorageClass", Name: "replica3",
})

expected := "true"
jpath := "{.metadata.annotations.storageclass\\.kubernetes\\.io/is-default-class}"
testutil.MatchJSONPathStringValue(t, got, jpath, expected)

expected = `- name: StoragePoolClaim
value: "cstor-pool-replica3"
- name: ReplicaCount
value: "3"
`
testutil.MatchJSONPathStringValue(t, got, "{.metadata.annotations.cas\\.openebs\\.io/config}", expected)
},
},
}

for _, tc := range testCases {
tc := tc
t.Run(tc.name, tc.fn)
}
}
49 changes: 14 additions & 35 deletions pkg/components/openebs-storage-class/template.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,39 +14,18 @@

package openebsstorageclass

const storageClassTmpl = `
apiVersion: storage.k8s.io/v1
kind: StorageClass
metadata:
name: {{ .Name }}
annotations:
openebs.io/cas-type: cstor
storageclass.kubernetes.io/is-default-class: "{{ .Default }}"
cas.openebs.io/config: |
- name: StoragePoolClaim
value: "cstor-pool-{{ .Name }}"
- name: ReplicaCount
value: "{{ .ReplicaCount }}"
provisioner: openebs.io/provisioner-iscsi
reclaimPolicy: "{{ .ReclaimPolicy }}"
`

const storagePoolTmpl = `
apiVersion: openebs.io/v1alpha1
kind: StoragePoolClaim
metadata:
name: cstor-pool-{{ .Name }}
spec:
name: cstor-pool-{{ .Name }}
type: disk
maxPools: 3
poolSpec:
poolType: striped
{{- if .Disks }}
blockDevices:
blockDeviceList:
{{range .Disks -}}
- {{.}}
{{end}}
{{- end }}
var chartValuesTmpl = `
storageClasses:
{{ range .StorageClasses -}}
- name: {{.Name}}
default: {{.Default}}
reclaimPolicy: {{.ReclaimPolicy}}
replicaCount: {{.ReplicaCount}}
{{if .Disks}}
disks:
{{ range .Disks -}}
- {{.}}
{{end}}
{{end}}
{{end}}
`