Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add describe control to all k8s resources #3589

Merged
merged 3 commits into from
May 17, 2019
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 48 additions & 0 deletions probe/kubernetes/client.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
package kubernetes

import (
"bytes"
"errors"
"fmt"
"io"
"io/ioutil"
"strings"
"sync"
"time"
Expand All @@ -14,6 +16,7 @@ import (
snapshot "github.com/openebs/k8s-snapshot-client/snapshot/pkg/client/clientset/versioned"
"github.com/pborman/uuid"
log "github.com/sirupsen/logrus"
apiappsv1 "k8s.io/api/apps/v1"
apiappsv1beta1 "k8s.io/api/apps/v1beta1"
apibatchv1 "k8s.io/api/batch/v1"
apibatchv1beta1 "k8s.io/api/batch/v1beta1"
Expand All @@ -22,6 +25,7 @@ import (
apiextensionsv1beta1 "k8s.io/api/extensions/v1beta1"
storagev1 "k8s.io/api/storage/v1"
apierrors "k8s.io/apimachinery/pkg/api/errors"
apimeta "k8s.io/apimachinery/pkg/api/meta"
"k8s.io/apimachinery/pkg/api/resource"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/fields"
Expand All @@ -32,6 +36,8 @@ import (
"k8s.io/client-go/tools/cache"
"k8s.io/client-go/tools/clientcmd"
clientcmdapi "k8s.io/client-go/tools/clientcmd/api"
kubectldescribe "k8s.io/kubernetes/pkg/kubectl/describe"
kubectl "k8s.io/kubernetes/pkg/kubectl/describe/versioned"
)

// Client keeps track of running kubernetes pods and services
Expand All @@ -55,12 +61,28 @@ type Client interface {
CloneVolumeSnapshot(namespaceID, volumeSnapshotID, persistentVolumeClaimID, capacity string) error
CreateVolumeSnapshot(namespaceID, persistentVolumeClaimID, capacity string) error
GetLogs(namespaceID, podID string, containerNames []string) (io.ReadCloser, error)
Describe(namespaceID, resourceID string, groupKind schema.GroupKind, restMapping apimeta.RESTMapping) (io.ReadCloser, error)
DeletePod(namespaceID, podID string) error
DeleteVolumeSnapshot(namespaceID, volumeSnapshotID string) error
ScaleUp(namespaceID, id string) error
ScaleDown(namespaceID, id string) error
}

// ResourceMap is the mapping of resource and their GroupKind
var ResourceMap = map[string]schema.GroupKind{
"Pod": {Group: apiv1.GroupName, Kind: "Pod"},
"Service": {Group: apiv1.GroupName, Kind: "Service"},
"Deployment": {Group: apiappsv1.GroupName, Kind: "Deployment"},
"DaemonSet": {Group: apiappsv1.GroupName, Kind: "DaemonSet"},
"StatefulSet": {Group: apiappsv1.GroupName, Kind: "StatefulSet"},
"Job": {Group: apibatchv1.GroupName, Kind: "Job"},
"CronJob": {Group: apibatchv1.GroupName, Kind: "CronJob"},
"Node": {Group: apiv1.GroupName, Kind: "Node"},
"PersistentVolume": {Group: apiv1.GroupName, Kind: "PersistentVolume"},
"PersistentVolumeClaim": {Group: apiv1.GroupName, Kind: "PersistentVolumeClaim"},
"StorageClass": {Group: storagev1.GroupName, Kind: "StorageClass"},
}

type client struct {
quit chan struct{}
client *kubernetes.Clientset
Expand Down Expand Up @@ -541,6 +563,32 @@ func (c *client) GetLogs(namespaceID, podID string, containerNames []string) (io
return NewLogReadCloser(readClosersWithLabel), nil
}

func (c *client) Describe(namespaceID, resourceID string, groupKind schema.GroupKind, restMapping apimeta.RESTMapping) (io.ReadCloser, error) {
readClosersWithLabel := map[io.ReadCloser]string{}
restConfig, err := rest.InClusterConfig()
if err != nil {
return nil, err
}
describer, ok := kubectl.DescriberFor(groupKind, restConfig)
if !ok {
describer, ok = kubectl.GenericDescriberFor(&restMapping, restConfig)
if !ok {
return nil, errors.New("Resource not found")
}
}
describerSetting := kubectldescribe.DescriberSettings{
ShowEvents: true,
}
obj, err := describer.Describe(namespaceID, resourceID, describerSetting)
if err != nil {
return nil, err
}
formattedObj := ioutil.NopCloser(bytes.NewReader([]byte(obj)))
readClosersWithLabel[formattedObj] = "describe"

return NewLogReadCloser(readClosersWithLabel), nil
}

func (c *client) DeletePod(namespaceID, podID string) error {
return c.client.CoreV1().Pods(namespaceID).Delete(podID, &metav1.DeleteOptions{})
}
Expand Down
Loading