-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathcommonconfig.go
125 lines (109 loc) · 3.87 KB
/
commonconfig.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
// Copyright © 2022 Dell Inc. or its subsidiaries. All Rights Reserved.
//
// 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 modules
import (
"context"
"fmt"
"os"
"path/filepath"
"strings"
csmv1 "github.com/dell/csm-operator/api/v1"
utils "github.com/dell/csm-operator/pkg/utils"
crclient "sigs.k8s.io/controller-runtime/pkg/client"
)
const (
// DefaultPluginIdentifier - spring placeholder for driver plugin
DefaultPluginIdentifier = "<DriverPluginIdentifier>"
// DefaultDriverConfigParamsVolumeMount - string placeholder for Driver ConfigParamsVolumeMount
DefaultDriverConfigParamsVolumeMount = "<DriverConfigParamsVolumeMount>"
// CertManagerManifest -
CertManagerManifest = "cert-manager.yaml"
// CommonNamespace -
CommonNamespace = "<NAMESPACE>"
// CSMName - name
CSMName = "<NAME>"
)
// SupportedDriverParam -
type SupportedDriverParam struct {
PluginIdentifier string
DriverConfigParamsVolumeMount string
}
func checkVersion(moduleType, givenVersion, configPath string) error {
files, err := os.ReadDir(fmt.Sprintf("%s/moduleconfig/%s/", configPath, moduleType))
if err != nil {
return err
}
found := false
supportedVersions := ""
for _, file := range files {
supportedVersions += (file.Name() + ",")
if file.Name() == givenVersion {
found = true
}
}
if !found {
return fmt.Errorf(
"CSM %s does not have %s version. The following are supported versions: %s",
moduleType, givenVersion, supportedVersions[:len(supportedVersions)-1],
)
}
return nil
}
func readConfigFile(module csmv1.Module, cr csmv1.ContainerStorageModule, op utils.OperatorConfig, filename string) ([]byte, error) {
var err error
moduleConfigVersion := module.ConfigVersion
if moduleConfigVersion == "" {
moduleConfigVersion, err = utils.GetModuleDefaultVersion(cr.Spec.Driver.ConfigVersion, cr.Spec.Driver.CSIDriverType, module.Name, op.ConfigDirectory)
if err != nil {
return nil, err
}
}
configMapPath := fmt.Sprintf("%s/moduleconfig/%s/%s/%s", op.ConfigDirectory, module.Name, moduleConfigVersion, filename)
return os.ReadFile(filepath.Clean(configMapPath))
}
// getCertManager - configure cert-manager with the specified namespace before installation
func getCertManager(op utils.OperatorConfig, cr csmv1.ContainerStorageModule) (string, error) {
YamlString := ""
certManagerPath := fmt.Sprintf("%s/moduleconfig/common/cert-manager/%s", op.ConfigDirectory, CertManagerManifest)
buf, err := os.ReadFile(filepath.Clean(certManagerPath))
if err != nil {
return YamlString, err
}
YamlString = string(buf)
certNamespace := cr.Namespace
YamlString = strings.ReplaceAll(YamlString, CommonNamespace, certNamespace)
YamlString = strings.ReplaceAll(YamlString, CSMName, cr.Name)
return YamlString, nil
}
// CommonCertManager - apply/delete cert-manager objects
func CommonCertManager(ctx context.Context, isDeleting bool, op utils.OperatorConfig, cr csmv1.ContainerStorageModule, ctrlClient crclient.Client) error {
YamlString, err := getCertManager(op, cr)
if err != nil {
return err
}
ctrlObjects, err := utils.GetModuleComponentObj([]byte(YamlString))
if err != nil {
return err
}
for _, ctrlObj := range ctrlObjects {
if isDeleting {
if err := utils.DeleteObject(ctx, ctrlObj, ctrlClient); err != nil {
return err
}
} else {
if err := utils.ApplyObject(ctx, ctrlObj, ctrlClient); err != nil {
return err
}
}
}
return nil
}