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

feat(provisioner-localpv): Merge CAS config from PersistentVolumeClaim #190

Merged
merged 4 commits into from
Jul 15, 2024
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
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ testv: format
# Requires KUBECONFIG env and Ginkgo binary
.PHONY: integration-test
integration-test:
@cd tests && sudo -E env "PATH=${PATH}" ginkgo -v -failFast
@cd tests && sudo -E env "PATH=${PATH}" ginkgo -v --fail-fast

.PHONY: format
format:
Expand Down
1 change: 1 addition & 0 deletions changelogs/unreleased/190-nobiit
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Add feature to merge cas-config from PVC
48 changes: 19 additions & 29 deletions cmd/provisioner-localpv/app/config.go
Original file line number Diff line number Diff line change
@@ -1,20 +1,3 @@
/*
Copyright 2019 The OpenEBS 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 app

import (
Expand All @@ -26,7 +9,7 @@ import (
cast "github.com/openebs/maya/pkg/castemplate/v1alpha1"
hostpath "github.com/openebs/maya/pkg/hostpath/v1alpha1"
"github.com/openebs/maya/pkg/util"
errors "github.com/pkg/errors"
"github.com/pkg/errors"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/klog/v2"
Expand Down Expand Up @@ -143,7 +126,7 @@ const (
)

// GetVolumeConfig creates a new VolumeConfig struct by
// parsing and merging the configuration provided in the PVC
// parsing and merging the configuration provided in the PVC/SC
// annotation - cas.openebs.io/config with the
// default configuration of the provisioner.
func (p *Provisioner) GetVolumeConfig(ctx context.Context, pvName string, pvc *corev1.PersistentVolumeClaim) (*VolumeConfig, error) {
Expand All @@ -169,16 +152,23 @@ func (p *Provisioner) GetVolumeConfig(ctx context.Context, pvName string, pvc *c
}
}

//TODO : extract and merge the cas volume config from pvc
//This block can be added once validation checks are added
// as to the type of config that can be passed via PVC
//pvcCASConfigStr := pvc.ObjectMeta.Annotations[string(mconfig.CASConfigKey)]
//if len(strings.TrimSpace(pvcCASConfigStr)) != 0 {
// pvcCASConfig, err := cast.UnMarshallToConfig(pvcCASConfigStr)
// if err == nil {
// pvConfig = cast.MergeConfig(pvcCASConfig, pvConfig)
// }
//}
// Extract and merge the cas config from persistentvolumeclaim.
// TODO: Validation checks for what all cas-config options can be
// set on the PVC.
pvcCASConfigStr := pvc.Annotations[string(mconfig.CASConfigKey)]
klog.V(4).Infof("PVC %v has config:%v", pvc.Name, pvcCASConfigStr)
if len(strings.TrimSpace(pvcCASConfigStr)) != 0 {
pvcCASConfig, err := cast.UnMarshallToConfig(pvcCASConfigStr)
if err == nil {
// Config keys which already exist (SC config),
// will be skipped
// i.e. SC config will have precedence over PVC config,
// if both have the same keys
pvConfig = cast.MergeConfig(pvConfig, pvcCASConfig)
} else {
return nil, errors.Wrapf(err, "failed to get config: invalid pvc config {%v}", pvcCASConfigStr)
}
}

pvConfigMap, err := cast.ConfigToMap(pvConfig)
if err != nil {
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ require (
k8s.io/client-go v0.27.2
k8s.io/klog/v2 v2.100.1
sigs.k8s.io/sig-storage-lib-external-provisioner/v9 v9.0.3
sigs.k8s.io/yaml v1.3.0
)

require (
Expand Down Expand Up @@ -84,7 +85,6 @@ require (
k8s.io/utils v0.0.0-20230505201702-9f6742963106 // indirect
sigs.k8s.io/json v0.0.0-20221116044647-bc3834ca7abd // indirect
sigs.k8s.io/structured-merge-diff/v4 v4.2.3 // indirect
sigs.k8s.io/yaml v1.3.0 // indirect
)

replace (
Expand Down
31 changes: 16 additions & 15 deletions pkg/kubernetes/api/apps/v1/deployment/deployment.go
Original file line number Diff line number Diff line change
@@ -1,24 +1,12 @@
/*
Copyright 2019 The OpenEBS 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 v1alpha1

import (
templatespec "github.com/openebs/dynamic-localpv-provisioner/pkg/kubernetes/api/core/v1/podtemplatespec"
stringer "github.com/openebs/maya/pkg/apis/stringer/v1alpha1"
errors "github.com/pkg/errors"
"github.com/pkg/errors"
appsv1 "k8s.io/api/apps/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"

templatespec "github.com/openebs/dynamic-localpv-provisioner/pkg/kubernetes/api/core/v1/podtemplatespec"
)

// Predicate abstracts conditional logic w.r.t the deployment instance
Expand Down Expand Up @@ -96,6 +84,19 @@ func (b *Builder) WithName(name string) *Builder {
return b
}

// WithGenerateName sets the Name field of deployment with a random value with the provided value as a prefix.
func (b *Builder) WithGenerateName(prefix string) *Builder {
if len(prefix) == 0 {
b.errors = append(
b.errors,
errors.New("failed to build deployment: missing prefix for generateName"),
)
return b
}
b.deployment.object.GenerateName = prefix + "-"
return b
}

// WithNamespace sets the Namespace field of deployment with provided value.
func (b *Builder) WithNamespace(namespace string) *Builder {
if len(namespace) == 0 {
Expand Down
20 changes: 2 additions & 18 deletions pkg/kubernetes/api/core/v1/persistentvolumeclaim/build.go
Original file line number Diff line number Diff line change
@@ -1,23 +1,7 @@
/*
Copyright 2020 The OpenEBS 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 persistentvolumeclaim

import (
errors "github.com/pkg/errors"
"github.com/pkg/errors"
corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/api/resource"
)
Expand Down Expand Up @@ -72,7 +56,7 @@ func (b *Builder) WithGenerateName(name string) *Builder {
return b
}

b.pvc.object.GenerateName = name
b.pvc.object.GenerateName = name + "-"
return b
}

Expand Down
34 changes: 18 additions & 16 deletions pkg/kubernetes/api/storage/v1/storageclass/build.go
Original file line number Diff line number Diff line change
@@ -1,19 +1,3 @@
/*
Copyright 2019 The OpenEBS 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 storageclass

import (
Expand Down Expand Up @@ -96,6 +80,24 @@ func WithLabels(labels map[string]string) StorageClassOption {
}
}

func WithAnnotations(annotations map[string]string) StorageClassOption {
return func(s *storagev1.StorageClass) error {
if len(annotations) == 0 {
return errors.New("Failed to set Annotations. " +
"Input is invalid.")
}

if s.ObjectMeta.Annotations == nil {
s.ObjectMeta.Annotations = map[string]string{}
}
for key, value := range annotations {
s.ObjectMeta.Annotations[key] = value
}

return nil
}
}

func WithParameters(parameters map[string]string) StorageClassOption {
return func(s *storagev1.StorageClass) error {
if len(parameters) == 0 {
Expand Down
43 changes: 22 additions & 21 deletions tests/operations.go
Original file line number Diff line number Diff line change
@@ -1,26 +1,9 @@
/*
Copyright 2019 The OpenEBS 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 tests

import (
"bytes"
"context"
"fmt"

//"sort"
"strconv"
"strings"
Expand Down Expand Up @@ -50,15 +33,15 @@ import (
"k8s.io/client-go/tools/remotecommand"

deploy "github.com/openebs/dynamic-localpv-provisioner/pkg/kubernetes/api/apps/v1/deployment"
container "github.com/openebs/dynamic-localpv-provisioner/pkg/kubernetes/api/core/v1/container"
event "github.com/openebs/dynamic-localpv-provisioner/pkg/kubernetes/api/core/v1/event"
"github.com/openebs/dynamic-localpv-provisioner/pkg/kubernetes/api/core/v1/container"
"github.com/openebs/dynamic-localpv-provisioner/pkg/kubernetes/api/core/v1/event"
pv "github.com/openebs/dynamic-localpv-provisioner/pkg/kubernetes/api/core/v1/persistentvolume"
pvc "github.com/openebs/dynamic-localpv-provisioner/pkg/kubernetes/api/core/v1/persistentvolumeclaim"
pod "github.com/openebs/dynamic-localpv-provisioner/pkg/kubernetes/api/core/v1/pod"
"github.com/openebs/dynamic-localpv-provisioner/pkg/kubernetes/api/core/v1/pod"
pts "github.com/openebs/dynamic-localpv-provisioner/pkg/kubernetes/api/core/v1/podtemplatespec"
k8svolume "github.com/openebs/dynamic-localpv-provisioner/pkg/kubernetes/api/core/v1/volume"
sc "github.com/openebs/dynamic-localpv-provisioner/pkg/kubernetes/api/storage/v1/storageclass"
ndmconfig "github.com/openebs/dynamic-localpv-provisioner/pkg/kubernetes/ndmconfig"
"github.com/openebs/dynamic-localpv-provisioner/pkg/kubernetes/ndmconfig"
)

const (
Expand Down Expand Up @@ -476,6 +459,24 @@ func (ops *Operations) GetPVNameFromPVCName(namespace, pvcName string) string {
return p.Spec.VolumeName
}

// GetNodeAffinityLabelKeysFromPv returns the label keys for NodeSelector MatchExpressions in a PV.
func (ops *Operations) GetNodeAffinityLabelKeysFromPv(pvName string) ([]string, error) {
pv, err := ops.PVClient.Get(context.TODO(), pvName, metav1.GetOptions{})
if err != nil {
return nil, err
}

var nodeAffinityLabelKeys []string

for _, selectorTerm := range pv.Spec.NodeAffinity.Required.NodeSelectorTerms {
for _, matchExpression := range selectorTerm.MatchExpressions {
nodeAffinityLabelKeys = append(nodeAffinityLabelKeys, matchExpression.Key)
}
}

return nodeAffinityLabelKeys, nil
}

// isNotFound returns true if the original
// cause of error was due to castemplate's
// not found error or kubernetes not found
Expand Down
Loading
Loading