Skip to content

Commit a306bd6

Browse files
authored
feat: add delete image_registry (#2634)
Signed-off-by: joyceliu <[email protected]>
1 parent 8593c18 commit a306bd6

File tree

12 files changed

+262
-18
lines changed

12 files changed

+262
-18
lines changed

builtin/core/playbooks/delete_cluster.yaml

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,4 +30,15 @@
3030
- role: uninstall/etcd
3131
when: .deleteETCD
3232

33+
- hosts:
34+
- image_registry
35+
roles:
36+
- role: uninstall/image_registry
37+
when:
38+
- .deleteImageRegistry
39+
- role: uninstall/cri
40+
when:
41+
- .deleteImageRegistry
42+
- .deleteCRI
43+
3344
- import_playbook: hook/post_install.yaml
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
---
2+
# load defaults vars
3+
- hosts:
4+
- all
5+
vars_files:
6+
- vars/delete_cluster.yaml
7+
8+
- import_playbook: hook/pre_install.yaml
9+
10+
- hosts:
11+
- image_registry
12+
roles:
13+
- role: uninstall/image_registry
14+
- role: uninstall/cri
15+
when: .deleteCRI
16+
17+
- import_playbook: hook/post_install.yaml

builtin/core/playbooks/delete_nodes.yaml

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,4 +76,17 @@
7676
- .deleteETCD
7777
- .delete_nodes | default list | has .inventory_hostname
7878

79+
- hosts:
80+
- image_registry
81+
roles:
82+
- role: uninstall/image_registry
83+
when:
84+
- .deleteImageRegistry
85+
- .delete_nodes | default list | has .inventory_hostname
86+
- role: uninstall/cri
87+
when:
88+
- .deleteImageRegistry
89+
- .deleteCRI
90+
- .delete_nodes | default list | has .inventory_hostname
91+
7992
- import_playbook: hook/post_install.yaml
Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,24 @@
1+
# Default Kubernetes configuration for deletion playbooks
12
kubernetes:
3+
# The CNI plugin used by the cluster. Default is "calico"
24
kube_network_plugin: "calico"
3-
# if deleteCRI
5+
6+
# Whether to delete the container runtime interface (CRI) on target nodes
7+
# Set to true to uninstall CRI (e.g., containerd, docker)
48
deleteCRI: false
5-
# if deleteETCD
9+
10+
# Whether to delete etcd on target nodes
11+
# Set to true to uninstall etcd
612
deleteETCD: false
7-
# if true. will delete local dns in localDNS file which create by kubekey
13+
14+
# Whether to delete local DNS entries in the localDNS file created by kubekey
15+
# Set to true to remove kubekey-managed DNS entries from the specified files
816
deleteDNS: false
17+
18+
# Whether to delete the image registry on target nodes
19+
# Set to true to uninstall the image registry
20+
deleteImageRegistry: false
21+
22+
# List of local DNS files to be cleaned up if deleteDNS is true
923
localDNS:
1024
- /etc/hosts
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
image_registry:
2+
type: harbor
3+
# Virtual IP address for repository High Availability. the Virtual IP address should be available.
4+
harbor:
5+
data_dir: /opt/harbor/data
6+
registry:
7+
storage:
8+
filesystem:
9+
rootdir: /opt/registry
10+
# nfs_mount: /repository/registry # if set. will mount rootdirectory to nfs server in nfs_mount.
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
- name: Stop harbor service
2+
command: |
3+
systemctl stop harbor.service
4+
systemctl disable harbor.service
5+
rm -rf /etc/systemd/system/harbor.service*
6+
systemctl daemon-reload
7+
8+
- name: Delete residue harbor files
9+
command: |
10+
rm -rf /opt/harbor/
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
- name: Delete residue keepalived files
2+
command: |
3+
rm -rf /opt/keepalived/
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
- include_tasks: harbor.yaml
2+
when: .image_registry.type | eq "harbor"
3+
4+
- include_tasks: registry.yaml
5+
when: .image_registry.type | eq "registry"
6+
7+
- include_tasks: keepalived.yaml
8+
when:
9+
- .image_registry.ha_vip | empty | not
10+
- .groups.image_registry | len | lt 1
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
- name: Stop registry service
2+
command: |
3+
systemctl stop registry.service
4+
systemctl disable registry.service
5+
rm -rf /etc/systemd/system/registry.service*
6+
systemctl daemon-reload
7+
8+
- name: unmount nfs
9+
when:
10+
- .image_registry.registry.storage.filesystem.nfs_mount | empty | not
11+
- .groups.nfs | default list | len | eq 1
12+
command: |
13+
unmount {{ .image_registry.registry.storage.filesystem.rootdir }}
14+
15+
- name: Delete residue registry files
16+
command: |
17+
rm -rf /opt/registry/

cmd/kk/app/builtin/delete.go

Lines changed: 41 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,14 +26,16 @@ import (
2626
)
2727

2828
// NewDeleteCommand creates a new delete command that allows deleting nodes or clusters.
29-
// It provides subcommands for deleting either an entire cluster or individual nodes.
29+
// It provides subcommands for deleting either an entire cluster or individual nodes, as well as the image registry.
3030
func NewDeleteCommand() *cobra.Command {
3131
cmd := &cobra.Command{
3232
Use: "delete",
3333
Short: "Delete node or cluster",
3434
}
35+
// Add subcommands for cluster, nodes, and image registry deletion
3536
cmd.AddCommand(newDeleteClusterCommand())
3637
cmd.AddCommand(newDeleteNodesCommand())
38+
cmd.AddCommand(newDeleteImageRegistryCommand())
3739

3840
return cmd
3941
}
@@ -45,20 +47,24 @@ func NewDeleteCommand() *cobra.Command {
4547
// - Remove DNS entries if specified
4648
// - Clean up etcd if specified
4749
func newDeleteClusterCommand() *cobra.Command {
50+
// Initialize options for deleting a cluster
4851
o := builtin.NewDeleteClusterOptions()
4952

5053
cmd := &cobra.Command{
5154
Use: "cluster",
5255
Short: "Delete a cluster",
5356
RunE: func(cmd *cobra.Command, args []string) error {
57+
// Complete the configuration and create a playbook for deleting the cluster
5458
playbook, err := o.Complete(cmd, []string{"playbooks/delete_cluster.yaml"})
5559
if err != nil {
5660
return err
5761
}
5862

63+
// Execute the playbook to delete the cluster
5964
return o.CommonOptions.Run(cmd.Context(), playbook)
6065
},
6166
}
67+
// Add all relevant flag sets to the command
6268
flags := cmd.Flags()
6369
for _, f := range o.Flags().FlagSets {
6470
flags.AddFlagSet(f)
@@ -74,23 +80,55 @@ func newDeleteClusterCommand() *cobra.Command {
7480
// - Remove DNS entries if specified
7581
// - Clean up etcd if the node was part of etcd cluster
7682
func newDeleteNodesCommand() *cobra.Command {
83+
// Initialize options for deleting nodes
7784
o := builtin.NewDeleteNodesOptions()
7885

7986
cmd := &cobra.Command{
8087
Use: "nodes {node1 node2 ...}",
8188
Aliases: []string{"node"},
8289
Short: "Delete a cluster nodes",
8390
RunE: func(cmd *cobra.Command, args []string) error {
84-
// Complete the configuration and create a playbook for delete nodes
91+
// Complete the configuration and create a playbook for deleting nodes
92+
// The playbook path is appended as the last argument
8593
playbook, err := o.Complete(cmd, append(args, "playbooks/delete_nodes.yaml"))
8694
if err != nil {
8795
return err
8896
}
8997

90-
// Execute the playbook to add the nodes
98+
// Execute the playbook to delete the specified nodes
9199
return o.CommonOptions.Run(cmd.Context(), playbook)
92100
},
93101
}
102+
// Add all relevant flag sets to the command
103+
flags := cmd.Flags()
104+
for _, f := range o.Flags().FlagSets {
105+
flags.AddFlagSet(f)
106+
}
107+
108+
return cmd
109+
}
110+
111+
// newDeleteImageRegistryCommand creates a new command for deleting the image registry created by kubekey.
112+
// It uses the delete_image_registry.yaml playbook to remove the image registry and optionally its container runtime.
113+
func newDeleteImageRegistryCommand() *cobra.Command {
114+
// Initialize options for deleting the image registry
115+
o := builtin.NewDeleteImageRegistryOptions()
116+
117+
cmd := &cobra.Command{
118+
Use: "image_registry",
119+
Short: "Delete a image_registry which create by kubekey.",
120+
RunE: func(cmd *cobra.Command, args []string) error {
121+
// Complete the configuration and create a playbook for deleting the image registry
122+
playbook, err := o.Complete(cmd, []string{"playbooks/delete_image_registry.yaml"})
123+
if err != nil {
124+
return err
125+
}
126+
127+
// Execute the playbook to delete the image registry
128+
return o.CommonOptions.Run(cmd.Context(), playbook)
129+
},
130+
}
131+
// Add all relevant flag sets to the command
94132
flags := cmd.Flags()
95133
for _, f := range o.Flags().FlagSets {
96134
flags.AddFlagSet(f)

0 commit comments

Comments
 (0)