Skip to content

Commit 5074919

Browse files
yevgeny-shnaidmank8s-ci-robot
authored andcommitted
Adding a preflight package
This package is going to be used by the Preflight controller to perform manipulation on the in-memory PreflightValidation object. The main functionalities: setting the status of the veirfied module, checking the status of veirfied module, and checking if all the modules in the cluster has already been verified
1 parent afc51f4 commit 5074919

File tree

3 files changed

+209
-0
lines changed

3 files changed

+209
-0
lines changed

internal/preflight/preflight.go

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
package preflight
2+
3+
import (
4+
"github.com/kubernetes-sigs/kernel-module-management/api/v1beta2"
5+
6+
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
7+
)
8+
9+
const VerificationStatusReasonVerified = "Verification successful (%s), this Module will not be verified again in this Preflight CR"
10+
11+
//go:generate mockgen -source=preflight.go -package=preflight -destination=mock_preflight.go
12+
13+
type PreflightAPI interface {
14+
SetModuleStatus(pv *v1beta2.PreflightValidation, namespace, name, status, reason string)
15+
GetModuleStatus(pv *v1beta2.PreflightValidation, namespace, name string) string
16+
AllModulesVerified(pv *v1beta2.PreflightValidation) bool
17+
}
18+
19+
func NewPreflightAPI() PreflightAPI {
20+
return &preflight{}
21+
}
22+
23+
type preflight struct {
24+
}
25+
26+
func (p *preflight) SetModuleStatus(pv *v1beta2.PreflightValidation, namespace, name, status, reason string) {
27+
stage := v1beta2.VerificationStageImage
28+
if status == v1beta2.VerificationSuccess || status == v1beta2.VerificationFailure {
29+
stage = v1beta2.VerificationStageDone
30+
}
31+
newStatus := v1beta2.PreflightValidationModuleStatus{
32+
Name: name,
33+
Namespace: namespace,
34+
CRBaseStatus: v1beta2.CRBaseStatus{
35+
VerificationStatus: status,
36+
StatusReason: reason,
37+
VerificationStage: stage,
38+
LastTransitionTime: metav1.Now(),
39+
},
40+
}
41+
for i, moduleStatus := range pv.Status.Modules {
42+
if moduleStatus.Name == name && moduleStatus.Namespace == namespace {
43+
pv.Status.Modules[i] = newStatus
44+
return
45+
}
46+
}
47+
48+
pv.Status.Modules = append(pv.Status.Modules, newStatus)
49+
}
50+
51+
func (p *preflight) GetModuleStatus(pv *v1beta2.PreflightValidation, namespace, name string) string {
52+
for _, moduleStatus := range pv.Status.Modules {
53+
if moduleStatus.Name == name && moduleStatus.Namespace == namespace {
54+
return moduleStatus.VerificationStatus
55+
}
56+
}
57+
return ""
58+
}
59+
60+
func (p *preflight) AllModulesVerified(pv *v1beta2.PreflightValidation) bool {
61+
for _, moduleStatus := range pv.Status.Modules {
62+
if !(moduleStatus.VerificationStatus == v1beta2.VerificationSuccess || moduleStatus.VerificationStatus == v1beta2.VerificationFailure) {
63+
return false
64+
}
65+
}
66+
return true
67+
}
Lines changed: 120 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,120 @@
1+
package preflight
2+
3+
import (
4+
"github.com/kubernetes-sigs/kernel-module-management/api/v1beta2"
5+
. "github.com/onsi/ginkgo/v2"
6+
. "github.com/onsi/gomega"
7+
)
8+
9+
var _ = Describe("SetModuleStatus", func() {
10+
var (
11+
preflightAPI PreflightAPI
12+
pv *v1beta2.PreflightValidation
13+
)
14+
15+
BeforeEach(func() {
16+
preflightAPI = NewPreflightAPI()
17+
pv = &v1beta2.PreflightValidation{
18+
Status: v1beta2.PreflightValidationStatus{
19+
Modules: []v1beta2.PreflightValidationModuleStatus{
20+
{
21+
Name: "test-name",
22+
Namespace: "test-namespace",
23+
CRBaseStatus: v1beta2.CRBaseStatus{
24+
VerificationStatus: "original status",
25+
StatusReason: "original reason",
26+
},
27+
},
28+
},
29+
},
30+
}
31+
})
32+
33+
It("should set the existing module status correctly", func() {
34+
preflightAPI.SetModuleStatus(pv, "test-namespace", "test-name", "some status", "some reason")
35+
Expect(pv.Status.Modules).To(HaveLen(1))
36+
Expect(pv.Status.Modules[0].VerificationStatus).To(Equal("some status"))
37+
Expect(pv.Status.Modules[0].StatusReason).To(Equal("some reason"))
38+
})
39+
40+
It("should set the new module status correctly", func() {
41+
preflightAPI.SetModuleStatus(pv, "test-namespace", "new-name", "new status", "new reason")
42+
Expect(pv.Status.Modules).To(HaveLen(2))
43+
Expect(pv.Status.Modules[1].Name).To(Equal("new-name"))
44+
Expect(pv.Status.Modules[1].VerificationStatus).To(Equal("new status"))
45+
Expect(pv.Status.Modules[1].StatusReason).To(Equal("new reason"))
46+
})
47+
})
48+
49+
var _ = Describe("GetModuleStatus", func() {
50+
var (
51+
preflightAPI PreflightAPI
52+
pv *v1beta2.PreflightValidation
53+
)
54+
55+
BeforeEach(func() {
56+
preflightAPI = NewPreflightAPI()
57+
pv = &v1beta2.PreflightValidation{
58+
Status: v1beta2.PreflightValidationStatus{
59+
Modules: []v1beta2.PreflightValidationModuleStatus{
60+
{
61+
Name: "test-name",
62+
Namespace: "test-namespace",
63+
CRBaseStatus: v1beta2.CRBaseStatus{
64+
VerificationStatus: "original status",
65+
StatusReason: "original reason",
66+
},
67+
},
68+
},
69+
},
70+
}
71+
})
72+
73+
It("should return the status of existing module", func() {
74+
res := preflightAPI.GetModuleStatus(pv, "test-namespace", "test-name")
75+
Expect(res).To(Equal("original status"))
76+
})
77+
78+
It("should return empty string for non-existing module", func() {
79+
res := preflightAPI.GetModuleStatus(pv, "test-namespace", "non-existing-name")
80+
Expect(res).To(Equal(""))
81+
})
82+
})
83+
84+
var _ = Describe("AllModulesVerified", func() {
85+
var (
86+
preflightAPI PreflightAPI
87+
pv *v1beta2.PreflightValidation
88+
)
89+
90+
BeforeEach(func() {
91+
preflightAPI = NewPreflightAPI()
92+
pv = &v1beta2.PreflightValidation{
93+
Status: v1beta2.PreflightValidationStatus{
94+
Modules: []v1beta2.PreflightValidationModuleStatus{
95+
{
96+
Name: "test-name1",
97+
Namespace: "test-namespace1",
98+
},
99+
{
100+
Name: "test-name2",
101+
Namespace: "test-namespace2",
102+
},
103+
},
104+
},
105+
}
106+
})
107+
108+
It("should return true if all modules are verified", func() {
109+
pv.Status.Modules[0].VerificationStatus = v1beta2.VerificationSuccess
110+
pv.Status.Modules[1].VerificationStatus = v1beta2.VerificationFailure
111+
Expect(preflightAPI.AllModulesVerified(pv)).To(BeTrue())
112+
113+
})
114+
115+
It("should return false if any module is not verified", func() {
116+
pv.Status.Modules[0].VerificationStatus = v1beta2.VerificationInProgress
117+
pv.Status.Modules[1].VerificationStatus = v1beta2.VerificationFailure
118+
Expect(preflightAPI.AllModulesVerified(pv)).To(BeFalse())
119+
})
120+
})

internal/preflight/suite_test.go

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package preflight
2+
3+
import (
4+
"testing"
5+
6+
"github.com/kubernetes-sigs/kernel-module-management/internal/test"
7+
. "github.com/onsi/ginkgo/v2"
8+
. "github.com/onsi/gomega"
9+
"k8s.io/apimachinery/pkg/runtime"
10+
)
11+
12+
var scheme *runtime.Scheme
13+
14+
func TestSuite(t *testing.T) {
15+
RegisterFailHandler(Fail)
16+
17+
var err error
18+
scheme, err = test.TestScheme()
19+
Expect(err).NotTo(HaveOccurred())
20+
21+
RunSpecs(t, "Preflight Suite")
22+
}

0 commit comments

Comments
 (0)