Skip to content
This repository was archived by the owner on Jul 22, 2024. It is now read-only.

Commit c1e1bad

Browse files
author
Dong Ying Bo
committed
initial version
1 parent 112f217 commit c1e1bad

File tree

10 files changed

+1055
-1
lines changed

10 files changed

+1055
-1
lines changed

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
.vscode
2+
main
3+
k8s-volume-utils

README.md

Lines changed: 42 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,43 @@
11
# k8s-volume-utils
2-
simple utils for k8s pv/pvc
2+
A simple tool to reuse PV. It can rename a PVC or move a PVC from one namespace to another namespace. After that we can resue bound PV without data lost
3+
4+
# Usage:
5+
```
6+
k8s-volume-utils pvc
7+
It is used to rename a PVC object in kubernetes cluster.
8+
It will delete original PVC and create new PVC referring to same PV.
9+
New PVC can be in another namespace.
10+
k8s-volume-utils pv
11+
It can help to reuse a pv
12+
13+
Usage:
14+
k8s-volume-utils [command]
15+
16+
Available Commands:
17+
help Help about any command
18+
pv help to reuse a pv
19+
pvc rename PVC object in kubernetes clusterd
20+
21+
```
22+
# Example
23+
1. create two namespaces for test
24+
`oc new-project pvc`
25+
`oc new-project pvc1`
26+
`oc project pvc `
27+
2. create a statefulset and it will create a PV.
28+
- Edit sts-pvc-test.yaml to update its `storageClassName` to available one on your cluster.
29+
- `oc create -f sts-pvc-test.yaml`
30+
- `oc get po www-pvc-test-0 -w ` to wait pod running. Then
31+
```
32+
$ oc exec pvc-test-0 cat /usr/share/nginx/html/a.txt
33+
a
34+
```
35+
We can see there is data in PV.
36+
3. delete the statefulset by `oc delete -f sts-pvc-test.yaml`. Now we have a pvc `www-pvc-test-0` to be reused by other service.
37+
4. move pvc to namespace pvc1 by `go run main.go pvc www-pvc-test-0 "" pvc1`
38+
`oc get pvc -n pvc1` to check PVC `www-pvc-test-0` is moved to namespace pvc1.
39+
5. create another statefulset to reuse the same PV.
40+
- Edit sts-pvc-test.yaml
41+
Change `"echo a > /usr/share/nginx/html/a.txt;sleep 3600"` to `"echo b > /usr/share/nginx/html/b.txt;sleep 3600"`
42+
- `oc -n pvc1 create -f sts-pvc-test.yaml`
43+
- when pod `pvc-test-0 ` is running in namespace `pvc1`. Run `oc exec pvc-test-0 cat /usr/share/nginx/html/a.txt -n pvc1` to check data is not lost.

cmd/pv.go

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
/*
2+
Copyright © 2020 NAME HERE <EMAIL ADDRESS>
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
package cmd
17+
18+
import (
19+
"os"
20+
21+
"github.com/IBM/k8s-volume-utils/pkg/kubeutils"
22+
"github.com/spf13/cobra"
23+
"k8s.io/klog"
24+
)
25+
26+
// pvCmd represents the pv command
27+
var pvCmd = &cobra.Command{
28+
Use: "pv",
29+
Short: "help to reuse a pv",
30+
Long: `
31+
k8s-volume-utils pv retain "pvName": change pv reclaim policy to retain
32+
k8s-volume-utils pv resue "pvName": change pv status to available so that it is ready to be bound to pvc
33+
34+
`,
35+
Args: cobra.ExactArgs(2),
36+
Run: func(cmd *cobra.Command, args []string) {
37+
if args[0] == "retain" {
38+
err := kubeutils.RetainPV(args[1])
39+
if err != nil {
40+
os.Exit(1)
41+
}
42+
} else if args[0] == "reuse" {
43+
err := kubeutils.ReusePV(args[1])
44+
if err != nil {
45+
os.Exit(1)
46+
}
47+
48+
} else {
49+
klog.Error("invalid argument. should be retain or reuse")
50+
os.Exit(1)
51+
}
52+
},
53+
}
54+
55+
func init() {
56+
rootCmd.AddCommand(pvCmd)
57+
58+
// Here you will define your flags and configuration settings.
59+
60+
// Cobra supports Persistent Flags which will work for this command
61+
// and all subcommands, e.g.:
62+
// pvCmd.PersistentFlags().String("foo", "", "A help for foo")
63+
64+
// Cobra supports local flags which will only run when this command
65+
// is called directly, e.g.:
66+
// pvCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle")
67+
}

cmd/pvc.go

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
//
2+
// Copyright 2020 IBM Corporation
3+
//
4+
// Licensed under the Apache License, Version 2.0 (the "License");
5+
// you may not use this file except in compliance with the License.
6+
// You may obtain a copy of the License at
7+
//
8+
// http://www.apache.org/licenses/LICENSE-2.0
9+
//
10+
// Unless required by applicable law or agreed to in writing, software
11+
// distributed under the License is distributed on an "AS IS" BASIS,
12+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
// See the License for the specific language governing permissions and
14+
// limitations under the License.
15+
//
16+
17+
package cmd
18+
19+
import (
20+
"os"
21+
22+
"github.com/IBM/k8s-volume-utils/pkg/kubeutils"
23+
"github.com/spf13/cobra"
24+
)
25+
26+
// pvcCmd represents the pvc command
27+
var pvcCmd = &cobra.Command{
28+
Use: "pvc",
29+
Short: "rename PVC object in kubernetes clusterd",
30+
Long: `
31+
Examples:
32+
k8s-volume-utils pvc "name" "targetName" "targetNamespace", "namespace"
33+
k8s-volume-utils pvc "name" "targetName" "targetNamespace"
34+
k8s-volume-utils pvc "name" "targetName"
35+
Paramethers:
36+
name: pvc name to be renamed
37+
targetName: pvc name to be renamed to
38+
targetNamespace: namespace of new pvc
39+
namespace: namespace of the source pvc
40+
41+
`,
42+
Args: cobra.RangeArgs(2, 4),
43+
Run: func(cmd *cobra.Command, args []string) {
44+
var err error
45+
if len(args) == 4 {
46+
err = kubeutils.RenamePVC(args[0], args[1], args[2], args[3])
47+
48+
}
49+
if len(args) == 3 {
50+
err = kubeutils.RenamePVC(args[0], args[1], args[2], "")
51+
52+
}
53+
if len(args) == 2 {
54+
err = kubeutils.RenamePVC(args[0], args[1], "", "")
55+
56+
}
57+
if err != nil {
58+
os.Exit(1)
59+
}
60+
},
61+
}
62+
63+
func init() {
64+
rootCmd.AddCommand(pvcCmd)
65+
66+
// Here you will define your flags and configuration settings.
67+
68+
// Cobra supports Persistent Flags which will work for this command
69+
// and all subcommands, e.g.:
70+
// pvcCmd.PersistentFlags().String("foo", "", "A help for foo")
71+
72+
// Cobra supports local flags which will only run when this command
73+
// is called directly, e.g.:
74+
// pvcCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle")
75+
}

cmd/root.go

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
//
2+
// Copyright 2020 IBM Corporation
3+
//
4+
// Licensed under the Apache License, Version 2.0 (the "License");
5+
// you may not use this file except in compliance with the License.
6+
// You may obtain a copy of the License at
7+
//
8+
// http://www.apache.org/licenses/LICENSE-2.0
9+
//
10+
// Unless required by applicable law or agreed to in writing, software
11+
// distributed under the License is distributed on an "AS IS" BASIS,
12+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
// See the License for the specific language governing permissions and
14+
// limitations under the License.
15+
//
16+
17+
package cmd
18+
19+
import (
20+
"flag"
21+
"fmt"
22+
"os"
23+
24+
"github.com/IBM/k8s-volume-utils/pkg/kubeutils"
25+
"github.com/spf13/cobra"
26+
"k8s.io/cli-runtime/pkg/genericclioptions"
27+
"k8s.io/klog"
28+
cmdutil "k8s.io/kubectl/pkg/cmd/util"
29+
)
30+
31+
// rootCmd represents the base command when called without any subcommands
32+
var rootCmd = &cobra.Command{
33+
Use: "k8s-volume-utils",
34+
Short: "it is used to rename PVC object or help to reuse pv",
35+
Long: `
36+
k8s-volume-utils pvc
37+
It is used to rename a PVC object in kubernetes cluster.
38+
It will delete original PVC and create new PVC referring to same PV.
39+
New PVC can be in another namespace.
40+
k8s-volume-utils pv
41+
It can help to reuse a pv
42+
`,
43+
// Uncomment the following line if your bare application
44+
// has an action associated with it:
45+
// Run: func(cmd *cobra.Command, args []string) { },
46+
}
47+
48+
// Execute adds all child commands to the root command and sets flags appropriately.
49+
// This is called by main.main(). It only needs to happen once to the rootCmd.
50+
func Execute() {
51+
klog.InitFlags(nil)
52+
if err := rootCmd.Execute(); err != nil {
53+
fmt.Println(err)
54+
os.Exit(1)
55+
}
56+
}
57+
func init() {
58+
flags := rootCmd.PersistentFlags()
59+
60+
kubeConfigFlags := genericclioptions.NewConfigFlags(true).WithDeprecatedPasswordFlag()
61+
kubeConfigFlags.AddFlags(flags)
62+
matchVersionKubeConfigFlags := cmdutil.NewMatchVersionFlags(kubeConfigFlags)
63+
matchVersionKubeConfigFlags.AddFlags(rootCmd.PersistentFlags())
64+
65+
rootCmd.PersistentFlags().AddGoFlagSet(flag.CommandLine)
66+
67+
f := cmdutil.NewFactory(matchVersionKubeConfigFlags)
68+
var namespace string
69+
var err error
70+
namespace, _, err = f.ToRawKubeConfigLoader().Namespace()
71+
if err != nil {
72+
klog.Errorf("fail to get default namespace: %s", err.Error())
73+
}
74+
75+
client, e := f.KubernetesClientSet()
76+
if e != nil {
77+
klog.Errorf("fail to get kubeclient: %s", e.Error())
78+
}
79+
kubeutils.InitKube(client, namespace)
80+
klog.Infof("kubeclient initialized. default namespace: %s", namespace)
81+
}

go.mod

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
module github.com/IBM/k8s-volume-utils
2+
3+
go 1.13
4+
5+
require (
6+
github.com/evanphx/json-patch v4.5.0+incompatible // indirect
7+
github.com/googleapis/gnostic v0.3.1 // indirect
8+
github.com/imdario/mergo v0.3.6 // indirect
9+
github.com/onsi/ginkgo v1.11.0 // indirect
10+
github.com/onsi/gomega v1.8.1 // indirect
11+
github.com/pkg/errors v0.8.1 // indirect
12+
github.com/spf13/cobra v0.0.7
13+
github.com/spf13/pflag v1.0.5 // indirect
14+
golang.org/x/crypto v0.0.0-20191011191535-87dc89f01550 // indirect
15+
golang.org/x/sys v0.0.0-20190922100055-0a153f010e69 // indirect
16+
gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15 // indirect
17+
gopkg.in/yaml.v2 v2.2.7 // indirect
18+
k8s.io/api v0.18.0
19+
k8s.io/apimachinery v0.18.0
20+
k8s.io/cli-runtime v0.0.0
21+
k8s.io/client-go v0.18.0
22+
k8s.io/klog v1.0.0
23+
k8s.io/kubectl v0.0.0
24+
k8s.io/utils v0.0.0-20191114184206-e782cd3c129f // indirect
25+
)
26+
27+
// Pinned to kubernetes-1.16.2
28+
replace (
29+
k8s.io/api => k8s.io/api v0.0.0-20191016110408-35e52d86657a
30+
k8s.io/apiextensions-apiserver => k8s.io/apiextensions-apiserver v0.0.0-20191016113550-5357c4baaf65
31+
k8s.io/apimachinery => k8s.io/apimachinery v0.0.0-20191004115801-a2eda9f80ab8
32+
k8s.io/apiserver => k8s.io/apiserver v0.0.0-20191016112112-5190913f932d
33+
k8s.io/cli-runtime => k8s.io/cli-runtime v0.0.0-20191016114015-74ad18325ed5
34+
k8s.io/client-go => k8s.io/client-go v0.0.0-20191016111102-bec269661e48
35+
k8s.io/cloud-provider => k8s.io/cloud-provider v0.0.0-20191016115326-20453efc2458
36+
k8s.io/cluster-bootstrap => k8s.io/cluster-bootstrap v0.0.0-20191016115129-c07a134afb42
37+
k8s.io/code-generator => k8s.io/code-generator v0.0.0-20191004115455-8e001e5d1894
38+
k8s.io/component-base => k8s.io/component-base v0.0.0-20191016111319-039242c015a9
39+
k8s.io/cri-api => k8s.io/cri-api v0.0.0-20190828162817-608eb1dad4ac
40+
k8s.io/csi-translation-lib => k8s.io/csi-translation-lib v0.0.0-20191016115521-756ffa5af0bd
41+
k8s.io/kube-aggregator => k8s.io/kube-aggregator v0.0.0-20191016112429-9587704a8ad4
42+
k8s.io/kube-controller-manager => k8s.io/kube-controller-manager v0.0.0-20191016114939-2b2b218dc1df
43+
k8s.io/kube-proxy => k8s.io/kube-proxy v0.0.0-20191016114407-2e83b6f20229
44+
k8s.io/kube-scheduler => k8s.io/kube-scheduler v0.0.0-20191016114748-65049c67a58b
45+
k8s.io/kubectl => k8s.io/kubectl v0.0.0-20191016120415-2ed914427d51
46+
k8s.io/kubelet => k8s.io/kubelet v0.0.0-20191016114556-7841ed97f1b2
47+
k8s.io/legacy-cloud-providers => k8s.io/legacy-cloud-providers v0.0.0-20191016115753-cf0698c3a16b
48+
k8s.io/metrics => k8s.io/metrics v0.0.0-20191016113814-3b1a734dba6e
49+
k8s.io/sample-apiserver => k8s.io/sample-apiserver v0.0.0-20191016112829-06bb3c9d77c9
50+
)

0 commit comments

Comments
 (0)