Skip to content

Commit 6d64ed4

Browse files
authored
Merge pull request #347 from jumppad-labs/erik/k8s-cluster-resources
Add resources to k8s cluster
2 parents 8b18bfb + 764db95 commit 6d64ed4

File tree

2 files changed

+44
-9
lines changed

2 files changed

+44
-9
lines changed

pkg/config/resources/k8s/provider_cluster.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -459,6 +459,21 @@ func (p *ClusterProvider) createK3s(ctx context.Context) error {
459459
})
460460
}
461461

462+
if p.config.Resources != nil {
463+
cc.Resources = &ctypes.Resources{
464+
CPU: p.config.Resources.CPU,
465+
CPUPin: p.config.Resources.CPUPin,
466+
Memory: p.config.Resources.Memory,
467+
}
468+
469+
if p.config.Resources.GPU != nil {
470+
cc.Resources.GPU = &ctypes.GPU{
471+
Driver: p.config.Resources.GPU.Driver,
472+
DeviceIDs: p.config.Resources.GPU.DeviceIDs,
473+
}
474+
}
475+
}
476+
462477
cc.Command = args
463478

464479
id, err := p.client.CreateContainer(cc)

pkg/config/resources/k8s/resource_cluster.go

Lines changed: 29 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import (
55

66
"github.com/jumppad-labs/hclconfig/types"
77
"github.com/jumppad-labs/jumppad/pkg/config"
8-
ctypes "github.com/jumppad-labs/jumppad/pkg/config/resources/container"
8+
"github.com/jumppad-labs/jumppad/pkg/config/resources/container"
99
"github.com/jumppad-labs/jumppad/pkg/utils"
1010
)
1111

@@ -18,17 +18,29 @@ type Cluster struct {
1818
// embedded type holding name, etc.
1919
types.ResourceBase `hcl:",remain"`
2020

21-
Networks []ctypes.NetworkAttachment `hcl:"network,block" json:"networks,omitempty"` // Attach to the correct network // only when Image is specified
21+
Networks []container.NetworkAttachment `hcl:"network,block" json:"networks,omitempty"` // Attach to the correct network // only when Image is specified
2222

23-
Image *ctypes.Image `hcl:"image,block" json:"images,omitempty"` // optional image to use when creating the cluster
24-
Nodes int `hcl:"nodes,optional" json:"nodes,omitempty"`
25-
Volumes []ctypes.Volume `hcl:"volume,block" json:"volumes,omitempty"` // volumes to attach to the cluster
23+
Image *container.Image `hcl:"image,block" json:"images,omitempty"` // optional image to use when creating the cluster
24+
Nodes int `hcl:"nodes,optional" json:"nodes,omitempty"`
25+
Volumes []container.Volume `hcl:"volume,block" json:"volumes,omitempty"` // volumes to attach to the cluster
2626

2727
// Images that will be copied from the local docker cache to the cluster
28-
CopyImages []ctypes.Image `hcl:"copy_image,block" json:"copy_images,omitempty"`
28+
CopyImages []container.Image `hcl:"copy_image,block" json:"copy_images,omitempty"`
2929

30-
Ports []ctypes.Port `hcl:"port,block" json:"ports,omitempty"` // ports to expose
31-
PortRanges []ctypes.PortRange `hcl:"port_range,block" json:"port_ranges,omitempty"` // range of ports to expose
30+
Ports []container.Port `hcl:"port,block" json:"ports,omitempty"` // ports to expose
31+
PortRanges []container.PortRange `hcl:"port_range,block" json:"port_ranges,omitempty"` // range of ports to expose
32+
33+
/*
34+
Define resource constraints for the cluster
35+
36+
```hcl
37+
resources {
38+
cpu = 100
39+
memory = 1024
40+
}
41+
```
42+
*/
43+
Resources *container.Resources `hcl:"resources,block" json:"resources,omitempty"`
3244

3345
Environment map[string]string `hcl:"environment,optional" json:"environment,omitempty"` // environment variables to set when starting the container
3446

@@ -83,13 +95,20 @@ func (k *Cluster) Process() error {
8395
}
8496

8597
if k.Image == nil {
86-
k.Image = &ctypes.Image{Name: fmt.Sprintf("%s:%s", k3sBaseImage, k3sBaseVersion)}
98+
k.Image = &container.Image{Name: fmt.Sprintf("%s:%s", k3sBaseImage, k3sBaseVersion)}
8799
}
88100

89101
for i, v := range k.Volumes {
90102
k.Volumes[i].Source = utils.EnsureAbsolute(v.Source, k.Meta.File)
91103
}
92104

105+
if k.Resources == nil {
106+
k.Resources = &container.Resources{
107+
CPU: 500,
108+
Memory: 2024,
109+
}
110+
}
111+
93112
// do we have an existing resource in the state?
94113
// if so we need to set any computed resources for dependents
95114
c, err := config.LoadState()
@@ -104,6 +123,7 @@ func (k *Cluster) Process() error {
104123
k.ConnectorPort = kstate.ConnectorPort
105124
k.ExternalIP = kstate.ExternalIP
106125
k.KubeConfig = kstate.KubeConfig
126+
k.Resources = kstate.Resources
107127

108128
// add the network addresses
109129
for _, a := range kstate.Networks {

0 commit comments

Comments
 (0)