|
| 1 | +//go:build windows |
| 2 | + |
| 3 | +package gcs |
| 4 | + |
| 5 | +import ( |
| 6 | + "encoding/json" |
| 7 | + "fmt" |
| 8 | + |
| 9 | + "github.com/Microsoft/hcsshim/internal/guest/prot" |
| 10 | + "github.com/Microsoft/hcsshim/internal/hcs/schema1" |
| 11 | +) |
| 12 | + |
| 13 | +// GuestDefinedCapabilities is an interface for different guest defined capabilities. |
| 14 | +// This allows us to define different capabilities by OS. |
| 15 | +// |
| 16 | +// When adding new fields to the implementations of type GuestDefinedCapabilities, a |
| 17 | +// new interface function should only be added if that capability describes a feature |
| 18 | +// available for both WCOW and LCOW. Otherwise, you can use the helper functions |
| 19 | +// GetWCOWCapabilities or GetLCOWCapabilities to check for capabilities specific to |
| 20 | +// one implementation. |
| 21 | +type GuestDefinedCapabilities interface { |
| 22 | + IsSignalProcessSupported() bool |
| 23 | + IsDeleteContainerStateSupported() bool |
| 24 | + IsDumpStacksSupported() bool |
| 25 | + IsNamespaceAddRequestSupported() bool |
| 26 | +} |
| 27 | + |
| 28 | +func GetWCOWCapabilities(gdc GuestDefinedCapabilities) *WCOWGuestDefinedCapabilities { |
| 29 | + g, ok := gdc.(*WCOWGuestDefinedCapabilities) |
| 30 | + if !ok { |
| 31 | + return nil |
| 32 | + } |
| 33 | + return g |
| 34 | +} |
| 35 | + |
| 36 | +func GetLCOWCapabilities(gdc GuestDefinedCapabilities) *LCOWGuestDefinedCapabilities { |
| 37 | + g, ok := gdc.(*LCOWGuestDefinedCapabilities) |
| 38 | + if !ok { |
| 39 | + return nil |
| 40 | + } |
| 41 | + return g |
| 42 | +} |
| 43 | + |
| 44 | +func unmarshalGuestCapabilities(os string, data json.RawMessage) (GuestDefinedCapabilities, error) { |
| 45 | + if os == "windows" { |
| 46 | + gdc := &WCOWGuestDefinedCapabilities{} |
| 47 | + if err := json.Unmarshal(data, gdc); err != nil { |
| 48 | + return nil, fmt.Errorf("unmarshal returned GuestDefinedCapabilities for windows: %w", err) |
| 49 | + } |
| 50 | + return gdc, nil |
| 51 | + } |
| 52 | + // linux |
| 53 | + gdc := &LCOWGuestDefinedCapabilities{} |
| 54 | + if err := json.Unmarshal(data, gdc); err != nil { |
| 55 | + return nil, fmt.Errorf("unmarshal returned GuestDefinedCapabilities for lcow: %w", err) |
| 56 | + } |
| 57 | + return gdc, nil |
| 58 | +} |
| 59 | + |
| 60 | +var _ GuestDefinedCapabilities = &LCOWGuestDefinedCapabilities{} |
| 61 | + |
| 62 | +type LCOWGuestDefinedCapabilities struct { |
| 63 | + prot.GcsGuestCapabilities |
| 64 | +} |
| 65 | + |
| 66 | +func (l *LCOWGuestDefinedCapabilities) IsNamespaceAddRequestSupported() bool { |
| 67 | + return l.NamespaceAddRequestSupported |
| 68 | +} |
| 69 | + |
| 70 | +func (l *LCOWGuestDefinedCapabilities) IsSignalProcessSupported() bool { |
| 71 | + return l.SignalProcessSupported |
| 72 | +} |
| 73 | + |
| 74 | +func (l *LCOWGuestDefinedCapabilities) IsDumpStacksSupported() bool { |
| 75 | + return l.DumpStacksSupported |
| 76 | +} |
| 77 | + |
| 78 | +func (l *LCOWGuestDefinedCapabilities) IsDeleteContainerStateSupported() bool { |
| 79 | + return l.DeleteContainerStateSupported |
| 80 | +} |
| 81 | + |
| 82 | +var _ GuestDefinedCapabilities = &WCOWGuestDefinedCapabilities{} |
| 83 | + |
| 84 | +type WCOWGuestDefinedCapabilities struct { |
| 85 | + schema1.GuestDefinedCapabilities |
| 86 | +} |
| 87 | + |
| 88 | +func (w *WCOWGuestDefinedCapabilities) IsNamespaceAddRequestSupported() bool { |
| 89 | + return w.NamespaceAddRequestSupported |
| 90 | +} |
| 91 | + |
| 92 | +func (w *WCOWGuestDefinedCapabilities) IsSignalProcessSupported() bool { |
| 93 | + return w.SignalProcessSupported |
| 94 | +} |
| 95 | + |
| 96 | +func (w *WCOWGuestDefinedCapabilities) IsDumpStacksSupported() bool { |
| 97 | + return w.DumpStacksSupported |
| 98 | +} |
| 99 | + |
| 100 | +func (w *WCOWGuestDefinedCapabilities) IsDeleteContainerStateSupported() bool { |
| 101 | + return w.DeleteContainerStateSupported |
| 102 | +} |
0 commit comments