Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Introduce nfd client for image compatibilty #1932

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ IMAGE_BUILD_ARGS_MINIMAL = --target minimal \

all: image

BUILD_BINARIES := nfd-master nfd-worker nfd-topology-updater nfd-gc kubectl-nfd
BUILD_BINARIES := nfd-master nfd-worker nfd-topology-updater nfd-gc kubectl-nfd nfd

build-%:
$(GO_CMD) build -v -o bin/ $(BUILD_FLAGS) ./cmd/$*
Expand Down
48 changes: 48 additions & 0 deletions api/image-compatibility/v1alpha1/spec.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
/*
Copyright 2024 The Kubernetes 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 v1alpha1

import (
nfdv1alpha1 "sigs.k8s.io/node-feature-discovery/api/nfd/v1alpha1"
)

// ArtifactType is a type of OCI artifact that contains image compatibility metadata.
const (
ArtifactType = "application/vnd.nfd.image-compatibility.v1alpha1"
Version = "v1alpha1"
)

// Spec represents image compatibility metadata.
type Spec struct {
// Version of the spec.
Version string `json:"version"`
// Compatibilities contains list of compatibility sets.
Compatibilties []Compatibility `json:"compatibilities"`
}

// Compatibility represents image compatibility metadata
// that describe the image requirements for the host and OS.
type Compatibility struct {
// Rules represents a list of Node Feature Rules.
Rules []nfdv1alpha1.Rule `json:"rules"`
// Weight indicates the priority of the compatibility set.
Weight int `json:"weight,omitempty"`
// Tag enables grouping or distinguishing between compatibility sets.
Tag string `json:"tag,omitempty"`
// Description of the compatibility set.
Description string `json:"description,omitempty"`
}
31 changes: 31 additions & 0 deletions api/nfd/v1alpha1/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@ limitations under the License.
package v1alpha1

import (
"encoding/json"
"fmt"

corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)
Expand Down Expand Up @@ -296,6 +299,34 @@ type MatchExpression struct {
// In other cases Value should contain at least one element.
// +optional
Value MatchValue `json:"value,omitempty"`

// IsMatch indicates whether the processed expression matches the host.
// This field should not be exposed in the CRD or used by users.
// It is for informational purposes only.
IsMatch bool `json:"-"`
}

// Override MarshalJSON to hide the IsMatch field,
// preventing users from accessing it and avoiding potential API confusion.
// This field is returned only after rule processing to indicate if the expression matched the host.
func (m *MatchExpression) MarshalJSON() ([]byte, error) {
if m.IsMatch {
return json.Marshal(struct {
MatchExpression
IsMatch bool `json:"isMatch"`
}{
*m, m.IsMatch,
})
}

return json.Marshal(*m)
}

func (m *MatchExpression) String() string {
if len(m.Value) < 1 {
return fmt.Sprintf("{op: %q}", m.Op)
}
return fmt.Sprintf("{op: %q, value: %q}", m.Op, m.Value)
}

// MatchOp is the match operator that is applied on values when evaluating a
Expand Down
27 changes: 27 additions & 0 deletions cmd/nfd/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
/*
Copyright 2024 The Kubernetes 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 main

import (
"sigs.k8s.io/node-feature-discovery/cmd/nfd/subcmd"
)

const ProgramName = "nfd"

func main() {
subcmd.Execute()
}
36 changes: 36 additions & 0 deletions cmd/nfd/subcmd/compat/compat.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/*
Copyright 2024 The Kubernetes 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 compat

import (
"fmt"
"os"

"github.com/spf13/cobra"
)

var CompatCmd = &cobra.Command{
Use: "compat",
Short: "Image compatibility commands",
}

func Execute() {
if err := CompatCmd.Execute(); err != nil {
fmt.Println(err)
os.Exit(1)
}
}
70 changes: 70 additions & 0 deletions cmd/nfd/subcmd/compat/options/platform.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
/*
Copyright 2024 The Kubernetes 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 options

import (
"fmt"
"runtime"
"strings"

ocispec "github.com/opencontainers/image-spec/specs-go/v1"
"github.com/spf13/cobra"
)

// PlatformOption represents
type PlatformOption struct {
// PlatformStr contains the raw platform argument provided by the user.
PlatformStr string
// Platform represents the OCI platform specification, built from PlatformStr.
Platform *ocispec.Platform
}

// Parse takes the PlatformStr argument provided by the user
// to build OCI platform specification.
func (opt *PlatformOption) Parse(*cobra.Command) error {
var pStr string

if opt.PlatformStr == "" {
return nil
}

platform := &ocispec.Platform{}
pStr, platform.OSVersion, _ = strings.Cut(opt.PlatformStr, ":")
parts := strings.Split(pStr, "/")

switch len(parts) {
case 3:
platform.Variant = parts[2]
fallthrough
case 2:
platform.Architecture = parts[1]
case 1:
platform.Architecture = runtime.GOARCH
default:
return fmt.Errorf("failed to parse platform %q: expected format os[/arch[/variant]]", opt.PlatformStr)
}

platform.OS = parts[0]
if platform.OS == "" {
return fmt.Errorf("invalid platform: OS cannot be empty")
}
if platform.Architecture == "" {
return fmt.Errorf("invalid platform: Architecture cannot be empty")
}
opt.Platform = platform
return nil
}
Loading