-
Notifications
You must be signed in to change notification settings - Fork 75
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #107 from Lyt99/feature/btfhack
feat(btfhack): add btfhack & replace initContainer image
- Loading branch information
Showing
16 changed files
with
582 additions
and
6 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
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,9 @@ | ||
package main | ||
|
||
import ( | ||
"github.com/alibaba/kubeskoop/pkg/exporter/btfhack" | ||
) | ||
|
||
func main() { | ||
btfhack.Execute() | ||
} |
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
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,70 @@ | ||
package btfhack | ||
|
||
import ( | ||
"fmt" | ||
"log" | ||
"os/exec" | ||
|
||
"github.com/alibaba/kubeskoop/pkg/exporter/bpfutil" | ||
|
||
"github.com/spf13/cobra" | ||
) | ||
|
||
const ( | ||
defaultBTFDstPath = "/etc/net-exporter/btf" | ||
) | ||
|
||
// cpCmd represents the cp command | ||
var ( | ||
cpCmd = &cobra.Command{ | ||
Use: "discover", | ||
Short: "copy or download appropriate btf file to dst path", | ||
Run: func(cmd *cobra.Command, args []string) { | ||
if btfSrcPath == "" { | ||
btfSrcPath = defaultBTFPath | ||
} | ||
if btfDstPath == "" { | ||
btfDstPath = defaultBTFDstPath | ||
} | ||
|
||
btffile, err := bpfutil.FindBTFFileWithPath(btfSrcPath) | ||
if err == nil { | ||
err := copyBtfFile(btffile, btfDstPath) | ||
if err != nil { | ||
log.Fatalf("Failed copy btf file: %s\n", err) | ||
} | ||
log.Printf("Copy btf file %s to %s succeed\n", btffile, btfDstPath) | ||
return | ||
} | ||
|
||
btffile, err = downloadBTFOnline(btfDstPath) | ||
if err != nil { | ||
log.Printf("Download btf error: %s\n", err) | ||
return | ||
} | ||
log.Printf("Download btf file %s succeed\n", btffile) | ||
}, | ||
} | ||
|
||
btfDstPath string | ||
) | ||
|
||
func copyBtfFile(path, dstPath string) error { | ||
cmdToExecute := exec.Command("cp", path, dstPath) | ||
output, err := cmdToExecute.CombinedOutput() | ||
if err != nil { | ||
return fmt.Errorf("load btf with:%s err:%s", output, err) | ||
} | ||
|
||
log.Printf("load btf %s to %s succeed", path, dstPath) | ||
return nil | ||
} | ||
|
||
func init() { | ||
rootCmd.AddCommand(cpCmd) | ||
|
||
flags := cpCmd.PersistentFlags() | ||
|
||
flags.StringVarP(&btfSrcPath, "src", "s", "", "btf source file") | ||
flags.StringVarP(&btfDstPath, "dst", "p", "", "btf destination directory") | ||
} |
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,90 @@ | ||
package btfhack | ||
|
||
import ( | ||
"fmt" | ||
"io" | ||
"log" | ||
"net" | ||
"net/http" | ||
"net/url" | ||
"os" | ||
"path" | ||
"time" | ||
|
||
"github.com/alibaba/kubeskoop/pkg/exporter/bpfutil" | ||
) | ||
|
||
const ( | ||
EnvBTFDownloadURL = "BTF_DOWNLOAD_URL" | ||
OpenBTFURL = "https://mirrors.openanolis.cn/coolbpf/btf/" | ||
) | ||
|
||
func downloadBTFOnline(btfDstPath string) (string, error) { | ||
release, err := bpfutil.KernelRelease() | ||
if err != nil { | ||
return "", err | ||
} | ||
arch, err := bpfutil.KernelArch() | ||
if err != nil { | ||
return "", err | ||
} | ||
|
||
filename := fmt.Sprintf("vmlinux-%s", release) | ||
dst := path.Join(btfDstPath, filename) | ||
urlPath := fmt.Sprintf("%s/%s", arch, filename) | ||
if envURL, ok := os.LookupEnv(EnvBTFDownloadURL); ok { | ||
downloadURL, err := url.JoinPath(envURL, urlPath) | ||
if err != nil { | ||
return "", err | ||
} | ||
err = downloadTo(downloadURL, dst) | ||
if err == nil { | ||
log.Printf("Downloaded btf file from %s", downloadURL) | ||
return dst, nil | ||
} | ||
log.Printf("Download btf file failed from %s: %s", downloadURL, err) | ||
} | ||
|
||
downloadURL, err := url.JoinPath(OpenBTFURL, urlPath) | ||
if err != nil { | ||
return "", err | ||
} | ||
err = downloadTo(downloadURL, dst) | ||
if err != nil { | ||
return "", fmt.Errorf("download btf file failed from %s: %w", downloadURL, err) | ||
} | ||
return dst, nil | ||
} | ||
|
||
func downloadTo(url, dst string) error { | ||
tr := &http.Transport{ | ||
Dial: (&net.Dialer{ | ||
Timeout: 1 * time.Second, | ||
KeepAlive: 30 * time.Second, | ||
}).Dial, | ||
} | ||
|
||
client := http.Client{ | ||
Timeout: 50 * time.Second, | ||
Transport: tr, | ||
} | ||
|
||
res, err := client.Get(url) | ||
if err != nil { | ||
return err | ||
} | ||
defer res.Body.Close() | ||
if res.StatusCode != http.StatusOK { | ||
return fmt.Errorf("got status code %d", res.StatusCode) | ||
} | ||
|
||
f, err := os.Create(dst) | ||
if err != nil { | ||
return err | ||
} | ||
_, err = io.Copy(f, res.Body) | ||
if err != nil { | ||
return err | ||
} | ||
return 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,23 @@ | ||
package btfhack | ||
|
||
import ( | ||
"os" | ||
|
||
"github.com/spf13/cobra" | ||
) | ||
|
||
const ( | ||
defaultBTFPath = "/etc/btf" | ||
) | ||
|
||
var rootCmd = &cobra.Command{ | ||
Use: "btfhack", | ||
Short: "A tool to automatically discover btf file from local path or online", | ||
} | ||
|
||
func Execute() { | ||
err := rootCmd.Execute() | ||
if err != nil { | ||
os.Exit(1) | ||
} | ||
} |
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,55 @@ | ||
package btfhack | ||
|
||
import ( | ||
"log" | ||
|
||
"github.com/alibaba/kubeskoop/pkg/exporter/bpfutil" | ||
"github.com/alibaba/kubeskoop/pkg/exporter/testbtf" | ||
|
||
"github.com/cilium/ebpf/btf" | ||
"github.com/spf13/cobra" | ||
) | ||
|
||
// testCmd represents the test command | ||
var ( | ||
testCmd = &cobra.Command{ | ||
Use: "test", | ||
Short: "test btf support locally", | ||
Run: func(cmd *cobra.Command, args []string) { | ||
if btfSrcPath == "" { | ||
btfSrcPath = defaultBTFPath | ||
} | ||
|
||
file, err := bpfutil.FindBTFFileWithPath(btfSrcPath) | ||
if err != nil { | ||
log.Printf("failed with %s", err) | ||
return | ||
} | ||
|
||
spec, err := bpfutil.LoadBTFFromFile(file) | ||
if err != nil { | ||
log.Printf("load btf spec faiild with %s", err) | ||
return | ||
} | ||
|
||
if err := testBTFAvailable(spec); err != nil { | ||
log.Printf("btf test failed: %v", err) | ||
} else { | ||
log.Printf("btf test ok") | ||
} | ||
}, | ||
} | ||
|
||
btfSrcPath string | ||
) | ||
|
||
func init() { | ||
rootCmd.AddCommand(testCmd) | ||
flags := testCmd.PersistentFlags() | ||
|
||
flags.StringVarP(&btfSrcPath, "src", "s", "", "btf source file") | ||
} | ||
|
||
func testBTFAvailable(spec *btf.Spec) error { | ||
return testbtf.RunBTFTest(spec) | ||
} |
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,20 @@ | ||
package btfhack | ||
|
||
import ( | ||
"github.com/alibaba/kubeskoop/version" | ||
"github.com/spf13/cobra" | ||
) | ||
|
||
var ( | ||
versionCmd = &cobra.Command{ | ||
Use: "version", | ||
Short: "show version", | ||
Run: func(_ *cobra.Command, args []string) { | ||
version.PrintVersion() | ||
}, | ||
} | ||
) | ||
|
||
func init() { | ||
rootCmd.AddCommand(versionCmd) | ||
} |
Oops, something went wrong.