Skip to content

Commit

Permalink
minor modifications
Browse files Browse the repository at this point in the history
  • Loading branch information
gmeghnag committed Mar 3, 2024
1 parent 88f327a commit bc732bc
Show file tree
Hide file tree
Showing 7 changed files with 80 additions and 15 deletions.
2 changes: 1 addition & 1 deletion cmd/certs/inspect.go
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
Expand Down
10 changes: 6 additions & 4 deletions cmd/get/get.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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/")
Expand Down Expand Up @@ -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)
Expand Down
21 changes: 21 additions & 0 deletions cmd/get/know-types.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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{
Expand Down
32 changes: 31 additions & 1 deletion cmd/get/missing-handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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) {
Expand Down Expand Up @@ -100,11 +110,31 @@ 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},
}
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
}
16 changes: 12 additions & 4 deletions cmd/helpers/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,16 @@ import (
"bytes"
"encoding/json"
"fmt"
"io/fs"
"io/ioutil"
"log"
"math/rand"
"os"
"strconv"
"strings"
"time"
"github.com/gmeghnag/omc/types"

"github.com/gmeghnag/omc/types"
"github.com/gmeghnag/omc/vars"

"github.com/olekukonko/tablewriter"
Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -439,4 +448,3 @@ func GetFromJsonPath(data interface{}, jsonPathTemplate string) string {
jPath.Execute(buf, data)
return buf.String()
}

6 changes: 6 additions & 0 deletions pkg/deserializer/deserializer.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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:
Expand Down
8 changes: 3 additions & 5 deletions root/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ import (
"encoding/json"
"errors"
"fmt"
"io/ioutil"
"os"

"github.com/gmeghnag/omc/cmd"
Expand Down Expand Up @@ -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
Expand All @@ -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(), "")
Expand Down

0 comments on commit bc732bc

Please sign in to comment.