Skip to content

Commit

Permalink
Merge pull request #1 from fedepaol/initialimport
Browse files Browse the repository at this point in the history
Initial import
  • Loading branch information
fedepaol authored Feb 3, 2022
2 parents 8a5e033 + 93b906e commit b49c03f
Show file tree
Hide file tree
Showing 8 changed files with 3,126 additions and 0 deletions.
62 changes: 62 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
# Kubernetes Reporter

This is a small library to produce a readeable dump of the required information contained in a k8s cluster.

The idea is to use it after an end to end test failure, to collect information useful for the failure.

## Usage

Init the reporter with the information about namespaces and the resources that requires to be dumped:

```go
// When using custom crds, we need to add them to the scheme
addToScheme := func(s *runtime.Scheme) error {
err := sriovv1.AddToScheme(s)
if err != nil {
return err
}
err = metallbv1beta1.AddToScheme(s)
if err != nil {
return err
}
return nil
}

// The namespaces we want to dump resources for (including pods and pod logs)
dumpNamespace := func(ns string) bool {
if strings.HasPrefix(ns, "test") {
return true
}
return false
}

// The list of CRDs we want to dump
crds := []k8sreporter.CRData{
{Cr: &sriovv1.SriovNetworkNodePolicyList{}},
{Cr: &sriovv1.SriovNetworkList{}},
{Cr: &sriovv1.SriovNetworkNodePolicyList{}},
{Cr: &sriovv1.SriovOperatorConfigList{}},
{Cr: &metallbv1beta1.MetalLBList{}},
}
```

Create the reporter and invoke dump (note reportbase must exists):

```go
reporter, err := k8sreporter.New(*kubeconfig, addToScheme, dumpNamespace, "/reportbase", crds...)
if err != nil {
log.Fatalf("Failed to initialize the reporter %s", err)
}
reporter.Dump(10*time.Minute, "nameofthetest")
```

The output will look like

```bash
├── reportbase
│ └── test
│ ├── crs.log
│ ├── metallb-system-pods_logs.log
│ ├── metallb-system-pods_specs.log
│ └── nodes.log
```
54 changes: 54 additions & 0 deletions client.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package k8sreporter

import (
"fmt"
"os"

"github.com/golang/glog"
clientconfigv1 "github.com/openshift/client-go/config/clientset/versioned/typed/config/v1"
"k8s.io/apimachinery/pkg/runtime"
appsv1client "k8s.io/client-go/kubernetes/typed/apps/v1"
corev1client "k8s.io/client-go/kubernetes/typed/core/v1"
"k8s.io/client-go/rest"
"k8s.io/client-go/tools/clientcmd"
"sigs.k8s.io/controller-runtime/pkg/client"
runtimeclient "sigs.k8s.io/controller-runtime/pkg/client"
)

type clientSet struct {
corev1client.CoreV1Interface
clientconfigv1.ConfigV1Interface
appsv1client.AppsV1Interface
runtimeclient.Client
}

// New returns a *ClientBuilder with the given kubeconfig.
func newClient(kubeconfig string, crScheme *runtime.Scheme) (*clientSet, error) {
var config *rest.Config
var err error

if kubeconfig == "" {
kubeconfig = os.Getenv("KUBECONFIG")
}

if kubeconfig != "" {
glog.V(4).Infof("Loading kube client config from path %q", kubeconfig)
config, err = clientcmd.BuildConfigFromFlags("", kubeconfig)
} else {
glog.V(4).Infof("Using in-cluster kube client config")
config, err = rest.InClusterConfig()
}
if err != nil {
return nil, fmt.Errorf("Failed to init client")
}

clientSet := &clientSet{}
clientSet.CoreV1Interface = corev1client.NewForConfigOrDie(config)
clientSet.ConfigV1Interface = clientconfigv1.NewForConfigOrDie(config)
clientSet.AppsV1Interface = appsv1client.NewForConfigOrDie(config)

clientSet.Client, err = runtimeclient.New(config, client.Options{
Scheme: crScheme,
})
return clientSet, nil
}
108 changes: 108 additions & 0 deletions example/go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
module reporterexample

go 1.17

require (
github.com/k8snetworkplumbingwg/sriov-network-operator v1.1.0
github.com/metallb/metallb-operator v0.10.2
github.com/openshift-kni/k8sreporter v0.0.0
k8s.io/apimachinery v0.23.3
)

require (
github.com/Masterminds/goutils v1.1.1 // indirect
github.com/Masterminds/semver/v3 v3.1.1 // indirect
github.com/Masterminds/sprig/v3 v3.2.2 // indirect
github.com/ajeddeloh/go-json v0.0.0-20170920214419-6a2fe990e083 // indirect
github.com/clarketm/json v1.14.1 // indirect
github.com/coreos/fcct v0.5.0 // indirect
github.com/coreos/go-semver v0.3.0 // indirect
github.com/coreos/go-systemd v0.0.0-20190719114852-fd7a80b32e1f // indirect
github.com/coreos/go-systemd/v22 v22.1.0 // indirect
github.com/coreos/ign-converter v0.0.0-20200629171308-e40a44f244c5 // indirect
github.com/coreos/ignition v0.35.0 // indirect
github.com/coreos/ignition/v2 v2.3.0 // indirect
github.com/coreos/vcontext v0.0.0-20191017033345-260217907eb5 // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/evanphx/json-patch v4.12.0+incompatible // indirect
github.com/ghodss/yaml v1.0.0 // indirect
github.com/go-logr/logr v1.2.0 // indirect
github.com/gogo/protobuf v1.3.2 // indirect
github.com/golang/glog v1.0.0 // indirect
github.com/golang/protobuf v1.5.2 // indirect
github.com/google/go-cmp v0.5.5 // indirect
github.com/google/gofuzz v1.1.0 // indirect
github.com/google/uuid v1.1.2 // indirect
github.com/googleapis/gnostic v0.5.5 // indirect
github.com/hashicorp/golang-lru v0.5.4 // indirect
github.com/huandu/xstrings v1.3.2 // indirect
github.com/imdario/mergo v0.3.12 // indirect
github.com/json-iterator/go v1.1.12 // indirect
github.com/k8snetworkplumbingwg/network-attachment-definition-client v0.0.0-20200626054723-37f83d1996bc // indirect
github.com/kennygrant/sanitize v1.2.4 // indirect
github.com/mitchellh/copystructure v1.2.0 // indirect
github.com/mitchellh/reflectwalk v1.0.2 // indirect
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
github.com/modern-go/reflect2 v1.0.2 // indirect
github.com/onsi/ginkgo v1.16.5 // indirect
github.com/openshift/api v0.0.0-20200829102639-8a3a835f1acf // indirect
github.com/openshift/client-go v0.0.0-20200827190008-3062137373b5 // indirect
github.com/openshift/machine-config-operator v0.0.1-0.20201023110058-6c8bd9b2915c // indirect
github.com/pkg/errors v0.9.1 // indirect
github.com/shopspring/decimal v1.2.0 // indirect
github.com/spf13/cast v1.3.1 // indirect
github.com/spf13/pflag v1.0.5 // indirect
github.com/stretchr/testify v1.7.0 // indirect
github.com/vincent-petithory/dataurl v0.0.0-20160330182126-9a301d65acbb // indirect
go4.org v0.0.0-20200104003542-c7e774b10ea0 // indirect
golang.org/x/crypto v0.0.0-20210220033148-5ea612d1eb83 // indirect
golang.org/x/net v0.0.0-20211209124913-491a49abca63 // indirect
golang.org/x/oauth2 v0.0.0-20210819190943-2bc19b11175f // indirect
golang.org/x/sys v0.0.0-20211029165221-6e7872819dc8 // indirect
golang.org/x/term v0.0.0-20210615171337-6886f2dfbf5b // indirect
golang.org/x/text v0.3.7 // indirect
golang.org/x/time v0.0.0-20210723032227-1f47c861a9ac // indirect
google.golang.org/appengine v1.6.7 // indirect
google.golang.org/protobuf v1.27.1 // indirect
gopkg.in/inf.v0 v0.9.1 // indirect
gopkg.in/yaml.v2 v2.4.0 // indirect
gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b // indirect
k8s.io/api v0.23.3 // indirect
k8s.io/apiextensions-apiserver v0.23.0 // indirect
k8s.io/client-go v0.23.3 // indirect
k8s.io/klog/v2 v2.30.0 // indirect
k8s.io/kube-openapi v0.0.0-20211115234752-e816edb12b65 // indirect
k8s.io/utils v0.0.0-20211116205334-6203023598ed // indirect
sigs.k8s.io/controller-runtime v0.11.0 // indirect
sigs.k8s.io/structured-merge-diff/v4 v4.2.1 // indirect
sigs.k8s.io/yaml v1.3.0 // indirect
)

replace (
github.com/openshift-kni/k8sreporter v0.0.0 => /home/fedepaol/devel/openshift-kni/k8s-reporter/
k8s.io/api => k8s.io/api v0.21.1
k8s.io/apiextensions-apiserver => k8s.io/apiextensions-apiserver v0.21.1
k8s.io/apimachinery => k8s.io/apimachinery v0.21.1
k8s.io/apiserver => k8s.io/apiserver v0.21.1
k8s.io/cli-runtime => k8s.io/cli-runtime v0.21.1
k8s.io/client-go => k8s.io/client-go v0.21.1
k8s.io/cloud-provider => k8s.io/cloud-provider v0.21.1
k8s.io/cluster-bootstrap => k8s.io/cluster-bootstrap v0.21.1
k8s.io/code-generator => k8s.io/code-generator v0.21.1
k8s.io/component-base => k8s.io/component-base v0.21.1
k8s.io/component-helpers => k8s.io/component-helpers v0.21.1
k8s.io/controller-manager => k8s.io/controller-manager v0.21.1
k8s.io/cri-api => k8s.io/cri-api v0.21.1
k8s.io/csi-translation-lib => k8s.io/csi-translation-lib v0.21.1
k8s.io/kube-aggregator => k8s.io/kube-aggregator v0.21.1
k8s.io/kube-controller-manager => k8s.io/kube-controller-manager v0.21.1
k8s.io/kube-proxy => k8s.io/kube-proxy v0.21.1
k8s.io/kube-scheduler => k8s.io/kube-scheduler v0.21.1
k8s.io/kubectl => k8s.io/kubectl v0.21.1
k8s.io/kubelet => k8s.io/kubelet v0.21.1
k8s.io/kubernetes => k8s.io/kubernetes v1.20.4
k8s.io/legacy-cloud-providers => k8s.io/legacy-cloud-providers v0.21.1
k8s.io/metrics => k8s.io/metrics v0.21.1
k8s.io/mount-utils => k8s.io/mount-utils v0.21.1
k8s.io/sample-apiserver => k8s.io/sample-apiserver v0.21.1
)
Loading

0 comments on commit b49c03f

Please sign in to comment.