-
Notifications
You must be signed in to change notification settings - Fork 10
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 #35 from fromanirh/add-machineinfo-command
add machineinfo command
- Loading branch information
Showing
9 changed files
with
663 additions
and
2 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
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,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 | ||
} |
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,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 | ||
} |
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,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 | ||
} |
Oops, something went wrong.