Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions builtin/core/playbooks/delete_cluster.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -30,4 +30,15 @@
- role: uninstall/etcd
when: .deleteETCD

- hosts:
- image_registry
roles:
- role: uninstall/image_registry
when:
- .deleteImageRegistry
- role: uninstall/cri
when:
- .deleteImageRegistry
- .deleteCRI

- import_playbook: hook/post_install.yaml
17 changes: 17 additions & 0 deletions builtin/core/playbooks/delete_image_registry.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
---
# load defaults vars
- hosts:
- all
vars_files:
- vars/delete_cluster.yaml

- import_playbook: hook/pre_install.yaml

- hosts:
- image_registry
roles:
- role: uninstall/image_registry
- role: uninstall/cri
when: .deleteCRI

- import_playbook: hook/post_install.yaml
13 changes: 13 additions & 0 deletions builtin/core/playbooks/delete_nodes.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -76,4 +76,17 @@
- .deleteETCD
- .delete_nodes | default list | has .inventory_hostname

- hosts:
- image_registry
roles:
- role: uninstall/image_registry
when:
- .deleteImageRegistry
- .delete_nodes | default list | has .inventory_hostname
- role: uninstall/cri
when:
- .deleteImageRegistry
- .deleteCRI
- .delete_nodes | default list | has .inventory_hostname

- import_playbook: hook/post_install.yaml
20 changes: 17 additions & 3 deletions builtin/core/playbooks/vars/delete_cluster.yaml
Original file line number Diff line number Diff line change
@@ -1,10 +1,24 @@
# Default Kubernetes configuration for deletion playbooks
kubernetes:
# The CNI plugin used by the cluster. Default is "calico"
kube_network_plugin: "calico"
# if deleteCRI

# Whether to delete the container runtime interface (CRI) on target nodes
# Set to true to uninstall CRI (e.g., containerd, docker)
deleteCRI: false
# if deleteETCD

# Whether to delete etcd on target nodes
# Set to true to uninstall etcd
deleteETCD: false
# if true. will delete local dns in localDNS file which create by kubekey

# Whether to delete local DNS entries in the localDNS file created by kubekey
# Set to true to remove kubekey-managed DNS entries from the specified files
deleteDNS: false

# Whether to delete the image registry on target nodes
# Set to true to uninstall the image registry
deleteImageRegistry: false

# List of local DNS files to be cleaned up if deleteDNS is true
localDNS:
- /etc/hosts
10 changes: 10 additions & 0 deletions builtin/core/roles/uninstall/image_registry/defaults/main.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
image_registry:
type: harbor
# Virtual IP address for repository High Availability. the Virtual IP address should be available.
harbor:
data_dir: /opt/harbor/data
registry:
storage:
filesystem:
rootdir: /opt/registry
# nfs_mount: /repository/registry # if set. will mount rootdirectory to nfs server in nfs_mount.
10 changes: 10 additions & 0 deletions builtin/core/roles/uninstall/image_registry/tasks/harbor.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
- name: Stop harbor service
command: |
systemctl stop harbor.service
systemctl disable harbor.service
rm -rf /etc/systemd/system/harbor.service*
systemctl daemon-reload

- name: Delete residue harbor files
command: |
rm -rf /opt/harbor/
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
- name: Delete residue keepalived files
command: |
rm -rf /opt/keepalived/
10 changes: 10 additions & 0 deletions builtin/core/roles/uninstall/image_registry/tasks/main.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
- include_tasks: harbor.yaml
when: .image_registry.type | eq "harbor"

- include_tasks: registry.yaml
when: .image_registry.type | eq "registry"

- include_tasks: keepalived.yaml
when:
- .image_registry.ha_vip | empty | not
- .groups.image_registry | len | lt 1
17 changes: 17 additions & 0 deletions builtin/core/roles/uninstall/image_registry/tasks/registry.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
- name: Stop registry service
command: |
systemctl stop registry.service
systemctl disable registry.service
rm -rf /etc/systemd/system/registry.service*
systemctl daemon-reload

- name: unmount nfs
when:
- .image_registry.registry.storage.filesystem.nfs_mount | empty | not
- .groups.nfs | default list | len | eq 1
command: |
unmount {{ .image_registry.registry.storage.filesystem.rootdir }}

- name: Delete residue registry files
command: |
rm -rf /opt/registry/
44 changes: 41 additions & 3 deletions cmd/kk/app/builtin/delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,16 @@ import (
)

// NewDeleteCommand creates a new delete command that allows deleting nodes or clusters.
// It provides subcommands for deleting either an entire cluster or individual nodes.
// It provides subcommands for deleting either an entire cluster or individual nodes, as well as the image registry.
func NewDeleteCommand() *cobra.Command {
cmd := &cobra.Command{
Use: "delete",
Short: "Delete node or cluster",
}
// Add subcommands for cluster, nodes, and image registry deletion
cmd.AddCommand(newDeleteClusterCommand())
cmd.AddCommand(newDeleteNodesCommand())
cmd.AddCommand(newDeleteImageRegistryCommand())

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

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

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

cmd := &cobra.Command{
Use: "nodes {node1 node2 ...}",
Aliases: []string{"node"},
Short: "Delete a cluster nodes",
RunE: func(cmd *cobra.Command, args []string) error {
// Complete the configuration and create a playbook for delete nodes
// Complete the configuration and create a playbook for deleting nodes
// The playbook path is appended as the last argument
playbook, err := o.Complete(cmd, append(args, "playbooks/delete_nodes.yaml"))
if err != nil {
return err
}

// Execute the playbook to add the nodes
// Execute the playbook to delete the specified nodes
return o.CommonOptions.Run(cmd.Context(), playbook)
},
}
// Add all relevant flag sets to the command
flags := cmd.Flags()
for _, f := range o.Flags().FlagSets {
flags.AddFlagSet(f)
}

return cmd
}

// newDeleteImageRegistryCommand creates a new command for deleting the image registry created by kubekey.
// It uses the delete_image_registry.yaml playbook to remove the image registry and optionally its container runtime.
func newDeleteImageRegistryCommand() *cobra.Command {
// Initialize options for deleting the image registry
o := builtin.NewDeleteImageRegistryOptions()

cmd := &cobra.Command{
Use: "image_registry",
Short: "Delete a image_registry which create by kubekey.",
RunE: func(cmd *cobra.Command, args []string) error {
// Complete the configuration and create a playbook for deleting the image registry
playbook, err := o.Complete(cmd, []string{"playbooks/delete_image_registry.yaml"})
if err != nil {
return err
}

// Execute the playbook to delete the image registry
return o.CommonOptions.Run(cmd.Context(), playbook)
},
}
// Add all relevant flag sets to the command
flags := cmd.Flags()
for _, f := range o.Flags().FlagSets {
flags.AddFlagSet(f)
Expand Down
Loading