From a56c011182fd219d7089092c865c0b8fe4f73bd4 Mon Sep 17 00:00:00 2001 From: Jwalant Modi Date: Mon, 4 Nov 2024 13:41:51 +0530 Subject: [PATCH] Captured BackupService, Backup, Restore, PDB, ReplicaSet and ConfigMap related information --- cmd/collectinfo.go | 4 +- pkg/collectinfo/collectinfo_test.go | 128 +++++++++++++++++++++++++--- pkg/collectinfo/resources.go | 52 ++++++++--- pkg/internal/constants.go | 24 ++++-- 4 files changed, 173 insertions(+), 35 deletions(-) diff --git a/cmd/collectinfo.go b/cmd/collectinfo.go index ba92e4e..918b116 100644 --- a/cmd/collectinfo.go +++ b/cmd/collectinfo.go @@ -33,7 +33,9 @@ var collectinfoCmd = &cobra.Command{ Use: "collectinfo", Short: "collectinfo command collects all the required info from kubernetes cluster", Long: `This command collects the following data from the given namespaces: -* pods, statefulsets, persistentvolumeclaims, aerospikeclusters, nodes, storageclasses objects. +* pods, statefulsets, deployments, persistentvolumeclaims, aerospikeclusters, aerospikebackupservices, +* aerospikebackups, aerospikerestores, configmaps, replicasets, poddisruptionbudgets, services, +* nodes, storageclasses, persistentvolumes objects. * mutating and validating webhooks configurations. * containers logs. * events logs.`, diff --git a/pkg/collectinfo/collectinfo_test.go b/pkg/collectinfo/collectinfo_test.go index 5f72753..6959b9d 100644 --- a/pkg/collectinfo/collectinfo_test.go +++ b/pkg/collectinfo/collectinfo_test.go @@ -29,11 +29,13 @@ import ( admissionv1 "k8s.io/api/admissionregistration/v1" appsv1 "k8s.io/api/apps/v1" corev1 "k8s.io/api/core/v1" + policyv1 "k8s.io/api/policy/v1" v1 "k8s.io/api/storage/v1" "k8s.io/apimachinery/pkg/api/resource" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" "k8s.io/apimachinery/pkg/apis/meta/v1/unstructured" "k8s.io/apimachinery/pkg/runtime/schema" + "k8s.io/apimachinery/pkg/util/intstr" "sigs.k8s.io/controller-runtime/pkg/client" "github.com/aerospike/aerospike-kubernetes-operator-ctl/pkg/collectinfo" @@ -42,16 +44,22 @@ import ( ) const ( - nodeName = "test-node" - scName = "test-sc" - serviceName = "test-service" - pvcName = "test-pvc" - pvName = "test-pv" - stsName = "test-sts" - deployName = "test-deploy" - podName = "test-pod" - containerName = "test-container" - aerospikeClusterName = "test-aerocluster" + nodeName = "test-node" + scName = "test-sc" + serviceName = "test-service" + pvcName = "test-pvc" + pvName = "test-pv" + stsName = "test-sts" + deployName = "test-deploy" + podName = "test-pod" + containerName = "test-container" + aerospikeClusterName = "test-aerocluster" + aerospikeBackupServiceName = "test-aerobackupservice" + aerospikeBackupName = "test-aerobackup" + aerospikeRestoreName = "test-aerorestore" + pdbName = "test-pdb" + cmName = "test-cm" + rsName = "test-rs" ) var ( @@ -89,6 +97,18 @@ var filesList = map[string]bool{ serviceName+collectinfo.FileSuffix): false, filepath.Join(namespaceScopeDir, namespace, collectinfo.KindDirNames[internal.AerospikeClusterKind], aerospikeClusterName+collectinfo.FileSuffix): false, + filepath.Join(namespaceScopeDir, namespace, collectinfo.KindDirNames[internal.AerospikeBackupServiceKind], + aerospikeBackupServiceName+collectinfo.FileSuffix): false, + filepath.Join(namespaceScopeDir, namespace, collectinfo.KindDirNames[internal.AerospikeBackupKind], + aerospikeBackupName+collectinfo.FileSuffix): false, + filepath.Join(namespaceScopeDir, namespace, collectinfo.KindDirNames[internal.AerospikeRestoreKind], + aerospikeRestoreName+collectinfo.FileSuffix): false, + filepath.Join(namespaceScopeDir, namespace, collectinfo.KindDirNames[internal.PodDisruptionBudgetKind], + pdbName+collectinfo.FileSuffix): false, + filepath.Join(namespaceScopeDir, namespace, collectinfo.KindDirNames[internal.ConfigMapKind], + cmName+collectinfo.FileSuffix): false, + filepath.Join(namespaceScopeDir, namespace, collectinfo.KindDirNames[internal.RSKind], + rsName+collectinfo.FileSuffix): false, filepath.Join(namespaceScopeDir, namespace, collectinfo.SummaryDir, collectinfo.SummaryFile): false, filepath.Join(collectinfo.RootOutputDir, @@ -233,7 +253,7 @@ var _ = Describe("collectInfo", func() { gvk := schema.GroupVersionKind{ Group: "asdb.aerospike.com", Version: "v1", - Kind: "AerospikeCluster", + Kind: internal.AerospikeClusterKind, } u := &unstructured.Unstructured{} @@ -244,6 +264,92 @@ var _ = Describe("collectInfo", func() { err = k8sClient.Create(context.TODO(), u) Expect(err).ToNot(HaveOccurred()) + gvk = schema.GroupVersionKind{ + Group: "asdb.aerospike.com", + Version: "v1beta1", + Kind: internal.AerospikeBackupServiceKind, + } + + u = &unstructured.Unstructured{} + u.SetName(aerospikeBackupServiceName) + u.SetNamespace(namespace) + u.SetGroupVersionKind(gvk) + + err = k8sClient.Create(context.TODO(), u) + Expect(err).ToNot(HaveOccurred()) + + gvk = schema.GroupVersionKind{ + Group: "asdb.aerospike.com", + Version: "v1beta1", + Kind: internal.AerospikeBackupKind, + } + + u = &unstructured.Unstructured{} + u.SetName(aerospikeBackupName) + u.SetNamespace(namespace) + u.SetGroupVersionKind(gvk) + + err = k8sClient.Create(context.TODO(), u) + Expect(err).ToNot(HaveOccurred()) + + gvk = schema.GroupVersionKind{ + Group: "asdb.aerospike.com", + Version: "v1beta1", + Kind: internal.AerospikeRestoreKind, + } + + u = &unstructured.Unstructured{} + u.SetName(aerospikeRestoreName) + u.SetNamespace(namespace) + u.SetGroupVersionKind(gvk) + + err = k8sClient.Create(context.TODO(), u) + Expect(err).ToNot(HaveOccurred()) + + rs := &appsv1.ReplicaSet{ + ObjectMeta: metav1.ObjectMeta{Name: rsName, Namespace: namespace}, + Spec: appsv1.ReplicaSetSpec{ + Selector: &metav1.LabelSelector{ + MatchLabels: map[string]string{"app": "t1", "s2iBuilder": "t1-s2i-1x55", "version": "v1"}, + }, + Template: corev1.PodTemplateSpec{ + ObjectMeta: metav1.ObjectMeta{ + Labels: map[string]string{"app": "t1", "s2iBuilder": "t1-s2i-1x55", "version": "v1"}, + }, + Spec: corev1.PodSpec{ + Containers: []corev1.Container{ + { + Name: containerName, + Image: "nginx:1.12", + }, + }, + }, + }, + }, + } + + err = k8sClient.Create(context.TODO(), rs, createOption) + Expect(err).ToNot(HaveOccurred()) + + maxUnavailable := intstr.FromInt32(1) + pdb := &policyv1.PodDisruptionBudget{ + ObjectMeta: metav1.ObjectMeta{Name: pdbName, Namespace: namespace}, + Spec: policyv1.PodDisruptionBudgetSpec{ + MaxUnavailable: &maxUnavailable, + }, + } + + err = k8sClient.Create(context.TODO(), pdb, createOption) + Expect(err).ToNot(HaveOccurred()) + + cm := &corev1.ConfigMap{ + ObjectMeta: metav1.ObjectMeta{Name: cmName, Namespace: namespace}, + Data: map[string]string{}, + } + + err = k8sClient.Create(context.TODO(), cm, createOption) + Expect(err).ToNot(HaveOccurred()) + err = os.MkdirAll(collectinfo.RootOutputDir, os.ModePerm) Expect(err).ToNot(HaveOccurred()) diff --git a/pkg/collectinfo/resources.go b/pkg/collectinfo/resources.go index 7628412..3273d34 100644 --- a/pkg/collectinfo/resources.go +++ b/pkg/collectinfo/resources.go @@ -16,29 +16,35 @@ limitations under the License. package collectinfo import ( + "github.com/aerospike/aerospike-kubernetes-operator-ctl/pkg/internal" admissionv1 "k8s.io/api/admissionregistration/v1" appsv1 "k8s.io/api/apps/v1" corev1 "k8s.io/api/core/v1" + policyv1 "k8s.io/api/policy/v1" v1 "k8s.io/api/storage/v1" "k8s.io/apimachinery/pkg/runtime/schema" - - "github.com/aerospike/aerospike-kubernetes-operator-ctl/pkg/internal" ) var ( KindDirNames = map[string]string{ - internal.NodeKind: "nodes", - internal.PVCKind: "persistentvolumeclaims", - internal.PVKind: "persistentvolumes", - internal.STSKind: "statefulsets", - internal.DeployKind: "deployments", - internal.SCKind: "storageclasses", - internal.AerospikeClusterKind: "aerospikeclusters", - internal.PodKind: "pods", - internal.EventKind: "events", - internal.MutatingWebhookKind: "mutatingwebhookconfigurations", - internal.ValidatingWebhookKind: "validatingwebhookconfigurations", - internal.ServiceKind: "services", + internal.NodeKind: "nodes", + internal.PVCKind: "persistentvolumeclaims", + internal.PVKind: "persistentvolumes", + internal.STSKind: "statefulsets", + internal.DeployKind: "deployments", + internal.SCKind: "storageclasses", + internal.AerospikeClusterKind: "aerospikeclusters", + internal.PodKind: "pods", + internal.EventKind: "events", + internal.MutatingWebhookKind: "mutatingwebhookconfigurations", + internal.ValidatingWebhookKind: "validatingwebhookconfigurations", + internal.ServiceKind: "services", + internal.AerospikeBackupServiceKind: "aerospikebackupservices", + internal.AerospikeBackupKind: "aerospikebackups", + internal.AerospikeRestoreKind: "aerospikerestores", + internal.PodDisruptionBudgetKind: "poddisruptionbudgets", + internal.ConfigMapKind: "configmaps", + internal.RSKind: "replicasets", } gvkListNSScoped = []schema.GroupVersionKind{ { @@ -46,11 +52,29 @@ var ( Version: "v1", Kind: internal.AerospikeClusterKind, }, + { + Group: "asdb.aerospike.com", + Version: "v1beta1", + Kind: internal.AerospikeBackupServiceKind, + }, + { + Group: "asdb.aerospike.com", + Version: "v1beta1", + Kind: internal.AerospikeBackupKind, + }, + { + Group: "asdb.aerospike.com", + Version: "v1beta1", + Kind: internal.AerospikeRestoreKind, + }, appsv1.SchemeGroupVersion.WithKind(internal.STSKind), appsv1.SchemeGroupVersion.WithKind(internal.DeployKind), + appsv1.SchemeGroupVersion.WithKind(internal.RSKind), corev1.SchemeGroupVersion.WithKind(internal.PodKind), corev1.SchemeGroupVersion.WithKind(internal.PVCKind), corev1.SchemeGroupVersion.WithKind(internal.ServiceKind), + policyv1.SchemeGroupVersion.WithKind(internal.PodDisruptionBudgetKind), + corev1.SchemeGroupVersion.WithKind(internal.ConfigMapKind), } gvkListClusterScoped = []schema.GroupVersionKind{ corev1.SchemeGroupVersion.WithKind(internal.NodeKind), diff --git a/pkg/internal/constants.go b/pkg/internal/constants.go index 2f4aec9..300de17 100644 --- a/pkg/internal/constants.go +++ b/pkg/internal/constants.go @@ -17,15 +17,21 @@ package internal const ( // Namespace scope resources - PodKind = "Pod" - STSKind = "StatefulSet" - DeployKind = "Deployment" - ServiceAccountKind = "ServiceAccount" - ServiceKind = "Service" - AerospikeClusterKind = "AerospikeCluster" - PVCKind = "PersistentVolumeClaim" - EventKind = "Event" - RoleBindingKind = "RoleBinding" + PodKind = "Pod" + STSKind = "StatefulSet" + RSKind = "ReplicaSet" + DeployKind = "Deployment" + ServiceAccountKind = "ServiceAccount" + ServiceKind = "Service" + AerospikeClusterKind = "AerospikeCluster" + PVCKind = "PersistentVolumeClaim" + EventKind = "Event" + RoleBindingKind = "RoleBinding" + AerospikeBackupKind = "AerospikeBackup" + AerospikeRestoreKind = "AerospikeRestore" + AerospikeBackupServiceKind = "AerospikeBackupService" + PodDisruptionBudgetKind = "PodDisruptionBudget" + ConfigMapKind = "ConfigMap" // Cluster scope resources NodeKind = "Node"