github.com/anth0d/nomad@v0.0.0-20221214183521-ae3a0a2cad06/nomad/mock/node.go (about)

     1  package mock
     2  
     3  import (
     4  	"github.com/hashicorp/nomad/helper/uuid"
     5  	"github.com/hashicorp/nomad/nomad/structs"
     6  	psstructs "github.com/hashicorp/nomad/plugins/shared/structs"
     7  )
     8  
     9  func Node() *structs.Node {
    10  	node := &structs.Node{
    11  		ID:         uuid.Generate(),
    12  		SecretID:   uuid.Generate(),
    13  		Datacenter: "dc1",
    14  		Name:       "foobar",
    15  		Drivers: map[string]*structs.DriverInfo{
    16  			"exec": {
    17  				Detected: true,
    18  				Healthy:  true,
    19  			},
    20  			"mock_driver": {
    21  				Detected: true,
    22  				Healthy:  true,
    23  			},
    24  		},
    25  		Attributes: map[string]string{
    26  			"kernel.name":        "linux",
    27  			"arch":               "x86",
    28  			"nomad.version":      "0.5.0",
    29  			"driver.exec":        "1",
    30  			"driver.mock_driver": "1",
    31  			"consul.version":     "1.11.4",
    32  		},
    33  
    34  		// TODO Remove once clientv2 gets merged
    35  		Resources: &structs.Resources{
    36  			CPU:      4000,
    37  			MemoryMB: 8192,
    38  			DiskMB:   100 * 1024,
    39  		},
    40  		Reserved: &structs.Resources{
    41  			CPU:      100,
    42  			MemoryMB: 256,
    43  			DiskMB:   4 * 1024,
    44  			Networks: []*structs.NetworkResource{
    45  				{
    46  					Device:        "eth0",
    47  					IP:            "192.168.0.100",
    48  					ReservedPorts: []structs.Port{{Label: "ssh", Value: 22}},
    49  					MBits:         1,
    50  				},
    51  			},
    52  		},
    53  
    54  		NodeResources: &structs.NodeResources{
    55  			Cpu: structs.NodeCpuResources{
    56  				CpuShares: 4000,
    57  			},
    58  			Memory: structs.NodeMemoryResources{
    59  				MemoryMB: 8192,
    60  			},
    61  			Disk: structs.NodeDiskResources{
    62  				DiskMB: 100 * 1024,
    63  			},
    64  			Networks: []*structs.NetworkResource{
    65  				{
    66  					Mode:   "host",
    67  					Device: "eth0",
    68  					CIDR:   "192.168.0.100/32",
    69  					MBits:  1000,
    70  				},
    71  			},
    72  			NodeNetworks: []*structs.NodeNetworkResource{
    73  				{
    74  					Mode:   "host",
    75  					Device: "eth0",
    76  					Speed:  1000,
    77  					Addresses: []structs.NodeNetworkAddress{
    78  						{
    79  							Alias:   "default",
    80  							Address: "192.168.0.100",
    81  							Family:  structs.NodeNetworkAF_IPv4,
    82  						},
    83  					},
    84  				},
    85  			},
    86  		},
    87  		ReservedResources: &structs.NodeReservedResources{
    88  			Cpu: structs.NodeReservedCpuResources{
    89  				CpuShares: 100,
    90  			},
    91  			Memory: structs.NodeReservedMemoryResources{
    92  				MemoryMB: 256,
    93  			},
    94  			Disk: structs.NodeReservedDiskResources{
    95  				DiskMB: 4 * 1024,
    96  			},
    97  			Networks: structs.NodeReservedNetworkResources{
    98  				ReservedHostPorts: "22",
    99  			},
   100  		},
   101  		Links: map[string]string{
   102  			"consul": "foobar.dc1",
   103  		},
   104  		Meta: map[string]string{
   105  			"pci-dss":  "true",
   106  			"database": "mysql",
   107  			"version":  "5.6",
   108  		},
   109  		NodeClass:             "linux-medium-pci",
   110  		Status:                structs.NodeStatusReady,
   111  		SchedulingEligibility: structs.NodeSchedulingEligible,
   112  	}
   113  	_ = node.ComputeClass()
   114  	return node
   115  }
   116  
   117  func DrainNode() *structs.Node {
   118  	node := Node()
   119  	node.DrainStrategy = &structs.DrainStrategy{
   120  		DrainSpec: structs.DrainSpec{},
   121  	}
   122  	node.Canonicalize()
   123  	return node
   124  }
   125  
   126  // NvidiaNode returns a node with two instances of an Nvidia GPU
   127  func NvidiaNode() *structs.Node {
   128  	n := Node()
   129  	n.NodeResources.Devices = []*structs.NodeDeviceResource{
   130  		{
   131  			Type:   "gpu",
   132  			Vendor: "nvidia",
   133  			Name:   "1080ti",
   134  			Attributes: map[string]*psstructs.Attribute{
   135  				"memory":           psstructs.NewIntAttribute(11, psstructs.UnitGiB),
   136  				"cuda_cores":       psstructs.NewIntAttribute(3584, ""),
   137  				"graphics_clock":   psstructs.NewIntAttribute(1480, psstructs.UnitMHz),
   138  				"memory_bandwidth": psstructs.NewIntAttribute(11, psstructs.UnitGBPerS),
   139  			},
   140  			Instances: []*structs.NodeDevice{
   141  				{
   142  					ID:      uuid.Generate(),
   143  					Healthy: true,
   144  				},
   145  				{
   146  					ID:      uuid.Generate(),
   147  					Healthy: true,
   148  				},
   149  			},
   150  		},
   151  	}
   152  	_ = n.ComputeClass()
   153  	return n
   154  }