Skip to content

Commit

Permalink
chore: add domainname and annotations
Browse files Browse the repository at this point in the history
Signed-off-by: Arjun Raja Yogidas <arjunry@amazon.com>
  • Loading branch information
coderbirju committed Feb 25, 2025
1 parent d051aca commit 303a9df
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 12 deletions.
19 changes: 10 additions & 9 deletions api/handlers/container/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,15 +107,14 @@ func (h *handler) create(w http.ResponseWriter, r *http.Request) {
}
}

// Annotations: TODO - available in nerdctl 2.0
// Annotations are passed in as a map of strings,
// but nerdctl expects an array of strings with format [annotations1=VALUE1, annotations2=VALUE2, ...].
// annotations := []string{}
// if req.HostConfig.Annotations != nil {
// for key, val := range req.HostConfig.Annotations {
// annotations = append(annotations, fmt.Sprintf("%s=%s", key, val))
// }
// }
annotations := []string{}
if req.HostConfig.Annotations != nil {
for key, val := range req.HostConfig.Annotations {
annotations = append(annotations, fmt.Sprintf("%s=%s", key, val))
}
}

ulimits := []string{}
if req.HostConfig.Ulimits != nil {
Expand Down Expand Up @@ -322,8 +321,9 @@ func (h *handler) create(w http.ResponseWriter, r *http.Request) {
// #endregion

// #region for metadata flags
Name: name, // container name
Label: labels, // container labels
Name: name, // container name
Label: labels, // container labels
Annotations: annotations,
// #endregion

// #region for logging flags
Expand Down Expand Up @@ -365,6 +365,7 @@ func (h *handler) create(w http.ResponseWriter, r *http.Request) {
}
netOpt := ncTypes.NetworkOptions{
Hostname: req.Hostname,
Domainname: req.Domainname,

Check failure on line 368 in api/handlers/container/create.go

View workflow job for this annotation

GitHub Actions / build

unknown field Domainname in struct literal of type "github.com/containerd/nerdctl/v2/pkg/api/types".NetworkOptions
NetworkSlice: []string{networkMode},
DNSServers: req.HostConfig.DNS, // Custom DNS lookup servers.
DNSResolvConfOptions: dnsOpt, // DNS options.
Expand Down
33 changes: 33 additions & 0 deletions api/handlers/container/create_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -302,6 +302,7 @@ var _ = Describe("Container Create API ", func() {
body := []byte(`{
"Image": "test-image",
"Hostname": "test-host",
"Domainname": "",
"HostConfig": {
"DNS": ["8.8.8.8"],
"DNSOptions": ["test-opt"],
Expand All @@ -313,6 +314,38 @@ var _ = Describe("Container Create API ", func() {

// expected network options
netOpt.Hostname = "test-host"
netOpt.Domainname = ""
netOpt.DNSServers = []string{"8.8.8.8"}
netOpt.DNSResolvConfOptions = []string{"test-opt"}
netOpt.DNSSearchDomains = []string{"test.com"}
netOpt.AddHost = []string{"test-host:127.0.0.1"}

service.EXPECT().Create(gomock.Any(), "test-image", nil, equalTo(createOpt), equalTo(netOpt)).Return(
cid, nil)

// handler should return success message with 201 status code.
h.create(rr, req)
Expect(rr).Should(HaveHTTPStatus(http.StatusCreated))
Expect(rr.Body).Should(MatchJSON(jsonResponse))
})

It("should set specified domainname", func() {
body := []byte(`{
"Image": "test-image",
"Hostname": "test-host",
"Domainname": "test.com",
"HostConfig": {
"DNS": ["8.8.8.8"],
"DNSOptions": ["test-opt"],
"DNSSearch": ["test.com"],
"ExtraHosts": ["test-host:127.0.0.1"]
}
}`)
req, _ := http.NewRequest(http.MethodPost, "/containers/create", bytes.NewReader(body))

// expected network options
netOpt.Hostname = "test-host"
netOpt.Domainname = "test.com"
netOpt.DNSServers = []string{"8.8.8.8"}
netOpt.DNSResolvConfOptions = []string{"test-opt"}
netOpt.DNSSearchDomains = []string{"test.com"}
Expand Down
6 changes: 3 additions & 3 deletions api/types/container_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@ type AttachOptions struct {

// ContainerConfig is from https://github.com/moby/moby/blob/v24.0.2/api/types/container/config.go#L64-L96
type ContainerConfig struct {
Hostname string `json:",omitempty"` // Hostname
// TODO: Domainname string // Domainname
Hostname string `json:",omitempty"` // Hostname
Domainname string `json:",omitempty"` // Domainname
User string `json:",omitempty"` // User that will run the command(s) inside the container, also support user:group
AttachStdin bool // Attach the standard input, makes possible user interaction
// TODO: AttachStdout bool // Attach the standard output
Expand Down Expand Up @@ -68,7 +68,7 @@ type ContainerHostConfig struct {
VolumesFrom []string // List of volumes to take from other container
// TODO: VolumeDriver string // Name of the volume driver used to mount volumes
// TODO: ConsoleSize [2]uint // Initial console size (height,width)
// TODO: Annotations map[string]string `json:",omitempty"` // Arbitrary non-identifying metadata attached to container and provided to the runtime
Annotations map[string]string `json:",omitempty"` // Arbitrary non-identifying metadata attached to container and provided to the runtime

// Applicable to UNIX platforms
CapAdd []string // List of kernel capabilities to add to the container
Expand Down

0 comments on commit 303a9df

Please sign in to comment.