diff --git a/cmd/certs/inspect.go b/cmd/certs/inspect.go index 503c4ee8..a3bb3fbb 100644 --- a/cmd/certs/inspect.go +++ b/cmd/certs/inspect.go @@ -205,7 +205,7 @@ func inspectResources(resourceTypes []string) { } } for _, curr := range resources { - age := helpers.GetAge(vars.MustGatherRootPath+"/cluster-scoped-resources/", curr.GetCreationTimestamp()) + age := helpers.GetAge(vars.MustGatherRootPath, curr.GetCreationTimestamp()) _list := []string{ curr.GetNamespace(), curr.GetName(), diff --git a/cmd/get/get.go b/cmd/get/get.go index 368b4b2f..08190793 100644 --- a/cmd/get/get.go +++ b/cmd/get/get.go @@ -139,6 +139,8 @@ func init() { apiregistration.AddToScheme, } _ = addAdmissionRegistrationTypes(vars.Schema) + _ = addApiextensionsTypes(vars.Schema) + _ = addApiextensionsV1Beta1Types(vars.Schema) _ = addApiServerInternalTypes(vars.Schema) _ = addApiRegistrationTypes(vars.Schema) _ = addAppsTypes(vars.Schema) @@ -187,7 +189,7 @@ func init() { func getNamespacedResources(resourceNamePlural string, resourceGroup string, resources map[string]struct{}) { var namespaces []string - if vars.AllNamespaceBoolVar == true { + if vars.AllNamespaceBoolVar { vars.Namespace = "" vars.ShowNamespace = true _namespaces, _ := ioutil.ReadDir(vars.MustGatherRootPath + "/namespaces/") @@ -357,20 +359,20 @@ func handleObject(obj unstructured.Unstructured) error { if vars.Namespace != "" && obj.GetNamespace() != "" && vars.Namespace != obj.GetNamespace() { return nil } - labelsOk, err := helpers.MatchLabelsFromMap(obj.GetLabels(), vars.LabelSelectorStringVar) + labelsOk, _ := helpers.MatchLabelsFromMap(obj.GetLabels(), vars.LabelSelectorStringVar) if !labelsOk { return nil } vars.LastKind = obj.GetKind() if vars.OutputStringVar == "yaml" || vars.OutputStringVar == "json" { - if vars.ShowManagedFields == false { + if !vars.ShowManagedFields { obj.SetManagedFields(nil) } vars.UnstructuredList.Items = append(vars.UnstructuredList.Items, obj) return nil } if strings.HasPrefix(vars.OutputStringVar, "jsonpath=") { - if vars.ShowManagedFields == false { + if !vars.ShowManagedFields { obj.SetManagedFields(nil) } vars.UnstructuredList.Items = append(vars.UnstructuredList.Items, obj) diff --git a/cmd/get/know-types.go b/cmd/get/know-types.go index 30cab37b..0fc7c010 100644 --- a/cmd/get/know-types.go +++ b/cmd/get/know-types.go @@ -36,6 +36,9 @@ import ( projectv1helpers "github.com/openshift/openshift-apiserver/pkg/project/apis/project" "github.com/openshift/openshift-apiserver/pkg/route/apis/route" runtime "k8s.io/apimachinery/pkg/runtime" + + apiextensionsv1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1" + apiextensionsv1beta1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1beta1" ) func addAdmissionRegistrationTypes(scheme *runtime.Scheme) error { @@ -50,6 +53,24 @@ func addAdmissionRegistrationTypes(scheme *runtime.Scheme) error { return nil } +func addApiextensionsTypes(scheme *runtime.Scheme) error { + GroupVersion := schema.GroupVersion{Group: "apiextensions.k8s.io", Version: "v1"} + types := []runtime.Object{ + &apiextensionsv1.CustomResourceDefinition{}, + } + scheme.AddKnownTypes(GroupVersion, types...) + return nil +} + +func addApiextensionsV1Beta1Types(scheme *runtime.Scheme) error { + GroupVersion := schema.GroupVersion{Group: "apiextensions.k8s.io", Version: "v1beta1"} + types := []runtime.Object{ + &apiextensionsv1beta1.CustomResourceDefinition{}, + } + scheme.AddKnownTypes(GroupVersion, types...) + return nil +} + func addApiServerInternalTypes(scheme *runtime.Scheme) error { GroupVersion := schema.GroupVersion{Group: "apiserverinternal.k8s.io", Version: "v1"} types := []runtime.Object{ diff --git a/cmd/get/missing-handlers.go b/cmd/get/missing-handlers.go index e8c77fad..3b9e1173 100644 --- a/cmd/get/missing-handlers.go +++ b/cmd/get/missing-handlers.go @@ -2,10 +2,13 @@ package get import ( "reflect" + "time" "github.com/gmeghnag/omc/cmd/helpers" "github.com/gmeghnag/omc/vars" configv1 "github.com/openshift/api/config/v1" + apiextensionsv1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1" + apiextensionsv1beta1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1beta1" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" v1 "k8s.io/apimachinery/pkg/apis/meta/v1" runtime "k8s.io/apimachinery/pkg/runtime" @@ -29,8 +32,15 @@ func AddMissingHandlers(h printers.PrintHandler) { {Name: "Status", Type: "string"}, } + customResourceDefinitionColumnDefinitions := []metav1.TableColumnDefinition{ + {Name: "Name", Type: "string", Format: "name"}, + {Name: "Ceated At", Type: "string"}, + } + _ = h.TableHandler(apiServiceColumnDefinitions, printAPIService) _ = h.TableHandler(clusterVersionDefinitions, printClusterVersion) + _ = h.TableHandler(customResourceDefinitionColumnDefinitions, printCustomResourceDefinitionv1) + _ = h.TableHandler(customResourceDefinitionColumnDefinitions, printCustomResourceDefinitionv1beta1) } func printAPIService(obj *apiregistrationv1.APIService, options printers.GenerateOptions) ([]metav1.TableRow, error) { @@ -100,7 +110,7 @@ func printClusterVersion(obj *configv1.ClusterVersion, options printers.Generate } } } - since := helpers.GetAge(vars.MustGatherRootPath+"/namespaces", lastTransitionTime) + since := helpers.GetAge(vars.MustGatherRootPath, lastTransitionTime) row := metav1.TableRow{ Object: runtime.RawExtension{Object: obj}, @@ -108,3 +118,23 @@ func printClusterVersion(obj *configv1.ClusterVersion, options printers.Generate row.Cells = append(row.Cells, clusterOperatorName, version, available, progressing, since, status) return []metav1.TableRow{row}, nil } + +func printCustomResourceDefinitionv1(obj *apiextensionsv1.CustomResourceDefinition, options printers.GenerateOptions) ([]metav1.TableRow, error) { + creationTimestamp := obj.GetCreationTimestamp() + createdAt := creationTimestamp.UTC().Format(time.RFC3339Nano) + row := metav1.TableRow{ + Object: runtime.RawExtension{Object: obj}, + } + row.Cells = append(row.Cells, obj.Name, createdAt) + return []metav1.TableRow{row}, nil +} + +func printCustomResourceDefinitionv1beta1(obj *apiextensionsv1beta1.CustomResourceDefinition, options printers.GenerateOptions) ([]metav1.TableRow, error) { + creationTimestamp := obj.GetCreationTimestamp() + createdAt := creationTimestamp.UTC().Format(time.RFC3339Nano) + row := metav1.TableRow{ + Object: runtime.RawExtension{Object: obj}, + } + row.Cells = append(row.Cells, obj.Name, createdAt) + return []metav1.TableRow{row}, nil +} diff --git a/cmd/helpers/helpers.go b/cmd/helpers/helpers.go index 29d9516c..6ded44a4 100644 --- a/cmd/helpers/helpers.go +++ b/cmd/helpers/helpers.go @@ -5,6 +5,7 @@ import ( "bytes" "encoding/json" "fmt" + "io/fs" "io/ioutil" "log" "math/rand" @@ -12,7 +13,8 @@ import ( "strconv" "strings" "time" - "github.com/gmeghnag/omc/types" + + "github.com/gmeghnag/omc/types" "github.com/gmeghnag/omc/vars" "github.com/olekukonko/tablewriter" @@ -194,9 +196,16 @@ func ReadYaml(YamlPath string) []byte { } func GetAge(resourcefilePath string, resourceCreationTimeStamp v1.Time) string { - ResourceFile, err := os.Stat(resourcefilePath) + var ResourceFile fs.FileInfo + ResourceFile, err := os.Stat(resourcefilePath + "/timestamp") if err != nil { - return "Unknown" + ResourceFile, err = os.Stat(resourcefilePath + "/namespaces") + if err != nil { + ResourceFile, err = os.Stat(resourcefilePath + "/cluster-scoped-resources") + if err != nil { + return "Unknown" + } + } } t2 := ResourceFile.ModTime() diffTime := t2.Sub(resourceCreationTimeStamp.Time).String() @@ -439,4 +448,3 @@ func GetFromJsonPath(data interface{}, jsonPathTemplate string) string { jPath.Execute(buf, data) return buf.String() } - diff --git a/pkg/deserializer/deserializer.go b/pkg/deserializer/deserializer.go index 1274c07f..913b722a 100644 --- a/pkg/deserializer/deserializer.go +++ b/pkg/deserializer/deserializer.go @@ -23,6 +23,8 @@ import ( "k8s.io/kubernetes/pkg/apis/rbac" template "github.com/openshift/openshift-apiserver/pkg/template/apis/template" + apiextensionsv1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1" + apiextensionsv1beta1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1beta1" storage "k8s.io/kubernetes/pkg/apis/storage" // "k8s.io/client-go/kubernetes/scheme" @@ -74,6 +76,10 @@ func RawObjectToRuntimeObject(rawObject []byte, schema *runtime.Scheme) runtime. return &admissionregistration.ValidatingWebhookConfiguration{} case *admissionregistration.ValidatingAdmissionPolicy: return &admissionregistration.ValidatingAdmissionPolicy{} + case *apiextensionsv1.CustomResourceDefinition: + return &apiextensionsv1.CustomResourceDefinition{} + case *apiextensionsv1beta1.CustomResourceDefinition: + return &apiextensionsv1beta1.CustomResourceDefinition{} case *apiregistration.APIService: return &apiregistration.APIService{} case *apiserverinternal.StorageVersion: diff --git a/root/root.go b/root/root.go index 21477a62..4fe588f7 100644 --- a/root/root.go +++ b/root/root.go @@ -19,7 +19,6 @@ import ( "encoding/json" "errors" "fmt" - "io/ioutil" "os" "github.com/gmeghnag/omc/cmd" @@ -138,8 +137,7 @@ func initConfig() { omcConfigJson := types.Config{} file, _ := os.ReadFile(viper.ConfigFileUsed()) _ = json.Unmarshal([]byte(file), &omcConfigJson) - var contexts []types.Context - contexts = omcConfigJson.Contexts + contexts := omcConfigJson.Contexts for _, context := range contexts { if context.Current == "*" { vars.MustGatherRootPath = context.Path @@ -150,9 +148,9 @@ func initConfig() { } } if vars.MustGatherRootPath != "" { - exist, _ := helpers.Exists(vars.MustGatherRootPath + "/namespaces") + exist, _ := helpers.Exists(vars.MustGatherRootPath) if !exist { - files, err := ioutil.ReadDir(vars.MustGatherRootPath) + files, err := os.ReadDir(vars.MustGatherRootPath) if err != nil { fmt.Println(err) cmd.DeleteContext(vars.MustGatherRootPath, viper.ConfigFileUsed(), "")