-
Notifications
You must be signed in to change notification settings - Fork 109
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[local-path-provisioner] add reclaimPolicy field.
- Loading branch information
1 parent
a963181
commit a48a6c6
Showing
14 changed files
with
257 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -27,3 +27,4 @@ prometheus: | | |
``` | ||
|
||
Wait for the restart of Prometheus Pods. | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
106 changes: 106 additions & 0 deletions
106
modules/031-local-path-provisioner/hooks/storage_classes.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,106 @@ | ||
/* | ||
Copyright 2021 Flant JSC | ||
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 hooks | ||
|
||
import ( | ||
"fmt" | ||
|
||
"github.com/flant/addon-operator/pkg/module_manager/go_hook" | ||
"github.com/flant/addon-operator/sdk" | ||
storagev1 "k8s.io/api/storage/v1" | ||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured" | ||
|
||
"github.com/deckhouse/deckhouse/modules/031-local-path-provisioner/hooks/internal/v1alpha1" | ||
) | ||
|
||
type StorageClass struct { | ||
Name string | ||
ReclaimPolicy string | ||
} | ||
|
||
var _ = sdk.RegisterFunc(&go_hook.HookConfig{ | ||
Queue: "/modules/local-path-provisioner", | ||
OnBeforeHelm: &go_hook.OrderedConfig{Order: 20}, | ||
Kubernetes: []go_hook.KubernetesConfig{ | ||
{ | ||
Name: "module_storageclasses", | ||
ApiVersion: "storage.k8s.io/v1", | ||
Kind: "StorageClass", | ||
LabelSelector: &metav1.LabelSelector{ | ||
MatchLabels: map[string]string{"module": "local-path-provisioner"}, | ||
}, | ||
FilterFunc: applyModuleStorageClassesFilter, | ||
}, | ||
{ | ||
Name: "module_crds", | ||
ApiVersion: "deckhouse.io/v1alpha1", | ||
Kind: "LocalPathProvisioner", | ||
FilterFunc: applyModuleCRDFilter, | ||
}, | ||
}, | ||
}, storageClasses) | ||
|
||
func applyModuleStorageClassesFilter(obj *unstructured.Unstructured) (go_hook.FilterResult, error) { | ||
var sc = &storagev1.StorageClass{} | ||
err := sdk.FromUnstructured(obj, sc) | ||
if err != nil { | ||
return nil, fmt.Errorf("cannot convert kubernetes object: %v", err) | ||
} | ||
|
||
return StorageClass{Name: sc.Name, ReclaimPolicy: string(*sc.ReclaimPolicy)}, nil | ||
} | ||
|
||
func applyModuleCRDFilter(obj *unstructured.Unstructured) (go_hook.FilterResult, error) { | ||
lpp := new(v1alpha1.LocalPathProvisioner) | ||
err := sdk.FromUnstructured(obj, lpp) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
return StorageClass{ | ||
Name: lpp.Name, | ||
ReclaimPolicy: lpp.Spec.ReclaimPolicy, | ||
}, nil | ||
} | ||
|
||
func storageClasses(input *go_hook.HookInput) error { | ||
var existedStorageClasses []StorageClass | ||
|
||
if len(input.Snapshots["module_storageclasses"]) == 0 || len(input.Snapshots["module_crds"]) == 0 { | ||
return nil | ||
} | ||
|
||
for _, snapshot := range input.Snapshots["module_storageclasses"] { | ||
sc := snapshot.(StorageClass) | ||
existedStorageClasses = append(existedStorageClasses, sc) | ||
} | ||
|
||
for _, snapshot := range input.Snapshots["module_crds"] { | ||
crd := snapshot.(StorageClass) | ||
for _, storageClass := range existedStorageClasses { | ||
if storageClass.Name == crd.Name { | ||
if storageClass.ReclaimPolicy != crd.ReclaimPolicy { | ||
input.LogEntry.Infof("Deleting storageclass/%s because its parameters has been changed", storageClass.Name) | ||
input.PatchCollector.Delete("storage.k8s.io/v1", "StorageClass", "", storageClass.Name) | ||
} | ||
break | ||
} | ||
} | ||
} | ||
return nil | ||
} |
83 changes: 83 additions & 0 deletions
83
modules/031-local-path-provisioner/hooks/storage_classes_test.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
/* | ||
Copyright 2021 Flant JSC | ||
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 hooks | ||
|
||
import ( | ||
. "github.com/onsi/ginkgo" | ||
. "github.com/onsi/gomega" | ||
|
||
. "github.com/deckhouse/deckhouse/testing/hooks" | ||
) | ||
|
||
const clusterStorageClasses = ` | ||
--- | ||
apiVersion: storage.k8s.io/v1 | ||
kind: StorageClass | ||
metadata: | ||
labels: | ||
module: local-path-provisioner | ||
name: localpath-worker | ||
reclaimPolicy: Retain | ||
--- | ||
apiVersion: storage.k8s.io/v1 | ||
kind: StorageClass | ||
metadata: | ||
labels: | ||
module: local-path-provisioner | ||
name: localpath-worker2 | ||
reclaimPolicy: Delete | ||
` | ||
|
||
var _ = Describe("Local Path Provisioner hooks :: delete SC when reclaimPolicy in CRD is changed ::", func() { | ||
f := HookExecutionConfigInit(`{"localPathProvisioner":{"internal": {}}}`, "") | ||
f.RegisterCRD("deckhouse.io", "v1alpha1", "LocalPathProvisioner", false) | ||
|
||
Context("Fresh cluster", func() { | ||
BeforeEach(func() { | ||
f.BindingContexts.Set(f.KubeStateSet(clusterStorageClasses)) | ||
f.RunHook() | ||
}) | ||
It("Should run", func() { | ||
Expect(f).To(ExecuteSuccessfully()) | ||
Expect(f.BindingContexts.Array()).ShouldNot(BeEmpty()) | ||
}) | ||
}) | ||
|
||
Context("With adding localPathProvisioner crd", func() { | ||
BeforeEach(func() { | ||
f.BindingContexts.Set(f.KubeStateSet(clusterStorageClasses + ` | ||
--- | ||
apiVersion: deckhouse.io/v1alpha1 | ||
kind: LocalPathProvisioner | ||
metadata: | ||
name: localpath-worker | ||
spec: | ||
nodeGroups: | ||
- worker | ||
path: "/local" | ||
reclaimPolicy: "Delete" | ||
`)) | ||
f.RunHook() | ||
}) | ||
It("Should remove localpath-worker storage class", func() { | ||
Expect(f).To(ExecuteSuccessfully()) | ||
Expect(f.BindingContexts.Array()).ShouldNot(BeEmpty()) | ||
Expect(f.KubernetesGlobalResource("StorageClass", "localpath-worker").Exists()).To(BeFalse()) | ||
Expect(f.KubernetesGlobalResource("StorageClass", "localpath-worker2").Exists()).To(BeTrue()) | ||
}) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters