Skip to content

Commit fc5aebd

Browse files
committed
feat: standardize NFS storage class configuration and logging
- Add a new default NFS storage class constant - Refactor log messages to use a common default storage class name - Remove unused variable from the NFS function implementation - Update the NFS deployment script to use new variable names for IP and path - Introduce a new template file for NFS storage class and service account configuration - Modify the Kubernetes status collector to utilize the new default storage class names Signed-off-by: ysicing <i@ysicing.me>
1 parent 7f2f73d commit fc5aebd

5 files changed

Lines changed: 152 additions & 24 deletions

File tree

cmd/storage/storage.go

Lines changed: 20 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ import (
2323
"github.com/easysoft/qcadmin/internal/pkg/util/log/survey"
2424

2525
qcexec "github.com/easysoft/qcadmin/internal/pkg/util/exec"
26+
kubeerr "k8s.io/apimachinery/pkg/api/errors"
2627
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
2728
)
2829

@@ -93,7 +94,7 @@ func local(f factory.Factory) *cobra.Command {
9394
logpkg.Errorf("upgrade install local storage failed: %s", string(output))
9495
return err
9596
}
96-
logpkg.Infof("install local storage class %s (%s) success", color.SGreen("q-local"), color.SGreen(path))
97+
logpkg.Infof("install local storage class %s (%s) success", color.SGreen(common.DefaultLocalStorageClass), color.SGreen(path))
9798
return nil
9899
},
99100
}
@@ -102,13 +103,23 @@ func local(f factory.Factory) *cobra.Command {
102103
}
103104

104105
func nfs(f factory.Factory) *cobra.Command {
105-
var ip, path, name string
106+
var ip, path string
106107
logpkg := f.GetLog()
107108
cmd := &cobra.Command{
108109
Use: "nfs",
109110
Short: "deploy nfs storage",
110111
Example: nfsExample,
111112
PreRunE: func(cmd *cobra.Command, args []string) error {
113+
kclient, _ := k8s.NewSimpleClient()
114+
_, err := kclient.GetNamespace(context.TODO(), common.DefaultStorageNamespace, metav1.GetOptions{})
115+
if err != nil {
116+
if !kubeerr.IsNotFound(err) {
117+
return err
118+
}
119+
if _, err := kclient.CreateNamespace(context.TODO(), common.DefaultStorageNamespace, metav1.CreateOptions{}); err != nil && kubeerr.IsAlreadyExists(err) {
120+
return err
121+
}
122+
}
112123
if len(ip) == 0 && len(path) == 0 {
113124
an, err := logpkg.Question(&survey.QuestionOptions{
114125
Question: "nfs server ip is empty, install local nfs",
@@ -139,23 +150,22 @@ func nfs(f factory.Factory) *cobra.Command {
139150
return errors.Errorf("nfs server ip or path is empty")
140151
},
141152
RunE: func(cmd *cobra.Command, args []string) error {
142-
// TODO check install repo exist
143-
if err := qcexec.Command(os.Args[0], "experimental", "helm", "repo-init").Run(); err != nil {
144-
logpkg.Warnf("check helm repo failed: %v", err)
153+
if err := qcexec.CommandRun("bash", "-c", fmt.Sprintf("%s %s %s", common.GetCustomFile("hack/manifests/storage/nfs.sh"), ip, path)); err != nil {
154+
return errors.Errorf("precheck nfs storage failed, reason: %v", err)
145155
}
146-
helmargs := []string{"experimental", "helm", "upgrade", "--name", name, "--repo", common.DefaultHelmRepoName, "--chart", "nfs-subdir-external-provisioner", "--namespace", common.DefaultStorageNamespace, "--set", "nfs.server=" + ip, "--set", "nfs.path=" + path, "--set", "storageClass.name=" + name}
147-
output, err := qcexec.Command(os.Args[0], helmargs...).CombinedOutput()
156+
yamlfile := common.GetCustomFile("hack/manifests/storage/nfs.deploy.yaml")
157+
kubeargs := []string{"experimental", "kubectl", "apply", "-f", yamlfile, "--namespace", common.DefaultStorageNamespace}
158+
output, err := qcexec.Command(os.Args[0], kubeargs...).CombinedOutput()
148159
if err != nil {
149-
logpkg.Errorf("upgrade install nfs failed: %s", string(output))
160+
logpkg.Errorf("upgrade install nfs storage failed: %s", string(output))
150161
return err
151162
}
152-
logpkg.Infof("install nfs storage class %s (%s:%s) success", color.SGreen(name), color.SGreen(ip), color.SGreen(path))
163+
logpkg.Infof("install nfs storage class %s (%s:%s) success", color.SGreen(common.DefaultNFSStorageClass), color.SGreen(ip), color.SGreen(path))
153164
return nil
154165
},
155166
}
156167
cmd.Flags().StringVar(&ip, "ip", os.Getenv("NFS_SERVER"), "cloud cfs/nas ip")
157168
cmd.Flags().StringVar(&path, "path", os.Getenv("NFS_PATH"), "cloud cfs/nas path")
158-
cmd.Flags().StringVar(&name, "name", "q-nfs", "storage class name")
159169
return cmd
160170
}
161171

common/const.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,8 @@ const (
9898
DefaultNerdctlConfig = "/etc/nerdctl/nerdctl.toml"
9999
DefaultExternalDBPort = 3306
100100
DefaultExternalDBName = "ex-zentaopaas-mysql"
101+
DefaultNFSStorageClass = "q-nfs"
102+
DefaultLocalStorageClass = "q-local"
101103
)
102104

103105
const (

hack/manifests/storage/nfs.sh

Lines changed: 7 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,12 @@
11
#!/usr/bin/env bash
22

3-
ip=${1:-127.0.0.1}
4-
path=${2:-/opt/quickon/storage/nfs}
3+
nfsIP=${1:-127.0.0.1}
4+
nfsPath=${2:-/opt/quickon/storage/nfs}
55

6-
kubectl get sc | grep q-nfs >/dev/null 2>&1 && exit 0
6+
sourcePath=$(dirname "$0")/nfs.yaml.tpl
7+
targetPath=$(dirname "$0")/nfs.deploy.yaml
78

8-
helm repo add install https://hub.zentao.net/chartrepo/stable
9+
cp -a "${sourcePath}" "${targetPath}"
910

10-
helm repo update
11-
12-
helm upgrade -i q-nfs install/nfs-subdir-external-provisioner \
13-
-n quickon-storage \
14-
--set nfs.server=${ip} \
15-
--set nfs.path=${path} \
16-
--set storageClass.defaultClass=true \
17-
--set storageClass.name=q-nfs
11+
sed -i "s|__NFS_IP__|${nfsIP}|g" "${targetPath}"
12+
sed -i "s|__NFS_PATH__|${nfsPath}|g" "${targetPath}"
Lines changed: 121 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,121 @@
1+
---
2+
apiVersion: v1
3+
kind: ServiceAccount
4+
metadata:
5+
name: q-nfs-nfs-subdir-external-provisioner
6+
namespace: quickon-storage
7+
---
8+
apiVersion: storage.k8s.io/v1
9+
kind: StorageClass
10+
metadata:
11+
name: q-nfs
12+
annotations:
13+
storageclass.kubernetes.io/is-default-class: "true"
14+
provisioner: cluster.local/q-nfs-nfs-subdir-external-provisioner
15+
allowVolumeExpansion: true
16+
reclaimPolicy: Delete
17+
volumeBindingMode: Immediate
18+
parameters:
19+
archiveOnDelete: "true"
20+
---
21+
# Source: nfs-subdir-external-provisioner/templates/clusterrole.yaml
22+
kind: ClusterRole
23+
apiVersion: rbac.authorization.k8s.io/v1
24+
metadata:
25+
name: q-nfs-nfs-subdir-external-provisioner-runner
26+
rules:
27+
- apiGroups: [""]
28+
resources: ["nodes"]
29+
verbs: ["get", "list", "watch"]
30+
- apiGroups: [""]
31+
resources: ["persistentvolumes"]
32+
verbs: ["get", "list", "watch", "create", "delete"]
33+
- apiGroups: [""]
34+
resources: ["persistentvolumeclaims"]
35+
verbs: ["get", "list", "watch", "update"]
36+
- apiGroups: ["storage.k8s.io"]
37+
resources: ["storageclasses"]
38+
verbs: ["get", "list", "watch"]
39+
- apiGroups: [""]
40+
resources: ["events"]
41+
verbs: ["create", "update", "patch"]
42+
---
43+
kind: ClusterRoleBinding
44+
apiVersion: rbac.authorization.k8s.io/v1
45+
metadata:
46+
name: run-q-nfs-nfs-subdir-external-provisioner
47+
subjects:
48+
- kind: ServiceAccount
49+
name: q-nfs-nfs-subdir-external-provisioner
50+
namespace: quickon-storage
51+
roleRef:
52+
kind: ClusterRole
53+
name: q-nfs-nfs-subdir-external-provisioner-runner
54+
apiGroup: rbac.authorization.k8s.io
55+
---
56+
kind: Role
57+
apiVersion: rbac.authorization.k8s.io/v1
58+
metadata:
59+
name: leader-locking-q-nfs-nfs-subdir-external-provisioner
60+
rules:
61+
- apiGroups: [""]
62+
resources: ["endpoints"]
63+
verbs: ["get", "list", "watch", "create", "update", "patch"]
64+
---
65+
kind: RoleBinding
66+
apiVersion: rbac.authorization.k8s.io/v1
67+
metadata:
68+
name: leader-locking-q-nfs-nfs-subdir-external-provisioner
69+
subjects:
70+
- kind: ServiceAccount
71+
name: q-nfs-nfs-subdir-external-provisioner
72+
namespace: quickon-storage
73+
roleRef:
74+
kind: Role
75+
name: leader-locking-q-nfs-nfs-subdir-external-provisioner
76+
apiGroup: rbac.authorization.k8s.io
77+
---
78+
apiVersion: apps/v1
79+
kind: Deployment
80+
metadata:
81+
name: q-nfs-nfs-subdir-external-provisioner
82+
namespace: quickon-storage
83+
spec:
84+
replicas: 1
85+
strategy:
86+
type: Recreate
87+
selector:
88+
matchLabels:
89+
app: nfs-subdir-external-provisioner
90+
release: q-nfs
91+
template:
92+
metadata:
93+
annotations:
94+
labels:
95+
app: nfs-subdir-external-provisioner
96+
release: q-nfs
97+
spec:
98+
serviceAccountName: q-nfs-nfs-subdir-external-provisioner
99+
securityContext:
100+
{}
101+
containers:
102+
- name: nfs-subdir-external-provisioner
103+
image: hub.zentao.net/app/nfs-subdir-external-provisioner:v4.0.2
104+
imagePullPolicy: IfNotPresent
105+
securityContext:
106+
{}
107+
volumeMounts:
108+
- name: nfs-subdir-external-provisioner-root
109+
mountPath: /persistentvolumes
110+
env:
111+
- name: PROVISIONER_NAME
112+
value: cluster.local/q-nfs-nfs-subdir-external-provisioner
113+
- name: NFS_SERVER
114+
value: __NFS_IP__
115+
- name: NFS_PATH
116+
value: __NFS_PATH__
117+
volumes:
118+
- name: nfs-subdir-external-provisioner-root
119+
nfs:
120+
server: __NFS_IP__
121+
path: __NFS_PATH__

internal/pkg/status/k8s.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -163,9 +163,9 @@ func (k *K8sStatusCollector) serviceStatus(ctx context.Context, status *Status)
163163
if len(k.cfg.Install.Version) == 0 {
164164
name = "local-path-provisioner"
165165
}
166-
k.deploymentStatus(ctx, "kube-system", name, "q-local", "k8s", status)
166+
k.deploymentStatus(ctx, "kube-system", name, common.DefaultLocalStorageClass, "k8s", status)
167167
} else if k.cfg.Storage.Type == "nfs" {
168-
k.deploymentStatus(ctx, "kube-system", "q-nfs-nfs-subdir-external-provisioner", "q-nfs", "k8s", status)
168+
k.deploymentStatus(ctx, "kube-system", "q-nfs-nfs-subdir-external-provisioner", common.DefaultNFSStorageClass, "k8s", status)
169169
}
170170
// 业务层
171171
k.deploymentStatus(ctx, common.GetDefaultSystemNamespace(true), common.GetReleaseName(k.cfg.Quickon.DevOps), common.GetReleaseName(k.cfg.Quickon.DevOps), "", status)

0 commit comments

Comments
 (0)