From 584d43a132322260f94857a793fc6bbb4fe87ea1 Mon Sep 17 00:00:00 2001 From: Niels de Vos Date: Fri, 1 Oct 2021 15:05:56 +0200 Subject: [PATCH] deploy: move rbd/CSIDriver to API Signed-off-by: Niels de Vos --- api/deploy/kubernetes/doc.go | 20 +++++ api/deploy/kubernetes/rbd/csidriver.go | 74 +++++++++++++++++++ api/deploy/kubernetes/rbd/csidriver.yaml | 10 +++ api/deploy/kubernetes/rbd/csidriver_test.go | 38 ++++++++++ api/go.mod | 1 + deploy/Makefile | 7 +- deploy/rbd/kubernetes/csidriver.yaml | 10 ++- tools/yamlgen/main.go | 25 ++++--- .../api/deploy/kubernetes/rbd/csidriver.go | 74 +++++++++++++++++++ .../api/deploy/kubernetes/rbd/csidriver.yaml | 10 +++ vendor/modules.txt | 1 + 11 files changed, 258 insertions(+), 12 deletions(-) create mode 100644 api/deploy/kubernetes/doc.go create mode 100644 api/deploy/kubernetes/rbd/csidriver.go create mode 100644 api/deploy/kubernetes/rbd/csidriver.yaml create mode 100644 api/deploy/kubernetes/rbd/csidriver_test.go create mode 100644 vendor/github.com/ceph/ceph-csi/api/deploy/kubernetes/rbd/csidriver.go create mode 100644 vendor/github.com/ceph/ceph-csi/api/deploy/kubernetes/rbd/csidriver.yaml diff --git a/api/deploy/kubernetes/doc.go b/api/deploy/kubernetes/doc.go new file mode 100644 index 000000000..e4bd824b1 --- /dev/null +++ b/api/deploy/kubernetes/doc.go @@ -0,0 +1,20 @@ +/* +Copyright 2021 The Ceph-CSI Authors. + +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 kubernetes contains functions to obtain standard and recommended +// deployment artifacts for Kubernetes. These artifacts can be used by +// automation tools that want to deploy Ceph-CSI. +package kubernetes diff --git a/api/deploy/kubernetes/rbd/csidriver.go b/api/deploy/kubernetes/rbd/csidriver.go new file mode 100644 index 000000000..2708e98a3 --- /dev/null +++ b/api/deploy/kubernetes/rbd/csidriver.go @@ -0,0 +1,74 @@ +/* +Copyright 2021 The Ceph-CSI Authors. + +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 rbd + +import ( + "bytes" + _ "embed" + "fmt" + "text/template" + + "github.com/ghodss/yaml" + storagev1 "k8s.io/api/storage/v1" +) + +//go:embed csidriver.yaml +var csiDriver string + +type CSIDriverValues struct { + Name string +} + +var CSIDriverDefaults = CSIDriverValues{ + Name: "rbd.csi.ceph.com", +} + +// NewCSIDriver takes a driver name from the CSIDriverValues struct and relaces +// the value in the template. A CSIDriver object is returned which can be +// created in the Kubernetes cluster. +func NewCSIDriver(values CSIDriverValues) (*storagev1.CSIDriver, error) { + data, err := NewCSIDriverYAML(values) + if err != nil { + return nil, err + } + + driver := &storagev1.CSIDriver{} + err = yaml.Unmarshal([]byte(data), driver) + if err != nil { + return nil, fmt.Errorf("failed convert YAML to %T: %w", driver, err) + } + + return driver, nil +} + +// NewCSIDriverYAML takes a driver name from the CSIDriverValues struct and relaces +// the value in the template. A CSIDriver object in YAML is returned which can be +// created in the Kubernetes cluster. +func NewCSIDriverYAML(values CSIDriverValues) (string, error) { + var buf bytes.Buffer + + tmpl, err := template.New("CSIDriver").Parse(csiDriver) + if err != nil { + return "", fmt.Errorf("failed to parse template: %w", err) + } + err = tmpl.Execute(&buf, values) + if err != nil { + return "", fmt.Errorf("failed to replace values in template: %w", err) + } + + return buf.String(), nil +} diff --git a/api/deploy/kubernetes/rbd/csidriver.yaml b/api/deploy/kubernetes/rbd/csidriver.yaml new file mode 100644 index 000000000..4ab105dcc --- /dev/null +++ b/api/deploy/kubernetes/rbd/csidriver.yaml @@ -0,0 +1,10 @@ +--- +# if Kubernetes version is less than 1.18 change +# apiVersion to storage.k8s.io/v1beta1 +apiVersion: storage.k8s.io/v1 +kind: CSIDriver +metadata: + name: "{{ .Name }}" +spec: + attachRequired: true + podInfoOnMount: false diff --git a/api/deploy/kubernetes/rbd/csidriver_test.go b/api/deploy/kubernetes/rbd/csidriver_test.go new file mode 100644 index 000000000..b3f6e8236 --- /dev/null +++ b/api/deploy/kubernetes/rbd/csidriver_test.go @@ -0,0 +1,38 @@ +/* +Copyright 2021 The Ceph-CSI Authors. + +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 rbd + +import ( + "testing" + + "github.com/stretchr/testify/require" +) + +func TestNewCSIDriver(t *testing.T) { + driver, err := NewCSIDriver(CSIDriverDefaults) + + require.NoError(t, err) + require.NotNil(t, driver) + require.Equal(t, driver.Name, CSIDriverDefaults.Name) +} + +func TestNewCSIDriverYAML(t *testing.T) { + yaml, err := NewCSIDriverYAML(CSIDriverDefaults) + + require.NoError(t, err) + require.NotEqual(t, "", yaml) +} diff --git a/api/go.mod b/api/go.mod index faa19817c..8db6eff6b 100644 --- a/api/go.mod +++ b/api/go.mod @@ -6,4 +6,5 @@ require ( github.com/ghodss/yaml v1.0.0 github.com/openshift/api v0.0.0-20210927171657-636513e97fda github.com/stretchr/testify v1.7.0 + k8s.io/api v0.22.1 ) diff --git a/deploy/Makefile b/deploy/Makefile index 02fb2e4c4..d68bb8af5 100644 --- a/deploy/Makefile +++ b/deploy/Makefile @@ -13,7 +13,12 @@ # limitations under the License. .PHONY: all -all: scc.yaml +all: \ + scc.yaml \ + rbd/kubernetes/csidriver.yaml scc.yaml: ../api/deploy/ocp/scc.yaml ../api/deploy/ocp/scc.go $(MAKE) -C ../tools generate-deploy + +rbd/kubernetes/csidriver.yaml: ../api/deploy/kubernetes/rbd/csidriver.yaml ../api/deploy/kubernetes/rbd/csidriver.go + $(MAKE) -C ../tools generate-deploy diff --git a/deploy/rbd/kubernetes/csidriver.yaml b/deploy/rbd/kubernetes/csidriver.yaml index 18ab2bc77..ef0eb1390 100644 --- a/deploy/rbd/kubernetes/csidriver.yaml +++ b/deploy/rbd/kubernetes/csidriver.yaml @@ -1,10 +1,18 @@ --- +# +# /!\ DO NOT MODIFY THIS FILE +# +# This file has been automatically generated by Ceph-CSI yamlgen. +# The source for the contents can be found in the api/deploy directory, make +# your modifications there. +# +--- # if Kubernetes version is less than 1.18 change # apiVersion to storage.k8s.io/v1beta1 apiVersion: storage.k8s.io/v1 kind: CSIDriver metadata: - name: rbd.csi.ceph.com + name: "rbd.csi.ceph.com" spec: attachRequired: true podInfoOnMount: false diff --git a/tools/yamlgen/main.go b/tools/yamlgen/main.go index ae1c9bc5f..5bdfd5a4e 100644 --- a/tools/yamlgen/main.go +++ b/tools/yamlgen/main.go @@ -19,7 +19,9 @@ package main import ( "fmt" "os" + "reflect" + "github.com/ceph/ceph-csi/api/deploy/kubernetes/rbd" "github.com/ceph/ceph-csi/api/deploy/ocp" ) @@ -35,18 +37,20 @@ const header = `--- type deploymentArtifact struct { filename string - // FIXME: This is not dynamic enough for additional YAML generating - // functions. Need to look into typecasting the functions and passing - // interface{} instead of ocp.SecurityContextConstraintsValues. - yamlFunc func(ocp.SecurityContextConstraintsValues) (string, error) - defaults ocp.SecurityContextConstraintsValues + yamlFunc reflect.Value + defaults reflect.Value } var yamlArtifacts = []deploymentArtifact{ { "../deploy/scc.yaml", - ocp.NewSecurityContextConstraintsYAML, - ocp.SecurityContextConstraintsDefaults, + reflect.ValueOf(ocp.NewSecurityContextConstraintsYAML), + reflect.ValueOf(ocp.SecurityContextConstraintsDefaults), + }, + { + "../deploy/rbd/kubernetes/csidriver.yaml", + reflect.ValueOf(rbd.NewCSIDriverYAML), + reflect.ValueOf(rbd.CSIDriverDefaults), }, } @@ -69,9 +73,10 @@ func writeArtifact(artifact deploymentArtifact) { panic(fmt.Sprintf("failed to write header to %q: %v", artifact.filename, err)) } - data, err := artifact.yamlFunc(artifact.defaults) - if err != nil { - panic(fmt.Sprintf("failed to generate YAML for %q: %v", artifact.filename, err)) + result := artifact.yamlFunc.Call([]reflect.Value{artifact.defaults}) + data := result[0].String() + if data == "" { + panic(fmt.Sprintf("failed to generate YAML for %q: %v", artifact.filename, result[1].String())) } _, err = f.WriteString(data) diff --git a/vendor/github.com/ceph/ceph-csi/api/deploy/kubernetes/rbd/csidriver.go b/vendor/github.com/ceph/ceph-csi/api/deploy/kubernetes/rbd/csidriver.go new file mode 100644 index 000000000..2708e98a3 --- /dev/null +++ b/vendor/github.com/ceph/ceph-csi/api/deploy/kubernetes/rbd/csidriver.go @@ -0,0 +1,74 @@ +/* +Copyright 2021 The Ceph-CSI Authors. + +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 rbd + +import ( + "bytes" + _ "embed" + "fmt" + "text/template" + + "github.com/ghodss/yaml" + storagev1 "k8s.io/api/storage/v1" +) + +//go:embed csidriver.yaml +var csiDriver string + +type CSIDriverValues struct { + Name string +} + +var CSIDriverDefaults = CSIDriverValues{ + Name: "rbd.csi.ceph.com", +} + +// NewCSIDriver takes a driver name from the CSIDriverValues struct and relaces +// the value in the template. A CSIDriver object is returned which can be +// created in the Kubernetes cluster. +func NewCSIDriver(values CSIDriverValues) (*storagev1.CSIDriver, error) { + data, err := NewCSIDriverYAML(values) + if err != nil { + return nil, err + } + + driver := &storagev1.CSIDriver{} + err = yaml.Unmarshal([]byte(data), driver) + if err != nil { + return nil, fmt.Errorf("failed convert YAML to %T: %w", driver, err) + } + + return driver, nil +} + +// NewCSIDriverYAML takes a driver name from the CSIDriverValues struct and relaces +// the value in the template. A CSIDriver object in YAML is returned which can be +// created in the Kubernetes cluster. +func NewCSIDriverYAML(values CSIDriverValues) (string, error) { + var buf bytes.Buffer + + tmpl, err := template.New("CSIDriver").Parse(csiDriver) + if err != nil { + return "", fmt.Errorf("failed to parse template: %w", err) + } + err = tmpl.Execute(&buf, values) + if err != nil { + return "", fmt.Errorf("failed to replace values in template: %w", err) + } + + return buf.String(), nil +} diff --git a/vendor/github.com/ceph/ceph-csi/api/deploy/kubernetes/rbd/csidriver.yaml b/vendor/github.com/ceph/ceph-csi/api/deploy/kubernetes/rbd/csidriver.yaml new file mode 100644 index 000000000..4ab105dcc --- /dev/null +++ b/vendor/github.com/ceph/ceph-csi/api/deploy/kubernetes/rbd/csidriver.yaml @@ -0,0 +1,10 @@ +--- +# if Kubernetes version is less than 1.18 change +# apiVersion to storage.k8s.io/v1beta1 +apiVersion: storage.k8s.io/v1 +kind: CSIDriver +metadata: + name: "{{ .Name }}" +spec: + attachRequired: true + podInfoOnMount: false diff --git a/vendor/modules.txt b/vendor/modules.txt index 8b0257647..46b538f17 100644 --- a/vendor/modules.txt +++ b/vendor/modules.txt @@ -51,6 +51,7 @@ github.com/blang/semver github.com/cenkalti/backoff/v3 # github.com/ceph/ceph-csi/api v0.0.0-00010101000000-000000000000 => ./api ## explicit +github.com/ceph/ceph-csi/api/deploy/kubernetes/rbd github.com/ceph/ceph-csi/api/deploy/ocp # github.com/ceph/go-ceph v0.11.0 ## explicit