forked from metal3-io/baremetal-operator
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add new CRD for Firmware Components Update
Add a new CRD for the HostFirmwareComponents described in the Firmware Interface proposal - metal3-io/metal3-docs#364. Signed-off-by: Iury Gregory Melo Ferreira <imelofer@redhat.com>
- Loading branch information
1 parent
9ce684e
commit 8d79121
Showing
8 changed files
with
627 additions
and
0 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
118 changes: 118 additions & 0 deletions
118
apis/metal3.io/v1alpha1/hostfirmwarecomponents_types.go
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,118 @@ | ||
/* | ||
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 ( | ||
"fmt" | ||
|
||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
) | ||
|
||
// NOTE: json tags are required. Any new fields you add must have json tags for the fields to be serialized. | ||
|
||
// FirmwareUpdate defines a firmware update specification. | ||
type FirmwareUpdate struct { | ||
Name string `json:"name"` | ||
URL string `json:"url"` | ||
} | ||
|
||
// FirmwareComponentStatus defines the status of a firmware component. | ||
type FirmwareComponentStatus struct { | ||
Component string `json:"component"` | ||
InitialVersion string `json:"initialVersion"` | ||
CurrentVersion string `json:"currentVersion"` | ||
LastVersionFlashed string `json:"lastVersionFlashed"` | ||
UpdatedAt metav1.Time `json:"updatedAt"` | ||
} | ||
|
||
type UpdatesConditionType string | ||
|
||
const ( | ||
// Indicates that the updates in the Spec are different than Status | ||
HostFirmwareComponentsChangeDetected UpdatesConditionType = "ChangeDetected" | ||
|
||
// Indicates if the updates are valid and can be configured on the host | ||
HostFirmwareComponentsValid UpdatesConditionType = "Valid" | ||
) | ||
|
||
// HostFirmwareComponentsSpec defines the desired state of HostFirmwareComponents | ||
type HostFirmwareComponentsSpec struct { | ||
Updates []FirmwareUpdate `json:"updates"` | ||
} | ||
|
||
// HostFirmwareComponentsStatus defines the observed state of HostFirmwareComponents | ||
type HostFirmwareComponentsStatus struct { | ||
// Updates is the list of all firmware components that should be updated | ||
// they are specified via name and url fields. | ||
Updates []FirmwareUpdate `json:"updates"` | ||
|
||
// Components is the list of all available firmware components and their information. | ||
Components []FirmwareComponentStatus `json:"components"` | ||
|
||
// Time that the status was last updated | ||
// +optional | ||
LastUpdated *metav1.Time `json:"lastUpdated,omitempty"` | ||
|
||
// Track whether updates stored in the spec are valid based on the schema | ||
// +patchMergeKey=type | ||
// +patchStrategy=merge | ||
// +listType=map | ||
// +listMapKey=type | ||
// +optional | ||
Conditions []metav1.Condition `json:"conditions,omitempty" patchStrategy:"merge" patchMergeKey:"type" protobuf:"bytes,1,rep,name=conditions"` | ||
} | ||
|
||
//+kubebuilder:object:root=true | ||
//+kubebuilder:resource:shortName=hfc | ||
//+kubebuilder:subresource:status | ||
|
||
// HostFirmwareComponents is the Schema for the hostfirmwarecomponents API | ||
type HostFirmwareComponents struct { | ||
metav1.TypeMeta `json:",inline"` | ||
metav1.ObjectMeta `json:"metadata,omitempty"` | ||
|
||
Spec HostFirmwareComponentsSpec `json:"spec,omitempty"` | ||
Status HostFirmwareComponentsStatus `json:"status,omitempty"` | ||
} | ||
|
||
//+kubebuilder:object:root=true | ||
|
||
// HostFirmwareComponentsList contains a list of HostFirmwareComponents | ||
type HostFirmwareComponentsList struct { | ||
metav1.TypeMeta `json:",inline"` | ||
metav1.ListMeta `json:"metadata,omitempty"` | ||
Items []HostFirmwareComponents `json:"items"` | ||
} | ||
|
||
// Check whether the updates's names are valid | ||
func (host *HostFirmwareComponents) ValidateHostFirmwareComponents() error { | ||
|
||
allowedNames := map[string]struct{}{"bmc": {}, "bios": {}} | ||
for _, update := range host.Spec.Updates { | ||
name := update.Name | ||
if _, ok := allowedNames[name]; !ok { | ||
return fmt.Errorf("Component %s is invalid, only 'bmc' or 'bios' are allowed as update names", update.Name) | ||
} | ||
|
||
} | ||
|
||
return nil | ||
} | ||
|
||
func init() { | ||
SchemeBuilder.Register(&HostFirmwareComponents{}, &HostFirmwareComponentsList{}) | ||
} |
95 changes: 95 additions & 0 deletions
95
apis/metal3.io/v1alpha1/hostfirmwarecomponents_types_test.go
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,95 @@ | ||
package v1alpha1 | ||
|
||
import ( | ||
"testing" | ||
"time" | ||
|
||
"github.com/stretchr/testify/assert" | ||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
) | ||
|
||
func TestValidateHostFirmwareComponents(t *testing.T) { | ||
for _, tc := range []struct { | ||
Scenario string | ||
Components []FirmwareComponentStatus | ||
Updates []FirmwareUpdate | ||
LastUpdated *metav1.Time | ||
Conditions []metav1.Condition | ||
ExpectedError string | ||
}{ | ||
{ | ||
Scenario: "ValidHostFirmwareComponents", | ||
Components: []FirmwareComponentStatus{ | ||
{ | ||
Component: "bios", | ||
InitialVersion: "1.0", | ||
CurrentVersion: "1.0", | ||
UpdatedAt: metav1.NewTime(time.Now()), | ||
}, | ||
{ | ||
Component: "bmc", | ||
InitialVersion: "1.0", | ||
CurrentVersion: "2.0", | ||
LastVersionFlashed: "2.0", | ||
UpdatedAt: metav1.NewTime(time.Now()), | ||
}, | ||
}, | ||
Updates: []FirmwareUpdate{ | ||
{ | ||
Name: "bmc", | ||
URL: "https://example.com/bmcupdate", | ||
}, | ||
}, | ||
Conditions: []metav1.Condition{ | ||
{ | ||
Type: string(HostFirmwareComponentsChangeDetected), | ||
Status: metav1.ConditionTrue, | ||
LastTransitionTime: metav1.NewTime(time.Now()), | ||
}, | ||
}, | ||
ExpectedError: "", | ||
}, | ||
{ | ||
Scenario: "InvalidHostFirmwareComponents", | ||
Components: []FirmwareComponentStatus{}, | ||
Updates: []FirmwareUpdate{ | ||
{ | ||
Name: "nic", | ||
URL: "https://example.com/bmcupdate", | ||
}, | ||
}, | ||
Conditions: []metav1.Condition{ | ||
{ | ||
Type: string(HostFirmwareComponentsValid), | ||
Status: metav1.ConditionFalse, | ||
LastTransitionTime: metav1.NewTime(time.Now()), | ||
}, | ||
}, | ||
ExpectedError: "Component nic is invalid, only 'bmc' or 'bios' are allowed as update names", | ||
}, | ||
} { | ||
t.Run(tc.Scenario, func(t *testing.T) { | ||
hostFirmwareComponents := &HostFirmwareComponents{ | ||
ObjectMeta: metav1.ObjectMeta{ | ||
Name: "myhostfirmware", | ||
Namespace: "myns", | ||
}, | ||
Spec: HostFirmwareComponentsSpec{ | ||
Updates: tc.Updates, | ||
}, | ||
Status: HostFirmwareComponentsStatus{ | ||
Components: tc.Components, | ||
LastUpdated: tc.LastUpdated, | ||
Conditions: tc.Conditions, | ||
}, | ||
} | ||
|
||
err := hostFirmwareComponents.ValidateHostFirmwareComponents() | ||
if err == nil { | ||
assert.Equal(t, tc.ExpectedError, "") | ||
} else { | ||
assert.Equal(t, tc.ExpectedError, err.Error()) | ||
} | ||
}) | ||
} | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Oops, something went wrong.