-
Notifications
You must be signed in to change notification settings - Fork 177
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: generate a file per resourcePool
Signed-off-by: Soule BA <[email protected]>
- Loading branch information
Showing
5 changed files
with
135 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
/* | ||
* SPDX-FileCopyrightText: Copyright (c) 2024 NVIDIA CORPORATION & AFFILIATES. All rights reserved. | ||
* SPDX-License-Identifier: Apache-2.0 | ||
* | ||
* 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 ( | ||
"encoding/json" | ||
"fmt" | ||
"strings" | ||
|
||
cdiSpecs "github.com/container-orchestrated-devices/container-device-interface/specs-go" | ||
"github.com/opencontainers/go-digest" | ||
) | ||
|
||
// Digest returns the digest of the given CDI spec. | ||
func Digest(spec cdiSpecs.Spec) digest.Digest { | ||
digester := digest.Canonical.Digester() | ||
enc := json.NewEncoder(digester.Hash()) | ||
if err := enc.Encode(spec); err != nil { | ||
return "" | ||
} | ||
return digester.Digest() | ||
} | ||
|
||
func extractEncodedFromDigest(digest string) (string, error) { | ||
var encoded string | ||
// expects a digest in the <algorithm>:<encoded> format | ||
if pair := strings.Split(digest, ":"); len(pair) != 2 { | ||
return "", fmt.Errorf("invalid digest %s", digest) | ||
} else { | ||
encoded = pair[1] | ||
} | ||
if len(encoded) < 12 { | ||
return "", fmt.Errorf("invalid digest %s", digest) | ||
} | ||
return encoded[0:12], nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
/* | ||
* SPDX-FileCopyrightText: Copyright (c) 2024 NVIDIA CORPORATION & AFFILIATES. All rights reserved. | ||
* SPDX-License-Identifier: Apache-2.0 | ||
* | ||
* 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_test | ||
|
||
import ( | ||
. "github.com/onsi/ginkgo" | ||
. "github.com/onsi/gomega" | ||
|
||
"github.com/container-orchestrated-devices/container-device-interface/specs-go" | ||
"github.com/k8snetworkplumbingwg/sriov-network-device-plugin/pkg/cdi" | ||
) | ||
|
||
var _ = Describe("Digest", func() { | ||
Context("successfully create digest", func() { | ||
It("should return digest", func() { | ||
spec := specs.Spec{ | ||
Version: specs.CurrentVersion, | ||
Kind: "nvidia.com/net-pci", | ||
Devices: []specs.Device{ | ||
{ | ||
ContainerEdits: specs.ContainerEdits{ | ||
DeviceNodes: []*specs.DeviceNode{ | ||
{ | ||
Path: "/dev/infiniband/issm0", | ||
HostPath: "/dev/infiniband/issm0", | ||
Permissions: "rw", | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
} | ||
got := cdi.Digest(spec) | ||
Expect(got.String()).To(Equal("sha256:250959bdd6c927d5eb06860bdbbd974011c3c26f06980aef3c0621a7c027140d")) | ||
}) | ||
}) | ||
}) |