Skip to content

Commit

Permalink
Merge pull request #83 from klihub/fixes/build/windows
Browse files Browse the repository at this point in the history
pkg/cdi: fix build/vet failures on windows.
  • Loading branch information
elezar committed Sep 27, 2022
2 parents a80a40e + 584db3c commit 5609688
Show file tree
Hide file tree
Showing 8 changed files with 188 additions and 37 deletions.
49 changes: 48 additions & 1 deletion .github/workflows/sanity.yml
Original file line number Diff line number Diff line change
@@ -1,14 +1,26 @@
on: [push, pull_request]
name: Sanity

env:
GO_VERSION: '1.17.x'

jobs:
build:
name: "Run go sanity tools"
runs-on: ubuntu-latest
strategy:
fail-fast: false
matrix:
include:
- goos: linux
goarch: amd64
- goos: windows
goarch: amd64
steps:
- uses: actions/checkout@v2
- uses: actions/setup-go@v2
with:
go-version: 1.17.x
go-version: ${{ env.GO_VERSION }}
- name: Install golint
run: go get -u golang.org/x/lint/golint
- name: Lint
Expand All @@ -17,5 +29,40 @@ jobs:
run: make fmt
- name: Vet
run: make vet

test:
name: "Run tests"
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- uses: actions/setup-go@v2
with:
go-version: ${{ env.GO_VERSION }}
- name: Test
run: make test


# Make sure binaries compile on multiple platforms.
crossbuild:
name: "Build / Crossbuild Binaries"
runs-on: ubuntu-20.04
strategy:
fail-fast: false
matrix:
include:
- goos: linux
goarch: amd64
- goos: windows
goarch: amd64
steps:
- uses: actions/checkout@v2
- uses: actions/setup-go@v2
with:
go-version: ${{ env.GO_VERSION }}
- name: Build
env:
GOOS: ${{matrix.goos}}
GOARCH: ${{matrix.goarch}}
run: |
env | grep ^GO
make
5 changes: 2 additions & 3 deletions pkg/cdi/cache_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ import (
"path/filepath"
"strings"
"sync"
"syscall"
"testing"
"time"

Expand Down Expand Up @@ -770,10 +769,10 @@ devices:
for {
select {
case _ = <-stopCh:
go syscall.Sync()
go osSync()
return
case _ = <-sync.C:
go syscall.Sync()
go osSync()
sync.Reset(2 * time.Second)
}
}
Expand Down
26 changes: 26 additions & 0 deletions pkg/cdi/cache_test_unix.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
//go:build !windows
// +build !windows

/*
Copyright © 2021 The CDI Authors
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package cdi

import "syscall"

func osSync() {
syscall.Sync()
}
22 changes: 22 additions & 0 deletions pkg/cdi/cache_test_windows.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
//go:build windows
// +build windows

/*
Copyright © 2021 The CDI Authors
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package cdi

func osSync() {}
33 changes: 0 additions & 33 deletions pkg/cdi/container-edits.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,6 @@ import (
"github.com/container-orchestrated-devices/container-device-interface/specs-go"
oci "github.com/opencontainers/runtime-spec/specs-go"
ocigen "github.com/opencontainers/runtime-tools/generate"

runc "github.com/opencontainers/runc/libcontainer/devices"
)

const (
Expand Down Expand Up @@ -289,37 +287,6 @@ func ensureOCIHooks(spec *oci.Spec) {
}
}

// fillMissingInfo fills in missing mandatory attributes from the host device.
func (d *DeviceNode) fillMissingInfo() error {
if d.HostPath == "" {
d.HostPath = d.Path
}

if d.Type != "" && (d.Major != 0 || d.Type == "p") {
return nil
}

hostDev, err := runc.DeviceFromPath(d.HostPath, "rwm")
if err != nil {
return errors.Wrapf(err, "failed to stat CDI host device %q", d.HostPath)
}

if d.Type == "" {
d.Type = string(hostDev.Type)
} else {
if d.Type != string(hostDev.Type) {
return errors.Errorf("CDI device (%q, %q), host type mismatch (%s, %s)",
d.Path, d.HostPath, d.Type, string(hostDev.Type))
}
}
if d.Major == 0 && d.Type != "p" {
d.Major = hostDev.Major
d.Minor = hostDev.Minor
}

return nil
}

// sortMounts sorts the mounts in the given OCI Spec.
func sortMounts(specgen *ocigen.Generator) {
mounts := specgen.Mounts()
Expand Down
56 changes: 56 additions & 0 deletions pkg/cdi/container-edits_unix.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
//go:build !windows
// +build !windows

/*
Copyright © 2021 The CDI Authors
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package cdi

import (
runc "github.com/opencontainers/runc/libcontainer/devices"
"github.com/pkg/errors"
)

// fillMissingInfo fills in missing mandatory attributes from the host device.
func (d *DeviceNode) fillMissingInfo() error {
if d.HostPath == "" {
d.HostPath = d.Path
}

if d.Type != "" && (d.Major != 0 || d.Type == "p") {
return nil
}

hostDev, err := runc.DeviceFromPath(d.HostPath, "rwm")
if err != nil {
return errors.Wrapf(err, "failed to stat CDI host device %q", d.HostPath)
}

if d.Type == "" {
d.Type = string(hostDev.Type)
} else {
if d.Type != string(hostDev.Type) {
return errors.Errorf("CDI device (%q, %q), host type mismatch (%s, %s)",
d.Path, d.HostPath, d.Type, string(hostDev.Type))
}
}
if d.Major == 0 && d.Type != "p" {
d.Major = hostDev.Major
d.Minor = hostDev.Minor
}

return nil
}
27 changes: 27 additions & 0 deletions pkg/cdi/container-edits_windows.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
//go:build windows
// +build windows

/*
Copyright © 2021 The CDI Authors
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package cdi

import "fmt"

// fillMissingInfo fills in missing mandatory attributes from the host device.
func (d *DeviceNode) fillMissingInfo() error {
return fmt.Errorf("unimplemented")
}
7 changes: 7 additions & 0 deletions pkg/cdi/spec.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
"io/ioutil"
"os"
"path/filepath"
"sync"

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

// Externally set CDI Spec validation function.
specValidator func(*cdi.Spec) error
validatorLock sync.RWMutex
)

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

// validateSpec validates the Spec using the extneral validator.
func validateSpec(raw *cdi.Spec) error {
validatorLock.RLock()
defer validatorLock.RUnlock()

if specValidator == nil {
return nil
}
Expand Down

0 comments on commit 5609688

Please sign in to comment.