Skip to content

Commit

Permalink
Determine supported archs from the release manifest (#79)
Browse files Browse the repository at this point in the history
Signed-off-by: Atanas Dinov <[email protected]>
  • Loading branch information
atanasdinov committed Sep 11, 2024
1 parent 4c94ce9 commit 3ef7610
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 33 deletions.
44 changes: 24 additions & 20 deletions api/v1alpha1/releasemanifest_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,30 @@ const (
ArchTypeARM Arch = "aarch64"
)

var SupportedArchitectures = map[string]struct{}{
string(ArchTypeX86): {},
string(ArchTypeARM): {},
ArchTypeX86.Short(): {},
ArchTypeARM.Short(): {},
// +kubebuilder:validation:Enum=x86_64;aarch64
type Arch string

func (a Arch) Short() string {
switch a {
case ArchTypeX86:
return "amd64"
case ArchTypeARM:
return "arm64"
default:
message := fmt.Sprintf("unknown arch: %s", a)
panic(message)
}
}

func SupportedArchitectures(architectures []Arch) map[string]struct{} {
supportedArchitectures := map[string]struct{}{}

for _, a := range architectures {
supportedArchitectures[string(a)] = struct{}{}
supportedArchitectures[a.Short()] = struct{}{}
}

return supportedArchitectures
}

// ReleaseManifestSpec defines the desired state of ReleaseManifest
Expand Down Expand Up @@ -90,21 +109,6 @@ type OperatingSystem struct {
PrettyName string `json:"prettyName"`
}

// +kubebuilder:validation:Enum=x86_64;aarch64
type Arch string

func (a Arch) Short() string {
switch a {
case ArchTypeX86:
return "amd64"
case ArchTypeARM:
return "arm64"
default:
message := fmt.Sprintf("unknown arch: %s", a)
panic(message)
}
}

// +kubebuilder:object:root=true
// +kubebuilder:subresource:status

Expand Down
4 changes: 2 additions & 2 deletions internal/controller/reconcile_os.go
Original file line number Diff line number Diff line change
Expand Up @@ -114,11 +114,11 @@ func isOSUpgraded(nodeList *corev1.NodeList, selector labels.Selector, osPrettyN
return true
}

func findUnsupportedNodes(nodeList *corev1.NodeList) []string {
func findUnsupportedNodes(nodeList *corev1.NodeList, supportedArchitectures map[string]struct{}) []string {
var unsupported []string

for _, node := range nodeList.Items {
if _, ok := lifecyclev1alpha1.SupportedArchitectures[node.Status.NodeInfo.Architecture]; !ok {
if _, ok := supportedArchitectures[node.Status.NodeInfo.Architecture]; !ok {
unsupported = append(unsupported, node.Name)
}
}
Expand Down
6 changes: 5 additions & 1 deletion internal/controller/reconcile_os_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,17 @@ import (
"testing"

"github.com/stretchr/testify/assert"
lifecyclev1alpha1 "github.com/suse-edge/upgrade-controller/api/v1alpha1"

corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/labels"
)

func TestFindUnsupportedNodes(t *testing.T) {
supportedArchitectures := lifecyclev1alpha1.SupportedArchitectures(
[]lifecyclev1alpha1.Arch{lifecyclev1alpha1.ArchTypeARM, lifecyclev1alpha1.ArchTypeX86})

nodes := &corev1.NodeList{
Items: []corev1.Node{
{
Expand Down Expand Up @@ -39,7 +43,7 @@ func TestFindUnsupportedNodes(t *testing.T) {
},
}}

assert.Equal(t, []string{"node1", "node4"}, findUnsupportedNodes(nodes))
assert.Equal(t, []string{"node1", "node4"}, findUnsupportedNodes(nodes, supportedArchitectures))
}

func TestIsOSUpgraded(t *testing.T) {
Expand Down
21 changes: 11 additions & 10 deletions internal/controller/upgradeplan_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -162,12 +162,22 @@ func (r *UpgradePlanReconciler) reconcileDelete(ctx context.Context, upgradePlan
}

func (r *UpgradePlanReconciler) reconcileNormal(ctx context.Context, upgradePlan *lifecyclev1alpha1.UpgradePlan) (ctrl.Result, error) {
release, err := r.retrieveReleaseManifest(ctx, upgradePlan)
if err != nil {
if !errors.Is(err, errReleaseManifestNotFound) {
return ctrl.Result{}, fmt.Errorf("retrieving release manifest: %w", err)
}

return ctrl.Result{}, r.createReleaseManifest(ctx, upgradePlan)
}

nodeList := &corev1.NodeList{}
if err := r.List(ctx, nodeList); err != nil {
return ctrl.Result{}, fmt.Errorf("listing nodes: %w", err)
}

if unsupportedNodes := findUnsupportedNodes(nodeList); len(unsupportedNodes) > 0 {
supportedArchitectures := lifecyclev1alpha1.SupportedArchitectures(release.Spec.Components.OperatingSystem.SupportedArchs)
if unsupportedNodes := findUnsupportedNodes(nodeList, supportedArchitectures); len(unsupportedNodes) > 0 {
condition := metav1.Condition{
Type: lifecyclev1alpha1.ValidationFailedCondition,
Status: metav1.ConditionTrue,
Expand All @@ -179,15 +189,6 @@ func (r *UpgradePlanReconciler) reconcileNormal(ctx context.Context, upgradePlan
return ctrl.Result{}, nil
}

release, err := r.retrieveReleaseManifest(ctx, upgradePlan)
if err != nil {
if !errors.Is(err, errReleaseManifestNotFound) {
return ctrl.Result{}, fmt.Errorf("retrieving release manifest: %w", err)
}

return ctrl.Result{}, r.createReleaseManifest(ctx, upgradePlan)
}

if upgradePlan.Status.ObservedGeneration != upgradePlan.Generation {
suffix, err := upgrade.GenerateSuffix()
if err != nil {
Expand Down

0 comments on commit 3ef7610

Please sign in to comment.