diff --git a/cmd/context.go b/cmd/context.go index 00ffeda..40acb4c 100644 --- a/cmd/context.go +++ b/cmd/context.go @@ -2,6 +2,7 @@ package cmd import ( "fmt" + "log" "os" "strings" @@ -156,10 +157,27 @@ var getContextCmd = &cobra.Command{ fmt.Printf("No context found for '%s'.\n", contextId) } else { structs.PrettyPrint(ctx) + } }, } +var exportNamespaceCmd = &cobra.Command{ + Use: "export", + Short: "Export all resources from a specific namespace.", + Long: `The get command lets you get a specific context from punq.`, + Run: func(cmd *cobra.Command, args []string) { + RequireStringFlag(contextId, "context-id") + RequireStringFlag(namespace, "namespace") + + resourcesYaml, err := kubernetes.AllResourcesFromToCombinedYaml(namespace, resources, &contextId) + if err != nil { + log.Fatal(err.Error()) + } + fmt.Print(resourcesYaml) + }, +} + func init() { contextCmd.AddCommand(listContextCmd) @@ -177,5 +195,9 @@ func init() { contextCmd.AddCommand(getContextCmd) + exportNamespaceCmd.Flags().StringVarP(&namespace, "namespace", "n", "", "A namespace to export resources from") + exportNamespaceCmd.Flags().StringSliceVarP(&resources, "resources", "r", []string{}, "A list of resources to gather separated by comma (,)") + contextCmd.AddCommand(exportNamespaceCmd) + rootCmd.AddCommand(contextCmd) } diff --git a/cmd/root.go b/cmd/root.go index e7da3bd..1abc655 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -26,6 +26,7 @@ var filePath string var contextId string var accessLevel string var forceUpgrade bool +var resources []string var cmdsWithoutContext = []string{ "punq", diff --git a/kubernetes/certificates.go b/kubernetes/certificates.go index fd4025f..c6aa166 100644 --- a/kubernetes/certificates.go +++ b/kubernetes/certificates.go @@ -103,7 +103,7 @@ func DeleteK8sCertificateBy(namespace string, name string, contextId *string) er } func DescribeK8sCertificate(namespace string, name string, contextId *string) utils.K8sWorkloadResult { - cmd := exec.Command("kubectl", ContextFlag(contextId), "describe", "certificate", name, "-n", namespace) + cmd := exec.Command("kubectl", "describe", "certificate", name, "-n", namespace, ContextFlag(contextId)) output, err := cmd.CombinedOutput() if err != nil { diff --git a/kubernetes/cluster-role-bindings.go b/kubernetes/cluster-role-bindings.go index 746d2d9..f29138e 100644 --- a/kubernetes/cluster-role-bindings.go +++ b/kubernetes/cluster-role-bindings.go @@ -75,7 +75,7 @@ func DeleteK8sClusterRoleBindingBy(name string, contextId *string) error { } func DescribeK8sClusterRoleBinding(name string, contextId *string) utils.K8sWorkloadResult { - cmd := exec.Command("kubectl", ContextFlag(contextId), "describe", "clusterrolebinding", name) + cmd := exec.Command("kubectl", "describe", "clusterrolebinding", name, ContextFlag(contextId)) output, err := cmd.CombinedOutput() if err != nil { diff --git a/kubernetes/cluster-roles.go b/kubernetes/cluster-roles.go index 3d74027..d6d550b 100644 --- a/kubernetes/cluster-roles.go +++ b/kubernetes/cluster-roles.go @@ -78,7 +78,7 @@ func DeleteK8sClusterRoleBy(name string, contextId *string) error { } func DescribeK8sClusterRole(name string, contextId *string) utils.K8sWorkloadResult { - cmd := exec.Command("kubectl", ContextFlag(contextId), "describe", "clusterrole", name) + cmd := exec.Command("kubectl", "describe", "clusterrole", name, ContextFlag(contextId)) output, err := cmd.CombinedOutput() if err != nil { diff --git a/kubernetes/clusterIssuer.go b/kubernetes/clusterIssuer.go index 3e11cc7..5bc6837 100644 --- a/kubernetes/clusterIssuer.go +++ b/kubernetes/clusterIssuer.go @@ -77,7 +77,7 @@ func DeleteK8sClusterIssuerBy(name string, contextId *string) error { } func DescribeK8sClusterIssuer(name string, contextId *string) utils.K8sWorkloadResult { - cmd := exec.Command("kubectl", ContextFlag(contextId), "describe", "clusterissuer", name) + cmd := exec.Command("kubectl", "describe", "clusterissuer", name, ContextFlag(contextId)) output, err := cmd.CombinedOutput() if err != nil { diff --git a/kubernetes/configmap.go b/kubernetes/configmap.go index 31e7d72..e2010c2 100644 --- a/kubernetes/configmap.go +++ b/kubernetes/configmap.go @@ -113,7 +113,7 @@ func DeleteK8sConfigmapBy(namespaceName string, name string, contextId *string) } func DescribeK8sConfigmap(namespace string, name string, contextId *string) utils.K8sWorkloadResult { - cmd := exec.Command("kubectl", ContextFlag(contextId), "describe", "configmap", name, "-n", namespace) + cmd := exec.Command("kubectl", "describe", "configmap", name, "-n", namespace, ContextFlag(contextId)) output, err := cmd.CombinedOutput() if err != nil { diff --git a/kubernetes/crds.go b/kubernetes/crds.go index 49e9764..506a48c 100644 --- a/kubernetes/crds.go +++ b/kubernetes/crds.go @@ -56,7 +56,7 @@ func DeleteK8sCustomResourceDefinition(data apiExt.CustomResourceDefinition) uti } func DescribeK8sCustomResourceDefinition(name string, contextId *string) utils.K8sWorkloadResult { - cmd := exec.Command("kubectl", ContextFlag(contextId), "describe", "crds", name) + cmd := exec.Command("kubectl", "describe", "crds", name, ContextFlag(contextId)) output, err := cmd.CombinedOutput() if err != nil { diff --git a/kubernetes/cronjobs.go b/kubernetes/cronjobs.go index 036b64a..6bd0a2c 100644 --- a/kubernetes/cronjobs.go +++ b/kubernetes/cronjobs.go @@ -78,7 +78,7 @@ func DeleteK8sCronJobBy(namespace string, name string, contextId *string) error } func DescribeK8sCronJob(namespace string, name string, contextId *string) utils.K8sWorkloadResult { - cmd := exec.Command("kubectl", ContextFlag(contextId), "describe", "cronjob", name, "-n", namespace) + cmd := exec.Command("kubectl", "describe", "cronjob", name, "-n", namespace, ContextFlag(contextId)) output, err := cmd.CombinedOutput() if err != nil { diff --git a/kubernetes/csr.go b/kubernetes/csr.go index 4a505fa..32c1579 100644 --- a/kubernetes/csr.go +++ b/kubernetes/csr.go @@ -77,7 +77,7 @@ func DeleteK8sCertificateSigningRequestBy(namespace string, name string, context } func DescribeK8sCertificateSigningRequest(namespace string, name string, contextId *string) utils.K8sWorkloadResult { - cmd := exec.Command("kubectl", ContextFlag(contextId), "describe", "-n", namespace, "csr", name) + cmd := exec.Command("kubectl", "describe", "-n", namespace, "csr", name, ContextFlag(contextId)) output, err := cmd.CombinedOutput() if err != nil { diff --git a/kubernetes/daemonsets.go b/kubernetes/daemonsets.go index af62402..29d9174 100644 --- a/kubernetes/daemonsets.go +++ b/kubernetes/daemonsets.go @@ -98,7 +98,7 @@ func DeleteK8sDaemonSetBy(namespace string, name string, contextId *string) erro } func DescribeK8sDaemonSet(namespace string, name string, contextId *string) utils.K8sWorkloadResult { - cmd := exec.Command("kubectl", ContextFlag(contextId), "describe", "daemonset", name, "-n", namespace) + cmd := exec.Command("kubectl", "describe", "daemonset", name, "-n", namespace, ContextFlag(contextId)) output, err := cmd.CombinedOutput() if err != nil { diff --git a/kubernetes/deployment.go b/kubernetes/deployment.go index fce8c2a..b2e2a16 100644 --- a/kubernetes/deployment.go +++ b/kubernetes/deployment.go @@ -111,7 +111,7 @@ func DeleteK8sDeploymentBy(namespace string, name string, contextId *string) err } func DescribeK8sDeployment(namespace string, name string, contextId *string) utils.K8sWorkloadResult { - cmd := exec.Command("kubectl", ContextFlag(contextId), "describe", "deployment", name, "-n", namespace) + cmd := exec.Command("kubectl", "describe", "deployment", name, "-n", namespace, ContextFlag(contextId)) output, err := cmd.CombinedOutput() if err != nil { diff --git a/kubernetes/endpoints.go b/kubernetes/endpoints.go index 5318614..745dc6a 100644 --- a/kubernetes/endpoints.go +++ b/kubernetes/endpoints.go @@ -76,7 +76,7 @@ func DeleteK8sEndpointBy(namespace string, name string, contextId *string) error } func DescribeK8sEndpoint(namespace string, name string, contextId *string) utils.K8sWorkloadResult { - cmd := exec.Command("kubectl", ContextFlag(contextId), "describe", "endpoint", name, "-n", namespace) + cmd := exec.Command("kubectl", "describe", "endpoint", name, "-n", namespace, ContextFlag(contextId)) output, err := cmd.CombinedOutput() if err != nil { diff --git a/kubernetes/events.go b/kubernetes/events.go index 15ac7da..be98b5b 100644 --- a/kubernetes/events.go +++ b/kubernetes/events.go @@ -41,7 +41,7 @@ func GetEvent(namespaceName string, name string, contextId *string) (*v1Core.Eve } func DescribeK8sEvent(namespace string, name string, contextId *string) utils.K8sWorkloadResult { - cmd := exec.Command("kubectl", ContextFlag(contextId), "describe", "event", name, "-n", namespace) + cmd := exec.Command("kubectl", "describe", "event", name, "-n", namespace, ContextFlag(contextId)) output, err := cmd.CombinedOutput() if err != nil { diff --git a/kubernetes/hpa.go b/kubernetes/hpa.go index 9df8fbb..e89fded 100644 --- a/kubernetes/hpa.go +++ b/kubernetes/hpa.go @@ -77,7 +77,7 @@ func DeleteK8sHpaBy(namespace string, name string, contextId *string) error { } func DescribeK8sHpa(namespace string, name string, contextId *string) utils.K8sWorkloadResult { - cmd := exec.Command("kubectl", ContextFlag(contextId), "describe", "hpa", name, "-n", namespace) + cmd := exec.Command("kubectl", "describe", "hpa", name, "-n", namespace, ContextFlag(contextId)) output, err := cmd.CombinedOutput() if err != nil { diff --git a/kubernetes/ingress.go b/kubernetes/ingress.go index ea233d3..012c661 100644 --- a/kubernetes/ingress.go +++ b/kubernetes/ingress.go @@ -101,7 +101,7 @@ func DeleteK8sIngressBy(namespace string, name string, contextId *string) error } func DescribeK8sIngress(namespace string, name string, contextId *string) utils.K8sWorkloadResult { - cmd := exec.Command("kubectl", ContextFlag(contextId), "describe", "ingress", name, "-n", namespace) + cmd := exec.Command("kubectl", "describe", "ingress", name, "-n", namespace, ContextFlag(contextId)) output, err := cmd.CombinedOutput() if err != nil { diff --git a/kubernetes/ingressClass.go b/kubernetes/ingressClass.go index a69ab8a..6d3f470 100644 --- a/kubernetes/ingressClass.go +++ b/kubernetes/ingressClass.go @@ -91,7 +91,7 @@ func DeleteK8sIngressClassBy(name string, contextId *string) error { } func DescribeK8sIngressClass(name string, contextId *string) utils.K8sWorkloadResult { - cmd := exec.Command("kubectl", ContextFlag(contextId), "describe", "ingressclass", name) + cmd := exec.Command("kubectl", "describe", "ingressclass", name, ContextFlag(contextId)) output, err := cmd.CombinedOutput() if err != nil { diff --git a/kubernetes/issuer.go b/kubernetes/issuer.go index d4f0fb6..329e1b4 100644 --- a/kubernetes/issuer.go +++ b/kubernetes/issuer.go @@ -77,7 +77,7 @@ func DeleteK8sIssuerBy(namespace string, name string, contextId *string) error { } func DescribeK8sIssuer(namespace string, name string, contextId *string) utils.K8sWorkloadResult { - cmd := exec.Command("kubectl", ContextFlag(contextId), "describe", "issuer", name, "-n", namespace) + cmd := exec.Command("kubectl", "describe", "issuer", name, "-n", namespace, ContextFlag(contextId)) output, err := cmd.CombinedOutput() if err != nil { diff --git a/kubernetes/jobs.go b/kubernetes/jobs.go index deb65d8..c9e88b1 100644 --- a/kubernetes/jobs.go +++ b/kubernetes/jobs.go @@ -77,7 +77,7 @@ func DeleteK8sJobBy(namespace string, name string, contextId *string) error { } func DescribeK8sJob(namespace string, name string, contextId *string) utils.K8sWorkloadResult { - cmd := exec.Command("kubectl", ContextFlag(contextId), "describe", "job", name, "-n", namespace) + cmd := exec.Command("kubectl", "describe", "job", name, "-n", namespace, ContextFlag(contextId)) output, err := cmd.CombinedOutput() if err != nil { diff --git a/kubernetes/leases.go b/kubernetes/leases.go index b223574..569336c 100644 --- a/kubernetes/leases.go +++ b/kubernetes/leases.go @@ -78,7 +78,7 @@ func DeleteK8sLeaseBy(namespace string, name string, contextId *string) error { } func DescribeK8sLease(namespace string, name string, contextId *string) utils.K8sWorkloadResult { - cmd := exec.Command("kubectl", ContextFlag(contextId), "describe", "lease", name, "-n", namespace) + cmd := exec.Command("kubectl", "describe", "lease", name, "-n", namespace, ContextFlag(contextId)) output, err := cmd.CombinedOutput() if err != nil { diff --git a/kubernetes/namespace.go b/kubernetes/namespace.go index 498d594..730ff37 100644 --- a/kubernetes/namespace.go +++ b/kubernetes/namespace.go @@ -115,7 +115,7 @@ func DeleteK8sNamespaceBy(name string, contextId *string) error { } func DescribeK8sNamespace(name string, contextId *string) utils.K8sWorkloadResult { - cmd := exec.Command("kubectl", ContextFlag(contextId), "describe", "namespace", name) + cmd := exec.Command("kubectl", "describe", "namespace", name, ContextFlag(contextId)) output, err := cmd.CombinedOutput() if err != nil { diff --git a/kubernetes/networkpolicies.go b/kubernetes/networkpolicies.go index 0e70268..04e4be6 100644 --- a/kubernetes/networkpolicies.go +++ b/kubernetes/networkpolicies.go @@ -77,7 +77,7 @@ func DeleteK8sNetworkPolicyBy(namespace string, name string, contextId *string) } func DescribeK8sNetworkPolicy(namespace string, name string, contextId *string) utils.K8sWorkloadResult { - cmd := exec.Command("kubectl", ContextFlag(contextId), "describe", "netpol", name, "-n", namespace) + cmd := exec.Command("kubectl", "describe", "netpol", name, "-n", namespace, ContextFlag(contextId)) output, err := cmd.CombinedOutput() if err != nil { diff --git a/kubernetes/nodes.go b/kubernetes/nodes.go index 2ffd53a..58e9c89 100644 --- a/kubernetes/nodes.go +++ b/kubernetes/nodes.go @@ -110,7 +110,7 @@ func DeleteK8sNode(name string, contextId *string) error { } func DescribeK8sNode(name string, contextId *string) utils.K8sWorkloadResult { - cmd := exec.Command("kubectl", ContextFlag(contextId), "describe", "node", name) + cmd := exec.Command("kubectl", "describe", "node", name, ContextFlag(contextId)) output, err := cmd.CombinedOutput() if err != nil { diff --git a/kubernetes/orders.go b/kubernetes/orders.go index c26ffe7..0a2b3b1 100644 --- a/kubernetes/orders.go +++ b/kubernetes/orders.go @@ -77,7 +77,7 @@ func DeleteK8sOrderBy(namespace string, name string, contextId *string) error { } func DescribeK8sOrder(namespace string, name string, contextId *string) utils.K8sWorkloadResult { - cmd := exec.Command("kubectl", ContextFlag(contextId), "describe", "order", name, "-n", namespace) + cmd := exec.Command("kubectl", "describe", "order", name, "-n", namespace, ContextFlag(contextId)) output, err := cmd.CombinedOutput() if err != nil { diff --git a/kubernetes/persistent-volume-claim.go b/kubernetes/persistent-volume-claim.go index 31308b1..7a40848 100644 --- a/kubernetes/persistent-volume-claim.go +++ b/kubernetes/persistent-volume-claim.go @@ -90,7 +90,7 @@ func DeleteK8sPersistentVolumeClaimBy(namespace string, name string, contextId * } func DescribeK8sPersistentVolumeClaim(namespace string, name string, contextId *string) utils.K8sWorkloadResult { - cmd := exec.Command("kubectl", ContextFlag(contextId), "describe", "persistentvolumeclaim", name, "-n", namespace) + cmd := exec.Command("kubectl", "describe", "persistentvolumeclaim", name, "-n", namespace, ContextFlag(contextId)) output, err := cmd.CombinedOutput() if err != nil { diff --git a/kubernetes/persistent-volume.go b/kubernetes/persistent-volume.go index 31c63d8..bd19fd1 100644 --- a/kubernetes/persistent-volume.go +++ b/kubernetes/persistent-volume.go @@ -73,7 +73,7 @@ func DeleteK8sPersistentVolumeBy(name string, contextId *string) error { } func DescribeK8sPersistentVolume(name string, contextId *string) utils.K8sWorkloadResult { - cmd := exec.Command("kubectl", ContextFlag(contextId), "describe", "persistentvolume", name) + cmd := exec.Command("kubectl", "describe", "persistentvolume", name, ContextFlag(contextId)) output, err := cmd.CombinedOutput() if err != nil { diff --git a/kubernetes/pods.go b/kubernetes/pods.go index e576fd4..884f7f8 100644 --- a/kubernetes/pods.go +++ b/kubernetes/pods.go @@ -288,7 +288,7 @@ func DeleteK8sPodBy(namespace string, name string, contextId *string) error { } func DescribeK8sPod(namespace string, name string, contextId *string) utils.K8sWorkloadResult { - cmd := exec.Command("kubectl", ContextFlag(contextId), "describe", "pod", name, "-n", namespace) + cmd := exec.Command("kubectl", "describe", "pod", name, "-n", namespace, ContextFlag(contextId)) output, err := cmd.CombinedOutput() if err != nil { diff --git a/kubernetes/priorityclasses.go b/kubernetes/priorityclasses.go index 158ab93..7b32ae6 100644 --- a/kubernetes/priorityclasses.go +++ b/kubernetes/priorityclasses.go @@ -78,7 +78,7 @@ func DeleteK8sPriorityClassBy(name string, contextId *string) error { } func DescribeK8sPriorityClass(name string, contextId *string) utils.K8sWorkloadResult { - cmd := exec.Command("kubectl", ContextFlag(contextId), "describe", "priorityclasses", name) + cmd := exec.Command("kubectl", "describe", "priorityclasses", name, ContextFlag(contextId)) output, err := cmd.CombinedOutput() if err != nil { diff --git a/kubernetes/replicasets.go b/kubernetes/replicasets.go index cb6c8e9..28a0ffb 100644 --- a/kubernetes/replicasets.go +++ b/kubernetes/replicasets.go @@ -98,7 +98,7 @@ func DeleteK8sReplicasetBy(namespace string, name string, contextId *string) err } func DescribeK8sReplicaset(namespace string, name string, contextId *string) utils.K8sWorkloadResult { - cmd := exec.Command("kubectl", ContextFlag(contextId), "describe", "replicaset", name, "-n", namespace) + cmd := exec.Command("kubectl", "describe", "replicaset", name, "-n", namespace, ContextFlag(contextId)) output, err := cmd.CombinedOutput() if err != nil { diff --git a/kubernetes/resourcequotas.go b/kubernetes/resourcequotas.go index c8c54b2..a5d955c 100644 --- a/kubernetes/resourcequotas.go +++ b/kubernetes/resourcequotas.go @@ -78,7 +78,7 @@ func DeleteK8sResourceQuotaBy(namespace string, name string, contextId *string) } func DescribeK8sResourceQuota(namespace string, name string, contextId *string) utils.K8sWorkloadResult { - cmd := exec.Command("kubectl", ContextFlag(contextId), "describe", "resourcequotas", name, "-n", namespace) + cmd := exec.Command("kubectl", "describe", "resourcequotas", name, "-n", namespace, ContextFlag(contextId)) output, err := cmd.CombinedOutput() if err != nil { diff --git a/kubernetes/role-bindings.go b/kubernetes/role-bindings.go index ee55723..add0ccb 100644 --- a/kubernetes/role-bindings.go +++ b/kubernetes/role-bindings.go @@ -77,7 +77,7 @@ func DeleteK8sRoleBindingBy(namespace string, name string, contextId *string) er } func DescribeK8sRoleBinding(namespace string, name string, contextId *string) utils.K8sWorkloadResult { - cmd := exec.Command("kubectl", ContextFlag(contextId), "describe", "rolebinding", name, "-n", namespace) + cmd := exec.Command("kubectl", "describe", "rolebinding", name, "-n", namespace, ContextFlag(contextId)) output, err := cmd.CombinedOutput() if err != nil { diff --git a/kubernetes/roles.go b/kubernetes/roles.go index 9a73ec5..463c2d4 100644 --- a/kubernetes/roles.go +++ b/kubernetes/roles.go @@ -77,7 +77,7 @@ func DeleteK8sRoleBy(namespace string, name string, contextId *string) error { } func DescribeK8sRole(namespace string, name string, contextId *string) utils.K8sWorkloadResult { - cmd := exec.Command("kubectl", ContextFlag(contextId), "describe", "role", name, "-n", namespace) + cmd := exec.Command("kubectl", "describe", "role", name, "-n", namespace, ContextFlag(contextId)) output, err := cmd.CombinedOutput() if err != nil { diff --git a/kubernetes/secret.go b/kubernetes/secret.go index e57b279..efcbabb 100644 --- a/kubernetes/secret.go +++ b/kubernetes/secret.go @@ -138,7 +138,7 @@ func DeleteK8sSecretBy(namespace string, name string, contextId *string) error { } func DescribeK8sSecret(namespace string, name string, contextId *string) utils.K8sWorkloadResult { - cmd := exec.Command("kubectl", ContextFlag(contextId), "describe", "secret", name, "-n", namespace) + cmd := exec.Command("kubectl", "describe", "secret", name, "-n", namespace, ContextFlag(contextId)) output, err := cmd.CombinedOutput() if err != nil { diff --git a/kubernetes/service-accounts.go b/kubernetes/service-accounts.go index a690408..ad81498 100644 --- a/kubernetes/service-accounts.go +++ b/kubernetes/service-accounts.go @@ -78,7 +78,7 @@ func DeleteK8sServiceAccountBy(namespace string, name string, contextId *string) } func DescribeK8sServiceAccount(namespace string, name string, contextId *string) utils.K8sWorkloadResult { - cmd := exec.Command("kubectl", ContextFlag(contextId), "describe", "serviceaccount", name, "-n", namespace) + cmd := exec.Command("kubectl", "describe", "serviceaccount", name, "-n", namespace, ContextFlag(contextId)) output, err := cmd.CombinedOutput() if err != nil { diff --git a/kubernetes/service.go b/kubernetes/service.go index 17aecf8..8950210 100644 --- a/kubernetes/service.go +++ b/kubernetes/service.go @@ -109,7 +109,7 @@ func DeleteK8sServiceBy(namespace string, name string, contextId *string) error } func DescribeK8sService(namespace string, name string, contextId *string) utils.K8sWorkloadResult { - cmd := exec.Command("kubectl", ContextFlag(contextId), "describe", "service", name, "-n", namespace) + cmd := exec.Command("kubectl", "describe", "service", name, "-n", namespace, ContextFlag(contextId)) output, err := cmd.CombinedOutput() if err != nil { diff --git a/kubernetes/statefulsets.go b/kubernetes/statefulsets.go index 001c06d..d9d1f71 100644 --- a/kubernetes/statefulsets.go +++ b/kubernetes/statefulsets.go @@ -77,7 +77,7 @@ func DeleteK8sStatefulsetBy(namespace string, name string, contextId *string) er } func DescribeK8sStatefulset(namespace string, name string, contextId *string) utils.K8sWorkloadResult { - cmd := exec.Command("kubectl", ContextFlag(contextId), "describe", "statefulset", name, "-n", namespace) + cmd := exec.Command("kubectl", "describe", "statefulset", name, "-n", namespace, ContextFlag(contextId)) output, err := cmd.CombinedOutput() if err != nil { diff --git a/kubernetes/storageClass.go b/kubernetes/storageClass.go index 117f9f9..2aa1cf8 100644 --- a/kubernetes/storageClass.go +++ b/kubernetes/storageClass.go @@ -73,7 +73,7 @@ func DeleteK8sStorageClassBy(name string, contextId *string) error { } func DescribeK8sStorageClass(name string, contextId *string) utils.K8sWorkloadResult { - cmd := exec.Command("kubectl", ContextFlag(contextId), "describe", "storageclass", name) + cmd := exec.Command("kubectl", "describe", "storageclass", name, ContextFlag(contextId)) output, err := cmd.CombinedOutput() if err != nil { diff --git a/kubernetes/utils.go b/kubernetes/utils.go index 712c28c..ded74b1 100644 --- a/kubernetes/utils.go +++ b/kubernetes/utils.go @@ -9,6 +9,7 @@ import ( "strings" "time" + "gopkg.in/yaml.v2" "k8s.io/apimachinery/pkg/runtime/schema" version2 "k8s.io/apimachinery/pkg/version" v1beta1 "k8s.io/metrics/pkg/apis/metrics/v1beta1" @@ -710,7 +711,7 @@ func LabelsContain(labels map[string]string, str string) bool { return false } -func AllResourcesFrom(namespace string, contextId *string) ([]interface{}, error) { +func AllResourcesFrom(namespace string, resourcesToLookFor []string, contextId *string) ([]interface{}, error) { ignoredResources := []string{ "events.k8s.io/v1", "events.k8s.io/v1beta1", @@ -757,22 +758,45 @@ func AllResourcesFrom(namespace string, contextId *string) ([]interface{}, error // Get a list of all resources of this type in the namespace list, err := restClient.List(context.Background(), metav1.ListOptions{}) if err != nil { - logger.Log.Error("%s: %s", resourceId.Resource, err.Error()) + logger.Log.Errorf("%s: %s", resourceId.Resource, err.Error()) continue } // Iterate over each resource and write it to a file for _, obj := range list.Items { - logger.Log.Noticef("(SUCCESS) %s: %s/%s", resourceId.Resource, obj.GetNamespace(), obj.GetName()) obj.SetManagedFields(nil) delete(obj.Object, "status") obj.SetUID("") obj.SetResourceVersion("") obj.SetCreationTimestamp(metav1.Time{}) - result = append(result, obj.Object) + if len(resourcesToLookFor) > 0 { + if utils.ContainsToLowercase(resourcesToLookFor, obj.GetKind()) { + result = append(result, obj.Object) + } + } else { + result = append(result, obj.Object) + } } } } return result, nil } + +func AllResourcesFromToCombinedYaml(namespace string, resourcesToLookFor []string, contextId *string) (string, error) { + result := "" + resources, err := AllResourcesFrom(namespace, resourcesToLookFor, contextId) + if err != nil { + return result, err + } + for _, res := range resources { + yamlData, err := yaml.Marshal(res) + if err != nil { + return result, err + } + + // Print the YAML string. + result += fmt.Sprintf("---\n%s\n", string(yamlData)) + } + return result, err +} diff --git a/kubernetes/volume-attachments.go b/kubernetes/volume-attachments.go index d5381f5..ec5753b 100644 --- a/kubernetes/volume-attachments.go +++ b/kubernetes/volume-attachments.go @@ -72,7 +72,7 @@ func DeleteK8sVolumeAttachmentBy(name string, contextId *string) error { } func DescribeK8sVolumeAttachment(name string, contextId *string) utils.K8sWorkloadResult { - cmd := exec.Command("kubectl", ContextFlag(contextId), "describe", "volumeattachment", name) + cmd := exec.Command("kubectl", "describe", "volumeattachment", name, ContextFlag(contextId)) output, err := cmd.CombinedOutput() if err != nil { diff --git a/kubernetes/volume-snapshots.go b/kubernetes/volume-snapshots.go index 77dfcd1..91479fe 100644 --- a/kubernetes/volume-snapshots.go +++ b/kubernetes/volume-snapshots.go @@ -64,7 +64,7 @@ func DeleteK8sVolumeSnapshotBy(namespace string, name string, contextId *string) } func DescribeK8sVolumeSnapshot(namespace string, name string, contextId *string) utils.K8sWorkloadResult { - cmd := exec.Command("kubectl", ContextFlag(contextId), "describe", "volumesnapshots", name, "-n", namespace) + cmd := exec.Command("kubectl", "describe", "volumesnapshots", name, "-n", namespace, ContextFlag(contextId)) output, err := cmd.CombinedOutput() if err != nil { diff --git a/punq-dev.json b/punq-dev.json index da3931f..52d612a 100644 --- a/punq-dev.json +++ b/punq-dev.json @@ -1,14 +1,14 @@ { - "version": "1.4.2-develop.4", + "version": "1.4.2-develop.7", "license": "MIT", "homepage": "https://punq.dev", "bin": "punq-dev.exe", - "pre_install": "Rename-Item \"$dir\\punq-dev-v1.4.2-develop.4-windows-amd64\" punq-dev.exe", + "pre_install": "Rename-Item \"$dir\\punq-dev-v1.4.2-develop.7-windows-amd64\" punq-dev.exe", "description": "A slim open-source workload manager for Kubernetes with team collaboration, WebApp, and CLI.", "architecture": { "64bit": { - "url": "https://github.com/mogenius/punq/releases/download/v1.4.2-develop.4/punq-v1.4.2-develop.4-windows-amd64", - "hash": "9ebb34f6d1d322f1eea2dcf169bae2f04b691c8971ceb67dbf445b74dde7b1f2" + "url": "https://github.com/mogenius/punq/releases/download/v1.4.2-develop.7/punq-v1.4.2-develop.7-windows-amd64", + "hash": "d924851b0806c912a41a13d65afa47951dbc0be6390eacf02cd23d010bf29968" } } } diff --git a/utils/utils.go b/utils/utils.go index 9b15ad3..5b29b08 100644 --- a/utils/utils.go +++ b/utils/utils.go @@ -162,6 +162,15 @@ func Contains(s []string, str string) bool { return false } +func ContainsToLowercase(s []string, str string) bool { + for _, v := range s { + if strings.Contains(strings.ToLower(str), strings.ToLower(v)) { + return true + } + } + return false +} + func ContainsEqual(s []string, str string) bool { for _, v := range s { if str == v {