Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
New provider for CAPV resource enumeration
This patch adds a new capv_provider() which is used to enumerate the compute resources for the management/or workload clusters managed by CAPV. It also extends the kube_config() directive to accept a CAPI provider implementation as an input, thus enabling workload cluster kube configs to be discoverable by the directive and used in the script subsequently. Also, this patch updates the generation of sshArgs by including the private key path. In addition, it also updates the SearchParams struct from the k8s package to accept the search parameters directly as golang slices. Adds private key path to ssh args
- Loading branch information
Showing
19 changed files
with
477 additions
and
199 deletions.
There are no files selected for viewing
This file contains 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 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 |
---|---|---|
@@ -0,0 +1,32 @@ | ||
conf = crashd_config(workdir=args.workdir) | ||
ssh_conf = ssh_config(username="capv", private_key_path=args.private_key) | ||
kube_conf = kube_config(path=args.mc_config) | ||
|
||
#list out management and workload cluster nodes | ||
wc_provider=capv_provider( | ||
workload_cluster=args.cluster_name, | ||
ssh_config=ssh_conf, | ||
kube_config=kube_conf | ||
) | ||
nodes = resources(provider=wc_provider) | ||
|
||
capture(cmd="sudo df -i", resources=nodes) | ||
capture(cmd="sudo crictl info", resources=nodes) | ||
capture(cmd="df -h /var/lib/containerd", resources=nodes) | ||
capture(cmd="sudo systemctl status kubelet", resources=nodes) | ||
capture(cmd="sudo systemctl status containerd", resources=nodes) | ||
capture(cmd="sudo journalctl -xeu kubelet", resources=nodes) | ||
|
||
capture(cmd="sudo cat /var/log/cloud-init-output.log", resources=nodes) | ||
capture(cmd="sudo cat /var/log/cloud-init.log", resources=nodes) | ||
|
||
#add code to collect pod info from cluster | ||
wc_kube_conf = kube_config(capi_provider = wc_provider) | ||
|
||
pod_ns=["default", "kube-system"] | ||
|
||
kube_capture(what="logs", namespaces=pod_ns, kube_config=wc_kube_conf) | ||
kube_capture(what="objects", kinds=["pods", "services"], namespaces=pod_ns, kube_config=wc_kube_conf) | ||
kube_capture(what="objects", kinds=["deployments", "replicasets"], groups=["apps"], namespaces=pod_ns, kube_config=wc_kube_conf) | ||
|
||
archive(output_file="diagnostics.tar.gz", source_paths=[conf.workdir]) |
This file contains 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 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 |
---|---|---|
@@ -0,0 +1,38 @@ | ||
// Copyright (c) 2020 VMware, Inc. All Rights Reserved. | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
package k8s | ||
|
||
import ( | ||
"encoding/base64" | ||
"fmt" | ||
"io" | ||
"io/ioutil" | ||
"os" | ||
|
||
"github.com/pkg/errors" | ||
"github.com/vladimirvivien/echo" | ||
) | ||
|
||
// FetchWorkloadConfig... | ||
func FetchWorkloadConfig(name, mgmtKubeConfigPath string) (string, error) { | ||
var filePath string | ||
cmdStr := fmt.Sprintf(`kubectl get secrets/%s-kubeconfig --template '{{.data.value}}' --kubeconfig %s`, name, mgmtKubeConfigPath) | ||
p := echo.New().RunProc(cmdStr) | ||
if p.Err() != nil { | ||
return filePath, fmt.Errorf("kubectl get secrets failed: %s: %s", p.Err(), p.Result()) | ||
} | ||
|
||
f, err := ioutil.TempFile(os.TempDir(), fmt.Sprintf("%s-workload-config", name)) | ||
if err != nil { | ||
return filePath, errors.Wrap(err, "Cannot create temporary file") | ||
} | ||
filePath = f.Name() | ||
defer f.Close() | ||
|
||
base64Dec := base64.NewDecoder(base64.StdEncoding, p.Out()) | ||
if _, err := io.Copy(f, base64Dec); err != nil { | ||
return filePath, errors.Wrap(err, "error decoding workload kubeconfig") | ||
} | ||
return filePath, nil | ||
} |
This file contains 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 |
---|---|---|
@@ -0,0 +1,63 @@ | ||
// Copyright (c) 2020 VMware, Inc. All Rights Reserved. | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
package k8s | ||
|
||
import ( | ||
"github.com/pkg/errors" | ||
coreV1 "k8s.io/api/core/v1" | ||
"k8s.io/apimachinery/pkg/runtime" | ||
) | ||
|
||
func GetNodeAddresses(kubeconfigPath string, labels, names []string) ([]string, error) { | ||
client, err := New(kubeconfigPath) | ||
if err != nil { | ||
return nil, errors.Wrap(err, "could not initialize search client") | ||
} | ||
|
||
nodes, err := getNodes(client, names, labels) | ||
if err != nil { | ||
return nil, errors.Wrapf(err, "could not fetch nodes") | ||
} | ||
|
||
var nodeIps []string | ||
for _, node := range nodes { | ||
nodeIps = append(nodeIps, getNodeInternalIP(node)) | ||
} | ||
return nodeIps, nil | ||
} | ||
|
||
func getNodes(k8sc *Client, names, labels []string) ([]*coreV1.Node, error) { | ||
nodeResults, err := k8sc.Search(SearchParams{ | ||
Groups: []string{"core"}, | ||
Kinds: []string{"nodes"}, | ||
Names: names, | ||
Labels: labels, | ||
}) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
// collate | ||
var nodes []*coreV1.Node | ||
for _, result := range nodeResults { | ||
for _, item := range result.List.Items { | ||
node := new(coreV1.Node) | ||
if err := runtime.DefaultUnstructuredConverter.FromUnstructured(item.Object, &node); err != nil { | ||
return nil, err | ||
} | ||
nodes = append(nodes, node) | ||
} | ||
} | ||
return nodes, nil | ||
} | ||
|
||
func getNodeInternalIP(node *coreV1.Node) (ipAddr string) { | ||
for _, addr := range node.Status.Addresses { | ||
if addr.Type == "InternalIP" { | ||
ipAddr = addr.Address | ||
return | ||
} | ||
} | ||
return | ||
} |
This file contains 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 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 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 |
---|---|---|
@@ -0,0 +1,26 @@ | ||
// Copyright (c) 2020 VMware, Inc. All Rights Reserved. | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
package provider | ||
|
||
import ( | ||
"fmt" | ||
|
||
"github.com/pkg/errors" | ||
"github.com/vmware-tanzu/crash-diagnostics/k8s" | ||
) | ||
|
||
// KubeConfig returns the kubeconfig that needs to be used by the provider. | ||
// The path of the management kubeconfig file gets returned if the workload cluster name is empty | ||
func KubeConfig(mgmtKubeConfigPath, workloadClusterName string) (string, error) { | ||
var err error | ||
|
||
kubeConfigPath := mgmtKubeConfigPath | ||
if len(workloadClusterName) != 0 { | ||
kubeConfigPath, err = k8s.FetchWorkloadConfig(workloadClusterName, mgmtKubeConfigPath) | ||
if err != nil { | ||
err = errors.Wrap(err, fmt.Sprintf("could not fetch kubeconfig for workload cluster %s", workloadClusterName)) | ||
} | ||
} | ||
return kubeConfigPath, err | ||
} |
This file contains 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.