-
Notifications
You must be signed in to change notification settings - Fork 2
Nim integration #159
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
Open
mrajagopal
wants to merge
9
commits into
main
Choose a base branch
from
nim-integration
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Nim integration #159
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
01d7d8b
Introduce support the NIM product
mrajagopal 401507d
Feat: Add pv, pvc, storageclass, api-resources, apiversions to the co…
mrajagopal fc8d207
Merge branch 'main' into nim-integration
mrajagopal 1e10c8b
Feat: Added clickhouse command to capture from a NIM deployment
mrajagopal f89f81f
Feat: dqlite dump for core DB
mrajagopal 879c4ea
Feat: dqlite dump for core, dpm, integrations and license databases
mrajagopal 4bcbf71
chore: Set dqlite and clickhouse data collection timeout to 10 minutes
mrajagopal b3cdd58
Feat: Provide the ability to exclude dqlite and clickhouse (timeserie…
mrajagopal cdf2a8e
Chore: Refactor
mrajagopal File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -24,9 +24,15 @@ import ( | |
"compress/gzip" | ||
"context" | ||
"fmt" | ||
"io" | ||
"log" | ||
"os" | ||
"path/filepath" | ||
"strconv" | ||
"time" | ||
|
||
helmClient "github.com/mittwald/go-helm-client" | ||
"github.com/nginxinc/nginx-k8s-supportpkg/pkg/crds" | ||
"io" | ||
corev1 "k8s.io/api/core/v1" | ||
crdClient "k8s.io/apiextensions-apiserver/pkg/client/clientset/clientset" | ||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
|
@@ -38,35 +44,32 @@ import ( | |
"k8s.io/client-go/tools/remotecommand" | ||
"k8s.io/client-go/util/homedir" | ||
metricsClient "k8s.io/metrics/pkg/client/clientset/versioned" | ||
"log" | ||
"os" | ||
"path/filepath" | ||
"strconv" | ||
"time" | ||
) | ||
|
||
type DataCollector struct { | ||
BaseDir string | ||
Namespaces []string | ||
Logger *log.Logger | ||
LogFile *os.File | ||
K8sRestConfig *rest.Config | ||
K8sCoreClientSet *kubernetes.Clientset | ||
K8sCrdClientSet *crdClient.Clientset | ||
K8sMetricsClientSet *metricsClient.Clientset | ||
K8sHelmClientSet map[string]helmClient.Client | ||
BaseDir string | ||
Namespaces []string | ||
Logger *log.Logger | ||
LogFile *os.File | ||
K8sRestConfig *rest.Config | ||
K8sCoreClientSet *kubernetes.Clientset | ||
K8sCrdClientSet *crdClient.Clientset | ||
K8sMetricsClientSet *metricsClient.Clientset | ||
K8sHelmClientSet map[string]helmClient.Client | ||
ExcludeDBData bool | ||
ExcludeTimeSeriesData bool | ||
} | ||
|
||
func NewDataCollector(namespaces ...string) (*DataCollector, error) { | ||
func NewDataCollector(collector *DataCollector) error { | ||
|
||
tmpDir, err := os.MkdirTemp("", "-pkg-diag") | ||
if err != nil { | ||
return nil, fmt.Errorf("unable to create temp directory: %s", err) | ||
return fmt.Errorf("unable to create temp directory: %s", err) | ||
} | ||
|
||
logFile, err := os.OpenFile(filepath.Join(tmpDir, "supportpkg.log"), os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644) | ||
if err != nil { | ||
return nil, fmt.Errorf("unable to create log file: %s", err) | ||
return fmt.Errorf("unable to create log file: %s", err) | ||
} | ||
|
||
// Find config | ||
|
@@ -77,30 +80,27 @@ func NewDataCollector(namespaces ...string) (*DataCollector, error) { | |
config, err := clientcmd.BuildConfigFromFlags("", kubeConfig) | ||
|
||
if err != nil { | ||
return nil, fmt.Errorf("unable to connect to k8s using file %s: %s", kubeConfig, err) | ||
} | ||
|
||
dc := DataCollector{ | ||
BaseDir: tmpDir, | ||
Namespaces: namespaces, | ||
LogFile: logFile, | ||
Logger: log.New(logFile, "", log.LstdFlags|log.LUTC|log.Lmicroseconds|log.Lshortfile), | ||
K8sHelmClientSet: make(map[string]helmClient.Client), | ||
return fmt.Errorf("unable to connect to k8s using file %s: %s", kubeConfig, err) | ||
} | ||
// Set up the DataCollector options | ||
collector.BaseDir = tmpDir | ||
collector.LogFile = logFile | ||
collector.Logger = log.New(logFile, "", log.LstdFlags|log.LUTC|log.Lmicroseconds|log.Lshortfile) | ||
collector.K8sHelmClientSet = make(map[string]helmClient.Client) | ||
|
||
//Initialize clients | ||
dc.K8sRestConfig = config | ||
dc.K8sCoreClientSet, _ = kubernetes.NewForConfig(config) | ||
dc.K8sCrdClientSet, _ = crdClient.NewForConfig(config) | ||
dc.K8sMetricsClientSet, _ = metricsClient.NewForConfig(config) | ||
for _, namespace := range dc.Namespaces { | ||
dc.K8sHelmClientSet[namespace], _ = helmClient.NewClientFromRestConf(&helmClient.RestConfClientOptions{ | ||
collector.K8sRestConfig = config | ||
collector.K8sCoreClientSet, _ = kubernetes.NewForConfig(config) | ||
collector.K8sCrdClientSet, _ = crdClient.NewForConfig(config) | ||
collector.K8sMetricsClientSet, _ = metricsClient.NewForConfig(config) | ||
for _, namespace := range collector.Namespaces { | ||
collector.K8sHelmClientSet[namespace], _ = helmClient.NewClientFromRestConf(&helmClient.RestConfClientOptions{ | ||
Options: &helmClient.Options{Namespace: namespace}, | ||
RestConfig: config, | ||
}) | ||
} | ||
|
||
return &dc, nil | ||
return nil | ||
} | ||
|
||
func (c *DataCollector) WrapUp(product string) (string, error) { | ||
|
@@ -266,3 +266,68 @@ func (c *DataCollector) AllNamespacesExist() bool { | |
|
||
return allExist | ||
} | ||
|
||
// CopyFileFromPod copies a file from a pod's container to the local filesystem. | ||
func (c *DataCollector) CopyFileFromPod(namespace, pod, container, srcPath, destPath string, ctx context.Context) error { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Cant' you just leverage on the |
||
cmd := []string{"tar", "cf", "-", "-C", filepath.Dir(srcPath), filepath.Base(srcPath)} | ||
req := c.K8sCoreClientSet.CoreV1().RESTClient().Post(). | ||
Namespace(namespace). | ||
Resource("pods"). | ||
Name(pod). | ||
SubResource("exec"). | ||
VersionedParams(&corev1.PodExecOptions{ | ||
Container: container, | ||
Command: cmd, | ||
Stdin: false, | ||
Stdout: true, | ||
Stderr: true, | ||
TTY: false, | ||
}, scheme.ParameterCodec) | ||
|
||
exec, err := remotecommand.NewSPDYExecutor(c.K8sRestConfig, "POST", req.URL()) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
// Stream the data from the Pod | ||
var stdout, stderr bytes.Buffer | ||
err = exec.StreamWithContext(ctx, remotecommand.StreamOptions{ | ||
Stdout: &stdout, | ||
Stderr: &stderr, | ||
}) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
// Create a local file to save the output | ||
localFile, err := os.Create(destPath) | ||
if err != nil { | ||
// return fmt.Errorf("failed to create local file: %w", err) | ||
return err | ||
} | ||
defer localFile.Close() | ||
|
||
// Untar the stream and write the content to the local file | ||
tarReader := tar.NewReader(&stdout) | ||
for { | ||
header, err := tarReader.Next() | ||
|
||
if err == io.EOF { | ||
break // End of tar archive | ||
} | ||
if err != nil { | ||
return err | ||
} | ||
|
||
// Ensure the tar file matches the expected file path | ||
if header.Name == filepath.Base(srcPath) { | ||
_, err = io.Copy(localFile, tarReader) | ||
if err != nil { | ||
return fmt.Errorf("failed to write to local file: %w", err) | ||
} | ||
break | ||
} | ||
} | ||
|
||
return nil | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.