Skip to content

Commit

Permalink
Merge pull request #35 from fromanirh/add-machineinfo-command
Browse files Browse the repository at this point in the history
add machineinfo command
  • Loading branch information
ffromani authored Oct 4, 2021
2 parents 423f77b + bae6c18 commit d68a039
Show file tree
Hide file tree
Showing 9 changed files with 663 additions and 2 deletions.
2 changes: 2 additions & 0 deletions cmd/knit/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (
"github.com/openshift-kni/debug-tools/pkg/knit/cmd"
"github.com/openshift-kni/debug-tools/pkg/knit/cmd/ghw"
"github.com/openshift-kni/debug-tools/pkg/knit/cmd/k8s"
"github.com/openshift-kni/debug-tools/pkg/knit/cmd/machineinfo"
)

func main() {
Expand All @@ -31,6 +32,7 @@ func main() {
ghw.NewLscpuCommand,
ghw.NewLspciCommand,
ghw.NewLstopoCommand,
machineinfo.NewMachineInfoCommand,
)
if err := root.Execute(); err != nil {
fmt.Fprintf(os.Stderr, "%v\n", err)
Expand Down
4 changes: 3 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,14 @@ go 1.15

require (
github.com/davecgh/go-spew v1.1.1
github.com/google/go-cmp v0.5.2
github.com/google/cadvisor v0.39.0
github.com/google/go-cmp v0.5.4
github.com/jaypipes/ghw v0.8.1-0.20210605191321-eb162add542b
github.com/onsi/ginkgo v1.11.0
github.com/onsi/gomega v1.7.0
github.com/spf13/cobra v1.1.1
github.com/spf13/pflag v1.0.5
k8s.io/klog/v2 v2.8.0
k8s.io/kubelet v0.0.0
k8s.io/kubernetes v0.0.0-00010101000000-000000000000
)
Expand Down
5 changes: 4 additions & 1 deletion go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -278,8 +278,9 @@ github.com/google/go-cmp v0.3.0/go.mod h1:8QqcDgzrUqlUb/G2PQTWiueGozuR1884gddMyw
github.com/google/go-cmp v0.3.1/go.mod h1:8QqcDgzrUqlUb/G2PQTWiueGozuR1884gddMywk6iLU=
github.com/google/go-cmp v0.4.0/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
github.com/google/go-cmp v0.5.0/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
github.com/google/go-cmp v0.5.2 h1:X2ev0eStA3AbceY54o37/0PQ/UWqKEiiO2dKL5OPaFM=
github.com/google/go-cmp v0.5.2/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
github.com/google/go-cmp v0.5.4 h1:L8R9j+yAqZuZjsqh/z+F1NCffTKKLShY6zXTItVIZ8M=
github.com/google/go-cmp v0.5.4/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
github.com/google/gofuzz v1.0.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg=
github.com/google/gofuzz v1.1.0 h1:Hsa8mG0dQ46ij8Sl2AYJDUv1oA9/d6Vk+3LG99Oe02g=
github.com/google/gofuzz v1.1.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg=
Expand Down Expand Up @@ -407,6 +408,7 @@ github.com/miekg/dns v1.0.14/go.mod h1:W1PPwlIAgtquWBMBEV9nkV9Cazfe8ScdGz/Lj7v3N
github.com/miekg/dns v1.1.3/go.mod h1:W1PPwlIAgtquWBMBEV9nkV9Cazfe8ScdGz/Lj7v3Nrg=
github.com/miekg/dns v1.1.35/go.mod h1:KNUDUusw/aVsxyTYZM1oqvCicbwhgbNgztCETuNZ7xM=
github.com/mindprince/gonvml v0.0.0-20190828220739-9ebdce4bb989/go.mod h1:2eu9pRWp8mo84xCg6KswZ+USQHjwgRhNp06sozOdsTY=
github.com/mistifyio/go-zfs v2.1.2-0.20190413222219-f784269be439+incompatible h1:aKW/4cBs+yK6gpqU3K/oIwk9Q/XICqd3zOX/UFuvqmk=
github.com/mistifyio/go-zfs v2.1.2-0.20190413222219-f784269be439+incompatible/go.mod h1:8AuVvqP/mXw1px98n46wfvcGfQ4ci2FwoAjKYxuo3Z4=
github.com/mitchellh/cli v1.0.0/go.mod h1:hNIlj7HEI86fIcpObd7a0FcrxTWetlwJDGcceTlRvqc=
github.com/mitchellh/go-homedir v1.0.0/go.mod h1:SfyaCUpYCn1Vlf4IUYiD9fPX4A5wJrkLzIz1N1q0pr0=
Expand All @@ -421,6 +423,7 @@ github.com/mitchellh/mapstructure v1.1.2/go.mod h1:FVVH3fgwuzCH5S8UJGiWEs2h04kUh
github.com/moby/ipvs v1.0.1/go.mod h1:2pngiyseZbIKXNv7hsKj3O9UEz30c53MT9005gt2hxQ=
github.com/moby/spdystream v0.2.0 h1:cjW1zVyyoiM0T7b6UoySUFqzXMoqRckQtXwGPiBhOM8=
github.com/moby/spdystream v0.2.0/go.mod h1:f7i0iNDQJ059oMTcWxx8MA/zKFIuD/lY+0GqbN2Wy8c=
github.com/moby/sys/mountinfo v0.4.0 h1:1KInV3Huv18akCu58V7lzNlt+jFmqlu1EaErnEHE/VM=
github.com/moby/sys/mountinfo v0.4.0/go.mod h1:rEr8tzG/lsIZHBtN/JjGG+LMYx9eXgW2JI+6q0qou+A=
github.com/moby/term v0.0.0-20201216013528-df9cb8a40635/go.mod h1:FBS0z0QWA44HXygs7VXDUOGoN/1TV3RuWkLO04am3wc=
github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q=
Expand Down
57 changes: 57 additions & 0 deletions pkg/knit/cmd/machineinfo/machineinfo.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
/*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* Copyright 2020 Red Hat, Inc.
*/

package machineinfo

import (
"os"

"github.com/spf13/cobra"

"github.com/openshift-kni/debug-tools/pkg/knit/cmd"
"github.com/openshift-kni/debug-tools/pkg/machineinformer"
)

type machineInfoOptions struct {
handle machineinformer.Handle
}

func NewMachineInfoCommand(knitOpts *cmd.KnitOptions) *cobra.Command {
opts := &machineInfoOptions{
handle: machineinformer.Handle{
Out: os.Stdout,
},
}
mInfo := &cobra.Command{
Use: "machineinfo",
Short: "show cadvisor's machine info",
RunE: func(cmd *cobra.Command, args []string) error {
// we need to do this AFTER we parsed the flags
opts.handle.RootDirectory = knitOpts.SysFSRoot
return showMachineInfo(cmd, opts, args)
},
Args: cobra.MaximumNArgs(1),
}
mInfo.Flags().BoolVarP(&opts.handle.RawOutput, "raw-output", "X", false, "include machine-identifiable data")
mInfo.Flags().BoolVar(&opts.handle.CleanTimestamp, "clean-timestamp", false, "clean the timestamp (for testing purposes)")
mInfo.Flags().BoolVar(&opts.handle.CleanProcfsInfo, "clean-procfs-info", false, "clean the information coming for /proc (for testing purposes)")
return mInfo
}

func showMachineInfo(cmd *cobra.Command, opts *machineInfoOptions, args []string) error {
opts.handle.Run()
return nil
}
65 changes: 65 additions & 0 deletions pkg/machineinformer/fakefsinfo.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
/*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* Copyright 2021 Red Hat, Inc.
*/

package machineinformer

import (
"fmt"

"github.com/google/cadvisor/fs"
)

type fakeFsInfo struct {
notImplemented error
}

func newFakeFsInfo() fs.FsInfo {
return fakeFsInfo{
notImplemented: fmt.Errorf("not implemented"),
}
}

func (ffi fakeFsInfo) GetGlobalFsInfo() ([]fs.Fs, error) {
return nil, ffi.notImplemented
}

func (ffi fakeFsInfo) GetFsInfoForPath(mountSet map[string]struct{}) ([]fs.Fs, error) {
return nil, ffi.notImplemented
}

func (ffi fakeFsInfo) GetDirUsage(dir string) (fs.UsageInfo, error) {
return fs.UsageInfo{}, ffi.notImplemented
}

func (ffi fakeFsInfo) GetDeviceInfoByFsUUID(uuid string) (*fs.DeviceInfo, error) {
return nil, ffi.notImplemented
}

func (ffi fakeFsInfo) GetDirFsDevice(dir string) (*fs.DeviceInfo, error) {
return nil, ffi.notImplemented
}

func (ffi fakeFsInfo) GetDeviceForLabel(label string) (string, error) {
return "", ffi.notImplemented
}

func (ffi fakeFsInfo) GetLabelsForDevice(device string) ([]string, error) {
return nil, ffi.notImplemented
}

func (ffi fakeFsInfo) GetMountpointForDevice(device string) (string, error) {
return "", ffi.notImplemented
}
89 changes: 89 additions & 0 deletions pkg/machineinformer/informer.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
/*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* Copyright 2021 Red Hat, Inc.
*/

package machineinformer

import (
"encoding/json"
"io"
"time"

infov1 "github.com/google/cadvisor/info/v1"
"github.com/google/cadvisor/machine"
"k8s.io/klog/v2"
)

type Handle struct {
RootDirectory string
RawOutput bool
CleanTimestamp bool
CleanProcfsInfo bool
Out io.Writer
}

func (handle *Handle) Run() {
var err error
var info *infov1.MachineInfo

info, err = GetRaw(handle.RootDirectory)
if err != nil {
klog.Fatalf("Cannot get machine info: %v")
}

if !handle.RawOutput {
info = cleanInfo(info)
}
if handle.CleanProcfsInfo {
info.NumPhysicalCores = 0
info.CpuFrequency = 0
info.MemoryCapacity = 0
}
if handle.CleanTimestamp {
info.Timestamp = time.Date(2000, time.January, 1, 0, 0, 0, 0, time.UTC)
}

json.NewEncoder(handle.Out).Encode(info)
}

func GetRaw(root string) (*infov1.MachineInfo, error) {
fsInfo := newFakeFsInfo()
sysFs := NewRelocatableSysFs(root)
inHostNamespace := true

return machine.Info(sysFs, fsInfo, inHostNamespace)
}

func Get(root string) (*infov1.MachineInfo, error) {
info, err := GetRaw(root)
if err != nil {
return nil, err
}
return cleanInfo(info), nil
}

func cleanInfo(in *infov1.MachineInfo) *infov1.MachineInfo {
out := in.Clone()
out.MachineID = ""
out.SystemUUID = ""
out.BootID = ""
for i := 0; i < len(out.NetworkDevices); i++ {
out.NetworkDevices[i].MacAddress = ""
}
out.CloudProvider = infov1.UnknownProvider
out.InstanceType = infov1.UnknownInstance
out.InstanceID = infov1.UnNamedInstance
return out
}
Loading

0 comments on commit d68a039

Please sign in to comment.