Skip to content

Commit 5609688

Browse files
authored
Merge pull request #83 from klihub/fixes/build/windows
pkg/cdi: fix build/vet failures on windows.
2 parents a80a40e + 584db3c commit 5609688

File tree

8 files changed

+188
-37
lines changed

8 files changed

+188
-37
lines changed

.github/workflows/sanity.yml

Lines changed: 48 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,26 @@
11
on: [push, pull_request]
22
name: Sanity
3+
4+
env:
5+
GO_VERSION: '1.17.x'
6+
37
jobs:
48
build:
59
name: "Run go sanity tools"
610
runs-on: ubuntu-latest
11+
strategy:
12+
fail-fast: false
13+
matrix:
14+
include:
15+
- goos: linux
16+
goarch: amd64
17+
- goos: windows
18+
goarch: amd64
719
steps:
820
- uses: actions/checkout@v2
921
- uses: actions/setup-go@v2
1022
with:
11-
go-version: 1.17.x
23+
go-version: ${{ env.GO_VERSION }}
1224
- name: Install golint
1325
run: go get -u golang.org/x/lint/golint
1426
- name: Lint
@@ -17,5 +29,40 @@ jobs:
1729
run: make fmt
1830
- name: Vet
1931
run: make vet
32+
33+
test:
34+
name: "Run tests"
35+
runs-on: ubuntu-latest
36+
steps:
37+
- uses: actions/checkout@v2
38+
- uses: actions/setup-go@v2
39+
with:
40+
go-version: ${{ env.GO_VERSION }}
2041
- name: Test
2142
run: make test
43+
44+
45+
# Make sure binaries compile on multiple platforms.
46+
crossbuild:
47+
name: "Build / Crossbuild Binaries"
48+
runs-on: ubuntu-20.04
49+
strategy:
50+
fail-fast: false
51+
matrix:
52+
include:
53+
- goos: linux
54+
goarch: amd64
55+
- goos: windows
56+
goarch: amd64
57+
steps:
58+
- uses: actions/checkout@v2
59+
- uses: actions/setup-go@v2
60+
with:
61+
go-version: ${{ env.GO_VERSION }}
62+
- name: Build
63+
env:
64+
GOOS: ${{matrix.goos}}
65+
GOARCH: ${{matrix.goarch}}
66+
run: |
67+
env | grep ^GO
68+
make

pkg/cdi/cache_test.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@ import (
2121
"path/filepath"
2222
"strings"
2323
"sync"
24-
"syscall"
2524
"testing"
2625
"time"
2726

@@ -770,10 +769,10 @@ devices:
770769
for {
771770
select {
772771
case _ = <-stopCh:
773-
go syscall.Sync()
772+
go osSync()
774773
return
775774
case _ = <-sync.C:
776-
go syscall.Sync()
775+
go osSync()
777776
sync.Reset(2 * time.Second)
778777
}
779778
}

pkg/cdi/cache_test_unix.go

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
//go:build !windows
2+
// +build !windows
3+
4+
/*
5+
Copyright © 2021 The CDI Authors
6+
7+
Licensed under the Apache License, Version 2.0 (the "License");
8+
you may not use this file except in compliance with the License.
9+
You may obtain a copy of the License at
10+
11+
http://www.apache.org/licenses/LICENSE-2.0
12+
13+
Unless required by applicable law or agreed to in writing, software
14+
distributed under the License is distributed on an "AS IS" BASIS,
15+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16+
See the License for the specific language governing permissions and
17+
limitations under the License.
18+
*/
19+
20+
package cdi
21+
22+
import "syscall"
23+
24+
func osSync() {
25+
syscall.Sync()
26+
}

pkg/cdi/cache_test_windows.go

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
//go:build windows
2+
// +build windows
3+
4+
/*
5+
Copyright © 2021 The CDI Authors
6+
7+
Licensed under the Apache License, Version 2.0 (the "License");
8+
you may not use this file except in compliance with the License.
9+
You may obtain a copy of the License at
10+
11+
http://www.apache.org/licenses/LICENSE-2.0
12+
13+
Unless required by applicable law or agreed to in writing, software
14+
distributed under the License is distributed on an "AS IS" BASIS,
15+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16+
See the License for the specific language governing permissions and
17+
limitations under the License.
18+
*/
19+
20+
package cdi
21+
22+
func osSync() {}

pkg/cdi/container-edits.go

Lines changed: 0 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,6 @@ import (
2727
"github.com/container-orchestrated-devices/container-device-interface/specs-go"
2828
oci "github.com/opencontainers/runtime-spec/specs-go"
2929
ocigen "github.com/opencontainers/runtime-tools/generate"
30-
31-
runc "github.com/opencontainers/runc/libcontainer/devices"
3230
)
3331

3432
const (
@@ -289,37 +287,6 @@ func ensureOCIHooks(spec *oci.Spec) {
289287
}
290288
}
291289

292-
// fillMissingInfo fills in missing mandatory attributes from the host device.
293-
func (d *DeviceNode) fillMissingInfo() error {
294-
if d.HostPath == "" {
295-
d.HostPath = d.Path
296-
}
297-
298-
if d.Type != "" && (d.Major != 0 || d.Type == "p") {
299-
return nil
300-
}
301-
302-
hostDev, err := runc.DeviceFromPath(d.HostPath, "rwm")
303-
if err != nil {
304-
return errors.Wrapf(err, "failed to stat CDI host device %q", d.HostPath)
305-
}
306-
307-
if d.Type == "" {
308-
d.Type = string(hostDev.Type)
309-
} else {
310-
if d.Type != string(hostDev.Type) {
311-
return errors.Errorf("CDI device (%q, %q), host type mismatch (%s, %s)",
312-
d.Path, d.HostPath, d.Type, string(hostDev.Type))
313-
}
314-
}
315-
if d.Major == 0 && d.Type != "p" {
316-
d.Major = hostDev.Major
317-
d.Minor = hostDev.Minor
318-
}
319-
320-
return nil
321-
}
322-
323290
// sortMounts sorts the mounts in the given OCI Spec.
324291
func sortMounts(specgen *ocigen.Generator) {
325292
mounts := specgen.Mounts()

pkg/cdi/container-edits_unix.go

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
//go:build !windows
2+
// +build !windows
3+
4+
/*
5+
Copyright © 2021 The CDI Authors
6+
7+
Licensed under the Apache License, Version 2.0 (the "License");
8+
you may not use this file except in compliance with the License.
9+
You may obtain a copy of the License at
10+
11+
http://www.apache.org/licenses/LICENSE-2.0
12+
13+
Unless required by applicable law or agreed to in writing, software
14+
distributed under the License is distributed on an "AS IS" BASIS,
15+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16+
See the License for the specific language governing permissions and
17+
limitations under the License.
18+
*/
19+
20+
package cdi
21+
22+
import (
23+
runc "github.com/opencontainers/runc/libcontainer/devices"
24+
"github.com/pkg/errors"
25+
)
26+
27+
// fillMissingInfo fills in missing mandatory attributes from the host device.
28+
func (d *DeviceNode) fillMissingInfo() error {
29+
if d.HostPath == "" {
30+
d.HostPath = d.Path
31+
}
32+
33+
if d.Type != "" && (d.Major != 0 || d.Type == "p") {
34+
return nil
35+
}
36+
37+
hostDev, err := runc.DeviceFromPath(d.HostPath, "rwm")
38+
if err != nil {
39+
return errors.Wrapf(err, "failed to stat CDI host device %q", d.HostPath)
40+
}
41+
42+
if d.Type == "" {
43+
d.Type = string(hostDev.Type)
44+
} else {
45+
if d.Type != string(hostDev.Type) {
46+
return errors.Errorf("CDI device (%q, %q), host type mismatch (%s, %s)",
47+
d.Path, d.HostPath, d.Type, string(hostDev.Type))
48+
}
49+
}
50+
if d.Major == 0 && d.Type != "p" {
51+
d.Major = hostDev.Major
52+
d.Minor = hostDev.Minor
53+
}
54+
55+
return nil
56+
}

pkg/cdi/container-edits_windows.go

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
//go:build windows
2+
// +build windows
3+
4+
/*
5+
Copyright © 2021 The CDI Authors
6+
7+
Licensed under the Apache License, Version 2.0 (the "License");
8+
you may not use this file except in compliance with the License.
9+
You may obtain a copy of the License at
10+
11+
http://www.apache.org/licenses/LICENSE-2.0
12+
13+
Unless required by applicable law or agreed to in writing, software
14+
distributed under the License is distributed on an "AS IS" BASIS,
15+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16+
See the License for the specific language governing permissions and
17+
limitations under the License.
18+
*/
19+
20+
package cdi
21+
22+
import "fmt"
23+
24+
// fillMissingInfo fills in missing mandatory attributes from the host device.
25+
func (d *DeviceNode) fillMissingInfo() error {
26+
return fmt.Errorf("unimplemented")
27+
}

pkg/cdi/spec.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import (
2121
"io/ioutil"
2222
"os"
2323
"path/filepath"
24+
"sync"
2425

2526
oci "github.com/opencontainers/runtime-spec/specs-go"
2627
"github.com/pkg/errors"
@@ -41,6 +42,7 @@ var (
4142

4243
// Externally set CDI Spec validation function.
4344
specValidator func(*cdi.Spec) error
45+
validatorLock sync.RWMutex
4446
)
4547

4648
// Spec represents a single CDI Spec. It is usually loaded from a
@@ -249,11 +251,16 @@ func ParseSpec(data []byte) (*cdi.Spec, error) {
249251
// is used for extra CDI Spec content validation whenever a Spec file
250252
// loaded (using ReadSpec() or NewSpec()) or written (Spec.Write()).
251253
func SetSpecValidator(fn func(*cdi.Spec) error) {
254+
validatorLock.Lock()
255+
defer validatorLock.Unlock()
252256
specValidator = fn
253257
}
254258

255259
// validateSpec validates the Spec using the extneral validator.
256260
func validateSpec(raw *cdi.Spec) error {
261+
validatorLock.RLock()
262+
defer validatorLock.RUnlock()
263+
257264
if specValidator == nil {
258265
return nil
259266
}

0 commit comments

Comments
 (0)